Current state of hardware RTC support

Hi

What’s the current state of balenaOS regarding hardware RTCs?
I’m trying to use a DS1307 based RTC on my RaspberryPi.

I found an old thread here: Disabling access to RTC
There it is mentioned that chronyd is managing the system time.
But in the general documentation, it is mentioned that one can enable the RTC with the given dtoverlay configuration: https://www.balena.io/docs/reference/OS/advanced/#setting-device-tree-overlays-dtoverlay-and-parameters-dtparam

Since the RaspberryPi always connects to balenaCloud (and I assume it syncs the time with the server), it is quite difficult to know if the time is taken from the RTC or from NTP.

At least hwclock on the host OS is giving me this error (which lets me assume it is not enabled):

root@<id>:~# hwclock -r
hwclock: can't open '/dev/misc/rtc': No such file or directory

/UncleSam

1 Like

Hi, have you enabled the DC1307’s interface (normally it’s on I2C as much as I can tell). See some example in the docs https://www.balena.io/docs/reference/OS/advanced/#setting-device-tree-overlays-dtoverlay-and-parameters-dtparam

I think you would need to set for your device RESIN_HOST_CONFIG_dtoverlay (or BALENA_HOST_CONFIG_dtoverlay supported on newer OS versions) to "i2c-rtc,ds1307", which enables the i2c-rtc interface and that specific RTC driver…

Could you give this a try, and let us know how it goes?

Thanks for the quick response.

Setting BALENA_HOST_CONFIG_dtoverlay to "i2c-rtc,ds1307" resulted in the RTC working:

root@<id>:~# hwclock -r
Tue Apr 30 06:00:25 2019  0.000000 seconds

Now to the first part of my question above: does this mean, chronyd will use the RTC when the device doesn’t have connection to the balenaCloud? Is there a way for me to verify this?

1 Like

You can run timedatectl and check that the RTC time property is correctly set

Hi, the whole process is decsribed quite well in this documentation:

Regards

1 Like

Thanks @thundron and @nmaas87 for your explanation.

Unfortunately the linked documentation does not mention an external (I2C) RTC at all. My assumption is, that it will work because systemd-timesyncd manages everything and will also use the external RTC.

But I guess, I will have to test the entire setup on a separate disconnected network using a development build and SSH to 22222.

For everybody reading this thread and wondering the same thing I did:

:+1: Yes, the hardware DS1307 based RTC works fine after adding the following setting:
BALENA_HOST_CONFIG_dtoverlay = "i2c-rtc,ds1307"

The RTC will recover the system time (as expected) even if it was disconnected from the Raspberry Pi; of course as long as it has a battery.

Thanks to everybody who helped me implement and verify this!

How would this work with other overlays already defined? Just getting started with my project but according to what I read I had to set RESIN_HOST_CONFIG_dtoverlay = pi3-miniuart-bt so that the serial port would work (currently reading from a sensor). I tried adding a second entry for the 'i2c-rtc,ds3231" but as expected it gave an error message saying the environment variable was already defined. Is there a way to combine the two entries in one overlay or what is the proper way of doing this?

Hi
the use of the RESIN_HOST_CONFIG_dtoverlay parameter is documented here: https://www.balena.io/docs/reference/OS/advanced/#setting-device-tree-overlays-dtoverlay-and-parameters-dtparam . If you have further question feel free to contact us again.
Regards
Thomas

1 Like

Thanks, that worked!

Just a FYI for anyone else stumbling in here - if you have additional dtoverlay parameters you should group these two together separately. For example, note how I’ve separated the touch screen parameters:

"rpi-ft5406,touchscreen-swapped-x-y=1,touchscreen-inverted-x=1","i2c-rtc,ds3231"

I couldn’t get the hwclock to return with the time until separating them in this fashion, as opposed to stringing it all together without quotes. Also just to point out I’m using a different clock so replace ds3231 with whatever chip you’re using.