Author Topic: stm32 rtc how to set timer alarm not time and date alarm  (Read 218 times)

0 Members and 1 Guest are viewing this topic.

Offline tariq

  • Contributor
  • Posts: 17
  • Country: ir
stm32 rtc how to set timer alarm not time and date alarm
« on: May 04, 2021, 07:02:37 pm »
hi
I'm using Stm32f101vbt6 i want to set rtc alarm every 25 second.because i want to wake up microcontroller from stop mode to reset the watchdog. in HAL libraries i can set rtc alarm by this command:

HAL_RTC_SetAlarm_IT(&hrtc, &RTC_Alarm_Time, RTC_FORMAT_BIN);

but for this command i have to get time from rtc which is hour min and sec and calculate 25 second after and then write the command above. what should i do if i don't want to calculate time each time and set rtc alarm just by adding numbers to RTC alarm register.

Thank you.
 

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 1540
  • Country: nl
Re: stm32 rtc how to set timer alarm not time and date alarm
« Reply #1 on: May 04, 2021, 07:28:55 pm »
The RTC is for hour, minute, second stuff.

If you just want to have regular intervals, then just use a generic timer an program it to generate an ISR every 25s.
 

Offline ajb

  • Super Contributor
  • ***
  • Posts: 2022
  • Country: us
Re: stm32 rtc how to set timer alarm not time and date alarm
« Reply #2 on: May 04, 2021, 07:37:14 pm »
The operation of the RTC is described in the Reference Manual, section 18.  It looks to be pretty simple, just a 32-bit counter with a prescaler.  So you'd set the prescaler to give you whatever base frequency you want from whatever the clock source is, so if you want seconds from a 32kHz clock then you should set the prescaler to 32768.  That will increment the RTC counter once per second, so to set an alarm for 25 seconds from now, simply set the alarm register to the current counter value plus 25.  Or if you want slightly more consistent intervals you can simply add 25 to the alarm register every time the alarm interrupt is triggered.  Be sure to note the configuration flag and register synchronization flags, it looks like you will need to deal with them during initialization/alarm setting.

Link to reference manual: https://www.st.com/resource/en/reference_manual/cd00171190-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf
 

Offline tariq

  • Contributor
  • Posts: 17
  • Country: ir
Re: stm32 rtc how to set timer alarm not time and date alarm
« Reply #3 on: May 04, 2021, 07:55:47 pm »
my rtc clock comes from LSI RC which is 40khz and 40000 in hex is 9C40 so i need write this value to RTC prescaler load register?
and i have to read data from RTC counter register low (RTC_CNTL) and add 25 to it and then write it to RTC alarm register low (RTC_ALRL)?
 

Offline ajb

  • Super Contributor
  • ***
  • Posts: 2022
  • Country: us
Re: stm32 rtc how to set timer alarm not time and date alarm
« Reply #4 on: May 04, 2021, 08:06:45 pm »
yes. Plus enable the RTC, enable the alarm interrupt, etc
 

Offline tariq

  • Contributor
  • Posts: 17
  • Country: ir
Re: stm32 rtc how to set timer alarm not time and date alarm
« Reply #5 on: May 04, 2021, 08:36:21 pm »
what the difference between RTC counter register low and RTC counter register high?is it because when RTC counter register low overflows rest of the counter comes at RTC counter register high?
so i have to check RTC counter register low not overflows?
 

Offline ajb

  • Super Contributor
  • ***
  • Posts: 2022
  • Country: us
Re: stm32 rtc how to set timer alarm not time and date alarm
« Reply #6 on: May 04, 2021, 10:27:47 pm »
The RTC counter register is really one 32-bit value, but it's shown as two 16-bit registers.  I'm not sure why, maybe the RTC is reused from an older 16-bit architecture.  You can probably just use a 32-bit access to read the entire counter value at once.  It's possible the header files for the device even have just a single RTC_CNT register defined for this purpose, you'd have to check. 

You don't have to check anything for overflows, the RTC just fires the alarm interrupt when the alarm register matches the count register.  It will wrap around when it hits 0xFFFFFFFF, so when you add 25 to get the next alarm time, just use 32-bit unsigned integers for the math and you'll always get correct behavior even when the counter wraps around
 

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 1540
  • Country: nl
Re: stm32 rtc how to set timer alarm not time and date alarm
« Reply #7 on: May 04, 2021, 11:22:18 pm »
I just had a peek at the datasheet of a STM32F411 I have lying around...

And it can both be used as Date + HH:mm:ss and for a periodic interrupt adjustable between 120us and 36 hours.
So forget the HAL read the datasheet for further assistance. and start programming some registers. (Maybe there is another HAL function that can do this too)

Code: [Select]
DocID026289 Rev 725/149STM32F411xC STM32F411xE
Functional overview
3.17        Real-time        clock        (RTC) and backup registers

The backup domain includes:
•The real-time clock (RTC)
•20 backup registers
The real-time clock (RTC) is an independent BCD timer/counter.
Dedicated registers contain the second, minute, hour (in 12/24 hour),
week day, date, month, year, in BCD (binary-coded decimal) format.
Correction for 28, 29 (leap year), 30, and 31 day of the month are
 performed automatically.
The RTC features a reference clock detection, a more precise second
 source clock (50 or 60 Hz) can be used to enhance the calendar precision.
 The RTC provides a programmable alarm and programmable periodic
interrupts with wakeup from Stop and Standby modes. The sub-seconds
 value is also available in binary format.It is clocked by a 32.768 kHz
external crystal, resonator or oscillator, the internal low-power RC
oscillator or the high-speed external clock divided by 128. The internal
low-speed RC has a typical frequency of 32 kHz. The RTC can be
calibrated using an external 512 Hz output to compensate for any
natural quartz deviation. Two alarm registers are used to generate
an alarm at a specific time and calendar fields can be independently
masked for alarm comparison. To generate a periodic interrupt, a
16-bit programmable binary auto-reload downcounter with
programmable resolution is available and allows automatic
wakeup and periodic alarms from every 120 μs to every 36
 hours.
« Last Edit: May 04, 2021, 11:24:31 pm by Doctorandus_P »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf