L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
mymems.h
Go to the documentation of this file.
1/*
2 * mymems.h
3 *
4 * Processing of flash & nfc
5 * Created on: 22. 1. 2026
6 * Author: Milan
7 */
8
9#ifndef INC_MYMEMS_H_
10#define INC_MYMEMS_H_
11
12#include "stm32wlxx_hal.h"
13#include "mysensors_base.h"
14
15/**
16 * @brief Size in bytes of the mems_MainBlock_t structure written at flash address 0.
17 * @warning This value is critical – changing it without resetting the flash will
18 * cause misaligned reads and data corruption.
19 */
20#define SZ_MAINBLOCK 64
21
22/**
23 * @brief Size in bytes of one mems_DataBlock_t entry in the circular data queue.
24 * Equals 5 bytes of header plus SENSORS_DATASIZE bytes of payload.
25 * @warning This value is critical – any change in the set of active sensors changes
26 * SENSORS_DATASIZE and therefore SZ_DATABLOCK, requiring a memory reset.
27 */
28#define SZ_DATABLOCK (5+SENSORS_DATASIZE)
29
30#pragma pack(1)
31/**
32 * @brief Master control block stored at flash address 0.
33 *
34 * Contains the application signature, per-sensor start counters, and all
35 * metadata required to manage the circular sensor-data queue in external flash.
36 * The structure is exactly SZ_MAINBLOCK (64) bytes.
37 *
38 * @note The total size MUST remain 64 bytes. Adjust Dummy0/Dummy1 if fields
39 * are added or removed to maintain alignment.
40 */
41typedef struct
42{
43 char Sign[8]; /**< 8-byte signature stamp; checked on boot to validate flash contents */
44 uint32_t Sens_SPS30Start :24; /**< Cumulative number of SPS30 fan-start events (used to schedule auto-cleaning) */
45 uint32_t Sens_SCD41Start :24; /**< Cumulative number of SCD41 measurement starts */
46 uint32_t Sens_BarometerStart :24;/**< Cumulative number of barometer (ILPS22QS / BMP585) measurement starts */
47 uint32_t Sens_TempHumStart :24; /**< Cumulative number of SHT45 measurement starts */
48 uint32_t Sens_AmbientStart :24; /**< Cumulative number of TSL2591 measurement starts */
49 uint8_t Dummy0[10]; /**< Reserved for future use */
50 /* Circular queue metadata (added MT 23.2.2026) */
51 uint32_t Data_AddrBase : 24; /**< Flash address of the first mems_DataBlock_t element in the queue */
52 uint32_t Data_Offset : 24; /**< Byte offset from Data_AddrBase pointing to the next write slot */
53 uint8_t Data_ItemSize; /**< Size of one queue element in bytes (== SZ_DATABLOCK) */
54 uint16_t Data_CountMax; /**< Maximum number of records the queue can hold */
55 uint16_t Data_CountCurrent; /**< Number of valid records currently stored in the queue */
56 uint8_t Dummy1[20]; /**< Reserved for future use */
58
59/**
60 * @brief Type tag stored in each mems_DataBlock_t to identify the payload format.
61 * Used when reading back blocks from the circular queue so the caller
62 * knows how to interpret the Data[] field.
63 */
64typedef enum
65{
66 MEMS_DATATYPE_NON, /**< Unspecified / uninitialised block – should not appear in valid data */
67 MEMS_DATATYPE_SENSOR /**< Payload contains a packed sensor data record (all active sensors) */
69/**
70 * @brief Sensor data record stored as one element in the flash circular queue.
71 *
72 * New sensor readings are appended at the tail; when the queue is full the
73 * oldest records are silently overwritten (circular / ring-buffer behaviour).
74 * The total size of one record is fixed at SZ_DATABLOCK bytes; changing the
75 * set of active sensors changes this size and forces a memory reset.
76 */
77typedef struct
78{
79 char Sign[3]; /**< 3-byte signature; checked when reading back to verify block integrity */
80 mems_DataType_t Type; /**< Payload type identifier (see mems_DataType_t) */
81 uint8_t Size; /**< Number of valid bytes written into Data[] (≤ SENSORS_DATASIZE) */
82 uint8_t Data[SENSORS_DATASIZE]; /**< Packed sensor payload; layout depends on which sensors are enabled */
84
85#pragma pack()
86
87/** @brief Global instance of the main flash control block; loaded by mems_ReadMainBlock(). */
89
90/**
91 * @brief Initialise NFC and external flash; load the main block and system parameters.
92 * Verifies the flash signature and resets the queue if the block size has changed.
93 */
94void mems_Init();
95
96/**
97 * @brief - reading of main block from flash. If there is not valid MEMS_SIGN, the structure is initialize to 0
98 * The main block it stored on address 0
99 * @retval HAL_OK or error
100 */
101HAL_StatusTypeDef mems_ReadMainBlock();
102
103/**
104 * @brief writing the main block on flash. The main block is stored on address 0
105 * @retval HAL_OK or error
106 */
107HAL_StatusTypeDef mems_WriteMainBlock();
108
109/**
110 * @brief memory reset - remove sign from chip
111 * * @retval status
112 *
113 */
114HAL_StatusTypeDef mems_Reset();
115
116/**
117 * @brief Initialize the work buffer - _memsDataBlock
118 * @retval return pointer to buffer
119 */
121
122
123/**
124 * @brief write new data of sensor at the end of queue, if is no space, the most older data start to rewrites
125 * @param data - the buffer, NULL(other parameter are ignored, if used the main default work buffer - _memsDataBlock)
126 * @param type - type of data, optional
127 * @param size - buffer, must be <= to SENSORS_DATASIZE (_memsMainBlock.Sens_ItemSize)
128 * @retval HAL_OK or error
129 */
130HAL_StatusTypeDef mems_AddData(const void *data, mems_DataType_t type, uint8_t size);
131
132/**
133 * @brief Get the last sensor data if exists. The data and mem block is stored in work buffer, which can be modify by
134 * other mems_xxxx function, temporary valid only
135 * @param memBlock - pointer to mem block with data
136 * @param inxFromEnd - the index from end of memory, which is reading to buffer (0 - last, 1 - before last, etc)
137 * @retval HAL_OK or error, HAL_BUSY no more data
138 */
139HAL_StatusTypeDef mems_GetLastData(mems_DataBlock_t **memBlock, uint8_t inxFromEnd);
140
141/**
142 * @brief Check the memory consist
143 * @param correct - correct - means to reset all sensor data
144 * @retval HAL_OK - memory is OK, HAL_ERROR - error
145 */
146HAL_StatusTypeDef mems_Check(int8_t correct);
147
148/**
149 * @brief remove last data
150 * @param fromEndTo remove from last to index, 0 - last, 1 - last&before last, etc
151 */
152void mems_RemoveLastData(uint8_t fromEndTo);
153
154/**
155 * @brief write data from memory to systemParams
156 * @retval 0 - data same-no changes, number is count of different changes
157 */
159
160
161
162
163
164/**
165 * @brief Read a value from an RTC backup register
166 *
167 * This function reads a 32-bit value from one of the RTC backup registers.
168 * These registers are retained during system standby mode and after system reset
169 * (as long as VDD is maintained or VBAT is supplied).
170 *
171 * @param backupRegister Backup register index (0-19 for STM32WL).
172 * Should use RTC_BKP_DRx constants defined in stm32wlxx_hal_rtc_ex.h
173 * (e.g., RTC_BKP_DR0=0, RTC_BKP_DR1=1, ..., RTC_BKP_DR19=19)
174 * @return uint32_t The 32-bit value stored in the backup register
175 */
176uint32_t getBackUpRegister(uint32_t backupRegister);
177
178/**
179 * @brief Write a value to an RTC backup register
180 *
181 * This function writes a 32-bit value to one of the RTC backup registers.
182 * These registers are retained during system standby mode and after system reset
183 * (as long as VDD is maintained or VBAT is supplied).
184 *
185 * Use this function to save critical data before entering standby mode.
186 * On restart, read the values using getBackUpRegister() to check system state
187 * and restore necessary information.
188 *
189 * @param backupRegister Backup register index (0-19 for STM32WL).
190 * Should use RTC_BKP_DRx constants defined in stm32wlxx_hal_rtc_ex.h
191 * (e.g., RTC_BKP_DR0=0, RTC_BKP_DR1=1, ..., RTC_BKP_DR19=19)
192 * @param value The 32-bit value to store in the backup register
193 */
194void setBackUpRegister(uint32_t backupRegister, uint32_t value);
195
196#endif /* INC_MYMEMS_H_ */
void mems_RemoveLastData(uint8_t fromEndTo)
remove last data
Definition mymems.c:242
HAL_StatusTypeDef mems_GetLastData(mems_DataBlock_t **memBlock, uint8_t inxFromEnd)
Get the last sensor data if exists. The data and mem block is stored in work buffer,...
Definition mymems.c:194
uint8_t mems_WriteToSystemParams()
write data from memory to systemParams
Definition mymems.c:268
mems_MainBlock_t _memsMainBlock
Global instance of the main flash control block; loaded by mems_ReadMainBlock().
Definition mymems.c:27
void mems_Init()
Initialise NFC and external flash; load the main block and system parameters. Verifies the flash sign...
Definition mymems.c:52
HAL_StatusTypeDef mems_AddData(const void *data, mems_DataType_t type, uint8_t size)
write new data of sensor at the end of queue, if is no space, the most older data start to rewrites
Definition mymems.c:168
mems_DataBlock_t * mems_InicBuffer(mems_DataType_t type)
Initialize the work buffer - _memsDataBlock.
Definition mymems.c:160
HAL_StatusTypeDef mems_Reset()
memory reset - remove sign from chip
Definition mymems.c:125
void setBackUpRegister(uint32_t backupRegister, uint32_t value)
Write a value to an RTC backup register.
Definition mymems.c:358
mems_DataType_t
Type tag stored in each mems_DataBlock_t to identify the payload format. Used when reading back block...
Definition mymems.h:65
@ MEMS_DATATYPE_NON
Definition mymems.h:66
@ MEMS_DATATYPE_SENSOR
Definition mymems.h:67
HAL_StatusTypeDef mems_WriteMainBlock()
writing the main block on flash. The main block is stored on address 0
Definition mymems.c:117
uint32_t getBackUpRegister(uint32_t backupRegister)
Read a value from an RTC backup register.
Definition mymems.c:337
HAL_StatusTypeDef mems_Check(int8_t correct)
Check the memory consist.
Definition mymems.c:213
HAL_StatusTypeDef mems_ReadMainBlock()
reading of main block from flash. If there is not valid MEMS_SIGN, the structure is initialize to 0 T...
Definition mymems.c:93
#define SENSORS_DATASIZE
Total size in bytes of one packed sensor data record. Computed as the sum of all enabled sensor struc...
Sensor data record stored as one element in the flash circular queue.
Definition mymems.h:78
uint8_t Size
Definition mymems.h:81
char Sign[3]
Definition mymems.h:79
mems_DataType_t Type
Definition mymems.h:80
uint8_t Data[(sizeof(uint16_t)+sizeof(tmphm_sht45_t)+sizeof(amb_tsl2591_t)+sizeof(bar_ils22qs_t)+sizeof(bar_bmp585_t)+sizeof(scd41_t)+sizeof(sps30_t))]
Definition mymems.h:82
Master control block stored at flash address 0.
Definition mymems.h:42
uint8_t Dummy1[20]
Definition mymems.h:56
uint32_t Data_AddrBase
Definition mymems.h:51
uint32_t Sens_SCD41Start
Definition mymems.h:45
uint32_t Sens_SPS30Start
Definition mymems.h:44
uint16_t Data_CountCurrent
Definition mymems.h:55
uint32_t Sens_AmbientStart
Definition mymems.h:48
uint16_t Data_CountMax
Definition mymems.h:54
uint32_t Sens_TempHumStart
Definition mymems.h:47
uint8_t Dummy0[10]
Definition mymems.h:49
uint32_t Sens_BarometerStart
Definition mymems.h:46
uint8_t Data_ItemSize
Definition mymems.h:53
char Sign[8]
Definition mymems.h:43
uint32_t Data_Offset
Definition mymems.h:52