RTC driver in Linux
-
First, we will briefly introduce the similarities and differences between date and hwclock.
Similarities: all represent time.
Differences:
Date indicates the system time. If it is not set when the system is started, the system will start from January 1, 1970.
Hwclock indicates the time of the hardware, such as the time in the clock chip or the RTC of the main chip.
Date is not introduced. Check out hwclock.
# Hwclock -- Help
Busybox v1.14.1 (10:35:16 Cst) Multi-call binary
Usage: hwclock [-R] [-S] [-W] [-L] [-u] [-F file]
Query and set hardware clock (RTC)
Options:
-R show hardware clock time
-S set system time from hardware clock
-W set hardware clock to system time
-U hardware clock is in UTC
-L hardware clock is in local time
-F file use specified device (e.g./dev/rtc2)
It should be said that the explanation is very detailed,
-S set system time from hardware clock reads data from the hardware clock to the system clock. It is mainly used to read time information from the hardware clock at the beginning, then the system time will be 1970.
-W set hardware clock to system time: set the system clock to the hardware clock, which is mainly used to set the hardware clock time. For example, in the root file system created using busybox, there are two steps to set the time: 1. Use date to set the system time; 2. Use hwclock-W to write the current system time to the hardware clock. If there is no second step, the system time will not be updated at the next startup.
Let's take a look at the following example:
# Date
Thu Jan 1 12:10:11 UTC 1970
# Hwclock
Thu Jan 1 12:10:13 1970 0.000000 seconds
The time is the same (of course, the seconds are different, which is the reason why my operation takes time)
Set the time with date
# Date-s 13:00:00
Thu Jan 1 13:00:00 UTC 1970
# Date
Thu Jan 1 13:00:02 UTC 1970
# Hwclock
Thu Jan 1 12:14:27 1970 0.000000 seconds
Obviously, the date and hwclock display are different after the settings.
Then, use
# Hwclock-W
# Date
Thu Jan 1 13:01:27 UTC 1970
# Hwclock
Thu Jan 1 13:01:29 1970 0.000000 seconds
Obviously, the time is the same.
Then, let's take a look at the driver.
First, configure the RTC that comes with at91sam9263.
Rtc-at91sam9 at91_rtt.0: hctosys: unable to read the hardware clock
So we used hwclock to check what went wrong.
# Hwclock
Hwclock: Can't open '/dev/MISC/rtc': no such file or directory
If you do not have this device, you need to create
View the RTC device number
# Cat/proc/devices
Character devices:
1 mem
2 Pty
3 ttyp
4/dev/VC/0
4 tty
4 TTYs
5/dev/tty
5/dev/console
5/dev/ptmx
7 VCs
10 Misc
13 Input
90 MTD
128 PTM
136 PTS
153 SPI
253 PCMCIA
254 rtc
Block devices:
1 ramdisk
7 Loop
31 mtdblock
179 MMC
#
RTC is 254, set up the device
# Mknod/dev/MISC/rtc c 254 0
Check the source code of busybox.
Int fast_func rtc_xopen (const char ** default_rtc, int flags)
{
Int RTC;
If (! * Default_rtc ){
* Default_rtc = "/dev/RTC ";
RTC = open (* default_rtc, flags );
If (RTC> = 0)
Return RTC;
* Default_rtc = "/dev/rtc0 ";
RTC = open (* default_rtc, flags );
If (RTC> = 0)
Return RTC;
* Default_rtc = "/dev/MISC/RTC ";
}
Return xopen (* default_rtc, flags );
}
You will find that you have made three attempts to call hwclock. You can use either/dev/RTC,/dev/rtc0,/dev/MISC/RTC, if none of the three are found, an error is returned.
For simplicity, get/dev/rtc0 later.
# Mknod/dev/rtc0 C 254 0
Check it with hwclock.
# Hwclock
Thu Jan 1 00:05:41 1970 0.000000 seconds
It's time.
If there is a problem with the clock format provided by the clock chip or the main chip, the following error will occur:
# Hwclock
Hwclock: rtc_rd_time: Invalid or incomplete multibyte or wide character
Here we need to set the system time to the clock chip or the RTC that comes with the main chip, as shown below:
# Hwclock-W
Then RTC can be used.