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