STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
sleep_math.c
Go to the documentation of this file.
1#include "sleep_math.h"
2
3uint16_t sleep_wut_reload(uint32_t ms, uint32_t lsi_hz) {
4 uint64_t ticks = (uint64_t)ms * lsi_hz / (1000ULL * SLEEP_WUT_DIV);
5 if (ticks < 1U) {
6 ticks = 1U;
7 }
8 if (ticks > 65536U) {
9 ticks = 65536U;
10 }
11 return (uint16_t)(ticks - 1U);
12}
13
14uint32_t sleep_rtc_steps(uint8_t hours, uint8_t minutes, uint8_t seconds,
15 uint32_t ssr) {
16 uint32_t s = (uint32_t)hours * 3600U + (uint32_t)minutes * 60U + seconds;
17 if (ssr > SLEEP_RTC_SYNC_DIV - 1U) {
18 ssr = SLEEP_RTC_SYNC_DIV - 1U;
19 }
20 return s * SLEEP_RTC_SYNC_DIV + (SLEEP_RTC_SYNC_DIV - 1U - ssr);
21}
22
23uint32_t sleep_elapsed_ms(uint32_t from_steps, uint32_t to_steps,
24 uint32_t lsi_hz) {
25 uint32_t steps =
26 (to_steps + SLEEP_DAY_STEPS - from_steps) % SLEEP_DAY_STEPS;
27 if (lsi_hz == 0U) {
28 return 0U;
29 }
30 return (uint32_t)((uint64_t)steps * SLEEP_RTC_ASYNC_DIV * 1000ULL / lsi_hz);
31}
32
33uint32_t sleep_lsi_hz(uint32_t steps, uint32_t us) {
34 if (us == 0U) {
35 return 0U;
36 }
37 return (uint32_t)((uint64_t)steps * SLEEP_RTC_ASYNC_DIV * 1000000ULL / us);
38}
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
#define SLEEP_RTC_SYNC_DIV
Definition sleep_math.h:23
#define SLEEP_DAY_STEPS
Definition sleep_math.h:25
#define SLEEP_WUT_DIV
Definition sleep_math.h:24
#define SLEEP_RTC_ASYNC_DIV
Definition sleep_math.h:22