AM263x MCU+ SDK  10.00.00
icss_timeSync.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 Texas Instruments Incorporated
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef ICSS_TIMESYNC_H_
34 #define ICSS_TIMESYNC_H_
35 
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #endif
40 
41 
42 /* ========================================================================== */
43 /* Include Files */
44 /* ========================================================================== */
45 #include <stdlib.h>
46 #include <math.h>
48 #include "icss_timeSyncApi.h"
49 
50 /* ========================================================================== */
51 /* Error Codes */
52 /* ========================================================================== */
56 #define TIME_SYNC_OK (1)
57 
60 #define TIME_SYNC_UNABLE_TO_CREATE_TASK (-1)
61 
64 #define TIME_SYNC_UNABLE_TO_CREATE_SEMAPHORE (-2)
65 
68 #define TIME_SYNC_UNABLE_TO_CREATE_INTERRUPT (-3)
69 
72 #define TIME_SYNC_UNABLE_TO_ALLOC_MEM (-4)
73 
76 #define TIME_SYNC_UNABLE_TO_CREATE_CLOCK (-5)
77 
80 #define TIME_SYNC_UNABLE_TO_CREATE_MAILBOX (-6)
81 
84 #define TIME_SYNC_UNABLE_TO_CREATE_EVENT (-7)
85 
88 #define TIME_SYNC_UNSUPPORTED_FORMAT (-8)
89 
92 #define TIME_SYNC_EDMA_INIT_FAILED (-9)
93 
96 #define TIME_SYNC_FEATURE_NOT_ENABLED (-10)
97 
100 #define TIME_SYNC_HANDLE_NOT_INITIALIZED (-11)
101 
102 
103 /* ========================================================================== */
104 /* Macros & Typedefs */
105 /* ========================================================================== */
110 #define DEFAULT_PDELAY_REQ_LOG_INTERVAL 3
111 
115 #define DEFAULT_SYNC_SEND_LOG_INTERVAL 0
116 
120 #define DEFAULT_ANNOUNCE_SEND_LOG_INTERVAL 1
121 
125 #define DEFAULT_ANNOUNCE_TIMEOUT_LOG_INTERVAL 3
126 
131 #define TIMESYNC_SYNC_ISR_MASK_P1 0x400000
132 
137 #define TIMESYNC_SYNC_ISR_MASK_P2 0x2000000
138 
139 /*
140  * \def TIMESYNC_TX_TS_ISR_MASK_P1
141  * Interrupt mask for PRU event 23
142  */
143 #define TIMESYNC_TX_TS_ISR_MASK_P1 0x800000
144 
148 #define TIMESYNC_TX_TS_ISR_MASK_P2 0x1000000
149 
154 #define PTP_SYNC_MSG_ID 0x00
155 
159 #define PTP_DLY_REQ_MSG_ID 0x01
160 
164 #define PTP_PDLY_REQ_MSG_ID 0x02
165 
169 #define PTP_PDLY_RSP_MSG_ID 0x03
170 
174 #define PTP_FOLLOW_UP_MSG_ID 0x08
175 
179 #define PTP_DLY_RESP_MSG_ID 0x09
180 
184 #define PTP_PDLY_RESP_FLW_UP_MSG_ID 0x0A
185 
189 #define PTP_ANNOUNCE_MSG_ID 0x0B
190 
194 #define PTP_MGMT_MSG_ID 0x0D
195 
200 #define PTP_FLW_UP_CTRL_MSG_ID 0x02
201 
206 #define SEC_TO_NS 1000000000
207 
212 #define FILTER_ALPHA_COEFF 0.85
213 
218 #define PTP_SYNC0_PERIOD_DIVIDER 4
219 
225 #define OFFSET_THRESHOLD_FOR_RESET 10000
226 
232 #define STABLE_FILTER_THRESHOLD 100
233 
243 #define TIMESYNC_PEER_DELAY_ERROR_THRESHOLD 10000
244 
250 #define NUM_SYNC_MISSED_THRESHOLD 3
251 
256 #define AVG_NUM_DELAY_MEASUREMENTS 3
257 
258 /*Hex Offsets for different fields in PTP Delay Req/Res Packet. Annex E*/
259 /*To get Annex F offsets subtract ANNEX_D_ANNEX_F_DIFF from it*/
260 
265 #define SRC_MAC_OFFSET 6
266 
270 #define IP_DSCP_OFFSET 15
271 
275 #define SRC_IP_OFFSET 26
276 
280 #define DST_IP_OFFSET 30
281 
285 #define IP_CHKSUM_OFFSET 40
286 
290 #define PTP_MSG_ID_OFFSET 42
291 
295 #define PTP_DOMAIN_NUM_OFFSET 46
296 
300 #define PTP_FLAG_OFFSET 48
301 
305 #define PTP_CORRECTION_OFFSET 50
306 
310 #define PTP_SRC_CLK_IDENTITY 62
311 
315 #define PTP_SRC_PORT_ID_OFFSET 70
316 
320 #define PTP_SEQ_ID_OFFSET 72
321 
326 #define PTP_CONTROL_MSG_ID_OFFSET 74
327 
331 #define PTP_LOG_MSG_PERIOD 75
332 
336 #define PTP_REQ_RCPT_TS_SEC_OFFSET 76
337 
341 #define PTP_REQ_RCPT_TS_NSEC_OFFSET 82
342 
346 #define PTP_REQ_SRC_PORT_IDENTITY 86
347 
351 #define PTP_REQ_SRC_PORT_ID 94
352 
357 #define PTP_UTC_OFFSET 86
358 
363 #define PTP_PRIORITY1_OFFSET 89
364 
369 #define PTP_GM_CLK_CLASS_OFFSET 90
370 
375 #define PTP_GM_CLK_ACCU_OFFSET 91
376 
381 #define PTP_GM_CLK_VARIANCE_OFFSET 92
382 
387 #define PTP_PRIORITY2_OFFSET 94
388 
393 #define PTP_GM_CLK_IDENTITY_OFFSET 95
394 
399 #define PTP_STEPS_REMOVED_OFFSET 103
400 
405 #define PTP_TIME_SRC_OFFSET 105
406 
411 #define PTP_E2E_BUFFER_OFFSET 42
412 
416 #define PTP_P2P_BUFFER_OFFSET 14
417 
422 #define ANNEX_D_ANNEX_F_DIFF 28
423 
427 #define HSR_CORRECTION 6
428 
433 #define SRC_DST_MAC_SIZE 12
434 
439 #define PTP_LEAP_61_MASK (1<<0)
440 
444 #define PTP_LEAP_59_MASK (1<<1)
445 
449 #define PTP_UTC_REASONABLE_MASK (1<<2)
450 
455 #define PTP_TIMESCALE_MASK (1<<3)
456 
461 #define PTP_TIME_TRACEABLE_MASK (1<<4)
462 
467 #define PTP_FREQ_TRACEABLE_MASK (1<<5)
468 
473 #define PTP_ALTERNATE_MASTER_MASK (1<<8)
474 
480 #define PTP_TWO_STEP_MASK (1<<9)
481 
486 #define PTP_UNICAST_MASK (1<<10)
487 
493 #define PTP_PROFILE_SPECIFIC_1_MASK (1<<13)
494 
500 #define PTP_PROFILE_SPECIFIC_2_MASK (1<<14)
501 
506 #define PTP_SECURITY_MASK (1<<15)
507 
513 #define SYNC_INTERVAL_PERIOD_CHANGE_THRESHOLD 60
514 
519 #define GPTP_NUM_DOMAINS 2
520 
525 #define PTP_FLAG_SIZE (2)
526 
531 #define PTP_SRC_CLK_IDENTITY_SIZE (8)
532 
536 #define PTP_SRC_PORT_ID_SIZE (2)
537 
542 #define PTP_GM_CLK_IDENTITY_SIZE (8)
543 
548 #define PTP_GM_CLK_CLASS_SIZE (1)
549 
554 #define PTP_GM_CLK_ACCU_SIZE (1)
555 
560 #define PTP_GM_CLK_VARIANCE_SIZE (2)
561 
566 #define PTP_UTC_SIZE (2)
567 
572 #define PTP_TIME_SRC_SIZE (1)
573 
578 #define PTP_PRIORITY1_SIZE (1)
579 
584 #define PTP_PRIORITY2_SIZE (1)
585 
590 #define PTP_STEPS_REMOVED_SIZE (2)
591 
592 /* ========================================================================== */
593 /* Function Definitions */
594 /* ========================================================================== */
606  uint8_t portNum);
607 
618 void TimeSync_Port1linkResetCallBack(uint8_t linkStatus, void *arg2);
619 
625 void TimeSync_Port2linkResetCallBack(uint8_t linkStatus, void *arg2);
626 
638 
649  uint8_t *pktBuffer, \
650  uint8_t portNum, uint16_t size, uint8_t isLinkLocal);
651 
657 
664  uint8_t *parentMac);
671  uint8_t *prevMac);
678  int8_t *buff);
686 
697 void TimeSync_getTxTS(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t portNum,
698  ptpFrameTypes_t frameType);
699 
707 
716  uint8_t twoStep, uint8_t portNum);
727  uint8_t *buff, \
728  uint8_t followUp, uint8_t portNum, uint16_t size);
737  uint8_t *buff, \
738  uint8_t portNum);
747  uint8_t *buff, \
748  uint8_t portNum);
758  uint8_t *buff, \
759  uint8_t followUp, uint8_t portNum);
767  uint8_t portNum);
768 
776 
783 void TimeSync_calcNRR(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t portNum);
792  uint8_t portNum);
793 
800 
808  uint8_t portNum);
809 
811  uint8_t *pktBuffer);
812 
813 
814 uint64_t getIEPTimestamp(TimeSync_ParamsHandle_t timeSyncHandle);
815 
816 
817 #ifdef __cplusplus
818 }
819 #endif
820 
821 #endif /* ICSS_TIMESYNC_H_ */
TimeSync_processPdelayRespFrame
void TimeSync_processPdelayRespFrame(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t *buff, uint8_t followUp, uint8_t portNum)
TimeSync_forced2StepBDCalc
void TimeSync_forced2StepBDCalc(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t portNum)
TimeSync_Port1linkResetCallBack
void TimeSync_Port1linkResetCallBack(uint8_t linkStatus, void *arg2)
Callback for link status change on Port 1.
TimeSync_getPrevAddress
void TimeSync_getPrevAddress(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t *prevMac)
Retuns the previous master's MAC ID.
TimeSync_dummyBMCA
void TimeSync_dummyBMCA(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t *pktBuffer)
TimeSync_lineDelayCalc
void TimeSync_lineDelayCalc(TimeSync_ParamsHandle_t timeSyncHandle)
TimeSync_processSyncFrame
void TimeSync_processSyncFrame(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t *buff, uint8_t followUp, uint8_t portNum, uint16_t size)
TimeSync_writeTS_SingleStep_Sync
void TimeSync_writeTS_SingleStep_Sync(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t portNum)
icss_emac.h
TimeSync_processPTPFrame
void TimeSync_processPTPFrame(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t *pktBuffer, uint8_t portNum, uint16_t size, uint8_t isLinkLocal)
Processes a PTP message and extract fields.
TimeSync_calcRcfAndSyncInterval
void TimeSync_calcRcfAndSyncInterval(TimeSync_ParamsHandle_t timeSyncHandle)
TimeSync_Port2linkResetCallBack
void TimeSync_Port2linkResetCallBack(uint8_t linkStatus, void *arg2)
Callback for link status change on Port 2.
TimeSync_processPdelayReqFrame
void TimeSync_processPdelayReqFrame(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t *buff, uint8_t portNum)
TimeSync_resetIEP
void TimeSync_resetIEP(TimeSync_ParamsHandle_t timeSyncHandle)
Resets CMP1 once first adjustment is done, else 1PPS signal doesn't trigger.
TimeSync_updateNRRParams
void TimeSync_updateNRRParams(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t portNum)
getIEPTimestamp
uint64_t getIEPTimestamp(TimeSync_ParamsHandle_t timeSyncHandle)
TimeSync_isEnabled
uint8_t TimeSync_isEnabled(TimeSync_ParamsHandle_t timeSyncHandle)
Return True/False 1/0 if timeSync module is enabled.
TimeSync_updateParentAddress
void TimeSync_updateParentAddress(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t *parentMac)
Write the MAC ID of PTP master to Firmware. BMC Algorithm should call this API.
TimeSync_calcNRR
void TimeSync_calcNRR(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t portNum)
TimeSync_doFirstAdjustment
void TimeSync_doFirstAdjustment(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t portNum)
Do initial adjustment for the IEP based on sync timestamp.
TimeSync_peerDelayCalc
void TimeSync_peerDelayCalc(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t twoStep, uint8_t portNum)
TimeSync_synchronizeClock
void TimeSync_synchronizeClock(TimeSync_ParamsHandle_t timeSyncHandle)
TimeSync_processDelayResFrame
void TimeSync_processDelayResFrame(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t *buff, uint8_t portNum)
TimeSync_getTxTS
void TimeSync_getTxTS(TimeSync_ParamsHandle_t timeSyncHandle, uint8_t portNum, ptpFrameTypes_t frameType)
TimeSync_getGeneralMessage
void TimeSync_getGeneralMessage(TimeSync_ParamsHandle_t timeSyncHandle, int8_t *buff)
Copies the PTP announce message data to PTP stack buffer.
icss_timeSyncApi.h
TimeSync_reset
void TimeSync_reset(TimeSync_ParamsHandle_t timeSyncHandle)
Reset the state machine in firmware to restart synchronization (First adjustment happens again)
TimeSync_rxPhyDelayCorrection
void TimeSync_rxPhyDelayCorrection(TimeSync_ParamsHandle_t timeSyncHandle)
Performs PHY delay correction on Rx timestamp Assumption : The function expects the timestamp in rxTi...
TimeSync_ParamsHandle_t
struct TimeSync_ParamsHandle_s * TimeSync_ParamsHandle_t
Definition: icss_timeSyncApi.h:962
ptpFrameTypes_t
ptpFrameTypes_t
PTP Delay Mechanism Type. From Table 9 of Standard.
Definition: icss_timeSyncApi.h:203