hpm_adc12_drv.h 15 KB

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