BeagleBone® Enhanced Developer Tutorial

How to initialise and set up a service to use the Real Time Clock (RTC) 

A simple step-by-step guide applicable to all SanCloud BeagleBone® Enhanced SBCs in which the optional RTC board is installed 

You will need to add some additional script to periodically synchronise the RTC module when you have a valid network connection.

 

1. Enable the RTC

echo pcf8563 0x51 > /sys/class/i2c-adapter/i2c-0/new_device

 

2. Check the rtc1 has been created:

[email protected]:/home/debian# ls -l /dev/rtc*
lrwxrwxrwx 1 root root      4 Nov 24 22:21 /dev/rtc -> rtc0
crw------- 1 root root 252, 0 Nov 24 22:21 /dev/rtc0
crw------- 1 root root 252, 1 Nov 24 22:35 /dev/rtc1
 

3. Read the time back in the RTC will fail the first time because it has never been set up:

[email protected]:/home/debian# hwclock -r -f /dev/rtc1
[ 912.096315] rtc-pcf8563 0-0051: low voltage detected, date/time is not reliable.
hwclock: ioctl(RTC_RD_TIME) to /dev/rtc1 to read the time failed: Invalid argument

 

4. Check you have the current system time set correctly:

[email protected]:/home/debian# date
Tue 19 Jan 2021 11:22:50 AM UTC
[email protected]:/home/debian#
[email protected]:/home/debian# timedatectl
               Local time: Tue 2021-01-19 11:23:10 UTC
           Universal time: Tue 2021-01-19 11:23:10 UTC
                 RTC time: Tue 2021-01-19 11:23:10
                Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
 

5. Update the RTC time:

[email protected]:/home/debian# hwclock -w -f /dev/rtc1

 

6. Check the time in the RTC:

[email protected]:/home/debian# hwclock -r -f /dev/rtc1
2021-01-19 11:23:32.810635+00:00
 

7. To create a startup script to enable RTC every time:

mkdir /usr/share/rtc_pcf8563
nano /usr/share/rtc_pcf8563/clock_init.sh
 

8. Put the contents in clock_init.sh:

#!/bin/bash
sleep 15
echo pcf8563 0x51 > /sys/class/i2c-adapter/i2c-0/new_device
hwclock -s -f /dev/rtc1
hwclock -w
 

9. Next create the service that will get started on boot:

[email protected]:/home/debian# nano /lib/systemd/system/rtc-pcf8563.service
 

10. Put the following contents in rtc-pcf8563.service:

[Unit]
Description=PFC8563 RTC Service
 
[Service]
Type=simple
WorkingDirectory=/usr/share/rtc_pcf8563
ExecStart=/bin/bash clock_init.sh
SyslogIdentifier=rtc_pcf8563
 
[Install]
WantedBy=multi-user.target
 

11. Enable the service:

[email protected]ebone:/home/debian# systemctl enable rtc-pcf8563.service