Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 7513

Advanced users • Raspberry pi internal clock is erratic / can't force change

$
0
0
Hello all

So for the past 10 months I've been trying to synchronise some Rpi 5s to a dell workstation. I've had mixed success, but in the last few months everything is breaking in unexpected ways. For example ptp4l works on some rpis but not others, and there continue to be low-level kernel bugs that are periodically introduced.

So I'm trying a work around for my application which just requires periodic (e.g. every 5-10 seconds) adjustment to < 1ms precision. There are few hacks including this one which open a socket on the dell workstation:

Code:

# dell_time_server.pyimport socket, times = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)s.bind(('', 9999))print("Dell time server ready on UDP port 9999")while True:    try:        d, a = s.recvfrom(1024)        now = time.time_ns()        s.sendto(str(now).encode(), a)    except Exception as e:        print(f"Error: {e}")
That can actually provide a time value to the Rpis with < 1ms delay:

Code:

cat@raspberrypi:~ $ sudo python3 -c "import socket, time, os; \s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM); s.settimeout(1); \t1 = time.time_ns(); s.sendto(b'x', ('192.168.2.240', 9999)); \d, a = s.recvfrom(1024); t4 = time.time_ns(); t2 = int(d.decode()); \rtt = t4 - t1; adjusted = t2 + rtt // 2; \os.system(f'date -s \"@{adjusted/1e9}\"'); \     <----------- manually force RPI clock to Dell workstation clockt_now = time.time_ns(); \print(f'Dell clock:    {t2/1e9:.9f} s'); \print(f'RPI clock:     {t_now/1e9:.9f} s'); \print(f'RTT: {rtt/1e6:.3f} ms')"Sat Jul 12 07:06:17 PM BST 2025Dell clock:    1752343577.325450420 sRPI clock:     1752343577.325790882 s             <----- post os.system() call we now have < 1ms differencesRTT: 0.296 ms                                       <------------- pretty awesome loop timecat@raspberrypi:~ $ 
So this gets me about half way to a working solution. But ideally I would want the os.system() call above to fix the Rpi clock for at least 30-60 seconds. Instead it seems that the rpi clock start to drift instantly, and goes back to a default which is about 36 seconds behind the Dell workstation. It does this within 10 seconds or so.

You can see this here when i call this sync function Without the os.system() clock setting - about 15 seconds later, the dell and rpi clocks are way off now byt 30 seconds:

Code:

cat@raspberrypi:~ $ sudo python3 -c "import socket, time, os; \s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM); s.settimeout(1); \t1 = time.time_ns(); s.sendto(b'x', ('192.168.2.240', 9999)); \d, a = s.recvfrom(1024); t4 = time.time_ns(); t2 = int(d.decode()); \rtt = t4 - t1; adjusted = t2 + rtt // 2; \t_now = time.time_ns(); \print(f'Dell clock:    {t2/1e9:.9f} s'); \print(f'RPI clock:     {t_now/1e9:.9f} s'); \print(f'RTT: {rtt/1e6:.3f} ms')"Dell clock:    1752343662.294407606 sRPI clock:     1752343625.536979914 s                           <------------- 15 seconds later the rpi clock has gone back to some defaultRTT: 0.310 ms
I've been racking my brain to figure out what's happening and can't seem to figure out. ChatGPT seems to think that ARM has some weird clock system that ignores adjtimex and anything else that I try to do to force the rpi system clock to update.

If anyone has any advice for how I could make the rpi system clock update - I would much appreciate it.

Thanks a bunch,

PS: I've also noticed that there's some weird and random updates to the system clock... but that's another matter.

Code:

cat@raspberrypi:~ $ sudo /home/cat/netholabs/tools/adjtimex --print         mode: 0       offset: 0    frequency: 0     maxerror: 0     esterror: 16000000       status: 8192time_constant: 2    precision: 1    tolerance: 32768000         tick: 11000     raw time:  1752343177s 771552632ns = 1752343177.771552632cat@raspberrypi:~ $ sudo /home/cat/netholabs/tools/adjtimex --print         mode: 0       offset: 0    frequency: 0     maxerror: 500           <----------- NEW VALUE     esterror: 16000000       status: 8192time_constant: 2    precision: 1    tolerance: 32768000[u][/u]         tick: 11000     raw time:  1752343179s 171669522ns = 1752343179.171669522cat@raspberrypi:~ $ sudo /home/cat/netholabs/tools/adjtimex --print         mode: 0       offset: 0    frequency: 0     maxerror: 500     esterror: 16000000       status: 8192time_constant: 2    precision: 1    tolerance: 32768000         tick: 11000     raw time:  1752343180s 501271564ns = 1752343180.501271564cat@raspberrypi:~ $ sudo /home/cat/netholabs/tools/adjtimex --print         mode: 0       offset: 0    frequency: 0     maxerror: 0                         <----------- NEW VALUE     esterror: 16000000       status: 8192time_constant: 2    precision: 1    tolerance: 32768000         tick: 11000     raw time:  1752343181s 785499665ns = 1752343181.785499665cat@raspberrypi:~ $ sudo /home/cat/netholabs/tools/adjtimex --print         mode: 0       offset: 0    frequency: 0     maxerror: 0     esterror: 16000000       status: 8192time_constant: 2    precision: 1    tolerance: 32768000         tick: 11000     raw time:  1752343182s 981714953ns = 1752343182.981714953cat@raspberrypi:~ $ sudo /home/cat/netholabs/tools/adjtimex --print

Statistics: Posted by catubc — Sat Jul 12, 2025 6:15 pm — Replies 5 — Views 178



Viewing all articles
Browse latest Browse all 7513

Trending Articles