cancel
Showing results for 
Search instead for 
Did you mean: 

RTC alarm problem only with some MCUs

felia
Associate II

Hi, I have a strange behavior with the RTC alarm only with some MCU. 

In my project use the STM32L476, when I set the alarm the MCU wake up without problem, but this work fine only with some board. On the other boards with the same MCU and the same FW they don't wake up from standby. 

I use LSE, a battery and the ALARM_A.

The MCU gets the time and date from the RTC, sets the alarm and entering in standby.

I use the HAL_RTC_GetTime() and HAL_RTC_GetDate() to get time and date.

I tried to Bypass the shadow registers, but the problem there is always.

Someone can help me!

Below RTC_init() code:

/* RTC init function */
void MX_RTC_Init(void)
{
  RTC_TimeTypeDef sTime;
  RTC_DateTypeDef sDate;
  RTC_AlarmTypeDef sAlarm;
 
    /**Initialize RTC Only */
  hrtc.Instance = RTC;
 
 
  /* Set the BYPSHAD bit */
  RTC->CR |= RTC_CR_BYPSHAD;	//Bypass the shadow registers
 
if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2){
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = 127;
  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(__FILE__, __LINE__);
  }
 
    /**Initialize RTC and set the Time and Date  */
  sTime.Hours = 10;	
  sTime.Minutes = 0;	
  sTime.Seconds = 0;
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
  sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  sDate.Month = RTC_MONTH_AUGUST;
  sDate.Date = 1;
  sDate.Year = 18;
 
  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
    /**Enable the Alarm A */
  sAlarm.AlarmTime.Hours = 0;
  sAlarm.AlarmTime.Minutes = 0;
  sAlarm.AlarmTime.Seconds = 0;
  sAlarm.AlarmTime.SubSeconds = 0;
  sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
  sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
  sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
  sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
  sAlarm.AlarmDateWeekDay = 1;
  sAlarm.Alarm = RTC_ALARM_A;
  if (HAL_RTC_SetAlarm(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
    HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
  }
}

Below the set_alarm() code:

/* set alarm function */
void set_alarm(uint8_t hours, uint8_t minutes, uint8_t seconds, alarm_type_t alarm_type) {
	RTC_AlarmTypeDef sAlarm;
	uint32_t type;
 
	switch (alarm_type) {
		case ALARM_A:
			type = RTC_ALARM_A;
			break;
		case ALARM_B:
			type = RTC_ALARM_B;
			break;
		case ALARM_AB:
			type = RTC_ALARM_A;
			break;
		default:
			return;
	}
 
	/**Enable the Alarm A */
	sAlarm.AlarmTime.Hours = hours;
	sAlarm.AlarmTime.Minutes = minutes;
	sAlarm.AlarmTime.Seconds = seconds;
	sAlarm.AlarmTime.SubSeconds = 0;
	sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
	sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
	sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
	sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
	sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
	sAlarm.AlarmDateWeekDay = 1;
	sAlarm.Alarm = type;
 
	if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK) {
		_Error_Handler(__FILE__, __LINE__);
	}
 
	if (alarm_type == ALARM_AB) {
		sAlarm.Alarm = RTC_ALARM_B;
		if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK) {
			_Error_Handler(__FILE__, __LINE__);
		}
	}
 
	HAL_NVIC_EnableIRQ(TIM2_IRQn);
}

Below the system_standby() code:

void system_standby (void) {
 
	/** deactivate_periodic_wakeup */
	HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
	__HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc);
	__HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&hrtc, RTC_FLAG_WUTF);
	__HAL_RTC_WRITEPROTECTION_ENABLE(&hrtc);
	
	/** enter standby */
	__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
	HAL_PWR_EnterSTANDBYMode();
	//HAL_PWREx_EnterSHUTDOWNMode();
	while(1);
}

Thank you guys!

5 REPLIES 5

Read out the RTC registers' content and check if they are set as you expect them.

Also check, if the RTC is actually running, especially on the problematic boards.

JW

felia
Associate II

Thank you for answer, but the RTC registers' content is ok an the RTC is running on the problematic boards. 

If the problematic board wake up by the HW RESET or by GPIO the system works.

With short period, for example 5 seconds, the wake up by the Alarm works, but with long period for example 6 or 8 hours the MCU doesn't wake up.

And does the problem relate to date crossing, or not? Or can't it be AM/PM related? Does the clock display the expected time/date after failing and being reset (in other words, didn't the clock stop, after several hours, before the alarm; or runs significantly slower)? As I've said I don't use Cube, so can't judge from the code. Maybe you might post readout of RTC registers from a device after the failure so we can have a look.

JW

felia
Associate II

The alarm is only at the specific time, every day.

The time is configured to 24H so it's not be AM/PM related.

After failing and being reset the clock display the expected time, the clock doesn't stop never.

For an unknown reason with some MCU works fine with others not.

Okay so show us what did you read out of the registers.

JW