Using LPC4357 On-Chip RTC in Linux Print


This application note explains how to use the LPC4357 on-chip Real Time Clock (RTC) in Linux.

All software tests documented below were performed on the Embedded Artists LPC4357 Dev Kit.

IMPORTANT NOTE: The tests described below work successfully when the Dev Kit board is reset. The LPC4357 maintains the date and time, as expected. However, then the Dev Kit board is power cycled, the RTC does not maintain the date and time and resets to zero values. We have been in contact with the Embedded Artists Support, who explained that, indeed, the RTC may not able to survive a power cycle on their LPC4357 Dev Kit board, due to the fact that the VBAT power implementation in the EA LPC4357 Dev Kit design is not ideal. Please contact the Embedded Artists Support for details. Emcraft validated that the LPC4357 software described below functions as expected, including the ability to survive a power cycle, using a custom LPC4357 board we supported with uClinux in a customer consulting project.

The RTC device driver is linux/drivers/rtc/rtc-lpc178x.c. To enable the RTC support in the kernel, you must enable the CONFIG_RTC_DRV_LPC178X build time option in the kernel configuration. To do so, start the kernel configuration GUI (make kmenuconfig from the project directory) and proceed to enable the following items: Device Drivers -> Real Time Clock -> LPC178X On-Chip RTC:

If you would like to access the RTC using the standard Linux proc and sysfs interfaces, you need to enable the corresponding configuration options in the same menu:

For the sake of completeness, here is a full list of the relevant kernel configuration options enabled in the Linux project used by the tests shown below in this application note:

CONFIG_RTC_LIB=y
CONFIG_RTC_CLASS=y
CONFIG_RTC_HCTOSYS=y
CONFIG_RTC_HCTOSYS_DEVICE="rtc0"
CONFIG_RTC_INTF_SYSFS=y
CONFIG_RTC_INTF_PROC=y
CONFIG_RTC_INTF_DEV=y
CONFIG_RTC_DRV_LPC178X=y

There must be a device node in the target root file system for the RTC to allow accessing it using standard Linux interfaces. Add the following line into your <project>.initramfs file to create a device node for the RTC:

nod /dev/rtc0 0600 0 0 c 254 0

The hwclock Linux utility is typically used to control an RTC from an interactive shell or a shell script. In uClinux, hwclock is available as part of the multi-call busybox utility. To enable hwclock in busybox, run the busybox configuration GUI (make bmenuconfig from the project directory) and proceed to enable the following options:

CONFIG_HWCLOCK=y
CONFIG_FEATURE_HWCLOCK_ADJTIME_FHS=y

To do so, go to Linux System Utilities and enable the two items related to hwclock:

Also, you will need to add a symlink for hwlcock to the target file system. This is done by adding the following line to the <project>.initramfs file:

slink /bin/hwclock busybox 777 0 0

Having updated your project configuration as described above, build the bootable Linux image (<project>.uImage) by running make in the project directory. The procedure described here explains how to install Linux image (uImage) to the target.

When you boot the newly installed Linux image on the target, there will be the following message in the kernel bootstrap print-out indicating that the RTC driver is available and has registered a platform device for the on-chip RTC:

...
rtc-lpc178x rtc-lpc178x: rtc core: registered rtc-lpc178x as rtc0
...

Here is what you should be able to do with the RTC on the target.

First thing to do is to set the system time and date to the right values. The most obvious way to do that is use the date command, for instance:

~ # date -s "2015-05-20 12:30:00"
~ # Wed May 20 12:30:00 UTC 2015
~ #

Another possibility is to use the Internet connectivity (which assumes a configuration with an Ethernet link and TCP/IP stack enabled) in order to get the current time and date from a public Internet server. Refer to Running TCP/IP stack in Linux for information on how to do that.

It is important to understand that the system time is maintained using the LPC4357 TIMER0 timer, which resets itself on each power / reset cycle. It is therefore important to write the system time into the RTC. This is done as follows:

~ # hwclock -w
~ #

Let's read the RTC and the system time back and make sure that they both show the correct time and date:

~ # hwclock; date
Wed May 20 12:32:24 2015 0.000000 seconds
Wed May 20 12:32:26 UTC 2015
~ #

Reset the board, interrupt the U-Boot autoboot sequence and let the kit stay at the U-Boot command line interface for several minutes (or as long as you like). Reset the board again and boot the Linux image. As Linux boots up, the RTC device driver copies the up-to-date time and date from the RTC to the Linux system time. Validate that the Linux time and the RTC clock match the wall clock:

~ # hwclock; date
Wed May 20 12:41:34 2015 0.000000 seconds
Wed May 20 12:41:36 UTC 2015
~ #