hpm_trgm_drv.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_TRGM_DRV_H
  8. #define HPM_TRGM_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_trgm_regs.h"
  11. #include "hpm_trgmmux_src.h"
  12. /**
  13. *
  14. * @brief TRGM driver APIs
  15. * @defgroup trgm_interface TRGM driver APIs
  16. * @{
  17. */
  18. /**
  19. * @brief Filter mode
  20. */
  21. typedef enum trgm_filter_mode {
  22. trgm_filter_mode_bypass = 0,
  23. trgm_filter_mode_rapid_change = 4,
  24. trgm_filter_mode_delay = 5,
  25. trgm_filter_mode_stable_high = 6,
  26. trgm_filter_mode_stable_low = 7,
  27. } trgm_filter_mode_t;
  28. /**
  29. * @brief Output type
  30. */
  31. typedef enum trgm_output_type {
  32. trgm_output_same_as_input = 0,
  33. trgm_output_pulse_at_input_falling_edge = TRGM_TRGOCFG_FEDG2PEN_MASK,
  34. trgm_output_pulse_at_input_rising_edge = TRGM_TRGOCFG_REDG2PEN_MASK,
  35. trgm_output_pulse_at_input_both_edge = trgm_output_pulse_at_input_falling_edge
  36. | trgm_output_pulse_at_input_rising_edge,
  37. } trgm_output_type_t;
  38. /**
  39. * @brief Input filter configuration
  40. */
  41. typedef struct trgm_input_filter {
  42. bool invert; /**< Invert output */
  43. bool sync; /**< Sync with TRGM clock */
  44. uint32_t filter_length; /**< Filter length in TRGM clock cycle */
  45. trgm_filter_mode_t mode; /**< Filter working mode */
  46. } trgm_input_filter_t;
  47. /**
  48. * @brief Output configuration
  49. */
  50. typedef struct trgm_output {
  51. bool invert; /**< Invert output */
  52. trgm_output_type_t type; /**< Output type */
  53. uint8_t input; /**< Input selection */
  54. } trgm_output_t;
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. /**
  59. * @brief Enable IO output
  60. *
  61. * @param[in] ptr TRGM base address
  62. * @param[in] mask Mask of IOs to be enabled
  63. */
  64. static inline void trgm_enable_io_output(TRGM_Type *ptr, uint32_t mask)
  65. {
  66. ptr->GCR |= mask;
  67. }
  68. /**
  69. * @brief Disable IO output
  70. *
  71. * @param[in] ptr TRGM base address
  72. * @param[in] mask Mask of IOs to be disabled
  73. */
  74. static inline void trgm_disable_io_output(TRGM_Type *ptr, uint32_t mask)
  75. {
  76. ptr->GCR &= ~mask;
  77. }
  78. /**
  79. * @brief Set filter length
  80. *
  81. * @param[in] ptr TRGM base address
  82. * @param[in] input Input selection
  83. * @param[in] length Filter length in TRGM clock cycles
  84. */
  85. static inline void trgm_input_filter_set_filter_length(TRGM_Type *ptr, uint8_t input, uint32_t length)
  86. {
  87. #if defined(TRGM_SOC_HAS_FILTER_SHIFT) && TRGM_SOC_HAS_FILTER_SHIFT
  88. uint32_t len = length;
  89. uint8_t shift;
  90. for (shift = 0; shift <= (TRGM_FILTCFG_FILTLEN_SHIFT_MASK >> TRGM_FILTCFG_FILTLEN_SHIFT_SHIFT); shift++) {
  91. if (shift > 0) {
  92. len >>= 1u;
  93. }
  94. if (len <= (TRGM_FILTCFG_FILTLEN_BASE_MASK >> TRGM_FILTCFG_FILTLEN_BASE_SHIFT)) {
  95. break;
  96. }
  97. }
  98. if (len > (TRGM_FILTCFG_FILTLEN_BASE_MASK >> TRGM_FILTCFG_FILTLEN_BASE_SHIFT)) {
  99. len = (TRGM_FILTCFG_FILTLEN_BASE_MASK >> TRGM_FILTCFG_FILTLEN_BASE_SHIFT);
  100. shift = (TRGM_FILTCFG_FILTLEN_SHIFT_MASK >> TRGM_FILTCFG_FILTLEN_SHIFT_SHIFT);
  101. }
  102. ptr->FILTCFG[input] = (ptr->FILTCFG[input] & ~(TRGM_FILTCFG_FILTLEN_BASE_MASK | TRGM_FILTCFG_FILTLEN_SHIFT_MASK))
  103. | TRGM_FILTCFG_FILTLEN_BASE_SET(len) | TRGM_FILTCFG_FILTLEN_SHIFT_SET(shift);
  104. #else
  105. ptr->FILTCFG[input] = (ptr->FILTCFG[input] & ~TRGM_FILTCFG_FILTLEN_MASK)
  106. | TRGM_FILTCFG_FILTLEN_SET(length);
  107. #endif
  108. }
  109. /**
  110. * @brief Set filter length
  111. *
  112. * @param[in] ptr TRGM base address
  113. * @param[in] input Input selection
  114. * @param[in] shift Filter length shift
  115. */
  116. static inline void trgm_input_filter_set_filter_shift(TRGM_Type *ptr, uint8_t input, uint8_t shift)
  117. {
  118. #if defined(TRGM_SOC_HAS_FILTER_SHIFT) && TRGM_SOC_HAS_FILTER_SHIFT
  119. ptr->FILTCFG[input] = (ptr->FILTCFG[input] & ~TRGM_FILTCFG_FILTLEN_SHIFT_MASK)
  120. | TRGM_FILTCFG_FILTLEN_SHIFT_SET(shift);
  121. #else
  122. (void) ptr;
  123. (void) input;
  124. (void) shift;
  125. #endif
  126. }
  127. /**
  128. * @brief Enable sync input with TRGM clock
  129. *
  130. * @param[in] ptr TRGM base address
  131. * @param[in] input Input selection
  132. */
  133. static inline void trgm_input_filter_enable_sync(TRGM_Type *ptr, uint8_t input)
  134. {
  135. ptr->FILTCFG[input] |= TRGM_FILTCFG_SYNCEN_MASK;
  136. }
  137. /**
  138. * @brief Disable sync input with TRGM clock
  139. *
  140. * @param[in] ptr TRGM base address
  141. * @param[in] input Input selection
  142. */
  143. static inline void trgm_input_filter_disable_sync(TRGM_Type *ptr, uint8_t input)
  144. {
  145. ptr->FILTCFG[input] &= ~TRGM_FILTCFG_SYNCEN_MASK;
  146. }
  147. /**
  148. * @brief Set filter working mode
  149. *
  150. * @param[in] ptr TRGM base address
  151. * @param[in] input Input selection
  152. * @param[in] mode Working mode
  153. */
  154. static inline void trgm_input_filter_set_mode(TRGM_Type *ptr, uint8_t input, trgm_filter_mode_t mode)
  155. {
  156. ptr->FILTCFG[input] = (ptr->FILTCFG[input] & ~TRGM_FILTCFG_MODE_MASK)
  157. | TRGM_FILTCFG_MODE_SET(mode);
  158. }
  159. /**
  160. * @brief Invert filter output
  161. *
  162. * @param[in] ptr TRGM base address
  163. * @param[in] input Input selection
  164. * @param[in] invert Set true to invert output
  165. */
  166. static inline void trgm_input_filter_invert(TRGM_Type *ptr, uint8_t input, bool invert)
  167. {
  168. ptr->FILTCFG[input] = (ptr->FILTCFG[input] & ~TRGM_FILTCFG_OUTINV_MASK)
  169. | TRGM_FILTCFG_OUTINV_SET(invert);
  170. }
  171. /**
  172. * @brief Configure filter
  173. *
  174. * @param[in] ptr TRGM base address
  175. * @param[in] input Input selection
  176. * @param[in] filter Pointer to filter configuration
  177. */
  178. static inline void trgm_input_filter_config(TRGM_Type *ptr, uint8_t input, trgm_input_filter_t *filter)
  179. {
  180. ptr->FILTCFG[input] = TRGM_FILTCFG_OUTINV_SET(filter->invert)
  181. | TRGM_FILTCFG_MODE_SET(filter->mode)
  182. | TRGM_FILTCFG_SYNCEN_SET(filter->sync);
  183. trgm_input_filter_set_filter_length(ptr, input, filter->filter_length);
  184. }
  185. /**
  186. * @brief Update source for TRGM output
  187. *
  188. * @param[in] ptr TRGM base address
  189. * @param[in] output Target output
  190. * @param[in] source Source for output
  191. */
  192. static inline void trgm_output_update_source(TRGM_Type *ptr, uint8_t output, uint8_t source)
  193. {
  194. ptr->TRGOCFG[output] = (ptr->TRGOCFG[output] & ~TRGM_TRGOCFG_TRIGOSEL_MASK)
  195. | TRGM_TRGOCFG_TRIGOSEL_SET(source);
  196. }
  197. /**
  198. * @brief Configure output
  199. *
  200. * @param[in] ptr TRGM base address
  201. * @param[in] output Target output
  202. * @param[in] config Pointer to output configuration
  203. */
  204. static inline void trgm_output_config(TRGM_Type *ptr, uint8_t output, trgm_output_t *config)
  205. {
  206. ptr->TRGOCFG[output] = TRGM_TRGOCFG_TRIGOSEL_SET(config->input)
  207. | (config->type & TRGM_TRGOCFG_FEDG2PEN_MASK)
  208. | (config->type & TRGM_TRGOCFG_REDG2PEN_MASK)
  209. | TRGM_TRGOCFG_OUTINV_SET(config->invert);
  210. }
  211. /**
  212. * @brief Configure DMA request
  213. *
  214. * @param[in] ptr TRGM base address
  215. * @param[in] dma_out Target DMA out
  216. * @param[in] dma_src DMA source selection
  217. */
  218. static inline void trgm_dma_request_config(TRGM_Type *ptr, uint8_t dma_out, uint8_t dma_src)
  219. {
  220. #if defined(TRGM_SOC_HAS_DMAMUX_EN) && TRGM_SOC_HAS_DMAMUX_EN
  221. ptr->DMACFG[dma_out] = TRGM_DMACFG_DMASRCSEL_SET(dma_src) | TRGM_DMACFG_DMAMUX_EN_MASK;
  222. #else
  223. ptr->DMACFG[dma_out] = TRGM_DMACFG_DMASRCSEL_SET(dma_src);
  224. #endif
  225. }
  226. #ifdef __cplusplus
  227. }
  228. #endif
  229. /**
  230. * @}
  231. */
  232. #endif /* HPM_TRGM_DRV_H */