adc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*****************************************************************************
  2. *
  3. * \file
  4. *
  5. * \brief ADC driver for AVR UC3.
  6. *
  7. * This file defines a useful set of functions for ADC on AVR UC3 devices.
  8. *
  9. * Copyright (c) 2009-2018 Microchip Technology Inc. and its subsidiaries.
  10. *
  11. * \asf_license_start
  12. *
  13. * \page License
  14. *
  15. * Subject to your compliance with these terms, you may use Microchip
  16. * software and any derivatives exclusively with Microchip products.
  17. * It is your responsibility to comply with third party license terms applicable
  18. * to your use of third party software (including open source software) that
  19. * may accompany Microchip software.
  20. *
  21. * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
  22. * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
  23. * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
  24. * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
  25. * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
  26. * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
  27. * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
  28. * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
  29. * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
  30. * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
  31. * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
  32. *
  33. * \asf_license_stop
  34. *
  35. *****************************************************************************/
  36. /*
  37. * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
  38. */
  39. #include <avr32/io.h>
  40. #include "compiler.h"
  41. #include "adc.h"
  42. /** \brief Configure ADC. Mandatory to call.
  43. * If not called, ADC channels will have side effects
  44. *
  45. * \param *adc Base address of the ADC
  46. */
  47. void adc_configure(volatile avr32_adc_t *adc)
  48. {
  49. Assert( adc != NULL );
  50. #ifdef USE_ADC_8_BITS
  51. adc->mr |= 1 << AVR32_ADC_LOWRES_OFFSET;
  52. #endif
  53. /* Set Sample/Hold time to max so that the ADC capacitor should be
  54. * loaded entirely */
  55. adc->mr |= 0xF << AVR32_ADC_SHTIM_OFFSET;
  56. /* Set Startup to max so that the ADC capacitor should be loaded
  57. * entirely */
  58. adc->mr |= 0x1F << AVR32_ADC_STARTUP_OFFSET;
  59. }
  60. /** \brief Start analog to digital conversion
  61. * \param *adc Base address of the ADC
  62. */
  63. void adc_start(volatile avr32_adc_t *adc)
  64. {
  65. Assert( adc != NULL );
  66. /* start conversion */
  67. adc->cr = AVR32_ADC_START_MASK;
  68. }
  69. /** \brief Enable channel
  70. *
  71. * \param *adc Base address of the ADC
  72. * \param channel channel to enable (0 to 7)
  73. */
  74. void adc_enable(volatile avr32_adc_t *adc, uint16_t channel)
  75. {
  76. Assert( adc != NULL );
  77. Assert( channel <= AVR32_ADC_CHANNELS_MSB ); /* check if channel exist
  78. **/
  79. /* enable channel */
  80. adc->cher = (1 << channel);
  81. }
  82. /** \brief Disable channel
  83. *
  84. * \param *adc Base address of the ADC
  85. * \param channel channel to disable (0 to 7)
  86. */
  87. void adc_disable(volatile avr32_adc_t *adc, uint16_t channel)
  88. {
  89. Assert( adc != NULL );
  90. Assert( channel <= AVR32_ADC_CHANNELS_MSB ); /* check if channel exist
  91. **/
  92. if (adc_get_status(adc, channel) == true) {
  93. /* disable channel */
  94. adc->chdr |= (1 << channel);
  95. }
  96. }
  97. /** \brief Get channel 0 to 7 status
  98. *
  99. * \param *adc Base address of the ADC
  100. * \param channel channel to handle (0 to 7)
  101. * \return bool true if channel is enabled
  102. * false if channel is disabled
  103. */
  104. bool adc_get_status(volatile avr32_adc_t *adc, uint16_t channel)
  105. {
  106. Assert( adc != NULL );
  107. Assert( channel <= AVR32_ADC_CHANNELS_MSB ); /* check if channel exist
  108. **/
  109. return ((adc->chsr & (1 << channel)) ? true : false);
  110. }
  111. /** \brief Check channel conversion status
  112. *
  113. * \param *adc Base address of the ADC
  114. * \param channel channel to check (0 to 7)
  115. * \return bool true if conversion not running
  116. * false if conversion running
  117. */
  118. bool adc_check_eoc(volatile avr32_adc_t *adc, uint16_t channel)
  119. {
  120. Assert( adc != NULL );
  121. Assert( channel <= AVR32_ADC_CHANNELS_MSB ); /* check if channel exist
  122. **/
  123. /* get SR register : EOC bit for channel */
  124. return ((adc->sr & (1 << channel)) ? true : false);
  125. }
  126. /** \brief Check channel conversion overrun error
  127. *
  128. * \param *adc Base address of the ADC
  129. * \param channel channel to check (0 to 7)
  130. * \return bool FAIL if an error occurred
  131. * PASS if no error occurred
  132. */
  133. bool adc_check_ovr(volatile avr32_adc_t *adc, uint16_t channel)
  134. {
  135. Assert( adc != NULL );
  136. Assert( channel <= AVR32_ADC_CHANNELS_MSB ); /* check if channel exist
  137. **/
  138. /* get SR register : OVR bit for channel */
  139. return ((adc->sr & (1 << (channel + 8))) ? FAIL : PASS);
  140. }
  141. /** \brief Get channel value
  142. *
  143. * \param *adc Base address of the ADC
  144. * \param channel channel to handle (0 to 7)
  145. * \return The value acquired (unsigned long)
  146. */
  147. uint32_t adc_get_value(volatile avr32_adc_t *adc, uint16_t channel)
  148. {
  149. Assert( adc != NULL );
  150. Assert( channel <= AVR32_ADC_CHANNELS_MSB );
  151. /* wait for end of conversion */
  152. while (adc_check_eoc(adc, channel) != true) {
  153. }
  154. return *((uint32_t *)((&(adc->cdr0)) + channel));
  155. }
  156. /** \brief Wait for the next converted data and return its value
  157. *
  158. * \param *adc Base address of the ADC
  159. * \return The latest converted value (unsigned long)
  160. */
  161. uint32_t adc_get_latest_value(volatile avr32_adc_t *adc)
  162. {
  163. Assert( adc != NULL );
  164. /* Wait for the data ready flag */
  165. while ((adc->sr & AVR32_ADC_DRDY_MASK) != AVR32_ADC_DRDY_MASK) {
  166. }
  167. return adc->lcdr;
  168. }