diff options
Diffstat (limited to 'keyboards/dc01/left/matrix.c')
-rw-r--r-- | keyboards/dc01/left/matrix.c | 479 |
1 files changed, 479 insertions, 0 deletions
diff --git a/keyboards/dc01/left/matrix.c b/keyboards/dc01/left/matrix.c new file mode 100644 index 000000000..792376635 --- /dev/null +++ b/keyboards/dc01/left/matrix.c | |||
@@ -0,0 +1,479 @@ | |||
1 | /* | ||
2 | Copyright 2012 Jun Wako | ||
3 | Copyright 2014 Jack Humbert | ||
4 | |||
5 | This program is free software: you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation, either version 2 of the License, or | ||
8 | (at your option) any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | #include <stdint.h> | ||
19 | #include <stdbool.h> | ||
20 | #if defined(__AVR__) | ||
21 | #include <avr/io.h> | ||
22 | #include <avr/wdt.h> | ||
23 | #include <avr/interrupt.h> | ||
24 | #include <util/delay.h> | ||
25 | #endif | ||
26 | #include "wait.h" | ||
27 | #include "print.h" | ||
28 | #include "debug.h" | ||
29 | #include "util.h" | ||
30 | #include "matrix.h" | ||
31 | #include "timer.h" | ||
32 | #include "i2c_master.h" | ||
33 | |||
34 | #define SLAVE_I2C_ADDRESS_RIGHT 0x19 | ||
35 | #define SLAVE_I2C_ADDRESS_NUMPAD 0x21 | ||
36 | #define SLAVE_I2C_ADDRESS_ARROW 0x23 | ||
37 | |||
38 | #define ERROR_DISCONNECT_COUNT 5 | ||
39 | static uint8_t error_count_right = 0; | ||
40 | static uint8_t error_count_numpad = 0; | ||
41 | static uint8_t error_count_arrow = 0; | ||
42 | |||
43 | /* Set 0 if debouncing isn't needed */ | ||
44 | |||
45 | #ifndef DEBOUNCING_DELAY | ||
46 | # define DEBOUNCING_DELAY 5 | ||
47 | #endif | ||
48 | |||
49 | #if (DEBOUNCING_DELAY > 0) | ||
50 | static uint16_t debouncing_time; | ||
51 | static bool debouncing = false; | ||
52 | #endif | ||
53 | |||
54 | #if (MATRIX_COLS <= 8) | ||
55 | # define print_matrix_header() print("\nr/c 01234567\n") | ||
56 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) | ||
57 | # define matrix_bitpop(i) bitpop(matrix[i]) | ||
58 | # define ROW_SHIFTER ((uint8_t)1) | ||
59 | #elif (MATRIX_COLS <= 16) | ||
60 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") | ||
61 | # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) | ||
62 | # define matrix_bitpop(i) bitpop16(matrix[i]) | ||
63 | # define ROW_SHIFTER ((uint16_t)1) | ||
64 | #elif (MATRIX_COLS <= 32) | ||
65 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") | ||
66 | # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) | ||
67 | # define matrix_bitpop(i) bitpop32(matrix[i]) | ||
68 | # define ROW_SHIFTER ((uint32_t)1) | ||
69 | #endif | ||
70 | |||
71 | #ifdef MATRIX_MASKED | ||
72 | extern const matrix_row_t matrix_mask[]; | ||
73 | #endif | ||
74 | |||
75 | #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) | ||
76 | static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | ||
77 | static const uint8_t col_pins[MATRIX_COLS_SCANNED] = MATRIX_COL_PINS; | ||
78 | #endif | ||
79 | |||
80 | /* matrix state(1:on, 0:off) */ | ||
81 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
82 | |||
83 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
84 | |||
85 | |||
86 | #if (DIODE_DIRECTION == COL2ROW) | ||
87 | static void init_cols(void); | ||
88 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row); | ||
89 | static void unselect_rows(void); | ||
90 | static void select_row(uint8_t row); | ||
91 | static void unselect_row(uint8_t row); | ||
92 | #elif (DIODE_DIRECTION == ROW2COL) | ||
93 | static void init_rows(void); | ||
94 | static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col); | ||
95 | static void unselect_cols(void); | ||
96 | static void unselect_col(uint8_t col); | ||
97 | static void select_col(uint8_t col); | ||
98 | #endif | ||
99 | |||
100 | __attribute__ ((weak)) | ||
101 | void matrix_init_quantum(void) { | ||
102 | matrix_init_kb(); | ||
103 | } | ||
104 | |||
105 | __attribute__ ((weak)) | ||
106 | void matrix_scan_quantum(void) { | ||
107 | matrix_scan_kb(); | ||
108 | } | ||
109 | |||
110 | __attribute__ ((weak)) | ||
111 | void matrix_init_kb(void) { | ||
112 | matrix_init_user(); | ||
113 | } | ||
114 | |||
115 | __attribute__ ((weak)) | ||
116 | void matrix_scan_kb(void) { | ||
117 | matrix_scan_user(); | ||
118 | } | ||
119 | |||
120 | __attribute__ ((weak)) | ||
121 | void matrix_init_user(void) { | ||
122 | } | ||
123 | |||
124 | __attribute__ ((weak)) | ||
125 | void matrix_scan_user(void) { | ||
126 | } | ||
127 | |||
128 | inline | ||
129 | uint8_t matrix_rows(void) { | ||
130 | return MATRIX_ROWS; | ||
131 | } | ||
132 | |||
133 | inline | ||
134 | uint8_t matrix_cols(void) { | ||
135 | return MATRIX_COLS; | ||
136 | } | ||
137 | |||
138 | |||
139 | i2c_status_t i2c_transaction(uint8_t address, uint32_t mask, uint8_t col_offset); | ||
140 | //uint8_t i2c_transaction_numpad(void); | ||
141 | //uint8_t i2c_transaction_arrow(void); | ||
142 | |||
143 | //this replases tmk code | ||
144 | void matrix_setup(void){ | ||
145 | i2c_init(); | ||
146 | } | ||
147 | |||
148 | void matrix_init(void) { | ||
149 | |||
150 | // initialize row and col | ||
151 | #if (DIODE_DIRECTION == COL2ROW) | ||
152 | unselect_rows(); | ||
153 | init_cols(); | ||
154 | #elif (DIODE_DIRECTION == ROW2COL) | ||
155 | unselect_cols(); | ||
156 | init_rows(); | ||
157 | #endif | ||
158 | |||
159 | // initialize matrix state: all keys off | ||
160 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
161 | matrix[i] = 0; | ||
162 | matrix_debouncing[i] = 0; | ||
163 | } | ||
164 | |||
165 | matrix_init_quantum(); | ||
166 | } | ||
167 | |||
168 | uint8_t matrix_scan(void) | ||
169 | { | ||
170 | |||
171 | #if (DIODE_DIRECTION == COL2ROW) | ||
172 | |||
173 | // Set row, read cols | ||
174 | for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { | ||
175 | # if (DEBOUNCING_DELAY > 0) | ||
176 | bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row); | ||
177 | |||
178 | if (matrix_changed) { | ||
179 | debouncing = true; | ||
180 | debouncing_time = timer_read(); | ||
181 | } | ||
182 | |||
183 | # else | ||
184 | read_cols_on_row(matrix, current_row); | ||
185 | # endif | ||
186 | |||
187 | } | ||
188 | |||
189 | #elif (DIODE_DIRECTION == ROW2COL) | ||
190 | |||
191 | // Set col, read rows | ||
192 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { | ||
193 | # if (DEBOUNCING_DELAY > 0) | ||
194 | bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col); | ||
195 | if (matrix_changed) { | ||
196 | debouncing = true; | ||
197 | debouncing_time = timer_read(); | ||
198 | } | ||
199 | # else | ||
200 | read_rows_on_col(matrix, current_col); | ||
201 | # endif | ||
202 | |||
203 | } | ||
204 | |||
205 | #endif | ||
206 | |||
207 | # if (DEBOUNCING_DELAY > 0) | ||
208 | if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) { | ||
209 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
210 | matrix[i] = matrix_debouncing[i]; | ||
211 | } | ||
212 | debouncing = false; | ||
213 | } | ||
214 | # endif | ||
215 | |||
216 | if (i2c_transaction(SLAVE_I2C_ADDRESS_RIGHT, 0x3F, 0)){ //error has occured for main right half | ||
217 | error_count_right++; | ||
218 | if (error_count_right > ERROR_DISCONNECT_COUNT){ //disconnect half | ||
219 | for (uint8_t i = 0; i < MATRIX_ROWS ; i++) { | ||
220 | matrix[i] &= 0x3F; //mask bits to keep | ||
221 | } | ||
222 | } | ||
223 | }else{ //no error | ||
224 | error_count_right = 0; | ||
225 | } | ||
226 | |||
227 | if (i2c_transaction(SLAVE_I2C_ADDRESS_ARROW, 0X3FFF, 8)){ //error has occured for arrow cluster | ||
228 | error_count_arrow++; | ||
229 | if (error_count_arrow > ERROR_DISCONNECT_COUNT){ //disconnect arrow cluster | ||
230 | for (uint8_t i = 0; i < MATRIX_ROWS ; i++) { | ||
231 | matrix[i] &= 0x3FFF; //mask bits to keep | ||
232 | } | ||
233 | } | ||
234 | }else{ //no error | ||
235 | error_count_arrow = 0; | ||
236 | } | ||
237 | |||
238 | if (i2c_transaction(SLAVE_I2C_ADDRESS_NUMPAD, 0x1FFFF, 11)){ //error has occured for numpad | ||
239 | error_count_numpad++; | ||
240 | if (error_count_numpad > ERROR_DISCONNECT_COUNT){ //disconnect numpad | ||
241 | for (uint8_t i = 0; i < MATRIX_ROWS ; i++) { | ||
242 | matrix[i] &= 0x1FFFF; //mask bits to keep | ||
243 | } | ||
244 | } | ||
245 | }else{ //no error | ||
246 | error_count_numpad = 0; | ||
247 | } | ||
248 | |||
249 | matrix_scan_quantum(); | ||
250 | return 1; | ||
251 | } | ||
252 | |||
253 | bool matrix_is_modified(void) | ||
254 | { | ||
255 | #if (DEBOUNCING_DELAY > 0) | ||
256 | if (debouncing) return false; | ||
257 | #endif | ||
258 | return true; | ||
259 | } | ||
260 | |||
261 | inline | ||
262 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
263 | { | ||
264 | return (matrix[row] & ((matrix_row_t)1<col)); | ||
265 | } | ||
266 | |||
267 | inline | ||
268 | matrix_row_t matrix_get_row(uint8_t row) | ||
269 | { | ||
270 | // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a | ||
271 | // switch blocker installed and the switch is always pressed. | ||
272 | #ifdef MATRIX_MASKED | ||
273 | return matrix[row] & matrix_mask[row]; | ||
274 | #else | ||
275 | return matrix[row]; | ||
276 | #endif | ||
277 | } | ||
278 | |||
279 | void matrix_print(void) | ||
280 | { | ||
281 | print_matrix_header(); | ||
282 | |||
283 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
284 | phex(row); print(": "); | ||
285 | print_matrix_row(row); | ||
286 | print("\n"); | ||
287 | } | ||
288 | } | ||
289 | |||
290 | uint8_t matrix_key_count(void) | ||
291 | { | ||
292 | uint8_t count = 0; | ||
293 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
294 | count += matrix_bitpop(i); | ||
295 | } | ||
296 | return count; | ||
297 | } | ||
298 | |||
299 | |||
300 | |||
301 | #if (DIODE_DIRECTION == COL2ROW) | ||
302 | |||
303 | static void init_cols(void) | ||
304 | { | ||
305 | for(uint8_t x = 0; x < MATRIX_COLS_SCANNED; x++) { | ||
306 | uint8_t pin = col_pins[x]; | ||
307 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
308 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
309 | } | ||
310 | } | ||
311 | |||
312 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) | ||
313 | { | ||
314 | // Store last value of row prior to reading | ||
315 | matrix_row_t last_row_value = current_matrix[current_row]; | ||
316 | |||
317 | // Clear data in matrix row | ||
318 | current_matrix[current_row] = 0; | ||
319 | |||
320 | // Select row and wait for row selecton to stabilize | ||
321 | select_row(current_row); | ||
322 | wait_us(30); | ||
323 | |||
324 | // For each col... | ||
325 | for(uint8_t col_index = 0; col_index < MATRIX_COLS_SCANNED; col_index++) { | ||
326 | |||
327 | // Select the col pin to read (active low) | ||
328 | uint8_t pin = col_pins[col_index]; | ||
329 | uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)); | ||
330 | |||
331 | // Populate the matrix row with the state of the col pin | ||
332 | current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); | ||
333 | } | ||
334 | |||
335 | // Unselect row | ||
336 | unselect_row(current_row); | ||
337 | |||
338 | return (last_row_value != current_matrix[current_row]); | ||
339 | } | ||
340 | |||
341 | static void select_row(uint8_t row) | ||
342 | { | ||
343 | uint8_t pin = row_pins[row]; | ||
344 | _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT | ||
345 | _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW | ||
346 | } | ||
347 | |||
348 | static void unselect_row(uint8_t row) | ||
349 | { | ||
350 | uint8_t pin = row_pins[row]; | ||
351 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
352 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
353 | } | ||
354 | |||
355 | static void unselect_rows(void) | ||
356 | { | ||
357 | for(uint8_t x = 0; x < MATRIX_ROWS; x++) { | ||
358 | uint8_t pin = row_pins[x]; | ||
359 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
360 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
361 | } | ||
362 | } | ||
363 | |||
364 | #elif (DIODE_DIRECTION == ROW2COL) | ||
365 | |||
366 | static void init_rows(void) | ||
367 | { | ||
368 | for(uint8_t x = 0; x < MATRIX_ROWS; x++) { | ||
369 | uint8_t pin = row_pins[x]; | ||
370 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
371 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
372 | } | ||
373 | } | ||
374 | |||
375 | static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) | ||
376 | { | ||
377 | bool matrix_changed = false; | ||
378 | |||
379 | // Select col and wait for col selecton to stabilize | ||
380 | select_col(current_col); | ||
381 | wait_us(30); | ||
382 | |||
383 | // For each row... | ||
384 | for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) | ||
385 | { | ||
386 | |||
387 | // Store last value of row prior to reading | ||
388 | matrix_row_t last_row_value = current_matrix[row_index]; | ||
389 | |||
390 | // Check row pin state | ||
391 | if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0) | ||
392 | { | ||
393 | // Pin LO, set col bit | ||
394 | current_matrix[row_index] |= (ROW_SHIFTER << current_col); | ||
395 | } | ||
396 | else | ||
397 | { | ||
398 | // Pin HI, clear col bit | ||
399 | current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); | ||
400 | } | ||
401 | |||
402 | // Determine if the matrix changed state | ||
403 | if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) | ||
404 | { | ||
405 | matrix_changed = true; | ||
406 | } | ||
407 | } | ||
408 | |||
409 | // Unselect col | ||
410 | unselect_col(current_col); | ||
411 | |||
412 | return matrix_changed; | ||
413 | } | ||
414 | |||
415 | static void select_col(uint8_t col) | ||
416 | { | ||
417 | uint8_t pin = col_pins[col]; | ||
418 | _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT | ||
419 | _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW | ||
420 | } | ||
421 | |||
422 | static void unselect_col(uint8_t col) | ||
423 | { | ||
424 | uint8_t pin = col_pins[col]; | ||
425 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
426 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
427 | } | ||
428 | |||
429 | static void unselect_cols(void) | ||
430 | { | ||
431 | for(uint8_t x = 0; x < MATRIX_COLS_SCANNED; x++) { | ||
432 | uint8_t pin = col_pins[x]; | ||
433 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
434 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
435 | } | ||
436 | } | ||
437 | |||
438 | #endif | ||
439 | |||
440 | // Complete rows from other modules over i2c | ||
441 | i2c_status_t i2c_transaction(uint8_t address, uint32_t mask, uint8_t col_offset) { | ||
442 | i2c_status_t err = i2c_start((address << 1) | I2C_WRITE, 10); | ||
443 | if (err) return err; | ||
444 | i2c_write(0x01, 10); | ||
445 | if (err) return err; | ||
446 | |||
447 | i2c_start((address << 1) | I2C_READ, 10); | ||
448 | if (err) return err; | ||
449 | |||
450 | err = i2c_read_ack(10); | ||
451 | if (err == 0x55) { //synchronization byte | ||
452 | |||
453 | for (uint8_t i = 0; i < MATRIX_ROWS-1 ; i++) { //assemble slave matrix in main matrix | ||
454 | matrix[i] &= mask; //mask bits to keep | ||
455 | err = i2c_read_ack(10); | ||
456 | if (err >= 0) { | ||
457 | matrix[i] |= ((uint32_t)err << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end | ||
458 | } else { | ||
459 | return err; | ||
460 | } | ||
461 | } | ||
462 | //last read request must be followed by a NACK | ||
463 | matrix[MATRIX_ROWS - 1] &= mask; //mask bits to keep | ||
464 | err = i2c_read_nack(10); | ||
465 | if (err >= 0) { | ||
466 | matrix[MATRIX_ROWS - 1] |= ((uint32_t)err << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end | ||
467 | } else { | ||
468 | return err; | ||
469 | } | ||
470 | } else { | ||
471 | i2c_stop(10); | ||
472 | return 1; | ||
473 | } | ||
474 | |||
475 | i2c_stop(10); | ||
476 | if (err) return err; | ||
477 | |||
478 | return 0; | ||
479 | } \ No newline at end of file | ||