STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
sleep_math.h
Go to the documentation of this file.
1#ifndef STAR_SLEEP_MATH_H
2#define STAR_SLEEP_MATH_H
3
4/*
5 * Arithmetic for sleeping in Stop on the RTC (board_sleep.c), portable.
6 *
7 * RTC from LSI (no LSE: PC14/PC15 are pyro pins). Prescalers 127 / 249
8 * (firmware.ioc): ck_apre = LSI / 128 drives the sub-second counter SSR,
9 * which counts DOWN from 249 to 0, then the seconds tick. One SSR step is
10 * 128 / LSI seconds: 4 ms at the nominal 32 kHz, but LSI varies chip to
11 * chip and with temperature, so board_sleep measures it against TIM2.
12 * Wakeup timer on RTCCLK / 16 (RM0468 51.4.7, p.1950): one WUT tick is
13 * 16 / LSI s, reload R wakes after R + 1 ticks, at most 65536 (~32 s).
14 */
15
16#include <stdint.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22#define SLEEP_RTC_ASYNC_DIV 128U /* AsynchPrediv + 1 */
23#define SLEEP_RTC_SYNC_DIV 250U /* SynchPrediv + 1 */
24#define SLEEP_WUT_DIV 16U /* RTC_WAKEUPCLOCK_RTCCLK_DIV16 */
25#define SLEEP_DAY_STEPS (86400UL * SLEEP_RTC_SYNC_DIV)
26
27/* WUT reload for a sleep of `ms` at `lsi_hz` (rounded down, so the board
28 * never oversleeps; at least 1 tick, at most 65536). */
29uint16_t sleep_wut_reload(uint32_t ms, uint32_t lsi_hz);
30
31/* Calendar time of day -> SSR steps since midnight (seconds x 250 plus the
32 * steps already counted down in this second). */
33uint32_t sleep_rtc_steps(uint8_t hours, uint8_t minutes, uint8_t seconds,
34 uint32_t ssr);
35
36/* Milliseconds between two stamps, across midnight, at `lsi_hz`. */
37uint32_t sleep_elapsed_ms(uint32_t from_steps, uint32_t to_steps,
38 uint32_t lsi_hz);
39
40/* LSI frequency from `steps` SSR steps that took `us` on TIM2 (1 MHz). */
41uint32_t sleep_lsi_hz(uint32_t steps, uint32_t us);
42
43#ifdef __cplusplus
44}
45#endif
46#endif
uint32_t sleep_elapsed_ms(uint32_t from_steps, uint32_t to_steps, uint32_t lsi_hz)
Definition sleep_math.c:23
uint32_t sleep_lsi_hz(uint32_t steps, uint32_t us)
Definition sleep_math.c:33
uint32_t sleep_rtc_steps(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t ssr)
Definition sleep_math.c:14
uint16_t sleep_wut_reload(uint32_t ms, uint32_t lsi_hz)
Definition sleep_math.c:3