#pragma once #include "driver/gpio.h" #include #include #include #include "freertos/FreeRTOS.h" namespace borla { class BorlaS3I2cBus; // 硬件引脚定义(ESP32-S3 I2C总线) #define TCA_I2C_NUM I2C_NUM_0 #define TCA_SCL_GPIO GPIO_NUM_1 #define TCA_SDA_GPIO GPIO_NUM_2 // 端口定义 Port0 / Port1 enum TCA_PORT : uint8_t { PORT0 = 0, PORT1 = 1, }; // TCA9555 寄存器定义 enum TCA9555_REG : uint8_t { TCA9555_REG_IN0 = 0x00, // Port0 输入寄存器 TCA9555_REG_IN1 = 0x01, // Port1 输入寄存器 TCA9555_REG_OUT0 = 0x02, // Port0 输出寄存器 TCA9555_REG_OUT1 = 0x03, // Port1 输出寄存器 TCA9555_REG_POL0 = 0x04, // Port0 极性反转 TCA9555_REG_POL1 = 0x05, // Port1 极性反转 TCA9555_REG_CFG0 = 0x06, // Port0 方向配置 1=输入 0=输出 TCA9555_REG_CFG1 = 0x07, // Port1 方向配置 }; // TCA9555 硬件地址(原理图 IIC ADDR:0x20) constexpr uint8_t TCA9555_ADDR = 0x20; // 原理图引脚映射宏 // Port0 P0~P7 #define TCA_P0_LEDA 0U #define TCA_P0_SC_RESET 1U #define TCA_P0_ADD_IO 2U #define TCA_P0_OV_RESET 3U #define TCA_P0_FLASH 6U #define TCA_P0_PWDN 7U // Port1 P10~P17 #define TCA_P1_KEY 0U // 输入引脚 P10 Port1 bit0 #define TCA_P1_MCU_VCC 2U #define TCA_P1_5V_EN 3U #define TCA_P1_PREP_VCC 5U #define TCA_P1_IMU_INT 6U #define TCA_P1_PA_CTL 7U //tcap555扩展芯片引脚配置接口 constexpr uint16_t TCA_IO(uint8_t port, uint8_t bit) { return (static_cast(port) << 8) | static_cast(bit); } constexpr uint8_t TCA_GET_PORT(uint16_t pin_code) { return static_cast((pin_code >> 8) & 0xFFU); } constexpr uint8_t TCA_GET_BIT(uint16_t pin_code) { return static_cast(pin_code & 0xFFU); } // PORT0 P00~P07 constexpr uint16_t SC_LEDA = TCA_IO(PORT0, TCA_P0_LEDA); constexpr uint16_t SC_RESET = TCA_IO(PORT0, TCA_P0_SC_RESET); constexpr uint16_t EN_4G = TCA_IO(PORT0, TCA_P0_ADD_IO); // 4G constexpr uint16_t OV_RESET = TCA_IO(PORT0, TCA_P0_OV_RESET); constexpr uint16_t OV_FLASH = TCA_IO(PORT0, TCA_P0_FLASH); constexpr uint16_t OV_PWDN = TCA_IO(PORT0, TCA_P0_PWDN); // PORT1 P10~P17 constexpr uint16_t KEY = TCA_IO(PORT1, TCA_P1_KEY); // 按键输入 constexpr uint16_t MCU_VCC_CTL = TCA_IO(PORT1, TCA_P1_MCU_VCC); constexpr uint16_t V5_EN = TCA_IO(PORT1, TCA_P1_5V_EN); constexpr uint16_t PREP_VCC_CTL = TCA_IO(PORT1, TCA_P1_PREP_VCC); constexpr uint16_t IMU_INT = TCA_IO(PORT1, TCA_P1_IMU_INT); constexpr uint16_t PA_CTL = TCA_IO(PORT1, TCA_P1_PA_CTL); class TCA9555 { public: SemaphoreHandle_t i2c_mtx_ = nullptr; // 传入已初始化完成的I2C总线句柄 explicit TCA9555(BorlaS3I2cBus &bus); ~TCA9555() = default; // 初始化:配置全部端口默认输出0(外设断电) esp_err_t Init(); // 底层寄存器读写 esp_err_t read_reg(uint8_t reg, uint8_t* data); esp_err_t write_reg(uint8_t reg, uint8_t data); // 释放I2C资源 esp_err_t deinit(void); // Port0 位操作 P0~P7 esp_err_t p0_set_bit(uint8_t bit); // 置高 esp_err_t p0_clr_bit(uint8_t bit); // 置低 esp_err_t p0_write_all(uint8_t val); // 整端口输出 // Port1 位操作 P10~P17 esp_err_t p1_set_bit(uint8_t bit); esp_err_t p1_clr_bit(uint8_t bit); esp_err_t p1_write_all(uint8_t val); // 读取输入电平 esp_err_t read_p0_input(uint8_t* val); esp_err_t read_p1_input(uint8_t* val); esp_err_t set_pin(uint16_t pin_code); esp_err_t clr_pin(uint16_t pin_code); private: i2c_master_bus_handle_t bus_ = nullptr; i2c_master_dev_handle_t dev_handle_ = nullptr; BorlaS3I2cBus &bus_ref_; }; } // namespace borla