mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Jul 17 09:15:10 2015 +0100
Revision:
592:a274ee790e56
Parent:
579:53297373a894
Synchronized with git revision e7144f83a8d75df80c4877936b6ffe552b0be9e6

Full URL: https://github.com/mbedmicro/mbed/commit/e7144f83a8d75df80c4877936b6ffe552b0be9e6/

More API implementation for SAMR21

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 579:53297373a894 1 #ifndef ADC_H_INCLUDED
mbed_official 579:53297373a894 2 #define ADC_H_INCLUDED
mbed_official 579:53297373a894 3
mbed_official 579:53297373a894 4 /**
mbed_official 579:53297373a894 5 * \defgroup asfdoc_sam0_adc_group SAM Analog to Digital Converter Driver (ADC)
mbed_official 579:53297373a894 6 *
mbed_official 579:53297373a894 7 * This driver for Atmel庐 | SMART SAM devices provides an interface for the configuration
mbed_official 579:53297373a894 8 * and management of the device's Analog to Digital Converter functionality, for
mbed_official 579:53297373a894 9 * the conversion of analog voltages into a corresponding digital form.
mbed_official 579:53297373a894 10 * The following driver API modes are covered by this manual:
mbed_official 579:53297373a894 11 * - Polled APIs
mbed_official 579:53297373a894 12 * \if ADC_CALLBACK_MODE
mbed_official 579:53297373a894 13 * - Callback APIs
mbed_official 579:53297373a894 14 * \endif
mbed_official 579:53297373a894 15 *
mbed_official 579:53297373a894 16 * The following peripherals are used by this module:
mbed_official 579:53297373a894 17 * - ADC (Analog to Digital Converter)
mbed_official 579:53297373a894 18 *
mbed_official 579:53297373a894 19 * The following devices can use this module:
mbed_official 579:53297373a894 20 * \if DEVICE_SAML21_SUPPORT
mbed_official 579:53297373a894 21 * - Atmel | SMART SAM L21
mbed_official 579:53297373a894 22 * \else
mbed_official 579:53297373a894 23 * - Atmel | SMART SAM D20/D21
mbed_official 579:53297373a894 24 * - Atmel | SMART SAM R21
mbed_official 579:53297373a894 25 * - Atmel | SMART SAM D10/D11
mbed_official 579:53297373a894 26 * \endif
mbed_official 579:53297373a894 27 *
mbed_official 579:53297373a894 28 * The outline of this documentation is as follows:
mbed_official 579:53297373a894 29 * - \ref asfdoc_sam0_adc_prerequisites
mbed_official 579:53297373a894 30 * - \ref asfdoc_sam0_adc_module_overview
mbed_official 579:53297373a894 31 * - \ref asfdoc_sam0_adc_special_considerations
mbed_official 579:53297373a894 32 * - \ref asfdoc_sam0_adc_extra_info
mbed_official 579:53297373a894 33 * - \ref asfdoc_sam0_adc_examples
mbed_official 579:53297373a894 34 * - \ref asfdoc_sam0_adc_api_overview
mbed_official 579:53297373a894 35 *
mbed_official 579:53297373a894 36 *
mbed_official 579:53297373a894 37 * \section asfdoc_sam0_adc_prerequisites Prerequisites
mbed_official 579:53297373a894 38 *
mbed_official 579:53297373a894 39 * There are no prerequisites for this module.
mbed_official 579:53297373a894 40 *
mbed_official 579:53297373a894 41 *
mbed_official 579:53297373a894 42 * \section asfdoc_sam0_adc_module_overview Module Overview
mbed_official 579:53297373a894 43 *
mbed_official 579:53297373a894 44 * This driver provides an interface for the Analog-to-Digital conversion
mbed_official 579:53297373a894 45 * functions on the device, to convert analog voltages to a corresponding
mbed_official 579:53297373a894 46 * digital value. The ADC has up to 12-bit resolution, and is capable of
mbed_official 579:53297373a894 47 * \if DEVICE_SAML21_SUPPORT
mbed_official 579:53297373a894 48 * converting up to 1,000,000 samples per second (1 Msps).
mbed_official 579:53297373a894 49 * \else
mbed_official 579:53297373a894 50 * converting up to 500K samples per second (Ksps).
mbed_official 579:53297373a894 51 * \endif
mbed_official 579:53297373a894 52 *
mbed_official 579:53297373a894 53 * The ADC has a compare function for accurate monitoring of user defined
mbed_official 579:53297373a894 54 * thresholds with minimum software intervention required.
mbed_official 579:53297373a894 55 * The ADC may be configured for 8-, 10-, or 12-bit result, reducing the
mbed_official 579:53297373a894 56 * conversion time. ADC conversion results are provided left or right adjusted
mbed_official 579:53297373a894 57 * which eases calculation when the result is represented as a signed integer.
mbed_official 579:53297373a894 58 *
mbed_official 579:53297373a894 59 * The input selection is flexible, and both single-ended and differential
mbed_official 579:53297373a894 60 * measurements can be made. For differential measurements, an optional gain
mbed_official 579:53297373a894 61 * stage is available to increase the dynamic range. In addition, several
mbed_official 579:53297373a894 62 * internal signal inputs are available. The ADC can provide both signed and
mbed_official 579:53297373a894 63 * unsigned results.
mbed_official 579:53297373a894 64 *
mbed_official 579:53297373a894 65 * The ADC measurements can either be started by application software or an
mbed_official 579:53297373a894 66 * incoming event from another peripheral in the device, and both internal and
mbed_official 579:53297373a894 67 * external reference voltages can be selected.
mbed_official 579:53297373a894 68 *
mbed_official 579:53297373a894 69 * \note Internal references will be enabled by the driver, but not disabled.
mbed_official 579:53297373a894 70 * Any reference not used by the application should be disabled by the application.
mbed_official 579:53297373a894 71 *
mbed_official 579:53297373a894 72 * A simplified block diagram of the ADC can be seen in
mbed_official 579:53297373a894 73 * \ref asfdoc_sam0_adc_module_block_diagram "the figure below".
mbed_official 579:53297373a894 74 *
mbed_official 579:53297373a894 75 * \anchor asfdoc_sam0_adc_module_block_diagram
mbed_official 579:53297373a894 76 * \dot
mbed_official 579:53297373a894 77 * digraph overview {
mbed_official 579:53297373a894 78 * splines = false;
mbed_official 579:53297373a894 79 * rankdir=LR;
mbed_official 579:53297373a894 80 *
mbed_official 579:53297373a894 81 * mux1 [label="Positive input", shape=box];
mbed_official 579:53297373a894 82 * mux2 [label="Negative input", shape=box];
mbed_official 579:53297373a894 83 *
mbed_official 579:53297373a894 84 *
mbed_official 579:53297373a894 85 * mux3 [label="Reference", shape=box];
mbed_official 579:53297373a894 86 *
mbed_official 579:53297373a894 87 * adc [label="ADC", shape=polygon, sides=5, orientation=90, distortion=-0.6, style=filled, fillcolor=darkolivegreen1, height=1, width=1];
mbed_official 579:53297373a894 88 * prescaler [label="PRESCALER", shape=box, style=filled, fillcolor=lightblue];
mbed_official 579:53297373a894 89 *
mbed_official 579:53297373a894 90 * mux1 -> adc;
mbed_official 579:53297373a894 91 * mux2 -> adc;
mbed_official 579:53297373a894 92 * mux3 -> adc:sw;
mbed_official 579:53297373a894 93 * prescaler -> adc;
mbed_official 579:53297373a894 94 *
mbed_official 579:53297373a894 95 * postproc [label="Post processing", shape=box];
mbed_official 579:53297373a894 96 * result [label="RESULT", shape=box, style=filled, fillcolor=lightblue];
mbed_official 579:53297373a894 97 *
mbed_official 579:53297373a894 98 * adc:e -> postproc:w;
mbed_official 579:53297373a894 99 * postproc:e -> result:w;
mbed_official 579:53297373a894 100 *
mbed_official 579:53297373a894 101 * {rank=same; mux1 mux2}
mbed_official 579:53297373a894 102 * {rank=same; prescaler adc}
mbed_official 579:53297373a894 103 *
mbed_official 579:53297373a894 104 * }
mbed_official 579:53297373a894 105 * \enddot
mbed_official 579:53297373a894 106 *
mbed_official 579:53297373a894 107 *
mbed_official 579:53297373a894 108 * \subsection asfdoc_sam0_adc_module_overview_prescaler Sample Clock Prescaler
mbed_official 579:53297373a894 109 * The ADC features a prescaler, which enables conversion at lower clock rates
mbed_official 579:53297373a894 110 * than the input Generic Clock to the ADC module. This feature can be used to
mbed_official 579:53297373a894 111 * lower the synchronization time of the digital interface to the ADC module
mbed_official 579:53297373a894 112 * via a high speed Generic Clock frequency, while still allowing the ADC
mbed_official 579:53297373a894 113 * sampling rate to be reduced.
mbed_official 579:53297373a894 114 *
mbed_official 579:53297373a894 115 * \subsection asfdoc_sam0_adc_module_overview_resolution ADC Resolution
mbed_official 579:53297373a894 116 * The ADC supports full 8-bit, 10-bit, or 12-bit resolution. Hardware
mbed_official 579:53297373a894 117 * oversampling and decimation can be used to increase the
mbed_official 579:53297373a894 118 * effective resolution at the expense of throughput. Using oversampling and
mbed_official 579:53297373a894 119 * decimation mode the ADC resolution is increased from 12-bit to an effective
mbed_official 579:53297373a894 120 * 13-, 14-, 15-, or 16-bit. In these modes the conversion rate is reduced, as
mbed_official 579:53297373a894 121 * a greater number of samples is used to achieve the increased resolution. The
mbed_official 579:53297373a894 122 * available resolutions and effective conversion rate is listed in
mbed_official 579:53297373a894 123 * \ref asfdoc_sam0_adc_module_conversion_rate "the table below".
mbed_official 579:53297373a894 124 *
mbed_official 579:53297373a894 125 * \anchor asfdoc_sam0_adc_module_conversion_rate
mbed_official 579:53297373a894 126 * <table>
mbed_official 579:53297373a894 127 * <caption>Effective ADC Conversion Speed Using Oversampling</caption>
mbed_official 579:53297373a894 128 * <tr>
mbed_official 579:53297373a894 129 * <th>Resolution</th>
mbed_official 579:53297373a894 130 * <th>Effective conversion rate</th>
mbed_official 579:53297373a894 131 * </tr>
mbed_official 579:53297373a894 132 * <tr>
mbed_official 579:53297373a894 133 * <td>13-bit</td>
mbed_official 579:53297373a894 134 * <td>Conversion rate divided by 4</td>
mbed_official 579:53297373a894 135 * </tr>
mbed_official 579:53297373a894 136 * <tr>
mbed_official 579:53297373a894 137 * <td>14-bit</td>
mbed_official 579:53297373a894 138 * <td>Conversion rate divided by 16</td>
mbed_official 579:53297373a894 139 * </tr>
mbed_official 579:53297373a894 140 * <tr>
mbed_official 579:53297373a894 141 * <td>15-bit</td>
mbed_official 579:53297373a894 142 * <td>Conversion rate divided by 64</td>
mbed_official 579:53297373a894 143 * </tr>
mbed_official 579:53297373a894 144 * <tr>
mbed_official 579:53297373a894 145 * <td>16-bit</td>
mbed_official 579:53297373a894 146 * <td>Conversion rate divided by 256</td>
mbed_official 579:53297373a894 147 * </tr>
mbed_official 579:53297373a894 148 * </table>
mbed_official 579:53297373a894 149 *
mbed_official 579:53297373a894 150 * \subsection asfdoc_sam0_adc_module_overview_conversion Conversion Modes
mbed_official 579:53297373a894 151 * ADC conversions can be software triggered on demand by the user application,
mbed_official 579:53297373a894 152 * if continuous sampling is not required. It is also possible to configure the
mbed_official 579:53297373a894 153 * ADC in free-running mode, where new conversions are started as soon as the
mbed_official 579:53297373a894 154 * previous conversion is completed, or configure the ADC to scan across a
mbed_official 579:53297373a894 155 * number of input pins (see \ref asfdoc_sam0_adc_module_overview_pin_scan).
mbed_official 579:53297373a894 156 *
mbed_official 579:53297373a894 157 * \subsection asfdoc_sam0_adc_module_overview_diff_mode Differential and Single-Ended Conversion
mbed_official 579:53297373a894 158 * The ADC has two conversion modes; differential and single-ended. When
mbed_official 579:53297373a894 159 * measuring signals where the positive input pin is always at a higher voltage
mbed_official 579:53297373a894 160 * than the negative input pin, the single-ended conversion mode should be used
mbed_official 579:53297373a894 161 * in order to achieve a full 12-bit output resolution.
mbed_official 579:53297373a894 162 *
mbed_official 579:53297373a894 163 * If however the positive input pin voltage may drop below the negative input
mbed_official 579:53297373a894 164 * pin the signed differential mode should be used.
mbed_official 579:53297373a894 165 *
mbed_official 579:53297373a894 166 * \subsection asfdoc_sam0_adc_module_overview_sample_time Sample Time
mbed_official 579:53297373a894 167 * The sample time for each ADC conversion is configurable as a number of half
mbed_official 579:53297373a894 168 * prescaled ADC clock cycles (depending on the prescaler value), allowing the
mbed_official 579:53297373a894 169 * user application to achieve faster or slower sampling depending on the
mbed_official 579:53297373a894 170 * source impedance of the ADC input channels. For applications with high
mbed_official 579:53297373a894 171 * impedance inputs the sample time can be increased to give the ADC an adequate
mbed_official 579:53297373a894 172 * time to sample and convert the input channel.
mbed_official 579:53297373a894 173 *
mbed_official 579:53297373a894 174 * The resulting sampling time is given by the following equation:
mbed_official 579:53297373a894 175 * \f[
mbed_official 579:53297373a894 176 * t_{SAMPLE} = (sample\_length+1) \times \frac{ADC_{CLK}} {2}
mbed_official 579:53297373a894 177 * \f]
mbed_official 579:53297373a894 178 *
mbed_official 579:53297373a894 179 * \subsection asfdoc_sam0_adc_module_overview_averaging Averaging
mbed_official 579:53297373a894 180 * The ADC can be configured to trade conversion speed for accuracy by averaging
mbed_official 579:53297373a894 181 * multiple samples in hardware. This feature is suitable when operating in
mbed_official 579:53297373a894 182 * noisy conditions.
mbed_official 579:53297373a894 183 *
mbed_official 579:53297373a894 184 * You can specify any number of samples to accumulate (up to 1024) and the
mbed_official 579:53297373a894 185 * divide ratio to use (up to divide by 128). To modify these settings the
mbed_official 579:53297373a894 186 * ADC_RESOLUTION_CUSTOM needs to be set as the resolution. When this is set
mbed_official 579:53297373a894 187 * the number of samples to accumulate and the division ratio can be set by
mbed_official 579:53297373a894 188 * the configuration struct members \ref adc_config.accumulate_samples and
mbed_official 579:53297373a894 189 * \ref adc_config.divide_result. When using this mode the ADC result register
mbed_official 579:53297373a894 190 * will be set to be 16-bit wide to accommodate the larger result sizes
mbed_official 579:53297373a894 191 * produced by the accumulator.
mbed_official 579:53297373a894 192 *
mbed_official 579:53297373a894 193 * The effective ADC conversion rate will be reduced by a factor of the number
mbed_official 579:53297373a894 194 * of accumulated samples;
mbed_official 579:53297373a894 195 * however the effective resolution will be increased according to
mbed_official 579:53297373a894 196 * \ref asfdoc_sam0_adc_module_hw_av_resolution "the table below".
mbed_official 579:53297373a894 197 *
mbed_official 579:53297373a894 198 * \anchor asfdoc_sam0_adc_module_hw_av_resolution
mbed_official 579:53297373a894 199 * <table>
mbed_official 579:53297373a894 200 * <caption>Effective ADC Resolution From Various Hardware Averaging Modes</caption>
mbed_official 579:53297373a894 201 * <tr>
mbed_official 579:53297373a894 202 * <th>Number of samples</tr>
mbed_official 579:53297373a894 203 * <th>Final result</tr>
mbed_official 579:53297373a894 204 * </tr>
mbed_official 579:53297373a894 205 * <tr>
mbed_official 579:53297373a894 206 * <td>1</td>
mbed_official 579:53297373a894 207 * <td>12-bit</td>
mbed_official 579:53297373a894 208 * </tr>
mbed_official 579:53297373a894 209 * <tr>
mbed_official 579:53297373a894 210 * <td>2</td>
mbed_official 579:53297373a894 211 * <td>13-bit</td>
mbed_official 579:53297373a894 212 * </tr>
mbed_official 579:53297373a894 213 * <tr>
mbed_official 579:53297373a894 214 * <td>4</td>
mbed_official 579:53297373a894 215 * <td>14-bit</td>
mbed_official 579:53297373a894 216 * </tr>
mbed_official 579:53297373a894 217 * <tr>
mbed_official 579:53297373a894 218 * <td>8</td>
mbed_official 579:53297373a894 219 * <td>15-bit</td>
mbed_official 579:53297373a894 220 * </tr>
mbed_official 579:53297373a894 221 * <tr>
mbed_official 579:53297373a894 222 * <td>16</td>
mbed_official 579:53297373a894 223 * <td>16-bit</td>
mbed_official 579:53297373a894 224 * </tr>
mbed_official 579:53297373a894 225 * <tr>
mbed_official 579:53297373a894 226 * <td>32</td>
mbed_official 579:53297373a894 227 * <td>16-bit</td>
mbed_official 579:53297373a894 228 * </tr>
mbed_official 579:53297373a894 229 * <tr>
mbed_official 579:53297373a894 230 * <td>64</td>
mbed_official 579:53297373a894 231 * <td>16-bit</td>
mbed_official 579:53297373a894 232 * </tr>
mbed_official 579:53297373a894 233 * <tr>
mbed_official 579:53297373a894 234 * <td>128</td>
mbed_official 579:53297373a894 235 * <td>16-bit</td>
mbed_official 579:53297373a894 236 * </tr>
mbed_official 579:53297373a894 237 * <tr>
mbed_official 579:53297373a894 238 * <td>256</td>
mbed_official 579:53297373a894 239 * <td>16-bit</td>
mbed_official 579:53297373a894 240 * </tr>
mbed_official 579:53297373a894 241 * <tr>
mbed_official 579:53297373a894 242 * <td>512</td>
mbed_official 579:53297373a894 243 * <td>16-bit</td>
mbed_official 579:53297373a894 244 * </tr>
mbed_official 579:53297373a894 245 * <tr>
mbed_official 579:53297373a894 246 * <td>1024</td>
mbed_official 579:53297373a894 247 * <td>16-bit</td>
mbed_official 579:53297373a894 248 * </tr>
mbed_official 579:53297373a894 249 * </table>
mbed_official 579:53297373a894 250 *
mbed_official 579:53297373a894 251 *
mbed_official 579:53297373a894 252 * \subsection asfdoc_sam0_adc_module_overview_offset_corr Offset and Gain Correction
mbed_official 579:53297373a894 253 * Inherent gain and offset errors affect the absolute accuracy of the ADC.
mbed_official 579:53297373a894 254 *
mbed_official 579:53297373a894 255 * The offset error is defined as the deviation of the ADC鈥檚 actual transfer
mbed_official 579:53297373a894 256 * function from ideal straight line at zero input voltage.
mbed_official 579:53297373a894 257 *
mbed_official 579:53297373a894 258 * The gain error is defined as the deviation of the last output step's
mbed_official 579:53297373a894 259 * midpoint from the ideal straight line, after compensating for offset error.
mbed_official 579:53297373a894 260 *
mbed_official 579:53297373a894 261 * The offset correction value is subtracted from the converted data before the
mbed_official 579:53297373a894 262 * result is ready. The gain correction value is multiplied with the offset
mbed_official 579:53297373a894 263 * corrected value.
mbed_official 579:53297373a894 264 *
mbed_official 579:53297373a894 265 * The equation for both offset and gain error compensation is shown below:
mbed_official 579:53297373a894 266 * \f[
mbed_official 579:53297373a894 267 * ADC_{RESULT} = (VALUE_{CONV} + CORR_{OFFSET}) \times CORR_{GAIN}
mbed_official 579:53297373a894 268 * \f]
mbed_official 579:53297373a894 269 *
mbed_official 579:53297373a894 270 * When enabled, a given set of offset and gain correction values can be applied
mbed_official 579:53297373a894 271 * to the sampled data in hardware, giving a corrected stream of sample data to
mbed_official 579:53297373a894 272 * the user application at the cost of an increased sample latency.
mbed_official 579:53297373a894 273 *
mbed_official 579:53297373a894 274 * In single conversion, a latency of 13 ADC Generic Clock cycles is added for
mbed_official 579:53297373a894 275 * the final sample result availability. As the correction time is always less
mbed_official 579:53297373a894 276 * than the propagation delay, in free running mode this latency appears only
mbed_official 579:53297373a894 277 * during the first conversion. After the first conversion is complete future
mbed_official 579:53297373a894 278 * conversion results are available at the defined sampling rate.
mbed_official 579:53297373a894 279 *
mbed_official 579:53297373a894 280 * \subsection asfdoc_sam0_adc_module_overview_pin_scan Pin Scan
mbed_official 579:53297373a894 281 * In pin scan mode, the first ADC conversion will begin from the configured
mbed_official 579:53297373a894 282 * positive channel, plus the requested starting offset. When the first
mbed_official 579:53297373a894 283 * conversion is completed, the next conversion will start at the next positive
mbed_official 579:53297373a894 284 * input channel and so on, until all requested pins to scan have been sampled
mbed_official 579:53297373a894 285 * and converted.
mbed_official 579:53297373a894 286 * SAM L21 has automatic sequences feature instead of pin scan mode. In automatic
mbed_official 579:53297373a894 287 * sequence mode, all of 32 positives inputs can be included in a sequence. The
mbed_official 579:53297373a894 288 * sequence starts from the lowest input, and go to the next enabled input
mbed_official 579:53297373a894 289 * automatically.
mbed_official 579:53297373a894 290 *
mbed_official 579:53297373a894 291 * Pin scanning gives a simple mechanism to sample a large number of physical
mbed_official 579:53297373a894 292 * input channel samples, using a single physical ADC channel.
mbed_official 579:53297373a894 293 *
mbed_official 579:53297373a894 294 * \subsection asfdoc_sam0_adc_module_overview_window_monitor Window Monitor
mbed_official 579:53297373a894 295 * The ADC module window monitor function can be used to automatically compare
mbed_official 579:53297373a894 296 * the conversion result against a preconfigured pair of upper and lower
mbed_official 579:53297373a894 297 * threshold values.
mbed_official 579:53297373a894 298 *
mbed_official 579:53297373a894 299 * The threshold values are evaluated differently, depending on whether
mbed_official 579:53297373a894 300 * differential or single-ended mode is selected. In differential mode, the
mbed_official 579:53297373a894 301 * upper and lower thresholds are evaluated as signed values for the comparison,
mbed_official 579:53297373a894 302 * while in single-ended mode the comparisons are made as a set of unsigned
mbed_official 579:53297373a894 303 * values.
mbed_official 579:53297373a894 304 *
mbed_official 579:53297373a894 305 * The significant bits of the lower window monitor threshold and upper window
mbed_official 579:53297373a894 306 * monitor threshold values are user-configurable, and follow the overall ADC
mbed_official 579:53297373a894 307 * sampling bit precision set when the ADC is configured by the user application.
mbed_official 579:53297373a894 308 * For example, only the eight lower bits of the window threshold values will be
mbed_official 579:53297373a894 309 * compares to the sampled data whilst the ADC is configured in 8-bit mode.
mbed_official 579:53297373a894 310 * In addition, if using differential mode, the 8<SUP>th</SUP> bit will be considered as
mbed_official 579:53297373a894 311 * the sign bit even if bit 9 is zero.
mbed_official 579:53297373a894 312 *
mbed_official 579:53297373a894 313 * \subsection asfdoc_sam0_adc_module_overview_events Events
mbed_official 579:53297373a894 314 * Event generation and event actions are configurable in the ADC.
mbed_official 579:53297373a894 315 *
mbed_official 579:53297373a894 316 * The ADC has two actions that can be triggered upon event reception:
mbed_official 579:53297373a894 317 * \li Start conversion
mbed_official 579:53297373a894 318 * \li Flush pipeline and start conversion
mbed_official 579:53297373a894 319 *
mbed_official 579:53297373a894 320 * The ADC can generate two events:
mbed_official 579:53297373a894 321 * \li Window monitor
mbed_official 579:53297373a894 322 * \li Result ready
mbed_official 579:53297373a894 323 *
mbed_official 579:53297373a894 324 * If the event actions are enabled in the configuration, any incoming event
mbed_official 579:53297373a894 325 * will trigger the action.
mbed_official 579:53297373a894 326 *
mbed_official 579:53297373a894 327 * If the window monitor event is enabled, an event will be generated
mbed_official 579:53297373a894 328 * when the configured window condition is detected.
mbed_official 579:53297373a894 329 *
mbed_official 579:53297373a894 330 * If the result ready event is enabled, an event will be generated when a
mbed_official 579:53297373a894 331 * conversion is completed.
mbed_official 579:53297373a894 332 *
mbed_official 579:53297373a894 333 * \note The connection of events between modules requires the use of the
mbed_official 579:53297373a894 334 * \ref asfdoc_sam0_events_group "SAM Event System Driver (EVENTS)"
mbed_official 579:53297373a894 335 * to route output event of one module to the the input event of another.
mbed_official 579:53297373a894 336 * For more information on event routing, refer to the event driver
mbed_official 579:53297373a894 337 * documentation.
mbed_official 579:53297373a894 338 *
mbed_official 579:53297373a894 339 *
mbed_official 579:53297373a894 340 * \section asfdoc_sam0_adc_special_considerations Special Considerations
mbed_official 579:53297373a894 341 *
mbed_official 579:53297373a894 342 * An integrated analog temperature sensor is available for use with the ADC.
mbed_official 579:53297373a894 343 * The bandgap voltage, as well as the scaled I/O and core voltages can also be
mbed_official 579:53297373a894 344 * measured by the ADC. For internal ADC inputs, the internal source(s) may need
mbed_official 579:53297373a894 345 * to be manually enabled by the user application before they can be measured.
mbed_official 579:53297373a894 346 *
mbed_official 579:53297373a894 347 *
mbed_official 579:53297373a894 348 * \section asfdoc_sam0_adc_extra_info Extra Information
mbed_official 579:53297373a894 349 *
mbed_official 579:53297373a894 350 * For extra information, see \ref asfdoc_sam0_adc_extra. This includes:
mbed_official 579:53297373a894 351 * - \ref asfdoc_sam0_adc_extra_acronyms
mbed_official 579:53297373a894 352 * - \ref asfdoc_sam0_adc_extra_dependencies
mbed_official 579:53297373a894 353 * - \ref asfdoc_sam0_adc_extra_errata
mbed_official 579:53297373a894 354 * - \ref asfdoc_sam0_adc_extra_history
mbed_official 579:53297373a894 355 *
mbed_official 579:53297373a894 356 *
mbed_official 579:53297373a894 357 * \section asfdoc_sam0_adc_examples Examples
mbed_official 579:53297373a894 358 *
mbed_official 579:53297373a894 359 * For a list of examples related to this driver, see
mbed_official 579:53297373a894 360 * \ref asfdoc_sam0_adc_exqsg.
mbed_official 579:53297373a894 361 *
mbed_official 579:53297373a894 362 *
mbed_official 579:53297373a894 363 * \section asfdoc_sam0_adc_api_overview API Overview
mbed_official 579:53297373a894 364 * @{
mbed_official 579:53297373a894 365 */
mbed_official 579:53297373a894 366
mbed_official 579:53297373a894 367 #ifdef __cplusplus
mbed_official 579:53297373a894 368 extern "C" {
mbed_official 579:53297373a894 369 #endif
mbed_official 579:53297373a894 370
mbed_official 579:53297373a894 371 #include <compiler.h>
mbed_official 579:53297373a894 372 #include <system.h>
mbed_official 579:53297373a894 373 #include <adc_feature.h>
mbed_official 579:53297373a894 374
mbed_official 579:53297373a894 375 /**
mbed_official 579:53297373a894 376 * \name Module Status Flags
mbed_official 579:53297373a894 377 *
mbed_official 579:53297373a894 378 * ADC status flags, returned by \ref adc_get_status() and cleared by
mbed_official 579:53297373a894 379 * \ref adc_clear_status().
mbed_official 579:53297373a894 380 *
mbed_official 579:53297373a894 381 * @{
mbed_official 579:53297373a894 382 */
mbed_official 579:53297373a894 383
mbed_official 579:53297373a894 384 /** ADC result ready. */
mbed_official 579:53297373a894 385 #define ADC_STATUS_RESULT_READY (1UL << 0)
mbed_official 579:53297373a894 386 /** Window monitor match. */
mbed_official 579:53297373a894 387 #define ADC_STATUS_WINDOW (1UL << 1)
mbed_official 579:53297373a894 388 /** ADC result overwritten before read. */
mbed_official 579:53297373a894 389 #define ADC_STATUS_OVERRUN (1UL << 2)
mbed_official 579:53297373a894 390
mbed_official 579:53297373a894 391 /** @} */
mbed_official 579:53297373a894 392
mbed_official 579:53297373a894 393 /**
mbed_official 579:53297373a894 394 * \name Driver Initialization and Configuration
mbed_official 579:53297373a894 395 * @{
mbed_official 579:53297373a894 396 */
mbed_official 579:53297373a894 397 enum status_code adc_init(
mbed_official 579:53297373a894 398 struct adc_module *const module_inst,
mbed_official 579:53297373a894 399 Adc *hw,
mbed_official 579:53297373a894 400 struct adc_config *config);
mbed_official 579:53297373a894 401
mbed_official 579:53297373a894 402 void adc_get_config_defaults(
mbed_official 579:53297373a894 403 struct adc_config *const config);
mbed_official 579:53297373a894 404 /** @} */
mbed_official 579:53297373a894 405
mbed_official 579:53297373a894 406 /**
mbed_official 579:53297373a894 407 * \name Status Management
mbed_official 579:53297373a894 408 * @{
mbed_official 579:53297373a894 409 */
mbed_official 579:53297373a894 410
mbed_official 579:53297373a894 411 /**
mbed_official 579:53297373a894 412 * \brief Retrieves the current module status.
mbed_official 579:53297373a894 413 *
mbed_official 579:53297373a894 414 * Retrieves the status of the module, giving overall state information.
mbed_official 579:53297373a894 415 *
mbed_official 579:53297373a894 416 * \param[in] module_inst Pointer to the ADC software instance struct
mbed_official 579:53297373a894 417 *
mbed_official 579:53297373a894 418 * \return Bitmask of \c ADC_STATUS_* flags.
mbed_official 579:53297373a894 419 *
mbed_official 579:53297373a894 420 * \retval ADC_STATUS_RESULT_READY ADC Result is ready to be read
mbed_official 579:53297373a894 421 * \retval ADC_STATUS_WINDOW ADC has detected a value inside the set
mbed_official 579:53297373a894 422 * window range
mbed_official 579:53297373a894 423 * \retval ADC_STATUS_OVERRUN ADC result has overrun
mbed_official 579:53297373a894 424 */
mbed_official 579:53297373a894 425 static inline uint32_t adc_get_status(
mbed_official 579:53297373a894 426 struct adc_module *const module_inst)
mbed_official 579:53297373a894 427 {
mbed_official 579:53297373a894 428 /* Sanity check arguments */
mbed_official 579:53297373a894 429 Assert(module_inst);
mbed_official 579:53297373a894 430 Assert(module_inst->hw);
mbed_official 579:53297373a894 431
mbed_official 579:53297373a894 432 Adc *const adc_module = module_inst->hw;
mbed_official 579:53297373a894 433
mbed_official 579:53297373a894 434 uint32_t int_flags = adc_module->INTFLAG.reg;
mbed_official 579:53297373a894 435
mbed_official 579:53297373a894 436 uint32_t status_flags = 0;
mbed_official 579:53297373a894 437
mbed_official 579:53297373a894 438 /* Check for ADC Result Ready */
mbed_official 579:53297373a894 439 if (int_flags & ADC_INTFLAG_RESRDY) {
mbed_official 579:53297373a894 440 status_flags |= ADC_STATUS_RESULT_READY;
mbed_official 579:53297373a894 441 }
mbed_official 579:53297373a894 442
mbed_official 579:53297373a894 443 /* Check for ADC Window Match */
mbed_official 579:53297373a894 444 if (int_flags & ADC_INTFLAG_WINMON) {
mbed_official 579:53297373a894 445 status_flags |= ADC_STATUS_WINDOW;
mbed_official 579:53297373a894 446 }
mbed_official 579:53297373a894 447
mbed_official 579:53297373a894 448 /* Check for ADC Overrun */
mbed_official 579:53297373a894 449 if (int_flags & ADC_INTFLAG_OVERRUN) {
mbed_official 579:53297373a894 450 status_flags |= ADC_STATUS_OVERRUN;
mbed_official 579:53297373a894 451 }
mbed_official 579:53297373a894 452
mbed_official 579:53297373a894 453 return status_flags;
mbed_official 579:53297373a894 454 }
mbed_official 579:53297373a894 455
mbed_official 579:53297373a894 456 /**
mbed_official 579:53297373a894 457 * \brief Clears a module status flag.
mbed_official 579:53297373a894 458 *
mbed_official 579:53297373a894 459 * Clears the given status flag of the module.
mbed_official 579:53297373a894 460 *
mbed_official 579:53297373a894 461 * \param[in] module_inst Pointer to the ADC software instance struct
mbed_official 579:53297373a894 462 * \param[in] status_flags Bitmask of \c ADC_STATUS_* flags to clear
mbed_official 579:53297373a894 463 */
mbed_official 579:53297373a894 464 static inline void adc_clear_status(
mbed_official 579:53297373a894 465 struct adc_module *const module_inst,
mbed_official 579:53297373a894 466 const uint32_t status_flags)
mbed_official 579:53297373a894 467 {
mbed_official 579:53297373a894 468 /* Sanity check arguments */
mbed_official 579:53297373a894 469 Assert(module_inst);
mbed_official 579:53297373a894 470 Assert(module_inst->hw);
mbed_official 579:53297373a894 471
mbed_official 579:53297373a894 472 Adc *const adc_module = module_inst->hw;
mbed_official 579:53297373a894 473
mbed_official 579:53297373a894 474 uint32_t int_flags = 0;
mbed_official 579:53297373a894 475
mbed_official 579:53297373a894 476 /* Check for ADC Result Ready */
mbed_official 579:53297373a894 477 if (status_flags & ADC_STATUS_RESULT_READY) {
mbed_official 579:53297373a894 478 int_flags |= ADC_INTFLAG_RESRDY;
mbed_official 579:53297373a894 479 }
mbed_official 579:53297373a894 480
mbed_official 579:53297373a894 481 /* Check for ADC Window Match */
mbed_official 579:53297373a894 482 if (status_flags & ADC_STATUS_WINDOW) {
mbed_official 579:53297373a894 483 int_flags |= ADC_INTFLAG_WINMON;
mbed_official 579:53297373a894 484 }
mbed_official 579:53297373a894 485
mbed_official 579:53297373a894 486 /* Check for ADC Overrun */
mbed_official 579:53297373a894 487 if (status_flags & ADC_STATUS_OVERRUN) {
mbed_official 579:53297373a894 488 int_flags |= ADC_INTFLAG_OVERRUN;
mbed_official 579:53297373a894 489 }
mbed_official 579:53297373a894 490
mbed_official 579:53297373a894 491 /* Clear interrupt flag */
mbed_official 579:53297373a894 492 adc_module->INTFLAG.reg = int_flags;
mbed_official 579:53297373a894 493 }
mbed_official 579:53297373a894 494 /** @} */
mbed_official 579:53297373a894 495
mbed_official 579:53297373a894 496 /**
mbed_official 579:53297373a894 497 * \name Enable, Disable and Reset ADC Module, Start Conversion and Read Result
mbed_official 579:53297373a894 498 * @{
mbed_official 579:53297373a894 499 */
mbed_official 579:53297373a894 500
mbed_official 579:53297373a894 501 /**
mbed_official 579:53297373a894 502 * \brief Enables the ADC module.
mbed_official 579:53297373a894 503 *
mbed_official 579:53297373a894 504 * Enables an ADC module that has previously been configured. If any internal reference
mbed_official 579:53297373a894 505 * is selected it will be enabled.
mbed_official 579:53297373a894 506 *
mbed_official 579:53297373a894 507 * \param[in] module_inst Pointer to the ADC software instance struct
mbed_official 579:53297373a894 508 */
mbed_official 579:53297373a894 509 static inline enum status_code adc_enable(
mbed_official 579:53297373a894 510 struct adc_module *const module_inst)
mbed_official 579:53297373a894 511 {
mbed_official 579:53297373a894 512 Assert(module_inst);
mbed_official 579:53297373a894 513 Assert(module_inst->hw);
mbed_official 579:53297373a894 514
mbed_official 579:53297373a894 515 Adc *const adc_module = module_inst->hw;
mbed_official 579:53297373a894 516
mbed_official 579:53297373a894 517 while (adc_is_syncing(module_inst)) {
mbed_official 579:53297373a894 518 /* Wait for synchronization */
mbed_official 579:53297373a894 519 }
mbed_official 579:53297373a894 520
mbed_official 579:53297373a894 521 #if ADC_CALLBACK_MODE == true
mbed_official 579:53297373a894 522 system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_ADC);
mbed_official 579:53297373a894 523 #endif
mbed_official 579:53297373a894 524
mbed_official 579:53297373a894 525 adc_module->CTRLA.reg |= ADC_CTRLA_ENABLE;
mbed_official 579:53297373a894 526
mbed_official 579:53297373a894 527 while (adc_is_syncing(module_inst)) {
mbed_official 579:53297373a894 528 /* Wait for synchronization */
mbed_official 579:53297373a894 529 }
mbed_official 579:53297373a894 530 return STATUS_OK;
mbed_official 579:53297373a894 531 }
mbed_official 579:53297373a894 532
mbed_official 579:53297373a894 533 /**
mbed_official 579:53297373a894 534 * \brief Disables the ADC module.
mbed_official 579:53297373a894 535 *
mbed_official 579:53297373a894 536 * Disables an ADC module that was previously enabled.
mbed_official 579:53297373a894 537 *
mbed_official 579:53297373a894 538 * \param[in] module_inst Pointer to the ADC software instance struct
mbed_official 579:53297373a894 539 */
mbed_official 579:53297373a894 540 static inline enum status_code adc_disable(
mbed_official 579:53297373a894 541 struct adc_module *const module_inst)
mbed_official 579:53297373a894 542 {
mbed_official 579:53297373a894 543 Assert(module_inst);
mbed_official 579:53297373a894 544 Assert(module_inst->hw);
mbed_official 579:53297373a894 545
mbed_official 579:53297373a894 546 Adc *const adc_module = module_inst->hw;
mbed_official 579:53297373a894 547
mbed_official 579:53297373a894 548 #if ADC_CALLBACK_MODE == true
mbed_official 579:53297373a894 549 system_interrupt_disable(SYSTEM_INTERRUPT_MODULE_ADC);
mbed_official 579:53297373a894 550 #endif
mbed_official 579:53297373a894 551
mbed_official 579:53297373a894 552 while (adc_is_syncing(module_inst)) {
mbed_official 579:53297373a894 553 /* Wait for synchronization */
mbed_official 579:53297373a894 554 }
mbed_official 579:53297373a894 555
mbed_official 579:53297373a894 556 adc_module->CTRLA.reg &= ~ADC_CTRLA_ENABLE;
mbed_official 579:53297373a894 557
mbed_official 579:53297373a894 558 while (adc_is_syncing(module_inst)) {
mbed_official 579:53297373a894 559 /* Wait for synchronization */
mbed_official 579:53297373a894 560 }
mbed_official 579:53297373a894 561 return STATUS_OK;
mbed_official 579:53297373a894 562 }
mbed_official 579:53297373a894 563
mbed_official 579:53297373a894 564 /**
mbed_official 579:53297373a894 565 * \brief Resets the ADC module.
mbed_official 579:53297373a894 566 *
mbed_official 579:53297373a894 567 * Resets an ADC module, clearing all module state and registers to their
mbed_official 579:53297373a894 568 * default values.
mbed_official 579:53297373a894 569 *
mbed_official 579:53297373a894 570 * \param[in] module_inst Pointer to the ADC software instance struct
mbed_official 579:53297373a894 571 */
mbed_official 579:53297373a894 572 static inline enum status_code adc_reset(
mbed_official 579:53297373a894 573 struct adc_module *const module_inst)
mbed_official 579:53297373a894 574 {
mbed_official 579:53297373a894 575 /* Sanity check arguments */
mbed_official 579:53297373a894 576 Assert(module_inst);
mbed_official 579:53297373a894 577 Assert(module_inst->hw);
mbed_official 579:53297373a894 578
mbed_official 579:53297373a894 579 Adc *const adc_module = module_inst->hw;
mbed_official 579:53297373a894 580
mbed_official 579:53297373a894 581 /* Disable to make sure the pipeline is flushed before reset */
mbed_official 579:53297373a894 582 adc_disable(module_inst);
mbed_official 579:53297373a894 583
mbed_official 579:53297373a894 584 /* Software reset the module */
mbed_official 579:53297373a894 585 adc_module->CTRLA.reg |= ADC_CTRLA_SWRST;
mbed_official 579:53297373a894 586
mbed_official 579:53297373a894 587 while (adc_is_syncing(module_inst)) {
mbed_official 579:53297373a894 588 /* Wait for synchronization */
mbed_official 579:53297373a894 589 }
mbed_official 579:53297373a894 590 return STATUS_OK;
mbed_official 579:53297373a894 591 }
mbed_official 579:53297373a894 592
mbed_official 579:53297373a894 593
mbed_official 579:53297373a894 594 /**
mbed_official 579:53297373a894 595 * \brief Enables an ADC event input or output.
mbed_official 579:53297373a894 596 *
mbed_official 579:53297373a894 597 * Enables one or more input or output events to or from the ADC module. See
mbed_official 579:53297373a894 598 * \ref adc_events "here" for a list of events this module supports.
mbed_official 579:53297373a894 599 *
mbed_official 579:53297373a894 600 * \note Events cannot be altered while the module is enabled.
mbed_official 579:53297373a894 601 *
mbed_official 579:53297373a894 602 * \param[in] module_inst Software instance for the ADC peripheral
mbed_official 579:53297373a894 603 * \param[in] events Struct containing flags of events to enable
mbed_official 579:53297373a894 604 */
mbed_official 579:53297373a894 605 static inline void adc_enable_events(
mbed_official 579:53297373a894 606 struct adc_module *const module_inst,
mbed_official 579:53297373a894 607 struct adc_events *const events)
mbed_official 579:53297373a894 608 {
mbed_official 579:53297373a894 609 /* Sanity check arguments */
mbed_official 579:53297373a894 610 Assert(module_inst);
mbed_official 579:53297373a894 611 Assert(module_inst->hw);
mbed_official 579:53297373a894 612 Assert(events);
mbed_official 579:53297373a894 613
mbed_official 579:53297373a894 614 Adc *const adc_module = module_inst->hw;
mbed_official 579:53297373a894 615
mbed_official 579:53297373a894 616 uint32_t event_mask = 0;
mbed_official 579:53297373a894 617
mbed_official 579:53297373a894 618 /* Configure Window Monitor event */
mbed_official 579:53297373a894 619 if (events->generate_event_on_window_monitor) {
mbed_official 579:53297373a894 620 event_mask |= ADC_EVCTRL_WINMONEO;
mbed_official 579:53297373a894 621 }
mbed_official 579:53297373a894 622
mbed_official 579:53297373a894 623 /* Configure Result Ready event */
mbed_official 579:53297373a894 624 if (events->generate_event_on_conversion_done) {
mbed_official 579:53297373a894 625 event_mask |= ADC_EVCTRL_RESRDYEO;
mbed_official 579:53297373a894 626 }
mbed_official 579:53297373a894 627
mbed_official 579:53297373a894 628 adc_module->EVCTRL.reg |= event_mask;
mbed_official 579:53297373a894 629 }
mbed_official 579:53297373a894 630
mbed_official 579:53297373a894 631 /**
mbed_official 579:53297373a894 632 * \brief Disables an ADC event input or output.
mbed_official 579:53297373a894 633 *
mbed_official 579:53297373a894 634 * Disables one or more input or output events to or from the ADC module. See
mbed_official 579:53297373a894 635 * \ref adc_events "here" for a list of events this module supports.
mbed_official 579:53297373a894 636 *
mbed_official 579:53297373a894 637 * \note Events cannot be altered while the module is enabled.
mbed_official 579:53297373a894 638 *
mbed_official 579:53297373a894 639 * \param[in] module_inst Software instance for the ADC peripheral
mbed_official 579:53297373a894 640 * \param[in] events Struct containing flags of events to disable
mbed_official 579:53297373a894 641 */
mbed_official 579:53297373a894 642 static inline void adc_disable_events(
mbed_official 579:53297373a894 643 struct adc_module *const module_inst,
mbed_official 579:53297373a894 644 struct adc_events *const events)
mbed_official 579:53297373a894 645 {
mbed_official 579:53297373a894 646 /* Sanity check arguments */
mbed_official 579:53297373a894 647 Assert(module_inst);
mbed_official 579:53297373a894 648 Assert(module_inst->hw);
mbed_official 579:53297373a894 649 Assert(events);
mbed_official 579:53297373a894 650
mbed_official 579:53297373a894 651 Adc *const adc_module = module_inst->hw;
mbed_official 579:53297373a894 652
mbed_official 579:53297373a894 653 uint32_t event_mask = 0;
mbed_official 579:53297373a894 654
mbed_official 579:53297373a894 655 /* Configure Window Monitor event */
mbed_official 579:53297373a894 656 if (events->generate_event_on_window_monitor) {
mbed_official 579:53297373a894 657 event_mask |= ADC_EVCTRL_WINMONEO;
mbed_official 579:53297373a894 658 }
mbed_official 579:53297373a894 659
mbed_official 579:53297373a894 660 /* Configure Result Ready event */
mbed_official 579:53297373a894 661 if (events->generate_event_on_conversion_done) {
mbed_official 579:53297373a894 662 event_mask |= ADC_EVCTRL_RESRDYEO;
mbed_official 579:53297373a894 663 }
mbed_official 579:53297373a894 664
mbed_official 579:53297373a894 665 adc_module->EVCTRL.reg &= ~event_mask;
mbed_official 579:53297373a894 666 }
mbed_official 579:53297373a894 667
mbed_official 579:53297373a894 668 /**
mbed_official 579:53297373a894 669 * \brief Starts an ADC conversion.
mbed_official 579:53297373a894 670 *
mbed_official 579:53297373a894 671 * Starts a new ADC conversion.
mbed_official 579:53297373a894 672 *
mbed_official 579:53297373a894 673 * \param[in] module_inst Pointer to the ADC software instance struct
mbed_official 579:53297373a894 674 */
mbed_official 579:53297373a894 675 static inline void adc_start_conversion(
mbed_official 579:53297373a894 676 struct adc_module *const module_inst)
mbed_official 579:53297373a894 677 {
mbed_official 579:53297373a894 678 Assert(module_inst);
mbed_official 579:53297373a894 679 Assert(module_inst->hw);
mbed_official 579:53297373a894 680
mbed_official 579:53297373a894 681 Adc *const adc_module = module_inst->hw;
mbed_official 579:53297373a894 682
mbed_official 579:53297373a894 683 while (adc_is_syncing(module_inst)) {
mbed_official 579:53297373a894 684 /* Wait for synchronization */
mbed_official 579:53297373a894 685 }
mbed_official 579:53297373a894 686
mbed_official 579:53297373a894 687 adc_module->SWTRIG.reg |= ADC_SWTRIG_START;
mbed_official 579:53297373a894 688
mbed_official 579:53297373a894 689 while (adc_is_syncing(module_inst)) {
mbed_official 579:53297373a894 690 /* Wait for synchronization */
mbed_official 579:53297373a894 691 }
mbed_official 579:53297373a894 692 }
mbed_official 579:53297373a894 693
mbed_official 579:53297373a894 694 /**
mbed_official 579:53297373a894 695 * \brief Reads the ADC result.
mbed_official 579:53297373a894 696 *
mbed_official 579:53297373a894 697 * Reads the result from an ADC conversion that was previously started.
mbed_official 579:53297373a894 698 *
mbed_official 579:53297373a894 699 * \param[in] module_inst Pointer to the ADC software instance struct
mbed_official 579:53297373a894 700 * \param[out] result Pointer to store the result value in
mbed_official 579:53297373a894 701 *
mbed_official 579:53297373a894 702 * \return Status of the ADC read request.
mbed_official 579:53297373a894 703 * \retval STATUS_OK The result was retrieved successfully
mbed_official 579:53297373a894 704 * \retval STATUS_BUSY A conversion result was not ready
mbed_official 579:53297373a894 705 * \retval STATUS_ERR_OVERFLOW The result register has been overwritten by the
mbed_official 579:53297373a894 706 * ADC module before the result was read by the software
mbed_official 579:53297373a894 707 */
mbed_official 579:53297373a894 708 static inline enum status_code adc_read(
mbed_official 579:53297373a894 709 struct adc_module *const module_inst,
mbed_official 579:53297373a894 710 uint16_t *result)
mbed_official 579:53297373a894 711 {
mbed_official 579:53297373a894 712 Assert(module_inst);
mbed_official 579:53297373a894 713 Assert(module_inst->hw);
mbed_official 579:53297373a894 714 Assert(result);
mbed_official 579:53297373a894 715
mbed_official 579:53297373a894 716 if (!(adc_get_status(module_inst) & ADC_STATUS_RESULT_READY)) {
mbed_official 579:53297373a894 717 /* Result not ready */
mbed_official 579:53297373a894 718 return STATUS_BUSY;
mbed_official 579:53297373a894 719 }
mbed_official 579:53297373a894 720
mbed_official 579:53297373a894 721 Adc *const adc_module = module_inst->hw;
mbed_official 579:53297373a894 722
mbed_official 579:53297373a894 723 while (adc_is_syncing(module_inst)) {
mbed_official 579:53297373a894 724 /* Wait for synchronization */
mbed_official 579:53297373a894 725 }
mbed_official 579:53297373a894 726
mbed_official 579:53297373a894 727 /* Get ADC result */
mbed_official 579:53297373a894 728 *result = adc_module->RESULT.reg;
mbed_official 579:53297373a894 729
mbed_official 579:53297373a894 730 /* Reset ready flag */
mbed_official 579:53297373a894 731 adc_clear_status(module_inst, ADC_STATUS_RESULT_READY);
mbed_official 579:53297373a894 732
mbed_official 579:53297373a894 733 if (adc_get_status(module_inst) & ADC_STATUS_OVERRUN) {
mbed_official 579:53297373a894 734 adc_clear_status(module_inst, ADC_STATUS_OVERRUN);
mbed_official 579:53297373a894 735 return STATUS_ERR_OVERFLOW;
mbed_official 579:53297373a894 736 }
mbed_official 579:53297373a894 737
mbed_official 579:53297373a894 738 return STATUS_OK;
mbed_official 579:53297373a894 739 }
mbed_official 579:53297373a894 740
mbed_official 579:53297373a894 741 /** @} */
mbed_official 579:53297373a894 742
mbed_official 579:53297373a894 743 /**
mbed_official 579:53297373a894 744 * \name Runtime Changes of ADC Module
mbed_official 579:53297373a894 745 * @{
mbed_official 579:53297373a894 746 */
mbed_official 579:53297373a894 747
mbed_official 579:53297373a894 748 /**
mbed_official 579:53297373a894 749 * \brief Flushes the ADC pipeline.
mbed_official 579:53297373a894 750 *
mbed_official 579:53297373a894 751 * Flushes the pipeline and restart the ADC clock on the next peripheral clock
mbed_official 579:53297373a894 752 * edge. All conversions in progress will be lost. When flush is complete, the
mbed_official 579:53297373a894 753 * module will resume where it left off.
mbed_official 579:53297373a894 754 *
mbed_official 579:53297373a894 755 * \param[in] module_inst Pointer to the ADC software instance struct
mbed_official 579:53297373a894 756 */
mbed_official 579:53297373a894 757 static inline void adc_flush(
mbed_official 579:53297373a894 758 struct adc_module *const module_inst)
mbed_official 579:53297373a894 759 {
mbed_official 579:53297373a894 760 Assert(module_inst);
mbed_official 579:53297373a894 761 Assert(module_inst->hw);
mbed_official 579:53297373a894 762
mbed_official 579:53297373a894 763 Adc *const adc_module = module_inst->hw;
mbed_official 579:53297373a894 764
mbed_official 579:53297373a894 765 while (adc_is_syncing(module_inst)) {
mbed_official 579:53297373a894 766 /* Wait for synchronization */
mbed_official 579:53297373a894 767 }
mbed_official 579:53297373a894 768
mbed_official 579:53297373a894 769 adc_module->SWTRIG.reg |= ADC_SWTRIG_FLUSH;
mbed_official 579:53297373a894 770
mbed_official 579:53297373a894 771 while (adc_is_syncing(module_inst)) {
mbed_official 579:53297373a894 772 /* Wait for synchronization */
mbed_official 579:53297373a894 773 }
mbed_official 579:53297373a894 774 }
mbed_official 579:53297373a894 775 void adc_set_window_mode(
mbed_official 579:53297373a894 776 struct adc_module *const module_inst,
mbed_official 579:53297373a894 777 const enum adc_window_mode window_mode,
mbed_official 579:53297373a894 778 const int16_t window_lower_value,
mbed_official 579:53297373a894 779 const int16_t window_upper_value);
mbed_official 579:53297373a894 780
mbed_official 579:53297373a894 781 /**
mbed_official 579:53297373a894 782 * \brief Sets positive ADC input pin.
mbed_official 579:53297373a894 783 *
mbed_official 579:53297373a894 784 * Sets the positive ADC input pin selection.
mbed_official 579:53297373a894 785 *
mbed_official 579:53297373a894 786 * \param[in] module_inst Pointer to the ADC software instance struct
mbed_official 579:53297373a894 787 * \param[in] positive_input Positive input pin
mbed_official 579:53297373a894 788 */
mbed_official 579:53297373a894 789 static inline void adc_set_positive_input(
mbed_official 579:53297373a894 790 struct adc_module *const module_inst,
mbed_official 579:53297373a894 791 const enum adc_positive_input positive_input)
mbed_official 579:53297373a894 792 {
mbed_official 579:53297373a894 793 /* Sanity check arguments */
mbed_official 579:53297373a894 794 Assert(module_inst);
mbed_official 579:53297373a894 795 Assert(module_inst->hw);
mbed_official 579:53297373a894 796
mbed_official 579:53297373a894 797 Adc *const adc_module = module_inst->hw;
mbed_official 579:53297373a894 798
mbed_official 579:53297373a894 799 while (adc_is_syncing(module_inst)) {
mbed_official 579:53297373a894 800 /* Wait for synchronization */
mbed_official 579:53297373a894 801 }
mbed_official 579:53297373a894 802
mbed_official 579:53297373a894 803 /* Set positive input pin */
mbed_official 579:53297373a894 804 adc_module->INPUTCTRL.reg =
mbed_official 579:53297373a894 805 (adc_module->INPUTCTRL.reg & ~ADC_INPUTCTRL_MUXPOS_Msk) |
mbed_official 579:53297373a894 806 (positive_input);
mbed_official 579:53297373a894 807
mbed_official 579:53297373a894 808 while (adc_is_syncing(module_inst)) {
mbed_official 579:53297373a894 809 /* Wait for synchronization */
mbed_official 579:53297373a894 810 }
mbed_official 579:53297373a894 811 }
mbed_official 579:53297373a894 812
mbed_official 579:53297373a894 813
mbed_official 579:53297373a894 814 /**
mbed_official 579:53297373a894 815 * \brief Sets negative ADC input pin for differential mode.
mbed_official 579:53297373a894 816 *
mbed_official 579:53297373a894 817 * Sets the negative ADC input pin, when the ADC is configured in differential
mbed_official 579:53297373a894 818 * mode.
mbed_official 579:53297373a894 819 *
mbed_official 579:53297373a894 820 * \param[in] module_inst Pointer to the ADC software instance struct
mbed_official 579:53297373a894 821 * \param[in] negative_input Negative input pin
mbed_official 579:53297373a894 822 */
mbed_official 579:53297373a894 823 static inline void adc_set_negative_input(
mbed_official 579:53297373a894 824 struct adc_module *const module_inst,
mbed_official 579:53297373a894 825 const enum adc_negative_input negative_input)
mbed_official 579:53297373a894 826 {
mbed_official 579:53297373a894 827 /* Sanity check arguments */
mbed_official 579:53297373a894 828 Assert(module_inst);
mbed_official 579:53297373a894 829 Assert(module_inst->hw);
mbed_official 579:53297373a894 830
mbed_official 579:53297373a894 831 Adc *const adc_module = module_inst->hw;
mbed_official 579:53297373a894 832
mbed_official 579:53297373a894 833 while (adc_is_syncing(module_inst)) {
mbed_official 579:53297373a894 834 /* Wait for synchronization */
mbed_official 579:53297373a894 835 }
mbed_official 579:53297373a894 836
mbed_official 579:53297373a894 837 /* Set negative input pin */
mbed_official 579:53297373a894 838 adc_module->INPUTCTRL.reg =
mbed_official 579:53297373a894 839 (adc_module->INPUTCTRL.reg & ~ADC_INPUTCTRL_MUXNEG_Msk) |
mbed_official 579:53297373a894 840 (negative_input);
mbed_official 579:53297373a894 841
mbed_official 579:53297373a894 842 while (adc_is_syncing(module_inst)) {
mbed_official 579:53297373a894 843 /* Wait for synchronization */
mbed_official 579:53297373a894 844 }
mbed_official 579:53297373a894 845 }
mbed_official 579:53297373a894 846
mbed_official 579:53297373a894 847 /** @} */
mbed_official 579:53297373a894 848
mbed_official 579:53297373a894 849 #if ADC_CALLBACK_MODE == true
mbed_official 579:53297373a894 850 /**
mbed_official 579:53297373a894 851 * \name Enable and Disable Interrupts
mbed_official 579:53297373a894 852 * @{
mbed_official 579:53297373a894 853 */
mbed_official 579:53297373a894 854
mbed_official 579:53297373a894 855 /**
mbed_official 579:53297373a894 856 * \brief Enable interrupt.
mbed_official 579:53297373a894 857 *
mbed_official 579:53297373a894 858 * Enable the given interrupt request from the ADC module.
mbed_official 579:53297373a894 859 *
mbed_official 579:53297373a894 860 * \param[in] module_inst Pointer to the ADC software instance struct
mbed_official 579:53297373a894 861 * \param[in] interrupt Interrupt to enable
mbed_official 579:53297373a894 862 */
mbed_official 579:53297373a894 863 static inline void adc_enable_interrupt(struct adc_module *const module_inst,
mbed_official 579:53297373a894 864 enum adc_interrupt_flag interrupt)
mbed_official 579:53297373a894 865 {
mbed_official 579:53297373a894 866 /* Sanity check arguments */
mbed_official 579:53297373a894 867 Assert(module_inst);
mbed_official 579:53297373a894 868 Assert(module_inst->hw);
mbed_official 579:53297373a894 869
mbed_official 579:53297373a894 870 Adc *const adc_module = module_inst->hw;
mbed_official 579:53297373a894 871 /* Enable interrupt */
mbed_official 579:53297373a894 872 adc_module->INTENSET.reg = interrupt;
mbed_official 579:53297373a894 873 }
mbed_official 579:53297373a894 874
mbed_official 579:53297373a894 875 /**
mbed_official 579:53297373a894 876 * \brief Disable interrupt.
mbed_official 579:53297373a894 877 *
mbed_official 579:53297373a894 878 * Disable the given interrupt request from the ADC module.
mbed_official 579:53297373a894 879 *
mbed_official 579:53297373a894 880 * \param[in] module_inst Pointer to the ADC software instance struct
mbed_official 579:53297373a894 881 * \param[in] interrupt Interrupt to disable
mbed_official 579:53297373a894 882 */
mbed_official 579:53297373a894 883 static inline void adc_disable_interrupt(struct adc_module *const module_inst,
mbed_official 579:53297373a894 884 enum adc_interrupt_flag interrupt)
mbed_official 579:53297373a894 885 {
mbed_official 579:53297373a894 886 /* Sanity check arguments */
mbed_official 579:53297373a894 887 Assert(module_inst);
mbed_official 579:53297373a894 888 Assert(module_inst->hw);
mbed_official 579:53297373a894 889
mbed_official 579:53297373a894 890 Adc *const adc_module = module_inst->hw;
mbed_official 579:53297373a894 891 /* Enable interrupt */
mbed_official 579:53297373a894 892 adc_module->INTENCLR.reg = interrupt;
mbed_official 579:53297373a894 893 }
mbed_official 579:53297373a894 894
mbed_official 579:53297373a894 895 /** @} */
mbed_official 579:53297373a894 896 #endif /* ADC_CALLBACK_MODE == true */
mbed_official 579:53297373a894 897
mbed_official 579:53297373a894 898 #ifdef __cplusplus
mbed_official 579:53297373a894 899 }
mbed_official 579:53297373a894 900 #endif
mbed_official 579:53297373a894 901
mbed_official 579:53297373a894 902 /** @} */
mbed_official 579:53297373a894 903
mbed_official 579:53297373a894 904
mbed_official 579:53297373a894 905 /**
mbed_official 579:53297373a894 906 * \page asfdoc_sam0_adc_extra Extra Information for ADC Driver
mbed_official 579:53297373a894 907 *
mbed_official 579:53297373a894 908 * \section asfdoc_sam0_adc_extra_acronyms Acronyms
mbed_official 579:53297373a894 909 * Below is a table listing the acronyms used in this module, along with their
mbed_official 579:53297373a894 910 * intended meanings.
mbed_official 579:53297373a894 911 *
mbed_official 579:53297373a894 912 * <table>
mbed_official 579:53297373a894 913 * <tr>
mbed_official 579:53297373a894 914 * <th>Acronym</th>
mbed_official 579:53297373a894 915 * <th>Description</th>
mbed_official 579:53297373a894 916 * </tr>
mbed_official 579:53297373a894 917 * <tr>
mbed_official 579:53297373a894 918 * <td>ADC</td>
mbed_official 579:53297373a894 919 * <td>Analog-to-Digital Converter</td>
mbed_official 579:53297373a894 920 * </tr>
mbed_official 579:53297373a894 921 * <tr>
mbed_official 579:53297373a894 922 * <td>DAC</td>
mbed_official 579:53297373a894 923 * <td>Digital-to-Analog Converter</td>
mbed_official 579:53297373a894 924 * </tr>
mbed_official 579:53297373a894 925 * <tr>
mbed_official 579:53297373a894 926 * <td>LSB</td>
mbed_official 579:53297373a894 927 * <td>Least Significant Bit</td>
mbed_official 579:53297373a894 928 * </tr>
mbed_official 579:53297373a894 929 * <tr>
mbed_official 579:53297373a894 930 * <td>MSB</td>
mbed_official 579:53297373a894 931 * <td>Most Significant Bit</td>
mbed_official 579:53297373a894 932 * </tr>
mbed_official 579:53297373a894 933 * <tr>
mbed_official 579:53297373a894 934 * <td>DMA</td>
mbed_official 579:53297373a894 935 * <td>Direct Memory Access</td>
mbed_official 579:53297373a894 936 * </tr>
mbed_official 579:53297373a894 937 * </table>
mbed_official 579:53297373a894 938 *
mbed_official 579:53297373a894 939 *
mbed_official 579:53297373a894 940 * \section asfdoc_sam0_adc_extra_dependencies Dependencies
mbed_official 579:53297373a894 941 * This driver has the following dependencies:
mbed_official 579:53297373a894 942 *
mbed_official 579:53297373a894 943 * - \ref asfdoc_sam0_system_pinmux_group "System Pin Multiplexer Driver"
mbed_official 579:53297373a894 944 *
mbed_official 579:53297373a894 945 *
mbed_official 579:53297373a894 946 * \section asfdoc_sam0_adc_extra_errata Errata
mbed_official 579:53297373a894 947 * There are no errata related to this driver.
mbed_official 579:53297373a894 948 *
mbed_official 579:53297373a894 949 *
mbed_official 579:53297373a894 950 * \section asfdoc_sam0_adc_extra_history Module History
mbed_official 579:53297373a894 951 * An overview of the module history is presented in the table below, with
mbed_official 579:53297373a894 952 * details on the enhancements and fixes made to the module since its first
mbed_official 579:53297373a894 953 * release. The current version of this corresponds to the newest version in
mbed_official 579:53297373a894 954 * the table.
mbed_official 579:53297373a894 955 *
mbed_official 579:53297373a894 956 * <table>
mbed_official 579:53297373a894 957 * <tr>
mbed_official 579:53297373a894 958 * <th>Changelog</th>
mbed_official 579:53297373a894 959 * </tr>
mbed_official 579:53297373a894 960 * \if DEVICE_SAML21_SUPPORT
mbed_official 579:53297373a894 961 * <tr>
mbed_official 579:53297373a894 962 * <td>Initial Release</td>
mbed_official 579:53297373a894 963 * </tr>
mbed_official 579:53297373a894 964 * \else
mbed_official 579:53297373a894 965 * <tr>
mbed_official 579:53297373a894 966 * <td>Added support for SAMR21</td>
mbed_official 579:53297373a894 967 * </tr>
mbed_official 579:53297373a894 968 * <tr>
mbed_official 579:53297373a894 969 * <td>Added support for SAMD21 and new DMA quick start guide</td>
mbed_official 579:53297373a894 970 * </tr>
mbed_official 579:53297373a894 971 * <tr>
mbed_official 579:53297373a894 972 * <td>Added ADC calibration constant loading from the device signature
mbed_official 579:53297373a894 973 * row when the module is initialized</td>
mbed_official 579:53297373a894 974 * </tr>
mbed_official 579:53297373a894 975 * <tr>
mbed_official 579:53297373a894 976 * <td>Initial Release</td>
mbed_official 579:53297373a894 977 * </tr>
mbed_official 579:53297373a894 978 * \endif
mbed_official 579:53297373a894 979 * </table>
mbed_official 579:53297373a894 980 */
mbed_official 579:53297373a894 981
mbed_official 579:53297373a894 982 /**
mbed_official 579:53297373a894 983 * \page asfdoc_sam0_adc_exqsg Examples for ADC Driver
mbed_official 579:53297373a894 984 *
mbed_official 579:53297373a894 985 * This is a list of the available Quick Start guides (QSGs) and example
mbed_official 579:53297373a894 986 * applications for \ref asfdoc_sam0_adc_group. QSGs are simple examples with
mbed_official 579:53297373a894 987 * step-by-step instructions to configure and use this driver in a selection of
mbed_official 579:53297373a894 988 * use cases. Note that QSGs can be compiled as a standalone application or be
mbed_official 579:53297373a894 989 * added to the user application.
mbed_official 579:53297373a894 990 *
mbed_official 579:53297373a894 991 * - \subpage asfdoc_sam0_adc_basic_use_case
mbed_official 579:53297373a894 992 * \if ADC_CALLBACK_MODE
mbed_official 579:53297373a894 993 * - \subpage asfdoc_sam0_adc_basic_use_case_callback
mbed_official 579:53297373a894 994 * \endif
mbed_official 579:53297373a894 995 * - \subpage asfdoc_sam0_adc_dma_use_case
mbed_official 579:53297373a894 996 *
mbed_official 579:53297373a894 997 * \page asfdoc_sam0_adc_document_revision_history Document Revision History
mbed_official 579:53297373a894 998 *
mbed_official 579:53297373a894 999 * <table>
mbed_official 579:53297373a894 1000 * <tr>
mbed_official 579:53297373a894 1001 * <th>Doc. Rev.</td>
mbed_official 579:53297373a894 1002 * <th>Date</td>
mbed_official 579:53297373a894 1003 * <th>Comments</td>
mbed_official 579:53297373a894 1004 * </tr>
mbed_official 579:53297373a894 1005 * \if DEVICE_SAML21_SUPPORT
mbed_official 579:53297373a894 1006 * <tr>
mbed_official 579:53297373a894 1007 * <td>A</td>
mbed_official 579:53297373a894 1008 * <td>11/2014</td>
mbed_official 579:53297373a894 1009 * <td>Initial release.</td>
mbed_official 579:53297373a894 1010 * </tr>
mbed_official 579:53297373a894 1011 * \else
mbed_official 579:53297373a894 1012 * <tr>
mbed_official 579:53297373a894 1013 * <td>D</td>
mbed_official 579:53297373a894 1014 * <td>12/2014</td>
mbed_official 579:53297373a894 1015 * <td>Added support for SAMR21 and SAMD10/D11.</td>
mbed_official 579:53297373a894 1016 * </tr>
mbed_official 579:53297373a894 1017 * <tr>
mbed_official 579:53297373a894 1018 * <td>C</td>
mbed_official 579:53297373a894 1019 * <td>01/2014</td>
mbed_official 579:53297373a894 1020 * <td>Added support for SAMD21.</td>
mbed_official 579:53297373a894 1021 * </tr>
mbed_official 579:53297373a894 1022 * <tr>
mbed_official 579:53297373a894 1023 * <td>B</td>
mbed_official 579:53297373a894 1024 * <td>06/2013</td>
mbed_official 579:53297373a894 1025 * <td>Added additional documentation on the event system. Corrected
mbed_official 579:53297373a894 1026 * documentation typos.</td>
mbed_official 579:53297373a894 1027 * </tr>
mbed_official 579:53297373a894 1028 * <tr>
mbed_official 579:53297373a894 1029 * <td>A</td>
mbed_official 579:53297373a894 1030 * <td>06/2013</td>
mbed_official 579:53297373a894 1031 * <td>Initial release</td>
mbed_official 579:53297373a894 1032 * </tr>
mbed_official 579:53297373a894 1033 * \endif
mbed_official 579:53297373a894 1034 * </table>
mbed_official 579:53297373a894 1035 */
mbed_official 579:53297373a894 1036
mbed_official 579:53297373a894 1037 #endif /* ADC_H_INCLUDED */