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);
87 debouncing = DEBOUNCING_DELAY;
97 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
98 matrix[i] = matrix_debouncing[i];
103 matrix_scan_quantum();
107 inline matrix_row_t matrix_get_row(uint8_t row) {
111 void 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)));
118 /* Row pin configuration
120 * pin: PB7 PD0 PD1 PD2 PD3 PD5
122 * Esc uses its own pin PE2
124 static void init_rows(void) {
125 DDRD &= ~0b00101111; // PD0, PD1, PD2, PD3, PD5 input
126 PORTD &= ~0b00101111; // PD0, PD1, PD2, PD3, PD5 low
128 DDRB &= ~0b10000000; // PB7 input
129 PORTB &= ~0b10000000; // PB7 low
131 DDRE &= ~0b00000100; // PE2 input
132 PORTE |= 0b00000100; // PE2 high
135 static uint8_t read_rows(uint8_t col) {
137 return PINE&(1<<2) ? 0 : (1<<0); // PE2 (Row 0)
139 return (PIND&(1<<0) ? (1<<0) : 0) | // PD0 (Row 0)
140 (PIND&(1<<1) ? (1<<1) : 0) | // PD1 (Row 1)
141 (PIND&(1<<2) ? (1<<2) : 0) | // PD2 (Row 2)
142 (PIND&(1<<3) ? (1<<3) : 0) | // PD3 (Row 3)
143 (PIND&(1<<5) ? (1<<4) : 0) | // PD5 (Row 4)
144 (PINB&(1<<7) ? (1<<5) : 0); // PB7 (Row 5)
148 uint8_t read_fwkey(void)
150 return PINE&(1<<2) ? 0 : (1<<0);
153 static void unselect_cols(void) {
154 DDRB |= 0b01000000; // PB6 (U2) output
155 PORTB &= ~0b01000000; // PB6 (U2) low
157 DDRC |= 0b11000000; // PC6 (U1), PC7 (A2) output
158 PORTC &= ~0b11000000; // PC6 (U1), PC7 (A2) low
160 DDRF |= 0b00000011; // PF0 (A0), PF1 (A1) output
161 PORTF &= ~0b00000011; // PF0 (A0), PF1 (A1) low
164 static void select_col(uint8_t col) {
168 PORTC |= 0b01000000; // PC6 high
171 PORTC |= 0b01000000; // PC6 high
172 PORTF |= 0b00000001; // PF0 high
175 PORTC |= 0b01000000; // PC6 high
176 PORTF |= 0b00000010; // PF1 high
179 PORTC |= 0b01000000; // PC6 high
180 PORTF |= 0b00000011; // PF0, PF1 high
183 PORTC |= 0b11000000; // PC6, PC7 high
186 PORTC |= 0b11000000; // PC6, PC7 high
187 PORTF |= 0b00000001; // PF0 high
190 PORTC |= 0b11000000; // PC6, PC7 high
191 PORTF |= 0b00000010; // PF1 high
194 PORTC |= 0b11000000; // PC6, PC7 high
195 PORTF |= 0b00000011; // PF0, PF1 high
198 PORTB |= 0b01000000; // PB6 high
201 PORTB |= 0b01000000; // PB6 high
202 PORTF |= 0b00000001; // PF0 high
205 PORTB |= 0b01000000; // PB6 high
206 PORTF |= 0b00000010; // PF1 high
209 PORTB |= 0b01000000; // PB6 high
210 PORTF |= 0b00000011; // PF0, PF1 high
213 PORTB |= 0b01000000; // PB6 high
214 PORTC |= 0b10000000; // PC7 high
217 PORTB |= 0b01000000; // PB6 high
218 PORTF |= 0b00000001; // PF0 high
219 PORTC |= 0b10000000; // PC7 high
222 PORTB |= 0b01000000; // PB6 high
223 PORTF |= 0b00000010; // PF1 high
224 PORTC |= 0b10000000; // PC7 high