Contiki 2.5
adc-drv.h
Go to the documentation of this file.
1 /* Copyright (c) 2010, Ulf Kulau
2  *
3  * Permission is hereby granted, free of charge, to any person
4  * obtaining a copy of this software and associated documentation
5  * files (the "Software"), to deal in the Software without
6  * restriction, including without limitation the rights to use,
7  * copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following
10  * conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 /**
26  * \mainpage
27  *
28  * \section about About
29  * In addition to the IBRnode sensor node hardware development, a software
30  * library for installed sensing and storage stuff was written. A short
31  * description and overview is given in chapter 'Software Peripherie Bibliothek'
32  * of the project thesis.
33  * The software library is divided into several layers, beginning with the low
34  * level hardware drivers. Theses drivers set up the communication devices like
35  * adc, i2c and spi. The interface layer holds the device specific information about
36  * register values and communication protocol. For each sensor and memory device, an
37  * interface uses the needed hardware drivers from the lower software layer. Note that
38  * this library is just a simple collection of modules to perform working with the
39  * the new developed IBRnode.
40  * \section usage Usage
41  * The software library is full compatible with contiki os and can be directly used
42  * in a process by adding the specific header files.
43  *
44  * \section lic License
45  *
46  * <pre>Copyright (c) 2009, Ulf Kulau <kulau@ibr.cs.tu-bs.de>
47  *
48  * Permission is hereby granted, free of charge, to any person
49  * obtaining a copy of this software and associated documentation
50  * files (the "Software"), to deal in the Software without
51  * restriction, including without limitation the rights to use,
52  * copy, modify, merge, publish, distribute, sublicense, and/or sell
53  * copies of the Software, and to permit persons to whom the
54  * Software is furnished to do so, subject to the following
55  * conditions:
56  *
57  * The above copyright notice and this permission notice shall be
58  * included in all copies or substantial portions of the Software.
59  *
60  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
61  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
62  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
63  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
64  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
65  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
66  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
67  * OTHER DEALINGS IN THE SOFTWARE.</pre>
68  */
69 
70 
71 /**
72  * \addtogroup Drivers
73  *
74  * \section about About
75  *
76  * These are the low level hardware drivers for the new developed sensor node,
77  * which was designed and developed by Ulf Kulau during his project thesis. The
78  * sensor node is based on the Atmel AVR Raven so please use the following
79  * datasheets for further information about the hardware:
80  * <ul>
81  * <li>ATmega164A/164PA/324A/324PA/644A/644PA/1284/1284P
82  * <li>AT86RF230
83  * </ul>
84  *
85  * \section usage Usage
86  *
87  * The hardware drivers are used to interface the various external peripherals.
88  *
89  * \section lic License
90  *
91  * <pre>Copyright (c) 2009, Ulf Kulau <kulau@ibr.cs.tu-bs.de>
92  *
93  * Permission is hereby granted, free of charge, to any person
94  * obtaining a copy of this software and associated documentation
95  * files (the "Software"), to deal in the Software without
96  * restriction, including without limitation the rights to use,
97  * copy, modify, merge, publish, distribute, sublicense, and/or sell
98  * copies of the Software, and to permit persons to whom the
99  * Software is furnished to do so, subject to the following
100  * conditions:
101  *
102  * The above copyright notice and this permission notice shall be
103  * included in all copies or substantial portions of the Software.
104  *
105  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
106  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
107  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
108  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
109  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
110  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
111  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
112  * OTHER DEALINGS IN THE SOFTWARE.</pre>
113  *
114  * @{
115  *
116  * \defgroup adc_driver Analog Digital Converter (ADC)
117  *
118  * <p>The ATmega1284p has 8 ADC-channels. You can run the ADC in
119  * different modes like just "single coversion", "free running" or
120  * "auto triggered" by timer or interrupts. Moreover it is possible
121  * to get differential values of two ADC-channels (with or without
122  * a gain factor)</p>
123  * @{
124  *
125  */
126 
127 /**
128  * \file
129  * ADC driver definitions
130  * \author
131  * Ulf Kulau <kulau@ibr.cs.tu-bs.de>
132  */
133 
134 #ifndef ADCCTR_H_
135 #define ADCCTR_H_
136 
137 #include <avr/io.h>
138 
139 
140 /********************************************************************
141  * ADC mode:
142  ********************************************************************/
143 
144 /*!
145  * mode: ADC Single Conversion
146  * \note The ADC will make just one conversion cycle (normal use)
147 */
148 #define ADC_SINGLE_CONVERSION 0xFF
149 /*!
150  * mode: ADC Free Running Mode
151  * \note The ADC runs all the time. An interrupt occurs whenever one ADC cycle
152  * will finish
153 */
154 #define ADC_FREE_RUNNING 0x00
155 /*!
156  * mode: ADC Analog Comparator
157  * \note Sets the ADC to comparator mode.
158 */
159 #define ADC_ANALOG_COMPARATOR (1 << ADTS0)
160 /*!
161  * mode: External Interrupt Trigger
162  * \note Enables the auto trigger mode. The ADC will start a conversion, whenever
163  * changes on external interrupt will occur.
164 */
165 #define ADC_EXT_INTERRUPT (1 << ADTS1)
166 /*!
167  * mode: Timer0 Compare Flag Trigger
168  * \note Enables the auto trigger mode. The ADC will start a conversion, whenever
169  * Timer0 Compare Flag is set.
170 */
171 #define ADC_TIMER0_COMP_FLAG ((1 << ADTS1) | (1 << ADTS0))
172 /*!
173  * mode: Timer0 Compare Flag Trigger
174  * \note Enables the auto trigger mode. The ADC will start a conversion, whenever
175  * Timer0 overflow occurs.
176 */
177 #define ADC_TIMER0_OVERFLOW (1 << ADTS2)
178 /*!
179  * mode: Timer1 Compare Flag Trigger
180  * \note Enables the auto trigger mode. The ADC will start a conversion, whenever
181  * Timer0 Compare Flag is set.
182 */
183 #define ADC_TIMER1_COMP_FLAG ((1 << ADTS2) | (1 << ADTS0))
184 /*!
185  * mode: Timer1 Compare Flag Trigger
186  * \note Enables the auto trigger mode. The ADC will start a conversion, whenever
187  * Timer0 overflow occurs.
188 */
189 #define ADC_TIMER1_OVERFLOW ((1 << ADTS2) | (1 << ADTS1))
190 /*!
191  * mode: Timer1 Compare Flag Trigger
192  * \note Enables the auto trigger mode. The ADC will start a conversion, whenever
193  * Timer0 Capture Flag is set.
194 */
195 #define ADC_TIMER1_CAPTURE ((1 << ADTS2) | (1 << ADTS1) | (1 << ADTS0))
196 
197 /********************************************************************
198  * ADC reference voltage source:
199  ********************************************************************/
200 
201 /*!
202  * ref: External Reference Voltage (Aref)
203  * \note The ADC reference voltage is provided by the external reference pin Aref
204 */
205 #define ADC_REF_AREF 0
206 /*!
207  * ref: Supply Voltage Reference (AVcc)
208  * \note The ADC reference voltage is provided by the supply voltage.
209 */
210 #define ADC_REF_AVCC (1 << REFS0)
211 /*!
212  * ref: Internal 1.1V Reference
213  * \note The ADC reference voltage is provided by internal 1.1V
214 */
215 #define ADC_REF_1100MV_INT (1 << REFS1)
216 /*!
217  * ref: Internal 2.56V Reference
218  * \note The ADC reference voltage is provided by internal 2.56V
219 */
220 #define ADC_REF_2560MV_INT ((1 << REFS1)| (1 << REFS0))
221 
222 
223 /*
224  * ADMUX - ADC Multiplexer Selection Register
225  */
226 
227 #define ADC_CHANNEL_0 0x00
228 #define ADC_CHANNEL_1 0x01
229 #define ADC_CHANNEL_2 0x02
230 #define ADC_CHANNEL_3 0x03
231 #define ADC_CHANNEL_4 0x04
232 #define ADC_CHANNEL_5 0x05
233 #define ADC_CHANNEL_6 0x06
234 #define ADC_CHANNEL_7 0x07
235 
236 #define ADC_DIFF_ADC0_ADC0_10X 0x08
237 #define ADC_DIFF_ADC1_ADC0_10X 0x09
238 #define ADC_DIFF_ADC0_ADC0_200X 0x0A
239 #define ADC_DIFF_ADC1_ADC0_200X 0x0B
240 #define ADC_DIFF_ADC2_ADC2_10X 0x0C
241 #define ADC_DIFF_ADC3_ADC2_10X 0x0D
242 #define ADC_DIFF_ADC2_ADC2_200X 0x0E
243 #define ADC_DIFF_ADC3_ADC2_200X 0x0F
244 #define ADC_DIFF_ADC0_ADC1_1X 0x10
245 #define ADC_DIFF_ADC1_ADC1_1X 0x11
246 #define ADC_DIFF_ADC2_ADC1_1X 0x12
247 #define ADC_DIFF_ADC3_ADC1_1X 0x13
248 #define ADC_DIFF_ADC4_ADC1_1X 0x14
249 #define ADC_DIFF_ADC5_ADC1_1X 0x15
250 #define ADC_DIFF_ADC6_ADC1_1X 0x16
251 #define ADC_DIFF_ADC7_ADC1_1X 0x17
252 #define ADC_DIFF_ADC0_ADC2_1X 0x18
253 #define ADC_DIFF_ADC1_ADC2_1X 0x19
254 #define ADC_DIFF_ADC2_ADC2_1X 0x1A
255 #define ADC_DIFF_ADC3_ADC2_1X 0x1B
256 #define ADC_DIFF_ADC4_ADC2_1X 0x1C
257 
258 
259 /*
260  * ADCSRA - ADC control and status register A
261  * \cond
262  */
263 #define ADC_ENABLE (1 << ADEN)
264 
265 #define ADC_START (1 << ADSC)
266 #define ADC_STOP 0x00
267 
268 #define ADC_LEFT_ADJUSTED (1 << ADLAR)
269 #define ADC_RIGHT_ADJUSTED (0 << ADLAR)
270 
271 #define ADC_TRIGGER_ENABLE (1 << ADATE)
272 #define ADC_TRIGGER_DISABLE (0 << ADATE)
273 
274 #define ADC_INTERRUPT_ENABLE (1 << ADIE)
275 #define ADC_INTERRUPT_DISABLE (0 << ADIE)
276 
277 #define ADC_PRESCALE_2 (1 << ADPS0)
278 #define ADC_PRESCALE_4 (1 << ADPS1)
279 #define ADC_PRESCALE_8 ((1 << ADPS0) | (1 << ADPS1))
280 #define ADC_PRESCALE_16 (1 << ADPS2)
281 #define ADC_PRESCALE_32 ((1 << ADPS2) | (1 << ADPS0))
282 #define ADC_PRESCALE_64 ((1 << ADPS2) | (1 << ADPS1)) /*choose this for F_CPU = 8MHZ*/
283 #define ADC_PRESCALE_128 ((1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0))
284 /*
285  * \endcond
286  */
287 
288 
289 
290 /**
291  * \brief Initialize the ADC module
292  *
293  * \param mode Select an ADC mode like single conversion or
294  * free running, etc.
295  * \param ref The ADC reference voltage source.
296  *
297  */
298 void adc_init(uint8_t mode, uint8_t ref);
299 
300 /**
301  * \brief This function returns the ADC data register
302  * value. Use adc_set_mux to preselect the ADC-channel
303  * or differential input.
304  *
305  * \return The ADC-value of preselected mux
306  *
307  */
308 uint16_t adc_get_value(void);
309 
310 /**
311  * \brief This function returns the ADC data register
312  * value of the given ADC channel.
313  *
314  * \param chn The ADC channel (ADC0 ... ADC7)
315  *
316  * \return The ADC-value
317  *
318  * This function is more efficient for the single
319  * conversion mode. Just select the ADC channel (or
320  * differential settings) and get the result.
321  */
322 uint16_t adc_get_value_from(uint8_t chn);
323 
324 /**
325  * \brief With this function you can set the ADX multiplexer.
326  * Here you select the ADC channel or choose differential
327  * value between two ADC-channels with or without gain.
328  *
329  * \param mux Select single ADC channel (ADC_CHANNEL_0 ... ADC_CHANNEL_7)
330  * or differential input of various ADC-channels with or
331  * without gain
332  *
333  */
334 void adc_set_mux(uint8_t mux);
335 
336 /**
337  * \brief This function stops all running ADC
338  *
339  */
340 void adc_deinit(void);
341 
342 
343 #endif /* ADCCTR_H_ */