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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
|
/**
* \file
*
* \brief SAM Peripheral Analog-to-Digital Converter Driver
*
* Copyright (C) 2012-2016 Atmel Corporation. All rights reserved.
*
* \asf_license_start
*
* \page License
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an
* Atmel microcontroller product.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* \asf_license_stop
*
*/
/*
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/
#ifndef ADC_H_INCLUDED
#define ADC_H_INCLUDED
/**
* \defgroup asfdoc_sam0_adc_group SAM Analog-to-Digital Converter (ADC) Driver
*
* This driver for Atmel® | SMART ARM®-based microcontrollers provides an interface for the configuration
* and management of the device's Analog-to-Digital Converter functionality, for
* the conversion of analog voltages into a corresponding digital form.
* The following driver Application Programming Interface (API) modes are covered by this manual:
* - Polled APIs
* \if ADC_CALLBACK_MODE
* - Callback APIs
* \endif
*
* The following peripheral is used by this module:
* - ADC (Analog-to-Digital Converter)
*
* The following devices can use this module:
* \if DEVICE_SAML21_SUPPORT
* - Atmel | SMART SAM L21/L22
* - Atmel | SMART SAM C20/C21
* \else
* - Atmel | SMART SAM D20/D21
* - Atmel | SMART SAM R21
* - Atmel | SMART SAM D09/D10/D11
* - Atmel | SMART SAM DA1
* - Atmel | SMART SAM HA1
* \endif
*
* The outline of this documentation is as follows:
* - \ref asfdoc_sam0_adc_prerequisites
* - \ref asfdoc_sam0_adc_module_overview
* - \ref asfdoc_sam0_adc_special_considerations
* - \ref asfdoc_sam0_adc_extra_info
* - \ref asfdoc_sam0_adc_examples
* - \ref asfdoc_sam0_adc_api_overview
*
*
* \section asfdoc_sam0_adc_prerequisites Prerequisites
*
* There are no prerequisites for this module.
*
*
* \section asfdoc_sam0_adc_module_overview Module Overview
*
* This driver provides an interface for the Analog-to-Digital conversion
* functions on the device, to convert analog voltages to a corresponding
* digital value. The ADC has up to 12-bit resolution, and is capable of
* \if DEVICE_SAML21_SUPPORT
* converting up to 1,000,000 samples per second (MSPS).
* \else
* converting up to 500K samples per second (KSPS).
* \endif
*
* The ADC has a compare function for accurate monitoring of user defined
* thresholds with minimum software intervention required.
* The ADC may be configured for 8-, 10-, or 12-bit result, reducing the
* conversion time. ADC conversion results are provided left or right adjusted
* which eases calculation when the result is represented as a signed integer.
*
* The input selection is flexible, and both single-ended and differential
* measurements can be made. For differential measurements, an optional gain
* stage is available to increase the dynamic range. In addition, several
* internal signal inputs are available. The ADC can provide both signed and
* unsigned results.
*
* The ADC measurements can either be started by application software or an
* incoming event from another peripheral in the device, and both internal and
* external reference voltages can be selected.
*
* \note Internal references will be enabled by the driver, but not disabled.
* Any reference not used by the application should be disabled by the application.
*
* A simplified block diagram of the ADC can be seen in
* \ref asfdoc_sam0_adc_module_block_diagram "the figure below".
*
* \anchor asfdoc_sam0_adc_module_block_diagram
* \dot
* digraph overview {
* splines = false;
* rankdir=LR;
*
* mux1 [label="Positive input", shape=box];
* mux2 [label="Negative input", shape=box];
*
*
* mux3 [label="Reference", shape=box];
*
* adc [label="ADC", shape=polygon, sides=5, orientation=90, distortion=-0.6, style=filled, fillcolor=darkolivegreen1, height=1, width=1];
* prescaler [label="PRESCALER", shape=box, style=filled, fillcolor=lightblue];
*
* mux1 -> adc;
* mux2 -> adc;
* mux3 -> adc:sw;
* prescaler -> adc;
*
* postproc [label="Post processing", shape=box];
* result [label="RESULT", shape=box, style=filled, fillcolor=lightblue];
*
* adc:e -> postproc:w;
* postproc:e -> result:w;
*
* {rank=same; mux1 mux2}
* {rank=same; prescaler adc}
*
* }
* \enddot
*
*
* \subsection asfdoc_sam0_adc_module_overview_prescaler Sample Clock Prescaler
* The ADC features a prescaler, which enables conversion at lower clock rates
* than the input Generic Clock to the ADC module. This feature can be used to
* lower the synchronization time of the digital interface to the ADC module
* via a high speed Generic Clock frequency, while still allowing the ADC
* sampling rate to be reduced.
*
* \subsection asfdoc_sam0_adc_module_overview_resolution ADC Resolution
* The ADC supports full 8-, 10-, or 12-bit resolution. Hardware
* oversampling and decimation can be used to increase the
* effective resolution at the expense of throughput. Using oversampling and
* decimation mode the ADC resolution is increased from 12-bit to an effective
* 13-, 14-, 15-, or 16-bit. In these modes the conversion rate is reduced, as
* a greater number of samples is used to achieve the increased resolution. The
* available resolutions and effective conversion rate is listed in
* \ref asfdoc_sam0_adc_module_conversion_rate "the table below".
*
* \anchor asfdoc_sam0_adc_module_conversion_rate
* <table>
* <caption>Effective ADC Conversion Speed Using Oversampling</caption>
* <tr>
* <th>Resolution</th>
* <th>Effective conversion rate</th>
* </tr>
* <tr>
* <td>13-bit</td>
* <td>Conversion rate divided by 4</td>
* </tr>
* <tr>
* <td>14-bit</td>
* <td>Conversion rate divided by 16</td>
* </tr>
* <tr>
* <td>15-bit</td>
* <td>Conversion rate divided by 64</td>
* </tr>
* <tr>
* <td>16-bit</td>
* <td>Conversion rate divided by 256</td>
* </tr>
* </table>
*
* \subsection asfdoc_sam0_adc_module_overview_conversion Conversion Modes
* ADC conversions can be software triggered on demand by the user application,
* if continuous sampling is not required. It is also possible to configure the
* ADC in free running mode, where new conversions are started as soon as the
* previous conversion is completed, or configure the ADC to scan across a
* number of input pins (see \ref asfdoc_sam0_adc_module_overview_pin_scan).
*
* \subsection asfdoc_sam0_adc_module_overview_diff_mode Differential and Single-ended Conversion
* The ADC has two conversion modes; differential and single-ended. When
* measuring signals where the positive input pin is always at a higher voltage
* than the negative input pin, the single-ended conversion mode should be used
* in order to achieve a full 12-bit output resolution.
*
* If however the positive input pin voltage may drop below the negative input
* pin the signed differential mode should be used.
*
* \subsection asfdoc_sam0_adc_module_overview_sample_time Sample Time
* The sample time for each ADC conversion is configurable as a number of half
* prescaled ADC clock cycles (depending on the prescaler value), allowing the
* user application to achieve faster or slower sampling depending on the
* source impedance of the ADC input channels. For applications with high
* impedance inputs the sample time can be increased to give the ADC an adequate
* time to sample and convert the input channel.
*
* The resulting sampling time is given by the following equation:
* \f[
* t_{SAMPLE} = (sample\_length+1) \times \frac{ADC_{CLK}} {2}
* \f]
*
* \subsection asfdoc_sam0_adc_module_overview_averaging Averaging
* The ADC can be configured to trade conversion speed for accuracy by averaging
* multiple samples in hardware. This feature is suitable when operating in
* noisy conditions.
*
* You can specify any number of samples to accumulate (up to 1024) and the
* divide ratio to use (up to divide by 128). To modify these settings the
* ADC_RESOLUTION_CUSTOM needs to be set as the resolution. When this is set
* the number of samples to accumulate and the division ratio can be set by
* the configuration struct members \ref adc_config.accumulate_samples and
* \ref adc_config.divide_result. When using this mode the ADC result register
* will be set to be 16-bit wide to accommodate the larger result sizes
* produced by the accumulator.
*
* The effective ADC conversion rate will be reduced by a factor of the number
* of accumulated samples;
* however, the effective resolution will be increased according to
* \ref asfdoc_sam0_adc_module_hw_av_resolution "the table below".
*
* \anchor asfdoc_sam0_adc_module_hw_av_resolution
* <table>
* <caption>Effective ADC Resolution From Various Hardware Averaging Modes</caption>
* <tr>
* <th>Number of samples</tr>
* <th>Final result</tr>
* </tr>
* <tr>
* <td>1</td>
* <td>12-bit</td>
* </tr>
* <tr>
* <td>2</td>
* <td>13-bit</td>
* </tr>
* <tr>
* <td>4</td>
* <td>14-bit</td>
* </tr>
* <tr>
* <td>8</td>
* <td>15-bit</td>
* </tr>
* <tr>
* <td>16</td>
* <td>16-bit</td>
* </tr>
* <tr>
* <td>32</td>
* <td>16-bit</td>
* </tr>
* <tr>
* <td>64</td>
* <td>16-bit</td>
* </tr>
* <tr>
* <td>128</td>
* <td>16-bit</td>
* </tr>
* <tr>
* <td>256</td>
* <td>16-bit</td>
* </tr>
* <tr>
* <td>512</td>
* <td>16-bit</td>
* </tr>
* <tr>
* <td>1024</td>
* <td>16-bit</td>
* </tr>
* </table>
*
*
* \subsection asfdoc_sam0_adc_module_overview_offset_corr Offset and Gain Correction
* Inherent gain and offset errors affect the absolute accuracy of the ADC.
*
* The offset error is defined as the deviation of the ADC's actual transfer
* function from ideal straight line at zero input voltage.
*
* The gain error is defined as the deviation of the last output step's
* midpoint from the ideal straight line, after compensating for offset error.
*
* The offset correction value is subtracted from the converted data before the
* result is ready. The gain correction value is multiplied with the offset
* corrected value.
*
* The equation for both offset and gain error compensation is shown below:
* \f[
* ADC_{RESULT} = (VALUE_{CONV} + CORR_{OFFSET}) \times CORR_{GAIN}
* \f]
*
* When enabled, a given set of offset and gain correction values can be applied
* to the sampled data in hardware, giving a corrected stream of sample data to
* the user application at the cost of an increased sample latency.
*
* In single conversion, a latency of 13 ADC Generic Clock cycles is added for
* the final sample result availability. As the correction time is always less
* than the propagation delay, in free running mode this latency appears only
* during the first conversion. After the first conversion is complete, future
* conversion results are available at the defined sampling rate.
*
* \subsection asfdoc_sam0_adc_module_overview_pin_scan Pin Scan
* In pin scan mode, the first ADC conversion will begin from the configured
* positive channel, plus the requested starting offset. When the first
* conversion is completed, the next conversion will start at the next positive
* input channel and so on, until all requested pins to scan have been sampled
* and converted.
* SAM L21/L22 has automatic sequences feature instead of pin scan mode. In automatic
* sequence mode, all of 32 positives inputs can be included in a sequence. The
* sequence starts from the lowest input, and go to the next enabled input
* automatically.
*
* Pin scanning gives a simple mechanism to sample a large number of physical
* input channel samples, using a single physical ADC channel.
*
* \subsection asfdoc_sam0_adc_module_overview_window_monitor Window Monitor
* The ADC module window monitor function can be used to automatically compare
* the conversion result against a preconfigured pair of upper and lower
* threshold values.
*
* The threshold values are evaluated differently, depending on whether
* differential or single-ended mode is selected. In differential mode, the
* upper and lower thresholds are evaluated as signed values for the comparison,
* while in single-ended mode the comparisons are made as a set of unsigned
* values.
*
* The significant bits of the lower window monitor threshold and upper window
* monitor threshold values are user-configurable, and follow the overall ADC
* sampling bit precision set when the ADC is configured by the user application.
* For example, only the eight lower bits of the window threshold values will be
* compared to the sampled data whilst the ADC is configured in 8-bit mode.
* In addition, if using differential mode, the 8<SUP>th</SUP> bit will be considered as
* the sign bit even if bit 9 is zero.
*
* \subsection asfdoc_sam0_adc_module_overview_events Events
* Event generation and event actions are configurable in the ADC.
*
* The ADC has two actions that can be triggered upon event reception:
* \li Start conversion
* \li Flush pipeline and start conversion
*
* The ADC can generate two events:
* \li Window monitor
* \li Result ready
*
* If the event actions are enabled in the configuration, any incoming event
* will trigger the action.
*
* If the window monitor event is enabled, an event will be generated
* when the configured window condition is detected.
*
* If the result ready event is enabled, an event will be generated when a
* conversion is completed.
*
* \note The connection of events between modules requires the use of the
* \ref asfdoc_sam0_events_group "SAM Event System Driver (EVENTS)"
* to route output event of one module to the input event of another.
* For more information on event routing, refer to the event driver
* documentation.
*
*
* \section asfdoc_sam0_adc_special_considerations Special Considerations
*
* An integrated analog temperature sensor is available for use with the ADC.
* The bandgap voltage, as well as the scaled I/O and core voltages can also be
* measured by the ADC. For internal ADC inputs, the internal source(s) may need
* to be manually enabled by the user application before they can be measured.
*
*
* \section asfdoc_sam0_adc_extra_info Extra Information
*
* For extra information, see \ref asfdoc_sam0_adc_extra. This includes:
* - \ref asfdoc_sam0_adc_extra_acronyms
* - \ref asfdoc_sam0_adc_extra_dependencies
* - \ref asfdoc_sam0_adc_extra_errata
* - \ref asfdoc_sam0_adc_extra_history
*
*
* \section asfdoc_sam0_adc_examples Examples
*
* For a list of examples related to this driver, see
* \ref asfdoc_sam0_adc_exqsg.
*
*
* \section asfdoc_sam0_adc_api_overview API Overview
* @{
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <compiler.h>
#include <system.h>
#include <adc_feature.h>
/**
* \name Module Status Flags
*
* ADC status flags, returned by \ref adc_get_status() and cleared by
* \ref adc_clear_status().
*
* @{
*/
/** ADC result ready. */
#define ADC_STATUS_RESULT_READY (1UL << 0)
/** Window monitor match. */
#define ADC_STATUS_WINDOW (1UL << 1)
/** ADC result overwritten before read. */
#define ADC_STATUS_OVERRUN (1UL << 2)
/** @} */
#if ADC_CALLBACK_MODE == true
# if (ADC_INST_NUM > 1)
# define _ADC_INTERRUPT_VECT_NUM(n, unused) \
SYSTEM_INTERRUPT_MODULE_ADC##n,
/**
* \internal Get the interrupt vector for the given device instance
*
* \param[in] The ADC module instance number
*
* \return Interrupt vector for of the given ADC module instance.
*/
static enum system_interrupt_vector _adc_interrupt_get_interrupt_vector(
uint32_t inst_num)
{
static uint8_t adc_interrupt_vectors[ADC_INST_NUM] = {
MREPEAT(ADC_INST_NUM, _ADC_INTERRUPT_VECT_NUM, 0)
};
return (enum system_interrupt_vector)adc_interrupt_vectors[inst_num];
}
# endif
#endif
#if !defined(__DOXYGEN__)
uint8_t _adc_get_inst_index(
Adc *const hw);
#endif
/**
* \name Driver Initialization and Configuration
* @{
*/
enum status_code adc_init(
struct adc_module *const module_inst,
Adc *hw,
struct adc_config *config);
void adc_get_config_defaults(
struct adc_config *const config);
#if (SAMD) || (SAMHA1) || (SAMR21)
void adc_regular_ain_channel(
uint32_t *pin_array, uint8_t size);
#endif
/** @} */
/**
* \name Status Management
* @{
*/
/**
* \brief Retrieves the current module status.
*
* Retrieves the status of the module, giving overall state information.
*
* \param[in] module_inst Pointer to the ADC software instance struct
*
* \return Bitmask of \c ADC_STATUS_* flags.
*
* \retval ADC_STATUS_RESULT_READY ADC result is ready to be read
* \retval ADC_STATUS_WINDOW ADC has detected a value inside the set
* window range
* \retval ADC_STATUS_OVERRUN ADC result has overrun
*/
static inline uint32_t adc_get_status(
struct adc_module *const module_inst)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Adc *const adc_module = module_inst->hw;
uint32_t int_flags = adc_module->INTFLAG.reg;
uint32_t status_flags = 0;
/* Check for ADC Result Ready */
if (int_flags & ADC_INTFLAG_RESRDY) {
status_flags |= ADC_STATUS_RESULT_READY;
}
/* Check for ADC Window Match */
if (int_flags & ADC_INTFLAG_WINMON) {
status_flags |= ADC_STATUS_WINDOW;
}
/* Check for ADC Overrun */
if (int_flags & ADC_INTFLAG_OVERRUN) {
status_flags |= ADC_STATUS_OVERRUN;
}
return status_flags;
}
/**
* \brief Clears a module status flag.
*
* Clears the given status flag of the module.
*
* \param[in] module_inst Pointer to the ADC software instance struct
* \param[in] status_flags Bitmask of \c ADC_STATUS_* flags to clear
*/
static inline void adc_clear_status(
struct adc_module *const module_inst,
const uint32_t status_flags)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Adc *const adc_module = module_inst->hw;
uint32_t int_flags = 0;
/* Check for ADC Result Ready */
if (status_flags & ADC_STATUS_RESULT_READY) {
int_flags |= ADC_INTFLAG_RESRDY;
}
/* Check for ADC Window Match */
if (status_flags & ADC_STATUS_WINDOW) {
int_flags |= ADC_INTFLAG_WINMON;
}
/* Check for ADC Overrun */
if (status_flags & ADC_STATUS_OVERRUN) {
int_flags |= ADC_INTFLAG_OVERRUN;
}
/* Clear interrupt flag */
adc_module->INTFLAG.reg = int_flags;
}
/** @} */
/**
* \name Enable, Disable, and Reset ADC Module, Start Conversion and Read Result
* @{
*/
/**
* \brief Enables the ADC module.
*
* Enables an ADC module that has previously been configured. If any internal reference
* is selected it will be enabled.
*
* \param[in] module_inst Pointer to the ADC software instance struct
*/
static inline enum status_code adc_enable(
struct adc_module *const module_inst)
{
Assert(module_inst);
Assert(module_inst->hw);
Adc *const adc_module = module_inst->hw;
while (adc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
#if ADC_CALLBACK_MODE == true
# if (ADC_INST_NUM > 1)
system_interrupt_enable(_adc_interrupt_get_interrupt_vector(
_adc_get_inst_index(adc_module)));
# elif (SAMC20)
system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_ADC0);
# else
system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_ADC);
# endif
#endif
/* Disbale interrupt */
adc_module->INTENCLR.reg = ADC_INTENCLR_MASK;
/* Clear interrupt flag */
adc_module->INTFLAG.reg = ADC_INTFLAG_MASK;
adc_module->CTRLA.reg |= ADC_CTRLA_ENABLE;
while (adc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
return STATUS_OK;
}
/**
* \brief Disables the ADC module.
*
* Disables an ADC module that was previously enabled.
*
* \param[in] module_inst Pointer to the ADC software instance struct
*/
static inline enum status_code adc_disable(
struct adc_module *const module_inst)
{
Assert(module_inst);
Assert(module_inst->hw);
Adc *const adc_module = module_inst->hw;
#if ADC_CALLBACK_MODE == true
# if (ADC_INST_NUM > 1)
system_interrupt_disable(_adc_interrupt_get_interrupt_vector(
_adc_get_inst_index(adc_module)));
# elif (SAMC20)
system_interrupt_disable(SYSTEM_INTERRUPT_MODULE_ADC0);
# else
system_interrupt_disable(SYSTEM_INTERRUPT_MODULE_ADC);
# endif
#endif
while (adc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
/* Disbale interrupt */
adc_module->INTENCLR.reg = ADC_INTENCLR_MASK;
/* Clear interrupt flag */
adc_module->INTFLAG.reg = ADC_INTFLAG_MASK;
adc_module->CTRLA.reg &= ~ADC_CTRLA_ENABLE;
while (adc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
return STATUS_OK;
}
/**
* \brief Resets the ADC module.
*
* Resets an ADC module, clearing all module state, and registers to their
* default values.
*
* \param[in] module_inst Pointer to the ADC software instance struct
*/
static inline enum status_code adc_reset(
struct adc_module *const module_inst)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Adc *const adc_module = module_inst->hw;
/* Disable to make sure the pipeline is flushed before reset */
adc_disable(module_inst);
/* Software reset the module */
adc_module->CTRLA.reg |= ADC_CTRLA_SWRST;
while (adc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
return STATUS_OK;
}
/**
* \brief Enables an ADC event input or output.
*
* Enables one or more input or output events to or from the ADC module. See
* \ref adc_events "Struct adc_events" for a list of events this module supports.
*
* \note Events cannot be altered while the module is enabled.
*
* \param[in] module_inst Software instance for the ADC peripheral
* \param[in] events Struct containing flags of events to enable
*/
static inline void adc_enable_events(
struct adc_module *const module_inst,
struct adc_events *const events)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Assert(events);
Adc *const adc_module = module_inst->hw;
uint32_t event_mask = 0;
/* Configure Window Monitor event */
if (events->generate_event_on_window_monitor) {
event_mask |= ADC_EVCTRL_WINMONEO;
}
/* Configure Result Ready event */
if (events->generate_event_on_conversion_done) {
event_mask |= ADC_EVCTRL_RESRDYEO;
}
adc_module->EVCTRL.reg |= event_mask;
}
/**
* \brief Disables an ADC event input or output.
*
* Disables one or more input or output events to or from the ADC module. See
* \ref adc_events "Struct adc_events" for a list of events this module supports.
*
* \note Events cannot be altered while the module is enabled.
*
* \param[in] module_inst Software instance for the ADC peripheral
* \param[in] events Struct containing flags of events to disable
*/
static inline void adc_disable_events(
struct adc_module *const module_inst,
struct adc_events *const events)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Assert(events);
Adc *const adc_module = module_inst->hw;
uint32_t event_mask = 0;
/* Configure Window Monitor event */
if (events->generate_event_on_window_monitor) {
event_mask |= ADC_EVCTRL_WINMONEO;
}
/* Configure Result Ready event */
if (events->generate_event_on_conversion_done) {
event_mask |= ADC_EVCTRL_RESRDYEO;
}
adc_module->EVCTRL.reg &= ~event_mask;
}
/**
* \brief Starts an ADC conversion.
*
* Starts a new ADC conversion.
*
* \param[in] module_inst Pointer to the ADC software instance struct
*/
static inline void adc_start_conversion(
struct adc_module *const module_inst)
{
Assert(module_inst);
Assert(module_inst->hw);
Adc *const adc_module = module_inst->hw;
while (adc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
adc_module->SWTRIG.reg |= ADC_SWTRIG_START;
while (adc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
}
/**
* \brief Reads the ADC result.
*
* Reads the result from an ADC conversion that was previously started.
*
* \param[in] module_inst Pointer to the ADC software instance struct
* \param[out] result Pointer to store the result value in
*
* \return Status of the ADC read request.
* \retval STATUS_OK The result was retrieved successfully
* \retval STATUS_BUSY A conversion result was not ready
* \retval STATUS_ERR_OVERFLOW The result register has been overwritten by the
* ADC module before the result was read by the software
*/
static inline enum status_code adc_read(
struct adc_module *const module_inst,
uint16_t *result)
{
Assert(module_inst);
Assert(module_inst->hw);
Assert(result);
if (!(adc_get_status(module_inst) & ADC_STATUS_RESULT_READY)) {
/* Result not ready */
return STATUS_BUSY;
}
Adc *const adc_module = module_inst->hw;
#if (SAMD) || (SAMHA1) || (SAMR21)
while (adc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
#endif
/* Get ADC result */
*result = adc_module->RESULT.reg;
/* Reset ready flag */
adc_clear_status(module_inst, ADC_STATUS_RESULT_READY);
if (adc_get_status(module_inst) & ADC_STATUS_OVERRUN) {
adc_clear_status(module_inst, ADC_STATUS_OVERRUN);
return STATUS_ERR_OVERFLOW;
}
return STATUS_OK;
}
/** @} */
/**
* \name Runtime Changes of ADC Module
* @{
*/
/**
* \brief Flushes the ADC pipeline.
*
* Flushes the pipeline and restarts the ADC clock on the next peripheral clock
* edge. All conversions in progress will be lost. When flush is complete, the
* module will resume where it left off.
*
* \param[in] module_inst Pointer to the ADC software instance struct
*/
static inline void adc_flush(
struct adc_module *const module_inst)
{
Assert(module_inst);
Assert(module_inst->hw);
Adc *const adc_module = module_inst->hw;
while (adc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
adc_module->SWTRIG.reg |= ADC_SWTRIG_FLUSH;
while (adc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
}
void adc_set_window_mode(
struct adc_module *const module_inst,
const enum adc_window_mode window_mode,
const int16_t window_lower_value,
const int16_t window_upper_value);
/**
* \brief Sets positive ADC input pin.
*
* Sets the positive ADC input pin selection.
*
* \param[in] module_inst Pointer to the ADC software instance struct
* \param[in] positive_input Positive input pin
*/
static inline void adc_set_positive_input(
struct adc_module *const module_inst,
const enum adc_positive_input positive_input)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Adc *const adc_module = module_inst->hw;
while (adc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
/* Set positive input pin */
adc_module->INPUTCTRL.reg =
(adc_module->INPUTCTRL.reg & ~ADC_INPUTCTRL_MUXPOS_Msk) |
(positive_input);
while (adc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
}
/**
* \brief Sets negative ADC input pin for differential mode.
*
* Sets the negative ADC input pin, when the ADC is configured in differential
* mode.
*
* \param[in] module_inst Pointer to the ADC software instance struct
* \param[in] negative_input Negative input pin
*/
static inline void adc_set_negative_input(
struct adc_module *const module_inst,
const enum adc_negative_input negative_input)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Adc *const adc_module = module_inst->hw;
while (adc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
/* Set negative input pin */
adc_module->INPUTCTRL.reg =
(adc_module->INPUTCTRL.reg & ~ADC_INPUTCTRL_MUXNEG_Msk) |
(negative_input);
while (adc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
}
/** @} */
#if ADC_CALLBACK_MODE == true
/**
* \name Enable and Disable Interrupts
* @{
*/
/**
* \brief Enable interrupt.
*
* Enable the given interrupt request from the ADC module.
*
* \param[in] module_inst Pointer to the ADC software instance struct
* \param[in] interrupt Interrupt to enable
*/
static inline void adc_enable_interrupt(struct adc_module *const module_inst,
enum adc_interrupt_flag interrupt)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Adc *const adc_module = module_inst->hw;
/* Enable interrupt */
adc_module->INTENSET.reg = interrupt;
}
/**
* \brief Disable interrupt.
*
* Disable the given interrupt request from the ADC module.
*
* \param[in] module_inst Pointer to the ADC software instance struct
* \param[in] interrupt Interrupt to disable
*/
static inline void adc_disable_interrupt(struct adc_module *const module_inst,
enum adc_interrupt_flag interrupt)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Adc *const adc_module = module_inst->hw;
/* Enable interrupt */
adc_module->INTENCLR.reg = interrupt;
}
/** @} */
#endif /* ADC_CALLBACK_MODE == true */
#ifdef __cplusplus
}
#endif
/** @} */
/**
* \page asfdoc_sam0_adc_extra Extra Information for ADC Driver
*
* \section asfdoc_sam0_adc_extra_acronyms Acronyms
* Below is a table listing the acronyms used in this module, along with their
* intended meanings.
*
* <table>
* <tr>
* <th>Acronym</th>
* <th>Description</th>
* </tr>
* <tr>
* <td>ADC</td>
* <td>Analog-to-Digital Converter</td>
* </tr>
* <tr>
* <td>DAC</td>
* <td>Digital-to-Analog Converter</td>
* </tr>
* <tr>
* <td>LSB</td>
* <td>Least Significant Bit</td>
* </tr>
* <tr>
* <td>MSB</td>
* <td>Most Significant Bit</td>
* </tr>
* <tr>
* <td>DMA</td>
* <td>Direct Memory Access</td>
* </tr>
* </table>
*
*
* \section asfdoc_sam0_adc_extra_dependencies Dependencies
* This driver has the following dependencies:
*
* - \ref asfdoc_sam0_system_pinmux_group "System Pin Multiplexer Driver"
*
*
* \section asfdoc_sam0_adc_extra_errata Errata
* There are no errata related to this driver.
*
*
* \section asfdoc_sam0_adc_extra_history Module History
* An overview of the module history is presented in the table below, with
* details on the enhancements and fixes made to the module since its first
* release. The current version of this corresponds to the newest version in
* the table.
*
* <table>
* <tr>
* <th>Changelog</th>
* </tr>
* \if DEVICE_SAML21_SUPPORT
* <tr>
* <td>Initial Release</td>
* </tr>
* \else
* <tr>
* <td>Added support for SAM R21</td>
* </tr>
* <tr>
* <td>Added support for SAM D21 and new DMA quick start guide</td>
* </tr>
* <tr>
* <td>Added ADC calibration constant loading from the device signature
* row when the module is initialized</td>
* </tr>
* <tr>
* <td>Initial Release</td>
* </tr>
* \endif
* </table>
*/
/**
* \page asfdoc_sam0_adc_exqsg Examples for ADC Driver
*
* This is a list of the available Quick Start guides (QSGs) and example
* applications for \ref asfdoc_sam0_adc_group. QSGs are simple examples with
* step-by-step instructions to configure and use this driver in a selection of
* use cases. Note that a QSG can be compiled as a standalone application or be
* added to the user application.
*
* - \subpage asfdoc_sam0_adc_basic_use_case
* \if ADC_CALLBACK_MODE
* - \subpage asfdoc_sam0_adc_basic_use_case_callback
* \endif
* - \subpage asfdoc_sam0_adc_dma_use_case
*
* \page asfdoc_sam0_adc_document_revision_history Document Revision History
*
* <table>
* <tr>
* <th>Doc. Rev.</th>
* <th>Date</th>
* <th>Comments</th>
* </tr>
* \if DEVICE_SAML21_SUPPORT
* <tr>
* <td>42451B</td>
* <td>12/2015</td>
* <td>Added support for SAM L22</td>
* </tr>
* <tr>
* <td>42451A</td>
* <td>07/2015</td>
* <td>Initial document release</td>
* </tr>
* \else
* <tr>
* <td>42109E</td>
* <td>12/2015</td>
* <td>Added support for SAM DA1 and SAM D09</td>
* </tr>
* <tr>
* <td>42109D</td>
* <td>12/2014</td>
* <td>Added support for SAM R21 and SAM D10/D11</td>
* </tr>
* <tr>
* <td>42109C</td>
* <td>01/2014</td>
* <td>Added support for SAM D21</td>
* </tr>
* <tr>
* <td>42109B</td>
* <td>06/2013</td>
* <td>Added additional documentation on the event system. Corrected
* documentation typos.</td>
* </tr>
* <tr>
* <td>42109A</td>
* <td>06/2013</td>
* <td>Initial release</td>
* </tr>
* \endif
* </table>
*/
#endif /* ADC_H_INCLUDED */
|