TB2026-YDS Firmware
Loading...
Searching...
No Matches
power.c File Reference

Power service implementation: fuel-gauge snapshot, charge/level policy, on/off. More...

#include "power.h"
#include "board_pins.h"
#include "fuel_gauge.h"
#include "gpio_expander.h"
#include "settings.h"
#include "driver/gpio.h"
#include "diag.h"
#include "nvs.h"
#include "esp_core_dump.h"
#include "esp_partition.h"
#include "storage.h"
#include "sdcard.h"
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"

Macros

#define LOW_SOC_PCT   15.0f
#define CRITICAL_SOC_PCT   5.0f /* red flag in the UI; the user's advance warning */
#define SHUTDOWN_SOC_PCT   1.0f /* graceful auto-off, decoupled so the red flag warns first */
#define CHARGE_CURRENT_MA   10.0f /* current into the cell above this = charging */
#define USB_AUTOROUTE_TIMEOUT_MS   5000 /* give up waiting for INOKB (no valid source) */
#define USB_CHARGER_HOLD_MS   1200
#define USB_AUTOROUTE_POLL_MS   100
#define OFF_NVS_NS   "power"
#define OFF_NVS_KEY   "clean_off"
#define SLEEP_MS_KEY   "sleep_ms"
#define POWEROFF_MS_KEY   "poweroff_ms"
#define SLEEP_MS_DEFAULT   30000u /* screen off after 30 s idle */
#define POWEROFF_MS_DEFAULT   300000u /* auto power-off after 5 min idle */

Functions

void power_self_hold (void)
 Latch the regulator on by driving EnableReg high.
void power_tick (void)
 Refresh the cached battery state and apply the low-battery policy.
bool power_soc_at_shutdown (float soc_pct)
 True when this SoC is at or below the graceful auto-off threshold.
void power_get_state (power_state_t *out)
 Copy the latest cached battery snapshot.
void power_set_usb_route (power_usb_route_t route)
 Route the USB-C mux to data or charge.
power_usb_route_t power_get_usb_route (void)
 The route currently driven onto PIN_USB_DIR (for diagnostics).
void power_usb_autoroute_start (void)
 USB mux hand-off: charger first, then console.
void power_set_low_batt_shutdown (bool enable)
 Enable or disable the built-in critical-battery auto-shutdown (default enabled).
void power_set_shutdown_hook (power_shutdown_hook_t hook)
 Register a callback to run at the start of power_shutdown().
uint32_t power_get_sleep_ms (void)
 Idle time before the screen blanks (UI task). 0 = never sleep.
void power_set_sleep_ms (uint32_t ms)
 Set and persist the screen-sleep timeout.
uint32_t power_get_poweroff_ms (void)
 Idle time before the board auto powers off (maintenance task). 0 = never.
void power_set_poweroff_ms (uint32_t ms)
 Set and persist the auto power-off timeout.
void power_boot_off_check (void)
 Classify how the previous session ended (call once at boot, after NVS is up).
power_off_cause_t power_last_off_cause (void)
 How the previous session ended. Valid after power_boot_off_check().
void power_coredump_export (void)
 Copy a pending panic core dump from flash to the SD card, then erase it.
const char * power_last_crash_task (void)
 Faulting task name when the cause is POWER_OFF_CRASH ("" otherwise).
uint32_t power_last_crash_pc (void)
 Faulting program counter when the cause is POWER_OFF_CRASH (0 otherwise).
void power_shutdown (void)
 Run the shutdown hook (if any), release EnableReg, and power the board off.

Detailed Description

Power service implementation: fuel-gauge snapshot, charge/level policy, on/off.

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

◆ CHARGE_CURRENT_MA

#define CHARGE_CURRENT_MA   10.0f /* current into the cell above this = charging */

◆ CRITICAL_SOC_PCT

#define CRITICAL_SOC_PCT   5.0f /* red flag in the UI; the user's advance warning */

◆ LOW_SOC_PCT

#define LOW_SOC_PCT   15.0f

◆ OFF_NVS_KEY

#define OFF_NVS_KEY   "clean_off"

◆ OFF_NVS_NS

#define OFF_NVS_NS   "power"

◆ POWEROFF_MS_DEFAULT

#define POWEROFF_MS_DEFAULT   300000u /* auto power-off after 5 min idle */

◆ POWEROFF_MS_KEY

#define POWEROFF_MS_KEY   "poweroff_ms"

◆ SHUTDOWN_SOC_PCT

#define SHUTDOWN_SOC_PCT   1.0f /* graceful auto-off, decoupled so the red flag warns first */

◆ SLEEP_MS_DEFAULT

#define SLEEP_MS_DEFAULT   30000u /* screen off after 30 s idle */

◆ SLEEP_MS_KEY

#define SLEEP_MS_KEY   "sleep_ms"

◆ USB_AUTOROUTE_POLL_MS

#define USB_AUTOROUTE_POLL_MS   100

◆ USB_AUTOROUTE_TIMEOUT_MS

#define USB_AUTOROUTE_TIMEOUT_MS   5000 /* give up waiting for INOKB (no valid source) */

◆ USB_CHARGER_HOLD_MS

#define USB_CHARGER_HOLD_MS   1200