This commit is contained in:
2026-05-08 23:46:39 +02:00
parent 469177e5cb
commit 0a7e80a16a
9 changed files with 1855 additions and 144 deletions
+18 -12
View File
@@ -4,21 +4,28 @@
#include <stdint.h>
#include <string.h>
i2c_master_dev_handle_t tca8418_i2c_dev;
KeyboardState_t keyboardState;
void tca8418_init(void) {
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_KP_GPIO1, 0x7F);
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_KP_GPIO2, 0xFF);
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_KP_GPIO3, 0x00);
i2c_device_config_t dev_cfg = {
.device_address = KEYBOARD_I2C_ADDRESS,
.scl_speed_hz = 400000,
};
i2c_master_bus_add_device(bus, &dev_cfg, &tca8418_i2c_dev);
i2c_write_reg(tca8418_i2c_dev, TCA8418_REG_KP_GPIO1, 0x7F);
i2c_write_reg(tca8418_i2c_dev, TCA8418_REG_KP_GPIO2, 0xFF);
i2c_write_reg(tca8418_i2c_dev, TCA8418_REG_KP_GPIO3, 0x00);
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_CFG,
i2c_write_reg(tca8418_i2c_dev, TCA8418_REG_CFG,
TCA8418_CFG_INT_ENABLE | TCA8418_CFG_OVERFLOW_MODE);
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_UNLOCK1, 0x00);
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_UNLOCK2, 0x00);
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_KP_LCK_TIMER, 0x00);
i2c_write_reg(tca8418_i2c_dev, TCA8418_REG_UNLOCK1, 0x00);
i2c_write_reg(tca8418_i2c_dev, TCA8418_REG_UNLOCK2, 0x00);
i2c_write_reg(tca8418_i2c_dev, TCA8418_REG_KP_LCK_TIMER, 0x00);
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_INT_STAT, 0xFF);
i2c_write_reg(tca8418_i2c_dev, TCA8418_REG_INT_STAT, 0xFF);
memset(&keyboardState, 0, sizeof(keyboardState));
}
@@ -26,7 +33,7 @@ int tca8418_read(uint8_t *key, uint8_t *pressed) {
uint8_t count;
// read event counter
i2c_read_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_KEY_LCK_EC, &count);
i2c_read_reg(tca8418_i2c_dev, TCA8418_REG_KEY_LCK_EC, &count);
count &= 0x0F;
@@ -37,7 +44,7 @@ int tca8418_read(uint8_t *key, uint8_t *pressed) {
uint8_t ev;
// reading this register POPS one FIFO event
i2c_read_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_KEY_EVENT_A, &ev);
i2c_read_reg(tca8418_i2c_dev, TCA8418_REG_KEY_EVENT_A, &ev);
// bit7:
// 0 = press
@@ -52,8 +59,7 @@ int tca8418_read(uint8_t *key, uint8_t *pressed) {
return 0;
// clear ONLY key event interrupt
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_INT_STAT,
TCA8418_INT_KEY_EVENT);
i2c_write_reg(tca8418_i2c_dev, TCA8418_REG_INT_STAT, TCA8418_INT_KEY_EVENT);
// update key state table
keyboardState.pressedKeys[*key] = *pressed;