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 65
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 'MTK0001' */
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 uint32_t Bat_SendTimeSendUNIX; /**< the time, when was send the battery level to LoRa */
50
51 uint8_t Dummy0[6]; /**< Reserved for future use */
52 /* Circular queue metadata (added MT 23.2.2026) */
53 uint32_t Data_AddrBase : 24; /**< Flash address of the first mems_DataBlock_t element in the queue */
54 uint32_t Data_Offset : 24; /**< Byte offset from Data_AddrBase pointing to the next write slot */
55 uint8_t Data_ItemSize; /**< Size of one queue element in bytes (== SZ_DATABLOCK) */
56 uint16_t Data_CountMax; /**< Maximum number of records the queue can hold */
57 uint16_t Data_CountCurrent; /**< Number of valid records currently stored in the queue */
58 uint8_t Dummy1[20]; /**< Reserved for future use */
59 // ------ CRC covers all fields above; Crc itself is excluded from the calculation
60 uint8_t Crc;
62
63/**
64 * @brief Type tag stored in each mems_DataBlock_t to identify the payload format.
65 * Used when reading back blocks from the circular queue so the caller
66 * knows how to interpret the Data[] field.
67 */
68typedef enum
69{
70 MEMS_DATATYPE_NON, /**< Unspecified / uninitialised block – should not appear in valid data */
71 MEMS_DATATYPE_SENSOR /**< Payload contains a packed sensor data record (all active sensors) */
73/**
74 * @brief Sensor data record stored as one element in the flash circular queue.
75 *
76 * New sensor readings are appended at the tail; when the queue is full the
77 * oldest records are silently overwritten (circular / ring-buffer behaviour).
78 * The total size of one record is fixed at SZ_DATABLOCK bytes; changing the
79 * set of active sensors changes this size and forces a memory reset.
80 */
81typedef struct
82{
83 char Sign[3]; /**< 3-byte signature 'TM'; checked when reading back to verify block integrity */
84 mems_DataType_t Type; /**< Payload type identifier (see mems_DataType_t) */
85 uint8_t Size; /**< Number of valid bytes written into Data[] (≤ SENSORS_DATASIZE) */
86 uint8_t Data[SENSORS_DATASIZE]; /**< Packed sensor payload; layout depends on which sensors are enabled */
88
89#pragma pack()
90
91/** @brief Global instance of the main flash control block; loaded by mems_ReadMainBlock(). */
93
94/**
95 * @brief Initialise NFC and external flash; load the main block and system parameters.
96 * Verifies the flash signature and resets the queue if the block size has changed.
97 */
98void mems_Init();
99
100/**
101 * @brief - reading of main block from flash. If there is not valid MEMS_SIGN, the structure is initialize to 0
102 * The main block it stored on address 0
103 * @retval HAL_OK or error
104 */
105HAL_StatusTypeDef mems_ReadMainBlock();
106
107/**
108 * @brief writing the main block on flash. The main block is stored on address 0
109 * @retval HAL_OK or error
110 */
111HAL_StatusTypeDef mems_WriteMainBlock();
112
113/**
114 * @brief memory reset - remove sign from chip
115 * * @retval status
116 *
117 */
118HAL_StatusTypeDef mems_Reset();
119
120/**
121 * @brief Initialize the work buffer - _memsDataBlock
122 * @retval return pointer to buffer
123 */
125
126
127/**
128 * @brief write new data of sensor at the end of queue, if is no space, the most older data start to rewrites
129 * @param data - the buffer for save, NULL(other parameter are ignored, if used the main default work buffer - _memsDataBlock)
130 * @param type - type of data, optional
131 * @param size - buffer, must be <= to SENSORS_DATASIZE (_memsMainBlock.Sens_ItemSize)
132 * @retval HAL_OK or error
133 */
134HAL_StatusTypeDef mems_AddData(const void *data, mems_DataType_t type, uint8_t size);
135
136/**
137 * @brief Get the last sensor data if exists. The data and mem block is stored in work buffer, which can be modify by
138 * other mems_xxxx function, temporary valid only
139 * @param memBlock - pointer to mem block with data
140 * @param inxFromEnd - the index from end of memory, which is reading to buffer (0 - last, 1 - before last, etc)
141 * @retval HAL_OK or error, HAL_BUSY no more data
142 */
143HAL_StatusTypeDef mems_GetLastData(mems_DataBlock_t **memBlock, uint8_t inxFromEnd);
144
145/**
146 * @brief Check the memory consist
147 * @param correct - correct - means to reset all sensor data
148 * @retval HAL_OK - memory is OK, HAL_ERROR - error
149 */
150HAL_StatusTypeDef mems_Check(int8_t correct);
151
152/**
153 * @brief remove last data
154 * @param fromEndTo remove from last to index, 0 - last, 1 - last&before last, etc
155 */
156void mems_RemoveLastData(uint8_t fromEndTo);
157
158/**
159 * @brief write data from memory to systemParams
160 * @retval 0 - data same-no changes, number is count of different changes
161 */
163
164/**
165 * @brief read data from NFC to mems block
166 */
168
169
170
171
172
173/**
174 * @brief Read a value from an RTC backup register
175 *
176 * This function reads a 32-bit value from one of the RTC backup registers.
177 * These registers are retained during system standby mode and after system reset
178 * (as long as VDD is maintained or VBAT is supplied).
179 *
180 * @param backupRegister Backup register index (0-19 for STM32WL).
181 * Should use RTC_BKP_DRx constants defined in stm32wlxx_hal_rtc_ex.h
182 * (e.g., RTC_BKP_DR0=0, RTC_BKP_DR1=1, ..., RTC_BKP_DR19=19)
183 * @return uint32_t The 32-bit value stored in the backup register
184 */
185uint32_t getBackUpRegister(uint32_t backupRegister);
186
187/**
188 * @brief Write a value to an RTC backup register
189 *
190 * This function writes a 32-bit value to one of the RTC backup registers.
191 * These registers are retained during system standby mode and after system reset
192 * (as long as VDD is maintained or VBAT is supplied).
193 *
194 * Use this function to save critical data before entering standby mode.
195 * On restart, read the values using getBackUpRegister() to check system state
196 * and restore necessary information.
197 *
198 * @param backupRegister Backup register index (0-19 for STM32WL).
199 * Should use RTC_BKP_DRx constants defined in stm32wlxx_hal_rtc_ex.h
200 * (e.g., RTC_BKP_DR0=0, RTC_BKP_DR1=1, ..., RTC_BKP_DR19=19)
201 * @param value The 32-bit value to store in the backup register
202 */
203void setBackUpRegister(uint32_t backupRegister, uint32_t value);
204
205#endif /* INC_MYMEMS_H_ */
void mems_RemoveLastData(uint8_t fromEndTo)
remove last data
Definition mymems.c:269
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:221
uint8_t mems_WriteToSystemParams()
write data from memory to systemParams
Definition mymems.c:295
mems_MainBlock_t _memsMainBlock
Global instance of the main flash control block; loaded by mems_ReadMainBlock().
Definition mymems.c:29
void mems_ReadFromSystemParams()
read data from NFC to mems block
Definition mymems.c:331
void mems_Init()
Initialise NFC and external flash; load the main block and system parameters. Verifies the flash sign...
Definition mymems.c:66
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:195
mems_DataBlock_t * mems_InicBuffer(mems_DataType_t type)
Initialize the work buffer - _memsDataBlock.
Definition mymems.c:187
HAL_StatusTypeDef mems_Reset()
memory reset - remove sign from chip
Definition mymems.c:152
void setBackUpRegister(uint32_t backupRegister, uint32_t value)
Write a value to an RTC backup register.
Definition mymems.c:399
mems_DataType_t
Type tag stored in each mems_DataBlock_t to identify the payload format. Used when reading back block...
Definition mymems.h:69
@ MEMS_DATATYPE_NON
Definition mymems.h:70
@ MEMS_DATATYPE_SENSOR
Definition mymems.h:71
HAL_StatusTypeDef mems_WriteMainBlock()
writing the main block on flash. The main block is stored on address 0
Definition mymems.c:142
uint32_t getBackUpRegister(uint32_t backupRegister)
Read a value from an RTC backup register.
Definition mymems.c:376
HAL_StatusTypeDef mems_Check(int8_t correct)
Check the memory consist.
Definition mymems.c:240
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:119
#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:82
uint8_t Size
Definition mymems.h:85
char Sign[3]
Definition mymems.h:83
mems_DataType_t Type
Definition mymems.h:84
uint8_t Data[(sizeof(SysTime_t)+sizeof(tmphm_sht45_t)+0+0+sizeof(bar_bmp585_t)+0+0)]
Definition mymems.h:86
Master control block stored at flash address 0.
Definition mymems.h:42
uint8_t Dummy1[20]
Definition mymems.h:58
uint32_t Data_AddrBase
Definition mymems.h:53
uint8_t Dummy0[6]
Definition mymems.h:51
uint32_t Sens_SCD41Start
Definition mymems.h:45
uint32_t Sens_SPS30Start
Definition mymems.h:44
uint32_t Bat_SendTimeSendUNIX
Definition mymems.h:49
uint16_t Data_CountCurrent
Definition mymems.h:57
uint8_t Crc
Definition mymems.h:60
uint32_t Sens_AmbientStart
Definition mymems.h:48
uint16_t Data_CountMax
Definition mymems.h:56
uint32_t Sens_TempHumStart
Definition mymems.h:47
uint32_t Sens_BarometerStart
Definition mymems.h:46
uint8_t Data_ItemSize
Definition mymems.h:55
char Sign[8]
Definition mymems.h:43
uint32_t Data_Offset
Definition mymems.h:54