2020-11-29 07:38 AM
That is my code below (Nucleo STM32L053). RTC works from 32768 quartz (LSE). Blue Button disconnected by eliminating of the SB17. Additional LED is connected to PC13 instead of the button. So, the device wakes up normally, when I set high level on wakeup pin PA0, but never wakes up from RTC ALARM-A signal. Alarm-A is working properly: additional LED on PC13-pin switches on after 12 seconds after reset system, but m/controller does not wake up (green LED PA5 switches on once after reset system and never more). Please, help me.
#include "main.h"
#include "rtc.h"
#include "usart.h"
#include "gpio.h"
/* USER CODE BEGIN PV */
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
RTC_AlarmTypeDef sAlarm = {0};
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_RTC_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
__HAL_RTC_ALARMA_DISABLE(&hrtc);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
HAL_Delay(1000);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
//-----------------------------------------------------------------------------
/* Load all parameters to ALARM registers */
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BCD);
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BCD);
sAlarm.AlarmTime.Seconds = sTime.Seconds + 0x12; // ---> Wakeup every 12 seconds...
sAlarm.AlarmTime.Hours = 0x0;
sAlarm.AlarmTime.Minutes = 0x0;
sAlarm.AlarmTime.SubSeconds = 0x0;
sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY|RTC_ALARMMASK_HOURS
|RTC_ALARMMASK_MINUTES;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
sAlarm.AlarmDateWeekDay = 0x1;
sAlarm.Alarm = RTC_ALARM_A;
if (HAL_RTC_SetAlarm(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
//----------------------------------------------------------------------------
if (__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET)
{
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB); // clear the flag
/** Disable the WWAKEUP PIN **/
HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1); // disable PA0
/** Deactivate the RTC wakeup **/
HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
}
/* clear the WU FLAG */
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
/* clear the RTC Wake UP (WU) flag */
__HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hrtc, RTC_FLAG_WUTF);
/* clear the RTC ALARM A flag */
__HAL_RTC_ALARM_CLEAR_FLAG(&hrtc, RTC_FLAG_ALRAF);
/* Enable ALARM A module */
__HAL_RTC_ALARMA_ENABLE(&hrtc);
/* Enable the WAKEUP PIN ---> PA0 */
HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);
/* Enter the standby mode */
HAL_PWR_EnterSTANDBYMode();
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
2020-11-29 08:28 AM
I see one trouble
sAlarm.AlarmTime.Seconds = sTime.Seconds + 0x12; // ---> Wakeup every 12 seconds...
is not allways in range 00-59sec, then when you run code in sec over 48 your set alarm is never real.
Maybe RTC_Init set time to zero , but i mean not, because RTC cant work then.
Second bigger is
if (__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET)
{
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB); // clear the flag
/** Disable the WWAKEUP PIN **/
HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1); // disable PA0
/** Deactivate the RTC wakeup **/
HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
}
Maybe you dont know , that wake from standby realy reset MCU , then your code make new start and do this if, where you disable wakeuptimer. Then your wakeup work only once. But still work with pin, that you reenable.
2020-11-30 07:04 AM
CubeMX generates code, for example, if ALARM seconds = 57 seconds:
/** Enable the Alarm A
*/
sAlarm.AlarmTime.Hours = 0x0;
sAlarm.AlarmTime.Minutes = 0x0;
sAlarm.AlarmTime.Seconds = 0x57;
That's right because I use BCD-format.
My MCU is working properly, when I use: 1) wakeup-pin; 2) RTC wakeup mode. And only ALARM does not wakeup the MCU! Never wakeup!! No once!!!
Moreover, ALARM-A output (PC13 pin) creates alarm-signal according to data written in sAlarm and sTime structures.
I remind, PC13 works in standby mode like in usual run-mode.
2020-11-30 07:18 AM
How much is 0x57 + 0x12 ??
2020-11-30 07:59 AM
My used example
// Disable the Alarm A
RTC_AlarmCmd(RTC_Alarm_A, DISABLE);
// Get the current Time
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
// Alarm A Settings
RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = RTC_TimeStructure.RTC_Seconds + sec;
if( RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds > 59 ) RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds -= 60;
// Configure the RTC Alarm A register
RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure);
// RTC Set Alarm success.
// Enable the RTC Alarm A Interrupt
RTC_ITConfig(RTC_IT_ALRA, ENABLE);
// Enable the Alarm A
RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
PWR_ClearFlag(PWR_FLAG_WU);
RTC_ClearFlag(RTC_FLAG_ALRAF);
PWR_EnterSTANDBYMode();
2020-12-01 09:13 AM
Where you have seen 0x57+0x12 ???
57 seconds = 0x57 in BCD format and 0x39 in BINARY format.
2020-12-01 09:16 AM
I do not see differs between my and your code (I used HAL). You use BINARY format, but I use BCD format of time. May be, the problem is in BCD format?
2020-12-01 10:24 AM
I see it i clear flags before enter standbymode.
2020-12-03 06:33 AM
I clear flags also:
/* clear the WU FLAG */
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
/* clear the RTC Wake UP (WU) flag */
__HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hrtc, RTC_FLAG_WUTF);
/* clear the RTC ALARM A flag */
__HAL_RTC_ALARM_CLEAR_FLAG(&hrtc, RTC_FLAG_ALRAF);
May be, the RTC ALARM A does not working simultaneously with other wakeup-sources ?
2020-12-03 09:56 AM
Hi Alexey,
i dont rememmber maybe order , i use enable then clear you dont, but maybe your all alarm init is incomplete. I compare and my code have in mx init
/**Configure the Alarm A */
sAlarm.AlarmTime.Hours = sTime.Hours;
sAlarm.AlarmTime.Minutes = sTime.Minutes;
sAlarm.AlarmTime.SubSeconds = 0x0;
sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY|RTC_ALARMMASK_HOURS|RTC_ALARMMASK_MINUTES;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
sAlarm.AlarmDateWeekDay = sDate.WeekDay;
sAlarm.Alarm = RTC_ALARM_A;
and in code only seconds is modified. MASK for seconds need be off. Too we use for RTC work in standby commands to enable backup domain
/* Enable Power Clock*/
__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWR_EnableBkUpAccess();
...