diff --git a/.vscode/settings.json b/.vscode/settings.json index e814114..28aaaad 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -82,7 +82,8 @@ "xstring": "cpp", "xutility": "cpp", "charconv": "cpp", - "format": "cpp" + "format": "cpp", + "bq4050.h": "c" }, "cmake.configureOnOpen": false } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 5c5d295..8328b7d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,4 +6,4 @@ set(EXTRA_COMPONENT_DIRS "./include") include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(SK231024827118) +project(CLSH1001) diff --git a/include/bq4050/CMakeLists.txt b/include/bq4050/CMakeLists.txt index 4818bcf..11fa19d 100644 --- a/include/bq4050/CMakeLists.txt +++ b/include/bq4050/CMakeLists.txt @@ -1,3 +1,3 @@ -idf_component_register(SRCS "bq4050.cpp" +idf_component_register(SRCS "bq4050.c" INCLUDE_DIRS "." REQUIRES driver) \ No newline at end of file diff --git a/include/bq4050/bq4050.cpp b/include/bq4050/bq4050.c similarity index 93% rename from include/bq4050/bq4050.cpp rename to include/bq4050/bq4050.c index 64fc4ce..a0dcee1 100644 --- a/include/bq4050/bq4050.cpp +++ b/include/bq4050/bq4050.c @@ -3,25 +3,28 @@ #include "driver/i2c.h" #include "esp_log.h" +void bq_Init(i2c_port_t I2C_NUM, uint8_t I2C_address, void *(esp_err_t)bq4050_err_cb) +{ + __BQ4050__I2C_port__ = I2C_NUM; + __BQ4050__I2C_address__ = I2C_address; + __BQ4050_ERR_CB__ = bq4050_err_cb; +} + void ESP_I2C_ERROR_CHECK(esp_err_t errcode) { if (errcode != ESP_OK) { - ESP_LOGI(BQ4050::BQ4050_TAG, "TinyUSB driver error\n:)"); - while (1) - { - vTaskDelay(1000 / portTICK_PERIOD_MS); - } + __BQ4050_ERR_CB__(errcode); } } esp_err_t bq4050_register_read(uint8_t reg_addr, uint8_t *data, size_t len) { - return i2c_master_write_read_device(BQ4050::I2C_MASTER, BQ4050_ADDR, ®_addr, 1, data, len, BQ4050::I2C_MASTER_TIMEOUT / portTICK_PERIOD_MS); + return i2c_master_write_read_device(__BQ4050__I2C_port__, __BQ4050__I2C_address__, ®_addr, 1, data, len, 14000 / portTICK_PERIOD_MS); } esp_err_t bq4050_register_write(uint8_t *data, size_t len) { - return i2c_master_write_to_device(BQ4050::I2C_MASTER, BQ4050_ADDR, data, len, BQ4050::I2C_MASTER_TIMEOUT / portTICK_PERIOD_MS); + return i2c_master_write_to_device(__BQ4050__I2C_port__, __BQ4050__I2C_address__, data, len, 14000 / portTICK_PERIOD_MS); } // inblock前面必须加0x44地址 diff --git a/include/bq4050/bq4050.h b/include/bq4050/bq4050.h index 4860bd2..3dcd71e 100644 --- a/include/bq4050/bq4050.h +++ b/include/bq4050/bq4050.h @@ -1,21 +1,14 @@ #ifndef __BQ4050_H__ #define __BQ4050_H__ -#include #include "driver/i2c.h" #include "esp_log.h" -// BQ4050配置 -#define BQ4050_ADDR 0x0B +static i2c_port_t __BQ4050__I2C_port__; +static uint8_t __BQ4050__I2C_address__; +static void *(esp_err_t)__BQ4050_ERR_CB__; -namespace BQ4050 -{ - static i2c_port_t I2C_MASTER; - static int I2C_MASTER_TIMEOUT; - static const char *BQ4050_TAG = "BQ4050"; -} - -void bq_Init(); +void bq_Init(i2c_port_t I2C_NUM, uint8_t I2C_address, void *(esp_err_t)bq4050_err_cb); uint8_t bq_BattState(); // Return CHG/DSG(0xF?/0x0?), OK/Bad(0x?0/0x?F) uint16_t bq_GetAdvState(); // Return XDSG/XCHG/PF/SS/FC/FD/TAPR/VCT/CB/SMTH/SU/IN/VDQ/FCCX/EDV2/EDV1 uint16_t bq_GetVoltage(); // Unit: mV diff --git a/include/sw7203/CMakeLists.txt b/include/sw7203/CMakeLists.txt new file mode 100644 index 0000000..d84dd4e --- /dev/null +++ b/include/sw7203/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "sw7203.c" + INCLUDE_DIRS "." + REQUIRES driver) \ No newline at end of file diff --git a/include/sw7203/sw7203.c b/include/sw7203/sw7203.c new file mode 100644 index 0000000..e69de29 diff --git a/include/sw7203/sw7203.h b/include/sw7203/sw7203.h new file mode 100644 index 0000000..09d895b --- /dev/null +++ b/include/sw7203/sw7203.h @@ -0,0 +1,156 @@ +#ifndef __SW7203_H__ +#define __SW7203_H__ + +#include "driver/i2c.h" +#include "esp_log.h" + +typedef struct sw7203_i2c_config +{ + i2c_port_t i2c_num; + uint8_t i2c_address; +}; + +union adc_config +{ + uint8_t hex; + struct + { + uint8_t adc_filter_time : 2; // 0: 10mS 1:20mS 2:40mS 4: 80mS + uint8_t : 2; + uint8_t adc_data : 4; // 0: VBAT电压 1: VBUS电压 4: VBAT充电电流 5: VBUS 充电电流 6: VBAT 放电电流 7:VBUS 放电电流 8: 芯片温度 9: NTC电压 10: VSYS电压 + } config; +}; + +enum sw7203_I2C_address_t +{ + sw7203_I2c_0x3C = 0, + sw7203_I2c_0x38, + sw7203_I2c_0x1C, + sw7203_I2c_0x18 +}; + +typedef struct sw7203_config +{ + sw7203_i2c_config i2cHost; + sw7203_I2C_address_t i2cAddress; + union + { + uint8_t hex; // 8位无符号整数,用于将位域组合成一个字节方便程序读取 + + struct + { + uint8_t : 3; // 未定义的 bit 不能被改写 + + uint8_t shutdownLowPowerModeEnable : 1; + // 关机低功耗模式使能标志位: + // 0: 启用关机低功耗模式,1: 禁用关机低功耗模式 + + uint8_t : 2; // 未定义的 bit 不能被改写 + + uint8_t a1PortDetectEnable : 1; + // A1端口检测使能标志位: + // 0: 禁用A1端口检测,1: 启用A1端口检测 + + uint8_t a2PortDetectEnable : 1; + // A2端口检测使能标志位: + // 0: 禁用A2端口检测,1: 启用A2端口检测 + } config; + } connectDetect; + union + { + uint8_t hex; // 8位无符号整数,用于将位域组合成一个字节方便程序读取 + + struct + { + uint8_t switchFrequency : 2; + // 开关频率选项: + // 0: 300KHz,1: 200KHz,2: 400KHz,3: 800KHz + + uint8_t forcePWMModeEnable : 1; + // 强制 PWM 模式使能: + // 0: 禁用,1: 启用 + + uint8_t : 2; // 未定义的 bit 不能被改写 + + uint8_t NTCProtectEnable : 1; + // NTC 过温保护使能 + // 0: 启用,1: 禁用 + + uint8_t PowerMOSMinConductTime : 1; + // 功率管最小导通时间设置: + // 0: 下管最小导通时间 112nS,上管最小导通时间 133nS + // 1: 下管最小导通时间 58nS,上管最小导通时间 81nS + + uint8_t VBUSVoltageRegulationMethod : 1; + // VBUS 输出调压方式选择: + // 0: I2C 调压,1: FB 调压 + } config; + } funcConfig1; + union + { + uint8_t hex; // 8位无符号整数,用于将位域组合成一个字节方便程序读取 + + struct + { + uint8_t deadzoneM1OpenM2Closed : 2; + // M2 关 M1 开死区设置: + // 0: 60nS,1: 20nS,2: 40nS,3: 80nS + + uint8_t deadzoneM1ClosedM2Open : 2; + // M2 关 M1 开死区设置: + // 0: 60nS,1: 20nS,2: 40nS,3: 60nS + + uint8_t deadzoneM3ClosedM4Open : 2; + // M3 关 M4 开死区设置: + // 0: 60nS,1: 20nS,2: 40nS,3: 80nS + + uint8_t deadzoneM3OpenM4Closed : 2; + // M4 关 M3 开死区设置: + // 0: 60nS,1: 20nS,2: 40nS,3: 60nS + } config; + } funcConfig2; + union + { + uint8_t hex; // 8位无符号整数,用于将位域组合成一个字节方便程序读取 + + struct + { + uint8_t M2MOSRdsOn : 2; + // M2 功率管内阻设置: + // 0: 2.5mR,1: 5mR,2: 7.5mR,3: 10mR + + uint8_t nductanceValue : 2; + // 电感感值设置: + // 0: 1uH,1: 2.2uH,2: 3.3uH,3: 4.7uH + + uint8_t : 4; // 未定义的 bit 不能被改写 + } config; + } funcConfig3; + union + { + uint8_t hex; // 8位无符号整数,用于将位域组合成一个字节方便程序读取 + + struct + { + uint8_t ACOutRegResetControl : 1; + // 适配器移出是否清除充电开关控制位: + // 0: 清除,1: 不清除 + + uint8_t stopChargingOnFullEnable : 1; + // 充满停充使能: + // 0: 使能,1: 禁止 + + uint8_t : 2; // 未定义的 bit 不能被改写 + + uint8_t fullChargeCurrent : 1; + // 充满截止电流设置: + // 0: 100mA,1: 200mA, 3: 300mA,4: 400mA + + uint8_t batteryCellNum : 1; + // 电池节数设置: + // 0: 1节,1: 2节, 3: 3节,4: 4节 + } config; + } funcConfig4; +}; + +#endif \ No newline at end of file diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 62ab7b4..1717c03 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,4 +1,4 @@ -idf_component_register(SRCS "main.cpp" +idf_component_register(SRCS "main.c" INCLUDE_DIRS ".") diff --git a/main/main.cpp b/main/main.c similarity index 92% rename from main/main.cpp rename to main/main.c index cf412ff..2a1e141 100644 --- a/main/main.cpp +++ b/main/main.c @@ -22,53 +22,26 @@ #include "sdkconfig.h" #include "esp_log.h" #include "bq4050.h" -#include "gpio_cxx.hpp" #include "driver/gpio.h" #include -using namespace std; -using namespace idf; - #define SetBitTrue(a, b) a |= (1 << b) #define SetBitFalse(a, b) a &= ~(1 << b) #define GetBit(a, b) a & (1 << b) -#define TUSB_DESC_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_HID_INOUT_DESC_LEN) -#define ITF_NUM_TOTAL 1 -static esp_err_t bq4050_i2c_master_init() +static esp_err_t i2c0_master_init() { i2c_config_t conf = { .mode = I2C_MODE_MASTER, - .sda_io_num = BQ4050_I2C_MASTER_SDA_IO, - .scl_io_num = BQ4050_I2C_MASTER_SCL_IO, + .sda_io_num = I2C0_SDA_IO, + .scl_io_num = I2C0_SCL_IO, .sda_pullup_en = GPIO_PULLUP_ENABLE, .scl_pullup_en = GPIO_PULLUP_ENABLE, .master{ - .clk_speed = BQ4050_I2C_MASTER_FREQ_HZ}, + .clk_speed = I2C_MASTER_TIMEOUT_MS}, }; - - BQ4050::I2C_MASTER = BQ4050_I2C_MASTER_NUM; - BQ4050::I2C_MASTER_TIMEOUT = BQ4050_I2C_MASTER_TIMEOUT_MS; - - i2c_param_config(BQ4050_I2C_MASTER_NUM, &conf); - - return i2c_driver_install(BQ4050_I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0); -} -static esp_err_t sw7203_i2c_master_init() -{ - i2c_config_t conf = { - .mode = I2C_MODE_MASTER, - .sda_io_num = SW7203_I2C_MASTER_SDA_IO, - .scl_io_num = SW7203_I2C_MASTER_SCL_IO, - .sda_pullup_en = GPIO_PULLUP_ENABLE, - .scl_pullup_en = GPIO_PULLUP_ENABLE, - .master{ - .clk_speed = SW7203_I2C_MASTER_FREQ_HZ}, - }; - - i2c_param_config(SW7203_I2C_MASTER_NUM, &conf); - - return i2c_driver_install(SW7203_I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0); + i2c_param_config(I2C0_NUM, &conf); + return i2c_driver_install(I2C0_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0); } esp_err_t sw7203_register_read(uint8_t reg_addr, uint8_t *data)