Skip to content

Commit b4f9c41

Browse files
committed
Issue #1: Automate PTP configuration and services.
1 parent 5f8b9e8 commit b4f9c41

File tree

8 files changed

+164
-18
lines changed

8 files changed

+164
-18
lines changed

example.config.yml

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
# GPSd configuration.
33
gpsd_devices: "/dev/ttyAMA0"
4-
gpsd_options: "-s 38400 -n"
4+
gpsd_baud: "115200"
5+
gpsd_options: "-s 115200 -n"
56

67
# Refclock to be used as a time source.
78
# See: https://chrony-project.org/doc/3.4/chrony.conf.html#refclock
@@ -15,3 +16,27 @@ chrony_allow: "10.0.2.0/24"
1516
# Force i226 eth1 interface to 1 Gbps speed.
1617
# See: https://github.com./geerlingguy/time-pi/issues/2
1718
i226_force_1gbps: true
19+
20+
# PTP 4 Linux options.
21+
ptp4l_ptp_iface: eth1
22+
ptp4l_service_state: started
23+
ptp4l_service_enabled: true
24+
25+
ts2phc_service_state: started
26+
ts2phc_service_enabled: true
27+
ts2phc_logging_level: 7
28+
ts2phc_clock: '/dev/ptp0'
29+
ts2phc_source: 'generic'
30+
ts2phc_extra_opts: '-m'
31+
32+
# Intel IGC driver has a bug - rising and falling edges are always used,
33+
# therefore we need to overrided a couple settings to lock sync.
34+
# See: https://github.com./geerlingguy/time-pi/issues/13
35+
ts2phc_extts_polarity: both
36+
ts2phc_pulsewidth: 100000000
37+
38+
phc2sys_service_state: started
39+
phc2sys_service_enabled: true
40+
phc2sys_clock: 'CLOCK_REALTIME'
41+
phc2sys_source: '/dev/ptp0'
42+
phc2sys_extra_opts: '-O 37 -m'

handlers/main.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
- name: Restart chrony
3+
ansible.builtin.service:
4+
name: chrony
5+
state: restarted
6+
7+
- name: Restart gpsd
8+
ansible.builtin.service:
9+
name: gpsd
10+
state: restarted
11+
12+
- name: Restart NetworkManager
13+
ansible.builtin.service:
14+
name: NetworkManager
15+
state: restarted
16+
17+
- name: Restart ptp4l
18+
ansible.builtin.service:
19+
name: ptp4l
20+
state: restarted
21+
22+
- name: Restart ts2phc
23+
ansible.builtin.service:
24+
name: ts2phc
25+
state: restarted
26+
27+
- name: Restart phc2sys
28+
ansible.builtin.service:
29+
name: phc2sys
30+
state: restarted

main.yml

+2-14
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,8 @@
66
vars_files: ['config.yml']
77

88
handlers:
9-
- name: Restart chrony
10-
ansible.builtin.service:
11-
name: chrony
12-
state: restarted
13-
14-
- name: Restart gpsd
15-
ansible.builtin.service:
16-
name: gpsd
17-
state: restarted
18-
19-
- name: Restart NetworkManager
20-
ansible.builtin.service:
21-
name: NetworkManager
22-
state: restarted
9+
- name: Include various service handlers.
10+
ansible.builtin.include_tasks: handlers/main.yml
2311

2412
tasks:
2513
- name: Configure Raspberry Pi settings.

tasks/ptp.yml

+51-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,54 @@
44
name: linuxptp
55
state: present
66

