59fd1c6893b6f62a602ce48f41fb28f2efbec420
[bertos.git] / bertos / cpu / arm / drv / adc_at91.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 2008 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  *
34  * \brief ADC hardware-specific implementation
35  *
36  * This ADC module should be use both whit kernel or none.
37  * If you are using a kernel, the adc drive does not wait the finish of
38  * conversion but use a singal every time a required conversion are 
39  * ended. This signal wake up a process that return a result of 
40  * conversion. Otherwise, if you not use a kernl, this module wait
41  * whit a loop the finishing of conversion.
42  *
43  *
44  * \version $Id$
45  * 
46  * \author Daniele Basile <asterix@develer.com>
47  */
48
49 #include "adc_at91.h"
50
51 #include <drv/adc.h>
52
53 #include <cfg/macros.h>
54 #include <cfg/compiler.h>
55
56 #include <io/arm.h>
57
58 #include "appconfig.h"
59
60 #if CONFIG_KERNEL
61         #include <cfg/module.h>
62         #include <config_kern.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_USER0
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         static void ISR_FUNC adc_conversion_end_irq(void)
82         {
83                 sig_signal(adc_process, SIG_ADC_COMPLETE);
84
85                 /* Inform hw that we have served the IRQ */
86                 AIC_EOICR = 0;
87         }
88
89         static void adc_enable_irq(void)
90         {
91
92                 // Disable all interrupt
93                 ADC_IDR = 0xFFFFFFFF;
94
95                 //Register interrupt vector
96                 AIC_SVR(ADC_ID) = adc_conversion_end_irq;
97                 AIC_SMR(ADC_ID) = AIC_SRCTYPE_INT_EDGE_TRIGGERED;
98                 AIC_IECR = BV(ADC_ID);
99
100                 //Enable data ready irq
101                 ADC_IER = BV(ADC_DRDY);
102         }
103
104 #endif /* CONFIG_KERNEL */
105
106
107 /**
108  * Select mux channel \a ch.
109  * \todo only first 8 channels are selectable!
110  */
111 INLINE void adc_hw_select_ch(uint8_t ch)
112 {
113         //Disable all channels
114         ADC_CHDR = ADC_CH_MASK;
115         //Enable select channel
116         ADC_CHER = BV(ch);
117 }
118
119
120 /**
121  * Start an ADC convertion.
122  * If a kernel is present, preempt until convertion is complete, otherwise
123  * a busy wait on ADCS bit is done.
124  */
125 INLINE uint16_t adc_hw_read(void)
126 {
127         ASSERT(!(ADC_SR & ADC_EOC_MASK));
128
129         #if CONFIG_KERNEL
130                 adc_process = proc_current();
131         #endif
132
133         // Start convertion
134         ADC_CR = BV(ADC_START);
135
136         #if CONFIG_KERNEL
137                 // Ensure IRQs enabled.
138                 ASSERT(IRQ_ENABLED());
139                 sig_wait(SIG_ADC_COMPLETE);
140         #else
141                 //Wait in polling until is done
142                 while (!(ADC_SR & BV(ADC_DRDY)));
143         #endif
144
145         //Return the last converted data
146         return(ADC_LCDR);
147 }
148
149 /**
150  * Init ADC hardware.
151  */
152 INLINE void adc_hw_init(void)
153 {
154         //Init ADC pins.
155         ADC_INIT_PINS();
156
157         /*
158          * Set adc mode register:
159          * - Disable hardware trigger and enable software trigger.
160          * - Select normal mode.
161          * - Set ADC_BITS bit convertion resolution.
162          *
163          * \{
164          */
165         ADC_MR = 0;
166         #if ADC_BITS == 10
167                 ADC_MR &= ~BV(ADC_LOWRES);
168         #elif ADC_BITS == 8
169                 ADC_MR |= BV(ADC_LOWRES);
170         #else
171                 #error No select bit resolution is supported to this CPU
172         #endif
173         /* \} */
174
175         TRACEMSG("prescaler[%ld], stup[%ld], shtim[%ld]\n",ADC_COMPUTED_PRESCALER,ADC_COMPUTED_STARTUPTIME,ADC_COMPUTED_SHTIME);
176
177
178         //Apply computed prescaler value
179         ADC_MR &= ~ADC_PRESCALER_MASK;
180         ADC_MR |= ((ADC_COMPUTED_PRESCALER << ADC_PRESCALER_SHIFT) & ADC_PRESCALER_MASK);
181         TRACEMSG("prescaler[%ld]\n", (ADC_COMPUTED_PRESCALER << ADC_PRESCALER_SHIFT) & ADC_PRESCALER_MASK);
182
183         //Apply computed start up time
184         ADC_MR &= ~ADC_STARTUP_MASK;
185         ADC_MR |= ((ADC_COMPUTED_STARTUPTIME << ADC_STARTUP_SHIFT) & ADC_STARTUP_MASK);
186         TRACEMSG("sttime[%ld]\n", (ADC_COMPUTED_STARTUPTIME << ADC_STARTUP_SHIFT) & ADC_STARTUP_MASK);
187
188         //Apply computed sample and hold time
189         ADC_MR &= ~ADC_SHTIME_MASK;
190         ADC_MR |= ((ADC_COMPUTED_SHTIME << ADC_SHTIME_SHIFT) & ADC_SHTIME_MASK);
191         TRACEMSG("shtime[%ld]\n", (ADC_COMPUTED_SHTIME << ADC_SHTIME_SHIFT) & ADC_SHTIME_MASK);
192
193         #if CONFIG_KERNEL
194                 //Register and enable irq for adc.
195                 adc_enable_irq();
196         #endif
197
198 }