How to use the RTC

Revision as of 17:38, 7 September 2021 by Registered User (Update logs with MP13 name)
Applicable for STM32MP13x lines, STM32MP15x lines

Unspecified MPUs checklist


1. Purpose[edit source]

This article describes how to use the RTC.

2. How to set a hardware clock using the hwclock tool[edit source]

The correct system time must first be set with the date command. Type the date command to see the date and time format of the string:

 date
Fri Mar  9 12:18:51 UTC 2018
 date --set="Fri Mar  9 19:18:51 UTC 2018"
Fri Mar  9 19:18:51 UTC 2018

Set the hardware clock to this date:

 hwclock
Fri Mar  9 12:21:03 2018  0.000000 seconds

 hwclock --systohc --utc

 hwclock
Fri Mar  9 19:19:52 2018  0.000000 seconds

See [1] for more about date settings. See [2] for more about hwclock settings.

3. How to set an alarm[edit source]

# disable the alarm
 echo 0 > /sys/class/rtc/rtc0/wakealarm
# calculate alarm with 1 minute later
 wakeuptime=`date -d "1 minute" +%s`
# set the alarm
 echo $wakeuptime > /sys/class/rtc/rtc0/wakealarm
root@stm32mp1:~# [  829.279019] rtc rtc0: Alarm occurred

You can also set an alarm 'n' seconds later:

 echo +10 > /sys/class/rtc/rtc0/wakealarm

4. How to set an alarm and go into a system sleep state with the rtcwake tool[edit source]

# calculate an alarm 1 minute later
 wakeuptime=`date -d "1 minute" +%s`
# set wakeup on /dev/rtc0
 rtcwake -lt$wakeuptime -m mem
rtcwake: wakeup from "mem" using /dev/rtc0 at Sun Mar 11 10:48:06 2018
[  154.022303] PM: suspend entry (deep)
[  154.024421] PM: Syncing filesystems ... done.
[  154.037258] Freezing user space processes ... (elapsed 0.001 seconds) done.
[  154.044397] OOM killer disabled.
[  154.047555] Freezing remaining freezable tasks ... (elapsed 0.001 seconds) done.
[  154.055039] Suspending console(s) (use no_console_suspend to debug)

One minute later...

NOTICE:  CPU: STM32MP135F Rev.A
NOTICE:  Model: STMicroelectronics STM32MP135F-DK Discovery Board
NOTICE:  Board: MB1635 Var1.0 Rev.A-01
INFO:    Reset reason (0x1830):
INFO:    System exits from STANDBY
INFO:    PMIC version = 0x21
INFO:    Using SDMMC
INFO:      Instance 1
INFO:    Boot used partition fsbl1
NOTICE:  BL2: v2.4-stm32mp13-r2.0
NOTICE:  BL2: Built : 16:43:51, Nov 17 2020
INFO:    BL2: Doing platform setup
INFO:    RAM: DDR3-1066 bin F 1x4Gb 533MHz v1.53
INFO:    BL2: Loading image id 31
INFO:    Loading image id=31 at address 0x30006000
INFO:    Image id=31 loaded: 0x30006000 - 0x30006246
NOTICE:  BL2: Booting BL32
INFO:    Entry point address = 0xde209d00
INFO:    SPSR = 0x1d3
I/TC: Resumed
[  154.701545] usb usb2: root hub lost power or was reset
[  155.053078] usb 2-1: reset high-speed USB device number 2 using ehci-platform
[  155.055443] stm32-dwmac 5800a000.eth1 eth0: configuring for phy/rmii link mode
[  155.086071] dwmac4: Master AXI performs any burst length
[  155.086119] stm32-dwmac 5800a000.eth1 eth0: No Safety Features support found
[  155.089853] stm32-dwmac 5800e000.eth2 eth1: configuring for phy/rmii link mode
[  155.147488] dwmac4: Master AXI performs any burst length
[  155.147530] stm32-dwmac 5800e000.eth2 eth1: No Safety Features support found
[  155.377627] OOM killer enabled.
[  155.377652] Restarting tasks ... done.
[  155.460869] PM: suspend exit

See [3] for more about rtcwake settings.

5. References[edit source]