sw7203库编写

This commit is contained in:
alex04130 2024-03-18 13:44:58 -05:00
parent a032edc502
commit ddd6c78209
2 changed files with 374 additions and 49 deletions

View File

@ -2,6 +2,8 @@
#include <sw7203.h>
#include "driver/i2c_master.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
uint8_t sw7203_I2C_address_find(i2c_master_bus_handle_t I2C_bus_handle)
{
@ -29,41 +31,58 @@ uint8_t sw7203_I2C_address_find(i2c_master_bus_handle_t I2C_bus_handle)
return -1;
}
void sw7203_register_change_bitmask(uint8_t reg_addr, uint8_t data, uint8_t bitmask)
void sw7203_register_change_bitmask(uint8_t *reg_addr, uint8_t *data, uint8_t *bitmask)
{
uint8_t original_reg;
ESP_ERROR_CHECK(i2c_master_transmit_receive(SW7203_I2C_handle, &reg_addr, 1, &original_reg, 1, -1));
uint8_t diff = (original_reg ^ data) & bitmask;
if (__sw7203_driver_config__.I2CBusBusyEnable == 1)
{
while (*SW7203_Mutex != 0)
{
vTaskDelay(50 / portTICK_PERIOD_MS);
}
*SW7203_Mutex = 1;
ESP_ERROR_CHECK(i2c_master_transmit_receive(SW7203_I2C_handle, reg_addr, 1, &original_reg, 1, -1));
*SW7203_Mutex = 0;
}
else
ESP_ERROR_CHECK(i2c_master_transmit_receive(SW7203_I2C_handle, reg_addr, 1, &original_reg, 1, -1));
uint8_t diff = (original_reg ^ (*data)) & (*bitmask);
if (diff)
{
uint16_t write_data = reg_addr << 8 | (diff ^ original_reg);
uint16_t write_data = (*reg_addr) << 8 | (diff ^ original_reg);
if (__sw7203_driver_config__.I2CBusBusyEnable == 1)
{
while (SW7203_Mutex != 0)
{
vTaskDelay(50 / portTICK_PERIOD_MS);
}
SW7203_Mutex = 1;
ESP_ERROR_CHECK(i2c_master_transmit(SW7203_I2C_handle, &write_data, 2, -1));
*SW7203_Mutex = 0;
}
else
ESP_ERROR_CHECK(i2c_master_transmit(SW7203_I2C_handle, &write_data, 2, -1));
}
}
esp_err_t sw7203_reg_read(uint8_t reg_addr, uint8_t *data)
{
esp_err_t errcode;
if (__sw7203_driver_config__.I2CBusBusyEnable == 1)
{
while (SW7203_Mutex != 0)
{
vTaskDelay(50 / portTICK_PERIOD_MS);
}
SW7203_Mutex = 1;
errcode = i2c_master_transmit(SW7203_I2C_handle, data, 2, -1);
*SW7203_Mutex = 0;
}
else
errcode = i2c_master_transmit(SW7203_I2C_handle, data, 2, -1);
return errcode;
}
/* {0x18, 0b00010011},
{0x1A, 0b00000011},
{0x20, 0b11100111},
{0x21, 0b11111111},
{0x22, 0b11110000},
{0x30, 0b11001111},
{0x31, 0b00001111},
{0x32, 0b11011111},
{0x33, 0b11111111},
{0x34, 0b11111111},
{0x35, 0b00000111},
{0x36, 0b01111111},
{0x37, 0b00001111},
{0x38, 0b11111111},
{0x39, 0b01111111},
{0x3A, 0b01111111},
{0x40, 0b01000011},
{0x41, 0b00011111},
{0x42, 0b01111111},
{0x42, 0b11111111},
{0x44, 0b00000011},*/
esp_err_t sw7203init(sw7203_config_t *sw7203_config, i2c_master_bus_handle_t I2C_bus_handle, uint8_t I2C_address)
esp_err_t sw7203_driver_install(sw7203_config_t *sw7203_config, i2c_master_bus_handle_t I2C_bus_handle, uint8_t I2C_address)
{
if (I2C_address == -1)
{
@ -75,11 +94,203 @@ esp_err_t sw7203init(sw7203_config_t *sw7203_config, i2c_master_bus_handle_t I2C
.device_address = I2C_address,
.scl_speed_hz = 100000};
ESP_ERROR_CHECK(errcode = i2c_master_bus_add_device(I2C_bus_handle, &sw7203_i2c_dev_config, &SW7203_I2C_handle));
sw7203_register_change_bitmask(SW7203RegAddress_bitmask[0][0], sw7203_config->connectDetect.hex, SW7203RegAddress_bitmask[0][1]);
sw7203_register_change_bitmask(SW7203RegAddress_bitmask[1][0], sw7203_config->i2cAddress, SW7203RegAddress_bitmask[1][1]);
sw7203_register_change_bitmask(SW7203RegAddress_bitmask[2][0], sw7203_config->funcConfig1.hex, SW7203RegAddress_bitmask[2][1]);
sw7203_register_change_bitmask(SW7203RegAddress_bitmask[3][0], sw7203_config->funcConfig2.hex, SW7203RegAddress_bitmask[3][1]);
sw7203_register_change_bitmask(SW7203RegAddress_bitmask[4][0], sw7203_config->funcConfig3.hex, SW7203RegAddress_bitmask[4][1]);
sw7203_register_change_bitmask(SW7203RegAddress_bitmask[5][0], sw7203_config->funcConfig4.hex, SW7203RegAddress_bitmask[5][1]);
if (sw7203_config->SW7203_control_mode.I2CBusBusyEnable == 0)
{
SW7203_Mutex = sw7203_config->Mutex;
__sw7203_driver_config__.I2CBusBusyEnable = 1;
}
else
__sw7203_driver_config__.I2CBusBusyEnable = 0;
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[0][0], &sw7203_config->connectDetect.hex, &SW7203RegAddress_bitmask[0][1]); // {0x18, 0b00010011}, 0
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[1][0], &sw7203_config->i2cAddress, &SW7203RegAddress_bitmask[1][1]); // {0x1A, 0b00000011}, 1
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[2][0], &sw7203_config->funcConfig1.hex, &SW7203RegAddress_bitmask[2][1]); // {0x20, 0b11100111}, 2
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[3][0], &sw7203_config->funcConfig2.hex, &SW7203RegAddress_bitmask[3][1]); // {0x21, 0b11111111}, 3
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[4][0], &sw7203_config->funcConfig3.hex, &SW7203RegAddress_bitmask[4][1]); // {0x22, 0b11110000}, 4
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[5][0], &sw7203_config->funcConfig4.hex, &SW7203RegAddress_bitmask[5][1]); // {0x30, 0b11001111}, 5
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[6][0], &sw7203_config->funcConfig5.hex, &SW7203RegAddress_bitmask[6][1]); // {0x31, 0b00001111}, 6
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[7][0], &sw7203_config->funcConfig6.hex, &SW7203RegAddress_bitmask[7][1]); // {0x32, 0b11011111}, 7
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[8][0], &sw7203_config->funcConfig7.hex, &SW7203RegAddress_bitmask[8][1]); // {0x33, 0b11111111}, 8
uint8_t data_up, data_lb;
int tmp;
if (sw7203_config->chargeVoltage_mV < 3000)
{
data_up = 0;
data_lb = 0;
}
else if (sw7203_config->chargeVoltage_mV > 19200)
{
data_up = 0b11001010;
data_lb = 0b00000100;
}
else
{
tmp = ((sw7203_config->chargeVoltage_mV - 3000) / 10);
data_up = (uint8_t)(tmp >> 3) & 0xff;
data_lb = (tmp) & 0b0000000000000111;
}
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[9][0], &data_up, &SW7203RegAddress_bitmask[9][1]); // {0x34, 0b11111111}, 9
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[10][0], &data_lb, &SW7203RegAddress_bitmask[10][1]); // {0x35, 0b00000111}, 10
if (sw7203_config->trickleVoltage_mV < 2500)
{
data_up = 0;
}
else if (sw7203_config->trickleVoltage_mV > 13200)
{
data_up = 107;
}
else
{
data_up = ((sw7203_config->trickleVoltage_mV - 2500) / 100);
}
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[11][0], &data_up, &SW7203RegAddress_bitmask[11][1]); // {0x36, 0b01111111}, 11
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[12][0], &sw7203_config->trickleConfig.hex, &SW7203RegAddress_bitmask[12][1]); // {0x37, 0b00001111}, 12
if (sw7203_config->chargeVBUSVoltageLimit_mV < 4000)
{
data_up = 0;
}
else if (sw7203_config->chargeVBUSVoltageLimit_mV > 20000)
{
data_up = 160;
}
else
{
data_up = ((sw7203_config->chargeVBUSVoltageLimit_mV - 4000) / 100);
}
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[13][0], &data_up, &SW7203RegAddress_bitmask[13][1]); // {0x38, 0b11111111}, 13
if (sw7203_config->chargeVBUSCurrentLimit_mA < 500)
{
data_up = 0;
}
else if (sw7203_config->chargeVBUSCurrentLimit_mA > 6850)
{
data_up = 127;
}
else
{
data_up = ((sw7203_config->chargeVBUSCurrentLimit_mA - 500) / 50);
}
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[14][0], &data_up, &SW7203RegAddress_bitmask[14][1]); // {0x39, 0b01111111}, 14
if (sw7203_config->chargeVBATCurrentLimit_mA < 100)
{
data_up = 0;
}
else if (sw7203_config->chargeVBATCurrentLimit_mA > 12000)
{
data_up = 119;
}
else
{
data_up = ((sw7203_config->chargeVBATCurrentLimit_mA - 100) / 100);
}
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[15][0], &data_up, &SW7203RegAddress_bitmask[15][1]); // {0x3A, 0b01111111}, 15
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[16][0], &sw7203_config->NVDCConfig1.hex, &SW7203RegAddress_bitmask[16][1]); // {0x40, 0b01000011}, 16
if (sw7203_config->chargeLDOCurrentLimit_mA < 100)
{
data_up = 0;
}
else if (sw7203_config->chargeLDOCurrentLimit_mA > 2000)
{
data_up = 19;
}
else
{
data_up = ((sw7203_config->chargeLDOCurrentLimit_mA - 100) / 100);
}
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[17][0], &data_up, &SW7203RegAddress_bitmask[17][1]); // {0x41, 0b00011111}, 17
if (sw7203_config->VSYSMinVoltage_mV < 3000)
{
data_up = 0;
}
else if (sw7203_config->VSYSMinVoltage_mV > 16600)
{
data_up = 68;
}
else
{
data_up = ((sw7203_config->VSYSMinVoltage_mV - 3000) / 200);
}
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[18][0], &data_up, &SW7203RegAddress_bitmask[18][1]); // {0x42, 0b01111111}, 18
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[19][0], &sw7203_config->NTCConfig.hex, &SW7203RegAddress_bitmask[19][1]); // {0x43, 0b11111111}, 19
return ESP_OK;
}
void sw7203SwitchOutput(sw7203_Vout_config_t *config)
{
uint8_t data_up, data_lb;
int tmp;
if (config->VBUSVoltage_mV < 3000)
{
data_up = 0;
data_lb = 0;
}
else if (config->VBUSVoltage_mV > 22000)
{
data_up = 0b11101101;
data_lb = 0b00000100;
}
else
{
tmp = ((config->VBUSVoltage_mV - 3000) / 10);
data_up = (uint8_t)(tmp >> 3) & 0xff;
data_lb = (tmp) & 0b0000000000000111;
}
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[20][0], &data_up, &SW7203RegAddress_bitmask[20][1]); // {0x23, 0b11111111} 20
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[21][0], &data_lb, &SW7203RegAddress_bitmask[21][1]); // {0x24, 0b00000111} 21
if (config->VBUSCurrentLimit_mA < 500)
{
data_up = 0;
}
else if (config->VBUSCurrentLimit_mA > 6850)
{
data_up = 127;
}
else
{
data_up = (uint8_t)((config->VBUSCurrentLimit_mA - 500) / 50);
}
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[22][0], &data_up, &SW7203RegAddress_bitmask[22][1]); // {0x25, 0b01111111} 22
if (config->VBATCurrentLimit_mA < 100)
{
data_up = 0;
}
else if (config->VBATCurrentLimit_mA > 12000)
{
data_up = 119;
}
else
{
data_up = (uint8_t)((config->VBUSCurrentLimit_mA - 100) / 100);
}
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[23][0], &data_up, &SW7203RegAddress_bitmask[23][1]); // {0x26, 0b01111111} 23
if (config->VBATVoltageLimit_mV < 2700)
{
data_up = 0;
}
else if (config->VBATVoltageLimit_mV > 13200)
{
data_up = 105;
}
else
{
data_up = (uint8_t)((config->VBATVoltageLimit_mV - 2700) / 100);
}
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[24][0], &data_up, &SW7203RegAddress_bitmask[24][1]); // {0x27, 0b01111111} 24
if (config->VBATVoltageLimitHysteresis_mV < 400)
{
data_up = 0;
}
else if (config->VBATVoltageLimitHysteresis_mV > 2000)
{
data_up = 16;
}
else
{
data_up = (uint8_t)((config->VBATVoltageLimitHysteresis_mV - 400) / 100);
}
sw7203_register_change_bitmask(&SW7203RegAddress_bitmask[25][0], &data_up, &SW7203RegAddress_bitmask[25][1]); // {0x28, 0b00011111} 25
}
esp_err_t sw7203_driver_uninstall(i2c_master_bus_handle_t I2C_bus_handle)
{
return i2c_master_bus_rm_device(SW7203_I2C_handle);
}

