logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (6566B)


  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 "matrix.h"
  15. #include "debug.h"
  16. #include "bitwise.h"
  17. #include "wait.h"
  18. #ifndef DEBOUNCE
  19. # define DEBOUNCE 5
  20. #endif
  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 matrix_init(void) {
  44. unselect_cols();
  45. init_rows();
  46. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  47. matrix[i] = 0;
  48. matrix_debouncing[i] = 0;
  49. }
  50. matrix_init_kb();
  51. }
  52. uint8_t matrix_scan(void) {
  53. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  54. select_col(col);
  55. _delay_us(3);
  56. uint8_t rows = read_rows(col);
  57. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  58. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  59. bool curr_bit = rows & (1<<row);
  60. if (prev_bit != curr_bit) {
  61. matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
  62. if (debouncing) {
  63. dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
  64. }
  65. debouncing = DEBOUNCE;
  66. }
  67. }
  68. unselect_cols();
  69. }
  70. if (debouncing) {
  71. if (--debouncing) {
  72. _delay_ms(1);
  73. } else {
  74. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  75. matrix[i] = matrix_debouncing[i];
  76. }
  77. }
  78. }
  79. matrix_scan_kb();
  80. return 1;
  81. }
  82. inline matrix_row_t matrix_get_row(uint8_t row) {
  83. return matrix[row];
  84. }
  85. void matrix_print(void) {
  86. print("\nr/c 0123456789ABCDEF\n");
  87. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  88. xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
  89. }
  90. }
  91. /* Row pin configuration - diode connected
  92. * row: 0 1 2 3 4 5
  93. * pin: PD0 PD1 PD2 PD3 PD5 PB7
  94. *
  95. * Backspace uses its own pin PE2 on the column pin, row pin is grounded
  96. */
  97. static void init_rows(void) {
  98. DDRD &= ~0b00101111;
  99. PORTD &= ~0b00101111;
  100. DDRB &= ~0b10000000;
  101. PORTB &= ~0b10000000;
  102. DDRE &= ~0b00000100;
  103. PORTE |= 0b00000100;
  104. }
  105. static uint8_t read_rows(uint8_t col) {
  106. return (PIND&(1<<0) ? (1<<0) : 0) |
  107. (PIND&(1<<1) ? (1<<1) : 0) |
  108. (PIND&(1<<2) ? (1<<2) : 0) |
  109. (PIND&(1<<3) ? (1<<3) : 0) |
  110. (PIND&(1<<5) ? (1<<4) : 0) |
  111. (PINB&(1<<7) ? (1<<5) : 0) |
  112. (col== 17 ? ((PINE&(1<<2) ? 0 : (1<<1))) : 0);
  113. }
  114. uint8_t read_fwkey(void)
  115. {
  116. return PINE&(1<<2) ? 0 : (1<<2);
  117. }
  118. /* Columns 0 - 15
  119. *
  120. * atmega32u4 decoder pin
  121. * PC6 U1 E3
  122. * PB6 U2 E3
  123. * PF0 U1, U2 A0
  124. * PF1 U1, U2 A1
  125. * PC7 U1, U2 A2
  126. *
  127. * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
  128. * col / pin: PC6 PB6 PF0 PF1 PC7 Decoder Pin
  129. * 0: 1 0 0 0 0 U1 Y0
  130. * 1: 1 0 1 0 0 U1 Y1
  131. * 2: 1 0 0 1 0 U1 Y2
  132. * 3: 1 0 1 1 0 U1 Y3
  133. * 4: 1 0 0 0 1 U1 Y4
  134. * 5: 1 0 1 0 1 U1 Y5
  135. * 6: 1 0 0 1 1 U1 Y6
  136. * 7: 1 0 1 1 1 U1 Y7
  137. *
  138. * 8: 0 1 0 0 0 U2 Y0
  139. * 9: 0 1 1 0 0 U2 Y1
  140. * 10: 0 1 0 1 0 U2 Y2
  141. * 11: 0 1 1 1 0 U2 Y3
  142. * 12: 0 1 0 0 1 U2 Y4
  143. * 13: 0 1 1 0 1 U2 Y5
  144. * 14: 0 1 0 1 1 U2 Y6
  145. * 15: 0 1 1 1 1 U2 Y7
  146. *
  147. */
  148. static void unselect_cols(void) {
  149. DDRB |= 0b01100000;
  150. PORTB &= ~0b01100000;
  151. DDRC |= 0b11000000;
  152. PORTC &= ~0b11000000;
  153. DDRF |= 0b00000011;
  154. PORTF &= ~0b00000011;
  155. }
  156. static void select_col(uint8_t col) {
  157. switch (col) {
  158. case 0:
  159. PORTC |= 0b01000000;
  160. break;
  161. case 1:
  162. PORTC |= 0b01000000;
  163. PORTF |= 0b00000001;
  164. break;
  165. case 2:
  166. PORTC |= 0b01000000;
  167. PORTF |= 0b00000010;
  168. break;
  169. case 3:
  170. PORTC |= 0b01000000;
  171. PORTF |= 0b00000011;
  172. break;
  173. case 4:
  174. PORTC |= 0b11000000;
  175. break;
  176. case 5:
  177. PORTC |= 0b11000000;
  178. PORTF |= 0b00000001;
  179. break;
  180. case 6:
  181. PORTC |= 0b11000000;
  182. PORTF |= 0b00000010;
  183. break;
  184. case 7:
  185. PORTC |= 0b11000000;
  186. PORTF |= 0b00000011;
  187. break;
  188. case 8:
  189. PORTB |= 0b01000000;
  190. break;
  191. case 9:
  192. PORTB |= 0b01000000;
  193. PORTF |= 0b00000001;
  194. break;
  195. case 10:
  196. PORTB |= 0b01000000;
  197. PORTF |= 0b00000010;
  198. break;
  199. case 11:
  200. PORTB |= 0b01000000;
  201. PORTF |= 0b00000011;
  202. break;
  203. case 12:
  204. PORTB |= 0b01000000;
  205. PORTC |= 0b10000000;
  206. break;
  207. case 13:
  208. PORTB |= 0b01000000;
  209. PORTF |= 0b00000001;
  210. PORTC |= 0b10000000;
  211. break;
  212. case 14:
  213. PORTB |= 0b01000000;
  214. PORTF |= 0b00000010;
  215. PORTC |= 0b10000000;
  216. break;
  217. case 15:
  218. PORTB |= 0b01000000;
  219. PORTF |= 0b00000011;
  220. PORTC |= 0b10000000;
  221. break;
  222. case 16:
  223. PORTB |= 0b00100000;
  224. break;
  225. }
  226. }