7-
# TODO: Set up NIC as grandmaster, configure phc2sys, and set up testptp
8-
# See: https://github.com./geerlingguy/time-pi/issues/1
9-
# sudo ptp4l -i eth1 -m
7+
- name: Copy ts2phc systemd unit file into place.
8+
ansible.builtin.template:
9+
src: ts2phc.unit.j2
10+
dest: /etc/systemd/system/ts2phc.service
11+
owner: root
12+
group: root
13+
mode: 0755
14+
notify: Restart ts2phc
15+
16+
- name: Ensure ts2phc service is in the correct state.
17+
ansible.builtin.service:
18+
name: ts2phc
19+
state: "{{ ts2phc_service_state }}"
20+
enabled: "{{ ts2phc_service_enabled }}"
21+
22+
- name: Copy phc2sys systemd unit file into place.
23+
ansible.builtin.template:
24+
src: phc2sys.unit.j2
25+
dest: /etc/systemd/system/phc2sys.service
26+
owner: root
27+
group: root
28+
mode: 0755
29+
notify: Restart phc2sys
30+
31+
- name: Ensure phc2sys service is in the correct state.
32+
ansible.builtin.service:
33+
name: phc2sys
34+
state: "{{ phc2sys_service_state }}"
35+
enabled: "{{ phc2sys_service_enabled }}"
36+
37+
- name: Add ptp4l configuration.
38+
ansible.builtin.template:
39+
src: "templates/ptp4l.conf.j2"
40+
dest: "/etc/ptp4l.conf"
41+
mode: 0644
42+
notify: Restart ptp4l
43+
44+
- name: Copy ptp4l systemd unit file into place.
45+
ansible.builtin.template:
46+
src: ptp4l.unit.j2
47+
dest: /etc/systemd/system/ptp4l.service
48+
owner: root
49+
group: root
50+
mode: 0755
51+
notify: Restart ptp4l
52+
53+
- name: Ensure ptp4l service is in the correct state.
54+
ansible.builtin.service:
55+
name: ptp4l
56+
state: "{{ ptp4l_service_state }}"
57+
enabled: "{{ ptp4l_service_enabled }}"

templates/phc2sys.unit.j2

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[Unit]
2+
Description=Sync PHC to System clock
3+
After=network-online.target
4+
Wants=network-online.target
5+
6+
[Service]
7+
Type=simple
8+
ExecStart=/usr/sbin/phc2sys -s {{ phc2sys_source }} -c {{ phc2sys_clock }} --step_threshold=1 --transportSpecific=1 {{ phc2sys_extra_opts }}
9+
Restart=always
10+
RestartSec=3
11+
12+
[Install]
13+
WantedBy=multi-user.target

templates/ptp4l.conf.j2

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[global]
2+
3+
# See: https://austinsnerdythings.com/2025/02/18/nanosecond-accurate-ptp-server-grandmaster-and-client-tutorial-for-raspberry-pi/
4+
5+
# Log level (0 = disabled, 1 = print to stdout)
6+
verbose 1
7+
8+
# Force hardware timestamping
9+
time_stamping hardware
10+
11+
# Act only as a master. ('serverOnly' on later versions...)
12+
masterOnly 1
13+
14+
# clockClass=6 for GNSS reference
15+
# other classes = https://documentation.nokia.com/srlinux/24-10/books/network-synchronization/ieee-1588-ptp.html
16+
clockClass 6

templates/ptp4l.unit.j2

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[Unit]
2+
Description=Precision Time Protocol (PTP) service
3+
After=network-online.target
4+
Wants=network-online.target
5+
6+
[Service]
7+
Type=simple
8+
ExecStart=/usr/sbin/ptp4l -f /etc/ptp4l.conf -i {{ ptp4l_ptp_iface }}
9+
Restart=always
10+
RestartSec=3
11+
12+
[Install]
13+
WantedBy=multi-user.target

templates/ts2phc.unit.j2

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[Unit]
2+
Description=Timestamp to PHC synchronization service
3+
After=network-online.target
4+
Wants=network-online.target
5+
6+
[Service]
7+
Type=simple
8+
ExecStart=/usr/sbin/ts2phc -c {{ ts2phc_clock }} -s {{ ts2phc_source }} --ts2phc.pin_index 2 --ts2phc.extts_polarity {{ ts2phc_extts_polarity }} --ts2phc.pulsewidth {{ ts2phc_pulsewidth }} -l {{ ts2phc_logging_level }} {{ ts2phc_extra_opts }}
9+
Restart=always
10+
RestartSec=3
11+
12+
[Install]
13+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)