2 Copyright 2017 MechMerlin <mechmerlin@gmail.com>
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <util/delay.h>
25 static uint8_t debouncing
= DEBOUNCING_DELAY
;
27 /* matrix state(1:on, 0:off) */
28 static matrix_row_t matrix
[MATRIX_ROWS
];
29 static matrix_row_t matrix_debouncing
[MATRIX_ROWS
];
31 static uint8_t read_rows(uint8_t col
);
32 static void init_rows(void);
33 static void unselect_cols(void);
34 static void select_col(uint8_t col
);
37 __attribute__ ((weak
))
38 void matrix_init_kb(void) {
42 __attribute__ ((weak
))
43 void matrix_scan_kb(void) {
47 __attribute__ ((weak
))
48 void matrix_init_user(void) {
51 __attribute__ ((weak
))
52 void matrix_scan_user(void) {
55 void backlight_init_ports(void)
57 DDRB
|= 0b00011111; // PB0 (caps), PB1 (alpha), PB2 (extra), PB3 (modnum), PB4 (caps)
58 DDRD
|= 0b11010000; // PD4, (rgb blue), PD6 (rgb red), PD7 (rgb green)
59 DDRE
|= 0b01000000; // PE6 (frow)
62 void matrix_init(void) {
63 backlight_init_ports();
67 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
69 matrix_debouncing
[i
] = 0;
72 matrix_init_quantum();
75 uint8_t matrix_scan(void) {
76 for (uint8_t col
= 0; col
< MATRIX_COLS
; col
++) {
80 uint8_t rows
= read_rows(col
);
82 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
83 bool prev_bit
= matrix_debouncing
[row
] & ((matrix_row_t
)1<<col
);
84 bool curr_bit
= rows
& (1<<row
);
85 if (prev_bit
!= curr_bit
) {
86 matrix_debouncing
[row
] ^= ((matrix_row_t
)1<<col
);
88 dprint("bounce!: "); dprintf("%02X", debouncing
); dprintln();
90 debouncing
= DEBOUNCING_DELAY
;
100 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
101 matrix
[i
] = matrix_debouncing
[i
];
106 matrix_scan_quantum();
110 inline matrix_row_t
matrix_get_row(uint8_t row
) {
114 void matrix_print(void) {
115 print("\nr/c 0123456789ABCDEF\n");
116 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
117 xprintf("%02X: %032lb\n", row
, bitrev32(matrix_get_row(row
)));
121 /* Row pin configuration
123 * pin: PB7 PD0 PD1 PD2 PD3 PD5
125 * Esc uses its own pin PE2
127 static void init_rows(void) {
128 DDRD
&= ~0b00101111; // PD0, PD1, PD2, PD3, PD5 input
129 PORTD
&= ~0b00101111; // PD0, PD1, PD2, PD3, PD5 low
131 DDRB
&= ~0b10000000; // PB7 input
132 PORTB
&= ~0b10000000; // PB7 low
134 DDRE
&= ~0b00000100; // PE2 input
135 PORTE
|= 0b00000100; // PE2 high
138 static uint8_t read_rows(uint8_t col
) {
140 return PINE
&(1<<2) ? 0 : (1<<0); // PE2 (Row 0)
142 return (PIND
&(1<<0) ? (1<<0) : 0) | // PD0 (Row 0)
143 (PIND
&(1<<1) ? (1<<1) : 0) | // PD1 (Row 1)
144 (PIND
&(1<<2) ? (1<<2) : 0) | // PD2 (Row 2)
145 (PIND
&(1<<3) ? (1<<3) : 0) | // PD3 (Row 3)
146 (PIND
&(1<<5) ? (1<<4) : 0) | // PD5 (Row 4)
147 (PINB
&(1<<7) ? (1<<5) : 0); // PB7 (Row 5)
151 uint8_t read_fwkey(void)
153 return PINE
&(1<<2) ? 0 : (1<<0);
156 static void unselect_cols(void) {
157 DDRB
|= 0b01000000; // PB6 (U2) output
158 PORTB
&= ~0b01000000; // PB6 (U2) low
160 DDRC
|= 0b11000000; // PC6 (U1), PC7 (A2) output
161 PORTC
&= ~0b11000000; // PC6 (U1), PC7 (A2) low
163 DDRF
|= 0b00000011; // PF0 (A0), PF1 (A1) output
164 PORTF
&= ~0b00000011; // PF0 (A0), PF1 (A1) low
167 static void select_col(uint8_t col
) {
171 PORTC
|= 0b01000000; // PC6 high
174 PORTC
|= 0b01000000; // PC6 high
175 PORTF
|= 0b00000001; // PF0 high
178 PORTC
|= 0b01000000; // PC6 high
179 PORTF
|= 0b00000010; // PF1 high
182 PORTC
|= 0b01000000; // PC6 high
183 PORTF
|= 0b00000011; // PF0, PF1 high
186 PORTC
|= 0b11000000; // PC6, PC7 high
189 PORTC
|= 0b11000000; // PC6, PC7 high
190 PORTF
|= 0b00000001; // PF0 high
193 PORTC
|= 0b11000000; // PC6, PC7 high
194 PORTF
|= 0b00000010; // PF1 high
197 PORTC
|= 0b11000000; // PC6, PC7 high
198 PORTF
|= 0b00000011; // PF0, PF1 high
201 PORTB
|= 0b01000000; // PB6 high
204 PORTB
|= 0b01000000; // PB6 high
205 PORTF
|= 0b00000001; // PF0 high
208 PORTB
|= 0b01000000; // PB6 high
209 PORTF
|= 0b00000010; // PF1 high
212 PORTB
|= 0b01000000; // PB6 high
213 PORTF
|= 0b00000011; // PF0, PF1 high
216 PORTB
|= 0b01000000; // PB6 high
217 PORTC
|= 0b10000000; // PC7 high
220 PORTB
|= 0b01000000; // PB6 high
221 PORTF
|= 0b00000001; // PF0 high
222 PORTC
|= 0b10000000; // PC7 high
225 PORTB
|= 0b01000000; // PB6 high
226 PORTF
|= 0b00000010; // PF1 high
227 PORTC
|= 0b10000000; // PC7 high