Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

matrix.c 6.5KB

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