AM64x MCU+ SDK  08.00.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 /* ========================================================================== */
37 /* Include Files */
38 /* ========================================================================== */
39 
40 #include "PN_Handle.h"
42 
43 /* ========================================================================== */
44 /* Macros & Typedefs */
45 /* ========================================================================== */
46 
54 #define PRU_IEP_COUNT_REG 0x0C
55 
61 #define PRU_IEP_CMP_CFG_REG 0x40
62 
68 #define PRU_IEP_CMP1_REG 0x4C
69 
75 #define PRU_IEP_SYNC_PWIDTH_REG 0x110
76 
82 #define PRU_IEP_CAP_CFG_REG 0x10
83 
84 
90 /*#define PTCP_DEBUG_SI*/
91 
97 /*#define PTCP_DEBUG_LI*/
98 
104 /*#define PTCP_DEBUG*/
105 
111 /*#define SYNC_ANALYSIS*/
112 
118 /*#define SYNC_SYS_LOG*/
119 
125 /*#define SYNC_SYS_LOG_1*/
126 
132 /*#define PTCP_DIRECT_IEP_ADJ*/
133 
139 #define PTCP_ENABLE_FILTER
140 
146 #define ISR_PTCP_NUM 24
147 
153 #define ISR_PTCP_NUM_ARM 124+32
154 
160 #define NUM_DELAYS_SMA 7
161 
167 #define NUM_DELAYS_BURST 5
168 
174 #define PTCP_NUM_PORTS 2
175 
181 #define MAX_SEQID 61435
182 
188 #define PTCP_DELAY_REQ_LEN 60
189 
195 #define MAX_CTR_VAL 512
196 
202 #define INTER_DEL_REQ_GAP 200000000
203 
209 #define INTER_DEL_REQ_GAP_MS 205
210 
217 #define INTER_DEL_REQ_BURST_GAP_MS 8205
218 
224 #define SYNC_INTERVAL 30000000
225 
231 #define PORT_RX_DELAY 184
232 
238 #define PORT_TX_DELAY 86
239 
245 #define DEBUG_DELAY_N_ITER 200
246 
252 #ifdef PTCP_DEBUG_SI
253 #define DEBUG_SYNC_N_ITER 300
254 #else
255 #define DEBUG_SYNC_N_ITER 40000
256 #endif
257 
263 #define SYNC_FILTER_SIZE 8
264 
270 #define SYNC_ANALYSIS_N_ITER 100
271 
277 #define SYNC_ANALYSIS_N_RESETS 40
278 
279 /* EDMA related defines for clock synchronization*/
285 #define EDMA3_ECAPEVT0_CHA_NUM (38U)
286 
292 #define EDMA3_ECAPEVT0_LINK_CHA_NUM (39U)
293 
299 #define EDMA3_CLEAR_ECAP_CHA_NUM (40U)
300 
306 #define EDMA3_CLEAR_ECAP_LINK_CHA_NUM (41U)
307 
313 #define EDMA3_SYNC0_RETRIGGER_CHA_NUM (42)
314 
320 #define EDMA3_SYNC0_RETRIGGER_LINK_CHA_NUM (43)
321 
327 #define EVT_QUEUE_NUM (0)
328 
329 
330 #define ISR_LATCH_NUM 27/* Latch interrupt number */
331 #define LATCH0_EVENT 12/* Latch Event number */
332 
333 #ifdef SOC_AM335X
334 #define ISR_LATCH_NUM_ARM 27
335 #elif defined SOC_AM437X
336 #define ISR_LATCH_NUM_ARM 27 + 32
337 #else
338 #define ISR_LATCH_NUM_ARM 27 + 32
339 #endif
340 
341 /* ========================================================================== */
342 /* Structure Declarations */
343 /* ========================================================================== */
344 
349 typedef struct
350 {
351  uint16_t seqId;
352  uint32_t T1PortTXDelay;
353  uint32_t T2PortRXDelay;
354  uint32_t T3PortTXDelay;
355  uint32_t T4PortRXDelay;
356  uint32_t T1TimeStamp;
357  uint32_t T2TimeStamp;
358  uint32_t T4TimeStamp;
359  uint32_t T1_cycle_ctr;
360  uint32_t T4_cycle_ctr;
361  uint32_t reqDelay;
362  uint32_t resDelay;
363  float rcf_peer;
364  uint32_t resDelay_peer;
365  uint32_t cable_delay;
366  uint32_t line_delay;
368 
373 typedef struct
374 {
376  /*TODO: Review NUM_PORTS -> PTCP_NUM_PORTS at all places*/
377  uint32_t cDelay[PTCP_NUM_PORTS];
379 
380  uint32_t syncPllWnd;
383 
384  uint8_t firstSyncRcv;
385  uint8_t syncRcv;
386  uint32_t nSyncMissed;
387 
388  uint8_t subdomainUUID[16];
389  uint8_t masterSA[6];
391 
396 typedef struct
397 {
398  uint32_t lDelays[NUM_DELAYS_SMA];
399  uint32_t cDelays[NUM_DELAYS_SMA];
401 
406 typedef struct
407 {
408  uint32_t *pT1TS;
409  uint32_t *pT4TS;
410  uint32_t *pT1CycleCtr;
411  uint32_t *pT4CycleCtr;
412 
413  uint8_t *pDelayReqPacket;
414  uint8_t *pDelayResPacket;
416 
419  uint8_t *pInDelayResCtrl;
420 
421  uint32_t *pSmaLineDelay;
423 
428 typedef struct
429 {
430  int32_t cDelay[DEBUG_DELAY_N_ITER];
431  uint32_t lDelay[DEBUG_DELAY_N_ITER];
432  float rcfPeer[DEBUG_DELAY_N_ITER];
433  uint32_t reqDelay[DEBUG_DELAY_N_ITER];
434  uint32_t resDelay[DEBUG_DELAY_N_ITER];
435  uint16_t seqId[DEBUG_DELAY_N_ITER];
436 } debugDelay_t;
437 
442 typedef struct
443 {
444  int32_t syncDeltaT[DEBUG_SYNC_N_ITER];
445  int32_t syncSmaDeltaT[DEBUG_SYNC_N_ITER];
446 
447  uint32_t syncLocalT[DEBUG_SYNC_N_ITER];
448  uint32_t syncMasterT[DEBUG_SYNC_N_ITER];
449 
450  uint32_t syncTorgT[DEBUG_SYNC_N_ITER];
451  uint32_t syncInDelayPlusLDT[DEBUG_SYNC_N_ITER];
452 } debugSync_t;
453 
454 
455 #define SYNC_SINGLE_ITER_SIZE 200
456 
460 typedef struct
461 {
462  int32_t deltaT[SYNC_SINGLE_ITER_SIZE];
463  int32_t smaDeltaT[SYNC_SINGLE_ITER_SIZE];
464  int32_t seqId[SYNC_SINGLE_ITER_SIZE];
465  int32_t tState[SYNC_SINGLE_ITER_SIZE];
466  int32_t tSeqId[SYNC_SINGLE_ITER_SIZE];
467  int32_t cycleTime[SYNC_SINGLE_ITER_SIZE];
469 
470 /* ========================================================================== */
471 /* Function Declarations */
472 /* ========================================================================== */
473 
480 void PN_PTCP_start(PN_Handle pnHandle);
481 
488 void PN_PTCP_ClockChange(PN_Handle pnHandle, uint32_t cycleTime);
489 
494 void PN_PTCP_reset(PN_Handle pnHandle);
495 
500 void PN_PTCP_init(PN_Handle pnHandle);
501 
508 
515 
522 
531  ptcp_iDelayResp_struct_t *ptcp_iDelayResp_parsed);
532 
545  *ptcp_iDelayResp_parsed, uint8_t *ptcp_iDelayResp_packet,
546  uint8_t *ptcp_iDelayFupResp_packet, int32_t w_FUP);
547 
557  ptcp_iDelayResp_struct_t *ptcp_iDelayResp_parsed, uint8_t port);
558 
566 uint32_t PN_PTCP_rotUint(uint32_t *input);
567 
575 uint16_t PN_PTCP_rotUshort(uint16_t *input);
576 
583 int32_t PN_PTCP_setupIsr(PN_Handle pnHandle);
584 
589 void PN_PTCP_isrHandler(void* arg);
590 
597 int32_t PN_PTCP_enableIsr(PN_Handle pnHandle);
598 
605 
613 void PN_PTCP_syncIepAdjustment(PN_Handle pnHandle, int32_t ecapPeriod,
614  uint32_t compensation);
615 
626  volatile ptcpSyncInfo_t *ptcp_sync_parsed, uint8_t *sync_sblock);
627 
634 
641 void PN_PTCP_reconfigEcap(PN_Handle pnHandle, int32_t period);
642 
649 
656 
662 
672 void PN_PTCP_syncPreprocess(PN_Handle pnHandle, uint8_t ctrlByte);
673 
681 void PN_PTCP_portDelaySmaCalc(PN_Handle pnHandle, uint8_t portNum);
682 
690 void PN_PTCP_processDelayResponse(PN_Handle pnHandle, uint8_t portNum);
691 
699 void PN_PTCP_resetDelayTimings(PN_Handle pnHandle, uint8_t portNum);
700 
709 int32_t PN_PTCP_adjCtrDiff(PN_Handle pnHandle, int32_t ctrDiff);
710 
718 int32_t PN_PTCP_absVal(int32_t num);
719 
726 
735 
742 void PN_PTCP_taskSleep(uint32_t mSec);
743 
750 
759 int32_t PN_PTCP_modFunc(int32_t num, uint32_t mod);
760 
772 #endif /*PTCP_UTILS*/
PN_PTCP_setEcapClrEdmaPaRamEntry
void PN_PTCP_setEcapClrEdmaPaRamEntry(void)
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:361
PN_PTCP_syncSubdomainCheck
int32_t PN_PTCP_syncSubdomainCheck()
ptcp_iDelayResp_struct_t::line_delay
uint32_t line_delay
Definition: iPtcpUtils.h:366
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:92
SYNC_SINGLE_ITER_SIZE
#define SYNC_SINGLE_ITER_SIZE
Definition: iPtcpUtils.h:455
NUM_DELAYS_SMA
#define NUM_DELAYS_SMA
Definition: iPtcpUtils.h:160
PN_PTCP_syncTimeoutMonitor
void PN_PTCP_syncTimeoutMonitor(PN_Handle pnHandle)
devicePortOffsets_t::pSeqIdInDelayPacket
uint16_t * pSeqIdInDelayPacket
Definition: iPtcpUtils.h:415
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:255
devicePortOffsets_t::pInDelayResPacket
uint8_t * pInDelayResPacket
Definition: iPtcpUtils.h:417
DEBUG_DELAY_N_ITER
#define DEBUG_DELAY_N_ITER
Definition: iPtcpUtils.h:245
PN_PTCP_portDelaySmaCalc
void PN_PTCP_portDelaySmaCalc(PN_Handle pnHandle, uint8_t portNum)
ptcp_iDelayResp_struct_t::T4PortRXDelay
uint32_t T4PortRXDelay
Definition: iPtcpUtils.h:355
PN_PTCP_setIepEdmaPaRamEntry
void PN_PTCP_setIepEdmaPaRamEntry(PN_Handle pnHandle)
currentPtcpStatus_t::nSyncMissed
uint32_t nSyncMissed
Definition: iPtcpUtils.h:386
syncAnalysis_t
Definition: iPtcpUtils.h:461
PN_PTCP_reconfigEcap
void PN_PTCP_reconfigEcap(PN_Handle pnHandle, int32_t period)
debugDelay_t
Definition: iPtcpUtils.h:429
currentPtcpStatus_t::takeoverTimeoutFactor
uint32_t takeoverTimeoutFactor
Definition: iPtcpUtils.h:382
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:360
devicePortOffsets_t::pT4TS
uint32_t * pT4TS
Definition: iPtcpUtils.h:409
currentPtcpStatus_t::syncState
syncState_t syncState
Definition: iPtcpUtils.h:375
PN_PTCP_syncPreprocess
void PN_PTCP_syncPreprocess(PN_Handle pnHandle, uint8_t ctrlByte)
PN_PTCP_initEcap
void PN_PTCP_initEcap(PN_Handle pnHandle)
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:408
currentPtcpStatus_t::syncPllWnd
uint32_t syncPllWnd
Definition: iPtcpUtils.h:380
ptcp_iDelayResp_struct_t::T1_cycle_ctr
uint32_t T1_cycle_ctr
Definition: iPtcpUtils.h:359
devicePortOffsets_t::pInDelayFupResPacket
uint8_t * pInDelayFupResPacket
Definition: iPtcpUtils.h:418
debugSync_t
Definition: iPtcpUtils.h:443
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:364
ptcp_iDelayResp_struct_t::seqId
uint16_t seqId
Definition: iPtcpUtils.h:351
ptcp_iDelayResp_struct_t::T3PortTXDelay
uint32_t T3PortTXDelay
Definition: iPtcpUtils.h:354
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:419
PN_PTCP_delayMeasurement
void PN_PTCP_delayMeasurement(PN_Handle pnHandle)
ptcp_iDelayResp_struct_t::T2PortRXDelay
uint32_t T2PortRXDelay
Definition: iPtcpUtils.h:353
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:356
PN_Handle.h
currentPtcpStatus_t::syncRcv
uint8_t syncRcv
Definition: iPtcpUtils.h:385
ptcp_iDelayResp_struct_t::T4TimeStamp
uint32_t T4TimeStamp
Definition: iPtcpUtils.h:358
ptcp_iDelayResp_struct_t::T1PortTXDelay
uint32_t T1PortTXDelay
Definition: iPtcpUtils.h:352
ptcp_iDelayResp_struct_t::rcf_peer
float rcf_peer
Definition: iPtcpUtils.h:363
devicePortOffsets_t::pDelayReqPacket
uint8_t * pDelayReqPacket
Definition: iPtcpUtils.h:413
devicePortOffsets_t
Definition: iPtcpUtils.h:407
deviceDelays_t
Definition: iPtcpUtils.h:397
currentPtcpStatus_t::syncTimeoutFactor
uint32_t syncTimeoutFactor
Definition: iPtcpUtils.h:381
ptcp_iDelayResp_struct_t::resDelay
uint32_t resDelay
Definition: iPtcpUtils.h:362
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:350
ptcp_iDelayResp_struct_t::T2TimeStamp
uint32_t T2TimeStamp
Definition: iPtcpUtils.h:357
PN_PTCP_configEdma
void PN_PTCP_configEdma(PN_Handle pnHandle)
devicePortOffsets_t::pT4CycleCtr
uint32_t * pT4CycleCtr
Definition: iPtcpUtils.h:411
PN_PTCP_taskSleep
void PN_PTCP_taskSleep(uint32_t mSec)
ptcp_iDelayResp_struct_t::cable_delay
uint32_t cable_delay
Definition: iPtcpUtils.h:365
ptcpSyncInfo_t
Structure containing current sync info.
Definition: iPtcpDrv.h:101
currentPtcpStatus_t::firstSyncRcv
uint8_t firstSyncRcv
Definition: iPtcpUtils.h:384
devicePortOffsets_t::pDelayResPacket
uint8_t * pDelayResPacket
Definition: iPtcpUtils.h:414
syncState_t
syncState_t
Enum for the various sync states.
Definition: iPtcpDrv.h:72
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:374
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:174
ptcpPortStatus_t
ptcpPortStatus_t
Enum for enable/disable states.
Definition: iPtcpDrv.h:61
devicePortOffsets_t::pSmaLineDelay
uint32_t * pSmaLineDelay
Definition: iPtcpUtils.h:421
PN_PTCP_modFunc
int32_t PN_PTCP_modFunc(int32_t num, uint32_t mod)
devicePortOffsets_t::pT1CycleCtr
uint32_t * pT1CycleCtr
Definition: iPtcpUtils.h:410