matrix.c (3994B)
1 // Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com> 2 // Copyright 2017 Erin Call <hello@erincall.com> 3 // Copyright 2023 @frobiac 4 // SPDX-License-Identifier: GPL-2.0-or-later 5 6 // This implements a matrix scan (lite) for the BlackBowl keyboard. 7 // Each side has a dedicated MCP23018 I2C expander. 8 9 #include <stdint.h> 10 #include <stdbool.h> 11 #include <avr/io.h> 12 #include "wait.h" 13 #include "action_layer.h" 14 #include "print.h" 15 #include "debug.h" 16 #include "util.h" 17 #include "matrix.h" 18 #include "blackbowl.h" 19 #include "i2c_master.h" 20 #include "timer.h" 21 22 #define MATRIX_ROWS_PER_SIDE (MATRIX_ROWS / 2) 23 #define ROW_SHIFTER ((matrix_row_t)1) 24 25 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col); 26 27 static uint8_t expander_reset_loop; 28 uint8_t expander_status; 29 const uint8_t expander_input_mask = ((1 << MATRIX_ROWS_PER_SIDE) - 1); // No special mapping, 5 bits [0..4] per side 30 bool i2c_initialized = false; 31 32 static const uint8_t I2C_ADDR_RIGHT = 0x4E; 33 static const uint8_t I2C_ADDR_LEFT = 0x46; 34 static const uint8_t i2c_addr[] = {I2C_ADDR_RIGHT, I2C_ADDR_LEFT}; 35 36 void matrix_init_custom(void) { 37 if (!i2c_initialized) { 38 i2c_init(); 39 wait_ms(1000); 40 } 41 42 // Pin direction and pull-up depends on diode direction and column register: 43 // ROW2COL, GPIOA => input, output 44 uint8_t direction[2] = {0, expander_input_mask}; 45 uint8_t pullup[2] = {0, expander_input_mask}; 46 47 for (uint8_t i = 0; i < 2; ++i) { 48 expander_status = i2c_write_register(i2c_addr[i], IODIRA, direction, 2, I2C_TIMEOUT); 49 if (expander_status) return; 50 51 expander_status = i2c_write_register(i2c_addr[i], GPPUA, pullup, 2, I2C_TIMEOUT); 52 } 53 } 54 55 bool matrix_scan_custom(matrix_row_t current_matrix[]) { 56 bool matrix_has_changed = false; 57 58 if (expander_status) { // if there was an error 59 ++expander_reset_loop; 60 if (++expander_reset_loop == 0) { 61 // since expander_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans 62 // this will be approx bit more frequent than once per second 63 matrix_init_custom(); 64 } 65 } 66 67 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { 68 matrix_has_changed |= read_rows_on_col(current_matrix, current_col); 69 } 70 71 return matrix_has_changed; 72 } 73 74 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { 75 bool matrix_changed = false; 76 uint8_t port = 0xFF & ~(1 << current_col); 77 uint8_t column_state[] = {0, 0}; 78 79 // On both expanders: select col and read rows 80 for (size_t i = 0; i < 2; ++i) { 81 if (!expander_status) { 82 expander_status = i2c_write_register(i2c_addr[i], EXPANDER_COL_REGISTER, &port, 1, I2C_TIMEOUT); 83 } 84 wait_us(30); 85 86 if (expander_status) { 87 return false; 88 } 89 90 expander_status = i2c_read_register(i2c_addr[i], EXPANDER_ROW_REGISTER, &column_state[i], 1, I2C_TIMEOUT); 91 column_state[i] = (~column_state[i]) & ((1 << MATRIX_ROWS_PER_SIDE) - 1); 92 } 93 94 // now map rows 0..4 on each side to cumulative to 0..9 95 uint16_t col_state = column_state[0] | ((column_state[1] << MATRIX_ROWS_PER_SIDE) /*& 0x3e0*/); 96 97 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { 98 // Store last value of row prior to reading 99 matrix_row_t last_row_value = current_matrix[current_row]; 100 101 if (col_state & (1 << current_row)) { 102 // key closed; set state bit in matrix 103 current_matrix[current_row] |= (ROW_SHIFTER << current_col); 104 } else { 105 // key open; clear state bit in matrix 106 current_matrix[current_row] &= ~(ROW_SHIFTER << current_col); 107 } 108 109 // Determine whether the matrix changed state 110 if ((last_row_value != current_matrix[current_row]) && !(matrix_changed)) { 111 matrix_changed = true; 112 } 113 } 114 return matrix_changed; 115 }