AM64x MCU+ SDK  08.02.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 #ifdef PROFINET_RGMII_MODE
232 #define PORT_RX_DELAY 374+160 //FIXME : MAC delay needs to be accounted correctly
233 #else
234 #define PORT_RX_DELAY 220
235 #endif
236 
242 #ifdef PROFINET_RGMII_MODE
243 #define PORT_TX_DELAY 248+160 //FIXME : MAC delay needs to be accounted correctly
244 #else
245 #define PORT_TX_DELAY 64
246 #endif
247 
248 
254 #define DEBUG_DELAY_N_ITER 200
255 
261 #ifdef PTCP_DEBUG_SI
262 #define DEBUG_SYNC_N_ITER 300
263 #else
264 #define DEBUG_SYNC_N_ITER 40000
265 #endif
266 
272 #define SYNC_FILTER_SIZE 8
273 
279 #define SYNC_ANALYSIS_N_ITER 100
280 
286 #define SYNC_ANALYSIS_N_RESETS 40
287 
288 #define LATCH0_EVENT 12/* Latch Event number */
289 
290 /* ========================================================================== */
291 /* Structure Declarations */
292 /* ========================================================================== */
293 
298 typedef struct
299 {
300  uint16_t seqId;
301  uint32_t T1PortTXDelay;
302  uint32_t T2PortRXDelay;
303  uint32_t T3PortTXDelay;
304  uint32_t T4PortRXDelay;
305  uint32_t T1TimeStamp;
306  uint32_t T2TimeStamp;
307  uint32_t T4TimeStamp;
308  uint32_t T1_cycle_ctr;
309  uint32_t T4_cycle_ctr;
310  uint32_t reqDelay;
311  uint32_t resDelay;
312  float rcf_peer;
313  uint32_t resDelay_peer;
314  uint32_t cable_delay;
315  uint32_t line_delay;
317 
322 typedef struct
323 {
325  /*TODO: Review NUM_PORTS -> PTCP_NUM_PORTS at all places*/
326  uint32_t cDelay[PTCP_NUM_PORTS];
328 
329  uint32_t syncPllWnd;
332 
333  uint8_t firstSyncRcv;
334  uint8_t syncRcv;
335  uint32_t nSyncMissed;
336 
337  uint8_t subdomainUUID[16];
338  uint8_t masterSA[6];
340 
345 typedef struct
346 {
347  uint32_t lDelays[NUM_DELAYS_SMA];
348  uint32_t cDelays[NUM_DELAYS_SMA];
350 
355 typedef struct
356 {
357  uint32_t *pT1TS;
358  uint32_t *pT4TS;
359  uint32_t *pT1CycleCtr;
360  uint32_t *pT4CycleCtr;
361 
362  uint8_t *pDelayReqPacket;
363  uint8_t *pDelayResPacket;
365 
368  uint8_t *pInDelayResCtrl;
369 
370  uint32_t *pSmaLineDelay;
372 
377 typedef struct
378 {
379  int32_t cDelay[DEBUG_DELAY_N_ITER];
380  uint32_t lDelay[DEBUG_DELAY_N_ITER];
381  float rcfPeer[DEBUG_DELAY_N_ITER];
382  uint32_t reqDelay[DEBUG_DELAY_N_ITER];
383  uint32_t resDelay[DEBUG_DELAY_N_ITER];
384  uint16_t seqId[DEBUG_DELAY_N_ITER];
385 } debugDelay_t;
386 
391 typedef struct
392 {
393  int32_t syncDeltaT[DEBUG_SYNC_N_ITER];
394  int32_t syncSmaDeltaT[DEBUG_SYNC_N_ITER];
395 
396  uint32_t syncLocalT[DEBUG_SYNC_N_ITER];
397  uint32_t syncMasterT[DEBUG_SYNC_N_ITER];
398 
399  uint32_t syncTorgT[DEBUG_SYNC_N_ITER];
400  uint32_t syncInDelayPlusLDT[DEBUG_SYNC_N_ITER];
401 } debugSync_t;
402 
403 
404 #define SYNC_SINGLE_ITER_SIZE 200
405 
409 typedef struct
410 {
411  int32_t deltaT[SYNC_SINGLE_ITER_SIZE];
412  int32_t smaDeltaT[SYNC_SINGLE_ITER_SIZE];
413  int32_t seqId[SYNC_SINGLE_ITER_SIZE];
414  int32_t tState[SYNC_SINGLE_ITER_SIZE];
415  int32_t tSeqId[SYNC_SINGLE_ITER_SIZE];
416  int32_t cycleTime[SYNC_SINGLE_ITER_SIZE];
418 
419 /* ========================================================================== */
420 /* Function Declarations */
421 /* ========================================================================== */
422 
429 void PN_PTCP_start(PN_Handle pnHandle);
430 
437 void PN_PTCP_ClockChange(PN_Handle pnHandle, uint32_t cycleTime);
438 
443 void PN_PTCP_reset(PN_Handle pnHandle);
444 
449 void PN_PTCP_init(PN_Handle pnHandle);
450 
457 
464 
471 
480  ptcp_iDelayResp_struct_t *ptcp_iDelayResp_parsed);
481 
494  *ptcp_iDelayResp_parsed, uint8_t *ptcp_iDelayResp_packet,
495  uint8_t *ptcp_iDelayFupResp_packet, int32_t w_FUP);
496 
506  ptcp_iDelayResp_struct_t *ptcp_iDelayResp_parsed, uint8_t port);
507 
515 uint32_t PN_PTCP_rotUint(uint32_t *input);
516 
524 uint16_t PN_PTCP_rotUshort(uint16_t *input);
525 
532 int32_t PN_PTCP_setupIsr(PN_Handle pnHandle);
533 
538 void PN_PTCP_isrHandler(void* arg);
539 
546 int32_t PN_PTCP_enableIsr(PN_Handle pnHandle);
547 
554 
562 void PN_PTCP_syncIepAdjustment(PN_Handle pnHandle, int32_t ecapPeriod,
563  uint32_t compensation);
564 
575  volatile ptcpSyncInfo_t *ptcp_sync_parsed, uint8_t *sync_sblock);
576 
577 
587 void PN_PTCP_syncPreprocess(PN_Handle pnHandle, uint8_t ctrlByte);
588 
596 void PN_PTCP_portDelaySmaCalc(PN_Handle pnHandle, uint8_t portNum);
597 
605 void PN_PTCP_processDelayResponse(PN_Handle pnHandle, uint8_t portNum);
606 
614 void PN_PTCP_resetDelayTimings(PN_Handle pnHandle, uint8_t portNum);
615 
624 int32_t PN_PTCP_adjCtrDiff(PN_Handle pnHandle, int32_t ctrDiff);
625 
633 int32_t PN_PTCP_absVal(int32_t num);
634 
641 
648 void PN_PTCP_taskSleep(uint32_t mSec);
649 
656 
665 int32_t PN_PTCP_modFunc(int32_t num, uint32_t mod);
666 
678 #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:310
ptcp_iDelayResp_struct_t::line_delay
uint32_t line_delay
Definition: iPtcpUtils.h:315
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:96
SYNC_SINGLE_ITER_SIZE
#define SYNC_SINGLE_ITER_SIZE
Definition: iPtcpUtils.h:404
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:364
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:264
devicePortOffsets_t::pInDelayResPacket
uint8_t * pInDelayResPacket
Definition: iPtcpUtils.h:366
DEBUG_DELAY_N_ITER
#define DEBUG_DELAY_N_ITER
Definition: iPtcpUtils.h:254
PN_PTCP_portDelaySmaCalc
void PN_PTCP_portDelaySmaCalc(PN_Handle pnHandle, uint8_t portNum)
ptcp_iDelayResp_struct_t::T4PortRXDelay
uint32_t T4PortRXDelay
Definition: iPtcpUtils.h:304
currentPtcpStatus_t::nSyncMissed
uint32_t nSyncMissed
Definition: iPtcpUtils.h:335
syncAnalysis_t
Definition: iPtcpUtils.h:410
debugDelay_t
Definition: iPtcpUtils.h:378
currentPtcpStatus_t::takeoverTimeoutFactor
uint32_t takeoverTimeoutFactor
Definition: iPtcpUtils.h:331
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:309
devicePortOffsets_t::pT4TS
uint32_t * pT4TS
Definition: iPtcpUtils.h:358
currentPtcpStatus_t::syncState
syncState_t syncState
Definition: iPtcpUtils.h:324
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:357
currentPtcpStatus_t::syncPllWnd
uint32_t syncPllWnd
Definition: iPtcpUtils.h:329
ptcp_iDelayResp_struct_t::T1_cycle_ctr
uint32_t T1_cycle_ctr
Definition: iPtcpUtils.h:308
devicePortOffsets_t::pInDelayFupResPacket
uint8_t * pInDelayFupResPacket
Definition: iPtcpUtils.h:367
debugSync_t
Definition: iPtcpUtils.h:392
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:313
ptcp_iDelayResp_struct_t::seqId
uint16_t seqId
Definition: iPtcpUtils.h:300
ptcp_iDelayResp_struct_t::T3PortTXDelay
uint32_t T3PortTXDelay
Definition: iPtcpUtils.h:303
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:368
PN_PTCP_delayMeasurement
void PN_PTCP_delayMeasurement(PN_Handle pnHandle)
ptcp_iDelayResp_struct_t::T2PortRXDelay
uint32_t T2PortRXDelay
Definition: iPtcpUtils.h:302
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:305
PN_Handle.h
currentPtcpStatus_t::syncRcv
uint8_t syncRcv
Definition: iPtcpUtils.h:334
ptcp_iDelayResp_struct_t::T4TimeStamp
uint32_t T4TimeStamp
Definition: iPtcpUtils.h:307
ptcp_iDelayResp_struct_t::T1PortTXDelay
uint32_t T1PortTXDelay
Definition: iPtcpUtils.h:301
ptcp_iDelayResp_struct_t::rcf_peer
float rcf_peer
Definition: iPtcpUtils.h:312
devicePortOffsets_t::pDelayReqPacket
uint8_t * pDelayReqPacket
Definition: iPtcpUtils.h:362
devicePortOffsets_t
Definition: iPtcpUtils.h:356
deviceDelays_t
Definition: iPtcpUtils.h:346
currentPtcpStatus_t::syncTimeoutFactor
uint32_t syncTimeoutFactor
Definition: iPtcpUtils.h:330
ptcp_iDelayResp_struct_t::resDelay
uint32_t resDelay
Definition: iPtcpUtils.h:311
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:299
ptcp_iDelayResp_struct_t::T2TimeStamp
uint32_t T2TimeStamp
Definition: iPtcpUtils.h:306
devicePortOffsets_t::pT4CycleCtr
uint32_t * pT4CycleCtr
Definition: iPtcpUtils.h:360
PN_PTCP_taskSleep
void PN_PTCP_taskSleep(uint32_t mSec)
ptcp_iDelayResp_struct_t::cable_delay
uint32_t cable_delay
Definition: iPtcpUtils.h:314
ptcpSyncInfo_t
Structure containing current sync info.
Definition: iPtcpDrv.h:101
currentPtcpStatus_t::firstSyncRcv
uint8_t firstSyncRcv
Definition: iPtcpUtils.h:333
devicePortOffsets_t::pDelayResPacket
uint8_t * pDelayResPacket
Definition: iPtcpUtils.h:363
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:323
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:370
PN_PTCP_modFunc
int32_t PN_PTCP_modFunc(int32_t num, uint32_t mod)
devicePortOffsets_t::pT1CycleCtr
uint32_t * pT1CycleCtr
Definition: iPtcpUtils.h:359