ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_batt.h File Reference

Bounded, allocation-free low-battery nag policy. More...

#include <stdint.h>
#include "ra8_err.h"
Include dependency graph for ra8_batt.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ra8_batt_monitor_t
 Caller-owned nag state carried across ra8_batt_update calls. More...

Enumerations

enum  ra8_batt_nag_t : uint8_t {
  k_ra8_batt_nag_none = 0U ,
  k_ra8_batt_nag_low = 1U ,
  k_ra8_batt_nag_critical = 2U
}
 The low-battery warning a single ra8_batt_update step raises. More...
enum  ra8_batt_threshold_t : uint8_t {
  k_ra8_batt_low_pct = 20U ,
  k_ra8_batt_critical_pct = 10U ,
  k_ra8_batt_rearm_margin = 3U ,
  k_ra8_batt_pct_max = 100U
}
 Nag thresholds and hysteresis margin, in whole percent. More...

Functions

ra8_err_t ra8_batt_monitor_init (ra8_batt_monitor_t *mon)
 Reset a nag monitor to the un-nagged, fully-armed state.
ra8_err_t ra8_batt_update (ra8_batt_monitor_t *mon, uint8_t soc_pct, bool charging, ra8_batt_nag_t *out_nag)
 Fold one SOC reading into the monitor and report the nag to raise.
const char * ra8_batt_nag_str (ra8_batt_nag_t nag)
 Map a nag level to a short, stable upper-case label.

Detailed Description

Bounded, allocation-free low-battery nag policy.

ra8_batt decides when to raise a low-battery warning from a stream of state-of-charge (SOC) readings. It is geometry-free and hardware-free: a caller feeds it the percent + charge flag each tick (e.g. from a MAX17048 fuel gauge read over ra8_smbus) and it returns the nag to surface, if any. The display / UART / e-reader chrome layer on top decide how to show it.

The policy is edge-triggered with hysteresis, so a steady or jittering battery does not spam the user:

The decision is a single forward step with no loop, no recursion, and no allocation (NASA P10 Rules 1-3), so it runs identically on the host test harness and on the RA8D2.

[Ring 5 / UI] {World: NS}

Since
0.1.0

Definition in file ra8_batt.h.

Enumeration Type Documentation

◆ ra8_batt_nag_t

enum ra8_batt_nag_t : uint8_t

The low-battery warning a single ra8_batt_update step raises.

Returned by value through ra8_batt_update. k_ra8_batt_nag_none means no new edge this step (the common case); the other two are the one-shot warnings for the low and critical bands.

Enumerator
k_ra8_batt_nag_none 

No new warning this step.

k_ra8_batt_nag_low 

SOC fell to the low band (<=20%).

k_ra8_batt_nag_critical 

SOC fell to the critical band (<=10%).

Definition at line 54 of file ra8_batt.h.

◆ ra8_batt_threshold_t

enum ra8_batt_threshold_t : uint8_t

Nag thresholds and hysteresis margin, in whole percent.

The bands are inclusive at and below their threshold; a band re-arms once SOC rises strictly above (threshold + k_ra8_batt_rearm_margin) so a battery hovering at the boundary does not re-nag every step.

Enumerator
k_ra8_batt_low_pct 

Low-battery threshold (inclusive).

k_ra8_batt_critical_pct 

Critical threshold (inclusive).

k_ra8_batt_rearm_margin 

Hysteresis above a band to re-arm it.

k_ra8_batt_pct_max 

SOC clamp ceiling.

Definition at line 68 of file ra8_batt.h.

Function Documentation

◆ ra8_batt_monitor_init()

ra8_err_t ra8_batt_monitor_init ( ra8_batt_monitor_t * mon)

Reset a nag monitor to the un-nagged, fully-armed state.

Writes zero to both band-raised flags inside mon, placing the monitor into the fully-armed state so that the first subsequent call to ra8_batt_update can raise either the low or critical nag. The operation is a single struct clear with no loops, no allocation, and no hardware access, making it safe to call from any initialisation context on both the RA8D2 target and the host unit-test harness.

Parameters
[out]monMonitor to initialise.
Returns
ra8_err_t Error code.
Return values
k_ra8_okMonitor zeroed; both bands armed.
k_ra8_err_null_ptrmon is nullptr.
Precondition
mon points to writable storage.
Called before the first ra8_batt_update on this monitor.
Postcondition
Both band flags are false (armed).
Subsequent ra8_batt_update can raise either band.
Note
Not thread-safe; one monitor per single-threaded reader.
See also
ra8_batt_update()
Since
0.1.0

Definition at line 27 of file ra8_batt.c.

References ra8_batt_monitor_t::critical_raised, k_ra8_ok, ra8_batt_monitor_t::low_raised, RA8_CHECK_NULL_PTR, and s_tag.

Referenced by main().

◆ ra8_batt_nag_str()

const char * ra8_batt_nag_str ( ra8_batt_nag_t nag)

Map a nag level to a short, stable upper-case label.

Parameters
[in]nagNag level.
Returns
A static NUL-terminated label.
Return values
OKFor k_ra8_batt_nag_none.
LOWFor k_ra8_batt_nag_low.
CRITICALFor k_ra8_batt_nag_critical.
?For any out-of-range value.
Precondition
None.
Postcondition
The returned pointer is non-null (asserted) and immutable.
Note
Thread-safe (returns a pointer to static storage).
Since
0.1.0

Definition at line 71 of file ra8_batt.c.

References k_ra8_batt_nag_critical, k_ra8_batt_nag_low, k_ra8_batt_nag_none, and RA8_ASSERT.

◆ ra8_batt_update()

ra8_err_t ra8_batt_update ( ra8_batt_monitor_t * mon,
uint8_t soc_pct,
bool charging,
ra8_batt_nag_t * out_nag )

Fold one SOC reading into the monitor and report the nag to raise.

Re-arms each band when charging or when soc_pct has recovered above the band threshold by k_ra8_batt_rearm_margin, then, only while not charging, raises the low and/or critical band the reading newly enters. When a reading enters both bands at once (a large drop), the returned nag is the worse one (k_ra8_batt_nag_critical). soc_pct above k_ra8_batt_pct_max is clamped.

Parameters
[in,out]monMonitor state (updated in place).
[in]soc_pctCurrent state-of-charge percent (clamped to 0..100).
[in]chargingTrue when the fuel gauge reports a charging rate.
[out]out_nagReceives the nag to surface this step.
Returns
ra8_err_t Error code.
Return values
k_ra8_okStep folded; out_nag written.
k_ra8_err_null_ptrmon or out_nag is nullptr.
Precondition
mon was initialised by ra8_batt_monitor_init.
out_nag points to writable storage.
Postcondition
out_nag holds one of ra8_batt_nag_t.
A band that fired is marked raised until it re-arms.
Note
Not thread-safe; one monitor per single-threaded reader.
See also
ra8_batt_monitor_init()
Since
0.1.0

Definition at line 36 of file ra8_batt.c.

References ra8_batt_monitor_t::critical_raised, k_ra8_batt_critical_pct, k_ra8_batt_low_pct, k_ra8_batt_nag_critical, k_ra8_batt_nag_low, k_ra8_batt_nag_none, k_ra8_batt_pct_max, k_ra8_batt_rearm_margin, k_ra8_ok, ra8_batt_monitor_t::low_raised, RA8_CHECK_NULL_PTR, and s_tag.

Referenced by er_poll_battery(), and main().