AM261x 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 #ifdef PROFINET_RGMII_MODE
238 #define PORT_RX_DELAY 374+160 //FIXME : MAC delay needs to be accounted correctly
239 #else
240 #if defined(SOC_AM64X) || defined (SOC_AM243X)
241 #define PORT_RX_DELAY 220
242 #else
243 #define PORT_RX_DELAY 250
244 #endif
245 #endif
246 
252 #ifdef PROFINET_RGMII_MODE
253 #define PORT_TX_DELAY 248+160 //FIXME : MAC delay needs to be accounted correctly
254 #else
255 #if defined(SOC_AM64X) || defined (SOC_AM243X)
256 #define PORT_TX_DELAY 64
257 #else
258 #define PORT_TX_DELAY 40
259 #endif
260 #endif
261 
262 
268 #define DEBUG_DELAY_N_ITER 200
269 
275 #ifdef PTCP_DEBUG_SI
276 #define DEBUG_SYNC_N_ITER 300
277 #else
278 #define DEBUG_SYNC_N_ITER 40000
279 #endif
280 
286 #define SYNC_FILTER_SIZE 8
287 
293 #define SYNC_ANALYSIS_N_ITER 100
294 
300 #define SYNC_ANALYSIS_N_RESETS 40
301 
302 #define LATCH0_EVENT 12/* Latch Event number */
303 
304 /* ========================================================================== */
305 /* Structure Declarations */
306 /* ========================================================================== */
307 
312 typedef struct
313 {
314  uint16_t seqId;
315  uint32_t T1PortTXDelay;
316  uint32_t T2PortRXDelay;
317  uint32_t T3PortTXDelay;
318  uint32_t T4PortRXDelay;
319  uint32_t T1TimeStamp;
320  uint32_t T2TimeStamp;
321  uint32_t T4TimeStamp;
322  uint32_t T1_cycle_ctr;
323  uint32_t T4_cycle_ctr;
324  uint32_t reqDelay;
325  uint32_t resDelay;
326  float rcf_peer;
327  uint32_t resDelay_peer;
328  uint32_t cable_delay;
329  uint32_t line_delay;
331 
336 typedef struct
337 {
339  /*TODO: Review NUM_PORTS -> PTCP_NUM_PORTS at all places*/
340  uint32_t cDelay[PTCP_NUM_PORTS];
342 
343  uint32_t syncPllWnd;
346 
347  uint8_t firstSyncRcv;
348  uint8_t syncRcv;
349  uint32_t nSyncMissed;
350 
351  uint8_t subdomainUUID[16];
352  uint8_t masterSA[6];
354 
359 typedef struct
360 {
361  uint32_t lDelays[NUM_DELAYS_SMA];
362  uint32_t cDelays[NUM_DELAYS_SMA];
364 
369 typedef struct
370 {
371  uint32_t *pT1TS;
372  uint32_t *pT4TS;
373  uint32_t *pT1CycleCtr;
374  uint32_t *pT4CycleCtr;
375 
376  uint8_t *pDelayReqPacket;
377  uint8_t *pDelayResPacket;
379 
382  uint8_t *pInDelayResCtrl;
383 
384  uint32_t *pSmaLineDelay;
386 
391 typedef struct
392 {
393  int32_t cDelay[DEBUG_DELAY_N_ITER];
394  uint32_t lDelay[DEBUG_DELAY_N_ITER];
395  float rcfPeer[DEBUG_DELAY_N_ITER];
396  uint32_t reqDelay[DEBUG_DELAY_N_ITER];
397  uint32_t resDelay[DEBUG_DELAY_N_ITER];
398  uint16_t seqId[DEBUG_DELAY_N_ITER];
399 } debugDelay_t;
400 
405 typedef struct
406 {
407  int32_t syncDeltaT[DEBUG_SYNC_N_ITER];
408  int32_t syncSmaDeltaT[DEBUG_SYNC_N_ITER];
409 
410  uint32_t syncLocalT[DEBUG_SYNC_N_ITER];
411  uint32_t syncMasterT[DEBUG_SYNC_N_ITER];
412 
413  uint32_t syncTorgT[DEBUG_SYNC_N_ITER];
414  uint32_t syncInDelayPlusLDT[DEBUG_SYNC_N_ITER];
415 } debugSync_t;
416 
417 
418 #define SYNC_SINGLE_ITER_SIZE 200
419 
423 typedef struct
424 {
425  int32_t deltaT[SYNC_SINGLE_ITER_SIZE];
426  int32_t smaDeltaT[SYNC_SINGLE_ITER_SIZE];
427  int32_t seqId[SYNC_SINGLE_ITER_SIZE];
428  int32_t tState[SYNC_SINGLE_ITER_SIZE];
429  int32_t tSeqId[SYNC_SINGLE_ITER_SIZE];
430  int32_t cycleTime[SYNC_SINGLE_ITER_SIZE];
432 
433 /* ========================================================================== */
434 /* Function Declarations */
435 /* ========================================================================== */
436 
443 void PN_PTCP_start(PN_Handle pnHandle);
444 
451 void PN_PTCP_ClockChange(PN_Handle pnHandle, uint32_t cycleTime);
452 
457 void PN_PTCP_reset(PN_Handle pnHandle);
458 
463 void PN_PTCP_init(PN_Handle pnHandle);
464 
471 
478 
485 
494  ptcp_iDelayResp_struct_t *ptcp_iDelayResp_parsed);
495 
508  *ptcp_iDelayResp_parsed, uint8_t *ptcp_iDelayResp_packet,
509  uint8_t *ptcp_iDelayFupResp_packet, int32_t w_FUP);
510 
520  ptcp_iDelayResp_struct_t *ptcp_iDelayResp_parsed, uint8_t port);
521 
529 uint32_t PN_PTCP_rotUint(uint32_t *input);
530 
538 uint16_t PN_PTCP_rotUshort(uint16_t *input);
539 
546 int32_t PN_PTCP_setupIsr(PN_Handle pnHandle);
547 
552 void PN_PTCP_isrHandler(void* arg);
553 
560 int32_t PN_PTCP_enableIsr(PN_Handle pnHandle);
561 
568 
576 void PN_PTCP_syncIepAdjustment(PN_Handle pnHandle, int32_t ecapPeriod,
577  uint32_t compensation);
578 
589  volatile ptcpSyncInfo_t *ptcp_sync_parsed, uint8_t *sync_sblock);
590 
591 
601 void PN_PTCP_syncPreprocess(PN_Handle pnHandle, uint8_t ctrlByte);
602 
610 void PN_PTCP_portDelaySmaCalc(PN_Handle pnHandle, uint8_t portNum);
611 
619 void PN_PTCP_processDelayResponse(PN_Handle pnHandle, uint8_t portNum);
620 
628 void PN_PTCP_resetDelayTimings(PN_Handle pnHandle, uint8_t portNum);
629 
638 int32_t PN_PTCP_adjCtrDiff(PN_Handle pnHandle, int32_t ctrDiff);
639 
647 int32_t PN_PTCP_absVal(int32_t num);
648 
655 
662 void PN_PTCP_taskSleep(uint32_t mSec);
663 
670 
679 int32_t PN_PTCP_modFunc(int32_t num, uint32_t mod);
680 
693 #ifdef __cplusplus
694 }
695 #endif
696 
697 #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:324
ptcp_iDelayResp_struct_t::line_delay
uint32_t line_delay
Definition: iPtcpUtils.h:329
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:418
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:378
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:278
devicePortOffsets_t::pInDelayResPacket
uint8_t * pInDelayResPacket
Definition: iPtcpUtils.h:380
DEBUG_DELAY_N_ITER
#define DEBUG_DELAY_N_ITER
Definition: iPtcpUtils.h:268
PN_PTCP_portDelaySmaCalc
void PN_PTCP_portDelaySmaCalc(PN_Handle pnHandle, uint8_t portNum)
ptcp_iDelayResp_struct_t::T4PortRXDelay
uint32_t T4PortRXDelay
Definition: iPtcpUtils.h:318
currentPtcpStatus_t::nSyncMissed
uint32_t nSyncMissed
Definition: iPtcpUtils.h:349
syncAnalysis_t
Definition: iPtcpUtils.h:424
debugDelay_t
Definition: iPtcpUtils.h:392
currentPtcpStatus_t::takeoverTimeoutFactor
uint32_t takeoverTimeoutFactor
Definition: iPtcpUtils.h:345
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:323
devicePortOffsets_t::pT4TS
uint32_t * pT4TS
Definition: iPtcpUtils.h:372
currentPtcpStatus_t::syncState
syncState_t syncState
Definition: iPtcpUtils.h:338
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:371
currentPtcpStatus_t::syncPllWnd
uint32_t syncPllWnd
Definition: iPtcpUtils.h:343
ptcp_iDelayResp_struct_t::T1_cycle_ctr
uint32_t T1_cycle_ctr
Definition: iPtcpUtils.h:322
devicePortOffsets_t::pInDelayFupResPacket
uint8_t * pInDelayFupResPacket
Definition: iPtcpUtils.h:381
debugSync_t
Definition: iPtcpUtils.h:406
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:327
ptcp_iDelayResp_struct_t::seqId
uint16_t seqId
Definition: iPtcpUtils.h:314
ptcp_iDelayResp_struct_t::T3PortTXDelay
uint32_t T3PortTXDelay
Definition: iPtcpUtils.h:317
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:382
PN_PTCP_delayMeasurement
void PN_PTCP_delayMeasurement(PN_Handle pnHandle)
ptcp_iDelayResp_struct_t::T2PortRXDelay
uint32_t T2PortRXDelay
Definition: iPtcpUtils.h:316
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:319
PN_Handle.h
currentPtcpStatus_t::syncRcv
uint8_t syncRcv
Definition: iPtcpUtils.h:348
ptcp_iDelayResp_struct_t::T4TimeStamp
uint32_t T4TimeStamp
Definition: iPtcpUtils.h:321
ptcp_iDelayResp_struct_t::T1PortTXDelay
uint32_t T1PortTXDelay
Definition: iPtcpUtils.h:315
ptcp_iDelayResp_struct_t::rcf_peer
float rcf_peer
Definition: iPtcpUtils.h:326
devicePortOffsets_t::pDelayReqPacket
uint8_t * pDelayReqPacket
Definition: iPtcpUtils.h:376
devicePortOffsets_t
Definition: iPtcpUtils.h:370
deviceDelays_t
Definition: iPtcpUtils.h:360
currentPtcpStatus_t::syncTimeoutFactor
uint32_t syncTimeoutFactor
Definition: iPtcpUtils.h:344
ptcp_iDelayResp_struct_t::resDelay
uint32_t resDelay
Definition: iPtcpUtils.h:325
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:313
ptcp_iDelayResp_struct_t::T2TimeStamp
uint32_t T2TimeStamp
Definition: iPtcpUtils.h:320
devicePortOffsets_t::pT4CycleCtr
uint32_t * pT4CycleCtr
Definition: iPtcpUtils.h:374
PN_PTCP_taskSleep
void PN_PTCP_taskSleep(uint32_t mSec)
ptcp_iDelayResp_struct_t::cable_delay
uint32_t cable_delay
Definition: iPtcpUtils.h:328
ptcpSyncInfo_t
Structure containing current sync info.
Definition: iPtcpDrv.h:108
currentPtcpStatus_t::firstSyncRcv
uint8_t firstSyncRcv
Definition: iPtcpUtils.h:347
devicePortOffsets_t::pDelayResPacket
uint8_t * pDelayResPacket
Definition: iPtcpUtils.h:377
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:337
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:384
PN_PTCP_modFunc
int32_t PN_PTCP_modFunc(int32_t num, uint32_t mod)
devicePortOffsets_t::pT1CycleCtr
uint32_t * pT1CycleCtr
Definition: iPtcpUtils.h:373