tsn_ptp_stack.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*!
  2. * Copyright (C) Fraunhofer-Institut for Photonic Microsystems (IPMS)
  3. * Maria-Reiche-Str. 2
  4. * 01109 Dresden
  5. *
  6. * Unauthorized copying of this file, via any medium is strictly prohibited
  7. * Proprietary and confidential
  8. *
  9. * \file tsn_ptp_stack.h
  10. * \author zimmerli
  11. * \date 2019-01-17
  12. * \brief IEEE 802.1as software protocol stack (header)
  13. *
  14. */
  15. #ifndef TSN_PTP_PROTO_H_
  16. #define TSN_PTP_PROTO_H_
  17. #include "tsn_ptp.h"
  18. /**
  19. * \brief Timer event enumeration
  20. *
  21. * Timer index used by functions hookSetTimer and tsn_ptp_stack_timerevent
  22. */
  23. typedef enum ptp_timer_event {
  24. PTP_TMR_EVENT_TXPDREQ = 0,
  25. PTP_TMR_EVENT_TXANNC = 1,
  26. PTP_TMR_EVENT_TXSYNC = 2,
  27. PTP_TMR_EVENT_RXANNC = 3,
  28. PTP_TMR_EVENT_RXSYNC = 4,
  29. } ptp_timer_event_t;
  30. /**
  31. * \brief Intsance status event enumeration
  32. *
  33. */
  34. typedef enum {
  35. PTP_STS_EVENT_SYNCLOST,
  36. PTP_STS_EVENT_SYNCRECV,
  37. PTP_STS_EVENT_PHASE_CHANGE,
  38. PTP_STS_EVENT_FREQ_CHANGE
  39. } ptp_status_event_t;
  40. /**
  41. * \brief Per-Port global variables
  42. * IEEE 802.1as 10.2.4
  43. * no delayAsymmetry (10.2.4.8)
  44. * no pttPortEnable
  45. * neighborPropDelay accuracy and range large enough using uint32_t
  46. */
  47. typedef struct {
  48. uint8_t asCapable; //!< bool
  49. uint8_t enabled; //!< bool
  50. int8_t curLogSyncInterval; //!< currentLogSyncInterval
  51. int32_t neighborRateOfs32; //!< (neighborRateRatio-1.0)<<32
  52. uint32_t meanLinkDelay; //!< PeerDelay in ns
  53. uint8_t computeNeighborRateRatio; //!< bool
  54. uint8_t computeMeanLinkDelay; //!< bool
  55. uint16_t thisPort; //!< port number
  56. uint8_t selectedRole; //!< port role
  57. port_id_t thisPortId; //!< port identity
  58. uint8_t delayMechanism; //!< delayMechanism IEEE 802.1as-2020 14.8.5
  59. uint16_t portIndex; //!< internal port index variable
  60. uint8_t rcvdPortStateInd; //!< bool enabled external port role
  61. uint8_t rcvdPortState; //!< external port role
  62. } port_glob_t;
  63. /**
  64. * \brief Media-Dependent global variables
  65. *
  66. * IEEE 802.1as 11.2.12
  67. * initialLogPdelayReqInterval: fixed constant
  68. * pdelayReqInterval: calc from curLogPdelayReqInterval
  69. * allowedLostResponses: fixed constant
  70. * allowedFaults: fixed constatnt
  71. */
  72. typedef struct {
  73. int8_t curLogPdelayReqInterval; //!< curLogPdelayReqInterval
  74. uint8_t isMeasuringDelay; //!< bool, measunring PeerDelay
  75. uint32_t meanLinkDelayTresh; //!< PerrDelay threshold in ns
  76. uint16_t syncSeqId; //!< syncSeqId
  77. uint8_t detectedFaults; //!< detectedFaults
  78. uint8_t asCapableAcrossDomains; //!< bool, AsCapable for all domains
  79. } md_glob_t;
  80. /**
  81. * \brief Pdelay-response-statemachine internals
  82. */
  83. typedef struct {
  84. uint16_t seqid; //!< sequenceID
  85. port_id_t srcPortId; //!< sourcePortIdentity
  86. } md_pdelay_resp_sm;
  87. /**
  88. * \brief Pdelay-request-statemachine internals
  89. */
  90. typedef struct {
  91. uint16_t seqid; //!< sequence id
  92. uint64_t time_local_txreq; //!< t1 - pdelayReqEventEgressTimestamp
  93. uint64_t time_local_rxresp; //!< t4 - pdelayRespEventIngressTimestamp
  94. uint64_t time_peer_rxreq; //!< t2
  95. uint64_t time_peer_txresp; //!< t3
  96. uint64_t saved_local_rxresp; //!< helper for calc neighbor rate ratio
  97. uint64_t saved_peer_txresp; //!< helper for calc neighbor rate ratio
  98. uint8_t measIsRunning; //!< bool
  99. uint16_t lostResponses; //!< lostResponses counter
  100. uint8_t neighborRateRatioValid; //!< bool
  101. uint8_t eventflags; //!< events, control of calculation
  102. } md_pdelay_req_sm;
  103. /**
  104. * \brief Sync-Receive-statemachine internals
  105. */
  106. typedef struct {
  107. uint16_t seqid; //!< sequenceId
  108. uint8_t waitFup; //!< waiting_for_follow_up
  109. port_sync_sync_t pss; //!< PortSyncSync structure to PortSyncRecv sm
  110. } md_sync_receive_sm;
  111. /**
  112. * \brief Sync-Send-statemachine internals
  113. */
  114. typedef struct {
  115. uint16_t seqid; //!< next sequence id
  116. port_sync_sync_t pss; //!< PortSyncSync structure
  117. } md_sync_send_sm;
  118. /**
  119. * \brief Time informations
  120. */
  121. typedef struct {
  122. uint16_t flags; //!< leap59, leap61, curUtcOfsVal, timeTrace, freqTrace
  123. int16_t curUtcOfs; //!< difference (TAI - UTC) in seconds
  124. uint8_t timeSource; //!< timeSource, see enum ptp_timesource_enum
  125. } time_info_t;
  126. /**
  127. * Maximum announce message payload words (uint32)
  128. */
  129. #define PTP_ANNC_PAYLOAD_SZ_U32 (((PTP_PAYLOAD_SZ_ANNOUNCE + 8 * TSN_PTP_CFG_PATHTRACE_MAX) + 3) / 4)
  130. /**
  131. * \brief Announce Message Information
  132. */
  133. typedef struct {
  134. uint8_t newInfo; //!< boolean flag
  135. uint8_t infoIs; //!< disabled, aged, mine, received
  136. ptp_prio_vect_t portPriority; //!< port priority vector
  137. uint16_t portStepsRemoved; //!< stepsRemoved
  138. time_info_t annInfo; //!< annInfo
  139. uint16_t txSeqId; //!< txSeqId
  140. int8_t curLogInterval; //!< curLogInterval
  141. uint32_t payloadBuf[PTP_ANNC_PAYLOAD_SZ_U32]; //!< only used for building announce payload
  142. } annc_info_sm;
  143. /**
  144. * \brief Per-Port strcuture
  145. */
  146. typedef struct {
  147. port_glob_t port; //!< port variables
  148. md_glob_t md; //!< media-dependent variables
  149. md_pdelay_resp_sm mdPdResp; //!< mdPdResp
  150. md_pdelay_req_sm mdPdReq; //!< mdPdReq
  151. md_sync_receive_sm mdSyncRecv; //!< mdSyncRecv
  152. md_sync_send_sm mdSyncSend; //!< mdSyncSend
  153. annc_info_sm anncInfo; //!< anncInfo
  154. } tsn_ptp_port_t;
  155. /**
  156. * \brief LocalClock strcuture
  157. */
  158. typedef struct {
  159. uint64_t offset_ns;
  160. scaled_ns_t lastPhaseChange;
  161. int32_t rateOfs;
  162. uint32_t fractOfs;
  163. uint64_t last_localtime;
  164. } tsn_ptp_clock_t;
  165. /**
  166. * \brief SiteSync strcuture
  167. */
  168. typedef struct {
  169. uint8_t hasSavedSync;
  170. int32_t gmRateOfs; // (ratio-1.0)<<32
  171. int64_t savedSyncReceiptTime;
  172. int64_t savedSyncReceiptLocalTime;
  173. scaled_ns_t clkSrcPhOfs;
  174. uint16_t clkTimeBaseInd;
  175. int32_t clkFrqOfsScaled;
  176. int32_t gmOffset;
  177. int32_t gmOffsetSum;
  178. int8_t logSyncIval;
  179. uint8_t hasSyncRcvd;
  180. } tsn_ptp_sync_t;
  181. /**
  182. * \brief PTP Stack main management structure
  183. *
  184. * includes time-aware system global variables, 10.2.3
  185. */
  186. typedef struct {
  187. void *cbParam; //!< callback parameter for hook functions
  188. void (*hookTxFrame)(void *cbParam, uint16_t portIndex, struct ptp_header_s *pPtpHeader, uint8_t *pPayload,
  189. uint32_t payloadLen); //!< hook, called for sending frame
  190. void (*hookGetLocalTime)(void *cbParam, ptp_timestamp_t *localtime);
  191. void (*hookGetLocalOffset)(void *cbParam, ptp_timestamp_t *offset);
  192. void (*hookStatusEvent)(void *cbParam, ptp_status_event_t event, void *param);
  193. void (*hookSetTimer)(void *cbParam, ptp_timer_event_t timer, uint32_t id, int8_t logInterval,
  194. uint8_t mult); //!< hook, called for settingh timer; logInterval 127: disable
  195. clock_id thisClock; //!< clockIdentity of system, 10.2.3.22
  196. tsn_ptp_port_t port[TSN_PTP_CFG_PORTNUM]; //!< port structures
  197. clock_id pathTrace[TSN_PTP_CFG_PATHTRACE_MAX]; //!< pathTrace, 10.3.8.21
  198. uint32_t pathTraceCount; //!< pathTraceCount
  199. ptp_system_id_t systemId; //!< systemId
  200. ptp_prio_vect_t gmPriority; //!< current GM priority vector
  201. uint16_t gmChanges; //!< counter, incrementing when GM changes
  202. uint16_t masterStepsRemoved; //!< masterStepsRemoved
  203. time_info_t sysInfo; //!< sysInfo
  204. time_info_t announceInfo; //!< announceInfo
  205. uint8_t selectedRole0; //!< role of system, see enum ptp_port_role_enum
  206. uint8_t domainNumber; //!< domain number of PTP instance
  207. tsn_ptp_sync_t siteSync; //!< sync structure
  208. tsn_ptp_clock_t localClock; //!< local clock structure of PTP instance
  209. uint8_t enableInvoke; //!< enable the status timing invoke calls
  210. } tsn_ptp_instance_t;
  211. /**
  212. * TSN PTP Stack initialization
  213. * Must be called before first use of stack
  214. * @param pStack pointer to stack
  215. * @param pMacAddr pointer to MAC address (uint8_t[6])
  216. * @param domainNumber domain number of the PTP instance
  217. */
  218. void tsn_ptp_stack_init(tsn_ptp_instance_t *pStack, uint8_t *pMacAddr, uint8_t domainNumber);
  219. /**
  220. * Perform action when a PTP frame is received on a port
  221. *
  222. * @param pStack pointer to PTP stack
  223. * @param portIndex port index (0 .. N-1)
  224. * @param hdrAddr address of start of PTP header of received message
  225. * @param length length of the PTP frame (valid data after hdrAddr)
  226. * @param pRxTime pointer to ingressTimestamp = timestamp to reference plane (timestamp corrected by ingress latency)
  227. */
  228. void tsn_ptp_stack_rxframe(tsn_ptp_instance_t *pStack, uint16_t portIndex, uintptr_t hdrAddr, uint32_t length, ptp_timestamp_t *pRxTime);
  229. /**
  230. * Call when transmission of a PTP frame is done and a tx timestamp is available.
  231. * The provided timestamp shall already be corrected by egress latency!
  232. *
  233. * @param pPort pointer to the protocol structure of the port
  234. * @param msgType PTP message type of the transmitted frame
  235. * @param pTxTime pointer to tx timestamp (ptp_timestamp_t)
  236. */
  237. void tsn_ptp_stack_txdone(tsn_ptp_instance_t *pStack, tsn_ptp_port_t *pPort, uint8_t msgType, ptp_timestamp_t *pTxTime);
  238. /**
  239. * Port role reselection
  240. * Must be run after hookReselectRoles has been called by any port
  241. *
  242. * @param pStack pointer to stack
  243. */
  244. void tsn_ptp_stack_portroleselection(tsn_ptp_instance_t *pStack);
  245. /**
  246. * Timer Event
  247. *
  248. * @param pStack pointer to PTP stack
  249. * @param tmrEvent event enumeration
  250. * @param id identifier is provided by hookSetTimer callback
  251. */
  252. void tsn_ptp_stack_timerevent(tsn_ptp_instance_t *pStack, ptp_timer_event_t tmrEvent, uint32_t id);
  253. /**
  254. * Enable or Disable Port
  255. * Enabling calls timer init hooks
  256. *
  257. * @param pStack pointer to PTP stack
  258. * @param portIndex index of port (0 .. N-1)
  259. * @param bEnable boolean, 0 - disable, otherwise enable
  260. */
  261. void tsn_ptp_stack_port_enable(tsn_ptp_instance_t *pStack, uint16_t portIndex, uint16_t bEnable);
  262. /**
  263. * Send PortSyncSync structure
  264. * called by SiteSyncSync state machine, when Sync was received
  265. *
  266. * @param pStack pointer to PTP stack
  267. * @param pPSS pointer to PortSyncSync structure (as received by hookRecvPortSyncSync)
  268. */
  269. void tsn_ptp_stack_send_pss(tsn_ptp_instance_t *pStack, port_sync_sync_t *pPSS);
  270. uint8_t tsn_ptp_msg(uintptr_t hdrAddr);
  271. uint8_t tsn_ptp_domain(uintptr_t hdrAddr);
  272. uint64_t tsn_ptp_clock_offset(tsn_ptp_instance_t *pStack);
  273. uint64_t tsn_ptp_time_get(tsn_ptp_instance_t *pStack);
  274. void tsn_ptp_stack_enable(tsn_ptp_instance_t *pStack, uint16_t bEnable);
  275. #endif /* TSN_PTP_PROTO_H_ */