matrix.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. Copyright 2019 MechMerlin <mechmerlin@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <util/delay.h>
  15. #include <avr/io.h>
  16. #include <stdio.h>
  17. #include "matrix.h"
  18. #include "util.h"
  19. #include "print.h"
  20. #include "debug.h"
  21. static uint8_t debouncing = DEBOUNCE;
  22. /* matrix state(1:on, 0:off) */
  23. static matrix_row_t matrix[MATRIX_ROWS];
  24. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  25. static uint8_t read_rows(uint8_t col);
  26. static void init_rows(void);
  27. static void unselect_cols(void);
  28. static void select_col(uint8_t col);
  29. __attribute__ ((weak))
  30. void matrix_init_kb(void) {
  31. matrix_init_user();
  32. }
  33. __attribute__ ((weak))
  34. void matrix_scan_kb(void) {
  35. matrix_scan_user();
  36. }
  37. __attribute__ ((weak))
  38. void matrix_init_user(void) {
  39. }
  40. __attribute__ ((weak))
  41. void matrix_scan_user(void) {
  42. }
  43. void backlight_init_ports(void)
  44. {
  45. // DDRD |= 0b11010000;
  46. // PORTD &= ~0b01010000;
  47. // PORTD |= 0b10000000;
  48. // DDRB |= 0b00011111;
  49. // PORTB &= ~0b00001110;
  50. // PORTB |= 0b00010001;
  51. // DDRE |= 0b01000000;
  52. // PORTE &= ~0b01000000;
  53. }
  54. void matrix_init(void) {
  55. backlight_init_ports();
  56. unselect_cols();
  57. init_rows();
  58. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  59. matrix[i] = 0;
  60. matrix_debouncing[i] = 0;
  61. }
  62. matrix_init_quantum();
  63. }
  64. uint8_t matrix_scan(void) {
  65. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  66. select_col(col);
  67. _delay_us(3);
  68. uint8_t rows = read_rows(col);
  69. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  70. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  71. bool curr_bit = rows & (1<<row);
  72. if (prev_bit != curr_bit) {
  73. matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
  74. if (debouncing) {
  75. dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
  76. }
  77. debouncing = DEBOUNCE;
  78. }
  79. }
  80. unselect_cols();
  81. }
  82. if (debouncing) {
  83. if (--debouncing) {
  84. _delay_ms(1);
  85. } else {
  86. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  87. matrix[i] = matrix_debouncing[i];
  88. }
  89. }
  90. }
  91. matrix_scan_quantum();
  92. return 1;
  93. }
  94. inline matrix_row_t matrix_get_row(uint8_t row) {
  95. return matrix[row];
  96. }
  97. void matrix_print(void) {
  98. print("\nr/c 0123456789ABCDEF\n");
  99. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  100. xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
  101. }
  102. }
  103. /* Row pin configuration
  104. * row: 0 1 2 3 4 5
  105. * pin: PB7 PD0 PD1 PD2 PD3 PD5
  106. *
  107. * Reset Key uses its own pin PE2
  108. */
  109. static void init_rows(void) {
  110. DDRD &= ~0b00101111;
  111. PORTD &= ~0b00101111;
  112. DDRB &= ~0b10000000;
  113. PORTB &= ~0b10000000;
  114. DDRE &= ~0b00000100;
  115. PORTE |= 0b00000100;
  116. }
  117. static uint8_t read_rows(uint8_t col) {
  118. if (col == 19) {
  119. return PINE&(1<<2) ? 0 : (1<<0);
  120. } else {
  121. return (PIND&(1<<0) ? (1<<1) : 0) |
  122. (PIND&(1<<1) ? (1<<2) : 0) |
  123. (PIND&(1<<2) ? (1<<3) : 0) |
  124. (PIND&(1<<3) ? (1<<4) : 0) |
  125. (PIND&(1<<5) ? (1<<5) : 0) |
  126. (PINB&(1<<7) ? (1<<0) : 0);
  127. }
  128. }
  129. /* Columns 0 - G
  130. * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
  131. *
  132. * atmega32u4 decoder pin
  133. * C6 U1 E2
  134. * B6 U2 E2
  135. * F0 U1, U2 A0
  136. * F1 U1, U2 A1
  137. * C7 U1, U2 A2
  138. *
  139. * col 0: B4
  140. * col 1: D7
  141. * col I: B5
  142. *
  143. * col / pin: PC6 PB6 PF0 PF1 PC7 Decoder Pin
  144. * 2: 1 0 0 0 0 U1 Y0
  145. * 3: 1 0 1 0 0 U1 Y1
  146. * 4: 1 0 0 1 0 U1 Y2
  147. * 5: 1 0 1 1 0 U1 Y3
  148. * 6: 1 0 0 0 1 U1 Y4
  149. * 7: 1 0 1 0 1 U1 Y5
  150. * 8: 1 0 0 1 1 U1 Y6
  151. * 9: 1 0 1 1 1 U1 Y7
  152. * A: 0 1 0 0 0 U2 Y0
  153. * B: 0 1 1 0 0 U2 Y1
  154. * C: 0 1 0 1 0 U2 Y2
  155. * D: 0 1 1 1 0 U2 Y3
  156. * E: 0 1 0 0 1 U2 Y4
  157. * F: 0 1 1 0 1 U2 Y5
  158. * G: 0 1 0 1 1 U2 Y6
  159. * H: 0 1 1 1 1 U2 Y7
  160. *
  161. */
  162. static void unselect_cols(void) {
  163. DDRB |= 0b01110000;
  164. PORTB &= ~0b01110000;
  165. DDRC |= 0b11000000;
  166. PORTC &= ~0b11000000;
  167. DDRD |= 0b10000000;
  168. PORTD &= ~0b10000000;
  169. DDRF |= 0b00000011;
  170. PORTF &= ~0b00000011;
  171. }
  172. static void select_col(uint8_t col) {
  173. switch (col) {
  174. case 0:
  175. PORTB |= 0b00010000;
  176. break;
  177. case 1:
  178. PORTD |= 0b10000000;
  179. break;
  180. case 2: // U1 Y0
  181. PORTC |= 0b01000000;
  182. break;
  183. case 3: // U1 Y1
  184. PORTC |= 0b01000000;
  185. PORTF |= 0b00000001;
  186. break;
  187. case 4: // U1 Y2
  188. PORTC |= 0b01000000;
  189. PORTF |= 0b00000010;
  190. break;
  191. case 5: // U1 Y3
  192. PORTC |= 0b01000000;
  193. PORTF |= 0b00000011;
  194. break;
  195. case 6: // U1 Y4
  196. PORTC |= 0b11000000;
  197. break;
  198. case 7: // U1 Y5
  199. PORTC |= 0b11000000;
  200. PORTF |= 0b00000001;
  201. break;
  202. case 8: // U1 Y6
  203. PORTC |= 0b11000000;
  204. PORTF |= 0b00000010;
  205. break;
  206. case 9: // U1 Y7
  207. PORTC |= 0b11000000;
  208. PORTF |= 0b00000011;
  209. break;
  210. case 10: // U2 Y0
  211. PORTB |= 0b01000000;
  212. break;
  213. case 11: // U2 Y1
  214. PORTB |= 0b01000000;
  215. PORTF |= 0b00000001;
  216. break;
  217. case 12: // U2 Y2
  218. PORTB |= 0b01000000;
  219. PORTF |= 0b00000010;
  220. break;
  221. case 13: // U2 Y3
  222. PORTB |= 0b01000000;
  223. PORTF |= 0b00000011;
  224. break;
  225. case 14: // U2 Y4
  226. PORTB |= 0b01000000;
  227. PORTC |= 0b10000000;
  228. break;
  229. case 15: // U2 Y5
  230. PORTB |= 0b01000000;
  231. PORTC |= 0b10000000;
  232. PORTF |= 0b00000001;
  233. break;
  234. case 16: // U2 Y6
  235. PORTB |= 0b01000000;
  236. PORTC |= 0b10000000;
  237. PORTF |= 0b00000010;
  238. break;
  239. case 17: // U2 Y7
  240. PORTB |= 0b01000000;
  241. PORTC |= 0b10000000;
  242. PORTF |= 0b00000011;
  243. break;
  244. case 18:
  245. PORTB |= 0b00100000;
  246. break;
  247. }
  248. }