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.pTxCb = NULL; // empty_tx_cb;
53 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);
54
55 // increment sequenceID
56 delReqHeader.sequenceID = (S.bmca.state == PTP_BMCA_SLAVE) ? (++S.slave.messaging.delay_reqSequenceID) : (++S.master.pdelay_reqSequenceID);
57 delReqHeader.domainNumber = S.profile.domainNumber;
58
59 // fill in header
61
62 // fill in timestamp
63 ptp_write_binary_timestamps(delReqMsg.data, &zeroTs, 1);
64
65 // send message
66 ptp_transmit_enqueue(&delReqMsg);
67}
68
70 // fetch header from sent PDelay_Resp message
71 PtpHeader header;
72 ptp_extract_header(&header, pMsg->data);
73 TimestampI t3 = pMsg->ts;
74
75 // modify header fields
76 header.messageType = PTP_MT_PDelay_Resp_Follow_Up; // change message type
77 ptp_clear_flags(&header.flags); // clear flags
79 header.minorVersionPTP = 0;
80
81 // write fields
83 ptp_write_binary_timestamps(pdelRespFUpMsg.data, &t3, 1); // t3 TIMESTAMP
84 uint32_t reqPortIdOffset = PTP_HEADER_LENGTH + PTP_TIMESTAMP_LENGTH;
85 memcpy(pdelRespFUpMsg.data + reqPortIdOffset, pMsg->data + reqPortIdOffset, PTP_PORT_ID_LENGTH);
86
87 // setup packet
92 pdelRespFUpMsg.pTxCb = NULL;
94
95 // MSG("PDelRespFollowUp: %d.%09d\n", (int32_t) t3.sec, t3.nanosec);
96
97 // send!
99}
100
102 // fetch header from received PDelay_Req message
103 PtpHeader header;
104 TimestampI t2 = pMsg->ts;
105 ptp_extract_header(&header, pMsg->data);
106 header.minorVersionPTP = 0; // minorVersionPTP should be fixed to 0
107
108 // create requestingSourcePortIdentity based on clockId from the header
109 PtpDelay_RespIdentification reqDelRespId = {header.clockIdentity, header.sourcePortID};
110
111 // modify header fields
112 header.messageType = PTP_MT_PDelay_Resp; // change message type
113 ptp_clear_flags(&header.flags); // set flags
114 header.flags.PTP_TWO_STEP = true;
115 header.sourcePortID = PTP_PORT_ID; // set source port number
116 header.logMessagePeriod = 0x7F; // see standard...
117 header.messageLength = PTP_PCKT_SIZE_PDELAY_RESP; // set appropriate size
118 header.clockIdentity = S.hwoptions.clockIdentity; // set our clock identity
119
120 // write fields
121 ptp_construct_binary_header(pdelRespMsg.data, &header); // HEADER
122 ptp_write_binary_timestamps(pdelRespMsg.data, &t2, 1); // t2 TIMESTAMP
123 ptp_write_delay_resp_id_data(pdelRespMsg.data, &reqDelRespId); // REQ.SRC.PORT.ID
124
125 // setup packet
132
133 // MSG("PDelResp: %d.%09d\n", (int32_t)t2.sec, t2.nanosec);
134
135 // send packet
137}
138
139void ptp_compute_mean_path_delay_e2e(const TimestampI *pTs, const uint64_t *pCf, TimestampI *pMPD) {
140 // convert correction field values to timestamp objects
141 TimestampI cf = {0, 0}; // variable for accounting correction fields in
142 nsToTsI(&cf, pCf[T1] + pCf[T2] + pCf[T4]);
143
144 // compute difference between master and slave clock
145 subTime(pMPD, &pTs[T2], &pTs[T1]); // t2 - t1 ...
146 subTime(pMPD, pMPD, &pTs[T3]); // - t3 ...
147 addTime(pMPD, pMPD, &pTs[T4]); // + t4
148 subTime(pMPD, pMPD, &cf); // - CF of (Sync + Follow_Up + Delay_Resp)
149 divTime(pMPD, pMPD, 2); // division by 2
150}
151
152void ptp_compute_mean_path_delay_p2p(const TimestampI *pTs, const uint64_t *pCf, TimestampI *pMPD) {
153 // convert correction field values to timestamp objects
154 TimestampI cf = {0, 0}; // variable for accounting correction fields in
155 nsToTsI(&cf, pCf[T2] + pCf[T3]);
156
157 // compute difference between master and slave clock
158 subTime(pMPD, &pTs[T4], &pTs[T1]); // t4 - t1 ...
159 subTime(pMPD, pMPD, &pTs[T3]); // - t3 ...
160 addTime(pMPD, pMPD, &pTs[T2]); // + t2
161 subTime(pMPD, pMPD, &cf); // - CF of (PDelay_Resp + PDelay_Resp_Follow_Up)
162 divTime(pMPD, pMPD, 2); // division by 2
163}
164
165// --------------
166
169}
void ptp_send_pdelay_resp_follow_up(const RawPtpMessage *pMsg)
Definition: common.c:69
void ptp_common_reset()
Definition: common.c:167
void ptp_compute_mean_path_delay_p2p(const TimestampI *pTs, const uint64_t *pCf, TimestampI *pMPD)
Definition: common.c:152
void ptp_compute_mean_path_delay_e2e(const TimestampI *pTs, const uint64_t *pCf, TimestampI *pMPD)
Definition: common.c:139
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:101
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: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:306
@ PTP_BMCA_SLAVE
Definition: ptp_types.h:314
@ 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
uint32_t size
Packet size.
Definition: ptp_types.h:188
uint8_t data[(128)]
raw packet data
Definition: ptp_types.h:198
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:502
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