hpm_gptmr_drv.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright (c) 2021 hpmicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_GPTMR_DRV_H
  8. #define HPM_GPTMR_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_gptmr_regs.h"
  11. /**
  12. * @brief GPTMR driver APIs
  13. * @defgroup gptmr_interface GPTMR driver APIs
  14. * @ingroup io_interfaces
  15. * @{
  16. */
  17. /**
  18. * @brief GPTMR channel IRQ mask
  19. */
  20. #define GPTMR_CH_CMP_IRQ_MASK(ch, cmp) (1 << (ch * 4 + 2 + cmp))
  21. #define GPTMR_CH_CAP_IRQ_MASK(ch) (1 << (ch * 4 + 1))
  22. #define GPTMR_CH_RLD_IRQ_MASK(ch) (1 << (ch * 4))
  23. /**
  24. * @brief GPTMR channel status
  25. */
  26. #define GPTMR_CH_CMP_STAT_MASK(ch, cmp) (1 << (ch * 4 + 2 + cmp))
  27. #define GPTMR_CH_CAP_STAT_MASK(ch) (1 << (ch * 4 + 1))
  28. #define GPTMR_CH_RLD_STAT_MASK(ch) (1 << (ch * 4))
  29. /**
  30. * @brief GPTMR one channel support output comparator count
  31. */
  32. #define GPTMR_CH_CMP_COUNT (2U)
  33. /**
  34. * @brief GPTMR synci valid edge
  35. */
  36. typedef enum gptmr_synci_edge {
  37. gptmr_synci_edge_none = 0,
  38. gptmr_synci_edge_falling = GPTMR_CHANNEL_CR_SYNCIFEN_MASK,
  39. gptmr_synci_edge_rising = GPTMR_CHANNEL_CR_SYNCIREN_MASK,
  40. gptmr_synci_edge_both = gptmr_synci_edge_falling | gptmr_synci_edge_rising,
  41. } gptmr_synci_edge_t;
  42. /**
  43. * @brief GPTMR work mode
  44. */
  45. typedef enum gptmr_work_mode {
  46. gptmr_work_mode_no_capture = 0,
  47. gptmr_work_mode_capture_at_rising_edge = 1,
  48. gptmr_work_mode_capture_at_falling_edge = 2,
  49. gptmr_work_mode_capture_at_both_edge = 3,
  50. gptmr_work_mode_measure_width = 4,
  51. } gptmr_work_mode_t;
  52. /**
  53. * @brief GPTMR DMA request event
  54. */
  55. typedef enum gptmr_dma_request_event {
  56. gptmr_dma_request_on_reload = 0,
  57. gptmr_dma_request_on_input_signal_toggle = 1,
  58. gptmr_dma_request_on_cmp0 = 2,
  59. gptmr_dma_request_on_cmp1 = 3,
  60. gptmr_dma_request_disabled = 0xFF,
  61. } gptmr_dma_request_event_t;
  62. /**
  63. * @brief GPTMR counter type
  64. */
  65. typedef enum gptmr_counter_type {
  66. gptmr_counter_type_rising_edge,
  67. gptmr_counter_type_falling_edge,
  68. gptmr_counter_type_measured_period,
  69. gptmr_counter_type_measured_duty_cycle,
  70. gptmr_counter_type_normal,
  71. } gptmr_counter_type_t;
  72. /**
  73. * @brief GPTMR channel config
  74. */
  75. typedef struct gptmr_channel_config {
  76. gptmr_work_mode_t mode;
  77. gptmr_dma_request_event_t dma_request_event;
  78. gptmr_synci_edge_t synci_edge;
  79. uint32_t cmp[GPTMR_CH_CMP_COUNT];
  80. uint32_t reload;
  81. bool cmp_initial_polarity_high;
  82. bool enable_cmp_output;
  83. bool enable_sync_follow_previous_channel;
  84. bool enable_software_sync;
  85. bool debug_mode;
  86. } gptmr_channel_config_t;
  87. #ifdef __cplusplus
  88. extern "C" {
  89. #endif
  90. /**
  91. * @brief gptmr channel enable
  92. *
  93. * @param [in] ptr GPTMR base address
  94. * @param [in] ch_index channel index
  95. * @param [in] enable
  96. * @arg true: enable
  97. * @arg false: disable
  98. */
  99. static inline void gptmr_channel_enable(GPTMR_Type *ptr, uint8_t ch_index, bool enable)
  100. {
  101. ptr->CHANNEL[ch_index].CR = (ptr->CHANNEL[ch_index].CR
  102. & ~(GPTMR_CHANNEL_CR_CNTRST_MASK | GPTMR_CHANNEL_CR_CMPEN_MASK))
  103. | GPTMR_CHANNEL_CR_CMPEN_SET(enable);
  104. }
  105. /**
  106. * @brief gptmr channel reset counter
  107. *
  108. * @param [in] ptr GPTMR base address
  109. * @param [in] ch_index channel index
  110. */
  111. static inline void gptmr_channel_reset_count(GPTMR_Type *ptr, uint8_t ch_index)
  112. {
  113. ptr->CHANNEL[ch_index].CR |= GPTMR_CHANNEL_CR_CNTRST_MASK;
  114. ptr->CHANNEL[ch_index].CR &= ~GPTMR_CHANNEL_CR_CNTRST_MASK;
  115. }
  116. /**
  117. * @brief gptmr channel update counter
  118. *
  119. * @param [in] ptr GPTMR base address
  120. * @param [in] ch_index channel index
  121. * @param [in] value updated vaue
  122. */
  123. static inline void gptmr_channel_update_count(GPTMR_Type *ptr,
  124. uint8_t ch_index,
  125. uint32_t value)
  126. {
  127. ptr->CHANNEL[ch_index].CNTUPTVAL = GPTMR_CHANNEL_CNTUPTVAL_CNTUPTVAL_SET(value);
  128. ptr->CHANNEL[ch_index].CR |= GPTMR_CHANNEL_CR_CNTUPT_MASK;
  129. }
  130. /**
  131. * @brief gptmr channel slect synci valid edge
  132. *
  133. * @param [in] ptr GPTMR base address
  134. * @param [in] ch_index channel index
  135. * @param [in] edge gptmr_synci_edge_t
  136. */
  137. static inline void gptmr_channel_select_synci_valid_edge(GPTMR_Type *ptr,
  138. uint8_t ch_index,
  139. gptmr_synci_edge_t edge)
  140. {
  141. ptr->CHANNEL[ch_index].CR = (ptr->CHANNEL[ch_index].CR
  142. & ~(GPTMR_CHANNEL_CR_SYNCIFEN_MASK
  143. | GPTMR_CHANNEL_CR_SYNCIREN_MASK)) | edge;
  144. }
  145. /**
  146. * @brief gptmr channel enable dma request
  147. *
  148. * @param [in] ptr GPTMR base address
  149. * @param [in] ch_index channel index
  150. * @param [in] enable
  151. * @arg true: enable
  152. * @arg false: disable
  153. */
  154. static inline void gptmr_channel_enable_dma_request(GPTMR_Type *ptr,
  155. uint8_t ch_index,
  156. bool enable)
  157. {
  158. ptr->CHANNEL[ch_index].CR = (ptr->CHANNEL[ch_index].CR
  159. & ~(GPTMR_CHANNEL_CR_DMAEN_MASK)) | GPTMR_CHANNEL_CR_DMAEN_SET(enable);
  160. }
  161. /**
  162. * @brief gptmr channel get counter value
  163. *
  164. * @param [in] ptr GPTMR base address
  165. * @param [in] ch_index channel index
  166. * @param [in] capture gptmr_counter_type_t
  167. */
  168. static inline uint32_t gptmr_channel_get_counter(GPTMR_Type *ptr,
  169. uint8_t ch_index,
  170. gptmr_counter_type_t capture)
  171. {
  172. uint32_t value;
  173. switch (capture) {
  174. case gptmr_counter_type_rising_edge:
  175. value = (ptr->CHANNEL[ch_index].CAPPOS & GPTMR_CHANNEL_CAPPOS_CAPPOS_MASK) >> GPTMR_CHANNEL_CAPPOS_CAPPOS_SHIFT;
  176. break;
  177. case gptmr_counter_type_falling_edge:
  178. value = (ptr->CHANNEL[ch_index].CAPNEG & GPTMR_CHANNEL_CAPNEG_CAPNEG_MASK) >> GPTMR_CHANNEL_CAPNEG_CAPNEG_SHIFT;
  179. break;
  180. case gptmr_counter_type_measured_period:
  181. value = (ptr->CHANNEL[ch_index].CAPPRD & GPTMR_CHANNEL_CAPPRD_CAPPRD_MASK) >> GPTMR_CHANNEL_CAPPRD_CAPPRD_SHIFT;
  182. break;
  183. case gptmr_counter_type_measured_duty_cycle:
  184. value = (ptr->CHANNEL[ch_index].CAPDTY & GPTMR_CHANNEL_CAPDTY_MEAS_HIGH_MASK) >> GPTMR_CHANNEL_CAPDTY_MEAS_HIGH_SHIFT;
  185. break;
  186. default:
  187. value = (ptr->CHANNEL[ch_index].CNT & GPTMR_CHANNEL_CNT_COUNTER_MASK) >> GPTMR_CHANNEL_CNT_COUNTER_SHIFT;
  188. break;
  189. }
  190. return value;
  191. }
  192. /**
  193. * @brief gptmr trigger channel software sync
  194. *
  195. * @param [in] ptr GPTMR base address
  196. * @param [in] ch_index_mask channel index mask
  197. */
  198. static inline void gptmr_trigger_channel_software_sync(GPTMR_Type *ptr, uint32_t ch_index_mask)
  199. {
  200. ptr->GCR = ch_index_mask;
  201. }
  202. /**
  203. * @brief gptmr enable irq
  204. *
  205. * @param [in] ptr GPTMR base address
  206. * @param [in] irq_mask irq mask
  207. */
  208. static inline void gptmr_enable_irq(GPTMR_Type *ptr, uint32_t irq_mask)
  209. {
  210. ptr->IRQEN |= irq_mask;
  211. }
  212. /**
  213. * @brief gptmr disable irq
  214. *
  215. * @param [in] ptr GPTMR base address
  216. * @param [in] irq_mask irq mask
  217. */
  218. static inline void gptmr_disable_irq(GPTMR_Type *ptr, uint32_t irq_mask)
  219. {
  220. ptr->IRQEN &= ~irq_mask;
  221. }
  222. /**
  223. * @brief gptmr check status
  224. *
  225. * @param [in] ptr GPTMR base address
  226. * @param [in] mask channel flag mask
  227. */
  228. static inline bool gptmr_check_status(GPTMR_Type *ptr, uint32_t mask)
  229. {
  230. return (ptr->SR & mask) == mask;
  231. }
  232. /**
  233. * @brief gptmr clear status
  234. *
  235. * @param [in] ptr GPTMR base address
  236. * @param [in] mask channel flag mask
  237. */
  238. static inline void gptmr_clear_status(GPTMR_Type *ptr, uint32_t mask)
  239. {
  240. ptr->SR |= mask;
  241. }
  242. /**
  243. * @brief gptmr get status
  244. *
  245. * @param [in] ptr GPTMR base address
  246. * @retval SR register value
  247. */
  248. static inline uint32_t gptmr_get_status(GPTMR_Type *ptr)
  249. {
  250. return ptr->SR;
  251. }
  252. /**
  253. * @brief gptmr channel start counter
  254. *
  255. * @param [in] ptr GPTMR base address
  256. * @param [in] ch_index channel index
  257. */
  258. static inline void gptmr_start_counter(GPTMR_Type *ptr, uint8_t ch_index)
  259. {
  260. ptr->CHANNEL[ch_index].CR |= GPTMR_CHANNEL_CR_CEN_MASK;
  261. }
  262. /**
  263. * @brief gptmr channel stop counter
  264. *
  265. * @param [in] ptr GPTMR base address
  266. * @param [in] ch_index channel index
  267. */
  268. static inline void gptmr_stop_counter(GPTMR_Type *ptr, uint8_t ch_index)
  269. {
  270. ptr->CHANNEL[ch_index].CR &= ~GPTMR_CHANNEL_CR_CEN_MASK;
  271. }
  272. /**
  273. * @brief gptmr channel update comparator
  274. *
  275. * @param [in] ptr GPTMR base address
  276. * @param [in] ch_index channel index
  277. * @param [in] cmp_index comparator index
  278. * @param [in] cmp comparator value
  279. */
  280. static inline void gptmr_update_cmp(GPTMR_Type *ptr, uint8_t ch_index, uint8_t cmp_index, uint32_t cmp)
  281. {
  282. ptr->CHANNEL[ch_index].CMP[cmp_index] = GPTMR_CMP_CMP_SET(cmp);
  283. }
  284. /**
  285. * @brief gptmr channel update reload
  286. *
  287. * @param [in] ptr GPTMR base address
  288. * @param [in] ch_index channel index
  289. * @param [in] reload reload value
  290. */
  291. static inline void gptmr_channel_config_update_reload(GPTMR_Type *ptr, uint8_t ch_index, uint32_t reload)
  292. {
  293. ptr->CHANNEL[ch_index].RLD = GPTMR_CHANNEL_RLD_RLD_SET(reload);
  294. }
  295. /**
  296. * @brief gptmr channel config
  297. *
  298. * @param [in] ptr GPTMR base address
  299. * @param [in] ch_index channel index
  300. * @param [in] config gptmr_channel_config_t
  301. * @param [in] enable
  302. * @arg true: enable
  303. * @arg false: disable
  304. *
  305. * @retval hpm_stat_t status_invalid_argument or status_success
  306. */
  307. hpm_stat_t gptmr_channel_config(GPTMR_Type *ptr,
  308. uint8_t ch_index,
  309. gptmr_channel_config_t *config,
  310. bool enable);
  311. /**
  312. * @brief gptmr channel get default config
  313. *
  314. * @param [in] ptr GPTMR base address
  315. * @param [out] config gptmr_channel_config_t
  316. */
  317. void gptmr_channel_get_default_config(GPTMR_Type *ptr, gptmr_channel_config_t *config);
  318. /**
  319. * @}
  320. */
  321. #ifdef __cplusplus
  322. }
  323. #endif
  324. #endif /* HPM_GPTMR_DRV_H */