一,Linux下的时间分为两种,系统时间与硬件时间(RTC芯片):
1,系统时间就是运行系统能够直接看到的时间;
2,硬件时间就是RTC芯片中的时间,断电任然有电池供电;
linux系统开机时,会从RTC中读取当前时间,作为系统时间,从此以后系统时间独立tick。意味着,如果你用date命令修改了系统时间,硬件时间是不受影响的,如果此时关机,下次的系统时间还是不对。要想将系统时间保存到硬件时间,可以是用 hwclock 或者clock命令,hwclock就是hardware clock的意思。
因为我的开发板不支持clock命令,所以只讲解hwclock命令:
BusyBox v1.20.2 (2014-07-26 20:22:32 CST) multi-call binary.
Usage: hwclock [-r|--show] [-s|--hctosys] [-w|--systohc] [-t|--systz] [-l|--localtime] [-u|--utc] [-f|--rtc FILE]
Query and set hardware clock (RTC)
-r Show hardware clock time
-s Set system time from hardware clock
-w Set hardware clock from system time
-t Set in-kernel timezone, correct system time
if hardware clock is in local time
-u Assume hardware clock is kept in UTC
-l Assume hardware clock is kept in local time
-f FILE Use specified device (e.g. /dev/rtc2)
hwclock –r show,显示硬件时钟,等价于不加参数的hwclock。
hwclock –s hctosys,hardware clock to system,将“硬件时钟”RTC的时间写到Linux“系统”时钟里。
hwclock –w systohc,system to hardware clock,将“系统”时钟写到“硬件时钟”RTC里面
二,内核添加支持RTC(内核linux-3.8)
1,RTC在内核已经有很好的支持,只需要添加内核编译,注册添加即可:
修改arch/arm/mach-s3c24xx/mach-smdk2440.c,添加(添加RTC设备即可,红字体):
static struct platform_device *smdk2440_devices[] __initdata = {
&s3c_device_ohci,
&s3c_device_lcd,
&s3c_device_wdt,
&s3c_device_i2c0,
&s3c_device_iis,
&s3c_device_dm9000,
&s3c_device_adc,
&s3c_device_ts,
&s3c_device_rtc,
};
在arch/arm/plat-samsung/devs.c有s3c_device_rtc定义:
#ifdef CONFIG_PLAT_S3C24XX
static struct resource s3c_rtc_resource[] = {
[0] = DEFINE_RES_MEM(S3C24XX_PA_RTC, SZ_256),
[1] = DEFINE_RES_IRQ(IRQ_RTC),
[2] = DEFINE_RES_IRQ(IRQ_TICK),
};
struct platform_device s3c_device_rtc = {
.name = "s3c2410-rtc",
.id = -1,
.num_resources = ARRAY_SIZE(s3c_rtc_resource),
.resource = s3c_rtc_resource,
};
#endif /* CONFIG_PLAT_S3C24XX */
2,make menuconfig添加配置选项:
DeviceDrivers --->
[*] Real Time Clock --->
--- Real Time Clock
[*] Set system time from RTC on startup and resume
(rtc0) RTC usedto set the system time
[*] /sys/class/rtc/rtcN (sysfs)
[*] /proc/driver/rtc (procfs for rtcN)
[*] /dev/rtcN(character devices)
Linux的RTC驱动实现之后,一般是默认的是一个普通的字符型设备,或者一个misc设备,也可以是一个平台设备。如果insmod驱动后或者直接编译进入内核,对应的设备文件一般是/dev/rtc或/dev/rtc0或者 /dev/misc/rtc。而在linux-3.8内核里,制作根文件系统工具busybox-1.20.2,包含hwclock命令定义:
在busybox-1.20.2的libbb/rtc.c有:
int FAST_FUNCrtc_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";
}
returnxopen(*default_rtc, flags);
}
3,编译下载启动:红色字体表示RTC添加ok
[ s3c2440@zhou]# boot
NAND read: device 0 offset 0x100000, size 0x800000
8388608 bytesread: OK
## Booting kernel from Legacy Image at 30008000 ...
ImageName: Linux Kernel
Created: 2014-08-13 7:55:08 UTC
ImageType: ARM Linux Kernel Image(uncompressed)
DataSize: 3115480 Bytes = 3 MiB
Load Address:30008000
EntryPoint: 30008040
VerifyingChecksum ... OK
XIP KernelImage ... OK
OK
OS entry point: 30008040
Image entry point=30008040
Starting kernel ...
NAND device: Manufacturer ID: 0xec, Chip ID: 0xda(Samsung NAND 256MiB 3,3V 8-bit), 256MiB, page size: 2048, OOB size: 64
Scanning device for bad blocks
Bad eraseblock 236 at 0x000001d80000
Bad eraseblock 704 at 0x000005800000
Bad eraseblock 705 at 0x000005820000
Bad eraseblock 717 at 0x0000059a0000
Bad eraseblock 1251 at 0x000009c60000
Bad eraseblock 1631 at 0x00000cbe0000
Bad eraseblock 1771 at 0x00000dd60000
Bad eraseblock 1947 at 0x00000f360000
Creating 9 MTD partitions on "NAND":
0x000000000000-0x000000100000 : "mtdblock0_u-Boot1MB "
0x000000100000-0x000001000000 : "mtdbolck1_kernel15MB"
0x000001000000-0x000002400000 :"mtdbolck2_ramdisk 20MB"
0x000002400000-0x000003800000 : "mtdblock3_cramfs20MB"
0x000003800000-0x000006000000 : "mtdblock4_jffs240MB"
0x000006000000-0x000008800000 : "mtdblock5_yaffs240MB"
0x000008800000-0x00000b000000 : "mtdblock6_ubifs40MB"
0x00000b000000-0x00000d800000 : "mtdblock7_apps40MB"
0x00000d800000-0x000010000000 : "mtdblock8_data40MB"
。。。。。
usbserial: USB Serial support registered for pl2303
mousedev: PS/2 mouse device common for all mice
s3c-rtc s3c2410-rtc: rtc disabled, re-enabling
s3c-rtc s3c2410-rtc: rtc core: registered s3c as rtc0
sdhci: Secure Digital Host Controller Interface driver
sdhci: Copyright(c) Pierre Ossman
。。。。。
UBI: background thread "ubi_bgt0d" started,PID 472
s3c-rtc s3c2410-rtc: setting system clock to 2024-04-2611:18:22 UTC (1714130302)
ALSA device list:
。。。。。
Freeing init memory: 156K
usb 1-1: New USB device found, idVendor=05e3,idProduct=0606
usb 1-1: New USB device strings: Mfr=1, Product=2,SerialNumber=0
usb 1-1: Product: USB Hub 2.0
hub 1-1:1.0: USB hub found
hub 1-1:1.0: 4 ports detected
dm9000 dm9000 eth0: link down dm9000 dm9000 eth0: linkup, 100Mbps, full-duplex, lpa 0xCDE1
Copyright (C) 2014zhouguangfeng
root for passwd: 12345
root login: root
Password:
apps data etc init linuxrc my2440 root sys usr
bin dev info lib mnt proc sbin tmp var
Wed Jan 1 01:04:08 MST 2014
Wed Aug 13 17:51:10 MDT 2014
Wed Jan 1 01:05:08 2014 0.000000 seconds
Wed Jan 1 01:05:26 MST 2014
Wed Aug 13 17:51:10 MDT 2014
Wed Jan 1 01:05:59 2014 0.000000 seconds
Wed Aug 13 17:51:25 2014 0.000000 seconds
crw-rw---- 1 root root 254, 0 Dec 31 1969 /dev/rtc0