1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
|
/*
* Copyright (c) 2022 hpmicro
*
* SPDX-License-Identifier: BSD-3-Clause
*
*/
#ifndef HPM_ADC_H
#define HPM_ADC_H
#include "hpm_common.h"
#ifdef CONFIG_HAS_HPMSDK_ADC12
#include "hpm_adc12_drv.h"
#endif
#ifdef CONFIG_HAS_HPMSDK_ADC16
#include "hpm_adc16_drv.h"
#endif
#include "hpm_soc_feature.h"
/**
* @brief ADC HAL driver APIs
* @defgroup hpm_adc_interface HPM ADC driver APIs
* @ingroup hpm_adc_interfaces
* @{
*/
/**
* @brief An ADC peripheral base address.
*
*/
typedef union
{
#ifdef CONFIG_HAS_HPMSDK_ADC12
ADC12_Type *adc12;
#endif
#ifdef CONFIG_HAS_HPMSDK_ADC16
ADC16_Type *adc16;
#endif
} adc_base;
/**
* @brief use adc12 or adc16.
*
*/
#define ADCX_MODULE_ADC12 1
#define ADCX_MODULE_ADC16 2
typedef enum {
adc_module_adc12 = ADCX_MODULE_ADC12,
adc_module_adc16 = ADCX_MODULE_ADC16
} adc_module;
/**
* @brief ADC common configuration struct.
*
*/
typedef struct {
adc_module module;
adc_base adc_base;
struct {
#ifdef CONFIG_HAS_HPMSDK_ADC12
adc12_config_t adc12;
#endif
#ifdef CONFIG_HAS_HPMSDK_ADC16
adc16_config_t adc16;
#endif
} config;
} adc_config_t;
/**
* @brief ADC channel configuration struct.
*
*/
typedef struct {
adc_module module;
adc_base adc_base;
struct {
#ifdef CONFIG_HAS_HPMSDK_ADC12
adc12_channel_config_t adc12_ch;
#endif
#ifdef CONFIG_HAS_HPMSDK_ADC16
adc16_channel_config_t adc16_ch;
#endif
} config;
} adc_channel_config_t;
/**
* @brief ADC DMA configuration struct.
*
* */
typedef struct {
adc_module module;
adc_base adc_base;
struct {
#ifdef CONFIG_HAS_HPMSDK_ADC12
adc12_dma_config_t adc12;
#endif
#ifdef CONFIG_HAS_HPMSDK_ADC16
adc16_dma_config_t adc16;
#endif
} config;
} adc_dma_config_t;
/**
* @brief ADC configuration struct for period mode.
*
* */
typedef struct {
adc_module module;
adc_base adc_base;
struct {
#ifdef CONFIG_HAS_HPMSDK_ADC12
adc12_prd_config_t adc12;
#endif
#ifdef CONFIG_HAS_HPMSDK_ADC16
adc16_prd_config_t adc16;
#endif
} config;
} adc_prd_config_t;
/**
* @brief ADC configuration struct for sequence mode.
*
* */
typedef struct {
adc_module module;
adc_base adc_base;
struct {
#ifdef CONFIG_HAS_HPMSDK_ADC12
adc12_seq_config_t adc12;
#endif
#ifdef CONFIG_HAS_HPMSDK_ADC16
adc16_seq_config_t adc16;
#endif
} config;
} adc_seq_config_t;
/**
* @brief ADC trigger configuration struct for preempt mode.
*
* */
typedef struct
{
adc_module module;
adc_base adc_base;
struct {
#ifdef CONFIG_HAS_HPMSDK_ADC12
adc12_pmt_config_t adc12;
#endif
#ifdef CONFIG_HAS_HPMSDK_ADC16
adc16_pmt_config_t adc16;
#endif
} config;
} adc_pmt_config_t;
typedef struct
{
adc_module module;
adc_base adc_base;
} adc_type;
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Get a default configuration for an ADC instance.
*
* @param[out] config A pointer to the configuration struct of "adc_config_t".
*
*/
static inline void hpm_adc_init_default_config(adc_config_t *config)
{
if (config->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
adc12_get_default_config(&config->config.adc12);
#endif
} else if (config->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
adc16_get_default_config(&config->config.adc16);
#endif
}
}
/**
* @brief Get a default configuration for an ADC channel instance.
*
* @param[out] config A pointer to the configuration struct of "adc_channel_config_t".
*
*/
static inline void hpm_adc_init_channel_default_config(adc_channel_config_t *config)
{
if (config->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
adc12_get_channel_default_config(&config->config.adc12_ch);
#endif
} else if (config->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
adc16_get_channel_default_config(&config->config.adc16_ch);
#endif
}
}
/**
* @brief Initialize an ADC instance.
*
* @param[in] config A pointer to the configuration struct of "adc_config_t".
* @retval status_success Initialize an ADC instance successfully.
* @retval status_invalid_argument Initialize an ADC instance unsuccessfully because of passing one or more invalid arguments.
*/
static inline hpm_stat_t hpm_adc_init(adc_config_t *config)
{
if (config->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
return adc12_init(config->adc_base.adc12, &config->config.adc12);
#else
return status_invalid_argument;
#endif
} else if (config->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
return adc16_init(config->adc_base.adc16, &config->config.adc16);
#else
return status_invalid_argument;
#endif
} else {
return false;
}
}
/**
* @brief Initialize an ADC channel.
*
* @param[in] config A pointer to the configuration struct of "adc_config_t".
* @retval status_success Initialize an ADC instance successfully.
* @retval status_invalid_argument Initialize an ADC instance unsuccessfully because of passing one or more invalid arguments.
*/
static inline hpm_stat_t hpm_adc_channel_init(adc_channel_config_t *config)
{
if (config->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
return adc12_init_channel(config->adc_base.adc12, &config->config.adc12_ch);
#else
return status_invalid_argument;
#endif
} else if (config->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
return adc16_init_channel(config->adc_base.adc16, &config->config.adc16_ch);
#else
return status_invalid_argument;
#endif
} else {
return false;
}
}
/**
* @brief Configure the periodic mode for an ADC instance.
*
* @param[in] config A pointer to the configuration struct of "adc_prd_config_t".
* @retval status_success Configure the periodic mode for an ADC instance successfully.
* @retval status_invalid_argument Configure the periodic mode for an ADC instance unsuccessfully because of passing one or more invalid arguments.
*
*/
static inline hpm_stat_t hpm_adc_set_period_config(adc_prd_config_t *config)
{
if (config->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
return adc12_set_prd_config(config->adc_base.adc12, &config->config.adc12);
#else
return status_invalid_argument;
#endif
} else if (config->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
return adc16_set_prd_config(config->adc_base.adc16, &config->config.adc16);
#else
return status_invalid_argument;
#endif
} else {
return false;
}
}
/**
* @brief Configure the sequence mode for an ADC instance.
*
* @param[in] config A pointer to configuration struct of "adc_seq_config_t".
* @retval status_success Configure the sequence mode for an ADC instance successfully.
* @retval status_invalid_argument Configure the sequence mode for an ADC instance unsuccessfully because of passing one or more invalid arguments.
*/
static inline hpm_stat_t hpm_adc_set_sequence_config(adc_seq_config_t *config)
{
if (config->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
return adc12_set_seq_config(config->adc_base.adc12, &config->config.adc12);
#else
return status_invalid_argument;
#endif
} else if (config->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
return adc16_set_seq_config(config->adc_base.adc16, &config->config.adc16);
#else
return status_invalid_argument;
#endif
} else {
return false;
}
}
/**
* @brief Configure the preemption mode for an ADC instance.
*
* @param[in] config a pointer to configuration struct of "adc_pmt_config_t".
* @retval status_success Configure the preemption mode for an ADC instance successfully.
* @retval status_invalid_argument Configure the preemption mode for an ADC instance unsuccessfully because of passing one or more invalid arguments.
*/
static inline hpm_stat_t hpm_adc_set_preempt_config(adc_pmt_config_t *config)
{
if (config->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
return adc12_set_pmt_config(config->adc_base.adc12, &config->config.adc12);
#else
return status_invalid_argument;
#endif
} else if (config->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
return adc16_set_pmt_config(config->adc_base.adc16, &config->config.adc16);
#else
return status_invalid_argument;
#endif
} else {
return false;
}
}
/**
* @brief Configure the stop position offset in the specified memory for DMA write operation for sequence mode.
*
* @param[in] ptr An ADC peripheral base address.
* @param[in] stop_pos The stop position offset.
*/
static inline void hpm_adc_set_seq_stop_pos(adc_type *ptr, uint16_t stop_pos)
{
if (ptr->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
adc12_set_seq_stop_pos(ptr->adc_base.adc12, stop_pos);
#endif
} else if (ptr->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
adc16_set_seq_stop_pos(ptr->adc_base.adc16, stop_pos);
#endif
}
}
/**
* @brief Configure the start address of DMA write operation for preemption mode.
*
* @param[in] ptr An ADC peripheral base address.
* @param[in] addr The start address of DMA write operation.
*/
static inline void hpm_adc_init_pmt_dma(adc_type *ptr, uint32_t addr)
{
if (ptr->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
adc12_init_pmt_dma(ptr->adc_base.adc12, addr);
#endif
} else if (ptr->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
adc16_init_pmt_dma(ptr->adc_base.adc16, addr);
#endif
}
}
/**
* @brief Configure the start address of DMA write operation for preemption mode.
*
* @param[in] config A pointer to configuration struct of "adc_dma_config_t".
*/
static inline void hpm_adc_init_seq_dma(adc_dma_config_t *config)
{
if (config->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
adc12_init_seq_dma(config->adc_base.adc12, &config->config.adc12);
#endif
} else if (config->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
adc16_init_seq_dma(config->adc_base.adc16, &config->config.adc16);
#endif
}
}
/**
* @brief Get ADC status flags.
*
* This function gets all ADC status flags.
* @param[in] ptr An ADC peripheral base address.
* @retval Status The ADC interrupt status flags.
*/
static inline uint32_t hpm_adc_get_status_flags(adc_type *ptr)
{
if (ptr->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
return adc12_get_status_flags(ptr->adc_base.adc12);
#else
return 0;
#endif
} else if (ptr->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
return adc16_get_status_flags(ptr->adc_base.adc16);
#else
return 0;
#endif
} else {
return false;
}
}
/**
* @brief Get the setting value of wait disable.
*
* This status flag is only used when wait_dis is set to disable.
*
* @param[in] ptr An ADC peripheral base address.
* @retval Status It means whether the current setting of wait disable is disable.
*/
static inline bool hpm_adc_get_wait_dis_status(adc_type *ptr)
{
if (ptr->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
return adc12_get_wait_dis_status(ptr->adc_base.adc12);
#else
return 1;
#endif
} else if (ptr->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
return adc16_get_wait_dis_status(ptr->adc_base.adc16);
#else
return 1;
#endif
} else {
return false;
}
}
/**
* @brief Get status flag of a conversion.
*
* This status flag is only used when wait_dis is set to disable.
*
* @param[in] ptr An ADC peripheral base address.
* @param[in] ch An ADC peripheral channel.
* @retval Status It means the current conversion is valid.
*/
static inline bool hpm_adc_get_conv_valid_status(adc_type *ptr, uint8_t ch)
{
if (ptr->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
return adc12_get_conv_valid_status(ptr->adc_base.adc12, ch);
#else
return 0;
#endif
} else if (ptr->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
return adc16_get_conv_valid_status(ptr->adc_base.adc16, ch);
#else
return 0;
#endif
} else {
return false;
}
}
/**
* @brief Clear status flags.
*
* Only the specified flags can be cleared by writing INT_STS register.
*
* @param[in] ptr An ADC peripheral base address.
* @param[in] mask Mask value for flags to be cleared. Refer to "adc12_irq_event_t". Refer to "adc16_irq_event_t".
*/
static inline void hpm_adc_clear_status_flags(adc_type *ptr, uint32_t mask)
{
if (ptr->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
adc12_clear_status_flags(ptr->adc_base.adc12, mask);
#endif
} else if (ptr->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
adc16_clear_status_flags(ptr->adc_base.adc16, mask);
#endif
}
}
/**
* @brief Enable interrupts.
*
* @param[in] ptr An ADC peripheral base address.
* @param[in] mask Mask value for interrupt events. Refer to "adc12_irq_event_t".Refer to "adc16_irq_event_t".
*/
static inline void hpm_adc_enable_interrupts(adc_type *ptr, uint32_t mask)
{
if (ptr->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
adc12_enable_interrupts(ptr->adc_base.adc12, mask);
#endif
} else if (ptr->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
adc16_enable_interrupts(ptr->adc_base.adc16, mask);
#endif
}
}
/**
* @brief Disable interrupts.
*
* @param[in] ptr An ADC peripheral base address.
* @param[in] mask Mask value for interrupt events. Refer to "adc12_irq_event_t".Refer to "adc16_irq_event_t".
*/
static inline void hpm_adc_disable_interrupts(adc_type *ptr, uint32_t mask)
{
if (ptr->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
adc12_disable_interrupts(ptr->adc_base.adc12, mask);
#endif
} else if (ptr->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
adc16_disable_interrupts(ptr->adc_base.adc16, mask);
#endif
}
}
/**
* @brief Get the result in oneshot mode.
*
* @param[in] ptr An ADC peripheral base address.
* @param[in] ch An ADC peripheral channel.
* @param[out] result The result of an ADC12 conversion.
*
* @retval status_success Get the result of an ADC12 conversion in oneshot mode successfully.
* @retval status_invalid_argument Get the result of an ADC12 conversion in oneshot mode unsuccessfully because of passing invalid arguments.
*/
static inline hpm_stat_t hpm_adc_get_oneshot_result(adc_type *ptr, uint8_t ch, uint16_t *result)
{
if (ptr->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
return adc12_get_oneshot_result(ptr->adc_base.adc12, ch, result);
#else
return status_invalid_argument;
#endif
} else if (ptr->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
return adc16_get_oneshot_result(ptr->adc_base.adc16, ch, result);
#else
return status_invalid_argument;
#endif
} else {
return false;
}
}
/**
* @brief Get the result in periodic mode.
*
* @param[in] ptr An ADC12 peripheral base address.
* @param[in] ch An ADC12 peripheral channel.
* @param[out] result The result of an ADC12 conversion.
*
* @retval status_success Get the result of an ADC12 conversion in periodic mode successfully.
* @retval status_invalid_argument Get the result of an ADC12 conversion in periodic mode unsuccessfully because of passing invalid arguments.
*/
hpm_stat_t hpm_adc_get_prd_result(adc_type *ptr, uint8_t ch, uint16_t *result)
{
if (ptr->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
return adc12_get_prd_result(ptr->adc_base.adc12, ch, result);
#else
return status_invalid_argument;
#endif
} else if (ptr->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
return adc16_get_prd_result(ptr->adc_base.adc16, ch, result);
#else
return status_invalid_argument;
#endif
} else {
return false;
}
}
/**
* @brief Do a software trigger for sequence mode.
*
* @param[in] ptr An adc peripheral base address.
*
*/
void hpm_adc_trigger_seq_by_sw(adc_type *ptr)
{
if (ptr->module == adc_module_adc12) {
#ifdef CONFIG_HAS_HPMSDK_ADC12
return adc12_trigger_seq_by_sw(ptr->adc_base.adc12);
#endif
} else if (ptr->module == adc_module_adc16) {
#ifdef CONFIG_HAS_HPMSDK_ADC16
return adc16_trigger_seq_by_sw(ptr->adc_base.adc16);
#endif
}
}
#ifdef __cplusplus
}
#endif
/** @} */
#endif
|