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 
40 #include <avr32/io.h>
41 #include "compiler.h"
42 #include "adc.h"
43 
44 /** \brief Configure ADC. Mandatory to call.
45  * If not called, ADC channels will have side effects
46  *
47  * \param *adc Base address of the ADC
48  */
adc_configure(volatile avr32_adc_t * adc)49 void adc_configure(volatile avr32_adc_t *adc)
50 {
51 	Assert( adc != NULL );
52 
53 #ifdef USE_ADC_8_BITS
54 	adc->mr |= 1 << AVR32_ADC_LOWRES_OFFSET;
55 #endif
56 
57 	/* Set Sample/Hold time to max so that the ADC capacitor should be
58 	 * loaded entirely */
59 	adc->mr |= 0xF << AVR32_ADC_SHTIM_OFFSET;
60 
61 	/* Set Startup to max so that the ADC capacitor should be loaded
62 	 * entirely */
63 	adc->mr |= 0x1F << AVR32_ADC_STARTUP_OFFSET;
64 }
65 
66 /** \brief Start analog to digital conversion
67  * \param *adc Base address of the ADC
68  */
adc_start(volatile avr32_adc_t * adc)69 void adc_start(volatile avr32_adc_t *adc)
70 {
71 	Assert( adc != NULL );
72 
73 	/* start conversion */
74 	adc->cr = AVR32_ADC_START_MASK;
75 }
76 
77 /** \brief Enable channel
78  *
79  * \param *adc Base address of the ADC
80  * \param  channel   channel to enable (0 to 7)
81  */
adc_enable(volatile avr32_adc_t * adc,uint16_t channel)82 void adc_enable(volatile avr32_adc_t *adc, uint16_t channel)
83 {
84 	Assert( adc != NULL );
85 	Assert( channel <= AVR32_ADC_CHANNELS_MSB ); /* check if channel exist
86 	                                              **/
87 
88 	/* enable channel */
89 	adc->cher = (1 << channel);
90 }
91 
92 /** \brief Disable channel
93  *
94  * \param *adc Base address of the ADC
95  * \param  channel   channel to disable (0 to 7)
96  */
adc_disable(volatile avr32_adc_t * adc,uint16_t channel)97 void adc_disable(volatile avr32_adc_t *adc, uint16_t channel)
98 {
99 	Assert( adc != NULL );
100 	Assert( channel <= AVR32_ADC_CHANNELS_MSB ); /* check if channel exist
101 	                                              **/
102 
103 	if (adc_get_status(adc, channel) == true) {
104 		/* disable channel */
105 		adc->chdr |= (1 << channel);
106 	}
107 }
108 
109 /** \brief Get channel 0 to 7 status
110  *
111  * \param *adc Base address of the ADC
112  * \param  channel   channel to handle (0 to 7)
113  * \return bool      true if channel is enabled
114  *                   false if channel is disabled
115  */
adc_get_status(volatile avr32_adc_t * adc,uint16_t channel)116 bool adc_get_status(volatile avr32_adc_t *adc, uint16_t channel)
117 {
118 	Assert( adc != NULL );
119 	Assert( channel <= AVR32_ADC_CHANNELS_MSB ); /* check if channel exist
120 	                                              **/
121 
122 	return ((adc->chsr & (1 << channel)) ? true : false);
123 }
124 
125 /** \brief Check channel conversion status
126  *
127  * \param *adc Base address of the ADC
128  * \param  channel   channel to check (0 to 7)
129  * \return bool      true if conversion not running
130  *                   false if conversion running
131  */
adc_check_eoc(volatile avr32_adc_t * adc,uint16_t channel)132 bool adc_check_eoc(volatile avr32_adc_t *adc, uint16_t channel)
133 {
134 	Assert( adc != NULL );
135 	Assert( channel <= AVR32_ADC_CHANNELS_MSB ); /* check if channel exist
136 	                                              **/
137 
138 	/* get SR register : EOC bit for channel */
139 	return ((adc->sr & (1 << channel)) ? true : false);
140 }
141 
142 /** \brief Check channel conversion overrun error
143  *
144  * \param *adc Base address of the ADC
145  * \param  channel   channel to check (0 to 7)
146  * \return bool      FAIL if an error occurred
147  *                   PASS if no error occurred
148  */
adc_check_ovr(volatile avr32_adc_t * adc,uint16_t channel)149 bool adc_check_ovr(volatile avr32_adc_t *adc, uint16_t channel)
150 {
151 	Assert( adc != NULL );
152 	Assert( channel <= AVR32_ADC_CHANNELS_MSB ); /* check if channel exist
153 	                                              **/
154 
155 	/* get SR register : OVR bit for channel */
156 	return ((adc->sr & (1 << (channel + 8))) ? FAIL : PASS);
157 }
158 
159 /** \brief Get channel value
160  *
161  * \param *adc Base address of the ADC
162  * \param  channel   channel to handle (0 to 7)
163  * \return The value acquired (unsigned long)
164  */
adc_get_value(volatile avr32_adc_t * adc,uint16_t channel)165 uint32_t adc_get_value(volatile avr32_adc_t *adc, uint16_t channel)
166 {
167 	Assert( adc != NULL );
168 	Assert( channel <= AVR32_ADC_CHANNELS_MSB );
169 
170 	/* wait for end of conversion */
171 	while (adc_check_eoc(adc, channel) != true) {
172 	}
173 
174 	return *((uint32_t *)((&(adc->cdr0)) + channel));
175 }
176 
177 /** \brief Wait for the next converted data and return its value
178  *
179  * \param *adc Base address of the ADC
180  * \return The latest converted value (unsigned long)
181  */
adc_get_latest_value(volatile avr32_adc_t * adc)182 uint32_t adc_get_latest_value(volatile avr32_adc_t *adc)
183 {
184 	Assert( adc != NULL );
185 
186 	/* Wait for the data ready flag */
187 	while ((adc->sr & AVR32_ADC_DRDY_MASK) != AVR32_ADC_DRDY_MASK) {
188 	}
189 
190 	return adc->lcdr;
191 }
192