How to use the RTC

Applicable for STM32MP13x lines, STM32MP15x lines

1 Purpose[edit]

This article describes how to use the RTC.

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

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]

# 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]

# 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 Mon Mar 20 11:59:16 2023
[   81.754154] PM: suspend entry (deep)
[   81.756533] Filesystems sync: 0.000 seconds
[   81.762182] Freezing user space processes ... (elapsed 0.001 seconds) done.
[   81.769556] OOM killer disabled.
[   81.772612] Freezing remaining freezable tasks ... (elapsed 0.001 seconds) done.
[   81.780067] printk: Suspending console(s) (use no_console_suspend to debug)

One minute later...

NOTICE:  CPU: STM32MP157CAA Rev.B
NOTICE:  Model: STMicroelectronics STM32MP157C eval daughter on eval mother
NOTICE:  Board: MB1263 Var1.0 Rev.C-01
INFO:    PMIC version = 0x10
INFO:    Reset reason (0x810):
INFO:    System exits from STANDBY
INFO:    FCONF: Reading TB_FW firmware configuration file from: 0x2ffe2000
INFO:    FCONF: Reading firmware configuration information for: stm32mp_io
NOTICE:  BL2: v2.8-stm32mp1-r1.0(debug):openstlinux-23-03-17_next-2-g48077eaee0(48077eae)
NOTICE:  BL2: Built : 16:10:57, Mar 31 2023
INFO:    BL2: Doing platform setup
INFO:    RAM: DDR3-DDR3L 32bits 533000kHz
INFO:    BL2: Skip loading image id 1
INFO:    BL2: Skip loading image id 4
INFO:    BL2: Skip loading image id 8
INFO:    BL2: Skip loading image id 9
INFO:    BL2: Skip loading image id 2
INFO:    BL2: Skip loading image id 16
INFO:    BL2: Skip loading image id 5
NOTICE:  BL2: Booting BL32
INFO:    Entry point address = 0x54000188
INFO:    SPSR = 0x1d3
I/TC: Resumed
I/TC: SeconI/TC: Secondary CPU 1 switching to normal world boot
[   81.789369] dwc2 49000000.usb-otg: suspending usb gadget configfs-gadget
[   81.922609] Disabling non-boot CPUs ...
[   81.923589] CPU1 killed.
[   81.924670] Enabling non-boot CPUs ...
[   81.930633] CPU1 is up
[   81.946846] dwmac4: Master AXI performs any burst length
[   81.946887] stm32-dwmac 5800a000.ethernet eth0: No Safety Features support found
[   81.946922] stm32-dwmac 5800a000.ethernet eth0: IEEE 1588-2008 Advanced Timestamp supported
[   81.946990] stm32-dwmac 5800a000.ethernet eth0: configuring for phy/rgmii-id link mode
[   81.981951] usb usb2: root hub lost power or was reset
[   82.106798] dwc2 49000000.usb-otg: resuming usb gadget configfs-gadget
[   82.337015] onboard-usb-hub 2-1: reset high-speed USB device number 2 using ehci-platform
[   82.726126] OOM killer enabled.
[   82.729301] Restarting tasks ... done.
[   82.742746] random: crng reseeded on system resumption
[   82.754424] PM: suspend exit


See [3] for more about rtcwake settings.

5 How to get RTC status[edit]

 cat /proc/driver/rtc
rtc_time        : 07:25:13
rtc_date        : 2000-01-01
alrm_time       : 00:00:00
alrm_date       : 2165-01-01
alarm_IRQ       : no
alrm_pending    : no
update IRQ enabled      : no
periodic IRQ enabled    : no
periodic IRQ frequency  : 1
max user IRQ frequency  : 64
24hr            : yes

6 References[edit]