SW7203库编写

This commit is contained in:
Alex 2024-03-05 19:32:15 -06:00
parent ce4d1953fe
commit 648c683858
10 changed files with 184 additions and 55 deletions

View File

@ -82,7 +82,8 @@
"xstring": "cpp", "xstring": "cpp",
"xutility": "cpp", "xutility": "cpp",
"charconv": "cpp", "charconv": "cpp",
"format": "cpp" "format": "cpp",
"bq4050.h": "c"
}, },
"cmake.configureOnOpen": false "cmake.configureOnOpen": false
} }

View File

@ -6,4 +6,4 @@ set(EXTRA_COMPONENT_DIRS "./include")
include($ENV{IDF_PATH}/tools/cmake/project.cmake) include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(SK231024827118) project(CLSH1001)

View File

@ -1,3 +1,3 @@
idf_component_register(SRCS "bq4050.cpp" idf_component_register(SRCS "bq4050.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
REQUIRES driver) REQUIRES driver)

View File

@ -3,25 +3,28 @@
#include "driver/i2c.h" #include "driver/i2c.h"
#include "esp_log.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) void ESP_I2C_ERROR_CHECK(esp_err_t errcode)
{ {
if (errcode != ESP_OK) if (errcode != ESP_OK)
{ {
ESP_LOGI(BQ4050::BQ4050_TAG, "TinyUSB driver error\n:)"); __BQ4050_ERR_CB__(errcode);
while (1)
{
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
} }
} }
esp_err_t bq4050_register_read(uint8_t reg_addr, uint8_t *data, size_t len) 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, &reg_addr, 1, data, len, BQ4050::I2C_MASTER_TIMEOUT / portTICK_PERIOD_MS); return i2c_master_write_read_device(__BQ4050__I2C_port__, __BQ4050__I2C_address__, &reg_addr, 1, data, len, 14000 / portTICK_PERIOD_MS);
} }
esp_err_t bq4050_register_write(uint8_t *data, size_t len) 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地址 // inblock前面必须加0x44地址

View File

@ -1,21 +1,14 @@
#ifndef __BQ4050_H__ #ifndef __BQ4050_H__
#define __BQ4050_H__ #define __BQ4050_H__
#include <bq4050.h>
#include "driver/i2c.h" #include "driver/i2c.h"
#include "esp_log.h" #include "esp_log.h"
// BQ4050配置 static i2c_port_t __BQ4050__I2C_port__;
#define BQ4050_ADDR 0x0B static uint8_t __BQ4050__I2C_address__;
static void *(esp_err_t)__BQ4050_ERR_CB__;
namespace BQ4050 void bq_Init(i2c_port_t I2C_NUM, uint8_t I2C_address, void *(esp_err_t)bq4050_err_cb);
{
static i2c_port_t I2C_MASTER;
static int I2C_MASTER_TIMEOUT;
static const char *BQ4050_TAG = "BQ4050";
}
void bq_Init();
uint8_t bq_BattState(); // Return CHG/DSG(0xF?/0x0?), OK/Bad(0x?0/0x?F) 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_GetAdvState(); // Return XDSG/XCHG/PF/SS/FC/FD/TAPR/VCT/CB/SMTH/SU/IN/VDQ/FCCX/EDV2/EDV1
uint16_t bq_GetVoltage(); // Unit: mV uint16_t bq_GetVoltage(); // Unit: mV

View File

@ -0,0 +1,3 @@
idf_component_register(SRCS "sw7203.c"
INCLUDE_DIRS "."
REQUIRES driver)

0
include/sw7203/sw7203.c Normal file
View File

156
include/sw7203/sw7203.h Normal file
View File

@ -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: 300KHz1: 200KHz2: 400KHz3: 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: 60nS1: 20nS2: 40nS3: 80nS
uint8_t deadzoneM1ClosedM2Open : 2;
// M2 关 M1 开死区设置:
// 0: 60nS1: 20nS2: 40nS3: 60nS
uint8_t deadzoneM3ClosedM4Open : 2;
// M3 关 M4 开死区设置:
// 0: 60nS1: 20nS2: 40nS3: 80nS
uint8_t deadzoneM3OpenM4Closed : 2;
// M4 关 M3 开死区设置:
// 0: 60nS1: 20nS2: 40nS3: 60nS
} config;
} funcConfig2;
union
{
uint8_t hex; // 8位无符号整数用于将位域组合成一个字节方便程序读取
struct
{
uint8_t M2MOSRdsOn : 2;
// M2 功率管内阻设置:
// 0: 2.5mR1: 5mR2: 7.5mR3: 10mR
uint8_t nductanceValue : 2;
// 电感感值设置:
// 0: 1uH1: 2.2uH2: 3.3uH3: 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: 100mA1: 200mA, 3: 300mA4: 400mA
uint8_t batteryCellNum : 1;
// 电池节数设置:
// 0: 1节1: 2节, 3: 3节4: 4节
} config;
} funcConfig4;
};
#endif

View File

@ -1,4 +1,4 @@
idf_component_register(SRCS "main.cpp" idf_component_register(SRCS "main.c"
INCLUDE_DIRS ".") INCLUDE_DIRS ".")

View File

@ -22,53 +22,26 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#include "esp_log.h" #include "esp_log.h"
#include "bq4050.h" #include "bq4050.h"
#include "gpio_cxx.hpp"
#include "driver/gpio.h" #include "driver/gpio.h"
#include <thread> #include <thread>
using namespace std;
using namespace idf;
#define SetBitTrue(a, b) a |= (1 << b) #define SetBitTrue(a, b) a |= (1 << b)
#define SetBitFalse(a, b) a &= ~(1 << b) #define SetBitFalse(a, b) a &= ~(1 << b)
#define GetBit(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) static esp_err_t i2c0_master_init()
#define ITF_NUM_TOTAL 1
static esp_err_t bq4050_i2c_master_init()
{ {
i2c_config_t conf = { i2c_config_t conf = {
.mode = I2C_MODE_MASTER, .mode = I2C_MODE_MASTER,
.sda_io_num = BQ4050_I2C_MASTER_SDA_IO, .sda_io_num = I2C0_SDA_IO,
.scl_io_num = BQ4050_I2C_MASTER_SCL_IO, .scl_io_num = I2C0_SCL_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE, .sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE, .scl_pullup_en = GPIO_PULLUP_ENABLE,
.master{ .master{
.clk_speed = BQ4050_I2C_MASTER_FREQ_HZ}, .clk_speed = I2C_MASTER_TIMEOUT_MS},
}; };
i2c_param_config(I2C0_NUM, &conf);
BQ4050::I2C_MASTER = BQ4050_I2C_MASTER_NUM; return i2c_driver_install(I2C0_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
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);
} }
esp_err_t sw7203_register_read(uint8_t reg_addr, uint8_t *data) esp_err_t sw7203_register_read(uint8_t reg_addr, uint8_t *data)