70e8b2d68e6663d4b0b66e1a1d79d684713a6b51
[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         uint16_t        *out = (uint16_t *) &ao_data_ring[ao_data_head].adc;
90         vuint32_t       *in = &lpc_adc.dr[0];
91
92         /* Store converted values in packet */
93
94 #if AO_ADC_0
95         *out++ = ((uint16_t) *in++) >> 1;
96 #endif
97 #if AO_ADC_1
98         *out++ = ((uint16_t) *in++) >> 1;
99 #endif
100 #if AO_ADC_2
101         *out++ = ((uint16_t) *in++) >> 1;
102 #endif
103 #if AO_ADC_3
104         *out++ = ((uint16_t) *in++) >> 1;
105 #endif
106 #if AO_ADC_4
107         *out++ = ((uint16_t) *in++) >> 1;
108 #endif
109 #if AO_ADC_5
110         *out++ = ((uint16_t) *in++) >> 1;
111 #endif
112 #if AO_ADC_6
113         *out++ = ((uint16_t) *in++) >> 1;
114 #endif
115 #if AO_ADC_7
116         *out++ = ((uint16_t) *in++) >> 1;
117 #endif
118
119         AO_DATA_PRESENT(AO_DATA_ADC);
120         if (ao_data_present == AO_DATA_ALL) {
121 #if HAS_MS5607
122                 ao_data_ring[ao_data_head].ms5607_raw = ao_ms5607_current;
123 #endif
124 #if HAS_MMA655X
125                 ao_data_ring[ao_data_head].mma655x = ao_mma655x_current;
126 #endif
127 #if HAS_HMC5883
128                 ao_data_ring[ao_data_head].hmc5883 = ao_hmc5883_current;
129 #endif
130 #if HAS_MPU6000
131                 ao_data_ring[ao_data_head].mpu6000 = ao_mpu6000_current;
132 #endif
133                 ao_data_ring[ao_data_head].tick = ao_tick_count;
134                 ao_data_head = ao_data_ring_next(ao_data_head);
135                 ao_wakeup((void *) &ao_data_head);
136         }
137         ao_adc_ready = 1;
138 }
139
140 #define AO_ADC_MASK     ((AO_ADC_0 << 0) |      \
141                          (AO_ADC_1 << 1) |      \
142                          (AO_ADC_2 << 2) |      \
143                          (AO_ADC_3 << 3) |      \
144                          (AO_ADC_4 << 4) |      \
145                          (AO_ADC_5 << 5) |      \
146                          (AO_ADC_6 << 6) |      \
147                          (AO_ADC_7 << 7))
148
149 #define AO_ADC_CLKDIV   (AO_LPC_CLKOUT / 4500000)
150
151 /*
152  * Start the ADC sequence using the DMA engine
153  */
154 void
155 ao_adc_poll(void)
156 {
157         if (!ao_adc_ready)
158                 return;
159         ao_adc_ready = 0;
160
161         lpc_adc.cr = ((AO_ADC_MASK << LPC_ADC_CR_SEL) |
162                       (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
163                       (1 << LPC_ADC_CR_BURST) |
164                       (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS));
165 }
166
167 static void
168 ao_adc_dump(void) __reentrant
169 {
170         struct ao_data  packet;
171         int16_t *d;
172         uint8_t i;
173
174         ao_data_get(&packet);
175 #ifdef AO_ADC_DUMP
176         AO_ADC_DUMP(&packet);
177 #else
178         printf("tick: %5u",  packet.tick);
179         d = (int16_t *) (&packet.adc);
180         for (i = 0; i < AO_NUM_ADC; i++)
181                 printf (" %2d: %5d", i, d[i]);
182         printf("\n");
183 #endif
184 }
185
186 __code struct ao_cmds ao_adc_cmds[] = {
187         { ao_adc_dump,  "a\0Display current ADC values" },
188         { 0, NULL },
189 };
190
191 void
192 ao_adc_init(void)
193 {
194         lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_ADC);
195         lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_ADC_PD);
196
197         /* Enable interrupt when last channel is complete */
198         lpc_adc.inten = (1 << AO_ADC_LAST);
199
200         lpc_nvic_set_enable(LPC_ISR_ADC_POS);
201         lpc_nvic_set_priority(LPC_ISR_ADC_POS, AO_LPC_NVIC_CLOCK_PRIORITY);
202 #if AO_ADC_0
203         ao_enable_analog(0, 11);
204 #endif
205 #if AO_ADC_1
206         ao_enable_analog(0, 12);
207 #endif
208 #if AO_ADC_2
209         ao_enable_analog(0, 13);
210 #endif
211 #if AO_ADC_3
212         ao_enable_analog(0, 14);
213 #endif
214 #if AO_ADC_4
215         ao_enable_analog(0, 14);
216 #endif
217 #if AO_ADC_5
218         ao_enable_analog(0, 14);
219 #endif
220 #if AO_ADC_6
221         ao_enable_analog(0, 14);
222 #endif
223 #if AO_ADC_7
224         ao_enable_analog(0, 14);
225 #endif
226         ao_cmd_register(&ao_adc_cmds[0]);
227
228         ao_adc_ready = 1;
229 }