STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
board_sleep.h
Go to the documentation of this file.
1#ifndef STAR_BOARD_SLEEP_H
2#define STAR_BOARD_SLEEP_H
3
4/*
5 * Sleep in Stop mode on the RTC wakeup timer (Idle: ~10 s between LoRa
6 * heartbeats). RM0468 Rev 3:
7 * Stop = D1 and D2 in DStop, system Stop; any EXTI wakes it, including
8 * the RTC wakeup timer and DIO1 (PB2, EXTI2) (p.259). After wake the
9 * system clock is HSI (STOPWUCK, p.333); SystemClock_Config() restores
10 * it. RAM and GPIO levels are kept, so pyro outputs stay low (never
11 * written here).
12 * NRST_STOP_D1 = 0 makes D1 DStop a reset (p.208): then Stop is refused
13 * and board_sleep() waits awake instead.
14 * With a debugger attached, DBGMCU keeps SWD alive in Stop (p.3230); not
15 * set otherwise, so flight current is not spent on it.
16 * Time: the RTC calendar is read before and after, scaled by the LSI rate
17 * measured at init (sleep_math.h), and FreeRTOS catches up on the ticks it
18 * missed, so osKernelGetTickCount() and uptime include the sleep.
19 */
20
21#include <stdbool.h>
22#include <stdint.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28typedef struct {
29 bool ready; /* RTC running, LSI measured */
30 bool stop_allowed; /* NRST_STOP_D1 = 1 in the option bytes */
31 uint32_t lsi_hz; /* measured against TIM2 */
33 uint32_t awake_waits; /* refused: not ready, NRST_STOP_D1 = 0, USB */
34 uint32_t last_slept_ms;
36
37/* Once, from a task, after board_time_start(): ~0.5 s to measure LSI. */
38bool board_sleep_init(void);
39
40/* Sleep up to `ms` in Stop. Returns the ms that really passed. Wakes early
41 * on any EXTI (radio DIO1). Other tasks are frozen until it returns.
42 * wake_now (may be NULL) is checked with the scheduler already suspended:
43 * true means work arrived just before (a queued command), so it returns 0
44 * without sleeping. Not ready / NRST_STOP_D1 = 0: returns 0 at once; the
45 * caller waits awake instead (board_sleep_info().stop_allowed). */
46uint32_t board_sleep(uint32_t ms, bool (*wake_now)(void));
47
49
50#ifdef __cplusplus
51}
52#endif
53#endif
uint32_t board_sleep(uint32_t ms, bool(*wake_now)(void))
Definition board_sleep.c:68
const board_sleep_info_t * board_sleep_info(void)
Definition board_sleep.c:64
bool board_sleep_init(void)
Definition board_sleep.c:52
uint32_t last_slept_ms
Definition board_sleep.h:34