aboutsummaryrefslogtreecommitdiff
path: root/keyboards/duck/tcv3/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/duck/tcv3/matrix.c')
-rw-r--r--keyboards/duck/tcv3/matrix.c272
1 files changed, 272 insertions, 0 deletions
diff --git a/keyboards/duck/tcv3/matrix.c b/keyboards/duck/tcv3/matrix.c
new file mode 100644
index 000000000..38bc5c882
--- /dev/null
+++ b/keyboards/duck/tcv3/matrix.c
@@ -0,0 +1,272 @@
1/*
2Copyright 2019 MechMerlin <mechmerlin@gmail.com>
3This program is free software: you can redistribute it and/or modify
4it under the terms of the GNU General Public License as published by
5the Free Software Foundation, either version 2 of the License, or
6(at your option) any later version.
7
8This program is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY; without even the implied warranty of
10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License
14along with this program. If not, see <http://www.gnu.org/licenses/>.
15*/
16
17#include <util/delay.h>
18#include <avr/io.h>
19#include <stdio.h>
20#include "matrix.h"
21#include "util.h"
22#include "print.h"
23#include "debug.h"
24#include "debounce.h"
25#include "wait.h"
26
27/* matrix state(1:on, 0:off) */
28static matrix_row_t matrix[MATRIX_ROWS];
29static matrix_row_t matrix_debouncing[MATRIX_ROWS];
30
31static uint8_t read_rows(uint8_t col);
32static void init_rows(void);
33static void unselect_cols(void);
34static void select_col(uint8_t col);
35
36
37__attribute__ ((weak))
38void matrix_init_kb(void) {
39 matrix_init_user();
40}
41
42__attribute__ ((weak))
43void matrix_scan_kb(void) {
44 matrix_scan_user();
45}
46
47__attribute__ ((weak))
48void matrix_init_user(void) {
49}
50
51__attribute__ ((weak))
52void matrix_scan_user(void) {
53}
54
55void backlight_init_ports(void)
56{
57 // DDRD |= 0b11010000;
58 // PORTD &= ~0b01010000;
59 // PORTD |= 0b10000000;
60 // DDRB |= 0b00011111;
61 // PORTB &= ~0b00001110;
62 // PORTB |= 0b00010001;
63 // DDRE |= 0b01000000;
64 // PORTE &= ~0b01000000;
65}
66
67void matrix_init(void) {
68 backlight_init_ports();
69 unselect_cols();
70 init_rows();
71
72 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
73 matrix[i] = 0;
74 matrix_debouncing[i] = 0;
75 }
76
77 debounce_init(MATRIX_ROWS);
78
79 matrix_init_quantum();
80}
81uint8_t matrix_scan(void) {
82 bool changed = false;
83
84 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
85 select_col(col);
86 wait_us(30);
87
88 uint8_t rows = read_rows(col);
89
90 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
91 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
92 bool curr_bit = rows & (1<<row);
93 if (prev_bit != curr_bit) {
94 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
95 changed = true;
96 }
97 }
98 unselect_cols();
99 }
100
101 debounce(matrix, matrix_debouncing, MATRIX_ROWS, changed);
102
103 matrix_scan_quantum();
104 return (uint8_t)changed;
105}
106
107inline matrix_row_t matrix_get_row(uint8_t row) {
108 return matrix[row];
109}
110
111void matrix_print(void) {
112 print("\nr/c 0123456789ABCDEF\n");
113 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
114 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
115 }
116}
117/* Row pin configuration
118 * row: 0 1 2 3 4 5
119 * pin: PB7 PD0 PD1 PD2 PD3 PD5
120 *
121 * Reset Key uses its own pin PE2
122 */
123static void init_rows(void) {
124 DDRD &= ~0b00101111;
125 PORTD &= ~0b00101111;
126
127 DDRB &= ~0b10000000;
128 PORTB &= ~0b10000000;
129
130 DDRE &= ~0b00000100;
131 PORTE |= 0b00000100;
132}
133
134static uint8_t read_rows(uint8_t col) {
135 if (col == 20) {
136 return PINE&(1<<2) ? 0 : (1<<0);
137 } else {
138 return (PIND&(1<<0) ? (1<<1) : 0) |
139 (PIND&(1<<1) ? (1<<2) : 0) |
140 (PIND&(1<<2) ? (1<<3) : 0) |
141 (PIND&(1<<3) ? (1<<4) : 0) |
142 (PIND&(1<<5) ? (1<<5) : 0) |
143 (PINB&(1<<7) ? (1<<0) : 0);
144 }
145}
146
147
148/* Columns 0 - G
149 * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
150 *
151 * atmega32u4 decoder pin
152 * C6 U1 E2
153 * B6 U2 E2
154 * F0 U1, U2 A0
155 * F1 U1, U2 A1
156 * C7 U1, U2 A2
157 *
158 * col 0: B4
159 * col 1: D7
160 * col I: B5
161 *
162 * col / pin: PC6 PB6 PF0 PF1 PC7 Decoder Pin
163 * 2: 1 0 0 0 0 U1 Y0
164 * 3: 1 0 1 0 0 U1 Y1
165 * 4: 1 0 0 1 0 U1 Y2
166 * 5: 1 0 1 1 0 U1 Y3
167 * 6: 1 0 0 0 1 U1 Y4
168 * 7: 1 0 1 0 1 U1 Y5
169 * 8: 1 0 0 1 1 U1 Y6
170 * 9: 1 0 1 1 1 U1 Y7
171 * A: 0 1 0 0 0 U2 Y0
172 * B: 0 1 1 0 0 U2 Y1
173 * C: 0 1 0 1 0 U2 Y2
174 * D: 0 1 1 1 0 U2 Y3
175 * E: 0 1 0 0 1 U2 Y4
176 * F: 0 1 1 0 1 U2 Y5
177 * G: 0 1 0 1 1 U2 Y6
178 * H: 0 1 1 1 1 U2 Y7
179 *
180 */
181static void unselect_cols(void) {
182 DDRB |= 0b01110000;
183 PORTB &= ~0b01110000;
184
185 DDRC |= 0b11000000;
186 PORTC &= ~0b11000000;
187
188 DDRD |= 0b10000000;
189 PORTD &= ~0b10000000;
190
191 DDRF |= 0b00000011;
192 PORTF &= ~0b00000011;
193}
194
195static void select_col(uint8_t col) {
196
197 switch (col) {
198 case 0:
199 PORTB |= 0b00010000;
200 break;
201 case 1:
202 PORTD |= 0b10000000;
203 break;
204 case 2: // U1 Y0
205 PORTC |= 0b01000000;
206 break;
207 case 3: // U1 Y1
208 PORTC |= 0b01000000;
209 PORTF |= 0b00000001;
210 break;
211 case 4: // U1 Y2
212 PORTC |= 0b01000000;
213 PORTF |= 0b00000010;
214 break;
215 case 5: // U1 Y3
216 PORTC |= 0b01000000;
217 PORTF |= 0b00000011;
218 break;
219 case 6: // U1 Y4
220 PORTC |= 0b11000000;
221 break;
222 case 7: // U1 Y5
223 PORTC |= 0b11000000;
224 PORTF |= 0b00000001;
225 break;
226 case 8: // U1 Y6
227 PORTC |= 0b11000000;
228 PORTF |= 0b00000010;
229 break;
230 case 9: // U1 Y7
231 PORTC |= 0b11000000;
232 PORTF |= 0b00000011;
233 break;
234 case 10: // U2 Y0
235 PORTB |= 0b01000000;
236 break;
237 case 11: // U2 Y1
238 PORTB |= 0b01000000;
239 PORTF |= 0b00000001;
240 break;
241 case 12: // U2 Y2
242 PORTB |= 0b01000000;
243 PORTF |= 0b00000010;
244 break;
245 case 13: // U2 Y3
246 PORTB |= 0b01000000;
247 PORTF |= 0b00000011;
248 break;
249 case 14: // U2 Y4
250 PORTB |= 0b01000000;
251 PORTC |= 0b10000000;
252 break;
253 case 15: // U2 Y5
254 PORTB |= 0b01000000;
255 PORTC |= 0b10000000;
256 PORTF |= 0b00000001;
257 break;
258 case 16: // U2 Y6
259 PORTB |= 0b01000000;
260 PORTC |= 0b10000000;
261 PORTF |= 0b00000010;
262 break;
263 case 17: // U2 Y7
264 PORTB |= 0b01000000;
265 PORTC |= 0b10000000;
266 PORTF |= 0b00000011;
267 break;
268 case 18:
269 PORTB |= 0b00100000;
270 break;
271 }
272}