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
1157
|
/**
* \file
*
* \brief SAM Sigma-Delta Analog-to-Digital Converter (SDADC) Driver
*
* Copyright (C) 2015-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 SDADC_H_INCLUDED
#define SDADC_H_INCLUDED
/**
* \defgroup asfdoc_sam0_sdadc_group SAM Sigma-Delta Analog-to-Digital Converter (SDADC) Driver
*
* This driver for Atmel® | SMART ARM®-based microcontrollers provides an interface for the configuration
* and management of the device's SDADC functionality.
*
* The following peripheral is used by this module:
* - SDADC (Sigma-Delta Analog-to-Digital Converter)
*
* The following devices can use this module:
* - Atmel | SMART SAM C21
*
* The outline of this documentation is as follows:
* - \ref asfdoc_sam0_sdadc_prerequisites
* - \ref asfdoc_sam0_sdadc_module_overview
* - \ref asfdoc_sam0_sdadc_special_considerations
* - \ref asfdoc_sam0_sdadc_extra_info
* - \ref asfdoc_sam0_sdadc_examples
* - \ref asfdoc_sam0_sdadc_api_overview
*
*
* \section asfdoc_sam0_sdadc_prerequisites Prerequisites
*
* There are no prerequisites for this module.
*
*
* \section asfdoc_sam0_sdadc_module_overview Module Overview
*
* The Sigma-Delta Analog-to-Digital Converter (SDADC) converts analog signals to
* digital values. The sigma-delta architecture of the SDADC implies a filtering
* and a decimation of the bitstream at the output of the SDADC. The input selection
* is up to three input analog channels.
*
* The SDADC provides up to 16-bit resolution at about 1000 samples per second (1KSPS)
* and sized 24 bits signed result to handle filtering and gain correction without overflow.
* The SDADC measurements can be started by either application software or an incoming
* event from another peripheral in the device.
*
* The conversion is performed on a full range between 0V and the reference voltage.
* Both internal and external reference voltages can be selected. The reference range
* must be set to match the voltage of the reference used. Analog inputs between
* these voltages convert to values based on a linear conversion.
*
*
* \subsection asfdoc_sam0_sdadc_module_overview_clock Sample Clock
* A generic clock (GCLK_SDADC) is required to generate the CLK_SDADC to the SDADC
* module. The SDADC features a prescaler, which enables conversion at lower clock
* rates than the input Generic Clock to the SDADC module.
*
* The SDADC data sampling frequency (CLK_SDADC_FS) in the SDADC module is the
* CLK_SDADC/4, the reduction comes from the phase generator between the prescaler
* and the SDADC.
*
* OSR is the Over Sampling Ratio, which can be modified to change the output data
* rate. The conversion time depends on the selected OSR and the sampling frequency
* of the SDADC.
* The conversion time can be described with:
* \f[
* t_{SAMPLE} = \frac {22 + 3 \times OSR} {CLK \_ SDADC \_ FS}
* \f]
* -# Initialization of the SDADC (22 sigma-delta samples).
* -# Filling of the decimation filter (3*OSR sigma-delta samples).
*
* \subsection asfdoc_sam0_sdadc_module_overview_offset_corr Gain and Offset Correction
* A specific offset, gain, and shift can be applied to each source of the SDADC
* by performing the following operation:
* \f[
* Data = (Data_{0} + OFFSET) \times \frac {GAIN}{2^{SHIFT}}
* \f]
*
*
* \subsection asfdoc_sam0_sdadc_module_overview_window_monitor Window Monitor
* The SDADC module window monitor function can be used to automatically compare
* the conversion result against a predefined pair of upper and lower
* threshold values.
*
*
* \subsection asfdoc_sam0_sdadc_module_overview_events Events
* Event generation and event actions are configurable in the SDADC.
*
* The SDADC has two actions that can be triggered upon event reception:
* \li Start conversion
* \li Conversion flush
*
* The SDADC can generate two kinds of 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.
*
*
* \section asfdoc_sam0_sdadc_special_considerations Special Considerations
*
* There are no special considerations for this module.
*
*
* \section asfdoc_sam0_sdadc_extra_info Extra Information
*
* For extra information see \ref asfdoc_sam0_sdadc_extra. This includes:
* - \ref asfdoc_sam0_sdadc_extra_acronyms
* - \ref asfdoc_sam0_sdadc_extra_dependencies
* - \ref asfdoc_sam0_sdadc_extra_errata
* - \ref asfdoc_sam0_sdadc_extra_history
*
*
* \section asfdoc_sam0_sdadc_examples Examples
*
* For a list of examples related to this driver, see
* \ref asfdoc_sam0_sdadc_exqsg.
*
*
* \section asfdoc_sam0_sdadc_api_overview API Overview
* @{
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <compiler.h>
#include <system.h>
#if SDADC_CALLBACK_MODE == true
# include <system_interrupt.h>
#if !defined(__DOXYGEN__)
extern struct sdadc_module *_sdadc_instances[SDADC_INST_NUM];
#endif
/** Forward definition of the device instance. */
struct sdadc_module;
/** Type of the callback functions. */
typedef void (*sdadc_callback_t)(const struct sdadc_module *const module);
/**
* \brief SDADC Callback enum.
*
* Callback types for SDADC callback driver.
*
*/
enum sdadc_callback {
/** Callback for buffer received */
SDADC_CALLBACK_READ_BUFFER,
/** Callback when window is hit */
SDADC_CALLBACK_WINDOW,
/** Callback for error */
SDADC_CALLBACK_ERROR,
# if !defined(__DOXYGEN__)
/** Number of available callbacks */
SDADC_CALLBACK_N,
# endif
};
#endif
/**
* \name Module Status Flags
*
* SDADC status flags, returned by \ref sdadc_get_status() and cleared by
* \ref sdadc_clear_status().
*
* @{
*/
/** SDADC result ready. */
#define SDADC_STATUS_RESULT_READY (1UL << 0)
/** SDADC result overwritten before read. */
#define SDADC_STATUS_OVERRUN (1UL << 1)
/** Window monitor match. */
#define SDADC_STATUS_WINDOW (1UL << 2)
/** @} */
/**
* \brief SDADC reference voltage enum.
*
* Enum for the possible reference voltages for the SDADC.
*
*/
enum sdadc_reference_select {
/** Internal Bandgap Reference */
SDADC_REFERENCE_INTREF = SDADC_REFCTRL_REFSEL(0),
/** External reference B */
SDADC_REFERENCE_AREFB = SDADC_REFCTRL_REFSEL(1),
/** DACOUT */
SDADC_REFERENCE_DACOUT = SDADC_REFCTRL_REFSEL(2),
/** VDDANA */
SDADC_REFERENCE_INTVCC = SDADC_REFCTRL_REFSEL(3),
};
/**
* \brief SDADC reference range enum.
*
* Enum for the matched voltage range of the SDADC reference used.
*
*/
enum sdadc_reference_range {
/** Vref < 1.4V */
SDADC_REFRANGE_0 = SDADC_REFCTRL_REFRANGE(0),
/** 1.4V < Vref < 2.4V */
SDADC_REFRANGE_1 = SDADC_REFCTRL_REFRANGE(1),
/** 2.4V < Vref < 3.6V */
SDADC_REFRANGE_2 = SDADC_REFCTRL_REFRANGE(2),
/** Vref > 3.6V */
SDADC_REFRANGE_3 = SDADC_REFCTRL_REFRANGE(3),
};
/**
* \brief SDADC over sampling ratio enum.
*
* Enum for the over sampling ratio, which change the output data rate.
*
*/
enum sdadc_over_sampling_ratio {
/** SDADC over Sampling Ratio is 64 */
SDADC_OVER_SAMPLING_RATIO64 = SDADC_CTRLB_OSR(0),
/** SDADC over Sampling Ratio is 128 */
SDADC_OVER_SAMPLING_RATIO128 = SDADC_CTRLB_OSR(1),
/** SDADC over Sampling Ratio is 256 */
SDADC_OVER_SAMPLING_RATIO256 = SDADC_CTRLB_OSR(2),
/** SDADC over Sampling Ratio is 512 */
SDADC_OVER_SAMPLING_RATIO512 = SDADC_CTRLB_OSR(3),
/** SDADC over Sampling Ratio is 1024 */
SDADC_OVER_SAMPLING_RATIO1024 = SDADC_CTRLB_OSR(4),
};
/**
* \brief SDADC window monitor mode enum.
*
* Enum for the possible window monitor modes for the SDADC.
*
*/
enum sdadc_window_mode {
/** No window mode */
SDADC_WINDOW_MODE_DISABLE = SDADC_WINCTRL_WINMODE(0),
/** RESULT > WINLT */
SDADC_WINDOW_MODE_ABOVE = SDADC_WINCTRL_WINMODE(1),
/** RESULT < WINUT */
SDADC_WINDOW_MODE_BELOW = SDADC_WINCTRL_WINMODE(2),
/** WINLT < RESULT < WINUT */
SDADC_WINDOW_MODE_INSIDE = SDADC_WINCTRL_WINMODE(3),
/** !(WINLT < RESULT < WINUT) */
SDADC_WINDOW_MODE_OUTSIDE = SDADC_WINCTRL_WINMODE(4),
};
/**
* \brief SDADC event action enum.
*
* Enum for the possible actions to take on an incoming event.
*
*/
enum sdadc_event_action {
/** Event action disabled */
SDADC_EVENT_ACTION_DISABLED = 0,
/** Flush SDADC and start conversion */
SDADC_EVENT_ACTION_FLUSH_START_CONV = SDADC_EVCTRL_FLUSHEI,
/** Start conversion */
SDADC_EVENT_ACTION_START_CONV = SDADC_EVCTRL_STARTEI,
};
/**
* \brief SDADC MUX input selection enum.
*
* Enum for the possible MUX input selections for the SDADC.
*
*/
enum sdadc_mux_input {
/** Select SDADC AINN0 and AINP0 pins */
SDADC_MUX_INPUT_AIN0 = SDADC_INPUTCTRL_MUXSEL_AIN0,
/** Select SDADC AINN1 and AINP1 pins */
SDADC_MUX_INPUT_AIN1 = SDADC_INPUTCTRL_MUXSEL_AIN1,
/** Select SDADC AINN2 and AINP2 pins */
SDADC_MUX_INPUT_AIN2 = SDADC_INPUTCTRL_MUXSEL_AIN2,
};
#if SDADC_CALLBACK_MODE == true
/**
* Enum for the possible SDADC interrupt flags.
*/
enum sdadc_interrupt_flag {
/** SDADC result ready */
SDADC_INTERRUPT_RESULT_READY = SDADC_INTFLAG_RESRDY,
/** SDADC result overwritten before read */
SDADC_INTERRUPT_OVERRUN = SDADC_INTFLAG_OVERRUN,
/** Window monitor match */
SDADC_INTERRUPT_WINDOW = SDADC_INTFLAG_WINMON,
};
#endif
/**
* \brief Reference configuration structure.
*
* Reference configuration structure.
*/
struct sdadc_reference {
/** Reference voltage selection */
enum sdadc_reference_select ref_sel;
/** Reference voltage range */
enum sdadc_reference_select ref_range;
/** Reference buffer turning switch */
bool on_ref_buffer;
};
/**
* \brief Window monitor configuration structure.
*
* Window monitor configuration structure.
*/
struct sdadc_window_config {
/** Selected window mode */
enum sdadc_window_mode window_mode;
/** Lower window value */
int32_t window_lower_value;
/** Upper window value */
int32_t window_upper_value;
};
/**
* \brief SDADC event enable/disable structure.
*
* Event flags for the SDADC module. This is used to enable and
* disable events via \ref sdadc_enable_events() and \ref sdadc_disable_events().
*/
struct sdadc_events {
/** Enable event generation on conversion done */
bool generate_event_on_conversion_done;
/** Enable event generation on window monitor */
bool generate_event_on_window_monitor;
};
/**
* \brief SDADC correction configuration structure.
*
* Offset, gain, and shift correction configuration structure.
* Part of the \ref sdadc_config struct will be initialized by
* \ref sdadc_get_config_defaults.
*/
struct sdadc_correction_config {
/** Offset correction */
int32_t offset_correction;
/** Gain correction */
uint16_t gain_correction;
/** Shift correction */
uint8_t shift_correction;
};
/**
* \brief SDADC configuration structure.
*
* Configuration structure for an SDADC instance. This structure should be
* initialized by the \ref sdadc_get_config_defaults()
* function before being modified by the user application.
*/
struct sdadc_config {
/** GCLK generator used to clock the peripheral */
enum gclk_generator clock_source;
/** Voltage reference */
struct sdadc_reference reference;
/** Over sampling ratio */
enum sdadc_over_sampling_ratio osr;
/** Clock prescaler */
uint8_t clock_prescaler;
/** Skip Count */
uint8_t skip_count;
/** MUX input */
enum sdadc_mux_input mux_input;
/** Enables free running mode if true */
bool freerunning;
/** Enables SDADC in standby sleep mode if true */
bool run_in_standby;
/** Enables SDADC depend on other peripheral if true */
bool on_command;
/** Enables positive input in the sequence if true */
bool seq_enable[3];
/** Window monitor configuration structure */
struct sdadc_window_config window;
/** Gain and offset correction configuration structure */
struct sdadc_correction_config correction;
/** Event action to take on incoming event */
enum sdadc_event_action event_action;
};
/**
* \brief SDADC software device instance structure.
*
* SDADC software instance structure, used to retain software state information
* of an associated hardware module instance.
*
* \note The fields of this structure should not be altered by the user
* application; they are reserved for module-internal use only.
*/
struct sdadc_module {
#if !defined(__DOXYGEN__)
/** Pointer to SDADC hardware module */
Sdadc *hw;
/** Keep reference configuration so we know when enable is called */
struct sdadc_reference reference;
# if SDADC_CALLBACK_MODE == true
/** Array to store callback functions */
sdadc_callback_t callback[SDADC_CALLBACK_N];
/** Pointer to buffer used for SDADC results */
volatile int32_t *job_buffer;
/** Remaining number of conversions in current job */
volatile uint16_t remaining_conversions;
/** Bit mask for callbacks registered */
uint8_t registered_callback_mask;
/** Bit mask for callbacks enabled */
uint8_t enabled_callback_mask;
/** Holds the status of the ongoing or last conversion job */
volatile enum status_code job_status;
/** If software triggering is needed */
bool software_trigger;
# endif
#endif
};
/**
* \name Driver Initialization and Configuration
* @{
*/
enum status_code sdadc_init(
struct sdadc_module *const module_inst,
Sdadc *hw,
struct sdadc_config *config);
/**
* \brief Initializes an SDADC configuration structure to defaults.
*
* Initializes a given SDADC configuration struct to a set of known default
* values. This function should be called on any new instance of the
* configuration struct before being modified by the user application.
*
* The default configuration is as follows:
* \li GCLK generator 0 (GCLK main) clock source
* \li Positive reference 1
* \li Div 2 clock prescaler
* \li Over Sampling Ratio is 64
* \li Skip 0 samples
* \li MUX input on SDADC AIN1
* \li All events (input and generation) disabled
* \li Free running disabled
* \li Run in standby disabled
* \li On command disabled
* \li Disable all positive input in sequence
* \li Window monitor disabled
* \li No gain/offset/shift correction
*
* \param[out] config Pointer to configuration struct to initialize to
* default values
*/
static inline void sdadc_get_config_defaults(struct sdadc_config *const config)
{
Assert(config);
config->clock_source = GCLK_GENERATOR_0;
config->reference.ref_sel = SDADC_REFERENCE_INTREF;
config->reference.ref_range = SDADC_REFRANGE_0;
config->reference.on_ref_buffer = false;
config->clock_prescaler = 2;
config->osr = SDADC_OVER_SAMPLING_RATIO64;
config->skip_count = 2;
config->mux_input = SDADC_MUX_INPUT_AIN1;
config->event_action = SDADC_EVENT_ACTION_DISABLED;
config->freerunning = false;
config->run_in_standby = false;
config->on_command = false;
config->seq_enable[0] = false;
config->seq_enable[1] = false;
config->seq_enable[2] = false;
config->window.window_mode = SDADC_WINDOW_MODE_DISABLE;
config->window.window_upper_value = 0;
config->window.window_lower_value = 0;
config->correction.gain_correction = 1;
config->correction.offset_correction = SDADC_OFFSETCORR_RESETVALUE;
config->correction.shift_correction = SDADC_SHIFTCORR_RESETVALUE;
}
/** @} */
/**
* \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 SDADC software instance struct
*
* \return Bitmask of \c SDADC_STATUS_* flags.
*
* \retval SDADC_STATUS_RESULT_READY SDADC result is ready to be read
* \retval SDADC_STATUS_WINDOW SDADC has detected a value inside the set
* window range
* \retval SDADC_STATUS_OVERRUN SDADC result has overrun
*/
static inline uint32_t sdadc_get_status(
struct sdadc_module *const module_inst)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Sdadc *const sdadc_module = module_inst->hw;
uint32_t int_flags = sdadc_module->INTFLAG.reg;
uint32_t status_flags = 0;
/* Check for SDADC Result Ready */
if (int_flags & SDADC_INTFLAG_RESRDY) {
status_flags |= SDADC_STATUS_RESULT_READY;
}
/* Check for SDADC Window Match */
if (int_flags & SDADC_INTFLAG_WINMON) {
status_flags |= SDADC_STATUS_WINDOW;
}
/* Check for SDADC Overrun */
if (int_flags & SDADC_INTFLAG_OVERRUN) {
status_flags |= SDADC_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 SDADC software instance struct
* \param[in] status_flags Bitmask of \c SDADC_STATUS_* flags to clear
*/
static inline void sdadc_clear_status(
struct sdadc_module *const module_inst,
const uint32_t status_flags)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Sdadc *const sdadc_module = module_inst->hw;
uint32_t int_flags = 0;
/* Check for SDADC Result Ready */
if (status_flags & SDADC_STATUS_RESULT_READY) {
int_flags |= SDADC_INTFLAG_RESRDY;
}
/* Check for SDADC Window Match */
if (status_flags & SDADC_STATUS_WINDOW) {
int_flags |= SDADC_INTFLAG_WINMON;
}
/* Check for SDADC Overrun */
if (status_flags & SDADC_STATUS_OVERRUN) {
int_flags |= SDADC_INTFLAG_OVERRUN;
}
/* Clear interrupt flag */
sdadc_module->INTFLAG.reg = int_flags;
}
/**
* \brief Get a module sequence flag.
*
* Get the given status flag of the module.
*
* \param[in] module_inst Pointer to the SDADC software instance struct
* \param[out] seq_state Identifies the last conversion done in the sequence
*
* \return Status of the SDADC sequence conversion.
*
* \retval true When the sequence start
* \retval false When the last conversion in a sequence is done
*/
static inline bool sdadc_get_sequence_status(
struct sdadc_module *const module_inst,
uint8_t* seq_state)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Sdadc *const sdadc_module = module_inst->hw;
/* Get SDADC result */
*seq_state = sdadc_module->SEQSTATUS.reg & SDADC_SEQSTATUS_SEQSTATE_Msk;
return ((sdadc_module->SEQSTATUS.bit.SEQBUSY == 1) ? true : false);
}
/** @} */
/**
* \name Enable, Disable, and Reset SDADC Module, Start Conversion and Read Result
* @{
*/
/**
* \brief Determines if the hardware module(s) are currently synchronizing to the bus.
*
* Checks to see if the underlying hardware peripheral module(s) are currently
* synchronizing across multiple clock domains to the hardware bus. This
* function can be used to delay further operations on a module until such time
* that it is ready, to prevent blocking delays for synchronization in the
* user application.
*
* \param[in] module_inst Pointer to the SDADC software instance struct
*
* \return Synchronization status of the underlying hardware module(s).
*
* \retval true If the module synchronization is ongoing
* \retval false If the module has completed synchronization
*/
static inline bool sdadc_is_syncing(
struct sdadc_module *const module_inst)
{
/* Sanity check arguments */
Assert(module_inst);
Sdadc *const sdadc_module = module_inst->hw;
if (sdadc_module->SYNCBUSY.reg) {
return true;
}
return false;
}
/**
* \brief Enables the SDADC module.
*
* Enables an SDADC module that has previously been configured. If any internal reference
* is selected it will be enabled.
*
* \param[in] module_inst Pointer to the SDADC software instance struct
*/
static inline enum status_code sdadc_enable(
struct sdadc_module *const module_inst)
{
Assert(module_inst);
Assert(module_inst->hw);
Sdadc *const sdadc_module = module_inst->hw;
while (sdadc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
#if SDADC_CALLBACK_MODE == true
system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_SDADC);
#endif
sdadc_module->CTRLA.reg |= SDADC_CTRLA_ENABLE;
return STATUS_OK;
}
/**
* \brief Disables the SDADC module.
*
* Disables an SDADC module that was previously enabled.
*
* \param[in] module_inst Pointer to the SDADC software instance struct
*/
static inline enum status_code sdadc_disable(
struct sdadc_module *const module_inst)
{
Assert(module_inst);
Assert(module_inst->hw);
Sdadc *const sdadc_module = module_inst->hw;
#if SDADC_CALLBACK_MODE == true
system_interrupt_disable(SYSTEM_INTERRUPT_MODULE_SDADC);
#endif
while (sdadc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
/* Disbale interrupt */
sdadc_module->INTENCLR.reg = SDADC_INTENCLR_MASK;
/* Clear interrupt flag */
sdadc_module->INTFLAG.reg = SDADC_INTFLAG_MASK;
sdadc_module->CTRLA.reg &= ~SDADC_CTRLA_ENABLE;
return STATUS_OK;
}
/**
* \brief Resets the SDADC module.
*
* Resets an SDADC module, clearing all module state, and registers to their
* default values.
*
* \param[in] module_inst Pointer to the SDADC software instance struct
*/
static inline enum status_code sdadc_reset(
struct sdadc_module *const module_inst)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Sdadc *const sdadc_module = module_inst->hw;
/* Disable to make sure the pipeline is flushed before reset */
sdadc_disable(module_inst);
while (sdadc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
/* Software reset the module */
sdadc_module->CTRLA.reg |= SDADC_CTRLA_SWRST;
return STATUS_OK;
}
/**
* \brief Enables an SDADC event input or output.
*
* Enables one or more input or output events to or from the SDADC module. See
* \ref sdadc_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 SDADC peripheral
* \param[in] events Struct containing flags of events to enable
*/
static inline void sdadc_enable_events(
struct sdadc_module *const module_inst,
struct sdadc_events *const events)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Assert(events);
Sdadc *const sdadc_module = module_inst->hw;
uint32_t event_mask = 0;
/* Configure Window Monitor event */
if (events->generate_event_on_window_monitor) {
event_mask |= SDADC_EVCTRL_WINMONEO;
}
/* Configure Result Ready event */
if (events->generate_event_on_conversion_done) {
event_mask |= SDADC_EVCTRL_RESRDYEO;
}
sdadc_module->EVCTRL.reg |= event_mask;
}
/**
* \brief Disables an SDADC event input or output.
*
* Disables one or more input or output events to or from the SDADC module. See
* \ref sdadc_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 SDADC peripheral
* \param[in] events Struct containing flags of events to disable
*/
static inline void sdadc_disable_events(
struct sdadc_module *const module_inst,
struct sdadc_events *const events)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Assert(events);
Sdadc *const sdadc_module = module_inst->hw;
uint32_t event_mask = 0;
/* Configure Window Monitor event */
if (events->generate_event_on_window_monitor) {
event_mask |= SDADC_EVCTRL_WINMONEO;
}
/* Configure Result Ready event */
if (events->generate_event_on_conversion_done) {
event_mask |= SDADC_EVCTRL_RESRDYEO;
}
sdadc_module->EVCTRL.reg &= ~event_mask;
}
/**
* \brief Starts an SDADC conversion.
*
* Starts a new SDADC conversion.
*
* \param[in] module_inst Pointer to the SDADC software instance struct
*/
static inline void sdadc_start_conversion(
struct sdadc_module *const module_inst)
{
Assert(module_inst);
Assert(module_inst->hw);
Sdadc *const sdadc_module = module_inst->hw;
while (sdadc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
sdadc_module->SWTRIG.reg |= SDADC_SWTRIG_START;
}
/**
* \brief Reads the SDADC result.
*
* Reads the result from an SDADC conversion that was previously started.
*
* \param[in] module_inst Pointer to the SDADC software instance struct
* \param[out] result Pointer to store the result value in
*
* \return Status of the SDADC 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
* SDADC module before the result was read by the software
*/
static inline enum status_code sdadc_read(
struct sdadc_module *const module_inst,
int32_t *result)
{
Assert(module_inst);
Assert(module_inst->hw);
Assert(result);
if (!(sdadc_get_status(module_inst) & SDADC_STATUS_RESULT_READY)) {
/* Result not ready */
return STATUS_BUSY;
}
Sdadc *const sdadc_module = module_inst->hw;
/* Get SDADC result */
*result = ((int32_t)(sdadc_module->RESULT.reg << 8)) >> 8;
/* Reset ready flag */
sdadc_clear_status(module_inst, SDADC_STATUS_RESULT_READY);
if (sdadc_get_status(module_inst) & SDADC_STATUS_OVERRUN) {
sdadc_clear_status(module_inst, SDADC_STATUS_OVERRUN);
return STATUS_ERR_OVERFLOW;
}
return STATUS_OK;
}
/** @} */
/**
* \name Runtime Changes of SDADC Module
* @{
*/
/**
* \brief Flushes the SDADC pipeline.
*
* Flushes the pipeline and restart the SDADC 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 SDADC software instance struct
*/
static inline void sdadc_flush(
struct sdadc_module *const module_inst)
{
Assert(module_inst);
Assert(module_inst->hw);
Sdadc *const sdadc_module = module_inst->hw;
while (sdadc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
sdadc_module->SWTRIG.reg |= SDADC_SWTRIG_FLUSH;
}
/**
* \brief Sets the SDADC window mode.
*
* Sets the SDADC window mode to a given mode and value range.
*
* \param[in] module_inst Pointer to the SDADC software instance struct
* \param[in] window_mode Window monitor mode to set
* \param[in] window_lower_value Lower window monitor threshold value
* \param[in] window_upper_value Upper window monitor threshold value
*/
static inline void sdadc_set_window_mode(
struct sdadc_module *const module_inst,
const enum sdadc_window_mode window_mode,
const int16_t window_lower_value,
const int16_t window_upper_value)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Sdadc *const sdadc_module = module_inst->hw;
while (sdadc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
/* Set window mode */
sdadc_module->WINCTRL.reg = window_mode << SDADC_WINCTRL_WINMODE_Pos;
while (sdadc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
/* Set lower window monitor threshold value */
sdadc_module->WINLT.reg = window_lower_value << SDADC_WINLT_WINLT_Pos;
while (sdadc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
/* Set upper window monitor threshold value */
sdadc_module->WINUT.reg = window_upper_value << SDADC_WINUT_WINUT_Pos;
}
/**
* \brief Sets MUX SDADC input pin.
*
* Sets the MUX SDADC input pin selection.
*
* \param[in] module_inst Pointer to the SDADC software instance struct
* \param[in] mux_input MUX input pin
*/
static inline void sdadc_set_mux_input(
struct sdadc_module *const module_inst,
const enum sdadc_mux_input mux_input)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Sdadc *const sdadc_module = module_inst->hw;
while (sdadc_is_syncing(module_inst)) {
/* Wait for synchronization */
}
/* Set mux input pin */
sdadc_module->INPUTCTRL.reg =
(sdadc_module->INPUTCTRL.reg & ~SDADC_INPUTCTRL_MUXSEL_Msk) | mux_input;
}
/** @} */
#if SDADC_CALLBACK_MODE == true
/**
* \name Enable and Disable Interrupts
* @{
*/
/**
* \brief Enable interrupt.
*
* Enable the given interrupt request from the SDADC module.
*
* \param[in] module_inst Pointer to the SDADC software instance struct
* \param[in] interrupt Interrupt to enable
*/
static inline void sdadc_enable_interrupt(struct sdadc_module *const module_inst,
enum sdadc_interrupt_flag interrupt)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Sdadc *const sdadc_module = module_inst->hw;
/* Enable interrupt */
sdadc_module->INTENSET.reg = interrupt;
}
/**
* \brief Disable interrupt.
*
* Disable the given interrupt request from the SDADC module.
*
* \param[in] module_inst Pointer to the SDADC software instance struct
* \param[in] interrupt Interrupt to disable
*/
static inline void sdadc_disable_interrupt(struct sdadc_module *const module_inst,
enum sdadc_interrupt_flag interrupt)
{
/* Sanity check arguments */
Assert(module_inst);
Assert(module_inst->hw);
Sdadc *const sdadc_module = module_inst->hw;
/* Enable interrupt */
sdadc_module->INTENCLR.reg = interrupt;
}
/** @} */
#endif /* SDADC_CALLBACK_MODE == true */
#ifdef __cplusplus
}
#endif
/** @} */
/**
* \page asfdoc_sam0_sdadc_extra Extra Information for SDADC Driver
*
* \section asfdoc_sam0_sdadc_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>SDADC</td>
* <td>Sigma-Delta Analog-to-Digital Converter</td>
* </tr>
* <tr>
* <td>OSR</td>
* <td>Over Sampling Ratio</td>
* </tr>
* </table>
*
*
* \section asfdoc_sam0_sdadc_extra_dependencies Dependencies
* This driver has no dependencies.
*
*
* \section asfdoc_sam0_sdadc_extra_errata Errata
* There are no errata related to this driver.
*
*
* \section asfdoc_sam0_sdadc_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>
* <tr>
* <td>Initial Release</td>
* </tr>
* </table>
*/
/**
* \page asfdoc_sam0_sdadc_exqsg Examples for SDADC Driver
*
* This is a list of the available Quick Start guides (QSGs) and example
* applications for \ref asfdoc_sam0_sdadc_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_sdadc_basic_use_case
* \if SDADC_CALLBACK_MODE
* - \subpage asfdoc_sam0_sdadc_basic_use_case_callback
* \endif
*
* \page asfdoc_sam0_sdadc_document_revision_history Document Revision History
*
* <table>
* <tr>
* <th>Doc. Rev.</td>
* <th>Date</td>
* <th>Comments</td>
* </tr>
* <tr>
* <td>42496A</td>
* <td>09/2015</td>
* <td>Initial document release</td>
* </tr>
* </table>
*/
#endif /* SDADC_H_INCLUDED */
|