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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
  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. xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
  130. }
  131. }
  132. uint8_t matrix_key_count(void)
  133. {
  134. uint8_t count = 0;
  135. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  136. count += bitpop32(matrix[i]);
  137. }
  138. return count;
  139. }
  140. /* Row pin configuration
  141. * row: 0 1 2 3 4 5
  142. * pin: B5 B4 B3 B2 B1 B0
  143. */
  144. static void init_rows(void)
  145. {
  146. // Input with pull-up(DDR:0, PORT:1)
  147. DDRB &= ~0b00111111;
  148. PORTB |= 0b00111111;
  149. }
  150. static uint8_t read_rows(void)
  151. {
  152. return (PINB&(1<<5) ? 0 : (1<<0)) |
  153. (PINB&(1<<4) ? 0 : (1<<1)) |
  154. (PINB&(1<<3) ? 0 : (1<<2)) |
  155. (PINB&(1<<2) ? 0 : (1<<3)) |
  156. (PINB&(1<<1) ? 0 : (1<<4)) |
  157. (PINB&(1<<0) ? 0 : (1<<5));
  158. }
  159. /* Column pin configuration
  160. * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  161. * pin: D5 C7 C6 D4 D0 E6 F0 F1 F4 F5 F6 F7 D7 D6 D1 D2 D3
  162. */
  163. static void unselect_cols(void)
  164. {
  165. // Hi-Z(DDR:0, PORT:0) to unselect
  166. DDRC |= 0b11000000; // PC: 7 6
  167. PORTC |= 0b11000000;
  168. DDRD |= 0b11111111; // PD: 7 6 5 4 3 2 1 0
  169. PORTD |= 0b11111111;
  170. DDRE |= 0b01000000; // PE: 6
  171. PORTE |= 0b01000000;
  172. DDRF |= 0b11110011; // PF: 7 6 5 4 1 0
  173. PORTF |= 0b11110011;
  174. }
  175. static void select_col(uint8_t col)
  176. {
  177. // Output low(DDR:1, PORT:0) to select
  178. switch (col) {
  179. case 0:
  180. DDRD |= (1<<5);
  181. PORTD &= ~(1<<5);
  182. break;
  183. case 1:
  184. DDRC |= (1<<7);
  185. PORTC &= ~(1<<7);
  186. break;
  187. case 2:
  188. DDRC |= (1<<6);
  189. PORTC &= ~(1<<6);
  190. break;
  191. case 3:
  192. DDRD |= (1<<4);
  193. PORTD &= ~(1<<4);
  194. break;
  195. case 4:
  196. DDRD |= (1<<0);
  197. PORTD &= ~(1<<0);
  198. break;
  199. case 5:
  200. DDRE |= (1<<6);
  201. PORTE &= ~(1<<6);
  202. break;
  203. case 6:
  204. DDRF |= (1<<0);
  205. PORTF &= ~(1<<0);
  206. break;
  207. case 7:
  208. DDRF |= (1<<1);
  209. PORTF &= ~(1<<1);
  210. break;
  211. case 8:
  212. DDRF |= (1<<4);
  213. PORTF &= ~(1<<4);
  214. break;
  215. case 9:
  216. DDRF |= (1<<5);
  217. PORTF &= ~(1<<5);
  218. break;
  219. case 10:
  220. DDRF |= (1<<6);
  221. PORTF &= ~(1<<6);
  222. break;
  223. case 11:
  224. DDRF |= (1<<7);
  225. PORTF &= ~(1<<7);
  226. break;
  227. case 12:
  228. DDRD |= (1<<7);
  229. PORTD &= ~(1<<7);
  230. break;
  231. case 13:
  232. DDRD |= (1<<6);
  233. PORTD &= ~(1<<6);
  234. break;
  235. case 14:
  236. DDRD |= (1<<1);
  237. PORTD &= ~(1<<1);
  238. break;
  239. case 15:
  240. DDRD |= (1<<2);
  241. PORTD &= ~(1<<2);
  242. break;
  243. case 16:
  244. DDRD |= (1<<3);
  245. PORTD &= ~(1<<3);
  246. break;
  247. }
  248. }