tsn_ptp.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.h
  10. * \author zimmerli
  11. * \date 2019-01-14
  12. * \brief IEEE 802.1as SW-Implementation, low-level, (Header)
  13. *
  14. */
  15. #ifndef TSN_PTP_H_
  16. #define TSN_PTP_H_
  17. #include <stdint.h>
  18. #include "tsn_ptp_types.h"
  19. #include "tsn_ptp_config.h"
  20. extern const uint8_t tsn_ptp_macaddr[6];
  21. /**
  22. * EtherType used by PTP communication
  23. */
  24. #define ETH_TYPE_PTP 0x88F7
  25. #define PTP_MAJOR_SDO_ID 0x1
  26. #define PTP_MINOR_SDO_ID 0x0
  27. #define PTP_VERSION 0x2
  28. #define PTP_MSGTYPE_SYNC 0x0
  29. #define PTP_MSGTYPE_PDELAY_REQ 0x2
  30. #define PTP_MSGTYPE_PDELAY_RESP 0x3
  31. #define PTP_MSGTYPE_FOLLOWUP 0x8
  32. #define PTP_MSGTYPE_PDELAY_FUP 0xA
  33. #define PTP_MSGTYPE_ANNOUNCE 0xB
  34. #define PTP_MSGTYPE_SIGNALING 0xC
  35. #define PTP_MSGTYPE_MASK 0xF
  36. #define PTP_CONTROL_SYNC 0x0
  37. #define PTP_CONTROL_FOLLOWUP 0x2
  38. #define PTP_CONTROL_OTHER 0x5
  39. #define PTP_HEADER_SZ 34
  40. #define PTP_PAYLOAD_SZ_PDELAY 20
  41. #define PTP_PAYLOAD_SZ_SYNC 10
  42. #define PTP_PAYLOAD_SZ_FOLLOWUP 42
  43. #define PTP_PAYLOAD_SZ_SIGNAL 26
  44. #define PTP_PAYLOAD_SZ_ANNOUNCE 34
  45. #define PTP_ALLOW_LOST_RESP 3
  46. #define PTP_ALLOW_PREQ_FAULTS 9
  47. #define PTP_INIT_INTVAL_SYNC -3
  48. #define PTP_INIT_INTVAL_ANNC 0
  49. #define PTP_INIT_INTVAL_PDELAY 0
  50. #define PTP_SYNC_ADJ_FREQ_IVAL 1000000000LL
  51. #define PTP_SYNC_RATE_OFS_TH (0x3000000)
  52. #define PTP_TLV_TYPE_ORG_EXT 0x3
  53. #define PTP_TLV_TYPE_PATHTRACE 0x8
  54. #define PTP_PATHTRACE_SZ 8
  55. #define PTP_FLAG_SIGNALING_CNRR (1 << 1) //!< flag computeNeighborRateRatio
  56. #define PTP_FLAG_SIGNALING_CNPD (1 << 2) //!< flag computeNeighborPropDelay
  57. /**
  58. * Type of source of time used by clock
  59. * Information only attribute, not used for GM selection,
  60. * see Table 8-3 for time source enumeration,
  61. * see 7.6.2.6 of IEEE Std 1588 for more details
  62. */
  63. enum ptp_timesource_enum {
  64. PTP_SRC_ATOMIC_CLOCK = 0x10,
  65. PTP_SRC_GPS = 0x20,
  66. PTP_SRC_TERREST_RADIO = 0x30,
  67. PTP_SRC_PTP = 0x40,
  68. PTP_SRC_NTP = 0x50,
  69. PTP_SRC_HAND_SET = 0x60,
  70. PTP_SRC_OTHER = 0x90,
  71. PTP_SRC_INTERNAL_OSC = 0xA0
  72. };
  73. /**
  74. * \brief PTP Flags, Uint16
  75. *
  76. * defined and used in network byte order
  77. */
  78. enum ptp_flags_enum {
  79. PTP_FLAG_TWO_STEP = (1 << 1),
  80. PTP_FLAG_LEAP61 = (1 << (8 + 0)),
  81. PTP_FLAG_LEAP59 = (1 << (8 + 1)),
  82. PTP_FLAG_CURUTCO_VALID = (1 << (8 + 2)),
  83. PTP_FLAG_PTP_TIMESCALE = (1 << (8 + 3)),
  84. PTP_FLAG_TIME_TRACEABLE = (1 << (8 + 4)),
  85. PTP_FLAG_FREQ_TRACEABLE = (1 << (8 + 5))
  86. };
  87. /**
  88. * \brief PTP Port state enumeration
  89. *
  90. * see IEEE1588
  91. * see IEEE802.1as - 14.6.3
  92. */
  93. enum ptp_port_role_enum {
  94. PTP_PORT_ROLE_DISABLED = 3,
  95. PTP_PORT_ROLE_MASTER = 6,
  96. PTP_PORT_ROLE_PASSIVE = 7,
  97. PTP_PORT_ROLE_SLAVE = 9
  98. };
  99. /**
  100. * \brief PTP header information
  101. */
  102. struct ptp_header_s {
  103. uint8_t msgType;
  104. uint8_t majorSdoId; // transportSpecific in 802.1AS-2011
  105. uint8_t version;
  106. uint8_t minorVersionPTP;
  107. uint16_t msgLen;
  108. uint8_t domainNumber;
  109. uint8_t minorSdoId; // reserved in 802.1AS-2011
  110. uint16_t flags;
  111. int64_t correctionField;
  112. port_id_t sourcePortId;
  113. uint16_t sequenceId;
  114. uint8_t control;
  115. int8_t logMsgInterval;
  116. };
  117. /**
  118. * \brief PTP FollowUp message information
  119. */
  120. struct ptp_followup_s {
  121. ptp_timestamp_t preciseOriginTimestamp;
  122. int32_t cumScaledRateOfs; // cumulativeScaledRateOffset
  123. uint16_t gmTimeBaseInd; // gmTimeBaseIndicator
  124. scaled_ns_t lastGmPhaseCh;
  125. int32_t scaledLastGmFreqCh;
  126. };
  127. /**
  128. * \brief PTP signaling TLV information
  129. */
  130. struct ptp_signaling_tlv_s {
  131. int8_t linkDelayInterval;
  132. int8_t timeSyncInterval;
  133. int8_t announceInterval;
  134. uint8_t flags;
  135. };
  136. /**
  137. * \brief PTP Announce information
  138. */
  139. struct ptp_announce_s {
  140. int16_t currentUtcOfs;
  141. uint8_t gmPrio1;
  142. uint8_t gmPrio2;
  143. clock_quality_t clockQuality;
  144. clock_id gmIdentity;
  145. uint16_t stepsRemoved;
  146. uint8_t timeSource;
  147. uint16_t pathCount;
  148. uint8_t *pPathSequence;
  149. };
  150. /**
  151. * Set ethernet header information in destination memory
  152. *
  153. * @param dstaddr Destination Address
  154. * @param pMacAddr Pointer to MAC address (expected array of 6 uint8_t)
  155. * @return returns length of header in bytes (always 14)
  156. */
  157. uint32_t tsn_ptp_set_eth_header(uintptr_t dstaddr, uint8_t *pMacAddr);
  158. /**
  159. * Set PTP header information in destination memory
  160. *
  161. * @param dstaddr Destination Address
  162. * @param pPtp Pointer to struct ptp_header_s, containing header information
  163. * @return returns length of header in bytes (always 34)
  164. */
  165. uint32_t tsn_ptp_set_ptp_header(uintptr_t dstaddr, struct ptp_header_s *pPtp);
  166. /**
  167. * Adjust PTP timestamp by nanosecond value (primarily used for ingress/egress latency adjustment)
  168. *
  169. * @param pTimestamp pointer to timestamp
  170. * @param adjustNs adjustement value in nanoseconds, must not exceed 1 second (10^9-1)
  171. */
  172. void tsn_ptp_adjust_timestamp(ptp_timestamp_t *pTimestamp, int32_t adjustNs);
  173. void tsn_ptp_mac_to_clockid(const uint8_t *pMacAddr, uint8_t *pClockId);
  174. uint32_t tsn_ptp_set_tstamp(uintptr_t dstaddr, ptp_timestamp_t *pTimestamp);
  175. void tsn_ptp_get_tstamp(uintptr_t srcaddr, ptp_timestamp_t *pTimestamp);
  176. uint32_t tsn_ptp_set_portid(uintptr_t dstaddr, port_id_t *pPortId);
  177. void tsn_ptp_get_portid(uintptr_t srcaddr, port_id_t *pPortId);
  178. uint16_t tsn_ptp_get_flags(uintptr_t srcaddr);
  179. uint32_t tsn_ptp_set_pdelay_req(uintptr_t dstaddr);
  180. uint32_t tsn_ptp_set_pdelay_resp(uintptr_t dstaddr, ptp_timestamp_t *pRxTstamp, port_id_t *pReqPortId);
  181. uint32_t tsn_ptp_set_pdelay_fup(uintptr_t dstaddr, ptp_timestamp_t *pRespOrigTstamp, port_id_t *pReqPortId);
  182. uint32_t tsn_ptp_set_sync(uintptr_t dstaddr);
  183. uint32_t tsn_ptp_set_followup(uintptr_t dstaddr, struct ptp_followup_s *pFollowup);
  184. uint32_t tsn_ptp_set_signaling(uintptr_t dstaddr, struct ptp_signaling_tlv_s *pSignaling);
  185. uint32_t tsn_ptp_set_announce(uintptr_t dstaddr, struct ptp_announce_s *pAnnounce);
  186. int32_t tsn_ptp_get_followup(uintptr_t addr, struct ptp_followup_s *pFollowup);
  187. int32_t tsn_ptp_get_announce(uintptr_t addr, struct ptp_announce_s *pAnnounce);
  188. int32_t tsn_ptp_cmp_systemid(ptp_system_id_t *pSysIdA, ptp_system_id_t *pSysIdB);
  189. int32_t tsn_ptp_cmp_priovect(ptp_prio_vect_t *pPrioA, ptp_prio_vect_t *pPrioB);
  190. #endif /* TSN_PTP_H_ */