hpm_trgm_drv.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 = 1,
  24. trgm_filter_mode_delay = 2,
  25. trgm_filter_mode_stable_low = 3,
  26. trgm_filter_mode_stable_high = 4,
  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. uint16_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 (0 ~ 0xFFF)
  84. */
  85. static inline void trgm_input_filter_set_filter_length(TRGM_Type *ptr, uint8_t input, uint16_t length)
  86. {
  87. ptr->FILTCFG[input] = (ptr->FILTCFG[input] & TRGM_FILTCFG_FILTLEN_MASK)
  88. | TRGM_FILTCFG_FILTLEN_SET(length);
  89. }
  90. /**
  91. * @brief Enable sync input with TRGM clock
  92. *
  93. * @param[in] ptr TRGM base address
  94. * @param[in] input Input selection
  95. */
  96. static inline void trgm_input_filter_enable_sync(TRGM_Type *ptr, uint8_t input)
  97. {
  98. ptr->FILTCFG[input] |= TRGM_FILTCFG_SYNCEN_MASK;
  99. }
  100. /**
  101. * @brief Disable sync input with TRGM clock
  102. *
  103. * @param[in] ptr TRGM base address
  104. * @param[in] input Input selection
  105. */
  106. static inline void trgm_input_filter_disable_sync(TRGM_Type *ptr, uint8_t input)
  107. {
  108. ptr->FILTCFG[input] &= ~TRGM_FILTCFG_SYNCEN_MASK;
  109. }
  110. /**
  111. * @brief Set filter working mode
  112. *
  113. * @param[in] ptr TRGM base address
  114. * @param[in] input Input selection
  115. * @param[in] mode Working mode
  116. */
  117. static inline void trgm_input_filter_set_mode(TRGM_Type *ptr, uint8_t input, trgm_filter_mode_t mode)
  118. {
  119. ptr->FILTCFG[input] = (ptr->FILTCFG[input] & TRGM_FILTCFG_MODE_MASK)
  120. | TRGM_FILTCFG_MODE_SET(mode);
  121. }
  122. /**
  123. * @brief Invert filter output
  124. *
  125. * @param[in] ptr TRGM base address
  126. * @param[in] input Input selection
  127. * @param[in] invert Set true to invert output
  128. */
  129. static inline void trgm_input_filter_invert(TRGM_Type *ptr, uint8_t input, bool invert)
  130. {
  131. ptr->FILTCFG[input] = (ptr->FILTCFG[input] & TRGM_FILTCFG_OUTINV_MASK)
  132. | TRGM_FILTCFG_OUTINV_SET(invert);
  133. }
  134. /**
  135. * @brief Configure filter
  136. *
  137. * @param[in] ptr TRGM base address
  138. * @param[in] input Input selection
  139. * @param[in] filter Pointer to filter configuration
  140. */
  141. static inline void trgm_input_filter_config(TRGM_Type *ptr, uint8_t input, trgm_input_filter_t *filter)
  142. {
  143. ptr->FILTCFG[input] = TRGM_FILTCFG_OUTINV_SET(filter->invert)
  144. | TRGM_FILTCFG_MODE_SET(filter->mode)
  145. | TRGM_FILTCFG_SYNCEN_SET(filter->sync)
  146. | TRGM_FILTCFG_FILTLEN_SET(filter->filter_length);
  147. }
  148. /**
  149. * @brief Update source for TRGM output
  150. *
  151. * @param[in] ptr TRGM base address
  152. * @param[in] output Target output
  153. * @param[in] source Source for output
  154. */
  155. static inline void trgm_output_update_source(TRGM_Type *ptr, uint8_t output, uint8_t source)
  156. {
  157. ptr->TRGOCFG[output] = (ptr->TRGOCFG[output] & ~TRGM_TRGOCFG_TRIGOSEL_MASK)
  158. | TRGM_TRGOCFG_TRIGOSEL_SET(source);
  159. }
  160. /**
  161. * @brief Configure output
  162. *
  163. * @param[in] ptr TRGM base address
  164. * @param[in] output Target output
  165. * @param[in] config Pointer to output configuration
  166. */
  167. static inline void trgm_output_config(TRGM_Type *ptr, uint8_t output, trgm_output_t *config)
  168. {
  169. ptr->TRGOCFG[output] = TRGM_TRGOCFG_TRIGOSEL_SET(config->input)
  170. | (config->type & TRGM_TRGOCFG_FEDG2PEN_MASK)
  171. | (config->type & TRGM_TRGOCFG_REDG2PEN_MASK)
  172. | TRGM_TRGOCFG_OUTINV_SET(config->invert);
  173. }
  174. /**
  175. * @brief Configure DMA request
  176. *
  177. * @param[in] ptr TRGM base address
  178. * @param[in] dma_out Target DMA out
  179. * @param[in] dma_src DMA source selection
  180. */
  181. static inline void trgm_dma_request_config(TRGM_Type *ptr, uint8_t dma_out, uint8_t dma_src)
  182. {
  183. ptr->DMACFG[dma_out] = TRGM_DMACFG_DMASRCSEL_SET(dma_src);
  184. }
  185. #ifdef __cplusplus
  186. }
  187. #endif
  188. /**
  189. * @}
  190. */
  191. #endif /* HPM_TRGM_DRV_H */