logo

qmk_firmware

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

matrix.c (6509B)


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