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