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