hpm_adc16_drv.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright (c) 2021 hpmicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_ADC16_DRV_H
  8. #define HPM_ADC16_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_adc16_regs.h"
  11. #include "hpm_soc_feature.h"
  12. /**
  13. * @brief ADC16 driver APIs
  14. * @defgroup adc16_interface ADC16 driver APIs
  15. * @ingroup adc_interfaces
  16. * @{
  17. */
  18. /** @brief Define ADC16 validity check for the channel number */
  19. #define ADC16_IS_CHANNEL_INVALID(CH) (CH > ADC16_SOC_MAX_CH_NUM && CH != ADC16_SOC_TEMP_CH_NUM)
  20. /** @brief Define ADC16 validity check for the trigger number */
  21. #define ADC16_IS_TRIG_CH_INVLAID(CH) (CH > ADC16_SOC_MAX_TRIG_CH_NUM)
  22. /** @brief Define ADC16 validity check for the trigger length */
  23. #define ADC16_IS_TRIG_LEN_INVLAID(TRIG_LEN) (TRIG_LEN > ADC_SOC_MAX_TRIG_CH_LEN)
  24. /** @brief Define ADC16 validity check for the sequence length */
  25. #define ADC16_IS_SEQ_LEN_INVLAID(LEN) ((LEN == 0) || (LEN > ADC_SOC_SEQ_MAX_LEN))
  26. /** @brief Define ADC16 validity check for the DMA buffer length in the sequence mode */
  27. #define ADC16_IS_SEQ_DMA_BUFF_LEN_INVLAID(LEN) ((LEN == 0) || (LEN > ADC_SOC_SEQ_MAX_DMA_BUFF_LEN_IN_4BYTES))
  28. /** @brief Define ADC16 validity check for the DMA buffer length in the preemption mode */
  29. #define ADC16_IS_PMT_DMA_BUFF_LEN_INVLAID(LEN) ((LEN == 0) || (LEN > ADC_SOC_PMT_MAX_DMA_BUFF_LEN_IN_4BYTES))
  30. /** @brief Define ADC16 conversion modes. */
  31. typedef enum {
  32. adc16_conv_mode_oneshot = 0,
  33. adc16_conv_mode_period,
  34. adc16_conv_mode_sequence,
  35. adc16_conv_mode_preemption
  36. } adc16_conversion_mode_t;
  37. /** @brief Define ADC16 irq events. */
  38. typedef enum {
  39. /** This mask indicates that a trigger conversion is complete. */
  40. adc16_event_trig_complete = ADC16_INT_STS_TRIG_CMPT_MASK,
  41. /** This mask indicates that a conflict caused by software-triggered conversions. */
  42. adc16_event_trig_sw_conflict = ADC16_INT_STS_TRIG_SW_CFLCT_MASK,
  43. /** This mask indicates that a conflict caused by hardware-triggered conversions. */
  44. adc16_event_trig_hw_conflict = ADC16_INT_STS_TRIG_HW_CFLCT_MASK,
  45. /** This mask indicates that a conflict caused when bus reading from different channels. */
  46. adc16_event_read_conflict = ADC16_INT_STS_READ_CFLCT_MASK,
  47. /** This mask indicates that a conflict caused by sequence-triggered conversions. */
  48. adc16_event_seq_sw_conflict = ADC16_INT_STS_SEQ_SW_CFLCT_MASK,
  49. /** This mask indicates that a conflict caused by hardware-triggered conversions. */
  50. adc16_event_seq_hw_conflict = ADC16_INT_STS_SEQ_HW_CFLCT_MASK,
  51. /** This mask indicates that DMA is stopped currently. */
  52. adc16_event_seq_dma_abort = ADC16_INT_STS_SEQ_DMAABT_MASK,
  53. /** This mask indicates that all of the configured conversion(s) in a queue is(are) complete. */
  54. adc16_event_seq_full_complete = ADC16_INT_STS_SEQ_CMPT_MASK,
  55. /** This mask indicates that one of the configured conversion(s) in a queue is complete. */
  56. adc16_event_seq_single_complete = ADC16_INT_STS_SEQ_CVC_MASK,
  57. /** This mask indicates that DMA FIFO is full currently. */
  58. adc16_event_dma_fifo_full = ADC16_INT_STS_DMA_FIFO_FULL_MASK
  59. } adc16_irq_event_t;
  60. /** @brief ADC16 common configuration struct. */
  61. typedef struct {
  62. uint8_t conv_mode;
  63. uint8_t wait_dis;
  64. uint32_t adc_clk_div;
  65. uint16_t conv_duration;
  66. bool port3_rela_time;
  67. bool sel_sync_ahb;
  68. bool adc_ahb_en;
  69. } adc16_config_t;
  70. /** @brief ADC16 channel configuration struct. */
  71. typedef struct {
  72. uint8_t ch;
  73. uint16_t thshdh;
  74. uint16_t thshdl;
  75. uint8_t sample_cycle_shift;
  76. uint32_t sample_cycle;
  77. } adc16_channel_config_t;
  78. /** @brief ADC16 DMA configuration struct. */
  79. typedef struct {
  80. uint32_t *start_addr;
  81. uint32_t buff_len_in_4bytes;
  82. uint32_t stop_pos;
  83. bool stop_en;
  84. } adc16_dma_config_t;
  85. /** @brief ADC16 DMA configuration struct for the sequence mode. */
  86. typedef struct {
  87. uint32_t result :16;
  88. uint32_t seq_num :4;
  89. uint32_t :4;
  90. uint32_t adc_ch :5;
  91. uint32_t :2;
  92. uint32_t cycle_bit :1;
  93. } adc16_seq_dma_data_t;
  94. /** @brief ADC16 DMA configuration struct for the preemption mode. */
  95. typedef struct {
  96. uint32_t result :16;
  97. uint32_t trig_ch :2;
  98. uint32_t :2;
  99. uint32_t trig_index :4;
  100. uint32_t adc_ch :5;
  101. uint32_t :2;
  102. uint32_t cycle_bit :1;
  103. } adc16_pmt_dma_data_t;
  104. /** @brief ADC16 configuration struct for the the period mode. */
  105. typedef struct {
  106. uint32_t clk_src_freq_in_hz;
  107. uint8_t ch;
  108. uint8_t prescale;
  109. uint8_t period_count;
  110. } adc16_prd_config_t;
  111. /** @brief ADC16 queue configuration struct for the sequence mode. */
  112. typedef struct {
  113. bool seq_int_en;
  114. uint8_t ch;
  115. } adc16_seq_queue_config_t;
  116. /** @brief ADC16 configuration struct for the sequence mode. */
  117. typedef struct {
  118. adc16_seq_queue_config_t queue[ADC_SOC_SEQ_MAX_LEN];
  119. bool restart_en;
  120. bool cont_en;
  121. bool sw_trig_en;
  122. bool hw_trig_en;
  123. uint8_t seq_len;
  124. } adc16_seq_config_t;
  125. /** @brief ADC16 trigger configuration struct for the preemption mode. */
  126. typedef struct {
  127. bool inten[ADC_SOC_MAX_TRIG_CH_LEN];
  128. uint8_t adc_ch[ADC_SOC_MAX_TRIG_CH_LEN];
  129. uint8_t trig_ch;
  130. uint8_t trig_len;
  131. } adc16_pmt_config_t;
  132. #ifdef __cplusplus
  133. extern "C" {
  134. #endif
  135. /**
  136. * @name Initialization and Deinitialization
  137. * @{
  138. */
  139. /**
  140. * @brief Get a default configuration for an ADC16 instance.
  141. *
  142. * @param[out] config A pointer to the configuration struct of @ref adc16_config_t.
  143. *
  144. */
  145. void adc16_get_default_config(adc16_config_t *config);
  146. /**
  147. * @brief Get a default configuration for an ADC16 Channel.
  148. *
  149. * @param[out] config A pointer to the configuration struct of @ref adc16_channel_config_t.
  150. */
  151. void adc16_get_channel_default_config(adc16_channel_config_t *config);
  152. /**
  153. * @brief Initialize an ADC16 instance.
  154. *
  155. * @param[in] ptr An ADC16 peripheral base address.
  156. * @param[in] config A pointer to the configuration struct of @ref adc16_config_t.
  157. * @return A result of initializing an ADC16 instance.
  158. * @retval status_success Initialize an ADC16 instance successfully. Please refer to @ref hpm_stat_t.
  159. * @retval status_invalid_argument Initialize an ADC16 instance unsuccessfully due to passing one or more invalid arguments. Please refer to @ref hpm_stat_t.
  160. */
  161. hpm_stat_t adc16_init(ADC16_Type *ptr, adc16_config_t *config);
  162. /**
  163. * @brief Initialize an ADC16 channel
  164. *
  165. * @param[in] ptr An ADC16 peripheral base address.
  166. * @param[in] config A pointer to the configuration struct of @ref adc16_channel_config_t.
  167. * @return A result of initializing an ADC16 channel.
  168. * @retval status_success Initialize an ADC16 channel successfully. Please refert to @ref hpm_stat_t.
  169. * @retval status_invalid_argument Initialize an ADC16 channel unsuccessfully due to passing one or more invalid arguments. Please refert to @ref hpm_stat_t.
  170. */
  171. hpm_stat_t adc16_init_channel(ADC16_Type *ptr, adc16_channel_config_t *config);
  172. /**
  173. * @brief Configure the the period mode for an ADC16 instance.
  174. *
  175. * @param[in] ptr An ADC16 peripheral base address.
  176. * @param[in] config A pointer to the configuration struct of @ref adc16_prd_config_t.
  177. * @return A result of configuring the the period mode for an ADC16 instance.
  178. * @retval status_success Configure the the period mode successfully. Please refert to @ref hpm_stat_t.
  179. * @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.
  180. */
  181. hpm_stat_t adc16_set_prd_config(ADC16_Type *ptr, adc16_prd_config_t *config);
  182. /**
  183. * @brief Configure the sequence mode for an ADC16 instance.
  184. *
  185. * @param[in] ptr An ADC16 peripheral base address.
  186. * @param[in] config A pointer to configuration struct of @ref adc16_seq_config_t.
  187. * @return A result of configuring the sequence mode for an ADC16 instance.
  188. * @retval status_success Configure the sequence mode successfully. Please refert to @ref hpm_stat_t.
  189. * @retval status_invalid_argument Configure the sequence mode unsuccessfully due to passing one or more invalid arguments. Please refert to @ref hpm_stat_t.
  190. */
  191. hpm_stat_t adc16_set_seq_config(ADC16_Type *ptr, adc16_seq_config_t *config);
  192. /**
  193. * @brief Configure the preemption mode for an ADC16 instance.
  194. *
  195. * @param[in] ptr An ADC16 peripheral base address.
  196. * @param[in] config A pointer to configuration struct of @ref adc16_pmt_config_t.
  197. * @return A result of configuring the preemption mode for an ADC16 instance.
  198. * @retval status_success Configure the preemption mode successfully. Please refert to @ref hpm_stat_t.
  199. * @retval status_invalid_argument Configure the preemption mode unsuccessfully due to passing one or more invalid arguments. Please refert to @ref hpm_stat_t.
  200. */
  201. hpm_stat_t adc16_set_pmt_config(ADC16_Type *ptr, adc16_pmt_config_t *config);
  202. /**
  203. * @brief Set the queue enable control.
  204. *
  205. * @param[in] ptr An ADC16 peripheral base address.
  206. * @param[in] trig_ch An ADC16 peripheral trigger channel.
  207. * @param[in] enable A enable control
  208. * @retval status_success Get the result of an ADC16 conversion in oneshot mode successfully.
  209. * @retval status_invalid_argument Get the result of an ADC16 conversion in oneshot mode unsuccessfully due to passing invalid arguments.
  210. */
  211. hpm_stat_t adc16_set_pmt_queue_enable(ADC16_Type *ptr, uint8_t trig_ch, bool enable);
  212. /** @} */
  213. /**
  214. * @name DMA Control
  215. * @{
  216. */
  217. /**
  218. * @brief Configure the stop position offset in the specified memory of DMA write operation for the sequence mode.
  219. *
  220. * @param[in] ptr An ADC16 peripheral base address.
  221. * @param[in] stop_pos A stop position offset.
  222. */
  223. static inline void adc16_set_seq_stop_pos(ADC16_Type *ptr, uint16_t stop_pos)
  224. {
  225. ptr->SEQ_DMA_CFG = (ptr->SEQ_DMA_CFG & ~ADC16_SEQ_DMA_CFG_STOP_POS_MASK)
  226. | ADC16_SEQ_DMA_CFG_STOP_POS_SET(stop_pos);
  227. }
  228. /**
  229. * @brief Configure the start address of DMA write operation for the preemption mode.
  230. *
  231. * @param[in] ptr An ADC16 peripheral base address.
  232. * @param[in] addr A start address of DMA write operation.
  233. */
  234. static inline void adc16_init_pmt_dma(ADC16_Type *ptr, uint32_t addr)
  235. {
  236. ptr->TRG_DMA_ADDR = addr & ADC16_TRG_DMA_ADDR_TRG_DMA_ADDR_MASK;
  237. }
  238. /**
  239. * @brief Configure the start address of DMA write operation for the preemption mode.
  240. *
  241. * @param[in] ptr An ADC16 peripheral base address.
  242. * @param[in] config A pointer to configuration struct of @ref adc16_dma_config_t.
  243. */
  244. void adc16_init_seq_dma(ADC16_Type *ptr, adc16_dma_config_t *config);
  245. /** @} */
  246. /**
  247. * @name Status
  248. * @{
  249. */
  250. /**
  251. * @brief Get all ADC16 status flags.
  252. *
  253. * @param[in] ptr An ADC16 peripheral base address.
  254. * @return A mask indicating all corresponding interrupt statuses.
  255. * @retval A mask. Please refer to @ref adc16_irq_event_t.
  256. */
  257. static inline uint32_t adc16_get_status_flags(ADC16_Type *ptr)
  258. {
  259. return ptr->INT_STS;
  260. }
  261. /**
  262. * @brief Get the setting value of the WAIT_DIS bit.
  263. *
  264. * @param[in] ptr An ADC16 peripheral base address.
  265. * @return Status that indicats whether the current setting of the WAIT_DIS bit in the BUF_RESULT register is disabled.
  266. * @retval true It means that the WAIT_DIS bit is 1.
  267. * @retval false It means that the WAIT_DIS bit is 0.
  268. */
  269. static inline bool adc16_get_wait_dis_status(ADC16_Type *ptr)
  270. {
  271. return ADC16_BUF_CFG0_WAIT_DIS_GET(ptr->BUF_CFG0);
  272. }
  273. /**
  274. * @brief Get the status of a conversion validity.
  275. *
  276. * @param[in] ptr An ADC16 peripheral base address.
  277. * @param[in] ch An ADC16 peripheral channel.
  278. * @retval Status indicating the validity of the current conversion result.
  279. *
  280. * @note This function is only used when the WAIT_DIS bit in the BUF_RESULT register is 1.
  281. */
  282. static inline bool adc16_get_conv_valid_status(ADC16_Type *ptr, uint8_t ch)
  283. {
  284. return ADC16_BUS_RESULT_VALID_GET(ptr->BUS_RESULT[ch]);
  285. }
  286. /**
  287. * @brief Clear the status flags.
  288. *
  289. *
  290. * @param[in] ptr An ADC16 peripheral base address.
  291. * @param[in] mask A mask that means the specified flags to be cleared. Please refer to @ref adc16_irq_event_t.
  292. *
  293. * @note Only the specified flags can be cleared by writing the INT_STS register.
  294. */
  295. static inline void adc16_clear_status_flags(ADC16_Type *ptr, uint32_t mask)
  296. {
  297. ptr->INT_STS |= mask;
  298. }
  299. /** @} */
  300. /**
  301. * @name Interrupts
  302. * @{
  303. */
  304. /**
  305. * @brief Enable interrupts.
  306. *
  307. * @param[in] ptr An ADC16 peripheral base address.
  308. * @param[in] mask A mask indicating the specified ADC interrupt events. Please refer to @ref adc16_irq_event_t.
  309. */
  310. static inline void adc16_enable_interrupts(ADC16_Type *ptr, uint32_t mask)
  311. {
  312. ptr->INT_EN |= mask;
  313. }
  314. /**
  315. * @brief Disable interrupts.
  316. *
  317. * @param[in] ptr An ADC16 peripheral base address.
  318. * @param[in] mask A mask indicating the specified interrupt events. Please refer to @ref adc16_irq_event_t.
  319. */
  320. static inline void adc16_disable_interrupts(ADC16_Type *ptr, uint32_t mask)
  321. {
  322. ptr->INT_EN &= ~mask;
  323. }
  324. /** @} */
  325. /**
  326. * @name Trigger and Conversion
  327. * @{
  328. */
  329. /**
  330. * @brief Trigger ADC coversions by software
  331. *
  332. * @param[in] ptr An ADC16 peripheral base address.
  333. */
  334. void adc16_trigger_seq_by_sw(ADC16_Type *ptr);
  335. /**
  336. * @brief Get the result in oneshot mode.
  337. *
  338. * @param[in] ptr An ADC16 peripheral base address.
  339. * @param[in] ch An ADC16 peripheral channel.
  340. * @param[out] result A pointer to an ADC16 conversion result.
  341. * @return An implementation result of getting an ADC16 conversion result in oneshot mode.
  342. * @retval status_success Get the result of an ADC16 conversion in oneshot mode successfully. Please refert to @ref hpm_stat_t.
  343. * @retval status_invalid_argument Get the result of an ADC16 conversion in oneshot mode unsuccessfully due to passing invalid arguments. Please refert to @ref hpm_stat_t.
  344. */
  345. hpm_stat_t adc16_get_oneshot_result(ADC16_Type *ptr, uint8_t ch, uint16_t *result);
  346. /**
  347. * @brief Get the result in the period mode.
  348. *
  349. * @param[in] ptr An ADC16 peripheral base address.
  350. * @param[in] ch An ADC16 peripheral channel.
  351. * @param[out] result A pointer to a specified ADC16 conversion result
  352. * @return An implementation of getting an ADC16 conversion result in the period mode.
  353. * @retval status_success Get the result of an ADC16 conversion in the period mode successfully. Please refert to @ref hpm_stat_t.
  354. * @retval status_invalid_argument Get the result of an ADC16 conversion in the period mode unsuccessfully due to passing invalid arguments. Please refert to @ref hpm_stat_t.
  355. */
  356. hpm_stat_t adc16_get_prd_result(ADC16_Type *ptr, uint8_t ch, uint16_t *result);
  357. /** @} */
  358. #ifdef __cplusplus
  359. }
  360. #endif
  361. /** @} */
  362. #endif /* HPM_ADC16_DRV_H */