hpm_acmp_drv.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_ACMP_DRV_H
  8. #define HPM_ACMP_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_acmp_regs.h"
  11. /**
  12. * @brief ACMP driver APIs
  13. * @defgroup acmp_interface ACMP driver APIs
  14. * @ingroup io_interfaces
  15. * @{
  16. *
  17. */
  18. /***********************************************************************************************************************
  19. *
  20. * Definitions
  21. *
  22. **********************************************************************************************************************/
  23. /**
  24. * @brief ACMP hysteresis level
  25. */
  26. #define ACMP_HYST_LEVEL_0 (0U)
  27. #define ACMP_HYST_LEVEL_1 (1U)
  28. #define ACMP_HYST_LEVEL_2 (2U)
  29. #define ACMP_HYST_LEVEL_3 (3U)
  30. /**
  31. * @brief ACMP input channel number
  32. */
  33. #define ACMP_INPUT_DAC_OUT (0U)
  34. #define ACMP_INPUT_ANALOG_1 (1U)
  35. #define ACMP_INPUT_ANALOG_2 (2U)
  36. #define ACMP_INPUT_ANALOG_3 (3U)
  37. #define ACMP_INPUT_ANALOG_4 (4U)
  38. #define ACMP_INPUT_ANALOG_5 (5U)
  39. #define ACMP_INPUT_ANALOG_6 (6U)
  40. #define ACMP_INPUT_ANALOG_7 (7U)
  41. /**
  42. * @brief ACMP output digital filter mode
  43. */
  44. #define ACMP_FILTER_MODE_BYPASS (0U)
  45. #define ACMP_FILTER_MODE_CHANGE_IMMEDIATELY (4U)
  46. #define ACMP_FILTER_MODE_CHANGE_AFTER_FILTER (5U)
  47. #define ACMP_FILTER_MODE_STABLE_LOW (6U)
  48. #define ACMP_FILTER_MODE_STABLE_HIGH (7U)
  49. /**
  50. * @brief ACMP rising/falling flage mask
  51. */
  52. #define ACMP_EVENT_RISING_EDGE (1U)
  53. #define ACMP_EVENT_FALLING_EDGE (2U)
  54. /**
  55. * @brief ACMP channel config
  56. */
  57. typedef struct acmp_channel_config {
  58. uint8_t plus_input;
  59. uint8_t minus_input;
  60. uint8_t filter_mode;
  61. uint8_t hyst_level;
  62. bool enable_cmp_output;
  63. bool enable_window_mode;
  64. bool invert_output;
  65. bool enable_clock_sync;
  66. bool bypass_filter;
  67. bool enable_dac;
  68. bool enable_hpmode;
  69. uint16_t filter_length; /* ACMP output digital filter length in ACMP clock cycle */
  70. } acmp_channel_config_t;
  71. #ifdef __cplusplus
  72. extern "C" {
  73. #endif
  74. /**
  75. * @brief ACMP channel config DAC output value
  76. *
  77. * @param [in] ptr ACMP base address
  78. * @param [in] ch ACMP channel number
  79. * @param [in] value DAC output value
  80. */
  81. static inline void acmp_channel_config_dac(ACMP_Type *ptr, uint8_t ch, uint32_t value)
  82. {
  83. ptr->CHANNEL[ch].DACCFG = ACMP_CHANNEL_DACCFG_DACCFG_SET(value);
  84. }
  85. /**
  86. * @brief ACMP channel clear status
  87. *
  88. * @param [in] ptr ACMP base address
  89. * @param [in] ch ACMP channel number
  90. * @param [in] mask :
  91. * @arg ACMP_EVENT_RISING_EDGE: ACMP output rising flag
  92. * @arg ACMP_EVENT_FALLING_EDGE: ACMP output fall flag
  93. */
  94. static inline void acmp_channel_clear_status(ACMP_Type *ptr, uint8_t ch, uint32_t mask)
  95. {
  96. ptr->CHANNEL[ch].SR = mask;
  97. }
  98. /**
  99. * @brief ACMP channel get status
  100. *
  101. * @param [in] ptr ACMP base address
  102. * @param [in] ch ACMP channel number
  103. * @retval ACMP channel's status
  104. */
  105. static inline uint32_t acmp_channel_get_status(ACMP_Type *ptr, uint8_t ch)
  106. {
  107. return ptr->CHANNEL[ch].SR;
  108. }
  109. /**
  110. * @brief ACMP channel enable DMA request
  111. *
  112. * @param [in] ptr ACMP base address
  113. * @param [in] ch ACMP channel number
  114. * @param [in] mask :
  115. * @arg ACMP_EVENT_RISING_EDGE: ACMP output rising flag
  116. * @arg ACMP_EVENT_FALLING_EDGE: ACMP output fall flag
  117. * @param [in] enable:
  118. * @arg true: enable
  119. * @arg false: disable
  120. */
  121. static inline void acmp_channel_dma_request_enable(ACMP_Type *ptr, uint8_t ch,
  122. uint32_t mask, bool enable)
  123. {
  124. ptr->CHANNEL[ch].DMAEN = (ptr->CHANNEL[ch].DMAEN & ~mask)
  125. | (enable ? mask : 0);
  126. }
  127. /**
  128. * @brief ACMP channel enable IRQ
  129. *
  130. * @param [in] ptr ACMP base address
  131. * @param [in] ch ACMP channel number
  132. * @param [in] mask :
  133. * @arg ACMP_EVENT_RISING_EDGE: ACMP output rising flag
  134. * @arg ACMP_EVENT_FALLING_EDGE: ACMP output fall flag
  135. * @param [in] enable:
  136. * @arg true: enable
  137. * @arg false: disable
  138. */
  139. static inline void acmp_channel_enable_irq(ACMP_Type *ptr, uint8_t ch,
  140. uint32_t mask, bool enable)
  141. {
  142. ptr->CHANNEL[ch].IRQEN = (ptr->CHANNEL[ch].IRQEN & ~mask)
  143. | (enable ? mask : 0);
  144. }
  145. /**
  146. * @brief ACMP channel enable DAC
  147. *
  148. * @param [in] ptr ACMP base address
  149. * @param [in] ch ACMP channel number
  150. * @param [in] enable:
  151. * @arg true: enable
  152. * @arg false: disable
  153. */
  154. static inline void acmp_channel_enable_dac(ACMP_Type *ptr, uint8_t ch, bool enable)
  155. {
  156. ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_DACEN_MASK)
  157. | ACMP_CHANNEL_CFG_DACEN_SET(enable);
  158. }
  159. /**
  160. * @brief ACMP channel enable high performance mode
  161. *
  162. * @param [in] ptr ACMP base address
  163. * @param [in] ch ACMP channel number
  164. * @param [in] enable:
  165. * @arg true: enable
  166. * @arg false: disable
  167. */
  168. static inline void acmp_channel_enable_hpmode(ACMP_Type *ptr, uint8_t ch, bool enable)
  169. {
  170. ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_HPMODE_MASK)
  171. | ACMP_CHANNEL_CFG_HPMODE_SET(enable);
  172. }
  173. /**
  174. * @brief ACMP channel enable hysteresis level
  175. *
  176. * @param [in] ptr ACMP base address
  177. * @param [in] ch ACMP channel number
  178. * @param [in] level: ACMP hysteresis level
  179. */
  180. static inline void acmp_channel_set_hyst(ACMP_Type *ptr, uint8_t ch, uint8_t level)
  181. {
  182. ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_HYST_MASK)
  183. | ACMP_CHANNEL_CFG_HYST_SET(level);
  184. }
  185. /**
  186. * @brief ACMP channel enable comparator
  187. *
  188. * @param [in] ptr ACMP base address
  189. * @param [in] ch ACMP channel number
  190. * @param [in] enable:
  191. * @arg true: enable
  192. * @arg false: disable
  193. */
  194. static inline void acmp_channel_enable_cmp(ACMP_Type *ptr, uint8_t ch, bool enable)
  195. {
  196. ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_CMPEN_MASK)
  197. | ACMP_CHANNEL_CFG_CMPEN_SET(enable);
  198. }
  199. /**
  200. * @brief ACMP channel enable comparator output
  201. *
  202. * @param [in] ptr ACMP base address
  203. * @param [in] ch ACMP channel number
  204. * @param [in] enable:
  205. * @arg true: enable
  206. * @arg false: disable
  207. */
  208. static inline void acmp_channel_enable_cmp_output(ACMP_Type *ptr, uint8_t ch, bool enable)
  209. {
  210. ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_CMPOEN_MASK)
  211. | ACMP_CHANNEL_CFG_CMPOEN_SET(enable);
  212. }
  213. /**
  214. * @brief ACMP channel bypass comparator output filter
  215. *
  216. * @param [in] ptr ACMP base address
  217. * @param [in] ch ACMP channel number
  218. * @param [in] enable:
  219. * @arg true: bypass
  220. * @arg false: not bypass
  221. */
  222. static inline void acmp_channel_cmp_output_bypass_filter(ACMP_Type *ptr, uint8_t ch, bool enable)
  223. {
  224. ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_FLTBYPS_MASK)
  225. | ACMP_CHANNEL_CFG_FLTBYPS_SET(!enable);
  226. }
  227. /**
  228. * @brief ACMP channel enable comparator window mode
  229. *
  230. * @param [in] ptr ACMP base address
  231. * @param [in] ch ACMP channel number
  232. * @param [in] enable:
  233. * @arg true: enable
  234. * @arg false: disable
  235. */
  236. static inline void acmp_channel_enable_cmp_window_mode(ACMP_Type *ptr, uint8_t ch, bool enable)
  237. {
  238. ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_WINEN_MASK)
  239. | ACMP_CHANNEL_CFG_WINEN_SET(enable);
  240. }
  241. /**
  242. * @brief ACMP channel invert comparator output
  243. *
  244. * @param [in] ptr ACMP base address
  245. * @param [in] ch ACMP channel number
  246. * @param [in] enable:
  247. * @arg true: invert
  248. * @arg false: not invert
  249. */
  250. static inline void acmp_channel_invert_output(ACMP_Type *ptr, uint8_t ch, bool enable)
  251. {
  252. ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_OPOL_MASK)
  253. | ACMP_CHANNEL_CFG_OPOL_SET(enable);
  254. }
  255. /**
  256. * @brief ACMP channel set comparator output filter mode
  257. *
  258. * @param [in] ptr ACMP base address
  259. * @param [in] ch ACMP channel number
  260. * @param [in] filter: ACMP output digital filter mode definition
  261. */
  262. static inline void acmp_channel_set_filter_mode(ACMP_Type *ptr, uint8_t ch, uint8_t filter)
  263. {
  264. ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_FLTMODE_MASK)
  265. | ACMP_CHANNEL_CFG_FLTMODE_SET(filter);
  266. }
  267. /**
  268. * @brief ACMP channel enable comparator output sync with clock
  269. *
  270. * @param [in] ptr ACMP base address
  271. * @param [in] ch ACMP channel number
  272. * @param [in] enable:
  273. * @arg true: enable
  274. * @arg false: disable
  275. */
  276. static inline void acmp_channel_enable_sync(ACMP_Type *ptr, uint8_t ch, bool enable)
  277. {
  278. ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_SYNCEN_MASK)
  279. | ACMP_CHANNEL_CFG_SYNCEN_SET(enable);
  280. }
  281. /**
  282. * @brief ACMP channel set comparator output filter length
  283. *
  284. * @param [in] ptr ACMP base address
  285. * @param [in] ch ACMP channel number
  286. * @param [in] filter_length: filter length in clock cycles
  287. */
  288. static inline void acmp_channel_set_filter_length(ACMP_Type *ptr, uint8_t ch, uint16_t filter_length)
  289. {
  290. ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_FLTLEN_MASK)
  291. | ACMP_CHANNEL_CFG_FLTLEN_SET(filter_length);
  292. }
  293. /**
  294. * @brief ADC channel config
  295. *
  296. * @param [in] ptr ACMP base address
  297. * @param [in] ch ACMP channel number
  298. * @param [in] config: acmp_channel_config_t
  299. * @param [in] enable:
  300. * @arg true: enable comparator
  301. * @arg false: disable comparator
  302. *
  303. * @retval hpm_stat_t
  304. */
  305. hpm_stat_t acmp_channel_config(ACMP_Type *ptr, uint8_t ch, acmp_channel_config_t *config, bool enable);
  306. /**
  307. * @brief ADC channel get default config setting
  308. *
  309. * @param [in] ptr ACMP base address
  310. * @param [out] config: acmp_channel_config_t
  311. */
  312. void acmp_channel_get_default_config(ACMP_Type *ptr, acmp_channel_config_t *config);
  313. /**
  314. * @}
  315. *
  316. */
  317. #ifdef __cplusplus
  318. }
  319. #endif
  320. #endif /* HPM_ACMP_DRV_H */