Fix ADC configurations.
[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  * This module is automatically included so no need to include
38  * in test list.
39  * notest: avr
40  *
41  * $WIZ$
42  */
43
44 #include "adc_avr.h"
45
46 #include "cfg/cfg_adc.h"
47 #include "cfg/cfg_proc.h"
48 #include "cfg/cfg_signal.h"
49 #include <cfg/macros.h>
50 #include <cfg/compiler.h>
51
52 #include <cpu/irq.h> // IRQ_ASSERT_ENABLED()
53
54 #include <drv/adc.h>
55
56 #include <avr/io.h>
57 #include <avr/interrupt.h>
58
59 /**
60  * ADC voltage referencese.
61  *
62  * $WIZ$ avr_adc_refs = "ADC_AVR_AREF", "ADC_AVR_AVCC", "ADC_AVR_INT256"
63  * \{
64  */
65 #define ADC_AVR_AREF   0
66 #define ADC_AVR_AVCC   1
67 #define ADC_AVR_INT256 2
68 /* \} */
69
70 #if CONFIG_KERN
71         #include <cfg/module.h>
72         #include <kern/proc.h>
73         #include <kern/signal.h>
74
75
76         #if !CONFIG_KERN_SIGNALS
77                 #error Signals must be active to use the ADC with kernel
78         #endif
79
80         /* Signal adc convertion end */
81         #define SIG_ADC_COMPLETE SIG_SINGLE
82
83         /* ADC waiting process */
84         static struct Process *adc_process;
85
86         /**
87          * ADC ISR.
88          * Simply signal the adc process that convertion is complete.
89          */
90         ISR(ADC_vect)
91         {
92                 sig_signal(adc_process, SIG_ADC_COMPLETE);
93         }
94 #endif /* CONFIG_KERN */
95
96 /**
97  * Select mux channel \a ch.
98  * \todo only first 8 channels are selectable!
99  */
100 INLINE void adc_hw_select_ch(uint8_t ch)
101 {
102         /* Set to 0 all mux registers */
103         ADMUX &= ~(BV(MUX4) | BV(MUX3) | BV(MUX2) | BV(MUX1) | BV(MUX0));
104
105         /* Select channel, only first 8 channel modes are supported for now */
106         ADMUX |= (ch & 0x07);
107 }
108
109
110 /**
111  * Start an ADC convertion.
112  * If a kernel is present, preempt until convertion is complete, otherwise
113  * a busy wait on ADCS bit is done.
114  */
115 INLINE uint16_t adc_hw_read(void)
116 {
117         // Ensure another convertion is not running.
118         ASSERT(!(ADCSRA & BV(ADSC)));
119
120         // Start convertion
121         ADCSRA |= BV(ADSC);
122
123         #if CONFIG_KERN
124                 // Ensure IRQs enabled.
125                 IRQ_ASSERT_ENABLED();
126                 adc_process = proc_current();
127                 sig_wait(SIG_ADC_COMPLETE);
128         #else
129                 //Wait in polling until is done
130                 while (ADCSRA & BV(ADSC)) ;
131         #endif
132
133         return(ADC);
134 }
135
136 /**
137  * Init ADC hardware.
138  */
139 INLINE void adc_hw_init(void)
140 {
141         /*
142          * Select channel 0 as default,
143          * result right adjusted.
144          */
145         ADMUX = 0;
146
147         #if CONFIG_ADC_AVR_REF == ADC_AVR_AREF
148                 /* External voltage at AREF as analog ref source */
149                 /* None */
150         #elif CONFIG_ADC_AVR_REF == ADC_AVR_AVCC
151                 /* AVCC as analog ref source */
152                 ADMUX |= BV(REFS0);
153         #elif CONFIG_ADC_AVR_REF == ADC_AVR_INT256
154                 /* Internal 2.56V as ref source */
155                 ADMUX |= BV(REFS1) | BV(REFS0);
156         #else
157                 #error Unsupported ADC ref value.
158         #endif
159
160         #if defined(ADCSRB)
161         /* Disable Auto trigger source: ADC in Free running mode. */
162         ADCSRB = 0;
163         #endif
164
165         /* Enable ADC, disable autotrigger mode. */
166         ADCSRA = BV(ADEN);
167
168         #if CONFIG_KERN
169                 MOD_CHECK(proc);
170                 ADCSRA |= BV(ADIE);
171         #endif
172
173         /* Set convertion frequency */
174         #if CONFIG_ADC_AVR_DIVISOR == 2
175                 ADCSRA |= BV(ADPS0);
176         #elif CONFIG_ADC_AVR_DIVISOR == 4
177                 ADCSRA |= BV(ADPS1);
178         #elif CONFIG_ADC_AVR_DIVISOR == 8
179                 ADCSRA |= BV(ADPS1) | BV(ADPS0);
180         #elif CONFIG_ADC_AVR_DIVISOR == 16
181                 ADCSRA |= BV(ADPS2);
182         #elif CONFIG_ADC_AVR_DIVISOR == 32
183                 ADCSRA |= BV(ADPS2) | BV(ADPS0);
184         #elif CONFIG_ADC_AVR_DIVISOR == 64
185                 ADCSRA |= BV(ADPS2) | BV(ADPS1);
186         #elif CONFIG_ADC_AVR_DIVISOR == 128
187                 ADCSRA |= BV(ADPS2) | BV(ADPS1) | BV(ADPS0);
188         #else
189                 #error Unsupported ADC prescaler value.
190         #endif
191
192         /* Start a convertion to init ADC hw */
193         adc_hw_read();
194 }