How to adjust Hardware RTC clock

From IGEP - ISEE Wiki

Revision as of 13:53, 13 November 2013 by Pau (talk | contribs) (Edit registers)

Jump to: navigation, search
Igep community logo.png This is a work in progress article. Help other developers like you in the IGEP Community by improving it!

Overview

Real time clock (RTC) is a computer clock (most often in the form of an integrated circuit) that keeps track of the current time. RTCs are present in almost any electronic device which needs to keep accurate time. More general information here.

In IGEP Boards, MPU and PMIC contains each one a RTC clock:

  • RTC System clock: system clock is controlled by OMAP processors using its internal RTC peripheral. Every bootup, the default firmware copies the hardware clock data from /dev/rtc to system clock data to keeps clock up to date.
  • RTC Hardware clock: hardware clock is controlled by PMIC IC using its internal RTC peripheral. RTC PMIC can afford two behaviours:
    • RTC battery is not used: PMIC RTC peripheral keeps hardware clock alive when OS isn't running and power source is active. If the power source fails, the RTC Hardware clock will lose its clock.
    • RTC battery is used: PMIC RTC peripheral keeps hardware clock alive when OS isn't running and backup battery powers the backup state as far as the input voltage is high enough.

Simplified RTC diagram

Block diagram from IGEP0033

Requirements

This How-to has been tested with an IGEPv2 RC Board (DM3730 and TPS65950), steps used below can be slightly different for other boards:

Rectify clock drift

IGEP Boards use as a source clock a 32.768kHz crystal. This passive component can add clock drifts due:

  • General drift error
  • Ambient temperature error
  • Aging error
  • ...

Some software tools can be used to compensate these errors. Once you tested your implementation add some rule to cron deamon to automatize it.

Set the date and time via NTP

If your board its is connected to Internet, this can be the most interesting workaround. The network time protocol (NTP) is the current widely accepted standard for synchronizing clocks over the Internet. NTP uses a hierarchical scheme in order to synchronize the clocks in the network.

ntpdate program

Ntpdate program is not installed by default, use zypper to download and install the package:

  • Uninstall ntp deamon:
 zypper rm ntp
  • Install ntpdate
zypper in ntpdate 

Once you installed the program, make sure that IGEP is connected to Internet.

  • Synchronize system clock and hardware clock:
 ntpdate pool.ntp.org; hwclock --systohc 
  • Compare offset between ntp server and your system clock:
 ntpdate -q pool.ntp.org && date 

If your result is positive, it means that your system clock is delayed and viceversa.

ntpd deamon

Under contruction

Rectify systematic error editing PMIC RTC registers

This workaround can be helpful when your board is not connected to Internet. Using this method we are going to:

  • Calculate systematic clock error
  • Rectify systematic error using PMIC RTC registers

Calculate systematic clock error

Every IGEP Board has its own systematic clock error. To find it, we use use ntpdate program to calculate the clock offset added every day:

  • Syncronize your system clock using: ntpdate pool.ntp.org
  • Compare actual offset (it is recommendable wait one or two days to deprecate random clock errors): ntpdate -q pool.ntp.org && date

My IGEP Board was a systematic drift of +3.6 seconds/day aprox.

Rectify systematic error using PMIC RTC registers

Overview

Using as a reference +3.6 seconds/day, now its time to compensated this delayed drift configuring PMIC RTC registers. These registers are:

  • RTC_CTRL_REG: Used to enable RTC compensation.
    • I2c bus: I2C1
    • Address: 0x4b
    • Register: 0x29
  • RTC_COMP_LSB_REG: Less significant bit of drift value (COM_REG) in 16 bit Two's complement.
    • I2c bus: I2C1
    • Address: 0x4b
    • Register: 0x2c
  • RTC_COMP_MSB_REG: Most significant bit of drift value (COM_REG) in 16 bit Two's complement.
    • I2c bus: I2C1
    • Address: 0x4b
    • Register: 0x2d

Calculate COM_REG value

If RTC_CTRL_REG

PMIC RTC peripheral compensates every hour and 1 second the clock drift using the COMP_REG value. Use the next formula to calculate it:

Rtc formula.gif

Now pass your COMP_REG decimal value to 16 bit hexadecimal, in my case was 0X1333. COMP_REG will be added to the RTC 32-kHz counter, the duration of the current second becomes [(32,768 – COMP_REG)/32,768] seconds; therefore, it is possible to compensate the RTC with 1/32,768-second time unit accuracy each hour.

Edit registers

Registers will be edited using the i2c-tools, these registers will be modified until RTC source is not active:

  • Enable Hardware compensation:
 i2cset -f -y 1 0x4b 0x29 0x05 
  • Write RTC_COMP registers:
i2cset -f -y 1 0x4b 0x2d 0x13
i2cset -f -y 1 0x4b 0x2c 0x33 
  • Check register values:
i2cdump -y -f 1 0x4b 

Correct system clock