]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/gergo/matrix.c
Remove more commented out MCUs
[qmk_firmware.git] / keyboards / gergo / 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 "debounce.h"
29 #include QMK_KEYBOARD_H
30 #ifdef DEBUG_MATRIX_SCAN_RATE
31 #   include  "timer.h"
32 #endif
33
34 #ifdef BALLER
35 #include <avr/interrupt.h>
36 #include "pointing_device.h"
37 #endif
38
39 #ifndef DEBOUNCE
40 #   define DEBOUNCE     5
41 #endif
42
43 // MCP Pin Defs
44 #define RROW1 (1<<3)
45 #define RROW2 (1<<2)
46 #define RROW3 (1<<1)
47 #define RROW4 (1<<0)
48 #define COL0 (1<<0)
49 #define COL1 (1<<1)
50 #define COL2 (1<<2)
51 #define COL3 (1<<3)
52 #define COL4 (1<<4)
53 #define COL5 (1<<5)
54 #define COL6 (1<<6)
55
56 // ATmega pin defs
57 #define ROW1  (1<<6)
58 #define ROW2  (1<<5)
59 #define ROW3  (1<<4)
60 #define ROW4  (1<<1)
61 #define COL7 (1<<0)
62 #define COL8 (1<<1)
63 #define COL9 (1<<2)
64 #define COL10 (1<<3)
65 #define COL11 (1<<2)
66 #define COL12 (1<<3)
67 #define COL13 (1<<6)
68
69 //Trackball pin defs
70 #define TRKUP (1<<4)
71 #define TRKDN (1<<5)
72 #define TRKLT (1<<6)
73 #define TRKRT (1<<7)
74 #define TRKBTN (1<<6)
75
76
77 // Multiple for mouse moves
78 #ifndef TRKSTEP
79 #define TRKSTEP 20
80 #endif
81
82 // multiple for mouse scroll
83 #ifndef SCROLLSTEP
84 #define SCROLLSTEP 5
85 #endif
86
87 // bit masks
88 #define BMASK     (COL7 | COL8 | COL9 | COL10)
89 #define CMASK     (COL13)
90 #define DMASK     (COL11 | COL12)
91 #define FMASK     (ROW1 | ROW2 | ROW3 | ROW4)
92 #define RROWMASK  (RROW1 | RROW2 | RROW3 | RROW4)
93 #define MCPMASK   (COL0 | COL1 | COL2 | COL3 | COL4 | COL5 | COL6)
94 #define TRKMASK   (TRKUP | TRKDN | TRKRT | TRKLT)
95
96 // Trackball interrupts accumulate over here. Processed on scan
97 // Stores prev state of mouse, high bits store direction
98 uint8_t trkState    = 0;
99 uint8_t trkBtnState = 0;
100
101 volatile uint8_t tbUpCnt  = 0;
102 volatile uint8_t tbDnCnt  = 0;
103 volatile uint8_t tbLtCnt  = 0;
104 volatile uint8_t tbRtCnt  = 0;
105
106 /* matrix state(1:on, 0:off) */
107 static matrix_row_t matrix[MATRIX_ROWS];
108 /*
109  * matrix state(1:on, 0:off)
110  * contains the raw values without debounce filtering of the last read cycle.
111  */
112 static matrix_row_t raw_matrix[MATRIX_ROWS];
113
114 // Debouncing: store for each key the number of scans until it's eligible to
115 // change.  When scanning the matrix, ignore any changes in keys that have
116 // already changed in the last DEBOUNCE scans.
117
118 static matrix_row_t read_cols(uint8_t row);
119 static void         init_cols(void);
120 static void         unselect_rows(void);
121 static void         select_row(uint8_t row);
122 static void enableInterrupts(void);
123
124 static uint8_t mcp23018_reset_loop;
125 // static uint16_t mcp23018_reset_loop;
126
127 #ifdef DEBUG_MATRIX_SCAN_RATE
128 uint32_t matrix_timer;
129 uint32_t matrix_scan_count;
130 #endif
131
132
133 __attribute__ ((weak)) void matrix_init_user(void) {}
134
135 __attribute__ ((weak)) void matrix_scan_user(void) {}
136
137 __attribute__ ((weak))
138 void matrix_init_kb(void) {
139   matrix_init_user();
140 }
141
142 __attribute__ ((weak))
143 void matrix_scan_kb(void) {
144   matrix_scan_user();
145 }
146
147 inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
148
149 inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
150
151
152 void matrix_init(void) {
153     // initialize row and col
154     mcp23018_status = init_mcp23018();
155     unselect_rows();
156     init_cols();
157
158   // initialize matrix state: all keys off
159   for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
160     matrix[i]     = 0;
161     raw_matrix[i] = 0;
162   }
163
164 #ifdef DEBUG_MATRIX_SCAN_RATE
165     matrix_timer      = timer_read32();
166     matrix_scan_count = 0;
167 #endif
168     debounce_init(MATRIX_ROWS);
169     matrix_init_quantum();
170 }
171
172 void matrix_power_up(void) {
173     mcp23018_status = init_mcp23018();
174
175     unselect_rows();
176     init_cols();
177
178     // initialize matrix state: all keys off
179     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
180         matrix[i] = 0;
181     }
182
183 #ifdef DEBUG_MATRIX_SCAN_RATE
184     matrix_timer      = timer_read32();
185     matrix_scan_count = 0;
186 #endif
187
188 }
189
190 // Reads and stores a row, returning
191 // whether a change occurred.
192 static inline bool store_raw_matrix_row(uint8_t index) {
193   matrix_row_t temp = read_cols(index);
194   if (raw_matrix[index] != temp) {
195     raw_matrix[index] = temp;
196     return true;
197   }
198   return false;
199 }
200
201
202
203 uint8_t matrix_scan(void) {
204     // TODO: Find what is trashing interrupts
205     enableInterrupts();
206
207     // First we handle the mouse inputs
208 #ifdef BALLER
209     uint8_t pBtn   = PINE & TRKBTN;
210
211     #ifdef DEBUG_BALLER
212     // Compare to previous, mod report
213     if (tbUpCnt + tbDnCnt + tbLtCnt + tbRtCnt != 0)
214         xprintf("U: %d D: %d L: %d R: %d B: %d\n", tbUpCnt, tbDnCnt, tbLtCnt, tbRtCnt, (trkBtnState >> 6));
215     #endif
216
217     // Modify the report
218     report_mouse_t pRprt = pointing_device_get_report();
219
220     // Scroll by default, move on layer
221     if (layer_state == 0) {
222                   pRprt.h += tbLtCnt * SCROLLSTEP; tbLtCnt = 0;
223                   pRprt.h -= tbRtCnt * SCROLLSTEP; tbRtCnt = 0;
224                   pRprt.v -= tbUpCnt * SCROLLSTEP; tbUpCnt = 0;
225                   pRprt.v += tbDnCnt * SCROLLSTEP; tbDnCnt = 0;
226     } else {
227                   pRprt.x -= tbLtCnt * TRKSTEP * (layer_state - 1); tbLtCnt = 0;
228                   pRprt.x += tbRtCnt * TRKSTEP * (layer_state - 1); tbRtCnt = 0;
229                   pRprt.y -= tbUpCnt * TRKSTEP * (layer_state - 1); tbUpCnt = 0;
230                   pRprt.y += tbDnCnt * TRKSTEP * (layer_state - 1); tbDnCnt = 0;
231     }
232
233 #ifdef DEBUG_BALLER
234     if (pRprt.x != 0 || pRprt.y != 0)
235         xprintf("X: %d Y: %d\n", pRprt.x, pRprt.y);
236 #endif
237
238     if ((pBtn != trkBtnState) && ((pBtn >> 6) == 0))  pRprt.buttons |= MOUSE_BTN1;
239     if ((pBtn != trkBtnState) && ((pBtn >> 6) == 1))  pRprt.buttons &= ~MOUSE_BTN1;
240
241     // Save state, push update
242     if (pRprt.x != 0 || pRprt.y != 0 || pRprt.h != 0 || pRprt.v != 0 || (trkBtnState != pBtn))
243         pointing_device_set_report(pRprt);
244
245     trkBtnState = pBtn;
246 #endif
247
248     // Then the keyboard
249     if (mcp23018_status) {  // if there was an error
250         if (++mcp23018_reset_loop == 0) {
251             // if (++mcp23018_reset_loop >= 1300) {
252             // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
253             // this will be approx bit more frequent than once per second
254             print("trying to reset mcp23018\n");
255             mcp23018_status = init_mcp23018();
256             if (mcp23018_status) {
257                 print("left side not responding\n");
258             } else {
259                 print("left side attached\n");
260             }
261         }
262     }
263
264 #ifdef DEBUG_MATRIX_SCAN_RATE
265     matrix_scan_count++;
266
267     uint32_t timer_now = timer_read32();
268     if (TIMER_DIFF_32(timer_now, matrix_timer) > 1000) {
269         print("matrix scan frequency: ");
270         pdec(matrix_scan_count);
271         print("\n");
272
273         matrix_timer      = timer_now;
274         matrix_scan_count = 0;
275     }
276 #endif
277
278     bool changed = false;
279     for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
280         // select rows from left and right hands
281         uint8_t left_index = i;
282         uint8_t right_index = i + MATRIX_ROWS_PER_SIDE;
283         select_row(left_index);
284         select_row(right_index);
285
286         // we don't need a 30us delay anymore, because selecting a
287         // left-hand row requires more than 30us for i2c.
288
289         changed |= store_raw_matrix_row(left_index);
290         changed |= store_raw_matrix_row(right_index);
291
292         unselect_rows();
293     }
294
295     debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
296     matrix_scan_quantum();
297
298     enableInterrupts();
299
300 #ifdef DEBUG_MATRIX
301     for (uint8_t c = 0; c < MATRIX_COLS; c++)
302                 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
303                   if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
304 #endif
305
306     return 1;
307 }
308
309 bool matrix_is_modified(void) // deprecated and evidently not called.
310 {
311     return true;
312 }
313
314 inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
315
316 inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
317
318 void matrix_print(void) {
319     print("\nr/c 0123456789ABCDEF\n");
320     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
321         phex(row); print(": ");
322         pbin_reverse16(matrix_get_row(row));
323         print("\n");
324     }
325 }
326
327 uint8_t matrix_key_count(void) {
328     uint8_t count = 0;
329     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
330         count += bitpop16(matrix[i]);
331     }
332     return count;
333 }
334
335 // Remember this means ROWS
336 static void  init_cols(void) {
337     // init on mcp23018
338     // not needed, already done as part of init_mcp23018()
339
340     // Input with pull-up(DDR:0, PORT:1)
341     DDRF  &= ~FMASK;
342     PORTF |=  FMASK;
343 }
344
345 static matrix_row_t read_cols(uint8_t row) {
346     if (row < 7) {
347         if (mcp23018_status) { // if there was an error
348             return 0;
349         } else {
350             uint8_t data = 0;
351             mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT);    if (mcp23018_status) goto out;
352             mcp23018_status = i2c_write(GPIOB, I2C_TIMEOUT);             if (mcp23018_status) goto out;
353             mcp23018_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT);     if (mcp23018_status) goto out;
354             mcp23018_status = i2c_read_nack(I2C_TIMEOUT);                if (mcp23018_status < 0) goto out;
355             data = ~((uint8_t)mcp23018_status);
356             mcp23018_status = I2C_STATUS_SUCCESS;
357         out:
358             i2c_stop();
359
360 #ifdef DEBUG_MATRIX
361             if (data != 0x00) xprintf("I2C: %d\n", data);
362 #endif
363             return data;
364         }
365     } else {
366          /* read from teensy
367                 * bitmask is 0b0111001, but we want the lower four
368                 * we'll return 1s for the top two, but that's harmless.
369                 */
370         // So I need to confuckulate all this
371         //return ~(((PIND & DMASK) >> 1  | ((PINC & CMASK) >> 6) | (PIN)));
372         //return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
373         return ~(
374            (((PINF & ROW4) >> 1)
375           | ((PINF & (ROW1 | ROW2 | ROW3)) >> 3))
376         & 0xF);
377     }
378 }
379
380 // Row pin configuration
381 static void unselect_rows(void)
382 {
383     // no need to unselect on mcp23018, because the select step sets all
384     // the other row bits high, and it's not changing to a different
385     // direction
386     // Hi-Z(DDR:0, PORT:0) to unselect
387     DDRB  &= ~(BMASK | TRKMASK);
388     PORTB &= ~(BMASK);
389     DDRC  &= ~CMASK;
390     PORTC &= ~CMASK;
391     DDRD  &= ~DMASK;
392     PORTD &= ~DMASK;
393
394         // Fix trashing of DDRB for TB
395     PORTB |= TRKMASK;
396 }
397
398 static void select_row(uint8_t row)
399 {
400     if (row < 7) {
401         // select on mcp23018
402         if (mcp23018_status) { // do nothing on error
403         } else { // set active row low  : 0 // set other rows hi-Z : 1
404             mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT);        if (mcp23018_status) goto out;
405             mcp23018_status = i2c_write(GPIOA, I2C_TIMEOUT);                 if (mcp23018_status) goto out;
406             mcp23018_status = i2c_write(0xFF & ~(1<<row), I2C_TIMEOUT);      if (mcp23018_status) goto out;
407         out:
408             i2c_stop();
409         }
410     } else {
411         // Output low(DDR:1, PORT:0) to select
412         switch (row) {
413             case 7:
414                 DDRB  |= COL7;
415                 PORTB &= ~COL7;
416                 break;
417             case 8:
418                 DDRB  |= COL8;
419                 PORTB &= ~COL8;
420                 break;
421             case 9:
422                 DDRB  |= COL9;
423                 PORTB &= ~COL9;
424                 break;
425             case 10:
426                 DDRB  |= COL10;
427                 PORTB &= ~COL10;
428                 break;
429             case 11:
430                 DDRD  |= COL11;
431                 PORTD &= ~COL11;
432                 break;
433             case 12:
434                 DDRD  |= COL12;
435                 PORTD &= ~COL12;
436                 break;
437             case 13:
438                 DDRC  |= COL13;
439                 PORTC &= ~COL13;
440                 break;
441         }
442     }
443 }
444
445
446 // Trackball Interrupts
447 static void enableInterrupts(void) {
448     #ifdef BALLER
449       // Set interrupt mask
450       // Set port defs
451       DDRB &= ~TRKMASK;
452       PORTB |= TRKMASK;
453       DDRE &= ~TRKBTN;
454       PORTE |= TRKBTN;
455
456       // Interrupt shenanigans
457       //EIMSK |= (1 << PCIE0);
458       PCMSK0 |= TRKMASK;
459       PCICR |= (1 << PCIE0);
460       sei();
461     #endif
462
463     return;
464 }
465 #ifdef BALLER
466 ISR (PCINT0_vect) {
467   // Don't get fancy, we're in a interrupt here
468   // PCINT reports a interrupt for a change on the bus
469   // We hand the button at scantime for debounce
470   volatile uint8_t pState = PINB & TRKMASK;
471   if ((pState & TRKUP) != (trkState & TRKUP)) tbUpCnt++;
472   if ((pState & TRKDN) != (trkState & TRKDN)) tbDnCnt++;
473   if ((pState & TRKLT) != (trkState & TRKLT)) tbLtCnt++;
474   if ((pState & TRKRT) != (trkState & TRKRT)) tbRtCnt++;
475   trkState = pState;
476
477 }
478 #endif