Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save emmiedev/a08002e1c662447bbf698d230b922bd4 to your computer and use it in GitHub Desktop.

Select an option

Save emmiedev/a08002e1c662447bbf698d230b922bd4 to your computer and use it in GitHub Desktop.
The use of the I2C DS3231/3232 from Armbian SBC has a few quirks, most tutorials on the internet works but only if you have internet connection.
In cuba we can sometimes use Armbian on devices that are not internet connected, so most tutorial fails and after a power failure or reboot you get a non working system because some apps/soft warns and fail with a "date in the future" message.
I have crafted a way to use it as a real systemd service (yes, it's a legacy service...)
Create a file named: rtc_ds1307 in /etc/init.d/ you can use the command below (root privileges are required)
```
nano /etc/init.d/rtc_ds1307
```
And paste the text below:
=============================================================
#! /bin/sh
### BEGIN INIT INFO
# Provides: rtc_ds1307
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: DS1307 real-time clock usage script
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d.
### END INIT INFO
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="ds1307_rtc maintenance service"
do_start() {
echo "Selecting correct RTC instance "
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
ln -f -s /dev/rtc1 /dev/rtc
rm -f /dev/rtc0
echo "Syncing system time to RTC"
hwclock -s
}
do_stop() {
echo "Syncing RTC to system time"
hwclock -w
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
status)
echo "RTC time:"
hwclock -r
echo "System time:"
date
;;
restart|force-reload)
do_stop
;;
*)
echo "Usage: rtc_ds1307 {start|stop|status|restart}" >&2
exit 3
;;
esac
=============================================================
Save it, just one comment, depending on your board and the I2C bus you use can may want to change the line than says:
```
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
```
Take note, in this one we use the I2C number ZERO
```
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
```
In this one we use the I2C number ONE.
After this you need to disable some services about the rtc in systemd and then enable the new service, you can accomplish that by issuing this commands:
```
systemctl disable fake-hwclock
systemctl disable hwclock
systemctl enable rtc_ds1307
```
If you have internet you may have good time by now, check it with:
```
date
```
If you have a bad date set it up with: (adjust your date to actual values)
```
date -s "January 16, 2020 11:54:00"
```
Now we will burn the date into the rtc.
```
hwclock -w
```
That's it, reboot or force a power failure, on reboot you will have good time on your board.
A plus: if you have internet while the service is stopping it will update the system clock to the hardware clock aka: the rtc. So every time yo issue a `reboot` or `poweroff` you will be saving the system time to the rtc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment