TB2026-YDS Firmware
Loading...
Searching...
No Matches
fuel_gauge.c File Reference
#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.

Detailed Description

Note
Développé avec l'assistance de Claude Opus 4.8 (Anthropic), sous la direction de Y. Dos Santos : spécification, revue et validation sur cible par l'auteur.

Macro Definition Documentation

◆ CAP_LSB_MAH

#define CAP_LSB_MAH   (5.0f / FG_RSENSE_MOHM) /* 5.0uVh/Rsense -> mAh (1.0 at 5mOhm) */

◆ CMD_CLEAR

#define CMD_CLEAR   0x0000

◆ CMD_SOFT_WAKEUP

#define CMD_SOFT_WAKEUP   0x0090

◆ CURRENT_LSB_MA

#define CURRENT_LSB_MA   (1.5625f / FG_RSENSE_MOHM) /* 1.5625uV/Rsense -> mA (0.3125 at 5mOhm) */

◆ CYCLES_LSB

#define CYCLES_LSB   (1.0f / 100.0f) /* 1% per LSB -> full cycles */

◆ DESIGN_CAP_RAW

#define DESIGN_CAP_RAW   ((uint16_t)(FG_DESIGN_CAP_MAH / CAP_LSB_MAH))

◆ FG_DESIGN_CAP_MAH

#define FG_DESIGN_CAP_MAH   950 /* cell nominal capacity */

◆ FG_I2C_TIMEOUT

#define FG_I2C_TIMEOUT   100 /* ms; fail loud rather than block forever */

◆ FG_ICHG_TERM_MA

#define FG_ICHG_TERM_MA   50 /* charger termination current: MAX77757 ITOPOFF */

◆ FG_RSENSE_MOHM

#define FG_RSENSE_MOHM   5.0f /* sense resistor, mOhm */

◆ FSTAT_DNR

#define FSTAT_DNR   0x0001 /* data not ready */

◆ FUEL_GAUGE_ADDR

#define FUEL_GAUGE_ADDR   0x36

◆ ICHG_TERM_RAW

#define ICHG_TERM_RAW   ((uint16_t)(FG_ICHG_TERM_MA / CURRENT_LSB_MA))

◆ MODEL_CFG

#define MODEL_CFG   0x8000 /* Refresh=1, ModelID=0 (LiCoO2), VChg=0 (4.2V charge) */

◆ MODELCFG_REFRESH

#define MODELCFG_REFRESH   0x8000 /* model reload in progress */

◆ PCT_LSB

#define PCT_LSB   (1.0f / 256.0f) /* 1/256 % per LSB */

◆ REFRESH_TIMEOUT_MS

#define REFRESH_TIMEOUT_MS   800 /* DNR clear / model refresh upper bound (~710ms typ) */

◆ REG_AGE

#define REG_AGE   0x07

◆ REG_CMD

#define REG_CMD   0x60

◆ REG_CURRENT

#define REG_CURRENT   0x0A

◆ REG_CYCLES

#define REG_CYCLES   0x17

◆ REG_DESIGN_CAP

#define REG_DESIGN_CAP   0x18

◆ REG_FSTAT

#define REG_FSTAT   0x3D

◆ REG_FULL_CAP_NOM

#define REG_FULL_CAP_NOM   0x23

◆ REG_FULL_CAP_REP

#define REG_FULL_CAP_REP   0x10

◆ REG_HIBCFG

#define REG_HIBCFG   0xBA

◆ REG_ICHG_TERM

#define REG_ICHG_TERM   0x1E

◆ REG_MODEL_CFG

#define REG_MODEL_CFG   0xDB

◆ REG_REP_CAP

#define REG_REP_CAP   0x05

◆ REG_REP_SOC

#define REG_REP_SOC   0x06

◆ REG_STATUS

#define REG_STATUS   0x00

◆ REG_TEMP

#define REG_TEMP   0x08

◆ REG_TTE

#define REG_TTE   0x11

◆ REG_TTF

#define REG_TTF   0x20

◆ REG_VCELL

#define REG_VCELL   0x09

◆ REG_VEMPTY

#define REG_VEMPTY   0x3A

◆ STATUS_POR

#define STATUS_POR   0x0002 /* power-on reset flag */

◆ TEMP_LSB_C

#define TEMP_LSB_C   (1.0f / 256.0f) /* 1/256 degC per LSB */

◆ TIME_LSB_S

#define TIME_LSB_S   5.625f /* 5.625s per LSB */

◆ VEMPTY_DEFAULT

#define VEMPTY_DEFAULT   0xA561 /* VE = 3.3V, VR = 3.88V (POR default, recommended) */

◆ VOLT_LSB_V

#define VOLT_LSB_V   78.125e-6f /* 78.125uV per LSB */