]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/matrix.c
Add ChibiOS support for QMK (#465)
[qmk_firmware.git] / quantum / matrix.c
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 #endif
23 #include "wait.h"
24 #include "print.h"
25 #include "debug.h"
26 #include "util.h"
27 #include "matrix.h"
28
29 #ifdef MATRIX_HAS_GHOST
30 #   error "The universal matrix.c file cannot be used for this keyboard."
31 #endif
32
33 #ifndef DEBOUNCING_DELAY
34 #   define DEBOUNCING_DELAY 5
35 #endif
36
37 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
38 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
39 /* matrix state */
40 #if DIODE_DIRECTION == COL2ROW
41 static matrix_row_t matrix[MATRIX_ROWS];
42 #else
43 static matrix_col_t matrix[MATRIX_COLS];
44 #endif
45 static int8_t debouncing_delay = -1;
46
47 #if DIODE_DIRECTION == COL2ROW
48 static void toggle_row(uint8_t row);
49 static matrix_row_t read_cols(void);
50 #else
51 static void toggle_col(uint8_t col);
52 static matrix_col_t read_rows(void);
53 #endif
54
55 __attribute__ ((weak))
56 void matrix_init_quantum(void) {
57     matrix_init_kb();
58 }
59
60 __attribute__ ((weak))
61 void matrix_scan_quantum(void) {
62     matrix_scan_kb();
63 }
64
65 __attribute__ ((weak))
66 void matrix_init_kb(void) {
67     matrix_init_user();
68 }
69
70 __attribute__ ((weak))
71 void matrix_scan_kb(void) {
72     matrix_scan_user();
73 }
74
75 __attribute__ ((weak))
76 void matrix_init_user(void) {
77 }
78
79 __attribute__ ((weak))
80 void matrix_scan_user(void) {
81 }
82
83 uint8_t matrix_rows(void) {
84     return MATRIX_ROWS;
85 }
86
87 uint8_t matrix_cols(void) {
88     return MATRIX_COLS;
89 }
90
91 // void matrix_power_up(void) {
92 // #if DIODE_DIRECTION == COL2ROW
93 //     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
94 //         /* DDRxn */
95 //         _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
96 //         toggle_row(r);
97 //     }
98 //     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
99 //         /* PORTxn */
100 //         _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
101 //     }
102 // #else
103 //     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
104 //         /* DDRxn */
105 //         _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
106 //         toggle_col(c);
107 //     }
108 //     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
109 //         /* PORTxn */
110 //         _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
111 //     }
112 // #endif
113 // }
114
115 void matrix_init(void) {
116     /* frees PORTF by setting the JTD bit twice within four cycles */
117     #ifdef __AVR_ATmega32U4__
118         MCUCR |= _BV(JTD);
119         MCUCR |= _BV(JTD);
120     #endif
121     /* initializes the I/O pins */
122 #if DIODE_DIRECTION == COL2ROW
123     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
124         /* DDRxn */
125         _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
126         toggle_row(r);
127     }
128     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
129         /* PORTxn */
130         _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
131     }
132 #else
133     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
134         /* DDRxn */
135         _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
136         toggle_col(c);
137     }
138     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
139         /* PORTxn */
140         _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
141     }
142 #endif
143     matrix_init_quantum();
144 }
145
146 #if DIODE_DIRECTION == COL2ROW
147 uint8_t matrix_scan(void) {
148     static matrix_row_t debouncing_matrix[MATRIX_ROWS];
149     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
150         toggle_row(r);
151         matrix_row_t state = read_cols();
152         if (debouncing_matrix[r] != state) {
153             debouncing_matrix[r] = state;
154             debouncing_delay = DEBOUNCING_DELAY;
155         }
156         toggle_row(r);
157     }
158     if (debouncing_delay >= 0) {
159         dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
160         --debouncing_delay;
161         if (debouncing_delay >= 0) {
162             wait_ms(1);
163         }
164         else {
165             for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
166                 matrix[r] = debouncing_matrix[r];
167             }
168         }
169     }
170     matrix_scan_quantum();
171     return 1;
172 }
173
174 static void toggle_row(uint8_t row) {
175     /* PINxn */
176     _SFR_IO8((row_pins[row] >> 4)) = _BV(row_pins[row] & 0xF);
177 }
178
179 static matrix_row_t read_cols(void) {
180     matrix_row_t state = 0;
181     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
182         /* PINxn */
183         if (!(_SFR_IO8((col_pins[c] >> 4)) & _BV(col_pins[c] & 0xF))) {
184             state |= (matrix_row_t)1 << c;
185         }
186     }
187     return state;
188 }
189
190 matrix_row_t matrix_get_row(uint8_t row) {
191     return matrix[row];
192 }
193
194 #else
195 uint8_t matrix_scan(void) {
196     static matrix_col_t debouncing_matrix[MATRIX_COLS];
197     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
198         toggle_col(c);
199         matrix_col_t state = read_rows();
200         if (debouncing_matrix[c] != state) {
201             debouncing_matrix[c] = state;
202             debouncing_delay = DEBOUNCING_DELAY;
203         }
204         toggle_col(c);
205     }
206     if (debouncing_delay >= 0) {
207         dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
208         --debouncing_delay;
209         if (debouncing_delay >= 0) {
210             wait_ms(1);
211         }
212         else {
213             for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
214                 matrix[c] = debouncing_matrix[c];
215             }
216         }
217     }
218     matrix_scan_quantum();
219     return 1;
220 }
221
222 static void toggle_col(uint8_t col) {
223     /* PINxn */
224     _SFR_IO8((col_pins[col] >> 4)) = _BV(col_pins[col] & 0xF);
225 }
226
227 static matrix_col_t read_rows(void) {
228     matrix_col_t state = 0;
229     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
230         /* PINxn */
231         if (!(_SFR_IO8((row_pins[r] >> 4)) & _BV(row_pins[r] & 0xF))) {
232             state |= (matrix_col_t)1 << r;
233         }
234     }
235     return state;
236 }
237
238 matrix_row_t matrix_get_row(uint8_t row) {
239     matrix_row_t state = 0;
240     matrix_col_t mask = (matrix_col_t)1 << row;
241     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
242         if (matrix[c] & mask) {
243             state |= (matrix_row_t)1 << c;
244         }
245     }
246     return state;
247 }
248
249 #endif
250
251 bool matrix_is_modified(void) {
252     if (debouncing_delay >= 0) return false;
253     return true;
254 }
255
256 bool matrix_is_on(uint8_t row, uint8_t col) {
257     return matrix_get_row(row) & (matrix_row_t)1 << col;
258 }
259
260 void matrix_print(void) {
261     dprintln("Human-readable matrix state:");
262     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
263         dprintf("State of row %X: %016b\n", r, bitrev16(matrix_get_row(r)));
264     }
265 }
266
267 uint8_t matrix_key_count(void) {
268     uint8_t count = 0;
269     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
270         count += bitpop16(matrix_get_row(r));
271     }
272     return count;
273 }