Use configuration file instead appconfig. Reformat. Clean up. Remove CVS logs.
[bertos.git] / bertos / cpu / avr / drv / adc_avr.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \brief ADC hardware-specific definition
33  *
34  * \version $Id$
35  * \author Francesco Sacchi <batt@develer.com>
36  */
37
38 #include "adc_avr.h"
39
40 #include <cfg/cfg_adc.h>
41 #include <cfg/cfg_kern.h>
42 #include <cfg/macros.h>
43 #include <cfg/compiler.h>
44
45 #include <drv/adc.h>
46
47 #include <avr/io.h>
48 #include <avr/interrupt.h>
49
50 #define ADC_AVR_AREF   0
51 #define ADC_AVR_AVCC   1
52 #define ADC_AVR_INT256 2
53
54 #if CONFIG_KERNEL
55         #include <cfg/module.h>
56         #include <kern/proc.h>
57         #include <kern/signal.h>
58
59
60         #if !CONFIG_KERN_SIGNALS
61                 #error Signals must be active to use ADC with kernel
62         #endif
63
64         /* Signal adc convertion end */
65         #define SIG_ADC_COMPLETE SIG_SINGLE
66
67         /* ADC waiting process */
68         static struct Process *adc_process;
69
70         /**
71          * ADC ISR.
72          * Simply signal the adc process that convertion is complete.
73          */
74         ISR(ADC_vect)
75         {
76                 sig_signal(adc_process, SIG_ADC_COMPLETE);
77         }
78 #endif /* CONFIG_KERNEL */
79
80 /**
81  * Select mux channel \a ch.
82  * \todo only first 8 channels are selectable!
83  */
84 INLINE void adc_hw_select_ch(uint8_t ch)
85 {
86         /* Set to 0 all mux registers */
87         ADMUX &= ~(BV(MUX3) | BV(MUX3) | BV(MUX2) | BV(MUX1) | BV(MUX0));
88
89         /* Select channel, only first 8 channel modes are supported for now */
90         ADMUX |= (ch & 0x07);
91 }
92
93
94 /**
95  * Start an ADC convertion.
96  * If a kernel is present, preempt until convertion is complete, otherwise
97  * a busy wait on ADCS bit is done.
98  */
99 INLINE uint16_t adc_hw_read(void)
100 {
101         // Ensure another convertion is not running.
102         ASSERT(!(ADCSRA & BV(ADSC)));
103
104         // Start convertion
105         ADCSRA |= BV(ADSC);
106
107         #if CONFIG_KERNEL
108                 // Ensure IRQs enabled.
109                 ASSERT(IRQ_ENABLED());
110                 adc_process = proc_current();
111                 sig_wait(SIG_ADC_COMPLETE);
112         #else
113                 //Wait in polling until is done
114                 while (ADCSRA & BV(ADSC)) ;
115         #endif
116
117         return(ADC);
118 }
119
120 /**
121  * Init ADC hardware.
122  */
123 INLINE void adc_hw_init(void)
124 {
125         /*
126          * Select channel 0 as default,
127          * result right adjusted.
128          */
129         ADMUX = 0;
130
131         #if CONFIG_ADC_AVR_REF == ADC_AVR_AREF
132                 /* External voltage at AREF as analog ref source */
133                 /* None */
134         #elif CONFIG_ADC_AVR_REF == ADC_AVR_AVCC
135                 /* AVCC as analog ref source */
136                 ADMUX |= BV(REFS0);
137         #elif CONFIG_ADC_AVR_REF == ADC_AVR_INT256
138                 /* Internal 2.56V as ref source */
139                 ADMUX |= BV(REFS1) | BV(REFS0);
140         #else
141                 #error Unsupported ADC ref value.
142         #endif
143
144         /* Disable Auto trigger source: ADC in Free running mode. */
145         ADCSRB = 0;
146         
147         /* Enable ADC, disable autotrigger mode. */
148         ADCSRA = BV(ADEN);
149
150         #if CONFIG_KERNEL
151                 MOD_CHECK(proc);
152                 ADCSRA |= BV(ADIE);
153         #endif
154
155         /* Set convertion frequency */
156         #if CONFIG_ADC_AVR_DIVISOR == 2
157                 ADCSRA |= BV(ADPS0);
158         #elif CONFIG_ADC_AVR_DIVISOR == 4
159                 ADCSRA |= BV(ADPS1);
160         #elif CONFIG_ADC_AVR_DIVISOR == 8
161                 ADCSRA |= BV(ADPS1) | BV(ADPS0);
162         #elif CONFIG_ADC_AVR_DIVISOR == 16
163                 ADCSRA |= BV(ADPS2);
164         #elif CONFIG_ADC_AVR_DIVISOR == 32
165                 ADCSRA |= BV(ADPS2) | BV(ADPS0);
166         #elif CONFIG_ADC_AVR_DIVISOR == 64
167                 ADCSRA |= BV(ADPS2) | BV(ADPS1);
168         #elif CONFIG_ADC_AVR_DIVISOR == 128
169                 ADCSRA |= BV(ADPS2) | BV(ADPS1) | BV(ADPS0);
170         #else
171                 #error Unsupported ADC prescaler value.
172         #endif
173
174         /* Start a convertion to init ADC hw */
175         adc_hw_read();
176 }