Robobo
adc.h
1 /* ----------------------------------------------------------------------------
2  * SAM Software Package License
3  * ----------------------------------------------------------------------------
4  * Copyright (c) 2011-2012, Atmel Corporation
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following condition is met:
10  *
11  * - Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the disclaimer below.
13  *
14  * Atmel's name may not be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  * ----------------------------------------------------------------------------
28  */
29 
30 #ifndef ADC_H_INCLUDED
31 #define ADC_H_INCLUDED
32 
33 #include "../chip.h"
34 
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 
43 /* The max adc sample freq definition*/
44 #define ADC_FREQ_MAX 20000000
45 /* The min adc sample freq definition*/
46 #define ADC_FREQ_MIN 1000000
47 /* The normal adc startup time*/
48 #define ADC_STARTUP_NORM 40
49 /* The fast adc startup time*/
50 #define ADC_STARTUP_FAST 12
51 
52 /* Definitions for ADC resolution */
53 #if SAM3S_SERIES || SAM4S_SERIES || SAM3XA_SERIES
54 enum adc_resolution_t {
55  ADC_10_BITS = ADC_MR_LOWRES_BITS_10, /* ADC 10-bit resolution */
56  ADC_12_BITS = ADC_MR_LOWRES_BITS_12 /* ADC 12-bit resolution */
57 };
58 #elif SAM3N_SERIES
59 enum adc_resolution_t {
60  ADC_8_BITS = ADC_MR_LOWRES_BITS_8, /* ADC 8-bit resolution */
61  ADC_10_BITS = ADC_MR_LOWRES_BITS_10 /* ADC 10-bit resolution */
62 } ;
63 #elif SAM3U_SERIES
64 enum adc_resolution_t {
65  ADC_8_BITS = ADC_MR_LOWRES_BITS_8, /* ADC 8-bit resolution */
66  ADC_10_BITS = ADC12B_MR_LOWRES_BITS_10, /* ADC 10-bit resolution */
67  ADC_12_BITS = ADC12B_MR_LOWRES_BITS_12 /* ADC 12-bit resolution */
68 } ;
69 #endif
70 
71 /* Definitions for ADC trigger */
72 enum adc_trigger_t {
73  ADC_TRIG_SW = ADC_MR_TRGEN_DIS, /* Starting a conversion is only possible by software. */
74  ADC_TRIG_EXT = ((ADC_MR_TRGSEL_ADC_TRIG0 << ADC_MR_TRGSEL_Pos) &
75  ADC_MR_TRGSEL_Msk) | ADC_MR_TRGEN, /* External trigger */
76  ADC_TRIG_TIO_CH_0 = (ADC_MR_TRGSEL_ADC_TRIG1 & ADC_MR_TRGSEL_Msk) |
77  ADC_MR_TRGEN, /* TIO Output of the Timer Counter Channel 0 */
78  ADC_TRIG_TIO_CH_1 = (ADC_MR_TRGSEL_ADC_TRIG2 & ADC_MR_TRGSEL_Msk) |
79  ADC_MR_TRGEN, /* TIO Output of the Timer Counter Channel 1 */
80  ADC_TRIG_TIO_CH_2 = (ADC_MR_TRGSEL_ADC_TRIG3 & ADC_MR_TRGSEL_Msk) |
81  ADC_MR_TRGEN, /* TIO Output of the Timer Counter Channel 2 */
82 #if SAM3S_SERIES || SAM4S_SERIES || SAM3XA_SERIES || SAM3U_SERIES
83  ADC_TRIG_PWM_EVENT_LINE_0 = (ADC_MR_TRGSEL_ADC_TRIG4 & ADC_MR_TRGSEL_Msk) |
84  ADC_MR_TRGEN, /* PWM Event Line 0 */
85  ADC_TRIG_PWM_EVENT_LINE_1 = (ADC_MR_TRGSEL_ADC_TRIG5 & ADC_MR_TRGSEL_Msk) |
86  ADC_MR_TRGEN /* PWM Event Line 1 */
87 #endif
88 } ;
89 
90 #if SAM3S_SERIES || SAM4S_SERIES || SAM3N_SERIES || SAM3XA_SERIES
91 /* Definitions for ADC channel number */
92 enum adc_channel_num_t {
93  ADC_CHANNEL_0 = 0,
94  ADC_CHANNEL_1 = 1,
95  ADC_CHANNEL_2 = 2,
96  ADC_CHANNEL_3 = 3,
97  ADC_CHANNEL_4 = 4,
98  ADC_CHANNEL_5 = 5,
99  ADC_CHANNEL_6 = 6,
100  ADC_CHANNEL_7 = 7,
101  ADC_CHANNEL_8 = 8,
102  ADC_CHANNEL_9 = 9,
103  ADC_CHANNEL_10 = 10,
104  ADC_CHANNEL_11 = 11,
105  ADC_CHANNEL_12 = 12,
106  ADC_CHANNEL_13 = 13,
107  ADC_CHANNEL_14 = 14,
108  ADC_TEMPERATURE_SENSOR = 15,
109 } ;
110 #elif SAM3U_SERIES
111 /* Definitions for ADC channel number */
112 enum adc_channel_num_t {
113  ADC_CHANNEL_0 = 0,
114  ADC_CHANNEL_1 = 1,
115  ADC_CHANNEL_2 = 2,
116  ADC_CHANNEL_3 = 3,
117  ADC_CHANNEL_4 = 4,
118  ADC_CHANNEL_5 = 5,
119  ADC_CHANNEL_6 = 6,
120  ADC_CHANNEL_7 = 7,
121 } ;
122 #endif
123 /* Definitions for ADC gain value */
124 enum adc_gainvalue_t{
125  ADC_GAINVALUE_0 = 0,
126  ADC_GAINVALUE_1 = 1,
127  ADC_GAINVALUE_2 = 2,
128  ADC_GAINVALUE_3 = 3
129 };
130 /* Definitions for ADC analog settling time */
131 #if SAM3S_SERIES || SAM4S_SERIES || SAM3XA_SERIES
132 enum adc_settling_time_t{
133  ADC_SETTLING_TIME_0 = ADC_MR_SETTLING_AST3,
134  ADC_SETTLING_TIME_1 = ADC_MR_SETTLING_AST5,
135  ADC_SETTLING_TIME_2 = ADC_MR_SETTLING_AST9,
136  ADC_SETTLING_TIME_3 = ADC_MR_SETTLING_AST17
137 };
138 #endif
139 
140 #if SAM3S_SERIES || SAM4S_SERIES || SAM3N_SERIES || SAM3XA_SERIES
141 uint32_t adc_init(Adc *p_adc, const uint32_t ul_mck,
142  const uint32_t ul_adc_clock, const uint8_t uc_startup);
143 void adc_configure_trigger(Adc *p_adc, const enum adc_trigger_t trigger,
144  const uint8_t uc_freerun);
145 void adc_configure_power_save(Adc *p_adc, const uint8_t uc_sleep, const uint8_t uc_fwup);
146 void adc_configure_sequence(Adc *p_adc, const enum adc_channel_num_t ch_list[],
147  const uint8_t uc_num);
148 void adc_enable_tag(Adc *p_adc);
149 void adc_disable_tag(Adc *p_adc);
150 enum adc_channel_num_t adc_get_tag(const Adc *p_adc);
151 void adc_start_sequencer(Adc *p_adc);
152 void adc_stop_sequencer(Adc *p_adc);
153 void adc_set_comparison_mode(Adc *p_adc, const uint8_t uc_mode);
154 uint32_t adc_get_comparison_mode(const Adc *p_adc);
155 void adc_set_comparison_window(Adc *p_adc, const uint16_t us_low_threshold,
156  const uint16_t us_high_threshold);
157 void adc_set_comparison_channel(Adc *p_adc, const enum adc_channel_num_t channel);
158 void adc_set_writeprotect(Adc *p_adc, const uint32_t ul_enable);
159 uint32_t adc_get_writeprotect_status(const Adc *p_adc);
160 void adc_check(Adc* p_adc, const uint32_t ul_mck);
161 uint32_t adc_get_overrun_status(const Adc *p_adc);
162 #elif SAM3U_SERIES
163 uint32_t adc_init(Adc * p_adc, const uint32_t ul_mck, const uint32_t ul_adc_clock,
164  const uint32_t ul_startuptime);
165 void adc_configure_trigger(Adc *p_adc, const enum adc_trigger_t trigger);
166 void adc_configure_power_save(Adc *p_adc, const uint8_t uc_sleep);
167 #endif
168 
169 void adc_set_resolution(Adc *p_adc, const enum adc_resolution_t resolution);
170 void adc_start(Adc *p_adc);
171 void adc_stop(Adc *p_adc);
172 void adc_enable_channel(Adc *p_adc, const enum adc_channel_num_t adc_ch);
173 void adc_disable_channel(Adc *p_adc, const enum adc_channel_num_t adc_ch);
174 void adc_enable_all_channel(Adc *p_adc);
175 void adc_disable_all_channel(Adc *p_adc);
176 uint32_t adc_get_channel_status(const Adc *p_adc, const enum adc_channel_num_t adc_ch);
177 uint32_t adc_get_channel_value(const Adc *p_adc,const enum adc_channel_num_t adc_ch);
178 uint32_t adc_get_latest_value(const Adc *p_adc);
179 uint32_t adc_get_actual_adc_clock(const Adc *p_adc, const uint32_t ul_mck);
180 void adc_enable_interrupt(Adc *p_adc, const uint32_t ul_source);
181 void adc_disable_interrupt(Adc *p_adc, const uint32_t ul_source);
182 uint32_t adc_get_status(const Adc *p_adc);
183 uint32_t adc_get_interrupt_mask(const Adc *p_adc);
184 Pdc *adc_get_pdc_base(const Adc *p_adc);
185 
186 #if SAM3S_SERIES || SAM4S_SERIES || SAM3XA_SERIES
187 void adc_configure_timing(Adc *p_adc, const uint8_t uc_tracking,
188  const enum adc_settling_time_t settling, const uint8_t uc_transfer);
189 void adc_enable_anch( Adc *p_adc );
190 void adc_disable_anch( Adc *p_adc );
191 void adc_enable_channel_differential_input(Adc *p_adc, const enum adc_channel_num_t channel);
192 void adc_disable_channel_differential_input(Adc *p_adc, const enum adc_channel_num_t channel);
193 void adc_enable_channel_input_offset(Adc *p_adc, const enum adc_channel_num_t channel);
194 void adc_disable_channel_input_offset(Adc *p_adc, const enum adc_channel_num_t channel);
195 void adc_set_channel_input_gain(Adc *p_adc, const enum adc_channel_num_t channel,
196  const enum adc_gainvalue_t uc_gain);
197 void adc_set_bias_current(Adc *p_adc, const uint8_t uc_ibctl);
198 void adc_enable_ts(Adc *p_adc);
199 void adc_disable_ts(Adc *p_adc);
200 #elif SAM3N_SERIES
201 void adc_configure_timing(Adc *p_adc, const uint8_t uc_tracking);
202 #elif SAM3U_SERIES
203 void adc_configure_timing(Adc *p_adc, const uint32_t ul_sh);
204 #endif
205 
206 #if SAM3SD8_SERIES || SAM4S_SERIES
207 void adc_set_calibmode(Adc *p_adc);
208 #endif
209 
210 #if SAM3U_SERIES
211 uint32_t adc12b_init(Adc12b *p_adc, const uint32_t ul_mck, const uint32_t ul_adc_clock,
212  const uint32_t ul_startuptime, const uint32_t ul_offmode_startuptime);
213 void adc12b_set_resolution(Adc12b *p_adc, const enum adc_resolution_t resolution);
214 void adc12b_configure_trigger(Adc12b *p_adc, const enum adc_trigger_t trigger);
215 void adc12b_configure_power_save(Adc12b *p_adc, const uint8_t uc_sleep, const uint8_t uc_offmode);
216 void adc12b_configure_timing(Adc12b *p_adc, const uint32_t ul_sh);
217 void adc12b_start(Adc12b *p_adc);
218 void adc12b_stop(Adc12b *p_adc);
219 void adc12b_enable_channel(Adc12b *p_adc, const enum adc_channel_num_t adc_ch);
220 void adc12b_disable_channel(Adc12b *p_adc, const enum adc_channel_num_t adc_ch);
221 void adc12b_enable_all_channel(Adc12b *p_adc);
222 void adc12b_disable_all_channel(Adc12b *p_adc);
223 uint32_t adc12b_get_channel_status(const Adc12b *p_adc,const enum adc_channel_num_t adc_ch);
224 uint32_t adc12b_get_channel_value(const Adc12b *p_adc, const enum adc_channel_num_t adc_ch);
225 uint32_t adc12b_get_latest_value(const Adc12b *p_adc);
226 void adc12b_enable_differential_input(Adc12b *p_adc);
227 void adc12b_disable_differential_input(Adc12b *p_adc);
228 void adc12b_enable_input_offset(Adc12b *p_adc);
229 void adc12b_disable_input_offset(Adc12b *p_adc);
230 void adc12b_set_input_gain(Adc12b *p_adc, const enum adc_gainvalue_t uc_gain);
231 uint32_t adc12b_get_actual_adc_clock(const Adc12b *p_adc, const uint32_t ul_mck);
232 void adc12b_enable_interrupt(Adc12b *p_adc, const uint32_t ul_source);
233 void adc12b_disable_interrupt(Adc12b *p_adc, const uint32_t ul_source);
234 uint32_t adc12b_get_interrupt_mask(const Adc12b *p_adc);
235 uint32_t adc12b_get_status(const Adc12b *p_adc);
236 void adc12b_set_bias_current(Adc12b *p_adc, const uint8_t uc_ibctl);
237 Pdc *adc12b_get_pdc_base(const Adc12b *p_adc);
238 #endif
239 
241 
242 #ifdef __cplusplus
243 }
244 #endif
245 
246 
248 #endif /* ADC_H_INCLUDED */
#define ADC_MR_TRGSEL_ADC_TRIG5
(ADC_MR) PWM Event Line 1
Definition: component_adc.h:92
Pdc * adc_get_pdc_base(const Adc *p_adc)
Get PDC registers base address.
Definition: adc.c:970
#define ADC12B_MR_LOWRES_BITS_10
(ADC12B_MR) 10-bit resolution
Definition: component_adc12b.h:86
#define ADC_MR_LOWRES_BITS_12
(ADC_MR) 12-bit resolution
Definition: component_adc.h:94
void adc_enable_all_channel(Adc *p_adc)
Enable all ADC channels.
Definition: adc.c:402
void adc_disable_all_channel(Adc *p_adc)
Disable all ADC channel.
Definition: adc.c:427
uint32_t adc_get_interrupt_mask(const Adc *p_adc)
Read ADC interrupt mask.
Definition: adc.c:781
#define ADC_MR_TRGEN
(ADC_MR) Trigger Enable
Definition: component_adc.h:79
void adc_disable_interrupt(Adc *p_adc, const uint32_t ul_source)
Disable ADC interrupts.
Definition: adc.c:731
uint32_t adc_get_channel_value(const Adc *p_adc, const enum adc_channel_num_t adc_ch)
Read the ADC result data of the specified channel.
Definition: adc.c:458
void adc_enable_channel(Adc *p_adc, const enum adc_channel_num_t adc_ch)
Enable the specified ADC channel.
Definition: adc.c:392
#define ADC_MR_SETTLING_AST3
(ADC_MR) 3 periods of ADCClock
Definition: component_adc.h:128
Pdc hardware registers.
Definition: component_pdc.h:41
#define ADC_MR_TRGSEL_ADC_TRIG2
(ADC_MR) TIO Output of the Timer Counter Channel 1
Definition: component_adc.h:86
void adc_set_resolution(Adc *p_adc, const enum adc_resolution_t resolution)
Configure the conversion resolution.
Definition: adc.c:176
#define ADC_MR_SETTLING_AST17
(ADC_MR) 17 periods of ADCClock
Definition: component_adc.h:131
#define ADC_MR_TRGEN_DIS
(ADC_MR) Hardware triggers are disabled. Starting a conversion is only possible by software...
Definition: component_adc.h:80
#define ADC12B_MR_LOWRES_BITS_12
(ADC12B_MR) 12-bit resolution
Definition: component_adc12b.h:85
#define ADC_MR_TRGSEL_ADC_TRIG1
(ADC_MR) TIO Output of the Timer Counter Channel 0
Definition: component_adc.h:85
void adc_disable_channel(Adc *p_adc, const enum adc_channel_num_t adc_ch)
Disable the specified ADC channel.
Definition: adc.c:417
#define ADC_MR_LOWRES_BITS_8
(ADC_MR) 8-bit resolution
Definition: component_adc.h:90
#define ADC_MR_TRGSEL_ADC_TRIG0
(ADC_MR) External trigger
Definition: component_adc.h:84
Adc12b hardware registers.
Definition: component_adc12b.h:41
void adc_start(Adc *p_adc)
Start analog-to-digital conversion.
Definition: adc.c:371
uint32_t adc_get_channel_status(const Adc *p_adc, const enum adc_channel_num_t adc_ch)
Read the ADC channel status.
Definition: adc.c:445
#define ADC_MR_TRGSEL_Msk
(ADC_MR) Trigger Selection
Definition: component_adc.h:83
#define ADC_MR_TRGSEL_ADC_TRIG4
(ADC_MR) PWM Event Line 0
Definition: component_adc.h:91
void adc_stop(Adc *p_adc)
Stop analog-to-digital conversion.
Definition: adc.c:381
#define ADC_MR_TRGSEL_ADC_TRIG3
(ADC_MR) TIO Output of the Timer Counter Channel 2
Definition: component_adc.h:87
#define ADC_MR_LOWRES_BITS_10
(ADC_MR) 10-bit resolution
Definition: component_adc.h:89
#define ADC_MR_SETTLING_AST5
(ADC_MR) 5 periods of ADCClock
Definition: component_adc.h:129
Adc hardware registers.
Definition: component_adc.h:41
uint32_t adc_get_actual_adc_clock(const Adc *p_adc, const uint32_t ul_mck)
Return the actual ADC clock.
Definition: adc.c:703
uint32_t adc_get_latest_value(const Adc *p_adc)
Read the last ADC result data.
Definition: adc.c:476
#define ADC_MR_SETTLING_AST9
(ADC_MR) 9 periods of ADCClock
Definition: component_adc.h:130
void adc_enable_interrupt(Adc *p_adc, const uint32_t ul_source)
Enable ADC interrupts.
Definition: adc.c:720