Set Host Time
1#!/usr/bin/env python3
2# .+
3# .context : Pyhackrf2_HTime
4# .title : Set Host Time
5# .kind : python script
6# .author : Fabrizio Pollastri <mxgbot@gmail.com>
7# .site : Revello - Italy
8# .creation : 27-Feb-2025
9# .copyright : (c) 2025 Fabrizio Pollastri
10# .license : GNU GPL v2 or higher
11# .description
12# Set the host time into the HackRF SDR. The host unix time
13# in seconds is set into the second counter of the SDR.
14# The host unix time fractional part is set into the tick
15# counter of the SDR.
16# .-
17
18
19import pyhackrf2_htime as ph
20
21import math as mt
22import time as tm
23
24hackrf = ph.HackRF()
25
26# set PPS timer divisor for 10 MHz main clock
27hackrf.divisor = 200000000-1
28
29# set SDR main clock to 10 MHz
30hackrf.clk_freq = 10000000
31
32# avoid working near second transition: set a 0.15s guard around transition.
33host_seconds_dec,host_seconds_int = mt.modf(tm.time())
34adjust = 0.15
35if host_seconds_dec > 0.14:
36 adjust += 1.0
37tm.sleep(adjust - host_seconds_dec)
38
39# set host time into SDR
40host_seconds_dec,host_seconds_int = mt.modf(tm.time())
41hackrf.ticks = int((host_seconds_dec) * hackrf.divisor)
42hackrf.seconds = int(host_seconds_int)
43
44# check host time vs SDR time: int part of seconds must be equal,
45# decimal part difference must not exceed +-0.001 s
46host_seconds_dec,host_seconds_int = mt.modf(tm.time())
47sdr_seconds_dec = hackrf.ticks / hackrf.divisor
48sdr_seconds_int = hackrf.seconds
49if host_seconds_int != sdr_seconds_int:
50 print("ERROR: host(%ld) and sdr(%ld) seconds int part differ" %
51 (host_seconds_int, sdr_seconds_int))
52else:
53 print("host and SDR seconds int part: %ld" % (host_seconds_int,))
54seconds_dec_diff = host_seconds_dec - sdr_seconds_dec
55if mt.fabs(seconds_dec_diff) > 0.001:
56 print("ERROR: host(%lf) and sdr(%lf) secs dec part differ too much" %
57 (host_seconds_dec, sdr_seconds_dec))
58else:
59 print("host(%lf) and sdr(%lf) secs dec part" %
60 (host_seconds_dec, sdr_seconds_dec))
61 print("host - SDR secs dec part: %lf" % (seconds_dec_diff,))
62
63#### END
Pyhackrf2 HTime documentation by Fabrizio Pollastri is licensed under CC BY-SA 4.0