1

On a Raspberry Pi 3 B+ I'm trying to read from a DS3231 RTC at boot time, sync the time to the system time and then unload the device. All the commands function from the terminal so I thought it would be easy enough to put them in /etc/rc.local or /etc/xdg/lxsession/LXDE-pi/autostart, but in both cases the device is registered, the clock syncs, but the device does not unload. I need to unload the device because it's no longer needed and I have a C++ GUI (QT) which accesses the temperature sensor on the RTC chip and this is blocked if the device is registered elsewhere.

The commands being run are:-

echo ds1307 0x68 | sudo tee /sys/class/i2c-adapter/i2c-1/new_device
sudo hwclock -s
echo 0x68 | sudo tee /sys/class/i2c-adapter/i2c-1/delete-device

After booting, the hwclock can still be accessed and the device can be deleted with the last command shown above when run in the terminal. Am I missing something blindingly obvious?

  • Are you using dt driver overlay for the DS3231 RTC? I think the driver is loaded at boot time, and cannot be unloaded until next boot. I read that a kernel module can be loaded and unloaded. But I am not very sure. I once used python to control the RTC, and therefore no such problem. – tlfong01 Jul 18 at 11:36
  • No. I did use that driver overlay first and indeed found it cannot be unloaded (or at least couldn't find how to). With the method I describe here you can merrily load and unload the device from the command line at will. If you load the driver (first line) you can access the hwclock. If you unload it (3rd line) you cannot access hwclock and the DS3231 can be accessed from other programmes without conflict (which is my desire). So no other drivers or overlays are needed to be installed other than what I describe here to get access to the hwclock. – AntonyCD Jul 18 at 11:55
  • Try sudo rmmod rtc_ds1307 or whatever the driver name is. Then delete the device. – Dmitry Grigoryev Jul 18 at 12:26
  • @AntonyCD,Thank you for letting me know that drivers loaded at boot time can be actually be unloaded using terminal commands. I must confess that I know too little about your auto unload method to understand you problem. As I said earlier, I did not use any driver but use python to directly initialize DS3231, and sync Network time. So I did not have your problem. – tlfong01 Jul 18 at 12:36
  • 1
    @Dmitry Grigorev, Good thinking, but it does the same. It will delete the driver when used from the command line but if I add it to the boot script it doesn't, the driver is still loaded (which I can then delete by running sudo rmmod rtc_ds1307 in the terminal). – AntonyCD Jul 18 at 13:15
1

The ONLY thing you have to do is include in /boot/config.txt:

dtoverlay=i2c-rtc,ds3231

DO NOT fiddle with the software or attempt to manually synchronise - it all happens AUTOMATICALLY.

PS a DS3231 is NOT a ds1307 although they both use the same driver.

Without code it is unclear what you expect to get from the temp sensor which is principally for increasing the accuracy of the RTC.

If you have written code to access the RTC it is comparatively simple to read the time and set the system clock.

Don’t bother with the driver.

  • I know this method for adding the overlay and allowing the automatic synchronisation. The question wasn't about how to install the RTC but how to remove the driver after it has performed its function in my application. There's no code to show other than what I describe above, my GUI application is not relevant to the question. If I want to read the temperature sensor in my application then that's my business isn't it? I actually have the DS3231 mounted on a PCB which connects to a custom interface and I want to monitor the board temperature without having to add another temperature sensor. – AntonyCD Jul 18 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.