matrix.c 6.2 KB

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