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