How to use RTC Real Time Clock hardware

From RidgeRun Developer Connection

Jump to:navigation, search

Contents

Overview

This wiki page explains some simple steps required to set an RTC clock chip. All these commands are tested on SDK stable version 20011q2 for the DM365 and using a DS1337.

References

date manual
hwclock manual


Low level access

You can use the i2c tools to directly read and write the RTC hardware registers

I2C_ADDR=0x68

i2cdetect  -y 1
i2c -f /dev/i2c-1 a $I2C_ADDR w 0 r r r r r r r r s

with output similar to

    0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- 08 -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: 30 -- -- -- -- -- -- -- 38 -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- 4c -- -- -- 
50: 50 -- 52 -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --   

0x07 0x45 0x18 0x07 0x14 0x01 0x00 0xB3


Board setup

The board file, like $DEVDIR/kernel/linux-2.6.32.17-psp03.01.01.39/arch/arm/mach-davinci/board-dm368-cooldevice.c needs to contain the following RTC platform data in your i2c list:

static struct i2c_board_info i2c_info[] = {
	{
		I2C_BOARD_INFO("rtc-ds1307", 0x68),
		.type = "ds1337",
	},
};

You can remove the call to dm365_init_rtc() as well.

Kernel configuration

You need to enable the driver for the RTC hardware you are using.

Maxim DS1337 Real Time Clock I2C chip

Enable the Linux kernel DS1307 driver.

Symbol: RTC_DRV_DS1307 [=n]
   Prompt: Dallas/Maxim DS1307/37/38/39/40, ST M41T00, EPSON RX-8025
     Defined at drivers/rtc/Kconfig:131
     Depends on: RTC_CLASS && I2C
Location:
       -> Kernel configuration
         -> Device Drivers
           -> Real Time Clock (RTC_CLASS [=y])           

and disable the RTC Davinci driver:

Symbol: RTC_DRV_DAVINCI [=n]                                                                                                
  Prompt: TI DaVinci RTC                                                                                                      
   Defined at drivers/rtc/Kconfig:579                                                                                          
   Depends on: RTC_CLASS && ARCH_DAVINCI_DM365                                                                              
   Location:                                                                                                                 
     -> Kernel configuration                                                                                                 
       -> Device Drivers                                                                                                     
         -> Real Time Clock (RTC_CLASS [=y]) 

If the driver is properly registered on the kernel, this should be displayed on boot time:

rtc-ds1307 1-0068: setting system clock to 2000-01-08 19:14:54 UTC (947358894)

Verify kernel supports RTC chip

In the boot output, you should see something similar to:

rtc-ds1307 1-0068: SET TIME!
rtc-ds1307 1-0068: rtc core: registered ds1338 as rtc0
rtc-ds1307 1-0068: 56 bytes nvram

The following files should exist:

ls -l /dev/rtc /dev/rtc0
cat /sys/class/rtc/rtc0/name 

with the expected results being:

lrwxrwxrwx    1 root     root             9 Jan  4 11:11 /dev/rtc -> /dev/rtc0
crw-rw----    1 root     root      254,   0 Jan  4 11:11 /dev/rtc0

ds1338

Setting the RTC

The hwclock command version on the current SDK version of Busybox supports writing the date/time to the RTC clock using the system date/time.

First we will need to set the system date/time:

date -u [MMDDhhmm[[CC]YY][.ss]]

for example, to set the date/time to Thu Set 1 2011 9:13:30

date -u 090109132011.30

once this is done, we can set up the RTC date/time using:

hwclock -u -w

Displaying the RTC time

To show the date/time stored on the RTC clock use:

while sleep 1 ; do
    hwclock
done

with the output being similar to

Thu Sep  1 09:13:56 2011  0.000000 seconds
Thu Sep  1 09:13:57 2011  0.000000 seconds
Thu Sep  1 09:13:59 2011  0.000000 seconds

Setting Linux wall clock on boot

On boot time, Linux will take care of reading the information stored on the chip and use it as the system date/time.

APIs for interacting with the RTC

To interact with the RTC the corresponding RTC file needs to be opened

int rtc = open("/dev/rtc", O_RDWR);

then use the ioctls defined on

$DEVDIR/kernel/linux*/include/linux/rtc.h

to set/read the time the used will be interested on these specific ioctls

RTC_SET_TIME
RTC_READ_TIME

NVRAM

If your RTC chip supports NVRAM, it will be exposed as a sysfs file named nvram. To find the file, run

find /sys -name nvram

and you will get output like

/sys/devices/platform/i2c_davinci.1/i2c-1/1-0068/nvram

If your busybox configuration includes

CONFIG_OD=y

then you can dump the contents of NVRAM using

od -x /sys/devices/platform/i2c_davinci.1/i2c-1/1-0068/nvram

and you will get output similar to

0000000     d67a    8472    5de8    b5d6    7e1d    10da    4aef    8016
0000020     26df    1027    56a5    b814    61ca    5214    1259    5aca
0000040     ba49    5930    e452    a730    f653    ec0a    f0bb    2ad0
0000060     88dc    657e    3468    9be8

You can test that you can write to NVRAM using

echo "123456789a123456789b123456789c123456789d123456789e1234" > /sys/devices/platform/i2c_davinci.1/i2c-1/1-0068/nvram
Navigation
Toolbox