adc.h 33 KB

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