hpm_ptpc_drv.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_PTPC_DRV_H
  8. #define HPM_PTPC_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_ptpc_regs.h"
  11. /**
  12. * @brief PTPC driver
  13. * @defgroup ptpc_interface PTPC driver APIs
  14. * @ingroup timer_interfaces Timers
  15. * @{
  16. */
  17. #define PTPC_EVENT_COMPARE0_MASK PTPC_INT_STS_COMP_INT_STS0_MASK
  18. #define PTPC_EVENT_CAPTURE0_MASK PTPC_INT_STS_CAPTURE_INT_STS0_MASK
  19. #define PTPC_EVENT_PPS0_MASK PTPC_INT_STS_PPS_INT_STS0_MASK
  20. #define PTPC_EVENT_COMPARE1_MASK PTPC_INT_STS_COMP_INT_STS1_MASK
  21. #define PTPC_EVENT_CAPTURE1_MASK PTPC_INT_STS_CAPTURE_INT_STS1_MASK
  22. #define PTPC_EVENT_PPS1_MASK PTPC_INT_STS_PPS_INT_STS1_MASK
  23. #define PTPC_MAX_NS_COUNTER (0x3B9ACA00UL)
  24. /**
  25. * @brief Counter types
  26. */
  27. typedef enum ptpc_ns_counter_rollover_type {
  28. ptpc_ns_counter_rollover_binary = 0, /**< binary mode, resolution ~0.466ns, overflow at 0x7FFFFFFF */
  29. ptpc_ns_counter_rollover_digital = 1, /**< digital mode, resolution 1ns, overflow at 0x3B9ACA00 */
  30. } ptpc_ns_counter_rollover_type_t;
  31. /**
  32. * @brief Capture trigger types
  33. */
  34. typedef enum ptpc_capture_trigger_type {
  35. ptpc_capture_trigger_none = 0,
  36. ptpc_capture_trigger_on_rising_edge = PTPC_PTPC_CTRL0_CAPT_SNAP_POS_EN_MASK,
  37. ptpc_capture_trigger_on_failing_edge = PTPC_PTPC_CTRL0_CAPT_SNAP_NEG_EN_MASK,
  38. ptpc_capture_trigger_on_both_edges = PTPC_PTPC_CTRL0_CAPT_SNAP_POS_EN_MASK
  39. | PTPC_PTPC_CTRL0_CAPT_SNAP_NEG_EN_MASK,
  40. } ptpc_capture_trigger_type_t;
  41. /**
  42. * @brief Capture trigger types
  43. */
  44. typedef enum ptpc_counting_mode {
  45. ptpc_counting_increment = 0, /**< Increment the counter */
  46. ptpc_counting_decrement = 1, /**< Decrement the counter */
  47. } ptpc_counting_mode;
  48. /**
  49. * @brief Timer config
  50. */
  51. typedef struct {
  52. ptpc_capture_trigger_type_t capture_trigger; /**< Capture trigger */
  53. ptpc_ns_counter_rollover_type_t ns_rollover_mode; /**< Ns rollover mode */
  54. bool coarse_increment; /**< Set false to use fine increment */
  55. bool capture_keep; /**< Keep captured timer counter */
  56. uint32_t src_frequency; /**< Source frequency */
  57. } ptpc_config_t;
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61. /**
  62. * @brief Configure ns counter rollover mode
  63. *
  64. * @param[in] ptr PTPC base address
  65. * @param[in] index Index of target counter
  66. * @param[in] rollover Rollover type
  67. */
  68. static inline void ptpc_set_ns_counter_rollover(PTPC_Type *ptr, uint8_t index,
  69. ptpc_ns_counter_rollover_type_t rollover)
  70. {
  71. ptr->PTPC[index].CTRL0 = (ptr->PTPC[index].CTRL0 & ~PTPC_PTPC_CTRL0_SUBSEC_DIGITAL_ROLLOVER_MASK)
  72. | PTPC_PTPC_CTRL0_SUBSEC_DIGITAL_ROLLOVER_SET(rollover);
  73. }
  74. /**
  75. * @brief Enable capture keep
  76. *
  77. * @param[in] ptr PTPC base address
  78. * @param[in] index Target index
  79. */
  80. static inline void ptpc_enable_capture_keep(PTPC_Type *ptr, uint8_t index)
  81. {
  82. ptr->PTPC[index].CTRL0 |= PTPC_PTPC_CTRL0_CAPT_SNAP_KEEP_MASK;
  83. }
  84. /**
  85. * @brief Disable capture keep
  86. *
  87. * @param[in] ptr PTPC base address
  88. * @param[in] index Target index
  89. */
  90. static inline void ptpc_disable_capture_keep(PTPC_Type *ptr, uint8_t index)
  91. {
  92. ptr->PTPC[index].CTRL0 &= ~PTPC_PTPC_CTRL0_CAPT_SNAP_KEEP_MASK;
  93. }
  94. /**
  95. * @brief Update ns counter update type
  96. *
  97. * @param[in] ptr PTPC base address
  98. * @param[in] index Index of target counter
  99. * @param[in] coarse_update Counter will be increment by 1 on
  100. */
  101. static inline void ptpc_set_ns_counter_update_type(PTPC_Type *ptr, uint8_t index, bool coarse_update)
  102. {
  103. ptr->PTPC[index].CTRL0 = (ptr->PTPC[index].CTRL0 & ~PTPC_PTPC_CTRL0_FINE_COARSE_SEL_MASK)
  104. | PTPC_PTPC_CTRL0_FINE_COARSE_SEL_SET(coarse_update);
  105. }
  106. /**
  107. * @brief Set ns counter increment step
  108. *
  109. * @param[in] ptr PTPC base address
  110. * @param[in] index Target index
  111. * @param[in] ns_step Increment step
  112. */
  113. static inline void ptpc_set_ns_counter_step(PTPC_Type *ptr, uint8_t index, uint32_t ns_step)
  114. {
  115. ptr->PTPC[index].CTRL1 = PTPC_PTPC_CTRL1_SS_INCR_SET(ns_step);
  116. }
  117. /**
  118. * @brief Set Second portion update value
  119. *
  120. * @param[in] ptr PTPC base address
  121. * @param[in] index Target index
  122. * @param[in] sec Second value
  123. */
  124. static inline void ptpc_set_second_update(PTPC_Type *ptr, uint8_t index, uint32_t sec)
  125. {
  126. ptr->PTPC[index].TS_UPDTH = PTPC_PTPC_TS_UPDTH_SEC_UPDATE_SET(sec);
  127. }
  128. /**
  129. * @brief Set ns portion update value
  130. *
  131. * @param[in] ptr PTPC base address
  132. * @param[in] index Target index
  133. * @param[in] ns Ns value (31 bits, 0x3B9ACA00 max)
  134. * @param[in] mode Counting mode
  135. *
  136. * @return status_success if everything is okay
  137. */
  138. hpm_stat_t ptpc_set_ns_update(PTPC_Type *ptr, uint8_t index, uint32_t ns, ptpc_counting_mode mode);
  139. /**
  140. * @brief Get current timestamp Second portion
  141. *
  142. * @param[in] ptr PTPC base address
  143. * @param[in] index Target index
  144. *
  145. * @return Seconds value
  146. */
  147. static inline uint32_t ptpc_get_timestamp_second(PTPC_Type *ptr, uint8_t index)
  148. {
  149. return PTPC_PTPC_TIMEH_TIMESTAMP_HIGH_GET(ptr->PTPC[index].TIMEH);
  150. }
  151. /**
  152. * @brief Get timestamp ns portion
  153. *
  154. * @param[in] ptr PTPC base address
  155. * @param[in] index Target index
  156. *
  157. * @return ns value
  158. */
  159. static inline uint32_t ptpc_get_timestamp_ns(PTPC_Type *ptr, uint8_t index)
  160. {
  161. return PTPC_PTPC_TIMEL_TIMESTAMP_LOW_GET(ptr->PTPC[index].TIMEL);
  162. }
  163. /**
  164. * @brief Config compare
  165. *
  166. * @param[in] ptr PTPC base address
  167. * @param[in] index Target index
  168. * @param[in] sec Target second
  169. * @param[in] ns Target ns
  170. */
  171. static inline void ptpc_config_compare(PTPC_Type *ptr, uint8_t index, uint32_t sec, uint32_t ns)
  172. {
  173. ptr->PTPC[index].TARH = PTPC_PTPC_TARH_TARGET_TIME_HIGH_SET(sec);
  174. ptr->PTPC[index].TARL = PTPC_PTPC_TARL_TARGET_TIME_LOW_SET(ns);
  175. ptr->PTPC[index].CTRL0 |= PTPC_PTPC_CTRL0_COMP_EN_MASK;
  176. }
  177. /**
  178. * @brief Update Second portion
  179. *
  180. * @param[in] ptr PTPC base address
  181. * @param[in] index Target index
  182. * @param[in] sec Target second
  183. */
  184. static inline void ptpc_update_timestamp_second(PTPC_Type *ptr, uint8_t index, uint32_t sec)
  185. {
  186. ptr->PTPC[index].TS_UPDTH = PTPC_PTPC_TS_UPDTH_SEC_UPDATE_SET(sec);
  187. }
  188. /**
  189. * @brief Update Ns portion
  190. *
  191. * @param[in] ptr PTPC base address
  192. * @param[in] index Target index
  193. * @param[in] ns Target ns
  194. * @param[in] mode Counting mode
  195. */
  196. static inline void ptpc_update_timestamp_ns(PTPC_Type *ptr, uint8_t index, uint32_t ns, ptpc_counting_mode mode)
  197. {
  198. ptr->PTPC[index].TS_UPDTL = PTPC_PTPC_TS_UPDTL_NS_UPDATE_SET(ns) | PTPC_PTPC_TS_UPDTL_ADD_SUB_SET(mode);
  199. }
  200. /**
  201. * @brief Enable timer
  202. *
  203. * @param[in] ptr PTPC base address
  204. * @param[in] index Target index
  205. */
  206. static inline void ptpc_enable_timer(PTPC_Type *ptr, uint8_t index)
  207. {
  208. ptr->PTPC[index].CTRL0 |= PTPC_PTPC_CTRL0_TIMER_ENABLE_MASK;
  209. }
  210. /**
  211. * @brief Disable timer
  212. *
  213. * @param[in] ptr PTPC base address
  214. * @param[in] index Target index
  215. */
  216. static inline void ptpc_disable_timer(PTPC_Type *ptr, uint8_t index)
  217. {
  218. ptr->PTPC[index].CTRL0 &= ~PTPC_PTPC_CTRL0_TIMER_ENABLE_MASK;
  219. }
  220. /**
  221. * @brief Config capture trigger event
  222. *
  223. * @param[in] ptr PTPC base address
  224. * @param[in] index Target index
  225. * @param[in] trigger Capture trigger type
  226. */
  227. static inline void ptpc_config_capture(PTPC_Type *ptr, uint8_t index,
  228. ptpc_capture_trigger_type_t trigger)
  229. {
  230. ptr->PTPC[index].CTRL0 = (ptr->PTPC[index].CTRL0 & ~(PTPC_PTPC_CTRL0_CAPT_SNAP_POS_EN_MASK
  231. | PTPC_PTPC_CTRL0_CAPT_SNAP_NEG_EN_MASK)) | trigger;
  232. }
  233. /**
  234. * @brief Get snapped ns value
  235. *
  236. * @param[in] ptr PTPC base address
  237. * @param[in] index Target index
  238. *
  239. * @return ns portion on capture
  240. */
  241. static inline uint32_t ptpc_get_capture_ns(PTPC_Type *ptr, uint8_t index)
  242. {
  243. return PTPC_PTPC_CAPT_SNAPL_CAPT_SNAP_LOW_GET(ptr->PTPC[index].CAPT_SNAPL);
  244. }
  245. /**
  246. * @brief Get captured second
  247. *
  248. * @param[in] ptr PTPC base address
  249. * @param[in] index Target index
  250. *
  251. * @return second portion on capture
  252. */
  253. static inline uint32_t ptpc_get_capture_second(PTPC_Type *ptr, uint8_t index)
  254. {
  255. return PTPC_PTPC_CAPT_SNAPH_CAPT_SNAP_HIGH_GET(ptr->PTPC[index].CAPT_SNAPH);
  256. }
  257. /**
  258. * @brief Clear interrupt status
  259. *
  260. * @param[in] ptr PTPC base address
  261. * @param[in] mask Mask of interrupts to be cleared
  262. */
  263. static inline void ptpc_clear_irq_status(PTPC_Type *ptr, uint32_t mask)
  264. {
  265. ptr->INT_STS |= mask;
  266. }
  267. /**
  268. * @brief Get interrupt status
  269. *
  270. * @param[in] ptr PTPC base address
  271. *
  272. * @return interrupt status mask
  273. */
  274. static inline uint32_t ptpc_get_irq_status(PTPC_Type *ptr)
  275. {
  276. return ptr->INT_STS;
  277. }
  278. /**
  279. * @brief Disable interrupt with mask
  280. *
  281. * @param[in] ptr PTPC base address
  282. * @param[in] mask Mask of interrupts to be disabled
  283. */
  284. static inline void ptpc_irq_disable(PTPC_Type *ptr, uint32_t mask)
  285. {
  286. ptr->INT_EN &= ~mask;
  287. }
  288. /**
  289. * @brief Enable interrupt with mask
  290. *
  291. * @param[in] ptr PTPC base address
  292. * @param[in] mask Mask of interrupts to be enabled
  293. */
  294. static inline void ptpc_irq_enable(PTPC_Type *ptr, uint32_t mask)
  295. {
  296. ptr->INT_EN |= mask;
  297. }
  298. /**
  299. * @brief Initialize PTPC module
  300. *
  301. * @param[in] ptr PTPC base address
  302. * @param[in] index Target index
  303. * @param[in] config Pointer to configuration struct
  304. *
  305. * @return status_success if everything is okay
  306. */
  307. hpm_stat_t ptpc_init(PTPC_Type *ptr, uint8_t index, ptpc_config_t *config);
  308. /**
  309. * @brief Set PTPC output destination
  310. *
  311. * @param[in] ptr PTPC base address
  312. * @param[in] can_index Target CAN instance
  313. * @param[in] use_ptpc1 Use PTPC1 if set to true, otherwise PTPC0
  314. *
  315. * @return status_success if everything is okay
  316. */
  317. hpm_stat_t ptpc_set_timer_output(PTPC_Type *ptr, uint8_t can_index, bool use_ptpc1);
  318. /**
  319. * @brief Get default config
  320. *
  321. * @param[in] ptr PTPC base address
  322. * @param[out] config Pointer of config struct
  323. */
  324. void ptpc_get_default_config(PTPC_Type *ptr, ptpc_config_t *config);
  325. /**
  326. * @brief Initialize timer to count from 0
  327. *
  328. * @param[in] ptr PTPC base address
  329. * @param[in] index Target index
  330. *
  331. */
  332. void ptpc_init_timer(PTPC_Type *ptr, uint8_t index);
  333. /**
  334. * @brief Initialize timer with initial values
  335. *
  336. * @param[in] ptr PTPC base address
  337. * @param[in] index Target index
  338. * @param[in] sec Seconds
  339. * @param[in] ns Ns
  340. * @param[in] mode Counting mode
  341. *
  342. * @return status_success if everything is okay
  343. */
  344. hpm_stat_t ptpc_init_timer_with_initial(PTPC_Type *ptr, uint8_t index, uint32_t sec, uint32_t ns, ptpc_counting_mode mode);
  345. /**
  346. * @brief Update timestamp counter
  347. *
  348. * @param[in] ptr PTPC base address
  349. * @param[in] index Target index
  350. * @param[in] sec Seconds
  351. * @param[in] ns Ns
  352. * @param[in] mode Counting mode
  353. * @return status_success if everything is okay
  354. */
  355. hpm_stat_t ptpc_update_timer(PTPC_Type *ptr, uint8_t index, uint32_t sec, uint32_t ns, ptpc_counting_mode mode);
  356. /**
  357. * @brief Set period of pulse generated per second
  358. *
  359. * @param[in] ptr PTPC base address
  360. * @param[in] index Target index
  361. * @param[in] p (2^power Hz, from [0 - 15])
  362. *
  363. * @return true if requested frequency can be set
  364. * @return false if requested frequency can not be set
  365. */
  366. hpm_stat_t ptpc_set_pps(PTPC_Type *ptr, uint8_t index, uint8_t p);
  367. #ifdef __cplusplus
  368. }
  369. #endif
  370. /**
  371. * @}
  372. */
  373. #endif /* HPM_PTPC_DRV_H */