STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_sleep_math.c
Go to the documentation of this file.
1/* sleep_math: wakeup-timer reloads, RTC stamps and elapsed time. Expected
2 * values computed independently in Python from RM0468 51.4.7 and the
3 * prescalers in firmware.ioc (127 / 249, WUT on RTCCLK / 16). */
4#include <stdio.h>
5#include <stdlib.h>
6
7#include "sleep_math.h"
8
9#define CHECK(c) \
10 do { \
11 if (!(c)) { \
12 fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, #c); \
13 exit(1); \
14 } \
15 } while (0)
16
17static void test_wut_reload(void) {
18 CHECK(sleep_wut_reload(10000, 32000) == 19999); /* 20000 ticks */
19 /* The measured LSI decides the reload: a slow LSI gets fewer ticks so
20 * the real sleep is still 10 s. */
21 CHECK(sleep_wut_reload(10000, 29500) == 18436);
22 CHECK(sleep_wut_reload(10000, 34500) == 21561);
23 CHECK(sleep_wut_reload(1, 32000) == 1); /* 2 ticks */
24 CHECK(sleep_wut_reload(0, 32000) == 0); /* at least 1 tick */
25 CHECK(sleep_wut_reload(60000, 32000) == 65535); /* clamped, ~32.8 s */
26}
27
29 /* SSR counts down: 249 is the start of a second, 0 its end. */
30 CHECK(sleep_rtc_steps(0, 0, 0, 249) == 0);
31 CHECK(sleep_rtc_steps(0, 0, 0, 0) == 249);
32 CHECK(sleep_rtc_steps(0, 0, 1, 249) == 250);
33 CHECK(sleep_rtc_steps(1, 2, 3, 249) == 3723U * 250U);
34}
35
36static void test_elapsed(void) {
37 uint32_t a = sleep_rtc_steps(0, 0, 5, 249),
38 b = sleep_rtc_steps(0, 0, 15, 249);
39 CHECK(sleep_elapsed_ms(a, b, 32000) == 10000);
40 /* 10 RTC seconds on a 30 kHz LSI are 10.67 real seconds. */
41 CHECK(sleep_elapsed_ms(a, b, 30000) == 10666);
42 /* Across midnight. */
43 CHECK(sleep_elapsed_ms(sleep_rtc_steps(23, 59, 58, 100),
44 sleep_rtc_steps(0, 0, 8, 100), 32000) == 10000);
45 /* A partial sleep (DIO1 woke it): 3.2 s. */
47 sleep_rtc_steps(0, 0, 8, 199), 32000) == 3200);
48 CHECK(sleep_elapsed_ms(a, a, 32000) == 0);
49 CHECK(sleep_elapsed_ms(a, b, 0) == 0);
50}
51
52static void test_lsi_measurement(void) {
53 CHECK(sleep_lsi_hz(125, 500000) == 32000);
54 CHECK(sleep_lsi_hz(125, 533333) == 30000);
55 CHECK(sleep_lsi_hz(125, 0) == 0);
56}
57
58int main(void) {
63 puts("sleep_math: all tests passed");
64 return 0;
65}
*brief create JSON pointer *sa reference_tokens const string_t const string_t & b
Definition json.hpp:13878
*brief create JSON pointer *sa reference_tokens const string_t & a
Definition json.hpp:13877
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
int main()
static void test_wut_reload(void)
static void test_lsi_measurement(void)
#define CHECK(c)
static void test_rtc_steps_count_down_subseconds(void)
static void test_elapsed(void)