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 "msg_buf.h"
4
5#include <string.h>
6
7#include "ptp_core.h"
8#include "task_ptp.h"
9
10#include "msg_utils.h"
11#include "ptp_defs.h"
12#include "ptp_types.h"
13
15#define S (gPtpCoreState)
17
21
22// ------------------
23
25 delReqHeader.messageType = (S.profile.delayMechanism == PTP_DM_E2E) ? PTP_MT_Delay_Req : PTP_MT_PDelay_Req;
26 delReqHeader.transportSpecific = (uint8_t)S.profile.transportSpecific;
27 delReqHeader.versionPTP = 2; // PTPv2
29 ((S.profile.delayMechanism == PTP_DM_P2P) ? PTP_TIMESTAMP_LENGTH : 0);
30 delReqHeader.domainNumber = S.profile.domainNumber;
31 ptp_clear_flags(&(delReqHeader.flags)); // no flags
35
36 memcpy(&delReqHeader.clockIdentity, &S.hwoptions.clockIdentity, 8);
37
39 delReqHeader.sequenceID = 0; // will increase in every sync cycle
40 delReqHeader.control = (S.profile.delayMechanism == PTP_DM_E2E) ? PTP_CON_Delay_Req : PTP_CON_Other;
42}
43
45 // PTP message
46 RawPtpMessage delReqMsg = {0};
47 delReqMsg.tag = RPMT_DELAY_REQ; // | MSGBUF_TAG_OVERWRITE;
49 // delReqMsg.pTs = (S.bmca.state == PTP_BMCA_SLAVE) ? (&(S.slave.scd.t[T3])) : (&(S.master.scd.t[T1])); // timestamp writeback address
50 delReqMsg.tx_dm = S.profile.delayMechanism;
51 delReqMsg.tx_mc = PTP_MC_EVENT;
52 delReqMsg.tx_mt = delReqHeader.messageType;
53 delReqMsg.pTxCb = NULL; // empty_tx_cb;
54 delReqMsg.ttl = ((S.bmca.state == PTP_BMCA_SLAVE) ? ((S.profile.logDelayReqPeriod == PTP_LOGPER_SYNCMATCHED) ? FLEXPTP_RANDOM_TAGGED_MESSAGE_TTL_TICKS : S.slave.delReqTickPeriod) : S.master.pdelayReqTickPeriod);
55
56 // increment sequenceID
57 delReqHeader.sequenceID = (S.bmca.state == PTP_BMCA_SLAVE) ? (++S.slave.messaging.delay_reqSequenceID) : (++S.master.pdelay_reqSequenceID);
58 delReqHeader.domainNumber = S.profile.domainNumber;
59
60 // fill in header
62
63 // fill in timestamp
64 ptp_write_binary_timestamps(delReqMsg.data, &zeroTs, 1);
65
66 // send message
67 ptp_transmit_enqueue(&delReqMsg);
68}
69
71 // fetch header from sent PDelay_Resp message
72 PtpHeader header;
73 ptp_extract_header(&header, pMsg->data);
74 TimestampI t3 = pMsg->ts;
75
76 // modify header fields
77 header.messageType = PTP_MT_PDelay_Resp_Follow_Up; // change message type
78 ptp_clear_flags(&header.flags); // clear flags
80 header.minorVersionPTP = 0;
81
82 // write fields
84 ptp_write_binary_timestamps(pdelRespFUpMsg.data, &t3, 1); // t3 TIMESTAMP
85 uint32_t reqPortIdOffset = PTP_HEADER_LENGTH + PTP_TIMESTAMP_LENGTH;
86 memcpy(pdelRespFUpMsg.data + reqPortIdOffset, pMsg->data + reqPortIdOffset, PTP_PORT_ID_LENGTH);
87
88 // setup packet
94 pdelRespFUpMsg.pTxCb = NULL;
96
97 // MSG("PDelRespFollowUp: %d.%09d\n", (int32_t) t3.sec, t3.nanosec);
98
99 // send!
101}
102
104 // fetch header from received PDelay_Req message
105 PtpHeader header;
106 TimestampI t2 = pMsg->ts;
107 ptp_extract_header(&header, pMsg->data);
108 header.minorVersionPTP = 0; // minorVersionPTP should be fixed to 0
109
110 // create requestingSourcePortIdentity based on clockId from the header
111 PtpDelay_RespIdentification reqDelRespId = {header.clockIdentity, header.sourcePortID};
112
113 // modify header fields
114 header.messageType = PTP_MT_PDelay_Resp; // change message type
115 ptp_clear_flags(&header.flags); // set flags
116 header.flags.PTP_TWO_STEP = true;
117 header.sourcePortID = PTP_PORT_ID; // set source port number
118 header.logMessagePeriod = 0x7F; // see standard...
119 header.messageLength = PTP_PCKT_SIZE_PDELAY_RESP; // set appropriate size
120 header.clockIdentity = S.hwoptions.clockIdentity; // set our clock identity
121
122 // write fields
123 ptp_construct_binary_header(pdelRespMsg.data, &header); // HEADER
124 ptp_write_binary_timestamps(pdelRespMsg.data, &t2, 1); // t2 TIMESTAMP
125 ptp_write_delay_resp_id_data(pdelRespMsg.data, &reqDelRespId); // REQ.SRC.PORT.ID
126
127 // setup packet
135
136 // MSG("PDelResp: %d.%09d\n", (int32_t)t2.sec, t2.nanosec);
137
138 // send packet
140}
141
142void ptp_compute_mean_path_delay_e2e(const TimestampI *pTs, const uint64_t *pCf, TimestampI *pMPD) {
143 // convert correction field values to timestamp objects
144 TimestampI cf = {0, 0}; // variable for accounting correction fields in
145 nsToTsI(&cf, pCf[T1] + pCf[T2] + pCf[T4]);
146
147 // compute difference between master and slave clock
148 subTime(pMPD, &pTs[T2], &pTs[T1]); // t2 - t1 ...
149 subTime(pMPD, pMPD, &pTs[T3]); // - t3 ...
150 addTime(pMPD, pMPD, &pTs[T4]); // + t4
151 subTime(pMPD, pMPD, &cf); // - CF of (Sync + Follow_Up + Delay_Resp)
152 divTime(pMPD, pMPD, 2); // division by 2
153}
154
155void ptp_compute_mean_path_delay_p2p(const TimestampI *pTs, const uint64_t *pCf, TimestampI *pMPD) {
156 // convert correction field values to timestamp objects
157 TimestampI cf = {0, 0}; // variable for accounting correction fields in
158 nsToTsI(&cf, pCf[T2] + pCf[T3]);
159
160 // compute difference between master and slave clock
161 subTime(pMPD, &pTs[T4], &pTs[T1]); // t4 - t1 ...
162 subTime(pMPD, pMPD, &pTs[T3]); // - t3 ...
163 addTime(pMPD, pMPD, &pTs[T2]); // + t2
164 subTime(pMPD, pMPD, &cf); // - CF of (PDelay_Resp + PDelay_Resp_Follow_Up)
165 divTime(pMPD, pMPD, 2); // division by 2
166}
167
168// --------------
169
172}
void ptp_send_pdelay_resp_follow_up(const RawPtpMessage *pMsg)
Definition: common.c:70
void ptp_common_reset()
Definition: common.c:170
void ptp_compute_mean_path_delay_p2p(const TimestampI *pTs, const uint64_t *pCf, TimestampI *pMPD)
Definition: common.c:155
void ptp_compute_mean_path_delay_e2e(const TimestampI *pTs, const uint64_t *pCf, TimestampI *pMPD)
Definition: common.c:142
static RawPtpMessage pdelRespMsg
whole, compiled PDelay_Resp message
Definition: common.c:19
void ptp_send_delay_req_message()
Definition: common.c:44
static PtpHeader delReqHeader
header for sending Delay_Reg messages
Definition: common.c:18
static RawPtpMessage pdelRespFUpMsg
whole, compiled PDelay_Resp_Follow_Up message
Definition: common.c:20
void ptp_init_delay_req_header()
Definition: common.c:24
void ptp_send_pdelay_resp(const RawPtpMessage *pMsg)
Definition: common.c:103
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:223
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:165
void ptp_clear_flags(PtpFlags *pFlags)
Definition: msg_utils.c:231
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:60
#define PTP_HEADER_LENGTH
Length of the PTP header.
Definition: ptp_defs.h:48
#define PTP_TIMESTAMP_LENGTH
Length of a single timestamp.
Definition: ptp_defs.h:49
#define PTP_PORT_ID_LENGTH
Length of the port identification field.
Definition: ptp_defs.h:50
#define PTP_PORT_ID
PTP port ID on the device.
Definition: ptp_defs.h:132
#define PTP_PCKT_SIZE_PDELAY_RESP
Size of a PDelay_Resp message.
Definition: ptp_defs.h:59
#define FLEXPTP_RANDOM_TAGGED_MESSAGE_TTL_TICKS
Definition: ptp_defs.h:240
#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:56
@ PTP_CON_Delay_Req
Delay Request.
Definition: ptp_types.h:53
@ PTP_LOGPER_SYNCMATCHED
Messaging occurs whenever a Sync arrives.
Definition: ptp_types.h:307
@ PTP_BMCA_SLAVE
Definition: ptp_types.h:315
@ PTP_MC_EVENT
Event Message Class.
Definition: ptp_types.h:161
@ PTP_MC_GENERAL
General Message Class.
Definition: ptp_types.h:162
@ PTP_MT_PDelay_Resp
Peer Delay Response.
Definition: ptp_types.h:41
@ PTP_MT_PDelay_Req
Peer Delay Request.
Definition: ptp_types.h:40
@ PTP_MT_Delay_Req
Delay Request.
Definition: ptp_types.h:39
@ PTP_MT_PDelay_Resp_Follow_Up
Peer Delay Response Follow Up.
Definition: ptp_types.h:44
@ PTP_DM_E2E
End-to-End Delay Mechanism.
Definition: ptp_types.h:145
@ PTP_DM_P2P
Peer-to-Peer Delay Mechanism.
Definition: ptp_types.h:146
@ RPMT_RANDOM
Create a random, unique tag.
Definition: ptp_types.h:181
@ RPMT_DELAY_REQ
(P)Delay_Req tag
Definition: ptp_types.h:183
Identification carrying Delay_Resp message.
Definition: ptp_types.h:128
bool PTP_TWO_STEP
Two Step.
Definition: ptp_types.h:67
PTP message header structure.
Definition: ptp_types.h:81
int8_t logMessagePeriod
Definition: ptp_types.h:122
uint8_t versionPTP
PTP version.
Definition: ptp_types.h:87
uint8_t messageType
ID.
Definition: ptp_types.h:83
uint64_t clockIdentity
Clock Identity.
Definition: ptp_types.h:110
uint16_t sequenceID
Sequence ID.
Definition: ptp_types.h:116
uint16_t messageLength
Length.
Definition: ptp_types.h:91
uint16_t sourcePortID
Source Port ID.
Definition: ptp_types.h:113
PtpFlags flags
Flags.
Definition: ptp_types.h:100
uint8_t minorVersionPTP
Definition: ptp_types.h:88
uint8_t control
Control.
Definition: ptp_types.h:119
uint64_t correction_ns
Correction nanoseconds.
Definition: ptp_types.h:103
uint8_t domainNumber
Domain.
Definition: ptp_types.h:94
uint32_t correction_subns
Correction subnanoseconds.
Definition: ptp_types.h:104
uint8_t transportSpecific
Transport Specific.
Definition: ptp_types.h:84
TimestampI ts
Timestamp.
Definition: ptp_types.h:187
uint32_t tag
unique transmit tag
Definition: ptp_types.h:191
PtpDelayMechanism tx_dm
transmit transport type
Definition: ptp_types.h:194
uint32_t ttl
transmit Time-to-Live in ticks
Definition: ptp_types.h:192
PtpMessageClass tx_mc
transmit message class
Definition: ptp_types.h:195
PtpMessageType tx_mt
transmit message type
Definition: ptp_types.h:196
uint32_t size
Packet size.
Definition: ptp_types.h:188
uint8_t data[(128)]
raw packet data
Definition: ptp_types.h:199
TxCb * pTxCb
transmit callback function
Definition: ptp_types.h:193
Timestamp (signed)
Definition: timeutils.h:33
bool ptp_transmit_enqueue(const RawPtpMessage *pMsg)
Definition: task_ptp.c:549
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:24
TimestampI * divTime(TimestampI *r, const TimestampI *a, int divisor)
Definition: timeutils.c:39
TimestampI * nsToTsI(TimestampI *r, int64_t ns)
Definition: timeutils.c:69
TimestampI * subTime(TimestampI *r, const TimestampI *a, const TimestampI *b)
Definition: timeutils.c:32