2024-03-06 09:32:15 +08:00
|
|
|
|
#ifndef __SW7203_H__
|
|
|
|
|
#define __SW7203_H__
|
|
|
|
|
|
2024-03-08 01:45:59 +08:00
|
|
|
|
#include "driver/i2c_master.h"
|
2024-03-06 09:32:15 +08:00
|
|
|
|
#include "esp_log.h"
|
|
|
|
|
|
2024-03-08 01:45:59 +08:00
|
|
|
|
static i2c_master_dev_handle_t SW7203_I2C_handle;
|
|
|
|
|
int8_t *SW7203_Mutex = NULL;
|
2024-03-06 09:32:15 +08:00
|
|
|
|
|
2024-03-06 11:13:15 +08:00
|
|
|
|
typedef union
|
2024-03-06 09:32:15 +08:00
|
|
|
|
{
|
|
|
|
|
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;
|
2024-03-06 11:13:15 +08:00
|
|
|
|
} adc_config;
|
2024-03-06 09:32:15 +08:00
|
|
|
|
|
2024-03-06 11:13:15 +08:00
|
|
|
|
typedef enum
|
2024-03-06 09:32:15 +08:00
|
|
|
|
{
|
2024-03-08 01:45:59 +08:00
|
|
|
|
sw7203_I2c_0x3C = 0,
|
|
|
|
|
sw7203_I2c_0x38 = 1,
|
|
|
|
|
sw7203_I2c_0x1C = 2,
|
|
|
|
|
sw7203_I2c_0x18 = 3
|
2024-03-06 11:13:15 +08:00
|
|
|
|
} sw7203_I2C_address_t;
|
2024-03-06 09:32:15 +08:00
|
|
|
|
|
2024-03-06 11:13:15 +08:00
|
|
|
|
typedef struct
|
|
|
|
|
{
|
2024-03-07 05:21:06 +08:00
|
|
|
|
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)
|
2024-03-06 11:13:15 +08:00
|
|
|
|
} sw7203_Vout_config_t;
|
|
|
|
|
|
|
|
|
|
typedef struct
|
2024-03-06 09:32:15 +08:00
|
|
|
|
{
|
|
|
|
|
sw7203_I2C_address_t i2cAddress;
|
2024-03-08 01:45:59 +08:00
|
|
|
|
void *Mutex;
|
2024-03-06 11:13:15 +08:00
|
|
|
|
|
|
|
|
|
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)
|
2024-03-08 01:45:59 +08:00
|
|
|
|
uint8_t chargeLDOCurrentLimit_dA; // LDO 充电电流设置 [1,20] 单位:分安(dA)
|
2024-03-06 11:13:15 +08:00
|
|
|
|
uint8_t VSYSMinVoltage_2dV; // VSYMIN 电压设置 [15,83] 单位:2分伏(2dV/0.2V)
|
|
|
|
|
|
2024-03-06 09:32:15 +08:00
|
|
|
|
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;
|
2024-03-06 11:13:15 +08:00
|
|
|
|
} connectDetect; // 负载接入检测及低功耗模式控制
|
|
|
|
|
|
2024-03-06 09:32:15 +08:00
|
|
|
|
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;
|
2024-03-06 11:13:15 +08:00
|
|
|
|
|
2024-03-06 09:32:15 +08:00
|
|
|
|
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;
|
2024-03-06 11:13:15 +08:00
|
|
|
|
|
2024-03-06 09:32:15 +08:00
|
|
|
|
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;
|
2024-03-06 11:13:15 +08:00
|
|
|
|
|
2024-03-06 09:32:15 +08:00
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
uint8_t hex; // 8位无符号整数,用于将位域组合成一个字节方便程序读取
|
|
|
|
|
|
|
|
|
|
struct
|
|
|
|
|
{
|
|
|
|
|
uint8_t ACOutRegResetControl : 1;
|
|
|
|
|
// 适配器移出是否清除充电开关控制位:
|
|
|
|
|
// 0: 清除,1: 不清除
|
|
|
|
|
|
|
|
|
|
uint8_t stopChargingOnFullEnable : 1;
|
|
|
|
|
// 充满停充使能:
|
|
|
|
|
// 0: 使能,1: 禁止
|
|
|
|
|
|
|
|
|
|
uint8_t : 2; // 未定义的 bit 不能被改写
|
|
|
|
|
|
2024-03-06 11:13:15 +08:00
|
|
|
|
uint8_t fullChargeCurrent : 2;
|
2024-03-06 09:32:15 +08:00
|
|
|
|
// 充满截止电流设置:
|
|
|
|
|
// 0: 100mA,1: 200mA, 3: 300mA,4: 400mA
|
|
|
|
|
|
2024-03-06 11:13:15 +08:00
|
|
|
|
uint8_t batteryCellNum : 2;
|
2024-03-06 09:32:15 +08:00
|
|
|
|
// 电池节数设置:
|
|
|
|
|
// 0: 1节,1: 2节, 3: 3节,4: 4节
|
|
|
|
|
} config;
|
|
|
|
|
} funcConfig4;
|
2024-03-06 11:13:15 +08:00
|
|
|
|
|
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
uint8_t hex; // 8位无符号整数,用于将位域组合成一个字节方便程序读取
|
|
|
|
|
|
|
|
|
|
struct
|
|
|
|
|
{
|
|
|
|
|
uint8_t : 4; // 未定义的 bit 不能被改写
|
|
|
|
|
|
|
|
|
|
uint8_t chipOverTemp : 2;
|
|
|
|
|
// 芯片过温温度设置:
|
|
|
|
|
// 0: 120℃,1: 130℃, 3: 140℃,4: 150℃
|
|
|
|
|
|
|
|
|
|
uint8_t peakCurrentLimit : 2;
|
|
|
|
|
// 峰值限流设置,充放电共用此设置:
|
|
|
|
|
// 0: 12A,1: 14A, 3: 16A,4: 18A
|
|
|
|
|
} config;
|
|
|
|
|
} funcConfig5;
|
|
|
|
|
|
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
uint8_t hex; // 8位无符号整数,用于将位域组合成一个字节方便程序读取
|
|
|
|
|
|
|
|
|
|
struct
|
|
|
|
|
{
|
|
|
|
|
uint8_t func62368Enable : 1;
|
|
|
|
|
// 62368 功能使能:
|
|
|
|
|
// 0: 使能,1: 禁止
|
|
|
|
|
|
|
|
|
|
uint8_t func62368IBUSLimitEnable : 1;
|
|
|
|
|
// 处于 62368 的高低温状态,降低 IBUS 限流功能使能:
|
|
|
|
|
// 0: 使能,1: 禁止
|
|
|
|
|
|
|
|
|
|
uint8_t : 1; // 未定义的 bit 不能被改写
|
|
|
|
|
|
|
|
|
|
uint8_t chargeOverTimeEnable : 1;
|
|
|
|
|
// 充电超时使能:
|
|
|
|
|
// 0: 使能,1: 禁止
|
|
|
|
|
|
|
|
|
|
uint8_t trickleChargeTimeout : 2;
|
|
|
|
|
// 涓流充电超时时间设置:
|
|
|
|
|
// 0: 30分钟,1: 1小时, 3: 2小时,4: 4小时
|
|
|
|
|
|
|
|
|
|
uint8_t constantCurrentChargeTimeout : 2;
|
|
|
|
|
// 恒流充电超时时间设置:
|
|
|
|
|
// 0: 12小时,1: 24小时, 3: 48小时,4: 72小时
|
|
|
|
|
} config;
|
|
|
|
|
} funcConfig6;
|
|
|
|
|
|
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
uint8_t hex; // 8位无符号整数,用于将位域组合成一个字节方便程序读取
|
|
|
|
|
|
|
|
|
|
struct
|
|
|
|
|
{
|
|
|
|
|
uint8_t func62368LowTempCurrentLimit : 2;
|
|
|
|
|
// 处于 62368 的低温状态,降低限流设置:
|
|
|
|
|
// 当 funcConfig6.func62368IBUSLimitEnable=0 时,同时降低 IBUS 和 IBAT 的限流值
|
|
|
|
|
// funcConfig6.func62368IBUSLimitEnable=1 时,只降低 IBAT 的限流值
|
|
|
|
|
// 0: 降低到设置值的 1/2,1: 降低到设置值的 1/4, 3: 不降低,4: 保留(不可选)
|
|
|
|
|
|
|
|
|
|
uint8_t func62368HighTempCurrentLimit : 2;
|
|
|
|
|
// 处于 62368 的高温状态,降低限流设置:
|
|
|
|
|
// 当 funcConfig6.func62368IBUSLimitEnable=0 时,同时降低 IBUS 和 IBAT 的限流值
|
|
|
|
|
// funcConfig6.func62368IBUSLimitEnable=1 时,只降低 IBAT 的限流值
|
|
|
|
|
// 0: 降低到设置值的 1/2,1: 降低到设置值的 1/4, 3: 不降低,4: 保留(不可选)
|
|
|
|
|
|
|
|
|
|
uint8_t func62368LowTempVoltageLimit : 1;
|
|
|
|
|
// 处于 62368 的低温状态,降低充电目标电压设置:
|
|
|
|
|
// 0: 充电目标电压降低电池节数设置值*0.1V,1: 不降低充电目标电压
|
|
|
|
|
|
|
|
|
|
uint8_t func62368HighTempVoltageLimit : 1;
|
|
|
|
|
// 处于 62368 的高温状态,降低充电目标电压设置:
|
|
|
|
|
// 0: 充电目标电压降低电池节数设置值*0.1V,1: 不降低充电目标电压
|
|
|
|
|
|
|
|
|
|
uint8_t func62368LowTempHysteresis : 1;
|
|
|
|
|
// 62368 低温迟滞:
|
|
|
|
|
// 0: 5℃,1: 10℃
|
|
|
|
|
|
|
|
|
|
uint8_t func62368HighTempHysteresis : 1;
|
|
|
|
|
// 62368 高温迟滞:
|
|
|
|
|
// 0: 5℃,1: 10℃
|
|
|
|
|
} config;
|
|
|
|
|
} funcConfig7;
|
|
|
|
|
|
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
uint8_t hex; // 8位无符号整数,用于将位域组合成一个字节方便程序读取
|
|
|
|
|
|
|
|
|
|
struct
|
|
|
|
|
{
|
|
|
|
|
uint8_t dischargeNTCHighLimit : 2;
|
|
|
|
|
// 放电 NTC 高温门限:
|
|
|
|
|
// 0: 50℃,1: 55℃,2: 60℃,3: 65℃
|
|
|
|
|
|
|
|
|
|
uint8_t dischargeNTCLowLimit : 2;
|
|
|
|
|
// 放电 NTC 低温门限:
|
|
|
|
|
// 0: -10℃,1: -5℃,2: 0℃,3: -20℃
|
|
|
|
|
|
|
|
|
|
uint8_t chargeNTCHighLimit : 2;
|
|
|
|
|
// 充电 NTC 高温门限:
|
|
|
|
|
// 0: 50℃,1: 55℃,2: 60℃,3: 65℃
|
|
|
|
|
|
|
|
|
|
uint8_t chargeNTCLowLimit : 2;
|
|
|
|
|
// 充电 NTC 低温门限:
|
|
|
|
|
// 0: -10℃,1: -5℃,2: 0℃,3: -20℃
|
|
|
|
|
} config;
|
|
|
|
|
} NTCConfig;
|
|
|
|
|
|
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
uint8_t hex; // 8位无符号整数,用于将位域组合成一个字节方便程序读取
|
|
|
|
|
|
|
|
|
|
struct
|
|
|
|
|
{
|
|
|
|
|
uint8_t : 1; // 未定义的 bit 不能被改写
|
|
|
|
|
|
|
|
|
|
uint8_t NVDCLearnModeEnable : 1;
|
|
|
|
|
// 学习模式使能:
|
|
|
|
|
// 适配器接入时,关闭充放电,NVDC 处于全打开状态
|
|
|
|
|
// 0: 禁止,1: 使能
|
|
|
|
|
|
|
|
|
|
uint8_t : 4; // 未定义的 bit 不能被改写
|
|
|
|
|
|
|
|
|
|
uint8_t VSYSOverVoltage : 2;
|
|
|
|
|
// VSYS 过载门限:
|
|
|
|
|
// 电池节数为 2~4 节时
|
|
|
|
|
// 0: 6V,1: 5.75V,2: 6.25V,3: 6.5V
|
|
|
|
|
// 电池节数为 1 节时
|
|
|
|
|
// 0: 3.1V,1: 2.85V,2: 3.35V,3: 3.6V
|
|
|
|
|
} config;
|
|
|
|
|
} NVDCConfig1;
|
|
|
|
|
|
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
uint8_t hex; // 8位无符号整数,用于将位域组合成一个字节方便程序读取
|
|
|
|
|
|
|
|
|
|
struct
|
|
|
|
|
{
|
|
|
|
|
uint8_t : 4; // 未定义的 bit 不能被改写
|
|
|
|
|
|
|
|
|
|
uint8_t trickleCurrent : 2;
|
|
|
|
|
// 涓流电流设置:
|
|
|
|
|
// 0: 100mA,1: 200mA,2: 300mA,3: 400mA
|
|
|
|
|
|
|
|
|
|
uint8_t trickleCurrentHysteresis : 2;
|
|
|
|
|
// 涓流电压迟滞设置:
|
|
|
|
|
// 默认值根据 BSSET 电阻设置的电池节数,设置为电池节数*0.1V
|
|
|
|
|
// 0: 0.1V,1: 0.2V,2: 0.3V,3: 0.4V
|
|
|
|
|
} config;
|
|
|
|
|
} trickleConfig; // 涓流电流设置
|
|
|
|
|
} sw7203_config_t;
|
2024-03-06 09:32:15 +08:00
|
|
|
|
|
2024-03-08 01:45:59 +08:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
const uint8_t SW7203RegAddress_bitmask[][2] = {
|
|
|
|
|
{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},
|
|
|
|
|
};
|
2024-03-07 05:21:06 +08:00
|
|
|
|
|
2024-03-06 09:32:15 +08:00
|
|
|
|
#endif
|