AM64x MCU+ SDK  08.06.00
iPtcpUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 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 IPTCPUTILS_H_
34 #define IPTCPUTILS_H_
35 
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #endif
40 
41 
42 /* ========================================================================== */
43 /* Include Files */
44 /* ========================================================================== */
45 
46 #include "PN_Handle.h"
48 
49 /* ========================================================================== */
50 /* Macros & Typedefs */
51 /* ========================================================================== */
52 
60 #define PRU_IEP_COUNT_REG 0x0C
61 
67 #define PRU_IEP_CMP_CFG_REG 0x40
68 
74 #define PRU_IEP_CMP1_REG 0x4C
75 
81 #define PRU_IEP_SYNC_PWIDTH_REG 0x110
82 
88 #define PRU_IEP_CAP_CFG_REG 0x10
89 
90 
96 /*#define PTCP_DEBUG_SI*/
97 
103 /*#define PTCP_DEBUG_LI*/
104 
110 /*#define PTCP_DEBUG*/
111 
117 /*#define SYNC_ANALYSIS*/
118 
124 /*#define SYNC_SYS_LOG*/
125 
131 /*#define SYNC_SYS_LOG_1*/
132 
138 /*#define PTCP_DIRECT_IEP_ADJ*/
139 
145 #define PTCP_ENABLE_FILTER
146 
152 #define ISR_PTCP_NUM 24
153 
159 #define ISR_PTCP_NUM_ARM 124+32
160 
166 #define NUM_DELAYS_SMA 7
167 
173 #define NUM_DELAYS_BURST 5
174 
180 #define PTCP_NUM_PORTS 2
181 
187 #define MAX_SEQID 61435
188 
194 #define PTCP_DELAY_REQ_LEN 60
195 
201 #define MAX_CTR_VAL 512
202 
208 #define INTER_DEL_REQ_GAP 200000000
209 
215 #define INTER_DEL_REQ_GAP_MS 205
216 
223 #define INTER_DEL_REQ_BURST_GAP_MS 8205
224 
230 #define SYNC_INTERVAL 30000000
231 
237 #ifdef PROFINET_RGMII_MODE
238 #define PORT_RX_DELAY 374+160 //FIXME : MAC delay needs to be accounted correctly
239 #else
240 #define PORT_RX_DELAY 220
241 #endif
242 
248 #ifdef PROFINET_RGMII_MODE
249 #define PORT_TX_DELAY 248+160 //FIXME : MAC delay needs to be accounted correctly
250 #else
251 #define PORT_TX_DELAY 64
252 #endif
253 
254 
260 #define DEBUG_DELAY_N_ITER 200
261 
267 #ifdef PTCP_DEBUG_SI
268 #define DEBUG_SYNC_N_ITER 300
269 #else
270 #define DEBUG_SYNC_N_ITER 40000
271 #endif
272 
278 #define SYNC_FILTER_SIZE 8
279 
285 #define SYNC_ANALYSIS_N_ITER 100
286 
292 #define SYNC_ANALYSIS_N_RESETS 40
293 
294 #define LATCH0_EVENT 12/* Latch Event number */
295 
296 /* ========================================================================== */
297 /* Structure Declarations */
298 /* ========================================================================== */
299 
304 typedef struct
305 {
306  uint16_t seqId;
307  uint32_t T1PortTXDelay;
308  uint32_t T2PortRXDelay;
309  uint32_t T3PortTXDelay;
310  uint32_t T4PortRXDelay;
311  uint32_t T1TimeStamp;
312  uint32_t T2TimeStamp;
313  uint32_t T4TimeStamp;
314  uint32_t T1_cycle_ctr;
315  uint32_t T4_cycle_ctr;
316  uint32_t reqDelay;
317  uint32_t resDelay;
318  float rcf_peer;
319  uint32_t resDelay_peer;
320  uint32_t cable_delay;
321  uint32_t line_delay;
323 
328 typedef struct
329 {
331  /*TODO: Review NUM_PORTS -> PTCP_NUM_PORTS at all places*/
332  uint32_t cDelay[PTCP_NUM_PORTS];
334 
335  uint32_t syncPllWnd;
338 
339  uint8_t firstSyncRcv;
340  uint8_t syncRcv;
341  uint32_t nSyncMissed;
342 
343  uint8_t subdomainUUID[16];
344  uint8_t masterSA[6];
346 
351 typedef struct
352 {
353  uint32_t lDelays[NUM_DELAYS_SMA];
354  uint32_t cDelays[NUM_DELAYS_SMA];
356 
361 typedef struct
362 {
363  uint32_t *pT1TS;
364  uint32_t *pT4TS;
365  uint32_t *pT1CycleCtr;
366  uint32_t *pT4CycleCtr;
367 
368  uint8_t *pDelayReqPacket;
369  uint8_t *pDelayResPacket;
371 
374  uint8_t *pInDelayResCtrl;
375 
376  uint32_t *pSmaLineDelay;
378 
383 typedef struct
384 {
385  int32_t cDelay[DEBUG_DELAY_N_ITER];
386  uint32_t lDelay[DEBUG_DELAY_N_ITER];
387  float rcfPeer[DEBUG_DELAY_N_ITER];
388  uint32_t reqDelay[DEBUG_DELAY_N_ITER];
389  uint32_t resDelay[DEBUG_DELAY_N_ITER];
390  uint16_t seqId[DEBUG_DELAY_N_ITER];
391 } debugDelay_t;
392 
397 typedef struct
398 {
399  int32_t syncDeltaT[DEBUG_SYNC_N_ITER];
400  int32_t syncSmaDeltaT[DEBUG_SYNC_N_ITER];
401 
402  uint32_t syncLocalT[DEBUG_SYNC_N_ITER];
403  uint32_t syncMasterT[DEBUG_SYNC_N_ITER];
404 
405  uint32_t syncTorgT[DEBUG_SYNC_N_ITER];
406  uint32_t syncInDelayPlusLDT[DEBUG_SYNC_N_ITER];
407 } debugSync_t;
408 
409 
410 #define SYNC_SINGLE_ITER_SIZE 200
411 
415 typedef struct
416 {
417  int32_t deltaT[SYNC_SINGLE_ITER_SIZE];
418  int32_t smaDeltaT[SYNC_SINGLE_ITER_SIZE];
419  int32_t seqId[SYNC_SINGLE_ITER_SIZE];
420  int32_t tState[SYNC_SINGLE_ITER_SIZE];
421  int32_t tSeqId[SYNC_SINGLE_ITER_SIZE];
422  int32_t cycleTime[SYNC_SINGLE_ITER_SIZE];
424 
425 /* ========================================================================== */
426 /* Function Declarations */
427 /* ========================================================================== */
428 
435 void PN_PTCP_start(PN_Handle pnHandle);
436 
443 void PN_PTCP_ClockChange(PN_Handle pnHandle, uint32_t cycleTime);
444 
449 void PN_PTCP_reset(PN_Handle pnHandle);
450 
455 void PN_PTCP_init(PN_Handle pnHandle);
456 
463 
470 
477 
486  ptcp_iDelayResp_struct_t *ptcp_iDelayResp_parsed);
487 
500  *ptcp_iDelayResp_parsed, uint8_t *ptcp_iDelayResp_packet,
501  uint8_t *ptcp_iDelayFupResp_packet, int32_t w_FUP);
502 
512  ptcp_iDelayResp_struct_t *ptcp_iDelayResp_parsed, uint8_t port);
513 
521 uint32_t PN_PTCP_rotUint(uint32_t *input);
522 
530 uint16_t PN_PTCP_rotUshort(uint16_t *input);
531 
538 int32_t PN_PTCP_setupIsr(PN_Handle pnHandle);
539 
544 void PN_PTCP_isrHandler(void* arg);
545 
552 int32_t PN_PTCP_enableIsr(PN_Handle pnHandle);
553 
560 
568 void PN_PTCP_syncIepAdjustment(PN_Handle pnHandle, int32_t ecapPeriod,
569  uint32_t compensation);
570 
581  volatile ptcpSyncInfo_t *ptcp_sync_parsed, uint8_t *sync_sblock);
582 
583 
593 void PN_PTCP_syncPreprocess(PN_Handle pnHandle, uint8_t ctrlByte);
594 
602 void PN_PTCP_portDelaySmaCalc(PN_Handle pnHandle, uint8_t portNum);
603 
611 void PN_PTCP_processDelayResponse(PN_Handle pnHandle, uint8_t portNum);
612 
620 void PN_PTCP_resetDelayTimings(PN_Handle pnHandle, uint8_t portNum);
621 
630 int32_t PN_PTCP_adjCtrDiff(PN_Handle pnHandle, int32_t ctrDiff);
631 
639 int32_t PN_PTCP_absVal(int32_t num);
640 
647 
654 void PN_PTCP_taskSleep(uint32_t mSec);
655 
662 
671 int32_t PN_PTCP_modFunc(int32_t num, uint32_t mod);
672 
685 #ifdef __cplusplus
686 }
687 #endif
688 
689 #endif /*PTCP_UTILS*/
PN_PTCP_parseInDelayResp
int32_t PN_PTCP_parseInDelayResp(ptcp_iDelayResp_struct_t *ptcp_iDelayResp_parsed, uint8_t *ptcp_iDelayResp_packet, uint8_t *ptcp_iDelayFupResp_packet, int32_t w_FUP)
PN_PTCP_parseSyncFields
int32_t PN_PTCP_parseSyncFields(PN_Handle pnHandle, volatile ptcpSyncInfo_t *ptcp_sync_parsed, uint8_t *sync_sblock)
PN_PTCP_start
void PN_PTCP_start(PN_Handle pnHandle)
Triggers the start of ptcp protocol Should be called from stack after initialization It only controls...
ptcp_iDelayResp_struct_t::reqDelay
uint32_t reqDelay
Definition: iPtcpUtils.h:316
ptcp_iDelayResp_struct_t::line_delay
uint32_t line_delay
Definition: iPtcpUtils.h:321
PN_PTCP_reset
void PN_PTCP_reset(PN_Handle pnHandle)
reset relevant data on start or sync timeout
PN_Handle
struct PN_Config_s * PN_Handle
Alias for Profinet Handle containing base addresses and modules.
Definition: PN_Handle.h:102
SYNC_SINGLE_ITER_SIZE
#define SYNC_SINGLE_ITER_SIZE
Definition: iPtcpUtils.h:410
NUM_DELAYS_SMA
#define NUM_DELAYS_SMA
Definition: iPtcpUtils.h:166
PN_PTCP_syncTimeoutMonitor
void PN_PTCP_syncTimeoutMonitor(PN_Handle pnHandle)
devicePortOffsets_t::pSeqIdInDelayPacket
uint16_t * pSeqIdInDelayPacket
Definition: iPtcpUtils.h:370
PN_PTCP_syncHandling
void PN_PTCP_syncHandling(PN_Handle pnHandle)
PN_PTCP_rotUint
uint32_t PN_PTCP_rotUint(uint32_t *input)
DEBUG_SYNC_N_ITER
#define DEBUG_SYNC_N_ITER
Definition: iPtcpUtils.h:270
devicePortOffsets_t::pInDelayResPacket
uint8_t * pInDelayResPacket
Definition: iPtcpUtils.h:372
DEBUG_DELAY_N_ITER
#define DEBUG_DELAY_N_ITER
Definition: iPtcpUtils.h:260
PN_PTCP_portDelaySmaCalc
void PN_PTCP_portDelaySmaCalc(PN_Handle pnHandle, uint8_t portNum)
ptcp_iDelayResp_struct_t::T4PortRXDelay
uint32_t T4PortRXDelay
Definition: iPtcpUtils.h:310
currentPtcpStatus_t::nSyncMissed
uint32_t nSyncMissed
Definition: iPtcpUtils.h:341
syncAnalysis_t
Definition: iPtcpUtils.h:416
debugDelay_t
Definition: iPtcpUtils.h:384
currentPtcpStatus_t::takeoverTimeoutFactor
uint32_t takeoverTimeoutFactor
Definition: iPtcpUtils.h:337
PN_PTCP_configureSync0Pin
void PN_PTCP_configureSync0Pin(PN_Handle pnHandle)
icss_emac.h
ptcp_iDelayResp_struct_t::T4_cycle_ctr
uint32_t T4_cycle_ctr
Definition: iPtcpUtils.h:315
devicePortOffsets_t::pT4TS
uint32_t * pT4TS
Definition: iPtcpUtils.h:364
currentPtcpStatus_t::syncState
syncState_t syncState
Definition: iPtcpUtils.h:330
PN_PTCP_syncPreprocess
void PN_PTCP_syncPreprocess(PN_Handle pnHandle, uint8_t ctrlByte)
PN_PTCP_lineDelayCalc
int32_t PN_PTCP_lineDelayCalc(PN_Handle pnHandle, ptcp_iDelayResp_struct_t *ptcp_iDelayResp_parsed)
devicePortOffsets_t::pT1TS
uint32_t * pT1TS
Definition: iPtcpUtils.h:363
currentPtcpStatus_t::syncPllWnd
uint32_t syncPllWnd
Definition: iPtcpUtils.h:335
ptcp_iDelayResp_struct_t::T1_cycle_ctr
uint32_t T1_cycle_ctr
Definition: iPtcpUtils.h:314
devicePortOffsets_t::pInDelayFupResPacket
uint8_t * pInDelayFupResPacket
Definition: iPtcpUtils.h:373
debugSync_t
Definition: iPtcpUtils.h:398
PN_PTCP_resetDelayTimings
void PN_PTCP_resetDelayTimings(PN_Handle pnHandle, uint8_t portNum)
ptcp_iDelayResp_struct_t::resDelay_peer
uint32_t resDelay_peer
Definition: iPtcpUtils.h:319
ptcp_iDelayResp_struct_t::seqId
uint16_t seqId
Definition: iPtcpUtils.h:306
ptcp_iDelayResp_struct_t::T3PortTXDelay
uint32_t T3PortTXDelay
Definition: iPtcpUtils.h:309
PN_PTCP_smaDelayMeasurement
void PN_PTCP_smaDelayMeasurement(PN_Handle pnHandle)
PN_PTCP_isrHandler
void PN_PTCP_isrHandler(void *arg)
Profinet PTCP ISR handler.
devicePortOffsets_t::pInDelayResCtrl
uint8_t * pInDelayResCtrl
Definition: iPtcpUtils.h:374
PN_PTCP_delayMeasurement
void PN_PTCP_delayMeasurement(PN_Handle pnHandle)
ptcp_iDelayResp_struct_t::T2PortRXDelay
uint32_t T2PortRXDelay
Definition: iPtcpUtils.h:308
PN_PTCP_cableDelayCalc
int32_t PN_PTCP_cableDelayCalc(PN_Handle pnHandle, ptcp_iDelayResp_struct_t *ptcp_iDelayResp_parsed, uint8_t port)
PN_PTCP_triggerMeasurement
void PN_PTCP_triggerMeasurement(PN_Handle pnHandle)
ptcp_iDelayResp_struct_t::T1TimeStamp
uint32_t T1TimeStamp
Definition: iPtcpUtils.h:311
PN_Handle.h
currentPtcpStatus_t::syncRcv
uint8_t syncRcv
Definition: iPtcpUtils.h:340
ptcp_iDelayResp_struct_t::T4TimeStamp
uint32_t T4TimeStamp
Definition: iPtcpUtils.h:313
ptcp_iDelayResp_struct_t::T1PortTXDelay
uint32_t T1PortTXDelay
Definition: iPtcpUtils.h:307
ptcp_iDelayResp_struct_t::rcf_peer
float rcf_peer
Definition: iPtcpUtils.h:318
devicePortOffsets_t::pDelayReqPacket
uint8_t * pDelayReqPacket
Definition: iPtcpUtils.h:368
devicePortOffsets_t
Definition: iPtcpUtils.h:362
deviceDelays_t
Definition: iPtcpUtils.h:352
currentPtcpStatus_t::syncTimeoutFactor
uint32_t syncTimeoutFactor
Definition: iPtcpUtils.h:336
ptcp_iDelayResp_struct_t::resDelay
uint32_t resDelay
Definition: iPtcpUtils.h:317
PN_PTCP_resetDelayValues
void PN_PTCP_resetDelayValues(PN_Handle pnHandle)
PN_PTCP_ClockChange
void PN_PTCP_ClockChange(PN_Handle pnHandle, uint32_t cycleTime)
Handles the clock (IEP CMP0) change scenario.
PN_PTCP_init
void PN_PTCP_init(PN_Handle pnHandle)
Initializes the PTCP data structure and memory for PTCP pre-formatted packets.
PN_PTCP_adjCtrDiff
int32_t PN_PTCP_adjCtrDiff(PN_Handle pnHandle, int32_t ctrDiff)
ptcp_iDelayResp_struct_t
Definition: iPtcpUtils.h:305
ptcp_iDelayResp_struct_t::T2TimeStamp
uint32_t T2TimeStamp
Definition: iPtcpUtils.h:312
devicePortOffsets_t::pT4CycleCtr
uint32_t * pT4CycleCtr
Definition: iPtcpUtils.h:366
PN_PTCP_taskSleep
void PN_PTCP_taskSleep(uint32_t mSec)
ptcp_iDelayResp_struct_t::cable_delay
uint32_t cable_delay
Definition: iPtcpUtils.h:320
ptcpSyncInfo_t
Structure containing current sync info.
Definition: iPtcpDrv.h:107
currentPtcpStatus_t::firstSyncRcv
uint8_t firstSyncRcv
Definition: iPtcpUtils.h:339
devicePortOffsets_t::pDelayResPacket
uint8_t * pDelayResPacket
Definition: iPtcpUtils.h:369
syncState_t
syncState_t
Enum for the various sync states.
Definition: iPtcpDrv.h:78
PN_PTCP_absVal
int32_t PN_PTCP_absVal(int32_t num)
PN_PTCP_enableIsr
int32_t PN_PTCP_enableIsr(PN_Handle pnHandle)
PN_PTCP_rotUshort
uint16_t PN_PTCP_rotUshort(uint16_t *input)
PN_PTCP_setupIsr
int32_t PN_PTCP_setupIsr(PN_Handle pnHandle)
PN_PTCP_processDelayResponse
void PN_PTCP_processDelayResponse(PN_Handle pnHandle, uint8_t portNum)
currentPtcpStatus_t
Definition: iPtcpUtils.h:329
PN_PTCP_syncIepAdjustment
void PN_PTCP_syncIepAdjustment(PN_Handle pnHandle, int32_t ecapPeriod, uint32_t compensation)
PTCP_NUM_PORTS
#define PTCP_NUM_PORTS
Definition: iPtcpUtils.h:180
ptcpPortStatus_t
ptcpPortStatus_t
Enum for enable/disable states.
Definition: iPtcpDrv.h:67
devicePortOffsets_t::pSmaLineDelay
uint32_t * pSmaLineDelay
Definition: iPtcpUtils.h:376
PN_PTCP_modFunc
int32_t PN_PTCP_modFunc(int32_t num, uint32_t mod)
devicePortOffsets_t::pT1CycleCtr
uint32_t * pT1CycleCtr
Definition: iPtcpUtils.h:365