From 2c9470003fe56151cbbb78cce42f071c9a0bf61b Mon Sep 17 00:00:00 2001 From: Dave Koberstein Date: Sun, 15 Oct 2023 14:53:35 -0700 Subject: [PATCH 1/3] Wake up the clock before loading rtc_ds1307 module Under Fedora 39 with kernel 6.5.6, loading the module rtc_ds1307 does not result in a functioning RTC. The clock needs to be read (woken up?) before the module is loaded. This change adds a read of the clock with a timeout. In addition, the /dev/rtc device file takes a fraction of a second to load so the test for its existence alway fails. A 1 second sleep gives time for the file to be created. It's not clear to me if eventually this will be required for Raspberry OS when it eventually gets to the 6.5.6 kernel. However it seems this change should always work on kernel 5. Also fix a misspelling of module in the success message. --- Software/Source/src/pijuice_sys.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Software/Source/src/pijuice_sys.py b/Software/Source/src/pijuice_sys.py index 18b791c0..eb40dc5c 100644 --- a/Software/Source/src/pijuice_sys.py +++ b/Software/Source/src/pijuice_sys.py @@ -381,12 +381,21 @@ def main(): if ret != 0: print('Remove rtc_ds1307 module failed', flush=True) else: + print('Wake up RTC hardware', flush=True) + timeout, pause = 100, 0.1 + while pijuice.rtcAlarm.GetTime()['error'] != 'NO_ERROR' and timeout > 0: + time.sleep(pause) + timeout -= pause + if timeout <= 0: + print('RTC hardware not responding, try to load rtc_ds1307 anyway', flush=True) + ret = os.system('sudo modprobe rtc_ds1307') if (ret != 0): print('Reload rtc_ds1307 module failed', flush=True) else: + time.sleep(1) # give udev time to create the device if os.path.exists('/dev/rtc'): - print('rtc_ds1307 mdule reloaded and RTC os-support OK', flush=True) + print('rtc_ds1307 module reloaded and RTC os-support OK', flush=True) else: print('RTC os-support not available', flush=True) From d296c398e00bc535d25a22dc614b5724dcb9dcfa Mon Sep 17 00:00:00 2001 From: Dave Koberstein Date: Thu, 19 Oct 2023 18:41:16 -0700 Subject: [PATCH 2/3] Set RTC to system time if it was reset to 0 When the RTC is reset to 0, for example from a loss of battery, it wreaks havoc on the systemd log and probably other subsystems. systemd will set the system time based on a recent file timestamp in the filesystem (how it does this depends if chrony or systemd.timesync is being used). But then when rtc_ds1307 loads, the kernel module sets the system time to the RTC's Jan 1, 2000. If network time is running it will eventually figure it out but the logs are messed up with the huge jumps in time. The best place to address this is just before the modprobe of rtc_ds1307. If the year is 2000, then most certainly the RTC has been reset. The convenient SetTime() method sets it to the current system time which is most certainly much closer to the correct time than year 2000. --- Software/Source/src/pijuice_sys.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Software/Source/src/pijuice_sys.py b/Software/Source/src/pijuice_sys.py index eb40dc5c..333fccfa 100644 --- a/Software/Source/src/pijuice_sys.py +++ b/Software/Source/src/pijuice_sys.py @@ -387,7 +387,12 @@ def main(): time.sleep(pause) timeout -= pause if timeout <= 0: - print('RTC hardware not responding, try to load rtc_ds1307 anyway', flush=True) + print('RTC hardware not responding, trying to load rtc_ds1307 anyway', flush=True) + else: + if pijuice.rtcAlarm.GetTime()['data']['year'] == 2000: + print('RTC clock was reset to 0. Possible battery loss.') + print('Setting RTC to current system time') + pijuice.rtcAlarm.SetTime(None) ret = os.system('sudo modprobe rtc_ds1307') if (ret != 0): From 68f4b4b3f6971709b76fb99312b996cbd49e5479 Mon Sep 17 00:00:00 2001 From: Dave Koberstein Date: Sun, 5 Nov 2023 14:52:22 -0800 Subject: [PATCH 3/3] Clean up pid file and a couple of helpful messages The pid file doesn't get cleaned up when the service is stopped and also doesn't get cleaned up on an uninstall. Remove the pid file in the "stop" routine. Add a couple of helpful status messages regarding starting and looking for the RTC. --- Software/Source/src/pijuice_sys.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Software/Source/src/pijuice_sys.py b/Software/Source/src/pijuice_sys.py index 333fccfa..faa8a7bd 100644 --- a/Software/Source/src/pijuice_sys.py +++ b/Software/Source/src/pijuice_sys.py @@ -39,8 +39,8 @@ noPowCnt = 0 # 0 will trigger at boot without power, 3 will not trigger at boot without power PowCnt = 3 # 0 will trigger at boot with power, 3 will not trigger at boot with power dopoll = True -PID_FILE = '/tmp/pijuice_sys.pid' -HALT_FILE = '/tmp/pijuice_halt.flag' +PID_FILE = '/run/pijuice/pijuice_sys.pid' +HALT_FILE = '/run/pijuice/pijuice_halt.flag' I2C_ADDRESS_DEFAULT = 0x14 I2C_BUS_DEFAULT = 1 @@ -284,6 +284,7 @@ def _LoadConfiguration(): bus = configData['board']['general']['i2c_bus'] pijuice = PiJuice(bus, addr) except: + print("Failed to connect to PiJuice with: {}".format(configPath)) sys.exit(0) try: @@ -352,6 +353,7 @@ def main(): pijuice.power.SetPowerOff(powerOffDelay) except ValueError: pass + os.remove(PID_FILE) sys.exit(0) # First check if rtc is operational when the rtc_ds1307 module is loaded. @@ -403,6 +405,8 @@ def main(): print('rtc_ds1307 module reloaded and RTC os-support OK', flush=True) else: print('RTC os-support not available', flush=True) + else: + print('RTC os-support not available', flush=True) if watchdogEn: _ConfigureWatchdog('ACTIVATE')