L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
utils.c
Go to the documentation of this file.
1/*
2 * utils.c
3 *
4 * Created on: May 5, 2024
5 * Author: Milan
6 */
7
8#include "utils.h"
9#include "main.h"
10
11
12// Internal Helper: CRC-8 Calculation
13uint8_t calculateCrc(uint8_t *data, uint8_t len)
14{
15 uint8_t crc = 0xFF;
16 for (uint8_t i = 0; i < len; i++)
17 {
18 crc ^= data[i];
19 for (uint8_t bit = 8; bit > 0; --bit)
20 {
21 if (crc & 0x80)
22 crc = (crc << 1) ^ 0x31;
23 else
24 crc = (crc << 1);
25 }
26 }
27 return crc;
28}
29
30
31////////////////////////////////////////////////////////////////
32// Sleeper /////////////////////////////////////////////////////
33void sleeper_Init(sleeper_t *v, uint32_t sleepMS) //
34{
35 v->InicTime = 0;
36 v->SleepMS = 0;
37 v->Stop = 0;
38 sleeper_SetSleepMS(v, sleepMS);
39}
40
42{
43 return !v->Stop && (v->SleepMS == 0 || HAL_GetTick() - v->InicTime > v->SleepMS);
44}
45
47{
48 int isTime = sleeper_IsElapsed(v);
49
50 if (isTime)
51 sleeper_Next(v);
52 return isTime;
53}
54
56{
57 v->InicTime = HAL_GetTick();
58 v->Stop = 0;
59}
60
61void sleeper_SetSleepMS(sleeper_t *v, uint32_t sleepMS) //
62{
63 sleeper_Next(v);
64 v->SleepMS = sleepMS;
65}
66
68{
69 int isTime = sleeper_IsElapsed(v);
70
71 if (isTime)
72 sleeper_Stop(v);
73 return isTime;
74}
75
77{
78 v->Stop = 1;
79}
80////////////////////////////////////////////////////////////////
81
82////////////////////////////////////////////////////////////////
83// valueChanger_Inic /////////////////////////////////////////////////////
84void valueChanger_Inic(valueChanger_t *v, TVAL inicValue, uint32_t timeMS) //
85{
86 v->LastValue = inicValue;
87 sleeper_Init(&v->Timer, timeMS);
88 v->IsLocked = 0;
89}
90
92{
93 if (newValue != v->LastValue) // the values are different
94 {
95 v->LastValue = newValue;
96 v->IsLocked = 0;
98 }
99 return !v->IsLocked && sleeper_IsElapsed(&v->Timer);
100}
101
103{
104 v->IsLocked = 1;
105}
106
108{
109 return v->LastValue;
110}
111
112
113/////////////////////////////////////////////////////////////////////
114void clearFlash() //
115{
116 /*
117 HAL_FLASH_Unlock();
118
119 FLASH_EraseInitTypeDef erase;
120 erase.NbPages = 1;
121 erase.TypeErase = FLASH_TYPEERASE_MASSERASE;
122 erase.PageAddress = FLASH_BASE;
123
124 uint32_t err;
125 HAL_FLASHEx_Erase(&erase, &err);
126
127 HAL_FLASH_Lock();
128
129 NVIC_SystemReset();
130 */
131}
132
133/////////////////////////////////////////////////////////////////////
134
136{
137 __set_FAULTMASK(1);
138 NVIC_SystemReset();
139}
: Header for main.c file. This file contains the common defines of the application....
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
uint32_t HAL_GetTick(void)
Definition sys_app.c:337
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
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
uint8_t TVAL
Alias for the value type used by valueChanger_t (default: uint8_t).
Definition utils.h:106