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