You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
6.0 KiB

  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. if (debouncing) {
  74. dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
  75. }
  76. debouncing = DEBOUNCING_DELAY;
  77. }
  78. }
  79. unselect_cols();
  80. }
  81. if (debouncing) {
  82. if (--debouncing) {
  83. _delay_ms(1);
  84. } else {
  85. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  86. matrix[i] = matrix_debouncing[i];
  87. }
  88. }
  89. }
  90. matrix_scan_quantum();
  91. return 1;
  92. }
  93. inline matrix_row_t matrix_get_row(uint8_t row) {
  94. return matrix[row];
  95. }
  96. void matrix_print(void) {
  97. print("\nr/c 0123456789ABCDEF\n");
  98. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  99. xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
  100. }
  101. }
  102. /* Row pin configuration
  103. * row: 0 1 2 3 4 5
  104. * pin: PB7 PD0 PD1 PD2 PD3 PD5
  105. *
  106. * Esc uses its own pin PE2
  107. */
  108. static void init_rows(void) {
  109. DDRB &= ~0b10000000;
  110. PORTB &= ~0b10000000;
  111. DDRD &= ~0b00101111;
  112. PORTD &= ~0b00101111;
  113. DDRE &= ~0b00000100;
  114. PORTE |= 0b00000100;
  115. }
  116. static uint8_t read_rows() {
  117. return (PINB&(1<<7) ? (1<<0) : 0) |
  118. (PIND&(1<<0) ? (1<<1) : 0) |
  119. (PIND&(1<<1) ? (1<<2) : 0) |
  120. (PIND&(1<<2) ? (1<<3) : 0) |
  121. (PIND&(1<<3) ? (1<<4) : 0) |
  122. (PIND&(1<<5) ? (1<<5) : 0);
  123. }
  124. static uint8_t read_fwkey(void)
  125. {
  126. return PINE&(1<<2) ? 0 : (1<<0);
  127. }
  128. /* Columns 0 - 15
  129. * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
  130. * col / pin: PC6 PB6 PF0 PF1 PC7
  131. * 0: 1 0 0 0 0
  132. * 1: 1 0 1 0 0
  133. * 2: 1 0 0 1 0
  134. * 3: 1 0 1 1 0
  135. * 4: 1 0 0 0 1
  136. * 5: 1 0 1 0 1
  137. * 6: 1 0 0 1 1
  138. * 7: 1 0 1 1 1
  139. * 8: 0 1 0 0 0
  140. * 9: 0 1 1 0 0
  141. * 10: 0 1 0 1 0
  142. * 11: 0 1 1 1 0
  143. * 12: 0 1 0 0 1
  144. * 13: 0 1 1 0 1
  145. * 14: 0 1 0 1 1
  146. * 15: 0 1 1 1 1
  147. *
  148. * col: 16
  149. * pin: PB5
  150. *
  151. * col: 17
  152. * pin: PB4
  153. *
  154. * col: 18
  155. * pin: PD7
  156. */
  157. static void unselect_cols(void) {
  158. DDRB |= 0b01110000;
  159. PORTB &= ~0b01110000;
  160. DDRC |= (1<<6) | (1<<7);
  161. PORTC &= ~((1<<6) | (1<<7));
  162. DDRF |= (1<<0) | (1<<1);
  163. PORTF &= ~((1<<0) | (1<<1));
  164. DDRD |= (1<<7);
  165. PORTD &= ~(1<<7);
  166. }
  167. static void select_col(uint8_t col) {
  168. switch (col) {
  169. case 0:
  170. PORTC |= (1<<6);
  171. break;
  172. case 1:
  173. PORTC |= (1<<6);
  174. PORTF |= (1<<0);
  175. break;
  176. case 2:
  177. PORTC |= (1<<6);
  178. PORTF |= (1<<1);
  179. break;
  180. case 3:
  181. PORTC |= (1<<6);
  182. PORTF |= (1<<0) | (1<<1);
  183. break;
  184. case 4:
  185. PORTC |= (1<<6);
  186. PORTC |= (1<<7);
  187. break;
  188. case 5:
  189. PORTC |= (1<<6);
  190. PORTF |= (1<<0);
  191. PORTC |= (1<<7);
  192. break;
  193. case 6:
  194. PORTC |= (1<<6);
  195. PORTF |= (1<<1);
  196. PORTC |= (1<<7);
  197. break;
  198. case 7:
  199. PORTC |= (1<<6);
  200. PORTF |= (1<<0) | (1<<1);
  201. PORTC |= (1<<7);
  202. break;
  203. case 8:
  204. PORTB |= (1<<6);
  205. break;
  206. case 9:
  207. PORTB |= (1<<6);
  208. PORTF |= (1<<0);
  209. break;
  210. case 10:
  211. PORTB |= (1<<6);
  212. PORTF |= (1<<1);
  213. break;
  214. case 11:
  215. PORTB |= (1<<6);
  216. PORTF |= (1<<0) | (1<<1);
  217. break;
  218. case 12:
  219. PORTB |= (1<<6);
  220. PORTC |= (1<<7);
  221. break;
  222. case 13:
  223. PORTB |= (1<<6);
  224. PORTF |= (1<<0);
  225. PORTC |= (1<<7);
  226. break;
  227. case 14:
  228. PORTB |= (1<<6);
  229. PORTF |= (1<<1);
  230. PORTC |= (1<<7);
  231. break;
  232. case 15:
  233. PORTB |= (1<<6);
  234. PORTF |= (1<<0) | (1<<1);
  235. PORTC |= (1<<7);
  236. break;
  237. case 16:
  238. PORTB |= (1<<5);
  239. break;
  240. case 17:
  241. PORTB |= (1<<4);
  242. break;
  243. case 18:
  244. PORTD |= (1<<7);
  245. break;
  246. }
  247. }