AM263Px INDUSTRIAL COMMUNICATIONS SDK  10.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 #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 #define DEBUG_DELAY_N_ITER 200
238 
244 #ifdef PTCP_DEBUG_SI
245 #define DEBUG_SYNC_N_ITER 300
246 #else
247 #define DEBUG_SYNC_N_ITER 40000
248 #endif
249 
255 #define SYNC_FILTER_SIZE 8
256 
262 #define SYNC_ANALYSIS_N_ITER 100
263 
269 #define SYNC_ANALYSIS_N_RESETS 40
270 
271 #define LATCH0_EVENT 12/* Latch Event number */
272 
273 /* ========================================================================== */
274 /* Structure Declarations */
275 /* ========================================================================== */
276 
281 typedef struct
282 {
283  uint16_t seqId;
284  uint32_t T1PortTXDelay;
285  uint32_t T2PortRXDelay;
286  uint32_t T3PortTXDelay;
287  uint32_t T4PortRXDelay;
288  uint32_t T1TimeStamp;
289  uint32_t T2TimeStamp;
290  uint32_t T4TimeStamp;
291  uint32_t T1_cycle_ctr;
292  uint32_t T4_cycle_ctr;
293  uint32_t reqDelay;
294  uint32_t resDelay;
295  float rcf_peer;
296  uint32_t resDelay_peer;
297  uint32_t cable_delay;
298  uint32_t line_delay;
300 
305 typedef struct
306 {
308  /*TODO: Review NUM_PORTS -> PTCP_NUM_PORTS at all places*/
309  uint32_t cDelay[PTCP_NUM_PORTS];
311 
312  uint32_t syncPllWnd;
315 
316  uint8_t firstSyncRcv;
317  uint8_t syncRcv;
318  uint32_t nSyncMissed;
319 
320  uint8_t subdomainUUID[16];
321  uint8_t masterSA[6];
323 
328 typedef struct
329 {
330  uint32_t lDelays[NUM_DELAYS_SMA];
331  uint32_t cDelays[NUM_DELAYS_SMA];
333 
338 typedef struct
339 {
340  uint32_t *pT1TS;
341  uint32_t *pT4TS;
342  uint32_t *pT1CycleCtr;
343  uint32_t *pT4CycleCtr;
344 
345  uint8_t *pDelayReqPacket;
346  uint8_t *pDelayResPacket;
348 
351  uint8_t *pInDelayResCtrl;
352 
353  uint32_t *pSmaLineDelay;
355 
360 typedef struct
361 {
362  int32_t cDelay[DEBUG_DELAY_N_ITER];
363  uint32_t lDelay[DEBUG_DELAY_N_ITER];
364  float rcfPeer[DEBUG_DELAY_N_ITER];
365  uint32_t reqDelay[DEBUG_DELAY_N_ITER];
366  uint32_t resDelay[DEBUG_DELAY_N_ITER];
367  uint16_t seqId[DEBUG_DELAY_N_ITER];
368 } debugDelay_t;
369 
374 typedef struct
375 {
376  int32_t syncDeltaT[DEBUG_SYNC_N_ITER];
377  int32_t syncSmaDeltaT[DEBUG_SYNC_N_ITER];
378 
379  uint32_t syncLocalT[DEBUG_SYNC_N_ITER];
380  uint32_t syncMasterT[DEBUG_SYNC_N_ITER];
381 
382  uint32_t syncTorgT[DEBUG_SYNC_N_ITER];
383  uint32_t syncInDelayPlusLDT[DEBUG_SYNC_N_ITER];
384 } debugSync_t;
385 
386 
387 #define SYNC_SINGLE_ITER_SIZE 200
388 
392 typedef struct
393 {
394  int32_t deltaT[SYNC_SINGLE_ITER_SIZE];
395  int32_t smaDeltaT[SYNC_SINGLE_ITER_SIZE];
396  int32_t seqId[SYNC_SINGLE_ITER_SIZE];
397  int32_t tState[SYNC_SINGLE_ITER_SIZE];
398  int32_t tSeqId[SYNC_SINGLE_ITER_SIZE];
399  int32_t cycleTime[SYNC_SINGLE_ITER_SIZE];
401 
402 /* ========================================================================== */
403 /* Function Declarations */
404 /* ========================================================================== */
405 
412 void PN_PTCP_start(PN_Handle pnHandle);
413 
420 void PN_PTCP_ClockChange(PN_Handle pnHandle, uint32_t cycleTime);
421 
426 void PN_PTCP_reset(PN_Handle pnHandle);
427 
432 void PN_PTCP_init(PN_Handle pnHandle);
433 
440 
447 
454 
463  ptcp_iDelayResp_struct_t *ptcp_iDelayResp_parsed);
464 
477  *ptcp_iDelayResp_parsed, uint8_t *ptcp_iDelayResp_packet,
478  uint8_t *ptcp_iDelayFupResp_packet, int32_t w_FUP);
479 
489  ptcp_iDelayResp_struct_t *ptcp_iDelayResp_parsed, uint8_t port);
490 
498 uint32_t PN_PTCP_rotUint(uint32_t *input);
499 
507 uint16_t PN_PTCP_rotUshort(uint16_t *input);
508 
515 int32_t PN_PTCP_setupIsr(PN_Handle pnHandle);
516 
521 void PN_PTCP_isrHandler(void* arg);
522 
529 int32_t PN_PTCP_enableIsr(PN_Handle pnHandle);
530 
537 
545 void PN_PTCP_syncIepAdjustment(PN_Handle pnHandle, int32_t ecapPeriod,
546  uint32_t compensation);
547 
558  volatile ptcpSyncInfo_t *ptcp_sync_parsed, uint8_t *sync_sblock);
559 
560 
570 void PN_PTCP_syncPreprocess(PN_Handle pnHandle, uint8_t ctrlByte);
571 
579 void PN_PTCP_portDelaySmaCalc(PN_Handle pnHandle, uint8_t portNum);
580 
588 void PN_PTCP_processDelayResponse(PN_Handle pnHandle, uint8_t portNum);
589 
597 void PN_PTCP_resetDelayTimings(PN_Handle pnHandle, uint8_t portNum);
598 
607 int32_t PN_PTCP_adjCtrDiff(PN_Handle pnHandle, int32_t ctrDiff);
608 
616 int32_t PN_PTCP_absVal(int32_t num);
617 
624 
631 void PN_PTCP_taskSleep(uint32_t mSec);
632 
639 
648 int32_t PN_PTCP_modFunc(int32_t num, uint32_t mod);
649 
662 #ifdef __cplusplus
663 }
664 #endif
665 
666 #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:293
ptcp_iDelayResp_struct_t::line_delay
uint32_t line_delay
Definition: iPtcpUtils.h:298
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:98
SYNC_SINGLE_ITER_SIZE
#define SYNC_SINGLE_ITER_SIZE
Definition: iPtcpUtils.h:387
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:347
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:247
devicePortOffsets_t::pInDelayResPacket
uint8_t * pInDelayResPacket
Definition: iPtcpUtils.h:349
DEBUG_DELAY_N_ITER
#define DEBUG_DELAY_N_ITER
Definition: iPtcpUtils.h:237
PN_PTCP_portDelaySmaCalc
void PN_PTCP_portDelaySmaCalc(PN_Handle pnHandle, uint8_t portNum)
ptcp_iDelayResp_struct_t::T4PortRXDelay
uint32_t T4PortRXDelay
Definition: iPtcpUtils.h:287
currentPtcpStatus_t::nSyncMissed
uint32_t nSyncMissed
Definition: iPtcpUtils.h:318
syncAnalysis_t
Definition: iPtcpUtils.h:393
debugDelay_t
Definition: iPtcpUtils.h:361
currentPtcpStatus_t::takeoverTimeoutFactor
uint32_t takeoverTimeoutFactor
Definition: iPtcpUtils.h:314
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:292
devicePortOffsets_t::pT4TS
uint32_t * pT4TS
Definition: iPtcpUtils.h:341
currentPtcpStatus_t::syncState
syncState_t syncState
Definition: iPtcpUtils.h:307
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:340
currentPtcpStatus_t::syncPllWnd
uint32_t syncPllWnd
Definition: iPtcpUtils.h:312
ptcp_iDelayResp_struct_t::T1_cycle_ctr
uint32_t T1_cycle_ctr
Definition: iPtcpUtils.h:291
devicePortOffsets_t::pInDelayFupResPacket
uint8_t * pInDelayFupResPacket
Definition: iPtcpUtils.h:350
debugSync_t
Definition: iPtcpUtils.h:375
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:296
ptcp_iDelayResp_struct_t::seqId
uint16_t seqId
Definition: iPtcpUtils.h:283
ptcp_iDelayResp_struct_t::T3PortTXDelay
uint32_t T3PortTXDelay
Definition: iPtcpUtils.h:286
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:351
PN_PTCP_delayMeasurement
void PN_PTCP_delayMeasurement(PN_Handle pnHandle)
ptcp_iDelayResp_struct_t::T2PortRXDelay
uint32_t T2PortRXDelay
Definition: iPtcpUtils.h:285
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:288
PN_Handle.h
currentPtcpStatus_t::syncRcv
uint8_t syncRcv
Definition: iPtcpUtils.h:317
ptcp_iDelayResp_struct_t::T4TimeStamp
uint32_t T4TimeStamp
Definition: iPtcpUtils.h:290
ptcp_iDelayResp_struct_t::T1PortTXDelay
uint32_t T1PortTXDelay
Definition: iPtcpUtils.h:284
ptcp_iDelayResp_struct_t::rcf_peer
float rcf_peer
Definition: iPtcpUtils.h:295
devicePortOffsets_t::pDelayReqPacket
uint8_t * pDelayReqPacket
Definition: iPtcpUtils.h:345
devicePortOffsets_t
Definition: iPtcpUtils.h:339
deviceDelays_t
Definition: iPtcpUtils.h:329
currentPtcpStatus_t::syncTimeoutFactor
uint32_t syncTimeoutFactor
Definition: iPtcpUtils.h:313
ptcp_iDelayResp_struct_t::resDelay
uint32_t resDelay
Definition: iPtcpUtils.h:294
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:282
ptcp_iDelayResp_struct_t::T2TimeStamp
uint32_t T2TimeStamp
Definition: iPtcpUtils.h:289
devicePortOffsets_t::pT4CycleCtr
uint32_t * pT4CycleCtr
Definition: iPtcpUtils.h:343
PN_PTCP_taskSleep
void PN_PTCP_taskSleep(uint32_t mSec)
ptcp_iDelayResp_struct_t::cable_delay
uint32_t cable_delay
Definition: iPtcpUtils.h:297
ptcpSyncInfo_t
Structure containing current sync info.
Definition: iPtcpDrv.h:108
currentPtcpStatus_t::firstSyncRcv
uint8_t firstSyncRcv
Definition: iPtcpUtils.h:316
devicePortOffsets_t::pDelayResPacket
uint8_t * pDelayResPacket
Definition: iPtcpUtils.h:346
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:306
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:353
PN_PTCP_modFunc
int32_t PN_PTCP_modFunc(int32_t num, uint32_t mod)
devicePortOffsets_t::pT1CycleCtr
uint32_t * pT1CycleCtr
Definition: iPtcpUtils.h:342