L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1/*
2 * utils.h
3 *
4 * General-purpose timing and value-stability utilities.
5 *
6 * Created on: May 5, 2024
7 * Author: Milan
8 */
9
10#ifndef UTILS_UTILS_H_
11#define UTILS_UTILS_H_
12
13#include <stdint.h>
14
15
16/**
17 * @brief Calculate a CRC-8 checksum (Sensirion polynomial) over a byte buffer.
18 * Used to validate sensor I2C response packets as well as the CRC field
19 * of systemParams_t.
20 * @param data Pointer to the data buffer.
21 * @param len Number of bytes to include in the calculation.
22 * @return Computed CRC-8 byte.
23 */
24uint8_t calculateCrc(uint8_t* data, uint8_t len);
25
26//////////////////////////////////////////////////////////////////////////////////
27/////////// sleeper_t ////////////////////////////////////////////////////////////
28
29/**
30 * @brief Non-blocking timer utility – similar to HAL_Delay but without CPU blocking.
31 *
32 * The caller polls sleeper_IsElapsed() or sleeper_IsElapsedNext() inside its
33 * main loop to detect when the configured time interval has elapsed.
34 * The timer can also be stopped; in that case all _IsElapsedXX functions
35 * return 0 regardless of elapsed time.
36 */
37#pragma pack(1)
38typedef struct
39{
40 uint32_t SleepMS; /**< Duration (ms) to wait; 0 means no pause */
41 uint32_t InicTime; /**< Reference timestamp captured at (re)start, used for comparison */
42 uint8_t Stop; /**< 1 – timer is stopped (all IsElapsed checks return 0); 0 – running */
43} sleeper_t;
44#pragma pack()
45
46/**
47 * @brief Initialise the sleeper and start timing from now.
48 * @param v Pointer to the sleeper_t instance to initialise.
49 * @param sleepMS Duration in milliseconds after which IsElapsed returns 1.
50 * Pass 0 to create a stopped / instant timer.
51 */
52void sleeper_Init(sleeper_t *v, uint32_t sleepMS);
53
54/**
55 * @brief Check whether the configured time interval has elapsed.
56 * @param v Pointer to a const sleeper_t instance.
57 * @retval 1 The time has elapsed (or SleepMS == 0 and Stop == 0).
58 * @retval 0 The time has not yet elapsed, or the timer is stopped.
59 */
60int sleeper_IsElapsed(const sleeper_t *v);
61
62/**
63 * @brief Check whether the time has elapsed and, if so, restart the timer for
64 * the next period (InicTime advances by SleepMS to avoid drift).
65 * @param v Pointer to the sleeper_t instance.
66 * @retval 1 The time has elapsed; timer restarted for the next cycle.
67 * @retval 0 The time has not yet elapsed, or the timer is stopped.
68 */
70
71/**
72 * @brief Restart the timer so the next period begins from now.
73 * InicTime is updated to the current HAL tick.
74 * @param v Pointer to the sleeper_t instance.
75 */
76void sleeper_Next(sleeper_t *v);
77
78/**
79 * @brief Change the sleep duration and restart the timer from now.
80 * @param v Pointer to the sleeper_t instance.
81 * @param sleepMS New duration in milliseconds.
82 */
83void sleeper_SetSleepMS(sleeper_t *v, uint32_t sleepMS);
84
85/**
86 * @brief Check whether the time has elapsed and, if so, stop the timer.
87 * @note Call sleeper_Next() to resume timing after this function returns 1.
88 * @param v Pointer to the sleeper_t instance.
89 * @retval 1 The time has elapsed; timer is now stopped.
90 * @retval 0 The time has not yet elapsed, or the timer was already stopped.
91 */
93
94/**
95 * @brief Stop the timer so that all subsequent IsElapsed checks return 0.
96 * @note Call sleeper_Next() or sleeper_Init() to resume timing.
97 * @param v Pointer to the sleeper_t instance.
98 */
99void sleeper_Stop(sleeper_t *v);
100
101//////////////////////////////////////////////////////////////////////////////////
102
103/**
104 * @brief Alias for the value type used by valueChanger_t (default: uint8_t).
105 */
106typedef uint8_t TVAL;
107
108/**
109 * @brief Value-stability detector – determines when a periodically updated
110 * value has remained unchanged for a configured time window.
111 *
112 * Call valueChanger_SetValue() each time a new sample arrives.
113 * The function returns 1 only when the value has been stable (unchanged)
114 * for the full duration specified at initialisation.
115 *
116 * When IsLocked is set, valueChanger_SetValue() always returns 0 until a
117 * different value is provided, at which point the lock is automatically cleared.
118 */
119typedef struct
120{
121 TVAL LastValue; /**< Most recently accepted value */
122 sleeper_t Timer; /**< Stability timer – restarted whenever the value changes */
123 uint8_t IsLocked; /**< 1 – locked (SetValue always returns 0); 0 – normal */
125
126/**
127 * @brief Initialise a valueChanger_t instance.
128 * @param v Pointer to the valueChanger_t to initialise.
129 * @param inicValue Initial value stored in LastValue.
130 * @param timeMS Stability window in milliseconds; SetValue returns 1 only
131 * after the value has been unchanged for this duration.
132 */
133void valueChanger_Inic(valueChanger_t *v, TVAL inicValue, uint32_t timeMS);
134
135/**
136 * @brief Submit a new value sample and check for stability.
137 *
138 * If newValue differs from LastValue, LastValue is updated, the stability
139 * timer is restarted, and the instance is unlocked.
140 * If newValue equals LastValue and the stability timer has elapsed, 1 is
141 * returned to signal that the value has been stable for the full window.
142 *
143 * @param v Pointer to the valueChanger_t instance.
144 * @param newValue The latest sampled value.
145 * @retval 1 The value has remained unchanged for the full stability window.
146 * @retval 0 The value changed, the timer has not elapsed, or the instance is locked.
147 */
149
150/**
151 * @brief Lock the valueChanger_t so that SetValue always returns 0.
152 * The lock is cleared automatically the next time SetValue receives
153 * a value different from LastValue.
154 * @param v Pointer to the valueChanger_t instance.
155 */
157
158/**
159 * @brief Return the most recently stored value.
160 * @param v Pointer to a const valueChanger_t instance.
161 * @return Current value held in LastValue.
162 */
164
165/////////////////////////////////////////////////////////////
166
167/**
168 * @brief Erase the external flash memory signature so that the next boot
169 * forces a full re-initialisation (used when flashing a new firmware
170 * version that changes persistent data structures).
171 */
172void clearFlash();
173
174/**
175 * @brief the calling of system restart with disabling of interrupts
176 */
177void systemRestart();
178
179/////////////////////////////////////////////////////////////
180
181#endif /* UTILS_UTILS_H_ */
Non-blocking timer utility – similar to HAL_Delay but without CPU blocking.
Definition utils.h:39
uint8_t Stop
Definition utils.h:42
uint32_t SleepMS
Definition utils.h:40
uint32_t InicTime
Definition utils.h:41
Value-stability detector – determines when a periodically updated value has remained unchanged for a ...
Definition utils.h:120
sleeper_t Timer
Definition utils.h:122
uint8_t IsLocked
Definition utils.h:123
TVAL LastValue
Definition utils.h:121
void valueChanger_Lock(valueChanger_t *v)
Lock the valueChanger_t so that SetValue always returns 0. The lock is cleared automatically the next...
Definition utils.c:102
void sleeper_SetSleepMS(sleeper_t *v, uint32_t sleepMS)
Change the sleep duration and restart the timer from now.
Definition utils.c:61
uint8_t calculateCrc(uint8_t *data, uint8_t len)
Calculate a CRC-8 checksum (Sensirion polynomial) over a byte buffer. Used to validate sensor I2C res...
Definition utils.c:13
void systemRestart()
the calling of system restart with disabling of interrupts
Definition utils.c:135
int sleeper_IsElapsedNext(sleeper_t *v)
Check whether the time has elapsed and, if so, restart the timer for the next period (InicTime advanc...
Definition utils.c:46
void sleeper_Init(sleeper_t *v, uint32_t sleepMS)
Initialise the sleeper and start timing from now.
Definition utils.c:33
void clearFlash()
Erase the external flash memory signature so that the next boot forces a full re-initialisation (used...
Definition utils.c:114
int sleeper_IsElapsedStop(sleeper_t *v)
Check whether the time has elapsed and, if so, stop the timer.
Definition utils.c:67
uint8_t TVAL
Alias for the value type used by valueChanger_t (default: uint8_t).
Definition utils.h:106
void sleeper_Stop(sleeper_t *v)
Stop the timer so that all subsequent IsElapsed checks return 0.
Definition utils.c:76
void sleeper_Next(sleeper_t *v)
Restart the timer so the next period begins from now. InicTime is updated to the current HAL tick.
Definition utils.c:55
void valueChanger_Inic(valueChanger_t *v, TVAL inicValue, uint32_t timeMS)
Initialise a valueChanger_t instance.
Definition utils.c:84
TVAL valueChanger_GetValue(const valueChanger_t *v)
Return the most recently stored value.
Definition utils.c:107
int valueChanger_SetValue(valueChanger_t *v, TVAL newValue)
Submit a new value sample and check for stability.
Definition utils.c:91
int sleeper_IsElapsed(const sleeper_t *v)
Check whether the configured time interval has elapsed.
Definition utils.c:41