1 /* Copyright 2012 Jun Wako <wakojun@gmail.com>: TMK Matrix
2 * Copyright 2018 bakageta <amo@bakageta.com>
4 * This is heavily based on hid_liber/board.{c|h}.
5 * https://github.com/BathroomEpiphanies/AVR-Keyboard
7 * Copyright (c) 2012 Fredrik Atmer, Bathroom Epiphanies Inc
8 * http://bathroomepiphanies.com
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include <util/delay.h>
41 static uint8_t debouncing = DEBOUNCE;
43 // bit array of key state(1:on, 0:off)
44 static matrix_row_t matrix[MATRIX_ROWS];
45 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
48 #define _DDRA (uint8_t *const)&DDRA
49 #define _DDRB (uint8_t *const)&DDRB
50 #define _DDRC (uint8_t *const)&DDRC
51 #define _DDRD (uint8_t *const)&DDRD
52 #define _DDRE (uint8_t *const)&DDRE
53 #define _DDRF (uint8_t *const)&DDRF
55 #define _PINA (uint8_t *const)&PINA
56 #define _PINB (uint8_t *const)&PINB
57 #define _PINC (uint8_t *const)&PINC
58 #define _PIND (uint8_t *const)&PIND
59 #define _PINE (uint8_t *const)&PINE
60 #define _PINF (uint8_t *const)&PINF
62 #define _PORTA (uint8_t *const)&PORTA
63 #define _PORTB (uint8_t *const)&PORTB
64 #define _PORTC (uint8_t *const)&PORTC
65 #define _PORTD (uint8_t *const)&PORTD
66 #define _PORTE (uint8_t *const)&PORTE
67 #define _PORTF (uint8_t *const)&PORTF
78 /* Specifies the ports and pin numbers for the rows */
80 uint8_t *const row_ddr[MATRIX_ROWS] = {
83 _DDRD, _DDRD, _DDRD, _DDRD, _DDRD, _DDRD, _DDRD, _DDRD,
84 _DDRF, _DDRF, _DDRF, _DDRF, _DDRF, _DDRF};
87 uint8_t *const row_port[MATRIX_ROWS] = {
90 _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD,
91 _PORTF, _PORTF, _PORTF, _PORTF, _PORTF, _PORTF};
94 uint8_t *const row_pin[MATRIX_ROWS] = {
97 _PIND, _PIND, _PIND, _PIND, _PIND, _PIND, _PIND, _PIND,
98 _PINF, _PINF, _PINF, _PINF, _PINF, _PINF};
101 const uint8_t row_bit[MATRIX_ROWS] = {
104 _BIT0, _BIT1, _BIT2, _BIT3, _BIT4, _BIT5, _BIT6, _BIT7,
105 _BIT0, _BIT1, _BIT4, _BIT5, _BIT6, _BIT7};
108 const uint8_t mask = 0x0E;
110 /* Specifies the ports and pin numbers for the columns */
112 const uint8_t col_bit[MATRIX_COLS] = { 0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E};
114 __attribute__ ((weak))
115 void matrix_init_kb(void) {
119 __attribute__ ((weak))
120 void matrix_scan_kb(void) {
124 __attribute__ ((weak))
125 void matrix_init_user(void) {
128 __attribute__ ((weak))
129 void matrix_scan_user(void) {
133 inline void pull_column(int col) {
134 PORTB = col_bit[col] | (PORTB & ~mask);
138 inline void release_column(int col) {
141 /* PORTB is set as input with pull-up resistors
142 PORTC,D,E,F are set to high output */
144 void setup_io_pins(void) {
148 for(row = 0; row < MATRIX_ROWS; row++) {
149 *row_ddr[row] &= ~row_bit[row];
150 *row_port[row] &= ~row_bit[row];
155 void setup_leds(void) {
162 uint8_t matrix_rows(void)
168 uint8_t matrix_cols(void)
173 void matrix_init(void)
175 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
179 // initialize row and col
183 // initialize matrix state: all keys off
184 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
186 matrix_debouncing[i] = 0;
189 matrix_init_quantum();
192 uint8_t matrix_scan(void)
194 for (uint8_t col = 0; col < MATRIX_COLS; col++) { // 0-7
195 pull_column(col); // output hi on theline
196 _delay_us(5); // without this wait it won't read stable value.
197 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // 0-17
198 bool prev_bit = matrix_debouncing[row] & (1<<col);
199 bool curr_bit = *row_pin[row] & row_bit[row];
200 if (prev_bit != curr_bit) {
201 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
203 dprintf("bounce!: %02X\n", debouncing);
205 debouncing = DEBOUNCE;
215 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
216 matrix[i] = matrix_debouncing[i];
221 matrix_scan_quantum();
225 bool matrix_is_modified(void)
227 // NOTE: no longer used
232 bool matrix_has_ghost(void)
238 bool matrix_is_on(uint8_t row, uint8_t col)
240 return (matrix[row] & ((matrix_row_t)1<<col));
244 matrix_row_t matrix_get_row(uint8_t row)
249 void matrix_print(void)
251 print("\nr/c 01234567\n");
252 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
253 phex(row); print(": ");
254 pbin_reverse(matrix_get_row(row));
259 uint8_t matrix_key_count(void)
262 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
263 for (uint8_t j = 0; j < MATRIX_COLS; j++) {
264 if (matrix_is_on(i, j))