2024-10-11 07:37 AM
hello evryon im facinsg problem with rtc hak function , il using stm32L031 in custom board . i want to use RTC wake up timer to exit from stop mode evrey 20 s , but it dosnt work and in debug mode it get ca reste fige dans la fonction
HAL_RTCEx_SetWakeUpTimer_IT(&hrtc,0x4A43, RTC_WAKEUPCLOCK_RTCCLK_DIV16 );
im wondirng what it could be the problem i atched the code ; and some debug , i stay stuck on the red led and i dont enter the loop
Hello everyone,
I'm facing a problem with the RTC wake-up function. I’m using the STM32L031 on a custom board. I want to use the RTC wake-up timer to exit from stop mode every 20 seconds, but it doesn't work. In debug mode, the system gets stuck in the function:
I’m wondering what could be the problem. I’ve attached the code and some debug output. The system stays stuck with the red LED on, and it doesn't enter the loop as expected.
Can anyone help me correct this issue?
Thanks!
Main
while (1)
{
// SystemClock_Config();
/* USER CODE END WHILE */
// routine();
// //ADC_Disable(hadc);
//HAL_PWR_DisablePVD();
//HAL_PWREx_DisableFastWakeUp();
//HAL_PWREx_EnableUltraLowPower(); //FFEFEFE
// LED_ROUGE_ON;
LED_Red_ON;
// __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
// HAL_Delay(1000);
// HAL_NVIC_DisableIRQ(EXTI0_1_IRQn);
//HAL_Delay(1000);
// HAL_PWR_DisableBkUpAccess();
HAL_SuspendTick();
HAL_RTCEx_SetWakeUpTimer_IT(&hrtc,0x4A43, RTC_WAKEUPCLOCK_RTCCLK_DIV16 );
//HAL_Delay(1000);
// HAL_Delay(500);
//HAL_Delay(1000);
if (wakeup_flag == 1) {
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
SystemPower_Config();
HAL_ResumeTick();
LED_Red_ON; ;
// HAL_Delay(1000);
wakeup_flag = 0;
}
//
//HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
//
//_HAL_PWR_GET_FALG(PWR_FLAG_);
//Equilibrage();
// Delay_B(4000000);
//Send_Trame_Data();
//Delay_B(400000);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
static void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
/** Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 124;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
}
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc){
wakeup_flag = 1;
/* Clear the EXTI's line Flag for RTC WakeUpTimer */
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Configure the main internal regulator output voltage
*/
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_3;
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_3;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV16;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_RTC;
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_SYSCLK;
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Configure LSE Drive Capability
*/
//HAL_PWR_EnableBkUpAccess();
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
}
msp
void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
{
if(hrtc->Instance==RTC)
{
/* USER CODE BEGIN RTC_MspInit 0 */
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}
/* USER CODE END RTC_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_RTC_ENABLE();
/* RTC interrupt Init */
HAL_NVIC_SetPriority(RTC_IRQn, 0x0, 0);
HAL_NVIC_EnableIRQ(RTC_IRQn);
/* USER CODE BEGIN RTC_MspInit 1 */
/* USER CODE END RTC_MspInit 1 */
}
}
/**
* @brief RTC MSP De-Initialization
* This function freeze the hardware resources used in this example
* @PAram hrtc: RTC handle pointer
* @retval None
*/
void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
{
if(hrtc->Instance==RTC)
{
/* USER CODE BEGIN RTC_MspDeInit 0 */
/* USER CODE END RTC_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_RTC_DISABLE();
/* RTC interrupt DeInit */
// HAL_NVIC_DisableIRQ(RTC_IRQn);
/* USER CODE BEGIN RTC_MspDeInit 1 */
/* USER CODE END RTC_MspDeInit 1 */
}
}
IT
void RTC_IRQHandler(void)
{
/* USER CODE BEGIN RTC_IRQn 0
*
* if(__HAL_PWR_GET_FLAG(RTC_FLAG_WUTF))
{
HAL_RTCEx_WakeUpTimerIRQHandler(&hrtc);
}
if(__HAL_PWR_GET_FLAG (RTC_FLAG_ALRBF || RTC_FLAG_ALRAF)){
HAL_RTC_AlarmIRQHandler(&hrtc);
}*/
HAL_RTCEx_WakeUpTimerIRQHandler(&hrtc);
/* USER CODE END RTC_IRQn 0 */
/* USER CODE BEGIN RTC_IRQn 1 */
/* USER CODE END RTC_IRQn 1 */
}
2024-10-11 10:53 AM
Hello @zohirbou1
After waking up from stop mode, the system clock needs to be reconfigured. Ensure that the SystemClock_Config function is properly defined to set up the system clock.
Try to follow this article and PWR example that may help you:
How to configure the RTC to wake up the STM32 peri... - STMicroelectronics Community
STM32CubeL0/Projects/NUCLEO-L073RZ/Examples/PWR at master · STMicroelectronics/STM32CubeL0 · GitHub