6. RTC

The full name of RTC is Real Time Clock. It is a hardware device specially used to record time. It can generally be integrated inside the SOC, or it can be plugged in to communicate with it through i2c.

Then why is RTC needed? Because the Linux system time (which is what we often call wall time) can only be used when the system is running, and the system shutdown time is lost. The RTC can continue to work by relying on an external battery or other supply after the system is shut down, thus saving time.

LubanCat-RK series boards require an RTC battery to enable the RTC function. The selection of RTC batteries is as follows:

注解

The link is for reference only. If necessary, please search and purchase based on the keywords in the above link.

The positive and negative poles of the wiring are as follows:

未找到该图片

If the line sequence is wrong, you can use tweezers to adjust the RTC battery interface:

未找到该图片

6.1. Instructions

Linux provides three user space calling interfaces. The corresponding path in the board is:

  • SYSFS interface:/sys/class/rtc/rtc0/

  • PROCFS interface: /proc/driver/rtc

  • IOCTL interface: /dev/rtc0

SYSFS interface:

1
2
3
4
root@lubancat:~# cat /sys/class/rtc/rtc0/date
2023-06-19
root@lubancat:~# cat /sys/class/rtc/rtc0/time
03:15:22

PROCFS interface:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
root@lubancat:~# cat /proc/driver/rtc
rtc_time        : 07:06:56
rtc_date        : 2023-06-19
alrm_time       : 03:18:50
alrm_date       : 2023-06-19
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
root@lubancat:~#

IOCTL interface:

1
2
You can refer to the kernel documentation for examples
tools/testing/selftests/timers/rtctest.c

6.2. Some commonly used commands

1
2
3
4
date          //Modify the system clock. The specific command can be used under man
hwclock -s    //Synchronize hardware time to system time
hwclock -w    //Synchronize system time to hardware time
timedatectl   //Display system time, etc.