matrix.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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_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. debouncing = DEBOUNCING_DELAY;
  75. }
  76. }
  77. unselect_cols();
  78. }
  79. if (debouncing) {
  80. if (--debouncing) {
  81. _delay_ms(1);
  82. } else {
  83. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  84. matrix[i] = matrix_debouncing[i];
  85. }
  86. }
  87. }
  88. matrix_scan_quantum();
  89. return 1;
  90. }
  91. inline matrix_row_t matrix_get_row(uint8_t row) {
  92. return matrix[row];
  93. }
  94. void matrix_print(void) {
  95. print("\nr/c 0123456789ABCDEF\n");
  96. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  97. xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
  98. }
  99. }
  100. /* Row pin configuration
  101. * row: 0 1 2 3 4 5
  102. * pin: PB7 PD0 PD1 PD2 PD3 PD5
  103. *
  104. * Esc uses its own pin PE2
  105. */
  106. static void init_rows(void) {
  107. DDRD &= ~0b00101111;
  108. PORTD &= ~0b00101111;
  109. DDRB &= ~0b10000000;
  110. PORTB &= ~0b10000000;
  111. DDRE &= ~0b00000100;
  112. PORTE |= 0b00000100;
  113. }
  114. static uint8_t read_rows(uint8_t col) {
  115. return (PIND&(1<<0) ? (1<<0) : 0) |
  116. (PIND&(1<<1) ? (1<<1) : 0) |
  117. (PIND&(1<<2) ? (1<<2) : 0) |
  118. (PIND&(1<<3) ? (1<<3) : 0) |
  119. (PIND&(1<<5) ? (1<<4) : 0) |
  120. (PINB&(1<<7) ? (1<<5) : 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. * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
  129. * col / pin: PC6 PB6 PF0 PF1 PC7
  130. * 0: 1 0 0 0 0
  131. * 1: 1 0 1 0 0
  132. * 2: 1 0 0 1 0
  133. * 3: 1 0 1 1 0
  134. * 4: 1 0 0 0 1
  135. * 5: 1 0 1 0 1
  136. * 6: 1 0 0 1 1
  137. * 7: 1 0 1 1 1
  138. * 8: 0 1 0 0 0
  139. * 9: 0 1 1 0 0
  140. * 10: 0 1 0 1 0
  141. * 11: 0 1 1 1 0
  142. * 12: 0 1 0 0 1
  143. * 13: 0 1 1 0 1
  144. * 14: 0 1 0 1 1
  145. * 15: 0 1 1 1 1
  146. *
  147. */
  148. static void unselect_cols(void) {
  149. DDRB |= 0b01000000;
  150. PORTB &= ~0b01000000;
  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. }
  223. }