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