9ecc7c13f1e313d572d98e1bad45d4eb4aefcbd0
[fw/altos] / src / lpc / ao_adc_lpc.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19 #include <ao_data.h>
20
21 #ifndef AO_ADC_0
22 #define AO_ADC_0        0
23 #endif
24
25 #ifndef AO_ADC_1
26 #define AO_ADC_1        0
27 #endif
28
29 #ifndef AO_ADC_2
30 #define AO_ADC_2        0
31 #endif
32
33 #ifndef AO_ADC_3
34 #define AO_ADC_3        0
35 #endif
36
37 #ifndef AO_ADC_4
38 #define AO_ADC_4        0
39 #endif
40
41 #ifndef AO_ADC_5
42 #define AO_ADC_5        0
43 #endif
44
45 #ifndef AO_ADC_6
46 #define AO_ADC_6        0
47 #endif
48
49 #ifndef AO_ADC_7
50 #define AO_ADC_7        0
51 #endif
52
53 #if AO_ADC_7
54 # define AO_ADC_LAST            7
55 #else
56 # if AO_ADC_6
57 #  define AO_ADC_LAST           6
58 # else
59 #  if AO_ADC_5
60 #   define AO_ADC_LAST          5
61 #  else
62 #   if AO_ADC_4
63 #    define AO_ADC_LAST         4
64 #   else
65 #    if AO_ADC_3
66 #     define AO_ADC_LAST        3
67 #    else
68 #     if AO_ADC_2
69 #      define AO_ADC_LAST       2
70 #     else
71 #      if AO_ADC_1
72 #       define AO_ADC_LAST      1
73 #      else
74 #       if AO_ADC_0
75 #        define AO_ADC_LAST     0
76 #       endif
77 #      endif
78 #     endif
79 #    endif
80 #   endif
81 #  endif
82 # endif
83 #endif
84
85 static uint8_t                  ao_adc_ready;
86
87 void  lpc_adc_isr(void)
88 {
89         uint32_t        stat = lpc_adc.stat;
90         uint16_t        *out = (uint16_t *) &ao_data_ring[ao_data_head].adc;
91         vuint32_t       *in = &lpc_adc.dr[0];
92
93         lpc_adc.cr = 0;
94         lpc_adc.stat = 0;
95
96         /* Store converted values in packet */
97
98 #if AO_ADC_0
99         *out++ = lpc_adc.dr[0] >> 1;
100 #endif
101 #if AO_ADC_1
102         *out++ = lpc_adc.dr[1] >> 1;
103 #endif
104 #if AO_ADC_2
105         *out++ = lpc_adc.dr[2] >> 1;
106 #endif
107 #if AO_ADC_3
108         *out++ = lpc_adc.dr[3] >> 1;
109 #endif
110 #if AO_ADC_4
111         *out++ = lpc_adc.dr[4] >> 1;
112 #endif
113 #if AO_ADC_5
114         *out++ = lpc_adc.dr[5] >> 1;
115 #endif
116 #if AO_ADC_6
117         *out++ = lpc_adc.dr[6] >> 1;
118 #endif
119 #if AO_ADC_7
120         *out++ = lpc_adc.dr[7] >> 1;
121 #endif
122
123         AO_DATA_PRESENT(AO_DATA_ADC);
124         if (ao_data_present == AO_DATA_ALL) {
125 #if HAS_MS5607
126                 ao_data_ring[ao_data_head].ms5607_raw = ao_ms5607_current;
127 #endif
128 #if HAS_MMA655X
129                 ao_data_ring[ao_data_head].mma655x = ao_mma655x_current;
130 #endif
131 #if HAS_HMC5883
132                 ao_data_ring[ao_data_head].hmc5883 = ao_hmc5883_current;
133 #endif
134 #if HAS_MPU6000
135                 ao_data_ring[ao_data_head].mpu6000 = ao_mpu6000_current;
136 #endif
137                 ao_data_ring[ao_data_head].tick = ao_tick_count;
138                 ao_data_head = ao_data_ring_next(ao_data_head);
139                 ao_wakeup((void *) &ao_data_head);
140         }
141         ao_adc_ready = 1;
142 }
143
144 #define AO_ADC_MASK     ((AO_ADC_0 << 0) |      \
145                          (AO_ADC_1 << 1) |      \
146                          (AO_ADC_2 << 2) |      \
147                          (AO_ADC_3 << 3) |      \
148                          (AO_ADC_4 << 4) |      \
149                          (AO_ADC_5 << 5) |      \
150                          (AO_ADC_6 << 6) |      \
151                          (AO_ADC_7 << 7))
152
153 #define AO_ADC_CLKDIV   (AO_LPC_CLKOUT / 4500000)
154
155 /*
156  * Start the ADC sequence using the DMA engine
157  */
158 void
159 ao_adc_poll(void)
160 {
161         if (!ao_adc_ready)
162                 return;
163         ao_adc_ready = 0;
164
165         lpc_adc.cr = ((AO_ADC_MASK << LPC_ADC_CR_SEL) |
166                       (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
167                       (1 << LPC_ADC_CR_BURST) |
168                       (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS));
169 }
170
171 static void
172 ao_adc_dump(void) __reentrant
173 {
174         struct ao_data  packet;
175         int16_t *d;
176         uint8_t i;
177
178         ao_data_get(&packet);
179 #ifdef AO_ADC_DUMP
180         AO_ADC_DUMP(&packet);
181 #else
182         printf("tick: %5u",  packet.tick);
183         d = (int16_t *) (&packet.adc);
184         for (i = 0; i < AO_NUM_ADC; i++)
185                 printf (" %2d: %5d", i, d[i]);
186         printf("\n");
187 #endif
188 }
189
190 __code struct ao_cmds ao_adc_cmds[] = {
191         { ao_adc_dump,  "a\0Display current ADC values" },
192         { 0, NULL },
193 };
194
195 void
196 ao_adc_init(void)
197 {
198         lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_ADC);
199         lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_ADC_PD);
200
201         /* Enable interrupt when last channel is complete */
202         lpc_adc.inten = (1 << AO_ADC_LAST);
203
204         lpc_nvic_set_enable(LPC_ISR_ADC_POS);
205         lpc_nvic_set_priority(LPC_ISR_ADC_POS, AO_LPC_NVIC_CLOCK_PRIORITY);
206 #if AO_ADC_0
207         ao_enable_analog(0, 11);
208 #endif
209 #if AO_ADC_1
210         ao_enable_analog(0, 12);
211 #endif
212 #if AO_ADC_2
213         ao_enable_analog(0, 13);
214 #endif
215 #if AO_ADC_3
216         ao_enable_analog(0, 14);
217 #endif
218 #if AO_ADC_4
219         ao_enable_analog(0, 14);
220 #endif
221 #if AO_ADC_5
222         ao_enable_analog(0, 14);
223 #endif
224 #if AO_ADC_6
225         ao_enable_analog(0, 14);
226 #endif
227 #if AO_ADC_7
228         ao_enable_analog(0, 14);
229 #endif
230         ao_cmd_register(&ao_adc_cmds[0]);
231
232         ao_adc_ready = 1;
233 }