1
0

hpm_adc12_drv.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_ADC12_DRV_H
  8. #define HPM_ADC12_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_adc12_regs.h"
  11. #include "hpm_soc_feature.h"
  12. /**
  13. * @brief ADC12 driver APIs
  14. * @defgroup adc12_interface ADC12 driver APIs
  15. * @ingroup adc_interfaces
  16. * @{
  17. */
  18. /** @brief Define ADC12 validity check for the signal type */
  19. #define ADC12_IS_SIGNAL_TYPE_INVALID(TYPE) (TYPE > (uint32_t)adc12_sample_signal_count)
  20. /** @brief Define ADC12 validity check for the channel number */
  21. #define ADC12_IS_CHANNEL_INVALID(CH) (CH > ADC12_SOC_MAX_CH_NUM)
  22. /** @brief Define ADC12 validity check for the trigger number */
  23. #define ADC12_IS_TRIG_CH_INVLAID(CH) (CH > ADC_SOC_MAX_TRIG_CH_NUM)
  24. /** @brief Define ADC12 validity check for the trigger length */
  25. #define ADC12_IS_TRIG_LEN_INVLAID(TRIG_LEN) (TRIG_LEN > ADC_SOC_MAX_TRIG_CH_LEN)
  26. /** @brief Define ADC12 validity check for the sequence length */
  27. #define ADC12_IS_SEQ_LEN_INVLAID(LEN) ((LEN == 0) || (LEN > ADC_SOC_SEQ_MAX_LEN))
  28. /** @brief Define ADC12 validity check for the DMA buffer length in the sequence mode */
  29. #define ADC12_IS_SEQ_DMA_BUFF_LEN_INVLAID(LEN) ((LEN == 0) || (LEN > ADC_SOC_SEQ_MAX_DMA_BUFF_LEN_IN_4BYTES))
  30. /** @brief Define ADC12 validity check for the DMA buffer length in the preemption mode */
  31. #define ADC12_IS_PMT_DMA_BUFF_LEN_INVLAID(LEN) ((LEN == 0) || (LEN > ADC_SOC_PMT_MAX_DMA_BUFF_LEN_IN_4BYTES))
  32. /** @brief Define ADC12 sample signal types. */
  33. typedef enum {
  34. adc12_sample_signal_single_ended = 0,
  35. adc12_sample_signal_differential = 1,
  36. adc12_sample_signal_count = 2
  37. } adc12_sample_signal_t;
  38. /** @brief Define ADC12 resolutions. */
  39. typedef enum {
  40. adc12_res_6_bits = 0,
  41. adc12_res_8_bits,
  42. adc12_res_10_bits,
  43. adc12_res_12_bits
  44. } adc12_resolution_t;
  45. /** @brief Define ADC12 conversion modes. */
  46. typedef enum {
  47. adc12_conv_mode_oneshot = 0,
  48. adc12_conv_mode_period,
  49. adc12_conv_mode_sequence,
  50. adc12_conv_mode_preemption
  51. } adc12_conversion_mode_t;
  52. /** @brief Define ADC12 irq events. */
  53. typedef enum {
  54. /** This mask indicates that a trigger conversion is complete. */
  55. adc12_event_trig_complete = ADC12_INT_STS_TRIG_CMPT_MASK,
  56. /** This mask indicates that a conflict caused by software-triggered conversions. */
  57. adc12_event_trig_sw_conflict = ADC12_INT_STS_TRIG_SW_CFLCT_MASK,
  58. /** This mask indicates that a conflict caused by hardware-triggered conversions. */
  59. adc12_event_trig_hw_conflict = ADC12_INT_STS_TRIG_HW_CFLCT_MASK,
  60. /** This mask indicates that a conflict caused when bus reading from different channels. */
  61. adc12_event_read_conflict = ADC12_INT_STS_READ_CFLCT_MASK,
  62. /** This mask indicates that a conflict caused by sequence-triggered conversions. */
  63. adc12_event_seq_sw_conflict = ADC12_INT_STS_SEQ_SW_CFLCT_MASK,
  64. /** This mask indicates that a conflict caused by hardware-triggered conversions. */
  65. adc12_event_seq_hw_conflict = ADC12_INT_STS_SEQ_HW_CFLCT_MASK,
  66. /** This mask indicates that DMA is stopped currently. */
  67. adc12_event_seq_dma_abort = ADC12_INT_STS_SEQ_DMAABT_MASK,
  68. /** This mask indicates that all of the configured conversion(s) in a queue is(are) complete. */
  69. adc12_event_seq_full_complete = ADC12_INT_STS_SEQ_CMPT_MASK,
  70. /** This mask indicates that one of the configured conversion(s) in a queue is complete. */
  71. adc12_event_seq_single_complete = ADC12_INT_STS_SEQ_CVC_MASK,
  72. /** This mask indicates that DMA FIFO is full currently. */
  73. adc12_event_dma_fifo_full = ADC12_INT_STS_DMA_FIFO_FULL_MASK
  74. } adc12_irq_event_t;
  75. /** @brief Define ADC12 Clock Divider */
  76. typedef enum {
  77. adc12_clock_divider_1 = 1,
  78. adc12_clock_divider_2,
  79. adc12_clock_divider_3,
  80. adc12_clock_divider_4,
  81. adc12_clock_divider_5,
  82. adc12_clock_divider_6,
  83. adc12_clock_divider_7,
  84. adc12_clock_divider_8,
  85. adc12_clock_divider_9,
  86. adc12_clock_divider_10,
  87. adc12_clock_divider_11,
  88. adc12_clock_divider_12,
  89. adc12_clock_divider_13,
  90. adc12_clock_divider_14,
  91. adc12_clock_divider_15,
  92. adc12_clock_divider_16,
  93. } adc12_clock_divider_t;
  94. /** @brief ADC12 common configuration struct. */
  95. typedef struct {
  96. uint8_t res;
  97. uint8_t conv_mode;
  98. uint32_t adc_clk_div;
  99. bool wait_dis;
  100. bool sel_sync_ahb;
  101. bool adc_ahb_en;
  102. } adc12_config_t;
  103. /** @brief ADC12 channel configuration struct. */
  104. typedef struct {
  105. uint8_t ch;
  106. uint8_t diff_sel;
  107. uint16_t thshdh;
  108. uint16_t thshdl;
  109. uint8_t sample_cycle_shift;
  110. uint32_t sample_cycle;
  111. } adc12_channel_config_t;
  112. /** @brief ADC12 DMA configuration struct. */
  113. typedef struct {
  114. uint32_t *start_addr;
  115. uint32_t buff_len_in_4bytes;
  116. uint32_t stop_pos;
  117. bool stop_en;
  118. } adc12_dma_config_t;
  119. /** @brief ADC12 DMA configuration struct for the sequence mode. */
  120. typedef struct {
  121. uint32_t :4;
  122. uint32_t result :12;
  123. uint32_t seq_num :4;
  124. uint32_t :4;
  125. uint32_t adc_ch :5;
  126. uint32_t :2;
  127. uint32_t cycle_bit :1;
  128. } adc12_seq_dma_data_t;
  129. /** @brief ADC12 DMA configuration struct for the preemption mode. */
  130. typedef struct {
  131. uint32_t :4;
  132. uint32_t result :12;
  133. uint32_t seq_num :2;
  134. uint32_t :2;
  135. uint32_t trig_ch :4;
  136. uint32_t adc_ch :5;
  137. uint32_t :2;
  138. uint32_t cycle_bit :1;
  139. } adc12_pmt_dma_data_t;
  140. /** @brief ADC12 configuration struct for the period mode. */
  141. typedef struct {
  142. uint8_t ch;
  143. uint8_t prescale;
  144. uint8_t period_count;
  145. } adc12_prd_config_t;
  146. /** @brief ADC12 queue configuration struct for the sequence mode. */
  147. typedef struct {
  148. bool seq_int_en;
  149. uint8_t ch;
  150. } adc12_seq_queue_config_t;
  151. /** @brief ADC12 configuration struct for the sequence mode. */
  152. typedef struct {
  153. adc12_seq_queue_config_t queue[ADC_SOC_SEQ_MAX_LEN];
  154. bool restart_en;
  155. bool cont_en;
  156. bool sw_trig_en;
  157. bool hw_trig_en;
  158. uint8_t seq_len;
  159. } adc12_seq_config_t;
  160. /** @brief ADC12 trigger configuration struct for the preemption mode. */
  161. typedef struct {
  162. bool inten[ADC_SOC_MAX_TRIG_CH_LEN];
  163. uint8_t adc_ch[ADC_SOC_MAX_TRIG_CH_LEN];
  164. uint8_t trig_ch;
  165. uint8_t trig_len;
  166. } adc12_pmt_config_t;
  167. #ifdef __cplusplus
  168. extern "C" {
  169. #endif
  170. /**
  171. * @name Initialization and Deinitialization
  172. * @{
  173. */
  174. /**
  175. * @brief Get a default configuration for an ADC12 instance.
  176. *
  177. * @param[out] config A pointer to the configuration struct of @ref adc12_config_t.
  178. */
  179. void adc12_get_default_config(adc12_config_t *config);
  180. /**
  181. * @brief Get a default configuration for an ADC12 channel.
  182. *
  183. * @param[out] config A pointer to the configuration struct of @ref adc12_channel_config_t.
  184. */
  185. void adc12_get_channel_default_config(adc12_channel_config_t *config);
  186. /**
  187. * @brief Initialize an ADC12 instance.
  188. *
  189. * @param[in] ptr An ADC12 peripheral base address.
  190. * @param[in] config A pointer to the configuration struct of @ref adc12_config_t.
  191. * @return A result of initializing an ADC12 instance.
  192. * @retval status_success Initialize an ADC12 instance successfully. Please refer to @ref hpm_stat_t.
  193. * @retval status_invalid_argument Initialize an ADC12 instance unsuccessfully due to passing one or more invalid arguments. Please refer to @ref hpm_stat_t.
  194. */
  195. hpm_stat_t adc12_init(ADC12_Type *ptr, adc12_config_t *config);
  196. /**
  197. * @brief Initialize an ADC12 channel.
  198. *
  199. * @param[in] ptr An ADC12 peripheral base address.
  200. * @param[in] config A pointer to the configuration struct of @ref adc12_channel_config_t.
  201. * @return A result of initializing an ADC12 channel.
  202. * @retval status_success Initialize an ADC12 channel successfully. Please refer to @ref hpm_stat_t.
  203. * @retval status_invalid_argument Initialize an ADC12 channel unsuccessfully due to passing one or more invalid arguments. Please refer to @ref hpm_stat_t.
  204. */
  205. hpm_stat_t adc12_init_channel(ADC12_Type *ptr, adc12_channel_config_t *config);
  206. /**
  207. * @brief Configure the the period mode for an ADC12 instance.
  208. *
  209. * @param[in] ptr An ADC12 peripheral base address.
  210. * @param[in] config A pointer to the configuration struct of @ref adc12_prd_config_t.
  211. * @return A result of configuring the the period mode for an ADC12 instance.
  212. * @retval status_success Configure the the period mode successfully. Please refer to @ref hpm_stat_t.
  213. * @retval status_invalid_argument Configure the the period mode unsuccessfully due to passing one or more invalid arguments. Please refer to @ref hpm_stat_t.
  214. */
  215. hpm_stat_t adc12_set_prd_config(ADC12_Type *ptr, adc12_prd_config_t *config);
  216. /**
  217. * @brief Configure the the sequence mode for an ADC12 instance.
  218. *
  219. * @param[in] ptr An ADC12 peripheral base address.
  220. * @param[in] config A pointer to configuration struct of @ref adc12_seq_config_t.
  221. * @return A result of configuring the the sequence mode for an ADC12 instance.
  222. * @retval status_success Configure the the sequence mode successfully. Please refer to @ref hpm_stat_t.
  223. * @retval status_invalid_argument Configure the the sequence mode unsuccessfully due to passing one or more invalid arguments. Please refer to @ref hpm_stat_t.
  224. */
  225. hpm_stat_t adc12_set_seq_config(ADC12_Type *ptr, adc12_seq_config_t *config);
  226. /**
  227. * @brief Configure the preemption mode for an ADC12 instance.
  228. *
  229. * @param[in] ptr An ADC12 peripheral base address.
  230. * @param[in] config A pointer to configuration struct of @ref adc12_pmt_config_t.
  231. * @return A result of configuring the preemption mode for an ADC12 instance.
  232. * @retval status_success Configure the preemption mode successfully. Please refer to @ref hpm_stat_t.
  233. * @retval status_invalid_argument Configure the preemption mode unsuccessfully due to passing one or more invalid arguments. Please refer to @ref hpm_stat_t.
  234. */
  235. hpm_stat_t adc12_set_pmt_config(ADC12_Type *ptr, adc12_pmt_config_t *config);
  236. /** @} */
  237. /**
  238. * @name DMA Control
  239. * @{
  240. */
  241. /**
  242. * @brief Configure the stop position offset in the specified memory of DMA write operation for the the sequence mode.
  243. *
  244. * @param[in] ptr An ADC12 peripheral base address.
  245. * @param[in] stop_pos A stop position offset.
  246. */
  247. static inline void adc12_set_seq_stop_pos(ADC12_Type *ptr, uint16_t stop_pos)
  248. {
  249. ptr->SEQ_DMA_CFG = (ptr->SEQ_DMA_CFG & ~ADC12_SEQ_DMA_CFG_STOP_POS_MASK)
  250. | ADC12_SEQ_DMA_CFG_STOP_POS_SET(stop_pos);
  251. }
  252. /**
  253. * @brief Configure the start address of DMA write operation for the preemption mode.
  254. *
  255. * @param[in] ptr An ADC12 peripheral base address.
  256. * @param[in] addr A start address of DMA write operation.
  257. */
  258. static inline void adc12_init_pmt_dma(ADC12_Type *ptr, uint32_t addr)
  259. {
  260. ptr->TRG_DMA_ADDR = addr & ADC12_TRG_DMA_ADDR_TRG_DMA_ADDR_MASK;
  261. }
  262. /**
  263. * @brief Configure the start address of DMA write operation for the sequence mode.
  264. *
  265. * @param[in] ptr An ADC12 peripheral base address.
  266. * @param[in] config A pointer to configuration struct of @ref adc12_dma_config_t.
  267. * @return An implementation result of DMA initializing for the sequence mode
  268. * @retval status_success ADC12 initialize in sequence mode successfully. Please refert to @ref hpm_stat_t.
  269. * @retval status_invalid_argument ADC12 initialize in sequence mode unsuccessfully due to passing invalid arguments. Please refert to @ref hpm_stat_t.
  270. */
  271. hpm_stat_t adc12_init_seq_dma(ADC12_Type *ptr, adc12_dma_config_t *config);
  272. /** @} */
  273. /**
  274. * @name Status
  275. * @{
  276. */
  277. /**
  278. * @brief Get all ADC12 status flags.
  279. *
  280. * @param[in] ptr An ADC12 peripheral base address.
  281. * @return A mask indicating all corresponding interrupt statuses.
  282. * @retval A mask. Please refer to @ref adc12_irq_event_t.
  283. */
  284. static inline uint32_t adc12_get_status_flags(ADC12_Type *ptr)
  285. {
  286. return ptr->INT_STS;
  287. }
  288. /**
  289. * @brief Set value of the WAIT_DIS bit. The ADC does not block access to the associated peripheral bus
  290. * until the ADC has completed its conversion.
  291. *
  292. * @param[in] ptr An ADC12 peripheral base address.
  293. */
  294. static inline void adc12_disable_busywait(ADC12_Type *ptr)
  295. {
  296. ptr->BUF_CFG0 |= ADC12_BUF_CFG0_WAIT_DIS_SET(1);
  297. }
  298. /**
  299. * @brief Set value of the WAIT_DIS bit. ADC blocks access to the associated peripheral bus
  300. * until the ADC completes the conversion.
  301. *
  302. * @param[in] ptr An ADC12 peripheral base address.
  303. */
  304. static inline void adc12_enable_busywait(ADC12_Type *ptr)
  305. {
  306. ptr->BUF_CFG0 &= ~ADC12_BUF_CFG0_WAIT_DIS_MASK;
  307. }
  308. /**
  309. * @brief Get the status of a conversion validity.
  310. *
  311. * @param[in] ptr An ADC12 peripheral base address.
  312. * @param[in] ch An ADC12 peripheral channel.
  313. * @retval Status indicating the validity of the current conversion result.
  314. *
  315. * @note This function is only used when the WAIT_DIS bit in the BUF_RESULT register is 1.
  316. */
  317. static inline bool adc12_get_conv_valid_status(ADC12_Type *ptr, uint8_t ch)
  318. {
  319. return ADC12_BUS_RESULT_VALID_GET(ptr->BUS_RESULT[ch]);
  320. }
  321. /**
  322. * @brief Clear the status flags.
  323. *
  324. *
  325. * @param[in] ptr An ADC12 peripheral base address.
  326. * @param[in] mask A mask that means the specified flags to be cleared. Please refer to @ref adc12_irq_event_t.
  327. *
  328. * @note Only the specified flags can be cleared by writing the INT_STS register.
  329. */
  330. static inline void adc12_clear_status_flags(ADC12_Type *ptr, uint32_t mask)
  331. {
  332. ptr->INT_STS |= mask;
  333. }
  334. /** @} */
  335. /**
  336. * @name Interrupts
  337. * @{
  338. */
  339. /**
  340. * @brief Enable interrupts.
  341. *
  342. * @param[in] ptr An ADC12 peripheral base address.
  343. * @param[in] mask A mask indicating the specified ADC interrupt events. Please refer to @ref adc12_irq_event_t.
  344. */
  345. static inline void adc12_enable_interrupts(ADC12_Type *ptr, uint32_t mask)
  346. {
  347. ptr->INT_EN |= mask;
  348. }
  349. /**
  350. * @brief Disable interrupts.
  351. *
  352. * @param[in] ptr An ADC12 peripheral base address.
  353. * @param[in] mask A mask indicating the specified interrupt events. Please refer to @ref adc12_irq_event_t.
  354. */
  355. static inline void adc12_disable_interrupts(ADC12_Type *ptr, uint32_t mask)
  356. {
  357. ptr->INT_EN &= ~mask;
  358. }
  359. /** @} */
  360. /**
  361. * @name Trigger and Conversion
  362. * @{
  363. */
  364. /**
  365. * @brief Trigger ADC conversions by software in sequence mode
  366. *
  367. * @param[in] ptr An ADC12 peripheral base address.
  368. * @return An implementation result of getting an ADC12 software trigger.
  369. * @retval status_success ADC12 software triggers successfully. Please refer to @ref hpm_stat_t.
  370. * @retval status_fail ADC12 software triggers unsuccessfully. Please refer to @ref hpm_stat_t.
  371. */
  372. hpm_stat_t adc12_trigger_seq_by_sw(ADC12_Type *ptr);
  373. /**
  374. * @brief Trigger ADC conversions by software in preemption mode
  375. *
  376. * @param[in] ptr An ADC12 peripheral base address.
  377. * @param[in] trig_ch A trigger channel number(e.g. TRIG0A,TRIG0B,TRIG0C...).
  378. * @return An implementation result of getting an ADC12 software trigger.
  379. * @retval status_success ADC12 software triggers successfully. Please refer to @ref hpm_stat_t.
  380. * @retval status_fail ADC12 software triggers unsuccessfully. Please refer to @ref hpm_stat_t.
  381. */
  382. hpm_stat_t adc12_trigger_pmt_by_sw(ADC12_Type *ptr, uint8_t trig_ch);
  383. /**
  384. * @brief Get the result in oneshot mode.
  385. *
  386. * @param[in] ptr An ADC12 peripheral base address.
  387. * @param[in] ch An ADC12 peripheral channel.
  388. * @param[out] result A pointer to an ADC12 conversion result.
  389. * @return An implementation result of getting an ADC12 conversion result in oneshot mode.
  390. * @retval status_success Get the result of an ADC12 conversion in oneshot mode successfully. Please refer to @ref hpm_stat_t.
  391. * @retval status_invalid_argument Get the result of an ADC12 conversion in oneshot mode unsuccessfully due to passing invalid arguments. Please refer to @ref hpm_stat_t.
  392. */
  393. hpm_stat_t adc12_get_oneshot_result(ADC12_Type *ptr, uint8_t ch, uint16_t *result);
  394. /**
  395. * @brief Get the result in the period mode.
  396. *
  397. * @param[in] ptr An ADC12 peripheral base address.
  398. * @param[in] ch An ADC12 peripheral channel.
  399. * @param[out] result A pointer to a specified ADC12 conversion result
  400. * @return An implementation of getting an ADC12 conversion result in the period mode.
  401. * @retval status_success Get the result of an ADC12 conversion in the period mode successfully. Please refer to @ref hpm_stat_t.
  402. * @retval status_invalid_argument Get the result of an ADC12 conversion in the period mode unsuccessfully due to passing invalid arguments. Please refer to @ref hpm_stat_t.
  403. */
  404. hpm_stat_t adc12_get_prd_result(ADC12_Type *ptr, uint8_t ch, uint16_t *result);
  405. /** @} */
  406. #ifdef __cplusplus
  407. }
  408. #endif
  409. /** @} */
  410. #endif /* HPM_ADC12_DRV_H */