cancel
Showing results for 
Search instead for 
Did you mean: 

RTC as wakeup-source on C0 family

Panda
Associate III

Hello,

 

I have found various guides on how to configure the RTC as wakeup-source. However, they do not seem to apply to the C0 family.

In the CubeIDE I cannot find the depicted settings on RTC, (most notably, no option to select internal wakeup and configure wakeup counter and clock)

 

Is this not implemented on the C0 family?

 

I just want my MCU to wake up from sleep mode every x milliseconds. What else can I do to implement this on a C011?

 

Thanks

Lucas

10 REPLIES 10
KnarfB
Super User

You can set a periodic RTC alarm to exit sleep mode or stop mode. For standby mode, you may use IWDG for periodic self-wakeup if the standfby period is reasonably small. Examples are in my C0 repository and register level guide: FrankBau/stm32c0 

hth

KnarfB

 

Ah, I wasn't aware that this existed.

Thank you, this is super helpful!

 

To clarify, I would like to use Stop Mode. So no need for IWDG I guess.

 

I guess that I can use the "rtc_alarm_irq - set a periodic RTC alarm interrupt, used to toggle a LED once per minute" project and build upon that. But instead of toggling the LED I return from Stop mode.

 

I am quite unexperienced in using sleep modes on stm32 MCUs. While I found some material about this (also was looking at your stop_uart project and the standby_iwdg project), I am still struggling to understand how to realize an implementation like this.

 

Can you lead me to any material regarding exiting stop mode from RTC interrupt that can push me in the right direction?

Thanks

Lucas

 

 

Let's switch from register level to a STM32CubeMX generated project using HAL.

Generate a new project, I used STM32C0116-DK for example with all default settings.

Activate a periodic RTC alarm once per minute, date and time don't matter.

KnarfB_0-1749619543458.png

The generated code will already activate that alarm in MX_RTC_Init();

Add code in main.c while loop:

  /* USER CODE BEGIN WHILE */
  while (1) {
    HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
    HAL_SuspendTick();
    HAL_PWREx_EnableInternalWakeUpLine();
    HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFE);
    SystemClock_Config();
    HAL_ResumeTick();
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */

Build and run the code. The on-board LED will toggle once per minute.

For reference I have attached the .ioc file used for code generation.

For you own designs: make sure that the RTC runs on LSI clock.

hth

KnarfB

 

 

 

Panda
Associate III

Thank you,

this was really helpful.

 

I am running this on a C011F6P6, which is embedded into a custom design.

It works! The LED toggles once a minute!

 

However, I seem to have bricked the MCU. At least I can't re-programm it anymore.

I am using the STLink from a F303 NUCLEO Board and I get the following Error:

 

ST-LINK SN : 066CFF484971754867025413

ST-LINK FW : V2J46M31

Board : NUCLEO-F303RE

Voltage : 3.27V

Error: Unable to get core ID

Error: ST-LINK error (DEV_TARGET_CMD_ERR)

Encountered Error when opening D:\Programme\STM\STM32CubeIDE_1.17.0\STM32CubeIDE\plugins\com.st.stm32cube.ide.mcu.externaltools.cubeprogrammer.win32_2.2.100.202412061334\tools\bin\STM32_Programmer_CLI.exe

Error in STM32CubeProgrammer

Shutting down...

Exit.

 

I know I need to pull the reset pin low until the STLink is ready, otherwise I get a cant connect to target error.

 

 

I didn`t use your IOC file but I replicated it in a new project for my MCU.

I notice now that I didn't enable Serial Wire DEBUG in my IOC file.

 

Feels like a *** question but.. is that the reason I am failing to re-programm it or is there anything else I might be doing wrong?

 

Thanks

Lucas

 

When the STM32 is in STOP mode you cannot connect the debugger.

But there should be a "Connect under reset" setting in the Debugger configuration Windows. 

Hopfully you have connected the Reset signal on you customer board.

Unfortunatly the other method to regain control (setting the STM32 in Boot mode) is not possible with the STM32C011, if you did not change the NBOOT_SEL bit in the FLASH_OPTR register to 0.

Panda
Associate III

Hello MHoll.2,

 

thanks for your answer.

I was able to programm it again, thank you!

Panda
Associate III

One more question regarding this topic:

 

How can I set a different time? 

 

My RTC Init looks like this

/**
  * @brief RTC Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_RTC_Init(void)
{

  /* USER CODE BEGIN RTC_Init 0 */

  /* USER CODE END RTC_Init 0 */

  RTC_TimeTypeDef sTime = {0};
  RTC_DateTypeDef sDate = {0};
  RTC_AlarmTypeDef sAlarm = {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 = 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;
  hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    Error_Handler();
  }

  /* USER CODE BEGIN Check_RTC_BKUP */

  /* USER CODE END Check_RTC_BKUP */

  /** Initialize RTC and set the Time and Date
  */
  sTime.Hours = 0;
  sTime.Minutes = 0;
  sTime.Seconds = 0;
  sTime.SubSeconds = 0;
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  sDate.Month = RTC_MONTH_JANUARY;
  sDate.Date = 1;
  sDate.Year = 0;

  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }

  /** 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_ALL;
  sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
  sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
  sAlarm.AlarmDateWeekDay = 1;
  sAlarm.Alarm = RTC_ALARM_A;
  if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN RTC_Init 2 */

  /* USER CODE END RTC_Init 2 */

}

 

I've already changed the IOC so that "Alarm Mask Seconds" is alos enabled. Now it flashes once per second, not per minute. But how can I set a custom time? I tried playing around with the RTC Init but could'nt achieve a different timing. 

Ideally, I want to use it in the subsecond domain (to get the MCU to sleep for a few us every time) but I guess the logic is the same once I understand how it's done.

 

Thanks

Lucas

Panda
Associate III

I have re-read it and I think my question might be unclear. I want to set a custom duration for the alarm, so that foir example it will wake up after 5 seconds instead of 1.

>  it will wake up after 5 seconds

There is no RTC periodic alarm feature for arbitrary time intervals.

The RTC deals with absolute time & date like a wall clock. In general, you have to set the next alarm individually by setting all appropriate registers.

For shorter intervals of periodic wakeup I would recommend using the independent watchdog (IWDG).

hth

KnarfB