2
0

matrix.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. Copyright 2017 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 - diode connected
  104. * row: 0 1 2 3 4
  105. * pin: PD0 PD1 PD2 PD3 PD5
  106. *
  107. * Caps Lock uses its own pin PE2 on the column pin, row pin is grounded
  108. */
  109. static void init_rows(void) {
  110. DDRD &= ~0b00101111;
  111. PORTD &= ~0b00101111;
  112. DDRE &= ~0b00000100;
  113. PORTE |= 0b00000100;
  114. }
  115. static uint8_t read_rows(uint8_t col) {
  116. return (PIND&(1<<0) ? (1<<0) : 0) |
  117. (PIND&(1<<1) ? (1<<1) : 0) |
  118. (PIND&(1<<2) ? (1<<2) : 0) |
  119. (PIND&(1<<3) ? (1<<3) : 0) |
  120. (PIND&(1<<5) ? (1<<4) : 0) |
  121. (col==0 ? ((PINE&(1<<2) ? 0 : (1<<2))) : 0);
  122. }
  123. uint8_t read_fwkey(void)
  124. {
  125. return PINE&(1<<2) ? 0 : (1<<2);
  126. }
  127. /* Columns 0 - 15
  128. *
  129. * atmega32u4 decoder pin
  130. * PC6 U1 E3
  131. * PB6 U2 E3
  132. * PF0 U1, U2 A0
  133. * PF1 U1, U2 A1
  134. * PC7 U1, U2 A2
  135. *
  136. * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
  137. * col / pin: PC6 PB6 PF0 PF1 PC7 Decoder Pin
  138. * 0: 1 0 0 0 0 U1 Y0
  139. * 1: 1 0 1 0 0 U1 Y1
  140. * 2: 1 0 0 1 0 U1 Y2
  141. * 3: 1 0 1 1 0 U1 Y3
  142. * 4: 1 0 0 0 1 U1 Y4
  143. * 5: 1 0 1 0 1 U1 Y5
  144. * 6: 1 0 0 1 1 U1 Y6
  145. * 7: 1 0 1 1 1 U1 Y7
  146. * 8: 0 1 0 0 0 U2 Y0
  147. * 9: 0 1 1 0 0 U2 Y1
  148. * 10: 0 1 0 1 0 U2 Y2
  149. * 11: 0 1 1 1 0 U2 Y3
  150. * 12: 0 1 0 0 1 U2 Y4
  151. * 13: 0 1 1 0 1 U2 Y5
  152. * 14: 0 1 0 1 1 U2 Y6
  153. *
  154. */
  155. static void unselect_cols(void) {
  156. DDRB |= 0b01000000;
  157. PORTB &= ~0b01000000;
  158. DDRC |= 0b11000000;
  159. PORTC &= ~0b11000000;
  160. DDRF |= 0b00000011;
  161. PORTF &= ~0b00000011;
  162. }
  163. static void select_col(uint8_t col) {
  164. switch (col) {
  165. case 0:
  166. PORTC |= 0b01000000;
  167. break;
  168. case 1:
  169. PORTC |= 0b01000000;
  170. PORTF |= 0b00000001;
  171. break;
  172. case 2:
  173. PORTC |= 0b01000000;
  174. PORTF |= 0b00000010;
  175. break;
  176. case 3:
  177. PORTC |= 0b01000000;
  178. PORTF |= 0b00000011;
  179. break;
  180. case 4:
  181. PORTC |= 0b11000000;
  182. break;
  183. case 5:
  184. PORTC |= 0b11000000;
  185. PORTF |= 0b00000001;
  186. break;
  187. case 6:
  188. PORTC |= 0b11000000;
  189. PORTF |= 0b00000010;
  190. break;
  191. case 7:
  192. PORTC |= 0b11000000;
  193. PORTF |= 0b00000011;
  194. break;
  195. case 8:
  196. PORTB |= 0b01000000;
  197. break;
  198. case 9:
  199. PORTB |= 0b01000000;
  200. PORTF |= 0b00000001;
  201. break;
  202. case 10:
  203. PORTB |= 0b01000000;
  204. PORTF |= 0b00000010;
  205. break;
  206. case 11:
  207. PORTB |= 0b01000000;
  208. PORTF |= 0b00000011;
  209. break;
  210. case 12:
  211. PORTB |= 0b01000000;
  212. PORTC |= 0b10000000;
  213. break;
  214. case 13:
  215. PORTB |= 0b01000000;
  216. PORTF |= 0b00000001;
  217. PORTC |= 0b10000000;
  218. break;
  219. case 14:
  220. PORTB |= 0b01000000;
  221. PORTF |= 0b00000010;
  222. PORTC |= 0b10000000;
  223. break;
  224. }
  225. }