1
\$\begingroup\$

I've started digging into STM32F103 using HAL Libraries and raw registers. It's the first time to program at such low level.

Anyway following a video I've successfully implemented "Standby" mode. https://www.youtube.com/watch?v=O82rj9qxkgs

I need an RTC to wake it up instead of a button. So I read the reference manual without really knowing how to start and finish it up.

RTC Simplified Block Diagram

Searching the internet, I found 2 main categories of sources: sources using Standard Peripheral Library (SPL), which is not my case. And sources using registers.

Anyway couldn't successfully implement the "RTC Wake Up" method.

I'm using this resource as my code starter.

Anyway, because I don't need RTC Calendar, I've removed any setting/reading date/time as I don't need them.

And I put a LED toggle to:

void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) {
   HAL_GPIO_TogglePin(LED_GPIO_Port,LED_Pin);
}

What I observe now: the MCU toggles the LED At the start of sleeping (couldn't measure time), then continues sleeping without running main() again.

  • The MCU wakes up successfully using WAKEUP positive edge via a button.
\$\endgroup\$
  • \$\begingroup\$ Could you tell us what you want to do more precisely ? Periodically wake up/standby ? One shot wake up after a certain amount of time ? How much time ? You seem to have already implemented a call to an Alarm so what do you want then ? I already did it on a F072 so I think I can help you. \$\endgroup\$ – Mathieu G. Feb 25 at 13:56
  • \$\begingroup\$ Ofcourse @MathieuG. I want to want to wake up the MCU periodically with changeable time in seconds and minutes. ("every 15 seconds", "every 60 seconds"..). doing some stuff (read sensor and send data) then go Standby mode.... In my code I've set the Alarm at 5 seconds, but it does respond immediately after sleep. Ask for the code if it helps. \$\endgroup\$ – Hamza Hajeir Feb 26 at 11:14
0
\$\begingroup\$

I used an Alarm to generate a 1 hour sleep in STOP mode before automatically wake up to do some stuffs and then shut off. I configured 3 wake up conditions :

  • 2 external interrupts
  • The 59min timeout.

Here is the source for a STM32F072. The source must be really close for your STM32F103:

RTC_AlarmTypeDef sAlarm;
GPIO_InitTypeDef GPIO_InitStruct;

/* Enable 2 external interruptions and configure EXTI lines in interruption mode */
NVIC_EnableIRQ(EXTI4_15_IRQn);
HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
NVIC_EnableIRQ(EXTI2_3_IRQn);
HAL_NVIC_SetPriority(EXTI2_3_IRQn, 0, 0);
GPIO_InitStruct.Pin = Alertb_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(Alertb_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = BPb_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(BPb_GPIO_Port, &GPIO_InitStruct);

/* Enable the Alarm A : mask only the minute and program a time in 59 minutes */
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
sAlarm.AlarmTime.Minutes = (sTime.Minutes + 59)%60;
sAlarm.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM;
sAlarm.AlarmDateWeekDay = RTC_WEEKDAY_MONDAY;
sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY | RTC_ALARMMASK_HOURS | RTC_ALARMMASK_SECONDS;
sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
sAlarm.Alarm = RTC_ALARM_A;
if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
{
    Error_Handler();
}

/* Go into stop mode + internal voltage regulator in low power mode :
 * System is awaken by :
 * - The 2 previous EXT interrupt
 * - The programmed ALARM */
HAL_SuspendTick();
__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFE); // Here the uc is stuck during 59 min or until an EXTI event occurred
HAL_ResumeTick();

/* Re-init system clock configuration after STOP mode exit */
SystemClock_Config();

Here is how ST does the Alarm config in their demonstration projects :

/**
  * @brief  Configure the current time and date.
  * @param  None
  * @retval None
  */
static void RTC_AlarmConfig(void)
{
  RTC_DateTypeDef  sdatestructure;
  RTC_TimeTypeDef  stimestructure;
  RTC_AlarmTypeDef salarmstructure;

  /*##-1- Configure the Date #################################################*/
  /* Set Date: Tuesday February 18th 2014 */
  sdatestructure.Year = 0x14;
  sdatestructure.Month = RTC_MONTH_FEBRUARY;
  sdatestructure.Date = 0x18;
  sdatestructure.WeekDay = RTC_WEEKDAY_TUESDAY;

  if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure,RTC_FORMAT_BCD) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler(); 
  } 

  /*##-2- Configure the Time #################################################*/
  /* Set Time: 02:20:00 */
  stimestructure.Hours = 0x02;
  stimestructure.Minutes = 0x20;
  stimestructure.Seconds = 0x00;

  if(HAL_RTC_SetTime(&RtcHandle,&stimestructure,RTC_FORMAT_BCD) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler(); 
  }  

  /*##-3- Configure the RTC Alarm peripheral #################################*/
  /* Set Alarm to 02:20:30 
     RTC Alarm Generation: Alarm on Hours, Minutes and Seconds */
  salarmstructure.Alarm = RTC_ALARM_A;
 salarmstructure.AlarmTime.Hours = 0x02;
  salarmstructure.AlarmTime.Minutes = 0x20;
  salarmstructure.AlarmTime.Seconds = 0x30;

  if(HAL_RTC_SetAlarm_IT(&RtcHandle,&salarmstructure,RTC_FORMAT_BCD) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler(); 
  }
}

Regards,

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks for your answer, My situation is a bit different. As I want to wake from Standby Mode... By reading the Reference manual I came across this sentence : "To wakeup from Stop mode with an RTC alarm event, it is necessary to: • Configure the EXTI Line 17 to be sensitive to rising edge • Configure the RTC to generate the RTC alarm To wakeup from Standby mode, there is no need to configure the EXTI Line 17." So I think your answer is more than enough. Just needs some modifications.. Can you share your RTC Init function So I keep up in the same page. \$\endgroup\$ – Hamza Hajeir Feb 27 at 14:57
  • \$\begingroup\$ I connect Vbat to 3.3V, Was this the case in your schematic ? \$\endgroup\$ – Hamza Hajeir Feb 27 at 14:58
  • \$\begingroup\$ I'm trying to merge your code to mine (F103), Came across several issues : -Should I read BKUP using HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) ? And sAlarm has no members/fields than Alarm and AlarmTime. AlarmTime doesn't have more than Hours, Minutes and Seconds. So from TimeFormat to AlarmDateWeekDaySel fields are not present in F103 HAL. Are they mandatory ? \$\endgroup\$ – Hamza Hajeir Feb 27 at 15:18
  • \$\begingroup\$ Yes connect 3v3 to Vbat to allow RTC clock to run. Indeed ST has modified its driver source a bit. It seems easier to use Alarm now. You can directly set the Alarm hours, min and seconds to what you want. I will edit my answer with a ST example I found. \$\endgroup\$ – Mathieu G. Feb 27 at 15:44
  • \$\begingroup\$ Unfortunately the bat is connected to 3v3 but It doesn't wake up from RTC.Can you share all your RTC initialization codes ? \$\endgroup\$ – Hamza Hajeir Feb 28 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.