matrix.c 5.9 KB

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