ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_batt.c
Go to the documentation of this file.
1
16
17#include "ra8_batt.h"
18
19#include <stdint.h>
20
21#include "ra8_check.h"
22#include "ra8_err.h"
23
25static const char* const s_tag = "ra8_batt";
26
28{
29 RA8_CHECK_NULL_PTR(mon, s_tag, "mon must not be nullptr");
30 mon->low_raised = false;
31 mon->critical_raised = false;
32 return k_ra8_ok;
33}
34
36ra8_batt_update(ra8_batt_monitor_t* mon, uint8_t soc_pct, bool charging, ra8_batt_nag_t* out_nag)
37{
38 RA8_CHECK_NULL_PTR(mon, s_tag, "mon must not be nullptr");
39 RA8_CHECK_NULL_PTR(out_nag, s_tag, "out_nag must not be nullptr");
40
41 const uint8_t soc =
42 (soc_pct > (uint8_t)k_ra8_batt_pct_max) ? (uint8_t)k_ra8_batt_pct_max : soc_pct;
43 const uint8_t low_rearm =
44 (uint8_t)((uint8_t)k_ra8_batt_low_pct + (uint8_t)k_ra8_batt_rearm_margin);
45 const uint8_t crit_rearm =
46 (uint8_t)((uint8_t)k_ra8_batt_critical_pct + (uint8_t)k_ra8_batt_rearm_margin);
47
48 /* Re-arm a band once charging, or once SOC has recovered above its margin. */
49 if (charging || (soc > low_rearm)) {
50 mon->low_raised = false;
51 }
52 if (charging || (soc > crit_rearm)) {
53 mon->critical_raised = false;
54 }
55
57 if (!charging) {
58 if ((soc <= (uint8_t)k_ra8_batt_low_pct) && !mon->low_raised) {
59 mon->low_raised = true;
61 }
62 if ((soc <= (uint8_t)k_ra8_batt_critical_pct) && !mon->critical_raised) {
63 mon->critical_raised = true;
65 }
66 }
67 *out_nag = nag;
68 return k_ra8_ok;
69}
70
72{
73 const char* label;
74 switch (nag) {
76 label = "OK";
77 break;
79 label = "LOW";
80 break;
82 label = "CRITICAL";
83 break;
84 default:
85 label = "?";
86 break;
87 }
88 RA8_ASSERT(label != nullptr, "nag label must be non-null");
89 return label;
90}
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
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.
Definition ra8_batt.c:36
ra8_err_t ra8_batt_monitor_init(ra8_batt_monitor_t *mon)
Reset a nag monitor to the un-nagged, fully-armed state.
Definition ra8_batt.c:27
const char * ra8_batt_nag_str(ra8_batt_nag_t nag)
Map a nag level to a short, stable upper-case label.
Definition ra8_batt.c:71
Bounded, allocation-free low-battery nag policy.
@ k_ra8_batt_pct_max
SOC clamp ceiling.
Definition ra8_batt.h:72
@ k_ra8_batt_critical_pct
Critical threshold (inclusive).
Definition ra8_batt.h:70
@ k_ra8_batt_low_pct
Low-battery threshold (inclusive).
Definition ra8_batt.h:69
@ k_ra8_batt_rearm_margin
Hysteresis above a band to re-arm it.
Definition ra8_batt.h:71
ra8_batt_nag_t
The low-battery warning a single ra8_batt_update step raises.
Definition ra8_batt.h:54
@ k_ra8_batt_nag_none
No new warning this step.
Definition ra8_batt.h:55
@ k_ra8_batt_nag_critical
SOC fell to the critical band (<=10%).
Definition ra8_batt.h:57
@ k_ra8_batt_nag_low
SOC fell to the low band (<=20%).
Definition ra8_batt.h:56
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_ASSERT(condition, message)
Runtime invariant check.
Definition ra8_check.h:123
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Definition ra8_check.h:243
Error Code Definitions for ra8-firmware.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
Caller-owned nag state carried across ra8_batt_update calls.
Definition ra8_batt.h:85
bool low_raised
Low nag raised and not yet re-armed.
Definition ra8_batt.h:86
bool critical_raised
Critical nag raised and not yet re-armed.
Definition ra8_batt.h:87