L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
sys_app.c
Go to the documentation of this file.
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file sys_app.c
5 * @author MCD Application Team
6 * @brief Initializes HW and SW system entities (not related to the radio)
7 ******************************************************************************
8 * @attention
9 *
10 * Copyright (c) 2026 STMicroelectronics.
11 * All rights reserved.
12 *
13 * This software is licensed under terms that can be found in the LICENSE file
14 * in the root directory of this software component.
15 * If no LICENSE file comes with this software, it is provided AS-IS.
16 *
17 ******************************************************************************
18 */
19/* USER CODE END Header */
20
21/* Includes ------------------------------------------------------------------*/
22#include <stdio.h>
23#include "platform.h"
24#include "sys_app.h"
25#include "adc_if.h"
26#include "stm32_seq.h"
27#include "stm32_systime.h"
28#include "stm32_lpm.h"
29#include "timer_if.h"
30#include "utilities_def.h"
31#include "sys_debug.h"
32#include "sys_sensors.h"
33
34/* USER CODE BEGIN Includes */
35
36/* USER CODE END Includes */
37
38/* External variables ---------------------------------------------------------*/
39/* USER CODE BEGIN EV */
40
41/* USER CODE END EV */
42
43/* Private typedef -----------------------------------------------------------*/
44/* USER CODE BEGIN PTD */
45
46/* USER CODE END PTD */
47
48/* Private define ------------------------------------------------------------*/
49#define MAX_TS_SIZE (int) 16
50
51/**
52 * Defines the maximum battery level
53 */
54#define LORAWAN_MAX_BAT 254
55
56/* USER CODE BEGIN PD */
57
58/* USER CODE END PD */
59
60/* Private macro -------------------------------------------------------------*/
61/* USER CODE BEGIN PM */
62
63/* USER CODE END PM */
64
65/* Private variables ---------------------------------------------------------*/
66static uint8_t SYS_TimerInitialisedFlag = 0;
67
68/* USER CODE BEGIN PV */
69
70/* USER CODE END PV */
71
72/* Private function prototypes -----------------------------------------------*/
73/**
74 * @brief Returns sec and msec based on the systime in use
75 * @param buff to update with timestamp
76 * @param size of updated buffer
77 */
78static void TimestampNow(uint8_t *buff, uint16_t *size);
79
80/**
81 * @brief it calls UTIL_ADV_TRACE_VSNPRINTF
82 */
83static void tiny_snprintf_like(char *buf, uint32_t maxsize, const char *strFormat, ...);
84
85/* USER CODE BEGIN PFP */
86
87/* USER CODE END PFP */
88
89/* Exported functions ---------------------------------------------------------*/
91{
92 /* USER CODE BEGIN SystemApp_Init_1 */
93
94 /* USER CODE END SystemApp_Init_1 */
95
96 /* Ensure that MSI is wake-up system clock */
97 __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_MSI);
98
99 /*Initialize timer and RTC*/
100 UTIL_TIMER_Init();
102 /* Initializes the SW probes pins and the monitor RF pins via Alternate Function */
103 DBG_Init();
104
105 /*Initialize the terminal */
106 UTIL_ADV_TRACE_Init();
107 UTIL_ADV_TRACE_RegisterTimeStampFunction(TimestampNow);
108
109 /* #warning "should be removed when proper obl is done" */
110 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
111
112 /*Set verbose LEVEL*/
113 UTIL_ADV_TRACE_SetVerboseLevel(VERBOSE_LEVEL);
114
115 /*Initialize the temperature and Battery measurement services */
117
118 /*Initialize the Sensors */
120
121 /*Init low power manager*/
122 UTIL_LPM_Init();
123 /* Disable Stand-by mode */
124 UTIL_LPM_SetOffMode((1 << CFG_LPM_APPLI_Id), UTIL_LPM_DISABLE);
125
126#if defined (LOW_POWER_DISABLE) && (LOW_POWER_DISABLE == 1)
127 /* Disable Stop Mode */
128 UTIL_LPM_SetStopMode((1 << CFG_LPM_APPLI_Id), UTIL_LPM_DISABLE);
129#elif !defined (LOW_POWER_DISABLE)
130#error LOW_POWER_DISABLE not defined
131#endif /* LOW_POWER_DISABLE */
132
133 /* USER CODE BEGIN SystemApp_Init_2 */
134
135 /* USER CODE END SystemApp_Init_2 */
136}
137
138/**
139 * @brief redefines __weak function in stm32_seq.c such to enter low power
140 */
142{
143 /* USER CODE BEGIN UTIL_SEQ_Idle_1 */
144//return; //kokot
145 /* USER CODE END UTIL_SEQ_Idle_1 */
146 UTIL_LPM_EnterLowPower();
147 /* USER CODE BEGIN UTIL_SEQ_Idle_2 */
148
149 /* USER CODE END UTIL_SEQ_Idle_2 */
150}
151
152uint8_t GetBatteryLevel(void)
153{
154 uint8_t batteryLevel = 0;
155 uint16_t batteryLevelmV;
156
157 /* USER CODE BEGIN GetBatteryLevel_0 */
158
159 /* USER CODE END GetBatteryLevel_0 */
160
161 batteryLevelmV = (uint16_t) SYS_GetBatteryLevel();
162
163 /* Convert battery level from mV to linear scale: 1 (very low) to 254 (fully charged) */
164 if (batteryLevelmV > VDD_BAT)
165 {
166 batteryLevel = LORAWAN_MAX_BAT;
167 }
168 else if (batteryLevelmV < VDD_MIN)
169 {
170 batteryLevel = 0;
171 }
172 else
173 {
174 batteryLevel = (((uint32_t)(batteryLevelmV - VDD_MIN) * LORAWAN_MAX_BAT) / (VDD_BAT - VDD_MIN));
175 }
176
177 /* USER CODE BEGIN GetBatteryLevel_2 */
178
179 /* USER CODE END GetBatteryLevel_2 */
180
181 return batteryLevel; /* 1 (very low) to 254 (fully charged) */
182}
183
185{
186 int16_t temperatureLevel = 0;
187
188 sensor_t sensor_data;
189
190 EnvSensors_Read(&sensor_data);
191 temperatureLevel = (int16_t)(sensor_data.temperature);
192 /* USER CODE BEGIN GetTemperatureLevel */
193
194 /* USER CODE END GetTemperatureLevel */
195 return temperatureLevel;
196}
197
198void GetUniqueId(uint8_t *id)
199{
200 /* USER CODE BEGIN GetUniqueId_1 */
201//memcpy(id, getAppDevEUI(), 8);
202//return;
203 /* USER CODE END GetUniqueId_1 */
204 uint32_t val = 0;
205 val = LL_FLASH_GetUDN();
206 if (val == 0xFFFFFFFF) /* Normally this should not happen */
207 {
208 uint32_t ID_1_3_val = HAL_GetUIDw0() + HAL_GetUIDw2();
209 uint32_t ID_2_val = HAL_GetUIDw1();
210
211 id[7] = (ID_1_3_val) >> 24;
212 id[6] = (ID_1_3_val) >> 16;
213 id[5] = (ID_1_3_val) >> 8;
214 id[4] = (ID_1_3_val);
215 id[3] = (ID_2_val) >> 24;
216 id[2] = (ID_2_val) >> 16;
217 id[1] = (ID_2_val) >> 8;
218 id[0] = (ID_2_val);
219 }
220 else /* Typical use case */
221 {
222 id[7] = val & 0xFF;
223 id[6] = (val >> 8) & 0xFF;
224 id[5] = (val >> 16) & 0xFF;
225 id[4] = (val >> 24) & 0xFF;
226 val = LL_FLASH_GetDeviceID();
227 id[3] = val & 0xFF;
228 val = LL_FLASH_GetSTCompanyID();
229 id[2] = val & 0xFF;
230 id[1] = (val >> 8) & 0xFF;
231 id[0] = (val >> 16) & 0xFF;
232 }
233
234 /* USER CODE BEGIN GetUniqueId_2 */
235
236 /* USER CODE END GetUniqueId_2 */
237}
238
239void GetDevAddr(uint32_t *devAddr)
240{
241 /* USER CODE BEGIN GetDevAddr_1 */
242
243 /* USER CODE END GetDevAddr_1 */
244
245 *devAddr = LL_FLASH_GetUDN();
246 if (*devAddr == 0xFFFFFFFF)
247 {
248 *devAddr = ((HAL_GetUIDw0()) ^ (HAL_GetUIDw1()) ^ (HAL_GetUIDw2()));
249 }
250
251 /* USER CODE BEGIN GetDevAddr_2 */
252
253 /* USER CODE END GetDevAddr_2 */
254}
255
256/* USER CODE BEGIN EF */
257
258/* USER CODE END EF */
259
260/* Private functions ---------------------------------------------------------*/
261
262static void TimestampNow(uint8_t *buff, uint16_t *size)
263{
264 /* USER CODE BEGIN TimestampNow_1 */
265
266 /* USER CODE END TimestampNow_1 */
267 SysTime_t curtime = SysTimeGet();
268 tiny_snprintf_like((char *)buff, MAX_TS_SIZE, "%ds%03d:", curtime.Seconds, curtime.SubSeconds);
269 *size = strlen((char *)buff);
270 /* USER CODE BEGIN TimestampNow_2 */
271
272 /* USER CODE END TimestampNow_2 */
273}
274
275/* Disable StopMode when traces need to be printed */
277{
278 /* USER CODE BEGIN UTIL_ADV_TRACE_PreSendHook_1 */
279 return; // MT 4.2.206 - toto nie, ja riadim
280 /* USER CODE END UTIL_ADV_TRACE_PreSendHook_1 */
281 UTIL_LPM_SetStopMode((1 << CFG_LPM_UART_TX_Id), UTIL_LPM_DISABLE);
282 /* USER CODE BEGIN UTIL_ADV_TRACE_PreSendHook_2 */
283
284 /* USER CODE END UTIL_ADV_TRACE_PreSendHook_2 */
285}
286/* Re-enable StopMode when traces have been printed */
288{
289 /* USER CODE BEGIN UTIL_LPM_SetStopMode_1 */
290 return; // MT 4.2.206 - toto nie, ja riadim
291
292 /* USER CODE END UTIL_LPM_SetStopMode_1 */
293 UTIL_LPM_SetStopMode((1 << CFG_LPM_UART_TX_Id), UTIL_LPM_ENABLE);
294 /* USER CODE BEGIN UTIL_LPM_SetStopMode_2 */
295
296 /* USER CODE END UTIL_LPM_SetStopMode_2 */
297}
298
299static void tiny_snprintf_like(char *buf, uint32_t maxsize, const char *strFormat, ...)
300{
301 /* USER CODE BEGIN tiny_snprintf_like_1 */
302
303 /* USER CODE END tiny_snprintf_like_1 */
304 va_list vaArgs;
305 va_start(vaArgs, strFormat);
306 UTIL_ADV_TRACE_VSNPRINTF(buf, maxsize, strFormat, vaArgs);
307 va_end(vaArgs);
308 /* USER CODE BEGIN tiny_snprintf_like_2 */
309
310 /* USER CODE END tiny_snprintf_like_2 */
311}
312
313/* USER CODE BEGIN PrFD */
314
315/* USER CODE END PrFD */
316
317/* HAL overload functions ---------------------------------------------------------*/
318
319/**
320 * @note This function overwrites the __weak one from HAL
321 */
322HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
323{
324 /*Don't enable SysTick if TIMER_IF is based on other counters (e.g. RTC) */
325 /* USER CODE BEGIN HAL_InitTick_1 */
326
327 /* USER CODE END HAL_InitTick_1 */
328 return HAL_OK;
329 /* USER CODE BEGIN HAL_InitTick_2 */
330
331 /* USER CODE END HAL_InitTick_2 */
332}
333
334/**
335 * @note This function overwrites the __weak one from HAL
336 */
337uint32_t HAL_GetTick(void)
338{
339 uint32_t ret = 0;
340 /* TIMER_IF can be based on other counter the SysTick e.g. RTC */
341 /* USER CODE BEGIN HAL_GetTick_1 */
342
343 /* USER CODE END HAL_GetTick_1 */
345 {
346 /* TIMER_IF_GetTimerValue should be used only once UTIL_TIMER_Init() is initialized */
347 /* If HAL_Delay or a TIMEOUT countdown is necessary during initialization phase */
348 /* please use temporarily another timebase source (SysTick or TIMx), which implies also */
349 /* to rework the above function HAL_InitTick() and to call HAL_IncTick() on the timebase IRQ */
350 /* Note: when TIMER_IF is based on RTC, stm32wlxx_hal_rtc.c calls this function before TimeServer is functional */
351 /* RTC TIMEOUT will not expire, i.e. if RTC has an hw problem it will keep looping in the RTC_Init function */
352 /* USER CODE BEGIN HAL_GetTick_EarlyCall */
353
354 /* USER CODE END HAL_GetTick_EarlyCall */
355 }
356 else
357 {
359 }
360 /* USER CODE BEGIN HAL_GetTick_2 */
361
362 /* USER CODE END HAL_GetTick_2 */
363 return ret;
364}
365
366/**
367 * @note This function overwrites the __weak one from HAL
368 */
369void HAL_Delay(__IO uint32_t Delay)
370{
371 /* TIMER_IF can be based on other counter the SysTick e.g. RTC */
372 /* USER CODE BEGIN HAL_Delay_1 */
373
374 /* USER CODE END HAL_Delay_1 */
375 TIMER_IF_DelayMs(Delay);
376 /* USER CODE BEGIN HAL_Delay_2 */
377
378 /* USER CODE END HAL_Delay_2 */
379}
380
381/* USER CODE BEGIN Overload_HAL_weaks */
382
383/* USER CODE END Overload_HAL_weaks */
Header for ADC interface configuration.
uint16_t SYS_GetBatteryLevel(void)
Get the current battery level.
Definition adc_if.c:139
void SYS_InitMeasurement(void)
Initializes the ADC input.
Definition adc_if.c:79
#define VDD_MIN
Minimum battery level in mV.
Definition adc_if.h:53
#define VDD_BAT
Maximum battery level in mV.
Definition adc_if.h:49
Header for General HW instances configuration.
float temperature
Definition sys_sensors.h:41
#define MAX_TS_SIZE
Definition sys_app.c:49
static void TimestampNow(uint8_t *buff, uint16_t *size)
Returns sec and msec based on the systime in use.
Definition sys_app.c:262
void SystemApp_Init(void)
initialize the system (dbg pins, trace, mbmux, sys timer, LPM, ...)
Definition sys_app.c:90
void UTIL_SEQ_Idle(void)
redefines __weak function in stm32_seq.c such to enter low power
Definition sys_app.c:141
void UTIL_ADV_TRACE_PreSendHook(void)
Definition sys_app.c:276
void GetDevAddr(uint32_t *devAddr)
callback to get the board 32 bits unique ID (LSB)
Definition sys_app.c:239
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
Definition sys_app.c:322
int16_t GetTemperatureLevel(void)
callback to get the current temperature in the MCU
Definition sys_app.c:184
static void tiny_snprintf_like(char *buf, uint32_t maxsize, const char *strFormat,...)
it calls UTIL_ADV_TRACE_VSNPRINTF
Definition sys_app.c:299
void GetUniqueId(uint8_t *id)
callback to get the board 64 bits unique ID
Definition sys_app.c:198
#define LORAWAN_MAX_BAT
Definition sys_app.c:54
void HAL_Delay(__IO uint32_t Delay)
Definition sys_app.c:369
uint8_t GetBatteryLevel(void)
callback to get the battery level in % of full charge (254 full charge, 0 no charge)
Definition sys_app.c:152
static uint8_t SYS_TimerInitialisedFlag
Definition sys_app.c:66
uint32_t HAL_GetTick(void)
Definition sys_app.c:337
void UTIL_ADV_TRACE_PostSendHook(void)
Definition sys_app.c:287
Function prototypes for sys_app.c file.
#define VERBOSE_LEVEL
Verbose level for all trace logs.
Definition sys_conf.h:50
Configuration of the debug.c instances.
void DBG_Init(void)
Initializes the SW probes pins and the monitor RF pins via Alternate Function.
Definition sys_debug.c:64
Header for sensors application.
int32_t EnvSensors_Read(sensor_t *sensor_data)
Environmental sensor read.
int32_t EnvSensors_Init(void)
initialize the environmental sensor
configuration of the timer_if.c instances
void TIMER_IF_DelayMs(uint32_t delay)
a delay of delay ms by polling RTC
Definition timer_if.c:359
uint32_t TIMER_IF_GetTimerValue(void)
Get the timer value.
Definition timer_if.c:304
#define UTIL_ADV_TRACE_VSNPRINTF(...)
Definitions for modules requiring utilities.
@ CFG_LPM_APPLI_Id
@ CFG_LPM_UART_TX_Id