View File

@ -4,8 +4,36 @@
#include "driver/i2c_master.h"
#include "esp_log.h"
typedef sw7203_Vout_config_t *(*SW7203CallBack_t)(sw7203_I2C_port);
static i2c_master_dev_handle_t SW7203_I2C_handle;
int8_t *SW7203_Mutex = NULL;
static SW7203CallBack_t *__SW7203CallBack_;
struct
{
uint8_t mode : 1;
// 控制模式选择
// 0: 被动模式 采取回调函数的方式进行运作,当检测到输入插入时调用回调函数,回调函数输入具体插入口,回调函数需返回输出配置的指针
// 1: 主动模式 放弃中断中插入检测,直接用函数和配置文件进行设置
uint8_t INTGPIOEnable : 1;
// 中断GPIO模式使能
// 0: 中断GPIO启动
// 1: 轮询I2C代替中断GPIO
uint8_t intMode : 1;
// 中断模式选择在使用轮询I2C后无效
// 0: 轮询模式 驱动程序自动加载中断轮询函数
// 1: ESP32中断回调模式 需用户单独配置中断端口并中断后调用 函数
uint8_t voltageConflictSwitch : 1;
// 电压冲突选择,在设置为主动模式后无效
// 0: 当A1 A2 任何一个已经插入且插入第二个时回调得到第二个不同的电压值,立刻切换到这个电压
// 1: 当A1 A2 任何一个已经插入且插入第二个时回调得到第二个不同的电压值,等待第一个插入的拔出后再输出第二个电压
uint8_t I2CBusBusyEnable : 1;
} __sw7203_driver_config__;
typedef union
{
@ -26,27 +54,71 @@ typedef enum
sw7203_I2c_0x18 = 3
} sw7203_I2C_address_t;
typedef enum
{
sw7203_port_A1,
sw7203_port_A2
} sw7203_I2C_port;
typedef struct
{
uint16_t VBUSVoltage_cV; // 放电 VBUS 输出电压设置 [300,2200] 单位厘伏cV
uint8_t VBUSCurrentLimit_5cA; // 充电 VBUS 输入限流设置 [10,137] 单位:五十毫安/五厘安50mA/5cA
uint8_t VBATCurrentLimit_dA; // 充电 VBAT 输入限流设置 [1,120] 单位分安dA
uint8_t VBATVoltageLimit_dV; // 放电 VBAT 欠压设置 [27,1320] 单位分伏dV
uint8_t VBATVoltageLimitHysteresis_dV; // 放电 VBAT 欠压迟滞设置 [4,20] 单位分伏dV
uint16_t VBUSVoltage_mV; // 放电 VBUS 输出电压设置 [3000,22000] 单位毫伏mV
uint16_t VBATVoltageLimit_mV; // 放电 VBAT 欠压设置 [2700,13200] 单位毫伏mV
uint16_t VBATVoltageLimitHysteresis_mV; // 放电 VBAT 欠压迟滞设置 [400,2000] 单位毫伏mV
uint16_t VBUSCurrentLimit_mA; // 放电 VBUS 输出限流设置 [500,6850] 单位毫安mA
uint16_t VBATCurrentLimit_mA; // 放电 VBAT 输出限流设置 [100,12000] 单位毫安mA
} sw7203_Vout_config_t;
typedef struct
{
uint8_t INT_gpio_num;
sw7203_I2C_address_t i2cAddress;
void *Mutex;
SW7203CallBack_t CallBack;
uint16_t chargeVoltage_cV; // 充电目标电压设置 [300,1920] 单位厘伏cV
uint8_t trickleVoltage_dV; // 涓流电压设置 [25,132] 单位分伏dV
uint8_t chargeVBUSVoltageLimit_dV; // 防拉挂电压设置 [40,200] 单位分伏dV
uint8_t chargeVBUSCurrentLimit_5cA; // 充电 VBUS 输入限流设置 [10,137] 单位:五十毫安/五厘安50mA/5cA
uint8_t chargeVBATCurrentLimit_dA; // 充电 VBAT 输入限流设置 [1,120] 单位分安dA
uint8_t chargeLDOCurrentLimit_dA; // LDO 充电电流设置 [1,20] 单位分安dA
uint8_t VSYSMinVoltage_2dV; // VSYMIN 电压设置 [15,83] 单位2分伏2dV/0.2V
struct
{
uint8_t mode : 1;
// 控制模式选择
// 0: 被动模式 采取回调函数的方式进行运作,当检测到输入插入时调用回调函数,回调函数输入具体插入口,回调函数需返回输出配置的指针
// 1: 主动模式 放弃中断中插入检测,直接用函数和配置文件进行设置
uint8_t INTGPIOEnable : 1;
// 中断GPIO模式使能
// 0: 中断GPIO启动
// 1: 轮询I2C代替中断GPIO
uint8_t intMode : 1;
// 中断模式选择在使用轮询I2C后无效
// 0: 轮询模式 驱动程序自动加载中断轮询函数
// 1: ESP32中断回调模式 需用户单独配置中断端口并中断后调用 函数
uint8_t gpioInitMode : 1;
// gpio初始化模式选择在使用轮询I2C后无效
// 0: 驱动程序初始化时自动初始化中断gpio
// 1: 用户自己初始化中断gpio后初始化sw7203
uint8_t voltageConflictSwitch : 1;
// 电压冲突选择,在设置为主动模式后无效
// 0: 当A1 A2 任何一个已经插入且插入第二个时回调得到第二个不同的电压值,立刻切换到这个电压
// 1: 当A1 A2 任何一个已经插入且插入第二个时回调得到第二个不同的电压值,等待第一个插入的拔出后再输出第二个电压
uint8_t I2CBusBusyEnable : 1;
// I2C总线忙使能
// 0: Mutex指针为I2C进程锁为1时表示总线正忙
// 1: 关闭总线防冲突功能
} SW7203_control_mode;
uint16_t chargeVoltage_mV; // 充电目标电压设置 [3000,19200] 单位毫伏mV
uint16_t trickleVoltage_mV; // 涓流电压设置 [2500,13200] 单位毫伏mV
uint16_t chargeVBUSVoltageLimit_mV; // 防拉挂电压设置 [4000,20000] 单位毫伏mV
uint16_t VSYSMinVoltage_mV; // VSYMIN 电压设置 [3000,16600] 单位毫伏mV
uint16_t chargeVBUSCurrentLimit_mA; // 充电 VBUS 输入限流设置 [500,6850] 单位毫安mA
uint16_t chargeVBATCurrentLimit_mA; // 充电 VBAT 输入限流设置 [100,12000] 单位毫安mA
uint16_t chargeLDOCurrentLimit_mA; // LDO 充电电流设置 [100,2000] 单位毫安mA
union
{
@ -323,8 +395,45 @@ typedef struct
} trickleConfig; // 涓流电流设置
} sw7203_config_t;
/**
* @brief SW7203 I2C
*
* @param handle i2c device handle
* @return
* - 8: -1
*/
uint8_t sw7203_I2C_address_find(i2c_master_bus_handle_t I2C_bus_handle);
esp_err_t sw7203init(sw7203_config_t *sw7023_config, i2c_master_bus_handle_t I2C_bus_handle, uint8_t I2C_address);
/**
* @brief SW7203
*
* @param[in] sw7203_config sw7203的配置文件
* @param handle I2C_bus_handle I2C 线handle
* @param[in] I2C_address sw7203的I2C地址
*
* @return
* - ESP_OK: .
* - ESP_ERR_NOT_ALLOWED: SW7203 I2C地址不存在.
*/
esp_err_t sw7203_driver_install(sw7203_config_t *sw7023_config, i2c_master_bus_handle_t I2C_bus_handle, uint8_t I2C_address);
/**
* @brief SW7203
*
* @param[in] config sw7203的输出配置文件
*
*/
void sw7203SwitchOutput(sw7203_Vout_config_t *config);
/**
* @brief SW7203
*
* @param handle I2C_bus_handle I2C 线handle
*
* @return
* - ESP_OK: .
*/
esp_err_t sw7203_driver_uninstall(i2c_master_bus_handle_t I2C_bus_handle);
const uint8_t SW7203RegAddress_bitmask[][2] = {
{0x18, 0b00010011},
@ -346,8 +455,13 @@ const uint8_t SW7203RegAddress_bitmask[][2] = {
{0x40, 0b01000011},
{0x41, 0b00011111},
{0x42, 0b01111111},
{0x42, 0b11111111},
{0x44, 0b00000011},
{0x43, 0b11111111},
{0x23, 0b11111111},
{0x24, 0b00000111},
{0x25, 0b01111111},
{0x26, 0b01111111},
{0x27, 0b01111111},
{0x28, 0b00011111},
};
#endif