|
TB2026-YDS Firmware
|
#include "fuel_gauge.h"#include "i2c_bus.h"#include "freertos/FreeRTOS.h"#include "freertos/task.h"Macros | |
| #define | FUEL_GAUGE_ADDR 0x36 |
| #define | FG_I2C_TIMEOUT 100 /* ms; fail loud rather than block forever */ |
| #define | FG_RSENSE_MOHM 5.0f /* sense resistor, mOhm */ |
| #define | FG_DESIGN_CAP_MAH 950 /* cell nominal capacity */ |
| #define | FG_ICHG_TERM_MA 50 /* charger termination current: MAX77757 ITOPOFF */ |
| #define | CAP_LSB_MAH (5.0f / FG_RSENSE_MOHM) /* 5.0uVh/Rsense -> mAh (1.0 at 5mOhm) */ |
| #define | CURRENT_LSB_MA (1.5625f / FG_RSENSE_MOHM) /* 1.5625uV/Rsense -> mA (0.3125 at 5mOhm) */ |
| #define | VOLT_LSB_V 78.125e-6f /* 78.125uV per LSB */ |
| #define | TIME_LSB_S 5.625f /* 5.625s per LSB */ |
| #define | PCT_LSB (1.0f / 256.0f) /* 1/256 % per LSB */ |
| #define | TEMP_LSB_C (1.0f / 256.0f) /* 1/256 degC per LSB */ |
| #define | CYCLES_LSB (1.0f / 100.0f) /* 1% per LSB -> full cycles */ |
| #define | DESIGN_CAP_RAW ((uint16_t)(FG_DESIGN_CAP_MAH / CAP_LSB_MAH)) |
| #define | ICHG_TERM_RAW ((uint16_t)(FG_ICHG_TERM_MA / CURRENT_LSB_MA)) |
| #define | VEMPTY_DEFAULT 0xA561 /* VE = 3.3V, VR = 3.88V (POR default, recommended) */ |
| #define | MODEL_CFG 0x8000 /* Refresh=1, ModelID=0 (LiCoO2), VChg=0 (4.2V charge) */ |
| #define | REG_REP_CAP 0x05 |
| #define | REG_FULL_CAP_REP 0x10 |
| #define | REG_FULL_CAP_NOM 0x23 |
| #define | REG_REP_SOC 0x06 |
| #define | REG_AGE 0x07 |
| #define | REG_TEMP 0x08 |
| #define | REG_VCELL 0x09 |
| #define | REG_CURRENT 0x0A |
| #define | REG_TTE 0x11 |
| #define | REG_CYCLES 0x17 |
| #define | REG_TTF 0x20 |
| #define | REG_STATUS 0x00 |
| #define | REG_DESIGN_CAP 0x18 |
| #define | REG_ICHG_TERM 0x1E |
| #define | REG_VEMPTY 0x3A |
| #define | REG_FSTAT 0x3D |
| #define | REG_CMD 0x60 |
| #define | REG_HIBCFG 0xBA |
| #define | REG_MODEL_CFG 0xDB |
| #define | STATUS_POR 0x0002 /* power-on reset flag */ |
| #define | FSTAT_DNR 0x0001 /* data not ready */ |
| #define | MODELCFG_REFRESH 0x8000 /* model reload in progress */ |
| #define | CMD_SOFT_WAKEUP 0x0090 |
| #define | CMD_CLEAR 0x0000 |
| #define | REFRESH_TIMEOUT_MS 800 /* DNR clear / model refresh upper bound (~710ms typ) */ |
Functions | |
| esp_err_t | fuel_gauge_init (i2c_master_bus_handle_t bus) |
| Attach the gauge to the shared I2C bus and run EZ config if needed. | |
| esp_err_t | fuel_gauge_read (fuel_gauge_data_t *out) |
| Read and convert all fields of a fuel_gauge_data_t in one pass. | |
| esp_err_t | fuel_gauge_read_debug (fuel_gauge_debug_t *out) |
| Read the configuration and learned-capacity registers for the debug page. | |
| esp_err_t | fuel_gauge_reload_ez (void) |
| Re-run the EZ model load to purge the learned battery state. | |
| #define CAP_LSB_MAH (5.0f / FG_RSENSE_MOHM) /* 5.0uVh/Rsense -> mAh (1.0 at 5mOhm) */ |
| #define CMD_CLEAR 0x0000 |
| #define CMD_SOFT_WAKEUP 0x0090 |
| #define CURRENT_LSB_MA (1.5625f / FG_RSENSE_MOHM) /* 1.5625uV/Rsense -> mA (0.3125 at 5mOhm) */ |
| #define CYCLES_LSB (1.0f / 100.0f) /* 1% per LSB -> full cycles */ |
| #define DESIGN_CAP_RAW ((uint16_t)(FG_DESIGN_CAP_MAH / CAP_LSB_MAH)) |
| #define FG_DESIGN_CAP_MAH 950 /* cell nominal capacity */ |
| #define FG_I2C_TIMEOUT 100 /* ms; fail loud rather than block forever */ |
| #define FG_ICHG_TERM_MA 50 /* charger termination current: MAX77757 ITOPOFF */ |
| #define FG_RSENSE_MOHM 5.0f /* sense resistor, mOhm */ |
| #define FSTAT_DNR 0x0001 /* data not ready */ |
| #define FUEL_GAUGE_ADDR 0x36 |
| #define ICHG_TERM_RAW ((uint16_t)(FG_ICHG_TERM_MA / CURRENT_LSB_MA)) |
| #define MODEL_CFG 0x8000 /* Refresh=1, ModelID=0 (LiCoO2), VChg=0 (4.2V charge) */ |
| #define MODELCFG_REFRESH 0x8000 /* model reload in progress */ |
| #define PCT_LSB (1.0f / 256.0f) /* 1/256 % per LSB */ |
| #define REFRESH_TIMEOUT_MS 800 /* DNR clear / model refresh upper bound (~710ms typ) */ |
| #define REG_AGE 0x07 |
| #define REG_CMD 0x60 |
| #define REG_CURRENT 0x0A |
| #define REG_CYCLES 0x17 |
| #define REG_DESIGN_CAP 0x18 |
| #define REG_FSTAT 0x3D |
| #define REG_FULL_CAP_NOM 0x23 |
| #define REG_FULL_CAP_REP 0x10 |
| #define REG_HIBCFG 0xBA |
| #define REG_ICHG_TERM 0x1E |
| #define REG_MODEL_CFG 0xDB |
| #define REG_REP_CAP 0x05 |
| #define REG_REP_SOC 0x06 |
| #define REG_STATUS 0x00 |
| #define REG_TEMP 0x08 |
| #define REG_TTE 0x11 |
| #define REG_TTF 0x20 |
| #define REG_VCELL 0x09 |
| #define REG_VEMPTY 0x3A |
| #define STATUS_POR 0x0002 /* power-on reset flag */ |
| #define TEMP_LSB_C (1.0f / 256.0f) /* 1/256 degC per LSB */ |
| #define TIME_LSB_S 5.625f /* 5.625s per LSB */ |
| #define VEMPTY_DEFAULT 0xA561 /* VE = 3.3V, VR = 3.88V (POR default, recommended) */ |
| #define VOLT_LSB_V 78.125e-6f /* 78.125uV per LSB */ |