|
@@ -0,0 +1,282 @@
|
|
|
|
+
|
|
|
|
+Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
|
|
|
|
+ 2020 Pierre Chevalier <pierrechevalier83@gmail.com>
|
|
|
|
+
|
|
|
|
+This program is free software: you can redistribute it and/or modify
|
|
|
|
+it under the terms of the GNU General Public License as published by
|
|
|
|
+the Free Software Foundation, either version 2 of the License, or
|
|
|
|
+(at your option) any later version.
|
|
|
|
+
|
|
|
|
+This program is distributed in the hope that it will be useful,
|
|
|
|
+but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
+GNU General Public License for more details.
|
|
|
|
+
|
|
|
|
+You should have received a copy of the GNU General Public License
|
|
|
|
+along with this program. If not, see <http:
|
|
|
|
+*/
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * This code was heavily inspired by the ergodox_ez keymap, and modernized
|
|
|
|
+ * to take advantage of the quantum.h microcontroller agnostics gpio control
|
|
|
|
+ * abstractions and use the macros defined in config.h for the wiring as opposed
|
|
|
|
+ * to repeating that information all over the place.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#include QMK_KEYBOARD_H
|
|
|
|
+#include "i2c_master.h"
|
|
|
|
+
|
|
|
|
+extern i2c_status_t mcp23017_status;
|
|
|
|
+#define I2C_TIMEOUT 1000
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#define I2C_ADDR 0b0100000
|
|
|
|
+#define I2C_ADDR_WRITE ((I2C_ADDR << 1) | I2C_WRITE)
|
|
|
|
+#define I2C_ADDR_READ ((I2C_ADDR << 1) | I2C_READ)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#define IODIRA 0x00
|
|
|
|
+#define IODIRB 0x01
|
|
|
|
+#define GPPUA 0x0C
|
|
|
|
+#define GPPUB 0x0D
|
|
|
|
+#define GPIOA 0x12
|
|
|
|
+#define GPIOB 0x13
|
|
|
|
+#define OLATA 0x14
|
|
|
|
+#define OLATB 0x15
|
|
|
|
+
|
|
|
|
+bool i2c_initialized = 0;
|
|
|
|
+i2c_status_t mcp23017_status = I2C_ADDR;
|
|
|
|
+
|
|
|
|
+uint8_t init_mcp23017(void) {
|
|
|
|
+ print("starting init");
|
|
|
|
+ mcp23017_status = I2C_ADDR;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (i2c_initialized == 0) {
|
|
|
|
+ i2c_init();
|
|
|
|
+ i2c_initialized = true;
|
|
|
|
+ wait_ms(I2C_TIMEOUT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ mcp23017_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT);
|
|
|
|
+ if (mcp23017_status) goto out;
|
|
|
|
+ mcp23017_status = i2c_write(IODIRA, I2C_TIMEOUT);
|
|
|
|
+ if (mcp23017_status) goto out;
|
|
|
|
+
|
|
|
|
+ mcp23017_status = i2c_write(0b11111111, I2C_TIMEOUT);
|
|
|
|
+ if (mcp23017_status) goto out;
|
|
|
|
+
|
|
|
|
+ mcp23017_status = i2c_write(0b11110000, I2C_TIMEOUT);
|
|
|
|
+ if (mcp23017_status) goto out;
|
|
|
|
+ i2c_stop();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ mcp23017_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT);
|
|
|
|
+ if (mcp23017_status) goto out;
|
|
|
|
+ mcp23017_status = i2c_write(GPPUA, I2C_TIMEOUT);
|
|
|
|
+ if (mcp23017_status) goto out;
|
|
|
|
+
|
|
|
|
+ mcp23017_status = i2c_write(0b11111111, I2C_TIMEOUT);
|
|
|
|
+ if (mcp23017_status) goto out;
|
|
|
|
+
|
|
|
|
+ mcp23017_status = i2c_write(0b11110000, I2C_TIMEOUT);
|
|
|
|
+ if (mcp23017_status) goto out;
|
|
|
|
+
|
|
|
|
+out:
|
|
|
|
+ i2c_stop();
|
|
|
|
+ return mcp23017_status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static matrix_row_t matrix[MATRIX_ROWS];
|
|
|
|
+
|
|
|
|
+static matrix_row_t read_cols(uint8_t row);
|
|
|
|
+static void init_cols(void);
|
|
|
|
+static void unselect_rows(void);
|
|
|
|
+static void select_row(uint8_t row);
|
|
|
|
+
|
|
|
|
+static uint8_t mcp23017_reset_loop;
|
|
|
|
+
|
|
|
|
+void matrix_init_custom(void) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ mcp23017_status = init_mcp23017();
|
|
|
|
+
|
|
|
|
+ unselect_rows();
|
|
|
|
+ init_cols();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
|
|
|
|
+ matrix[i] = 0;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void matrix_power_up(void) {
|
|
|
|
+ mcp23017_status = init_mcp23017();
|
|
|
|
+
|
|
|
|
+ unselect_rows();
|
|
|
|
+ init_cols();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
|
|
|
|
+ matrix[i] = 0;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static inline bool store_matrix_row(matrix_row_t current_matrix[], uint8_t index) {
|
|
|
|
+ matrix_row_t temp = read_cols(index);
|
|
|
|
+ if (current_matrix[index] != temp) {
|
|
|
|
+ current_matrix[index] = temp;
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+bool matrix_scan_custom(matrix_row_t current_matrix[]) {
|
|
|
|
+ if (mcp23017_status) {
|
|
|
|
+ if (++mcp23017_reset_loop == 0) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ dprint("trying to reset mcp23017\n");
|
|
|
|
+ mcp23017_status = init_mcp23017();
|
|
|
|
+ if (mcp23017_status) {
|
|
|
|
+ dprint("right side not responding\n");
|
|
|
|
+ } else {
|
|
|
|
+ dprint("right side attached\n");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ bool changed = false;
|
|
|
|
+ for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
|
|
|
|
+
|
|
|
|
+ uint8_t left_index = i;
|
|
|
|
+ uint8_t right_index = i + MATRIX_ROWS_PER_SIDE;
|
|
|
|
+ select_row(left_index);
|
|
|
|
+ select_row(right_index);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ changed |= store_matrix_row(current_matrix, left_index);
|
|
|
|
+ changed |= store_matrix_row(current_matrix, right_index);
|
|
|
|
+
|
|
|
|
+ unselect_rows();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return changed;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void init_cols(void) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ pin_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_MCU;
|
|
|
|
+ for (int pin_index = 0; pin_index < MATRIX_COLS_PER_SIDE; pin_index++) {
|
|
|
|
+ pin_t pin = matrix_col_pins_mcu[pin_index];
|
|
|
|
+ setPinInput(pin);
|
|
|
|
+ writePinHigh(pin);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static matrix_row_t read_cols(uint8_t row) {
|
|
|
|
+ if (row < MATRIX_ROWS_PER_SIDE) {
|
|
|
|
+ pin_t matrix_col_pins_mcu[MATRIX_COLS_PER_SIDE] = MATRIX_COL_PINS_MCU;
|
|
|
|
+ matrix_row_t current_row_value = 0;
|
|
|
|
+
|
|
|
|
+ for (uint8_t col_index = 0; col_index < MATRIX_COLS_PER_SIDE; col_index++) {
|
|
|
|
+
|
|
|
|
+ uint8_t pin_state = readPin(matrix_col_pins_mcu[col_index]);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
|
|
|
|
+ }
|
|
|
|
+ return current_row_value;
|
|
|
|
+ } else {
|
|
|
|
+ if (mcp23017_status) {
|
|
|
|
+ return 0;
|
|
|
|
+ } else {
|
|
|
|
+ uint8_t data = 0;
|
|
|
|
+ mcp23017_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT);
|
|
|
|
+ if (mcp23017_status) goto out;
|
|
|
|
+ mcp23017_status = i2c_write(GPIOA, I2C_TIMEOUT);
|
|
|
|
+ if (mcp23017_status) goto out;
|
|
|
|
+ mcp23017_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT);
|
|
|
|
+ if (mcp23017_status) goto out;
|
|
|
|
+ mcp23017_status = i2c_read_nack(I2C_TIMEOUT);
|
|
|
|
+ if (mcp23017_status < 0) goto out;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ data = ~((uint8_t)mcp23017_status);
|
|
|
|
+ mcp23017_status = I2C_STATUS_SUCCESS;
|
|
|
|
+ out:
|
|
|
|
+ i2c_stop();
|
|
|
|
+
|
|
|
|
+ return data;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void unselect_rows(void) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_MCU;
|
|
|
|
+ for (int pin_index = 0; pin_index < MATRIX_ROWS_PER_SIDE; pin_index++) {
|
|
|
|
+ pin_t pin = matrix_row_pins_mcu[pin_index];
|
|
|
|
+ setPinInput(pin);
|
|
|
|
+ writePinLow(pin);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void select_row(uint8_t row) {
|
|
|
|
+ if (row < MATRIX_ROWS_PER_SIDE) {
|
|
|
|
+
|
|
|
|
+ pin_t matrix_row_pins_mcu[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS_MCU;
|
|
|
|
+ pin_t pin = matrix_row_pins_mcu[row];
|
|
|
|
+ setPinOutput(pin);
|
|
|
|
+ writePinLow(pin);
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ if (mcp23017_status) {
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ mcp23017_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT);
|
|
|
|
+ if (mcp23017_status) goto out;
|
|
|
|
+ mcp23017_status = i2c_write(GPIOB, I2C_TIMEOUT);
|
|
|
|
+ if (mcp23017_status) goto out;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ mcp23017_status = i2c_write(0xFF & ~(1 << (row - MATRIX_ROWS_PER_SIDE)), I2C_TIMEOUT);
|
|
|
|
+
|
|
|
|
+ if (mcp23017_status) goto out;
|
|
|
|
+ out:
|
|
|
|
+ i2c_stop();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|