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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

matrix.c 6.6KB

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