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