How to use the RTC

来自百问网嵌入式Linux wiki


Purpose

本文介绍了如何使用RTC。 RTC overview文章中介绍了RTC概述。

How to set a hardware clock using the hwclock tool

必须首先使用date命令设置正确的系统时间。 键入date命令以查看字符串的日期和时间格式:

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

将硬件时钟设置为此日期:

Board $> hwclock
Fri Mar  9 12:21:03 2018  0.000000 seconds

Board $> hwclock --systohc --utc

Board $> hwclock
Fri Mar  9 19:19:52 2018  0.000000 seconds

有关日期设置的更多信息,请参见[1] 有关hwclock设置的更多信息,请参见[2]

How to set an alarm

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

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

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

How to set an alarm and go into a system sleep state with the rtcwake tool

# calculate an alarm 1 minute later
Board $> wakeuptime=`date -d "1 minute" +%s`
# set wakeup on /dev/rtc0
Board $> 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.

References

<securetransclude src="ProtectedTemplate:PublicationRequestId" params="15042 | 2020-02-21 |"></securetransclude>