How to use the RTC

1 Purpose

This article describes how to use the RTC.

2 How to set a hardware clock using the hwclock tool

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

# 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

# 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: STM32MP157CAA Rev.B
NOTICE:  Model: STMicroelectronics STM32MP157C eval daughter on eval mother
NOTICE:  Board: MB1263 Var1 Rev.C-01
INFO:    Reset reason (0x810):
INFO:    System exits from STANDBY
INFO:    Using SDMMC
INFO:      Instance 1
INFO:    Boot used partition fsbl1
INFO:    Product_below_2v5=1: HSLVEN update is
INFO:      destructive, no update as VDD>2.7V
NOTICE:  BL2: v2.0(debug):v2.0-stm32mp-19-01-29
NOTICE:  BL2: Built : 15:58:42, Jan 29 2019
INFO:    BL2: Doing platform setup
INFO:    PMIC version = 0x10
INFO:    RAM: DDR3-1066/888 bin G 2x4Gb 533MHz v1.41
INFO:    BL2 runs SP_MIN setup
INFO:    BL2: Loading image id 4
INFO:    Loading image id=4 at address 0x2ffe5000
INFO:    Image id=4 loaded: 0x2ffe5000 - 0x30000000
INFO:    BL2: Skip loading image id 5
INFO:    read version 0 current version 0
NOTICE:  BL2: Booting BL32
INFO:    Entry point address = 0x2ffe5000
INFO:    SPSR = 0x1d3
INFO:    PMIC version = 0x10
NOTICE:  SP_MIN: v2.0(debug):v2.0-stm32mp-19-01-29
NOTICE:  SP_MIN: Built : 15:58:42, Jan 29 2019
INFO:    ARM GICv2 driver initialized
INFO:    stm32mp HSI (18): Secure only
INFO:    stm32mp HSE (20): Secure only
INFO:    stm32mp PLL2 (27): Secure only
INFO:    stm32mp PLL2_R (30): Secure only
INFO:    SP_MIN: Initializing runtime services
INFO:    SP_MIN: Preparing exit to normal world
[  154.074319] dwc2 49000000.usb-otg: suspending usb gadget configfs-gadget
[  154.196804] Disabling non-boot CPUs ...
[  154.250046] CPU1 killed.
[  154.251704] Enabling non-boot CPUs ...
[  154.252697] CPU1 is up
[  154.254885] rtc rtc0: Alarm occurred
[  154.273940] dwmac4: Master AXI performs any burst length
[  154.273972] stm32-dwmac 5800a000.ethernet eth0: No Safety Features support found
[  154.276938] usb usb2: root hub lost power or was reset
[  154.284703] dwc2 49000000.usb-otg: resuming usb gadget configfs-gadget
[  154.491151] dwc2 49000000.usb-otg: new device is high-speed
[  154.619589] dwc2 49000000.usb-otg: new device is high-speed
[  154.669343] usb 2-1: reset high-speed USB device number 2 using ehci-platform
[  154.697272] dwc2 49000000.usb-otg: new address 2
[  154.722774] configfs-gadget gadget: high-speed config #1: c
[  155.066198] OOM killer enabled.
[  155.069339] Restarting tasks ... done.
[  155.075470] PM: suspend exit

See [3] for more about rtcwake settings.

5 How to get RTC status

 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