matrix.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* Copyright 2012 Jun Wako <wakojun@gmail.com>
  2. *
  3. * This is heavily based on phantom/board.{c|h}.
  4. * https://github.com/BathroomEpiphanies/AVR-Keyboard
  5. *
  6. * Copyright (c) 2012 Fredrik Atmer, Bathroom Epiphanies Inc
  7. * http://bathroomepiphanies.com
  8. *
  9. * As for liscensing consult with the original files or its author.
  10. */
  11. #include <stdint.h>
  12. #include <stdbool.h>
  13. #include <avr/io.h>
  14. #include <util/delay.h>
  15. #include "print.h"
  16. #include "debug.h"
  17. #include "util.h"
  18. #include "matrix.h"
  19. #ifndef DEBOUNCE
  20. # define DEBOUNCE 0
  21. #endif
  22. static uint8_t debouncing = DEBOUNCE;
  23. // bit array of key state(1:on, 0:off)
  24. static matrix_row_t matrix[MATRIX_ROWS];
  25. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  26. static uint8_t read_rows(void);
  27. static void init_rows(void);
  28. static void unselect_cols(void);
  29. static void select_col(uint8_t col);
  30. /* LEDs are on output compare pins OC1B OC1C
  31. This activates fast PWM mode on them.
  32. Prescaler 256 and 8-bit counter results in
  33. 16000000/256/256 = 244 Hz blink frequency.
  34. LED_A: Caps Lock
  35. LED_B: Scroll Lock */
  36. /* Output on PWM pins are turned off when the timer
  37. reaches the value in the output compare register,
  38. and are turned on when it reaches TOP (=256). */
  39. static
  40. void setup_leds(void) {
  41. TCCR1A |= // Timer control register 1A
  42. (1<<WGM10) | // Fast PWM 8-bit
  43. (1<<COM1B1)| // Clear OC1B on match, set at TOP
  44. (1<<COM1C1); // Clear OC1C on match, set at TOP
  45. TCCR1B |= // Timer control register 1B
  46. (1<<WGM12) | // Fast PWM 8-bit
  47. (1<<CS12); // Prescaler 256
  48. OCR1B = 250; // Output compare register 1B
  49. OCR1C = 250; // Output compare register 1C
  50. // LEDs: LED_A -> PORTB6, LED_B -> PORTB7
  51. DDRB &= 0x3F;
  52. PORTB &= 0x3F;
  53. }
  54. inline
  55. uint8_t matrix_rows(void)
  56. {
  57. return MATRIX_ROWS;
  58. }
  59. inline
  60. uint8_t matrix_cols(void)
  61. {
  62. return MATRIX_COLS;
  63. }
  64. void matrix_init(void)
  65. {
  66. // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
  67. MCUCR |= (1<<JTD);
  68. MCUCR |= (1<<JTD);
  69. // initialize row and col
  70. unselect_cols();
  71. init_rows();
  72. setup_leds();
  73. // initialize matrix state: all keys off
  74. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  75. matrix[i] = 0;
  76. matrix_debouncing[i] = 0;
  77. }
  78. }
  79. uint8_t matrix_scan(void)
  80. {
  81. for (uint8_t col = 0; col < MATRIX_COLS; col++) { // 0-16
  82. select_col(col);
  83. _delay_us(3); // without this wait it won't read stable value.
  84. uint8_t rows = read_rows();
  85. for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // 0-5
  86. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  87. bool curr_bit = rows & (1<<row);
  88. if (prev_bit != curr_bit) {
  89. matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
  90. if (debouncing) {
  91. debug("bounce!: "); debug_hex(debouncing); print("\n");
  92. }
  93. debouncing = DEBOUNCE;
  94. }
  95. }
  96. unselect_cols();
  97. }
  98. if (debouncing) {
  99. if (--debouncing) {
  100. _delay_ms(1);
  101. } else {
  102. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  103. matrix[i] = matrix_debouncing[i];
  104. }
  105. }
  106. }
  107. return 1;
  108. }
  109. bool matrix_is_modified(void)
  110. {
  111. if (debouncing) return false;
  112. return true;
  113. }
  114. inline
  115. bool matrix_is_on(uint8_t row, uint8_t col)
  116. {
  117. return (matrix[row] & ((matrix_row_t)1<<col));
  118. }
  119. inline
  120. matrix_row_t matrix_get_row(uint8_t row)
  121. {
  122. return matrix[row];
  123. }
  124. void matrix_print(void)
  125. {
  126. print("\nr/c 0123456789ABCDEF\n");
  127. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  128. phex(row); print(": ");
  129. print_bin_reverse32(matrix_get_row(row));
  130. print("\n");
  131. }
  132. }
  133. uint8_t matrix_key_count(void)
  134. {
  135. uint8_t count = 0;
  136. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  137. count += bitpop32(matrix[i]);
  138. }
  139. return count;
  140. }
  141. /* Row pin configuration
  142. * row: 0 1 2 3 4 5
  143. * pin: B0 B1 B2 B3 B4 B5
  144. */
  145. static void init_rows(void)
  146. {
  147. // Input with pull-up(DDR:0, PORT:1)
  148. DDRB &= ~0b00111111;
  149. PORTB |= 0b00111111;
  150. }
  151. static uint8_t read_rows(void)
  152. {
  153. return (PINB&(1<<0) ? 0 : (1<<0)) |
  154. (PINB&(1<<1) ? 0 : (1<<1)) |
  155. (PINB&(1<<2) ? 0 : (1<<2)) |
  156. (PINB&(1<<3) ? 0 : (1<<3)) |
  157. (PINB&(1<<4) ? 0 : (1<<4)) |
  158. (PINB&(1<<5) ? 0 : (1<<5));
  159. }
  160. /* Column pin configuration
  161. * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  162. * pin: D5 C7 C6 D4 D0 E6 F0 F1 F4 F5 F6 F7 D7 D6 D1 D2 D3
  163. */
  164. static void unselect_cols(void)
  165. {
  166. // Hi-Z(DDR:0, PORT:0) to unselect
  167. DDRC |= 0b11000000; // PC: 7 6
  168. PORTC |= 0b11000000;
  169. DDRD |= 0b11111111; // PD: 7 6 5 4 3 2 1 0
  170. PORTD |= 0b11111111;
  171. DDRE |= 0b01000000; // PE: 6
  172. PORTE |= 0b01000000;
  173. DDRF |= 0b11110011; // PF: 7 6 5 4 1 0
  174. PORTF |= 0b11110011;
  175. }
  176. static void select_col(uint8_t col)
  177. {
  178. // Output low(DDR:1, PORT:0) to select
  179. switch (col) {
  180. case 0:
  181. DDRD |= (1<<5);
  182. PORTD &= ~(1<<5);
  183. break;
  184. case 1:
  185. DDRC |= (1<<7);
  186. PORTC &= ~(1<<7);
  187. break;
  188. case 2:
  189. DDRC |= (1<<6);
  190. PORTC &= ~(1<<6);
  191. break;
  192. case 3:
  193. DDRD |= (1<<4);
  194. PORTD &= ~(1<<4);
  195. break;
  196. case 4:
  197. DDRD |= (1<<0);
  198. PORTD &= ~(1<<0);
  199. break;
  200. case 5:
  201. DDRE |= (1<<6);
  202. PORTE &= ~(1<<6);
  203. break;
  204. case 6:
  205. DDRF |= (1<<0);
  206. PORTF &= ~(1<<0);
  207. break;
  208. case 7:
  209. DDRF |= (1<<1);
  210. PORTF &= ~(1<<1);
  211. break;
  212. case 8:
  213. DDRF |= (1<<4);
  214. PORTF &= ~(1<<4);
  215. break;
  216. case 9:
  217. DDRF |= (1<<5);
  218. PORTF &= ~(1<<5);
  219. break;
  220. case 10:
  221. DDRF |= (1<<6);
  222. PORTF &= ~(1<<6);
  223. break;
  224. case 11:
  225. DDRF |= (1<<7);
  226. PORTF &= ~(1<<7);
  227. break;
  228. case 12:
  229. DDRD |= (1<<7);
  230. PORTD &= ~(1<<7);
  231. break;
  232. case 13:
  233. DDRD |= (1<<6);
  234. PORTD &= ~(1<<6);
  235. break;
  236. case 14:
  237. DDRD |= (1<<1);
  238. PORTD &= ~(1<<1);
  239. break;
  240. case 15:
  241. DDRD |= (1<<2);
  242. PORTD &= ~(1<<2);
  243. break;
  244. case 16:
  245. DDRD |= (1<<3);
  246. PORTD &= ~(1<<3);
  247. break;
  248. }
  249. }