logo

qmk_firmware

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

matrix.c (7160B)


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