linux icon indicating copy to clipboard operation
linux copied to clipboard

(ds1307/ds3231) hwclock: ioctl(3, RTC_UIE_ON, 0): Invalid argument

Open boatbodger opened this issue 7 years ago • 12 comments

Describe the bug When running hwclock on a Pi 4 with a DS3231 RTC, an "invalid argument" error occurs Waiting for clock tick... `ioctl(3, RTC_UIE_ON, 0): Invalid argument'

Additional context Ultimately my problem with reading the clock was being caused by noise on the ground connection between the R-Pi and the clock chip. The 'Invalid argment' is still occurring, but is non-fatal - many thanks to pelwell for the explanation below.

boatbodger avatar Oct 11 '19 18:10 boatbodger

The error is caused by the lack of an interrupt signal from the RTC - the I and the E in RTC_UIE stand for Interrupt Enable. As you've discovered, that shouldn't stop the basic RTC functions working.

pelwell avatar Oct 12 '19 13:10 pelwell

I had the same error, and for me the cause was that the initial value for the month was 0x1f, which is neither BCD (as it is supposed to) nor a value somewhere within the proper range 8yes it is accidentaly decimal 31, but it was the month field). I finally took a python script that could write the system date and time via /dev/i2c, from this moment on everything worked as expected.

qjayjayp avatar Apr 15 '21 15:04 qjayjayp

@qjayjayp thanks for posting your note, as I was concerned about the same. What data is it that you're sending via i2c? Would you mind linking to your python script?

thrasher avatar Aug 26 '21 16:08 thrasher

@thraser Yes I looked it up, I used this old script from, 2013 (@XiErCh): https://gist.github.com/sorz/6049584 and commented in the 'write now' line 208. I've seen the code may got some improvements, it is mentioned here (https://www.switchdoc.com/2014/07/python-driver-ds1307-real-time-clock/) and put into a lib here (https://github.com/switchdoclabs/RTC_SDL_DS1307), but you can still use it the same way (ds1307.write_now()).

qjayjayp avatar Aug 26 '21 17:08 qjayjayp

Why has this been closed? This is an issue when using Chrony, for example: chronyd[383]: Could not enable RTC interrupt : Invalid argument This pevents chrony from measuring the RTC clock drift and as a result from acting against it. The DS3231 is capapble of emitting a 1 Hz signal (which corresponds to "interrupt on every clock update, for RTCs that support this once-per-second interrupt") on the SQW pin. Uptream DT even mentions it but doesn't tell how to route it to a GPIO pin and from there back to the driver: https://github.com/torvalds/linux/blob/master/Documentation/devicetree/bindings/rtc/maxim%2Cds3231.txt - Raspberry DT completely ignores the #clock-cells parameter.

V10lator avatar Sep 26 '21 12:09 V10lator

Answering in the Forums - please don't cross-post.

pelwell avatar Sep 27 '21 08:09 pelwell

@pelwell : Which forum is that answer in, please?

I've almost the same error (ioctl(4, RTC_UIE_ON, 0): Invalid argument), dmesg says, [ 6.579872] rtc-ds1307 0-0068: 'wakeup-source' is set, request for an IRQ is disabled!, and I do have the wakeup-source param set in /boot/config.txt: dtoverlay=i2c-rtc,ds3231,wakeup-source,i2c0 . My system is RPi OS buster, RPi 4B

I suspected this was as it should be, since I initially had no value written to the wakealarm folder for rtc0. However, I've had to drop that theory. FWIW, my reasoning follows:

From the DS3231 data sheet: On the DS3231, SQW and INT# share the same pin. INT# is the default at power up, but at least one of the Alarms must be enabled/set. Since the driver only supports Alarm1, registers 07h to 0Ah must be written to enable the interrupt function... obviously you won't get an interrupt if an alarm time isn't set!

To test this I wrote a value to sysfs as follows:

$ date '+%s' -d '+ 30 minutes' | sudo tee /sys/class/rtc/rtc0/wakealarm 
1646036519
$ cat wakealarm
1646036519 

### run hwclock w/ wakealarm set

$ sudo hwclock -rv
hwclock from util-linux 2.33.1
System Time: 1646034782.768757
Trying to open: /dev/rtc0
Using the rtc interface to the clock.
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
ioctl(4, RTC_UIE_ON, 0): Invalid argument 
Waiting in loop for time from /dev/rtc0 to change
...got clock tick
Time read from Hardware Clock: 2022/02/28 07:53:04
Hw clock time : 2022/02/28 07:53:04 = 1646034784 seconds since 1969
Time since last adjustment is 1646034784 seconds
Calculated Hardware Clock drift is 0.000000 seconds
2022-02-28 01:53:03.240644-06:00 

### NOTE that same error received above w/ valid wakealarm time set 

$ dmesg | grep -E '(i2c|rtc)' | head -20
[    3.734395] i2c /dev entries driver
[    5.528018] i2c i2c-22: Added multiplexed i2c bus 0
[    5.533458] i2c i2c-22: Added multiplexed i2c bus 10
[    6.579872] rtc-ds1307 0-0068: 'wakeup-source' is set, request for an IRQ is disabled!
[    6.592120] rtc-ds1307 0-0068: registered as rtc0
[    6.594601] rtc-ds1307 0-0068: setting system clock to 2022-02-28T05:34:16 UTC (1646026456)

I don't have a pullup connected to the IRQ# line on the RTC, but I don't think that matters. And I haven't actually tested the alarm function, but the error is troubling on its own. I'm at a loss to resolve the ioctl(4, RTC_UIE_ON, 0): Invalid argument error.

EDIT: I changed to dtoverlay=i2c-rtc,ds3231,i2c0 to remove the wakeup-source parameter. I rebooted the machine. I continue to get the exact same complaint ioctl(4, RTC_UIE_ON, 0): Invalid argument from hwclock, but the dmesg line rtc-ds1307 0-0068: 'wakeup-source' is set, request for an IRQ is disabled! disappeared. That would suggest the two complaints are unrelated, but ... So - still need some resolution. Thnx

seamusdemora avatar Feb 28 '22 06:02 seamusdemora

When compiling a customer kernel with CONFIG_RTC_INTF_DEV_UIE_EMUL, the error 'hwclock: ioctl(3, RTC_UIE_ON, 0): Invalid argument' is gone.

by avatar Apr 17 '22 14:04 by

@qjayjayp

[...] the initial value for the month was 0x1f [...] decimal 31

That could also be -1 expressed as a 5-bit 2's-complement number, perhaps trying to indicate that the month isn't available?

kurahaupo avatar Nov 29 '22 01:11 kurahaupo

Why has this been closed? This is an issue when using Chrony, for example: chronyd[383]: Could not enable RTC interrupt : Invalid argument This pevents chrony from measuring the RTC clock drift and as a result from acting against it.

has this ever been solved? I have the exact same issue with an uputronics gps hat with rv3028.

I couldn't find a follow-up discussion on the forums...

alexdelprete avatar Feb 16 '23 16:02 alexdelprete

I closed it because in my case, the RTC module I was using did not have an interrupt output connected - and as pelwell explained, the error message was effectively a warning about that being the case (albeit not a very obvious way of saying it). For me, therefore, there was no bug to fix. My use case did not need the RTC to be able to create interrupts. If you think you need an interrupt, make sure your hardware actually has one connected, and then fiddle with the devicetree to enable it.

boatbodger avatar Feb 16 '23 17:02 boatbodger

When compiling a customer kernel with CONFIG_RTC_INTF_DEV_UIE_EMUL, the error 'hwclock: ioctl(3, RTC_UIE_ON, 0): Invalid argument' is gone.

Thanks a lot. I compiled 6.1.11 and it worked perfectly. I can finally use chrony RTC drift monitoring functionality.

alexdelprete avatar Feb 18 '23 06:02 alexdelprete