4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
32 * \brief ADC hardware-specific definition
35 * \author Francesco Sacchi <batt@develer.com>
37 * This module is automatically included so no need to include
44 #include "cfg/cfg_adc.h"
45 #include "cfg/cfg_kern.h"
46 #include <cfg/macros.h>
47 #include <cfg/compiler.h>
49 #include <cpu/irq.h> // IRQ_ASSERT_ENABLED()
54 #include <avr/interrupt.h>
56 #define ADC_AVR_AREF 0
57 #define ADC_AVR_AVCC 1
58 #define ADC_AVR_INT256 2
61 #include <cfg/module.h>
62 #include <kern/proc.h>
63 #include <kern/signal.h>
66 #if !CONFIG_KERN_SIGNALS
67 #error Signals must be active to use ADC with kernel
70 /* Signal adc convertion end */
71 #define SIG_ADC_COMPLETE SIG_SINGLE
73 /* ADC waiting process */
74 static struct Process *adc_process;
78 * Simply signal the adc process that convertion is complete.
82 sig_signal(adc_process, SIG_ADC_COMPLETE);
84 #endif /* CONFIG_KERN */
87 * Select mux channel \a ch.
88 * \todo only first 8 channels are selectable!
90 INLINE void adc_hw_select_ch(uint8_t ch)
92 /* Set to 0 all mux registers */
93 ADMUX &= ~(BV(MUX4) | BV(MUX3) | BV(MUX2) | BV(MUX1) | BV(MUX0));
95 /* Select channel, only first 8 channel modes are supported for now */
101 * Start an ADC convertion.
102 * If a kernel is present, preempt until convertion is complete, otherwise
103 * a busy wait on ADCS bit is done.
105 INLINE uint16_t adc_hw_read(void)
107 // Ensure another convertion is not running.
108 ASSERT(!(ADCSRA & BV(ADSC)));
114 // Ensure IRQs enabled.
115 IRQ_ASSERT_ENABLED();
116 adc_process = proc_current();
117 sig_wait(SIG_ADC_COMPLETE);
119 //Wait in polling until is done
120 while (ADCSRA & BV(ADSC)) ;
129 INLINE void adc_hw_init(void)
132 * Select channel 0 as default,
133 * result right adjusted.
137 #if CONFIG_ADC_AVR_REF == ADC_AVR_AREF
138 /* External voltage at AREF as analog ref source */
140 #elif CONFIG_ADC_AVR_REF == ADC_AVR_AVCC
141 /* AVCC as analog ref source */
143 #elif CONFIG_ADC_AVR_REF == ADC_AVR_INT256
144 /* Internal 2.56V as ref source */
145 ADMUX |= BV(REFS1) | BV(REFS0);
147 #error Unsupported ADC ref value.
151 /* Disable Auto trigger source: ADC in Free running mode. */
155 /* Enable ADC, disable autotrigger mode. */
163 /* Set convertion frequency */
164 #if CONFIG_ADC_AVR_DIVISOR == 2
166 #elif CONFIG_ADC_AVR_DIVISOR == 4
168 #elif CONFIG_ADC_AVR_DIVISOR == 8
169 ADCSRA |= BV(ADPS1) | BV(ADPS0);
170 #elif CONFIG_ADC_AVR_DIVISOR == 16
172 #elif CONFIG_ADC_AVR_DIVISOR == 32
173 ADCSRA |= BV(ADPS2) | BV(ADPS0);
174 #elif CONFIG_ADC_AVR_DIVISOR == 64
175 ADCSRA |= BV(ADPS2) | BV(ADPS1);
176 #elif CONFIG_ADC_AVR_DIVISOR == 128
177 ADCSRA |= BV(ADPS2) | BV(ADPS1) | BV(ADPS0);
179 #error Unsupported ADC prescaler value.
182 /* Start a convertion to init ADC hw */