flexPTP 1.0
An IEEE 1588 PTP implementation designed for microcontrollers
Loading...
Searching...
No Matches
common.c
Go to the documentation of this file.
1#include "common.h"
2
3#include "ptp_core.h"
4#include "task_ptp.h"
5
6#include "msg_utils.h"
7#include "ptp_defs.h"
8#include "ptp_types.h"
9
11#define S (gPtpCoreState)
13
17
18// ------------------
19
21 delReqHeader.messageType = (S.profile.delayMechanism == PTP_DM_E2E) ? PTP_MT_Delay_Req : PTP_MT_PDelay_Req;
22 delReqHeader.transportSpecific = (uint8_t)S.profile.transportSpecific;
23 delReqHeader.versionPTP = 2; // PTPv2
25 ((S.profile.delayMechanism == PTP_DM_P2P) ? PTP_TIMESTAMP_LENGTH : 0);
26 delReqHeader.domainNumber = S.profile.domainNumber;
27 ptp_clear_flags(&(delReqHeader.flags)); // no flags
30
31 memcpy(&delReqHeader.clockIdentity, &S.hwoptions.clockIdentity, 8);
32
34 delReqHeader.sequenceID = 0; // will increase in every sync cycle
35 delReqHeader.control = (S.profile.delayMechanism == PTP_DM_E2E) ? PTP_CON_Delay_Req : PTP_CON_Other;
37}
38
40 // PTP message
41 RawPtpMessage delReqMsg = {0};
43 delReqMsg.pTs = (S.bmca.state == PTP_BMCA_SLAVE) ? (&(S.slave.scd.t[T3])) : (&(S.master.scd.t[T1])); // timestamp writeback address
44 delReqMsg.tx_dm = S.profile.delayMechanism;
45 delReqMsg.tx_mc = PTP_MC_EVENT;
46
47 // increment sequenceID
48 delReqHeader.sequenceID = (S.bmca.state == PTP_BMCA_SLAVE) ? (++S.slave.messaging.delay_reqSequenceID) : (++S.master.pdelay_reqSequenceID);
49 delReqHeader.domainNumber = S.profile.domainNumber;
50
51 // fill in header
53
54 // fill in timestamp
55 ptp_write_binary_timestamps(delReqMsg.data, &zeroTs, 1);
56
57 // send message
58 ptp_transmit_enqueue(&delReqMsg);
59}
60
62 // fetch header from sent PDelay_Resp message
63 PtpHeader header;
64 ptp_extract_header(&header, pMsg->data);
65 TimestampI t3 = pMsg->ts;
66
67 // modify header fields
68 header.messageType = PTP_MT_PDelay_Resp_Follow_Up; // change message type
69 ptp_clear_flags(&header.flags); // clear flags
71
72 // write fields
74 ptp_write_binary_timestamps(pdelRespFUpMsg.data, &t3, 1); // t3 TIMESTAMP
75 uint32_t reqPortIdOffset = PTP_HEADER_LENGTH + PTP_TIMESTAMP_LENGTH;
76 memcpy(pdelRespFUpMsg.data + reqPortIdOffset, pMsg->data + reqPortIdOffset, PTP_PORT_ID_LENGTH);
77
78 // setup packet
79 pdelRespFUpMsg.pTs = NULL;
83 pdelRespFUpMsg.pTxCb = NULL;
84
85 // MSG("PDelRespFollowUp: %d.%09d\n", (int32_t) t3.sec, t3.nanosec);
86
87 // send!
89}
90
92 // fetch header from received PDelay_Req message
93 PtpHeader header;
94 TimestampI t2 = pMsg->ts;
95 ptp_extract_header(&header, pMsg->data);
96 header.minorVersionPTP = 0; // minorVersionPTP should be fixed to 0
97
98 // create requestingSourcePortIdentity based on clockId from the header
99 PtpDelay_RespIdentification reqDelRespId = {header.clockIdentity, header.sourcePortID};
100
101 // modify header fields
102 header.messageType = PTP_MT_PDelay_Resp; // change message type
103 ptp_clear_flags(&header.flags); // set flags
104 header.flags.PTP_TWO_STEP = true;
105 header.sourcePortID = PTP_PORT_ID; // set source port number
106 header.logMessagePeriod = 0x7F; // see standard...
107 header.messageLength = PTP_PCKT_SIZE_PDELAY_RESP; // set appropriate size
108 header.clockIdentity = S.hwoptions.clockIdentity; // set our clock identity
109
110 // write fields
111 ptp_construct_binary_header(pdelRespMsg.data, &header); // HEADER
112 ptp_write_binary_timestamps(pdelRespMsg.data, &t2, 1); // t2 TIMESTAMP
113 ptp_write_delay_resp_id_data(pdelRespMsg.data, &reqDelRespId); // REQ.SRC.PORT.ID
114
115 // setup packet
121
122 // MSG("PDelResp: %d.%09d\n", (int32_t)t2.sec, t2.nanosec);
123
124 // send packet
126}
127
128void ptp_compute_mean_path_delay_e2e(const TimestampI *pTs, const uint64_t *pCf, TimestampI *pMPD) {
129 // convert correction field values to timestamp objects
130 TimestampI cf = {0, 0}; // variable for accounting correction fields in
131 nsToTsI(&cf, pCf[T1] + pCf[T2] + pCf[T4]);
132
133 // compute difference between master and slave clock
134 subTime(pMPD, &pTs[T2], &pTs[T1]); // t2 - t1 ...
135 subTime(pMPD, pMPD, &pTs[T3]); // - t3 ...
136 addTime(pMPD, pMPD, &pTs[T4]); // + t4
137 subTime(pMPD, pMPD, &cf); // - CF of (Sync + Follow_Up + Delay_Resp)
138 divTime(pMPD, pMPD, 2); // division by 2
139}
140
141void ptp_compute_mean_path_delay_p2p(const TimestampI *pTs, const uint64_t *pCf, TimestampI *pMPD) {
142 // convert correction field values to timestamp objects
143 TimestampI cf = {0, 0}; // variable for accounting correction fields in
144 nsToTsI(&cf, pCf[T2] + pCf[T3]);
145
146 // compute difference between master and slave clock
147 subTime(pMPD, &pTs[T4], &pTs[T1]); // t4 - t1 ...
148 subTime(pMPD, pMPD, &pTs[T3]); // - t3 ...
149 addTime(pMPD, pMPD, &pTs[T2]); // + t2
150 subTime(pMPD, pMPD, &cf); // - CF of (PDelay_Resp + PDelay_Resp_Follow_Up)
151 divTime(pMPD, pMPD, 2); // division by 2
152}
153
154// --------------
155
158}
void ptp_send_pdelay_resp_follow_up(const RawPtpMessage *pMsg)
Definition: common.c:61
void ptp_common_reset()
Definition: common.c:156
void ptp_compute_mean_path_delay_p2p(const TimestampI *pTs, const uint64_t *pCf, TimestampI *pMPD)
Definition: common.c:141
void ptp_compute_mean_path_delay_e2e(const TimestampI *pTs, const uint64_t *pCf, TimestampI *pMPD)
Definition: common.c:128
static RawPtpMessage pdelRespMsg
whole, compiled PDelay_Resp message
Definition: common.c:15
void ptp_send_delay_req_message()
Definition: common.c:39
static PtpHeader delReqHeader
header for sending Delay_Reg messages
Definition: common.c:14
static RawPtpMessage pdelRespFUpMsg
whole, compiled PDelay_Resp_Follow_Up message
Definition: common.c:16
void ptp_init_delay_req_header()
Definition: common.c:20
void ptp_send_pdelay_resp(const RawPtpMessage *pMsg)
Definition: common.c:91
This module defines messaging functions for both the slave and master modules.
void ptp_write_delay_resp_id_data(void *pPayload, const PtpDelay_RespIdentification *pDRData)
Definition: msg_utils.c:224
void ptp_extract_header(PtpHeader *pHeader, const void *pPayload)
Definition: msg_utils.c:55
void ptp_construct_binary_header(void *pData, const PtpHeader *pHeader)
Definition: msg_utils.c:90
void ptp_write_binary_timestamps(void *pPayload, const TimestampI *ts, uint8_t n)
Definition: msg_utils.c:166
void ptp_clear_flags(PtpFlags *pFlags)
Definition: msg_utils.c:232
This module defines functions that deal with actual PTP messages; they can extract or insert headers,...
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_PCKT_SIZE_PDELAY_RESP_FOLLOW_UP
Size of a PDelay_Resp_Follow_Up message.
Definition: ptp_defs.h:52
#define PTP_HEADER_LENGTH
Length of the PTP header.
Definition: ptp_defs.h:40
#define PTP_TIMESTAMP_LENGTH
Length of a single timestamp.
Definition: ptp_defs.h:41
#define PTP_PORT_ID_LENGTH
Length of the port identification field.
Definition: ptp_defs.h:42
#define PTP_PORT_ID
PTP port ID on the device.
Definition: ptp_defs.h:73
#define PTP_PCKT_SIZE_PDELAY_RESP
Size of a PDelay_Resp message.
Definition: ptp_defs.h:51
#define T2
#define T3
#define T4
#define T1
This module defines the fundamental PTP message and state machine type, flags, bitfields and the PTP ...
@ PTP_CON_Other
Other.
Definition: ptp_types.h:48
@ PTP_CON_Delay_Req
Delay Request.
Definition: ptp_types.h:45
@ PTP_BMCA_SLAVE
Definition: ptp_types.h:289
@ PTP_MC_EVENT
Event Message Class.
Definition: ptp_types.h:153
@ PTP_MC_GENERAL
General Message Class.
Definition: ptp_types.h:154
@ PTP_MT_PDelay_Resp
Peer Delay Response.
Definition: ptp_types.h:33
@ PTP_MT_PDelay_Req
Peer Delay Request.
Definition: ptp_types.h:32
@ PTP_MT_Delay_Req
Delay Request.
Definition: ptp_types.h:31
@ PTP_MT_PDelay_Resp_Follow_Up
Peer Delay Response Follow Up.
Definition: ptp_types.h:36
@ PTP_DM_E2E
End-to-End Delay Mechanism.
Definition: ptp_types.h:137
@ PTP_DM_P2P
Peer-to-Peer Delay Mechanism.
Definition: ptp_types.h:138
Identification carrying Delay_Resp message.
Definition: ptp_types.h:120
bool PTP_TWO_STEP
Two Step.
Definition: ptp_types.h:59
PTP message header structure.
Definition: ptp_types.h:73
int8_t logMessagePeriod
Definition: ptp_types.h:114
uint8_t versionPTP
PTP version.
Definition: ptp_types.h:79
uint8_t messageType
ID.
Definition: ptp_types.h:75
uint64_t clockIdentity
Clock Identity.
Definition: ptp_types.h:102
uint16_t sequenceID
Sequence ID.
Definition: ptp_types.h:108
uint16_t messageLength
Length.
Definition: ptp_types.h:83
uint16_t sourcePortID
Source Port ID.
Definition: ptp_types.h:105
PtpFlags flags
Flags.
Definition: ptp_types.h:92
uint8_t minorVersionPTP
Definition: ptp_types.h:80
uint8_t control
Control.
Definition: ptp_types.h:111
uint64_t correction_ns
Correction nanoseconds.
Definition: ptp_types.h:95
uint8_t domainNumber
Domain.
Definition: ptp_types.h:86
uint32_t correction_subns
Correction subnanoseconds.
Definition: ptp_types.h:96
uint8_t transportSpecific
Transport Specific.
Definition: ptp_types.h:76
Raw PTP message structure.
Definition: ptp_types.h:162
TimestampI ts
Timestamp.
Definition: ptp_types.h:163
PtpDelayMechanism tx_dm
transmit transport type
Definition: ptp_types.h:169
PtpMessageClass tx_mc
transmit message class
Definition: ptp_types.h:170
void(* pTxCb)(const struct RawPtpMessage_ *pMsg)
transmit callback function
Definition: ptp_types.h:168
uint32_t size
Packet size.
Definition: ptp_types.h:164
TimestampI * pTs
pointer to timestamp
Definition: ptp_types.h:167
uint8_t data[(128)]
raw packet data
Definition: ptp_types.h:173
Timestamp (signed)
Definition: timeutils.h:29
bool ptp_transmit_enqueue(const RawPtpMessage *pMsg)
Definition: task_ptp.c:171
The entry point of the whole PTP-implementation. Calling reg_task_ptp() initializes the PTP-engine,...
TimestampI * addTime(TimestampI *r, const TimestampI *a, const TimestampI *b)
Definition: timeutils.c:16
TimestampI * divTime(TimestampI *r, const TimestampI *a, int divisor)
Definition: timeutils.c:31
TimestampI * nsToTsI(TimestampI *r, int64_t ns)
Definition: timeutils.c:61
TimestampI * subTime(TimestampI *r, const TimestampI *a, const TimestampI *b)
Definition: timeutils.c:24