Refactor to use new protocol module and sipo.
[bertos.git] / bertos / cpu / cortex-m3 / drv / adc_lm3s.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 2010 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_lm3s.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
55 #include <cfg/macros.h>
56 #include <cfg/compiler.h>
57 #include <cfg/debug.h>
58
59 // Define log settings for cfg/log.h.
60 #define LOG_LEVEL         ADC_LOG_LEVEL
61 #define LOG_FORMAT        ADC_LOG_FORMAT
62 #include <cfg/log.h>
63
64 #include <drv/adc.h>
65 #include <drv/timer.h>
66 #include <drv/clock_lm3s.h>
67
68 #include <io/lm3s.h>
69
70 /* Select witch ADC use */
71 #if CPU_CM3_LM3S1968 || CPU_CM3_LM3S8962
72         #define ADC_BASE            ADC0_BASE
73         #define SYSCTL_RCGC_R       SYSCTL_RCGC0_R
74         #define SYSCTL_RCGC_ADC     SYSCTL_RCGC0_ADC0
75 #else
76         #error Unknow ADC register for select cpu core
77 #endif
78
79
80 #if CONFIG_KERN
81         #include <cfg/module.h>
82
83         #include <kern/proc.h>
84         #include <kern/signal.h>
85
86         #include <drv/irq_cm3.h>
87
88
89         #if !CONFIG_KERN_SIGNALS
90                 #error Signals must be active to use ADC with kernel
91         #endif
92
93         /* Signal adc convertion end */
94         #define SIG_ADC_COMPLETE SIG_USER0
95
96         /* ADC waiting process */
97         static struct Process *adc_process;
98
99         /**
100          * ADC ISR.
101          * Simply signal the adc process that convertion is complete.
102          */
103         static DECLARE_ISR(adc_conversion_end_irq)
104         {
105                 sig_post(adc_process, SIG_ADC_COMPLETE);
106
107                 /* Clear the status bit */
108                 HWREG(ADC_BASE + ADC_O_ISC) |= ADC_ISC_IN3;
109
110         }
111
112         static void adc_enable_irq(void)
113         {
114                 /* Clear all pending irq */
115                 HWREG(ADC_BASE + ADC_O_ISC) = 0;
116                 /* Register the IRQ handler */
117                 sysirq_setHandler(INT_ADC3, adc_conversion_end_irq);
118                 /* Enable IRQ */
119                 HWREG(ADC_BASE + ADC_O_SSCTL3) |= ADC_SSCTL3_IE0;
120                 HWREG(ADC_BASE + ADC_O_IM) |= ADC_IM_MASK3;
121         }
122
123 #endif /* CONFIG_KERN */
124
125 /**
126  * Select mux channel \a ch.
127  * Generally the stm32 cpu family allow us to program the order
128  * of adc channel that we want to read.
129  * In this driver implementation we put as fist channel to read the
130  * select ones.
131  */
132 void adc_hw_select_ch(uint8_t ch)
133 {
134         /* Select channel that we want read */
135         HWREG(ADC_BASE + ADC_O_SSMUX3) = ch;
136         /* Make single acquisition */
137         HWREG(ADC_BASE + ADC_O_SSCTL3) |= ADC_SSCTL3_END0;
138         /* Enable sequence S03 (single sample on select channel) */
139         HWREG(ADC_BASE + ADC_O_ACTSS) |= ADC_ACTSS_ASEN3;
140 }
141
142 /**
143  * Start an ADC convertion.
144  * If a kernel is present, preempt until convertion is complete, otherwise
145  * a busy wait on ADC_DRDY bit is done.
146  */
147 uint16_t adc_hw_read(void)
148 {
149         #if CONFIG_KERN
150                 /* Ensure ADC is not already in use by another process */
151                 ASSERT(adc_process == NULL);
152                 adc_process = proc_current();
153         #endif
154
155         /* Start convertion */
156         HWREG(ADC0_BASE + ADC_O_PSSI) |= ADC_PSSI_SS3;
157
158         #if CONFIG_KERN
159                 /* Ensure IRQs enabled. */
160                 IRQ_ASSERT_ENABLED();
161                 sig_wait(SIG_ADC_COMPLETE);
162
163                 /* Prevent race condition in case of preemptive kernel */
164                 uint16_t ret = (uint16_t)HWREG(ADC_BASE + ADC_O_SSFIFO3);
165                 MEMORY_BARRIER;
166                 adc_process = NULL;
167                 return ret;
168         #else
169                 /* Wait in polling until conversion is done */
170                 while (!(HWREG(ADC_BASE + ADC_O_SSFSTAT3) & ADC_SSFSTAT3_FULL));
171
172                 /* Return the last converted data */
173                 return (uint16_t)HWREG(ADC_BASE + ADC_O_SSFIFO3);
174         #endif
175 }
176
177 /**
178  * Init ADC hardware.
179  */
180 void adc_hw_init(void)
181 {
182         /* Enable ADC0 clock */
183         SYSCTL_RCGC_R |= SYSCTL_RCGC_ADC;
184
185         /*
186          * We wait some time because the clock is istable
187          * and that could cause system hardfault
188          */
189         lm3s_busyWait(10);
190
191         /* Disable all sequence */
192         HWREG(ADC_BASE + ADC_O_ACTSS) = 0;
193         /* Set trigger event to programmed (for all sequence) */
194         HWREG(ADC_BASE + ADC_O_EMUX) = 0;
195
196         #if CONFIG_KERN
197                 adc_enable_irq();
198         #endif
199 }