flexPTP 1.0
An IEEE 1588 PTP implementation designed for microcontrollers
Loading...
Searching...
No Matches
debug_servo.c
Go to the documentation of this file.
1#include "debug_servo.h"
2#include "../ptp_defs.h"
3
4#include <flexptp_options.h>
5#include <stdlib.h>
6#include <string.h>
7
8// --------------
9
10static double skew0; // clock skew offset (ppb)
11static double skew_prev; // clock skew in the previous cycle (ppb)
12static int32_t dt0; // time error offset (ns)
13static int32_t dt_prev; // time error in the previous cycle (ns)
14static int32_t offset_prev; // time error offset in the previous cycle (ns)
15
16static uint64_t cycle; // cycle counter
17
18static double tuning_next_ppb; // tuning effective in the next cycle (ppb)
19static bool tuning_next_cycle_valid; // indicates that the above tuning will be implemented
20
21// ---------------
22
23#ifdef CLI_REG_CMD
24
25#ifndef CMD_FUNCTION
26#error "No CMD_FUNCTION macro has been defined, cannot register CLI functions!"
27#endif
28
29typedef enum {
30 DS_CMDH_TUNE,
31 DS_CMDH_SET_SKEW0,
32 DS_CMDH_SET_DT0,
33 DS_CMDH_N
34} DebugServoCmdHandle;
35
36static int cmd_handles[DS_CMDH_N];
37
38static CMD_FUNCTION(tune) {
39 tuning_next_ppb = atof(ppArgs[0]);
41 MSG("Tuning in next cycle: " PTP_COLOR_BGREEN "%.4f" PTP_COLOR_RESET " ppb\n", tuning_next_ppb);
42 return 0;
43}
44
45static CMD_FUNCTION(skew_offset) {
46 if (argc > 0) {
47 if (!strcmp("last", ppArgs[0])) {
49 } else {
50 skew0 = atof(ppArgs[0]);
51 }
52 }
53 MSG("Skew offset: " PTP_COLOR_BGREEN "%.4f" PTP_COLOR_RESET "ppb\n", skew0);
54 return 0;
55}
56
57static CMD_FUNCTION(time_offset) {
58 if (argc > 0) {
59 if (!strcmp("last", ppArgs[0])) {
60 dt0 = dt_prev;
61 } else {
62 dt0 = atof(ppArgs[0]);
63 }
64 }
65 MSG("Time offset: " PTP_COLOR_BYELLOW "%i" PTP_COLOR_RESET "ns", dt0);
66 return 0;
67}
68
69static void register_cli_cmds() {
70 cmd_handles[DS_CMDH_TUNE] = CLI_REG_CMD("ptp servo tune <tuning>\t\t\tSet relative tuning effective in next cycle", 3, 1, tune);
71 cmd_handles[DS_CMDH_SET_SKEW0] = CLI_REG_CMD("ptp servo skew0 [skew|last]\t\t\tSet or get skew offset (ppb)", 3, 0, skew_offset);
72 cmd_handles[DS_CMDH_SET_DT0] = CLI_REG_CMD("ptp servo dt0 [dt|last]\t\t\tSet or get time offset (ns)", 3, 0, time_offset);
73}
74
75#ifdef CLI_REMOVE_CMD
76static void remove_cli_cmds() {
77 for (uint8_t i = 0; i < DS_CMDH_N; i++) {
78 CLI_REMOVE_CMD(i);
79 }
80}
81#endif
82
83#endif
84
87
88#ifdef CLI_REG_CMD
89 register_cli_cmds();
90#endif
91}
92
94#if defined(CLI_REG_CMD) && defined(CLI_REMOVE_CMD)
95 remove_cli_cmds();
96#endif
97}
98
100 skew0 = 0.0;
101 dt0 = 0.0;
102 dt_prev = 0.0;
103 cycle = 0.0;
104 tuning_next_ppb = 0.0;
105}
106
107float debug_servo_run(int32_t dt, PtpServoAuxInput *pAux) {
108 double tuning_ppb = 0.0;
109
110 // in the very first cycle skip running the filter
111 if (cycle == 0) {
112 goto retain_cycle_data;
113 }
114
115 // calculate input data
116 double skew = ((double)(dt - dt_prev)) / ((double)pAux->measSyncPeriodNs) * 1E+09; // skew
117 double skew_rel = skew - skew0; // substract skew offset
118 int32_t offset = dt - dt0 - skew_rel * pAux->measSyncPeriodNs * 1E-09; // time offset
119 int32_t offset_delta = offset - offset_prev;
120
121 MSG(PTP_COLOR_BGREEN "% 8.4f" PTP_COLOR_BYELLOW " % 12i" PTP_COLOR_BRED " % 12i" PTP_COLOR_RESET "\n", skew_rel, offset, offset_delta);
122
123 offset_prev = offset;
124 skew_prev = skew;
125
126retain_cycle_data:
127 dt_prev = dt;
128
129 cycle++;
130
132 tuning_ppb = tuning_next_ppb;
134
135 MSG("Now tuning " PTP_COLOR_BGREEN "%.4f" PTP_COLOR_RESET "ppb!\n", tuning_ppb);
136 }
137
138 return tuning_ppb;
139}
float debug_servo_run(int32_t dt, PtpServoAuxInput *pAux)
Definition: debug_servo.c:107
static double skew_prev
Definition: debug_servo.c:11
static int32_t dt_prev
Definition: debug_servo.c:13
static uint64_t cycle
Definition: debug_servo.c:16
static double tuning_next_ppb
Definition: debug_servo.c:18
static double skew0
Definition: debug_servo.c:10
static int32_t offset_prev
Definition: debug_servo.c:14
void debug_servo_init()
Definition: debug_servo.c:85
void debug_servo_deinit()
Definition: debug_servo.c:93
static bool tuning_next_cycle_valid
Definition: debug_servo.c:19
void debug_servo_reset()
Definition: debug_servo.c:99
static int32_t dt0
Definition: debug_servo.c:12
#define CLI_REG_CMD(cmd_hintline, n_cmd, n_min_arg, cb)
#define PTP_COLOR_BYELLOW
Bright yellow.
Definition: ptp_defs.h:209
#define PTP_COLOR_BGREEN
Bright green.
Definition: ptp_defs.h:207
#define PTP_COLOR_BRED
Bright red.
Definition: ptp_defs.h:205
#define PTP_COLOR_RESET
Reset colors.
Definition: ptp_defs.h:214
Data to perform a full synchronization.
int64_t measSyncPeriodNs
Measured synchronization period (t1->t1)