flexPTP 1.0
An IEEE 1588 PTP implementation designed for microcontrollers
Loading...
Searching...
No Matches
stats.c
Go to the documentation of this file.
1#include "stats.h"
2
3#include <memory.h>
4#include <math.h>
5
6#include "event.h"
7#include "ptp_core.h"
8#include "ptp_defs.h"
10
11
13#define S (gPtpCoreState)
15
16// clear statistics
18 memset(&S.stats, 0, sizeof(PtpStats));
19 S.stats.filtTimeErr = 100 * PTP_ACCURACY_LIMIT_NS; // prevent strange LOCKED-UNLOCKED-LOCKED series when starting up
20}
21
22// get statistics
24 return &S.stats;
25}
26// filter parameters for statistics calculation
27// WARNING: Data calculation won't be totally accurate due to unvertain sampling time!
28
29#define PTP_TE_FILT_Fc_HZ (0.1)
30
31// collect statistics
32void ptp_collect_stats(int64_t d) {
33 double a = exp(-PTP_TE_FILT_Fc_HZ * 2 * M_PI * (S.slave.messaging.syncPeriodMs / 1000.0));
34
35 // performing time error filtering
36 double y_prev = S.stats.filtTimeErr, y;
37 y = a * y_prev + (1 - a) * d; // filtering equation
38 S.stats.filtTimeErr = y;
39
40 // set locked state
41 bool locked = ((fabs(S.stats.filtTimeErr) < (PTP_ACCURACY_LIMIT_NS)) && (ptp_get_current_master_clock_identity() != 0));
42
43 // print "[LOG-LCKD]" only once
44 bool lockStateChanged = (locked != S.stats.locked);
45 CLILOG(S.logging.logid && S.logging.locked && lockStateChanged, "[LOG-LCKD] ");
46 CLILOG(S.logging.locked && lockStateChanged, "PTP %s!\n", locked ? "LOCKED" : "DIVERGED");
47
48 // invoke LOCKED/UNLOCKED event
49 if (locked != S.stats.locked) {
51 }
52
53 S.stats.locked = locked;
54}
In this module are the core and user events defined.
@ PTP_UEV_LOCKED
The average clock accuracy is sufficient.
Definition: event.h:67
@ PTP_UEV_UNLOCKED
Our clock has deviated from the master in average.
Definition: event.h:68
#define PTP_IUEV(uev)
Definition: event.h:83
#define CLILOG(en,...)
Core of the PTP implementation. Defines functions for message processing, clock tuning,...
In here reside a multitude of fundamental PTP-related constants and definitions.
#define PTP_ACCURACY_LIMIT_NS
Limit of the LOCKED state.
Definition: ptp_defs.h:136
uint64_t ptp_get_current_master_clock_identity()
This module features functions to tweak around the PTP engine's almost every property.
void ptp_clear_stats()
Definition: stats.c:17
#define PTP_TE_FILT_Fc_HZ
Cutoff frequency (Hz)
Definition: stats.c:29
void ptp_collect_stats(int64_t d)
Definition: stats.c:32
const PtpStats * ptp_get_stats()
Definition: stats.c:23
This is the statistics module that gathers data of the operating PTP-engine.
Structure for statistics.
Definition: ptp_types.h:471