2 Copyright 2012 Jun Wako <wakojun@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include "split_util.h"
30 #include "pro_micro.h"
33 #ifdef DEBUG_MATRIX_SCAN_RATE
34 #include "matrix_scanrate.h"
42 # error "only i2c supported"
45 #ifndef DEBOUNCING_DELAY
46 # define DEBOUNCING_DELAY 5
50 #if (MATRIX_COLS <= 8)
51 # define print_matrix_header() print("\nr/c 01234567\n")
52 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
53 # define matrix_bitpop(i) bitpop(matrix[i])
54 # define ROW_SHIFTER ((uint8_t)1)
56 # error "Currently only supports 8 COLS"
59 #define ERROR_DISCONNECT_COUNT 5
61 #define ROWS_PER_HAND (MATRIX_ROWS/2)
63 static uint8_t error_count = 0;
65 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
66 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
68 /* matrix state(1:on, 0:off) */
69 static matrix_row_t matrix[MATRIX_ROWS];
70 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
71 static matrix_row_t* debouncing_matrix_hand_offsetted; //pointer to matrix_debouncing for our hand
72 static matrix_row_t* matrix_hand_offsetted; // pointer to matrix for our hand
75 typedef uint8_t debounce_counter_t;
76 #define DEBOUNCE_COUNTER_MODULO 250
77 #define DEBOUNCE_COUNTER_INACTIVE 251
78 static debounce_counter_t debounce_counters[MATRIX_ROWS * MATRIX_COLS];
79 static debounce_counter_t *debounce_counters_hand_offsetted;
82 #if (DIODE_DIRECTION == ROW2COL)
83 error "Only Col2Row supported";
85 static void init_cols(void);
86 static void unselect_rows(void);
87 static void select_row(uint8_t row);
88 static void unselect_row(uint8_t row);
89 static matrix_row_t optimized_col_reader(void);
91 __attribute__ ((weak))
92 void matrix_init_kb(void) {
96 __attribute__ ((weak))
97 void matrix_scan_kb(void) {
101 __attribute__ ((weak))
102 void matrix_init_user(void) {
105 __attribute__ ((weak))
106 void matrix_scan_user(void) {
109 __attribute__ ((weak))
110 void matrix_slave_scan_user(void) {
114 uint8_t matrix_rows(void)
120 uint8_t matrix_cols(void)
125 void matrix_init(void)
128 // JTAG disable for PORT F. write JTD bit twice within four cycles.
134 debug_matrix = false;
136 // initialize row and col
142 // initialize matrix state: all keys off
143 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
145 matrix_debouncing[i] = 0;
147 int my_hand_offset = isLeftHand ? 0 : (ROWS_PER_HAND);
148 debouncing_matrix_hand_offsetted = matrix_debouncing + my_hand_offset;
149 matrix_hand_offsetted = matrix + my_hand_offset;
150 debounce_counters_hand_offsetted = debounce_counters + my_hand_offset;
152 for (uint8_t i = 0; i < MATRIX_ROWS * MATRIX_COLS; i++) {
153 debounce_counters[i] = DEBOUNCE_COUNTER_INACTIVE;
156 matrix_init_quantum();
160 //#define TIMER_DIFF(a, b, max) ((a) >= (b) ? (a) - (b) : (max) - (b) + (a))
161 void update_debounce_counters(uint8_t current_time)
163 debounce_counter_t *debounce_pointer = debounce_counters_hand_offsetted;
164 for (uint8_t row = 0; row < ROWS_PER_HAND; row++)
166 for (uint8_t col = 0; col < MATRIX_COLS; col++)
168 if (*debounce_pointer != DEBOUNCE_COUNTER_INACTIVE)
170 if (TIMER_DIFF(current_time, *debounce_pointer, DEBOUNCE_COUNTER_MODULO) >=
172 *debounce_pointer = DEBOUNCE_COUNTER_INACTIVE;
180 void transfer_matrix_values(uint8_t current_time)
182 //upload from debounce_matrix to final matrix;
183 debounce_counter_t *debounce_pointer = debounce_counters_hand_offsetted;
184 for (uint8_t row = 0; row < ROWS_PER_HAND; row++)
186 matrix_row_t row_value = matrix_hand_offsetted[row];
187 matrix_row_t debounce_value = debouncing_matrix_hand_offsetted[row];
189 for (uint8_t col = 0; col < MATRIX_COLS; col++)
191 bool final_value = debounce_value & (1 << col);
192 bool current_value = row_value & (1 << col);
193 if (*debounce_pointer == DEBOUNCE_COUNTER_INACTIVE
194 && (current_value != final_value))
196 *debounce_pointer = current_time;
197 row_value ^= (1 << col);
201 matrix_hand_offsetted[row] = row_value;
205 uint8_t _matrix_scan(void)
207 uint8_t current_time = timer_read() % DEBOUNCE_COUNTER_MODULO;
209 // Set row, read cols
210 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
211 select_row(current_row);
212 asm volatile ("nop"); asm volatile("nop");
214 debouncing_matrix_hand_offsetted[current_row] = optimized_col_reader();
216 unselect_row(current_row);
219 update_debounce_counters(current_time);
220 transfer_matrix_values(current_time);
226 // Get rows from other half over i2c
227 int i2c_transaction(void) {
228 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
230 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
231 if (err) goto i2c_error;
233 // start of matrix stored at 0x00
234 err = i2c_master_write(I2C_KEYMAP_START);
235 if (err) goto i2c_error;
238 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
239 if (err) goto i2c_error;
243 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
244 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
246 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
249 i2c_error: // the cable is disconnceted, or something else went wrong
257 uint8_t matrix_scan(void)
259 #ifdef DEBUG_MATRIX_SCAN_RATE
260 matrix_check_scan_rate();
261 matrix_time_between_scans();
263 uint8_t ret = _matrix_scan();
265 if( i2c_transaction() ) {
266 // turn on the indicator led when halves are disconnected
271 if (error_count > ERROR_DISCONNECT_COUNT) {
272 // reset other half if disconnected
273 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
274 for (int i = 0; i < ROWS_PER_HAND; ++i) {
275 matrix[slaveOffset+i] = 0;
279 // turn off the indicator led on no error
283 matrix_scan_quantum();
287 void matrix_slave_scan(void) {
290 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
292 for (int i = 0; i < ROWS_PER_HAND; ++i) {
293 i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i];
296 matrix_slave_scan_user();
300 bool matrix_is_on(uint8_t row, uint8_t col)
302 return (matrix[row] & ((matrix_row_t)1<<col));
307 matrix_row_t matrix_get_row(uint8_t row)
312 void matrix_print(void)
314 print("\nr/c 0123456789ABCDEF\n");
315 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
316 phex(row); print(": ");
317 pbin_reverse16(matrix_get_row(row));
322 uint8_t matrix_key_count(void)
325 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
326 count += bitpop16(matrix[i]);
331 static void init_cols(void)
333 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
334 uint8_t pin = col_pins[x];
335 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
336 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
341 static matrix_row_t optimized_col_reader(void) {
342 //MATRIX_COL_PINS { B6, B2, B3, B1, F7, F6, F5, F4 }
343 return (PINB & (1 << 6) ? 0 : (ROW_SHIFTER << 0)) |
344 (PINB & (1 << 2) ? 0 : (ROW_SHIFTER << 1)) |
345 (PINB & (1 << 3) ? 0 : (ROW_SHIFTER << 2)) |
346 (PINB & (1 << 1) ? 0 : (ROW_SHIFTER << 3)) |
347 (PINF & (1 << 7) ? 0 : (ROW_SHIFTER << 4)) |
348 (PINF & (1 << 6) ? 0 : (ROW_SHIFTER << 5)) |
349 (PINF & (1 << 5) ? 0 : (ROW_SHIFTER << 6)) |
350 (PINF & (1 << 4) ? 0 : (ROW_SHIFTER << 7));
354 static void select_row(uint8_t row)
356 uint8_t pin = row_pins[row];
357 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
358 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
361 static void unselect_row(uint8_t row)
363 uint8_t pin = row_pins[row];
364 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
365 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
368 static void unselect_rows(void)
370 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
371 uint8_t pin = row_pins[x];
372 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
373 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI