L14-Click 1.0
STM32WLE5CC LoRaWAN Sensor Platform
Loading...
Searching...
No Matches
stm32_lpm_if.c
Go to the documentation of this file.
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file stm32_lpm_if.c
5 * @author MCD Application Team
6 * @brief Low layer function to enter/exit low power modes (stop, sleep)
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 "platform.h"
23#include "stm32_lpm.h"
24#include "stm32_lpm_if.h"
25#include "usart_if.h"
26
27/* USER CODE BEGIN Includes */
28#include "spi.h"
29#include "rtc.h"
30#include "adc.h"
31#include "lora_app.h"
32#include "timer_if.h"
33#include "mymems.h"
34#include "radio.h"
35#include "mysensors_base.h"
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/**
45 * @brief Power driver callbacks handler
46 */
47const struct UTIL_LPM_Driver_s UTIL_PowerDriver =
48{
51
54
57};
58
59/* USER CODE BEGIN PTD */
60
61/* USER CODE END PTD */
62
63/* Private define ------------------------------------------------------------*/
64/* USER CODE BEGIN PD */
65
66/* USER CODE END PD */
67
68/* Private macro -------------------------------------------------------------*/
69/* USER CODE BEGIN PM */
70
71/* USER CODE END PM */
72
73/* Private variables ---------------------------------------------------------*/
74/* USER CODE BEGIN PV */
75
76/* USER CODE END PV */
77
78/* Private function prototypes -----------------------------------------------*/
79/* USER CODE BEGIN PFP */
80
81/* USER CODE END PFP */
82
83/* Exported functions --------------------------------------------------------*/
84
86{
87 /* USER CODE BEGIN EnterOffMode_1 */
88 // no, the LoRa radio has problem with this mode
89 /* Deinitialize/suspend peripherals as needed */
90 setBackUpRegister(RTC_BKP_STANDBY, RTC_BKP_STANDBY_SIGN); // indicator of restart after
91
92 Radio.Sleep();
93
94 //MX_I2C2_DeInit(); // if applicable
96 // Stop UART DMA/IT if used; optional: HAL_UART_DeInit(&huart1);
97 // ADC deinit
98 HAL_ADC_Stop_IT(&hadc);
99 HAL_ADC_DeInit(&hadc);
100
101 /* Clear all wake flags before entering Standby to avoid immediate wake-up */
102 __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); /* Clear WUF1/2/3 */
103 __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WRFBUSY); /* Clear radio busy wake flag, if set */
104
105 /* Disable wake-up pins unless you intentionally use them */
106// HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1);
107 HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1_LOW);
108 HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN2);
109 HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN3);
110
111
112 /* Deactivate any RTC Alarm and clear its flag (Alarm A) */
113 __HAL_RTC_ALARM_CLEAR_FLAG(&hrtc, RTC_FLAG_ALRAF);
114 HAL_RTC_DeactivateAlarm(&hrtc, RTC_ALARM_A);
115 HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
116
117
118 /* Optional: ensure radio busy IRQ does not wake us during Standby */
119 //HAL_PWREx_SetRadioBusyTrigger(PWR_RADIO_IRQ_TRIGGER_NONE); // if available
120 CLEAR_BIT(PWR->CR3, PWR_CR3_EWRFBUSY | PWR_CR3_EWRFIRQ);
121#if 1
122 GPIO_InitTypeDef GPIO_InitStruct =
123 { 0 };
124#if 0
125 GPIO_InitStruct.Pin = GPIO_PIN_12;
126 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
127 GPIO_InitStruct.Pull = GPIO_NOPULL;
128 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
129
130 while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_12) == GPIO_PIN_SET)
131 {
132 // Wait for radio to finish entering sleep mode
133 }
134#endif
135 /* 7. THE LEAKAGE KILLER: Set all GPIOs to ANALOG */
136 /* This includes your I2C pins, RF Switch pins, and UART pins */
137 // --- PORT A ---
138 // Keep PA0 (Radio Reset) and PA4 (Radio NSS) if necessary,
139 // // PA0 - NFC_INT - exclude
140 // though for STANDBY, the most important is to NOT trigger a pin reset.
141 GPIO_InitStruct.Pin = GPIO_PIN_All & ~(GPIO_PIN_0 | GPIO_PIN_4 | NFC_INT_Pin);
142 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
143
144 // --- PORT B ---
145 // PB8 is your RF Switch. Setting to Analog is fine,
146 // but ensure it's not needed for a specific wakeup.
147 // PB3/PB4 (Debug)
148 // PB12 (Internal Radio CS/Busy related)
149 // PB13/14/15 (Internal SPI)
150 // PB5-PB8 (Commonly used for RF Switch or Sensors)
151
152 // Try this instead of the "All" approach for Port B
153 GPIO_InitStruct.Pin = (
154 GPIO_PIN_0 |
155 GPIO_PIN_1 |
156 GPIO_PIN_2 |
157 GPIO_PIN_6 |
158 GPIO_PIN_8 |
159 GPIO_PIN_9 |
160 GPIO_PIN_10 |
161 GPIO_PIN_11 |
162 GPIO_PIN_12 |
163 GPIO_PIN_13 |
164 GPIO_PIN_14 |
165 GPIO_PIN_15 |
166 0 );// & ~(NFC_INT_Pin);
167
168 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
169 GPIO_InitStruct.Pull = GPIO_NOPULL;
170 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
171
172 // --- PORT C ---
173 // PC13 is your other RF Switch.
174 GPIO_InitStruct.Pin = GPIO_PIN_All & ~(GPIO_PIN_14 | GPIO_PIN_15); //LSE crystal
175 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
176#endif
177 // --- CRITICAL: Keep Radio NSS High ---
178 // This prevents the radio from waking up and causing a system crash
179 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
180
181 /* Enter Standby mode: after WFI, the device will restart from reset on wake */
182 /* Suspend SysTick */
183 HAL_SuspendTick();
184
185 /* Ensure the system knows to use the lowest power regulator state */
186 //HAL_PWR_DisablePVD();
187 // because of EXT0 and NFC_INT
188 __HAL_RCC_GPIOA_CLK_ENABLE();
189#ifndef DEBUG
190 // !!! this is important !!!
191 HAL_DBGMCU_DisableDBGStandbyMode();
192#endif
193 HAL_PWR_EnterSTANDBYMode();
194
195 /* USER CODE END EnterOffMode_1 */
196}
197
199{
200 /* USER CODE BEGIN ExitOffMode_1 */
201 //Error_Handler(); // never reach
202 NVIC_SystemReset();
203 /* USER CODE END ExitOffMode_1 */
204}
205
207{
208 /* USER CODE BEGIN EnterStopMode_1 */
209 // Deinitialize peripherals before entering stop mode
210 //MX_I2C2_DeInit(); // MT 10.1.2026 is in SensorOFF
211 writeLog("PWR_EnterStopMode");
212 sensorsBase_StartTimerToOff(); // start timer for check to go into off mode
213
215
216 /* USER CODE END EnterStopMode_1 */
217 HAL_SuspendTick();
218 /* Clear Status Flag before entering STOP/STANDBY Mode */
219 LL_PWR_ClearFlag_C1STOP_C1STB();
220
221 /* USER CODE BEGIN EnterStopMode_2 */
222 /*
223 * this has no mean, the consumption is still on 2mA
224#ifndef DEBUG
225 HAL_DBGMCU_DisableDBGStopMode();
226 HAL_DBGMCU_DisableDBGSleepMode();
227#endif
228*/
229 /* USER CODE END EnterStopMode_2 */
230 HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);
231 /* USER CODE BEGIN EnterStopMode_3 */
232
233 /* USER CODE END EnterStopMode_3 */
234}
235
237{
238 /* USER CODE BEGIN ExitStopMode_1 */
239//return;
240 /* USER CODE END ExitStopMode_1 */
241 /* Resume sysTick : work around for debugger problem in dual core */
242 HAL_ResumeTick();
243 /*Not retained periph:
244 ADC interface
245 DAC interface USARTx, TIMx, i2Cx, SPIx
246 SRAM ctrls, DMAx, DMAMux, AES, RNG, HSEM */
247
248 /* Resume not retained USARTx and DMA */
249 vcom_Resume();
250 /* USER CODE BEGIN ExitStopMode_2 */
251 // After wakeup from stop mode, reconfigure system clock
253 MX_SPI1_Init();
254 uart_Start();
255 writeLog("PWR_ExitStopMode");
256 sensorsBase_StopTimerToOff(); // stop timer for check to go into off mode
257 /* USER CODE END ExitStopMode_2 */
258}
259
261{
262 /* USER CODE BEGIN EnterSleepMode_1 */
263 writeLog("PWR_EnterSleepMode");
264
265 /* USER CODE END EnterSleepMode_1 */
266 /* Suspend sysTick */
267 HAL_SuspendTick();
268 /* USER CODE BEGIN EnterSleepMode_2 */
269
270 /* USER CODE END EnterSleepMode_2 */
271 HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
272 /* USER CODE BEGIN EnterSleepMode_3 */
273
274 /* USER CODE END EnterSleepMode_3 */
275}
276
278{
279 /* USER CODE BEGIN ExitSleepMode_1 */
280
281 /* USER CODE END ExitSleepMode_1 */
282 /* Resume sysTick */
283 HAL_ResumeTick();
284
285 /* USER CODE BEGIN ExitSleepMode_2 */
286 writeLog("PWR_ExitSleepMode");
287
288 /* USER CODE END ExitSleepMode_2 */
289}
290
291/* USER CODE BEGIN EF */
292
293/* USER CODE END EF */
294
295/* Private Functions Definition -----------------------------------------------*/
296/* USER CODE BEGIN PrFD */
297
298/* USER CODE END PrFD */
This file contains all the function prototypes for the adc.c file.
ADC_HandleTypeDef hadc
Definition adc.c:27
Header of application of the LRWAN Middleware.
void uart_Start()
start of UART reading
Definition main.c:142
void writeLog(const char *format,...)
Format and send a log message over UART (printf-style). Available only when WRITELOG is defined; comp...
Definition main.c:97
#define NFC_INT_Pin
Definition main.h:139
void SystemClock_Config(void)
Re-configure all system clocks after waking from STOP or OFF mode. Called automatically during the st...
Definition main.c:453
void setBackUpRegister(uint32_t backupRegister, uint32_t value)
Write a value to an RTC backup register.
Definition mymems.c:358
void sensorsBase_StartTimerToOff()
start timer to off the function is called from PWR_EnterStopMode - timer is started
void sensorsBase_StopTimerToOff()
stop timer to off the function is called from PWR_ExitStopMode - the timer value is checked and the P...
Header for General HW instances configuration.
This file contains all the function prototypes for the rtc.c file.
RTC_HandleTypeDef hrtc
Definition rtc.c:27
This file contains all the function prototypes for the spi.c file.
void MX_SPI1_DeInit(void)
Deinitialise SPI1 peripheral to reduce current consumption in low-power mode. Call before entering ST...
Definition spi.c:130
void MX_SPI1_Init(void)
Initialise SPI1 peripheral (hspi1) as configured by STM32CubeMX. Sets clock polarity/phase,...
Definition spi.c:30
void PWR_ExitStopMode(void)
Exits Low Power Stop Mode.
void PWR_EnterOffMode(void)
Accumulates the total time spent in STOP or SLEEP mode (in ms or ticks). Incremented each time PWR_Ex...
void PWR_EnterSleepMode(void)
Enters Low Power Sleep Mode.
void PWR_EnterStopMode(void)
Enters Low Power Stop Mode.
void PWR_ExitSleepMode(void)
Exits Low Power Sleep Mode.
const struct UTIL_LPM_Driver_s UTIL_PowerDriver
Power driver callbacks handler.
void PWR_ExitOffMode(void)
Exits Low Power Off Mode.
Header for Low Power Manager interface configuration.
void PWR_ExitStopMode(void)
Exits Low Power Stop Mode.
void PWR_EnterOffMode(void)
Accumulates the total time spent in STOP or SLEEP mode (in ms or ticks). Incremented each time PWR_Ex...
void PWR_EnterSleepMode(void)
Enters Low Power Sleep Mode.
void PWR_EnterStopMode(void)
Enters Low Power Stop Mode.
void PWR_ExitSleepMode(void)
Exits Low Power Sleep Mode.
void PWR_ExitOffMode(void)
Exits Low Power Off Mode.
configuration of the timer_if.c instances
#define RTC_BKP_STANDBY_SIGN
Magic value stored in RTC_BKP_STANDBY before entering standby mode. On wakeup, if getBackUpRegister(R...
Definition timer_if.h:69
#define RTC_BKP_STANDBY
RTC backup register index used to persist the standby-mode flag across a system reset / wakeup from O...
Definition timer_if.h:61
Header for USART interface configuration.
void vcom_Resume(void)
Resume the UART and associated DMA (used by LPM).
Definition usart_if.c:217