aboutsummaryrefslogtreecommitdiff
path: root/keyboards/ergodone/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/ergodone/matrix.c')
-rw-r--r--keyboards/ergodone/matrix.c295
1 files changed, 295 insertions, 0 deletions
diff --git a/keyboards/ergodone/matrix.c b/keyboards/ergodone/matrix.c
new file mode 100644
index 000000000..2eb8f24ba
--- /dev/null
+++ b/keyboards/ergodone/matrix.c
@@ -0,0 +1,295 @@
1#include <stdint.h>
2#include <stdbool.h>
3#include <avr/io.h>
4#include "wait.h"
5#include "action_layer.h"
6#include "print.h"
7#include "debug.h"
8#include "util.h"
9#include "matrix.h"
10#include "ergodone.h"
11#include "expander.h"
12#ifdef DEBUG_MATRIX_SCAN_RATE
13#include "timer.h"
14#endif
15
16/*
17 * This constant define not debouncing time in msecs, but amount of matrix
18 * scan loops which should be made to get stable debounced results.
19 *
20 * On Ergodox matrix scan rate is relatively low, because of slow I2C.
21 * Now it's only 317 scans/second, or about 3.15 msec/scan.
22 * According to Cherry specs, debouncing time is 5 msec.
23 *
24 * And so, there is no sense to have DEBOUNCE higher than 2.
25 */
26
27#ifndef DEBOUNCE
28# define DEBOUNCE 5
29#endif
30
31/* matrix state(1:on, 0:off) */
32static matrix_row_t matrix[MATRIX_ROWS];
33
34// Debouncing: store for each key the number of scans until it's eligible to
35// change. When scanning the matrix, ignore any changes in keys that have
36// already changed in the last DEBOUNCE scans.
37static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
38
39static matrix_row_t read_cols(uint8_t row);
40static void init_cols(void);
41static void unselect_rows(void);
42static void select_row(uint8_t row);
43
44#ifdef DEBUG_MATRIX_SCAN_RATE
45uint32_t matrix_timer;
46uint32_t matrix_scan_count;
47#endif
48
49
50__attribute__ ((weak))
51void matrix_init_user(void) {}
52
53__attribute__ ((weak))
54void matrix_scan_user(void) {}
55
56__attribute__ ((weak))
57void matrix_init_kb(void) {
58 matrix_init_user();
59}
60
61__attribute__ ((weak))
62void matrix_scan_kb(void) {
63 matrix_scan_user();
64}
65
66inline
67uint8_t matrix_rows(void)
68{
69 return MATRIX_ROWS;
70}
71
72inline
73uint8_t matrix_cols(void)
74{
75 return MATRIX_COLS;
76}
77
78void matrix_init(void)
79{
80 // disable JTAG
81 MCUCR = (1<<JTD);
82 MCUCR = (1<<JTD);
83
84 unselect_rows();
85 init_cols();
86
87 // initialize matrix state: all keys off
88 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
89 matrix[i] = 0;
90 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
91 debounce_matrix[i * MATRIX_COLS + j] = 0;
92 }
93 }
94
95#ifdef DEBUG_MATRIX_SCAN_RATE
96 matrix_timer = timer_read32();
97 matrix_scan_count = 0;
98#endif
99
100 matrix_init_quantum();
101
102}
103
104void matrix_power_up(void) {
105 unselect_rows();
106 init_cols();
107
108 // initialize matrix state: all keys off
109 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
110 matrix[i] = 0;
111 }
112
113#ifdef DEBUG_MATRIX_SCAN_RATE
114 matrix_timer = timer_read32();
115 matrix_scan_count = 0;
116#endif
117}
118
119// Returns a matrix_row_t whose bits are set if the corresponding key should be
120// eligible to change in this scan.
121matrix_row_t debounce_mask(uint8_t row) {
122 matrix_row_t result = 0;
123 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
124 if (debounce_matrix[row * MATRIX_COLS + j]) {
125 --debounce_matrix[row * MATRIX_COLS + j];
126 } else {
127 result |= (1 << j);
128 }
129 }
130 return result;
131}
132
133// Report changed keys in the given row. Resets the debounce countdowns
134// corresponding to each set bit in 'change' to DEBOUNCE.
135void debounce_report(matrix_row_t change, uint8_t row) {
136 for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
137 if (change & (1 << i)) {
138 debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
139 }
140 }
141}
142
143uint8_t matrix_scan(void)
144{
145 expander_scan();
146
147#ifdef DEBUG_MATRIX_SCAN_RATE
148 matrix_scan_count++;
149
150 uint32_t timer_now = timer_read32();
151 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
152 print("matrix scan frequency: ");
153 pdec(matrix_scan_count);
154 print("\n");
155 matrix_print();
156
157 matrix_timer = timer_now;
158 matrix_scan_count = 0;
159 }
160#endif
161
162 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
163 select_row(i);
164 wait_us(30); // without this wait read unstable value.
165 matrix_row_t mask = debounce_mask(i);
166 matrix_row_t cols = (read_cols(i) & mask) | (matrix[i] & ~mask);
167 debounce_report(cols ^ matrix[i], i);
168 matrix[i] = cols;
169
170 unselect_rows();
171 }
172
173 matrix_scan_quantum();
174
175 return 1;
176}
177
178inline
179bool matrix_is_on(uint8_t row, uint8_t col)
180{
181 return (matrix[row] & ((matrix_row_t)1<<col));
182}
183
184inline
185matrix_row_t matrix_get_row(uint8_t row)
186{
187 return matrix[row];
188}
189
190void matrix_print(void)
191{
192 print("\nr/c 0123456789ABCDEF\n");
193 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
194 phex(row); print(": ");
195 pbin_reverse16(matrix_get_row(row));
196 print("\n");
197 }
198}
199
200uint8_t matrix_key_count(void)
201{
202 uint8_t count = 0;
203 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
204 count += bitpop16(matrix[i]);
205 }
206 return count;
207}
208
209/* Column pin configuration
210 *
211 * Pro Micro: 6 5 4 3 2 1 0
212 * PD3 PD2 PD4 PC6 PD7 PE6 PB4
213 *
214 * Expander: 13 12 11 10 9 8 7
215 */
216static void init_cols(void)
217{
218 // Pro Micro
219 DDRE &= ~(1<<PE6);
220 PORTE |= (1<<PE6);
221 DDRD &= ~(1<<PD2 | 1<<PD3 | 1<<PD4 | 1<<PD7);
222 PORTD |= (1<<PD2 | 1<<PD3 | 1<<PD4 | 1<<PD7);
223 DDRC &= ~(1<<PC6);
224 PORTC |= (1<<PC6);
225 DDRB &= ~(1<<PB4);
226 PORTB |= (1<<PB4);
227
228 // MCP23017
229 expander_init();
230}
231
232static matrix_row_t read_cols(uint8_t row)
233{
234 return expander_read_row() |
235 (PIND&(1<<PD3) ? 0 : (1<<6)) |
236 (PIND&(1<<PD2) ? 0 : (1<<5)) |
237 (PIND&(1<<PD4) ? 0 : (1<<4)) |
238 (PINC&(1<<PC6) ? 0 : (1<<3)) |
239 (PIND&(1<<PD7) ? 0 : (1<<2)) |
240 (PINE&(1<<PE6) ? 0 : (1<<1)) |
241 (PINB&(1<<PB4) ? 0 : (1<<0)) ;
242}
243
244/* Row pin configuration
245 *
246 * Pro Micro: 0 1 2 3 4 5
247 * F4 F5 F6 F7 B1 B2
248 *
249 * Expander: 0 1 2 3 4 5
250 */
251static void unselect_rows(void)
252{
253 // Pro Micro
254 DDRF &= ~(1<<PF4 | 1<<PF5 | 1<<PF6 | 1<<PF7);
255 PORTF &= ~(1<<PF4 | 1<<PF5 | 1<<PF6 | 1<<PF7);
256 DDRB &= ~(1<<PB1 | 1<<PB2);
257 PORTB &= ~(1<<PB1 | 1<<PB2);
258
259 // Expander
260 expander_unselect_rows();
261}
262
263static void select_row(uint8_t row)
264{
265 // Pro Micro
266 switch (row) {
267 case 0:
268 DDRF |= (1<<PF4);
269 PORTF &= ~(1<<PF4);
270 break;
271 case 1:
272 DDRF |= (1<<PF5);
273 PORTF &= ~(1<<PF5);
274 break;
275 case 2:
276 DDRF |= (1<<PF6);
277 PORTF &= ~(1<<PF6);
278 break;
279 case 3:
280 DDRF |= (1<<PF7);
281 PORTF &= ~(1<<PF7);
282 break;
283 case 4:
284 DDRB |= (1<<PB1);
285 PORTB &= ~(1<<PB1);
286 break;
287 case 5:
288 DDRB |= (1<<PB2);
289 PORTB &= ~(1<<PB2);
290 break;
291 }
292
293 expander_select_row(row);
294}
295