matrix.c (3087B)
1 // Copyright 2023 Kyle McCreery 2 // SPDX-License-Identifier: GPL-2.0-or-later 3 4 #include "matrix.h" 5 #include "mcp23018.h" 6 #include "wait.h" 7 #include "debug.h" 8 #include "encoder.h" 9 10 #define I2C_ADDR 0x20 11 #define ROW_POS { 0b01000000, 0b10000000, 0b01000000, 0b10000000, 0b00000100, 0b00010000, 0b00100000, 0b00000010, 0b00001000 } 12 13 static uint8_t mcp23018_errors = 0; 14 15 static void mcp23018_init_cols(void) { 16 mcp23018_errors += !mcp23018_set_config(I2C_ADDR, mcp23018_PORTA, ALL_INPUT); 17 mcp23018_errors += !mcp23018_set_config(I2C_ADDR, mcp23018_PORTB, ALL_INPUT); 18 } 19 20 static void mcp23018_scan(void) { 21 if (!mcp23018_errors) { 22 return; 23 } 24 25 static uint16_t mcp23018_reset_loop = 0; 26 if (++mcp23018_reset_loop > 0x1FFF) { 27 // tuned to about 5s given the current scan rate 28 dprintf("trying to reset mcp23018\n"); 29 mcp23018_reset_loop = 0; 30 mcp23018_errors = 0; 31 mcp23018_init_cols(); 32 } 33 } 34 35 static matrix_row_t read_cols(void) { 36 if (mcp23018_errors) { 37 return 0; 38 } 39 40 uint8_t ret = 0xFF; // sets all to 1 41 mcp23018_errors += !mcp23018_read_pins(I2C_ADDR, mcp23018_PORTB, &ret); // will update with values 0 = pulled down by connection, 1 = pulled up by pullup resistors 42 43 return (~ret) & 0b00111111; // Clears out the two row bits in the B buffer. 44 } 45 46 static void select_row(uint8_t row) { 47 uint8_t row_pos[MATRIX_ROWS] = ROW_POS; 48 if (mcp23018_errors) { 49 // wait to mimic i2c interactions 50 //wait_us(100); 51 return; 52 } 53 54 if (row > 1) { 55 mcp23018_errors += !mcp23018_set_config(I2C_ADDR, mcp23018_PORTB, ALL_INPUT); 56 mcp23018_errors += !mcp23018_set_config(I2C_ADDR, mcp23018_PORTA, ~(row_pos[row])); 57 } else { 58 mcp23018_errors += !mcp23018_set_config(I2C_ADDR, mcp23018_PORTA, ALL_INPUT); 59 mcp23018_errors += !mcp23018_set_config(I2C_ADDR, mcp23018_PORTB, ~(row_pos[row])); 60 } 61 } 62 63 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 64 // Store last value of row prior to reading 65 matrix_row_t last_row_value = current_matrix[current_row]; 66 67 // Clear data in matrix row 68 current_matrix[current_row] = 0; 69 70 // Select row and wait for row selection to stabilize 71 select_row(current_row); 72 // Skip the wait_us(30); as i2c is slow enough to debounce the io changes 73 74 current_matrix[current_row] = read_cols(); 75 76 return (last_row_value != current_matrix[current_row]); 77 } 78 79 void matrix_init_custom(void) { 80 mcp23018_init(I2C_ADDR); 81 mcp23018_init_cols(); 82 } 83 84 bool matrix_scan_custom(matrix_row_t current_matrix[]) { 85 mcp23018_scan(); 86 87 bool changed = false; 88 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { 89 changed |= read_cols_on_row(current_matrix, current_row); 90 91 #ifdef ENCODER_ENABLE 92 // Need to frequently read the encoder pins while scanning because the I/O expander takes a long time in comparison. 93 encoder_driver_task(); 94 #endif 95 } 96 return changed; 97 }