]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ergotaco/matrix.c
[Keyboard] fixed pins for numpad_5x4 layout (#6311)
[qmk_firmware.git] / keyboards / ergotaco / matrix.c
1 /*
2
3 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
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
19 #include "matrix.h"
20 #include <stdint.h>
21 #include <stdbool.h>
22 #include <avr/io.h>
23 #include "wait.h"
24 #include "action_layer.h"
25 #include "print.h"
26 #include "debug.h"
27 #include "util.h"
28 #include QMK_KEYBOARD_H
29 #ifdef DEBUG_MATRIX_SCAN_RATE
30 #include  "timer.h"
31 #endif
32
33 #ifndef DEBOUNCE
34 #   define DEBOUNCE     5
35 #endif
36
37 // ATmega pin defs
38 #define ROW1  (1<<5)
39 #define COL6  (1<<0)
40 #define COL7  (1<<1)
41 #define COL8  (1<<2)
42 #define COL9  (1<<3)
43 #define COL10 (1<<2)
44 #define COL11 (1<<3)
45
46
47 // bit masks
48 #define BMASK     (COL7 | COL8 | COL9 | COL6)
49 #define DMASK     (COL10 | COL11)
50 #define FMASK     (ROW1)
51
52 /* matrix state(1:on, 0:off) */
53 static matrix_row_t matrix[MATRIX_ROWS];
54 /*
55  * matrix state(1:on, 0:off)
56  * contains the raw values without debounce filtering of the last read cycle.
57  */
58 static matrix_row_t raw_matrix[MATRIX_ROWS];
59
60 // Debouncing: store for each key the number of scans until it's eligible to
61 // change.  When scanning the matrix, ignore any changes in keys that have
62 // already changed in the last DEBOUNCE scans.
63 static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
64
65 static matrix_row_t read_cols(uint8_t row);
66 static void init_cols(void);
67 static void unselect_rows(void);
68 static void select_row(uint8_t row);
69
70 static uint8_t mcp23018_reset_loop;
71 // static uint16_t mcp23018_reset_loop;
72
73 #ifdef DEBUG_MATRIX_SCAN_RATE
74 uint32_t matrix_timer;
75 uint32_t matrix_scan_count;
76 #endif
77
78
79 __attribute__ ((weak))
80 void matrix_init_user(void) {}
81
82 __attribute__ ((weak))
83 void matrix_scan_user(void) {}
84
85 __attribute__ ((weak))
86 void matrix_init_kb(void) {
87   matrix_init_user();
88 }
89
90 __attribute__ ((weak))
91 void matrix_scan_kb(void) {
92   matrix_scan_user();
93 }
94
95 inline
96 uint8_t matrix_rows(void)
97 {
98     return MATRIX_ROWS;
99 }
100
101 inline
102 uint8_t matrix_cols(void)
103 {
104     return MATRIX_COLS;
105 }
106
107
108 void matrix_init(void)
109 {
110     // initialize row and col
111     mcp23018_status = init_mcp23018();
112     unselect_rows();
113     init_cols();
114
115     // initialize matrix state: all keys off
116     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
117         matrix[i] = 0;
118         raw_matrix[i] = 0;
119         for (uint8_t j=0; j < MATRIX_COLS; ++j) {
120             debounce_matrix[i * MATRIX_COLS + j] = 0;
121         }
122     }
123
124 #ifdef DEBUG_MATRIX_SCAN_RATE
125     matrix_timer = timer_read32();
126     matrix_scan_count = 0;
127 #endif
128     matrix_init_quantum();
129 }
130
131 void matrix_power_up(void) {
132     mcp23018_status = init_mcp23018();
133
134     unselect_rows();
135     init_cols();
136
137     // initialize matrix state: all keys off
138     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
139         matrix[i] = 0;
140     }
141
142 #ifdef DEBUG_MATRIX_SCAN_RATE
143     matrix_timer = timer_read32();
144     matrix_scan_count = 0;
145 #endif
146
147 }
148
149 // Returns a matrix_row_t whose bits are set if the corresponding key should be
150 // eligible to change in this scan.
151 matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
152   matrix_row_t result = 0;
153   matrix_row_t change = rawcols ^ raw_matrix[row];
154   raw_matrix[row] = rawcols;
155   for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
156     if (debounce_matrix[row * MATRIX_COLS + i]) {
157       --debounce_matrix[row * MATRIX_COLS + i];
158     } else {
159       result |= (1 << i);
160     }
161     if (change & (1 << i)) {
162       debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
163     }
164   }
165   return result;
166 }
167
168 matrix_row_t debounce_read_cols(uint8_t row) {
169   // Read the row without debouncing filtering and store it for later usage.
170   matrix_row_t cols = read_cols(row);
171   // Get the Debounce mask.
172   matrix_row_t mask = debounce_mask(cols, row);
173   // debounce the row and return the result.
174   return (cols & mask) | (matrix[row] & ~mask);;
175 }
176
177 uint8_t matrix_scan(void)
178 {
179   // Then the keyboard
180   if (mcp23018_status) { // if there was an error
181       if (++mcp23018_reset_loop == 0) {
182       // if (++mcp23018_reset_loop >= 1300) {
183           // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
184           // this will be approx bit more frequent than once per second
185           print("trying to reset mcp23018\n");
186           mcp23018_status = init_mcp23018();
187           if (mcp23018_status) {
188               print("left side not responding\n");
189           } else {
190               print("left side attached\n");
191           }
192       }
193   }
194
195 #ifdef DEBUG_MATRIX_SCAN_RATE
196     matrix_scan_count++;
197     uint32_t timer_now = timer_read32();
198     if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
199         print("matrix scan frequency: ");
200         pdec(matrix_scan_count);
201         print("\n");
202
203         matrix_timer = timer_now;
204         matrix_scan_count = 0;
205     }
206 #endif
207     for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
208         select_row(i);
209         // and select on left hand
210         select_row(i + MATRIX_ROWS_PER_SIDE);
211         // we don't need a 30us delay anymore, because selecting a
212         // left-hand row requires more than 30us for i2c.
213
214         // grab cols from left hand
215         matrix[i] = debounce_read_cols(i);
216         // grab cols from right hand
217         matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
218
219         unselect_rows();
220     }
221
222     matrix_scan_quantum();
223
224 #ifdef DEBUG_MATRIX
225     for (uint8_t c = 0; c < MATRIX_COLS; c++)
226                 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
227                   if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
228 #endif
229
230     return 1;
231 }
232
233 bool matrix_is_modified(void) // deprecated and evidently not called.
234 {
235     return true;
236 }
237
238 inline
239 bool matrix_is_on(uint8_t row, uint8_t col)
240 {
241     return (matrix[row] & ((matrix_row_t)1<<col));
242 }
243
244 inline
245 matrix_row_t matrix_get_row(uint8_t row)
246 {
247     return matrix[row];
248 }
249
250 void matrix_print(void)
251 {
252     print("\nr/c 0123456789ABCDEF\n");
253     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
254         phex(row); print(": ");
255         pbin_reverse16(matrix_get_row(row));
256         print("\n");
257     }
258 }
259
260 uint8_t matrix_key_count(void)
261 {
262     uint8_t count = 0;
263     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
264         count += bitpop16(matrix[i]);
265     }
266     return count;
267 }
268
269 // Remember this means ROWS
270 static void  init_cols(void)
271 {
272     // init on mcp23018
273     // not needed, already done as part of init_mcp23018()
274
275     // Input with pull-up(DDR:0, PORT:1)
276     DDRF  &= ~FMASK;
277     PORTF |=  FMASK;
278 }
279
280 static matrix_row_t read_cols(uint8_t row)
281 {
282     if (row < 6) {
283         if (mcp23018_status) { // if there was an error
284             return 0;
285         } else {
286             uint8_t data = 0;
287             mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);    if (mcp23018_status) goto out;
288             mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT);             if (mcp23018_status) goto out;
289             mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT);     if (mcp23018_status) goto out;
290             mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT);                if (mcp23018_status < 0) goto out;
291             data = (~((uint8_t)mcp23018_status) >> 2) & 0x01 ;
292             mcp23018_status = I2C_STATUS_SUCCESS;
293         out:
294             i2c_stop();
295
296 #ifdef DEBUG_MATRIX
297             if (data != 0x00) xprintf("I2C: %d\n", data);
298 #endif
299             return data;
300         }
301     } else {
302                 // Read using bitmask
303         return ~((((PINF & ROW1) >> 5)) & 0x1);
304     }
305 }
306
307 // Row pin configuration
308 static void unselect_rows(void)
309 {
310     // no need to unselect on mcp23018, because the select step sets all
311     // the other row bits high, and it's not changing to a different
312     // direction
313     // Hi-Z(DDR:0, PORT:0) to unselect
314     DDRB  &= ~BMASK;
315     PORTB &= ~BMASK;
316     DDRD  &= ~DMASK;
317     PORTD &= ~DMASK;
318 }
319
320 static void select_row(uint8_t row)
321 {
322     if (row < 6) {
323         // select on mcp23018
324         if (mcp23018_status) { // do nothing on error
325                         // Read using bitmask
326         } else { // set active row low  : 0 // set other rows hi-Z : 1
327             mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);        if (mcp23018_status) goto out;
328             mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT);                 if (mcp23018_status) goto out;
329             mcp23018_status = i2c_write(~(1<<row), ERGODOX_EZ_I2C_TIMEOUT);                     if (mcp23018_status) goto out;
330         out:
331             i2c_stop();
332         }
333     } else {
334         // Output low(DDR:1, PORT:0) to select
335         switch (row) {
336             case 6:
337                 DDRB  |= COL6;
338                 PORTB &= ~COL6;
339                 break;
340             case 7:
341                 DDRB  |= COL7;
342                 PORTB &= ~COL7;
343                 break;
344             case 8:
345                 DDRB  |= COL8;
346                 PORTB &= ~COL8;
347                 break;
348             case 9:
349                 DDRB  |= COL9;
350                 PORTB &= ~COL9;
351                 break;
352             case 10:
353                 DDRD  |= COL10;
354                 PORTD &= ~COL10;
355                 break;
356             case 11:
357                 DDRD  |= COL11;
358                 PORTD &= ~COL11;
359                 break;
360         }
361     }
362 }