STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
idle.h
Go to the documentation of this file.
1#ifndef STAR_IDLE_H
2#define STAR_IDLE_H
3
4/*
5 * Idle state logic: portable, no HAL, no RTOS, no heap.
6 *
7 * The state machine calls idle_enter() once on entry from self-test, then
8 * idle_on_wake() every time the MCU wakes (RTC wakeup or radio DIO1). Idle
9 * answers with what the state machine should do next; it never sleeps,
10 * schedules the RTC, changes state or touches pyro outputs itself.
11 */
12
13#include "idle_port.h"
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19/* Upper bound on commands drained per wake, so a noisy link cannot keep the
20 * MCU awake indefinitely. */
21#define IDLE_MAX_COMMANDS_PER_WAKE 4U
22
23typedef struct {
24 uint32_t heartbeat_interval_ms; /* proposed 10000 */
25 /* A wake this early is still treated as on time, to absorb LSI drift. */
26 uint32_t wake_tolerance_ms; /* proposed 500; must be < interval */
28 uint16_t battery_warn_mv; /* proposed 7000 (not yet approved) */
29 uint16_t battery_fail_mv; /* proposed 6600 (not yet approved) */
30 uint16_t battery_hysteresis_mv; /* proposed 100 */
32
33typedef enum {
34 /* Sleep for sleep_s seconds (RTC wakeup), radio stays in RX. */
36 /* A valid Arm command arrived. The state machine applies the arm policy;
37 * Idle has not transitioned and has not touched any output. */
39 /* idle_on_wake() was called on an invalid instance, or before
40 * idle_enter(). */
43
44typedef struct {
46 uint32_t sleep_s; /* valid for IDLE_EVENT_SLEEP; at least 1 */
47 bool heartbeat_sent; /* a heartbeat was attempted on this wake */
49
50/* Fixed caller-owned storage. Zero-initialize; treat fields as private. */
51typedef struct {
54 void* context;
55 idle_heartbeat_t last; /* most recent heartbeat contents */
56 idle_battery_state_t battery_band; /* OK/WARN/FAIL memory for hysteresis */
59 bool entered; /* idle_enter() has set the heartbeat schedule */
60} idle_t;
61
62/* Validates config and requires every port callback. Inputs are copied.
63 * Returns false and leaves the instance disabled on invalid input. */
65 const idle_port_t* port, void* context);
66/* Call on entry to Idle. The first wake after entry sends a heartbeat. */
67bool idle_enter(idle_t* idle, uint32_t now_ms);
68/* Call on every wake. now_ms must come from a clock that keeps counting
69 * through Stop mode (see src/app/IDLE.md). Modular milliseconds;
70 * gaps < 2^31. */
71idle_result_t idle_on_wake(idle_t* idle, uint32_t now_ms);
72/* Most recent heartbeat contents; NULL for an invalid instance. */
74
75/* Exposed for tests: PA0 counts to supply millivolts (22k/10k divider,
76 * 3.32 V reference). Returns 0 for counts above the 16-bit range. */
77uint16_t idle_counts_to_mv(uint32_t raw_counts);
78
79#ifdef __cplusplus
80}
81#endif
82#endif
static const emmc_port_t port
Definition emmc_stm32.c:210
idle_event_t
Definition idle.h:33
@ IDLE_EVENT_ARM_REQUESTED
Definition idle.h:38
@ IDLE_EVENT_SLEEP
Definition idle.h:35
@ IDLE_EVENT_ERROR
Definition idle.h:41
idle_result_t idle_on_wake(idle_t *idle, uint32_t now_ms)
Definition idle.c:168
const idle_heartbeat_t * idle_last_heartbeat(const idle_t *idle)
Definition idle.c:211
bool idle_enter(idle_t *idle, uint32_t now_ms)
Definition idle.c:70
uint16_t idle_counts_to_mv(uint32_t raw_counts)
Definition idle.c:13
bool idle_init(idle_t *idle, const idle_config_t *config, const idle_port_t *port, void *context)
Definition idle.c:50
idle_battery_state_t
Definition idle_port.h:31
static idle_t idle
Definition idle_task.c:26
static const idle_config_t config
Definition idle_task.c:17
uint32_t callback_budget_ms
Definition idle.h:27
uint16_t battery_fail_mv
Definition idle.h:29
uint32_t wake_tolerance_ms
Definition idle.h:26
uint16_t battery_hysteresis_mv
Definition idle.h:30
uint32_t heartbeat_interval_ms
Definition idle.h:24
uint16_t battery_warn_mv
Definition idle.h:28
uint32_t sleep_s
Definition idle.h:46
idle_event_t event
Definition idle.h:45
bool heartbeat_sent
Definition idle.h:47
Definition idle.h:51
idle_battery_state_t battery_band
Definition idle.h:56
idle_port_t port
Definition idle.h:53
idle_config_t config
Definition idle.h:52
uint32_t next_heartbeat_ms
Definition idle.h:57
bool initialized
Definition idle.h:58
bool entered
Definition idle.h:59
void * context
Definition idle.h:54
idle_heartbeat_t last
Definition idle.h:55