altos/lpc: adc code computes number of active ADC channels
[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 #define AO_ADC_NUM      (AO_ADC_0 + AO_ADC_1 + AO_ADC_2 + AO_ADC_3 + \
54                          AO_ADC_4 + AO_ADC_5 + AO_ADC_6 + AO_ADC_7)
55
56 /* ADC clock is divided by this value + 1, which ensures that
57  * the ADC clock will be strictly less than 4.5MHz as required
58  */
59 #define AO_ADC_CLKDIV   (AO_LPC_SYSCLK / 450000)
60
61 static uint8_t          ao_adc_ready;
62 static uint8_t          ao_adc_sequence;
63
64 static const uint8_t    ao_adc_mask_seq[AO_ADC_NUM] = {
65 #if AO_ADC_0
66         1 << 0,
67 #endif
68 #if AO_ADC_1
69         1 << 1,
70 #endif
71 #if AO_ADC_2
72         1 << 2,
73 #endif
74 #if AO_ADC_3
75         1 << 3,
76 #endif
77 #if AO_ADC_4
78         1 << 4,
79 #endif
80 #if AO_ADC_5
81         1 << 6,
82 #endif
83 #if AO_ADC_6
84         1 << 6,
85 #endif
86 #if AO_ADC_7
87         1 << 7,
88 #endif
89 };
90
91 #define sample(id)      (*out++ = (uint16_t) lpc_adc.dr[id] >> 1)
92
93 static inline void lpc_adc_start(void) {
94         lpc_adc.cr = ((ao_adc_mask_seq[ao_adc_sequence] << LPC_ADC_CR_SEL) |
95                       (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
96                       (0 << LPC_ADC_CR_BURST) |
97                       (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS) |
98                       (LPC_ADC_CR_START_NOW << LPC_ADC_CR_START));
99 }
100
101 void  lpc_adc_isr(void)
102 {
103         uint16_t        *out;
104
105         /* Store converted value in packet */
106         out = (uint16_t *) &ao_data_ring[ao_data_head].adc;
107         out[ao_adc_sequence] = (uint16_t) lpc_adc.gdr >> 1;
108         if (++ao_adc_sequence < AO_ADC_NUM) {
109                 lpc_adc_start();
110                 return;
111         }
112
113         AO_DATA_PRESENT(AO_DATA_ADC);
114         if (ao_data_present == AO_DATA_ALL) {
115 #if HAS_MS5607
116                 ao_data_ring[ao_data_head].ms5607_raw = ao_ms5607_current;
117 #endif
118 #if HAS_MMA655X
119                 ao_data_ring[ao_data_head].mma655x = ao_mma655x_current;
120 #endif
121 #if HAS_HMC5883
122                 ao_data_ring[ao_data_head].hmc5883 = ao_hmc5883_current;
123 #endif
124 #if HAS_MPU6000
125                 ao_data_ring[ao_data_head].mpu6000 = ao_mpu6000_current;
126 #endif
127                 ao_data_ring[ao_data_head].tick = ao_tick_count;
128                 ao_data_head = ao_data_ring_next(ao_data_head);
129                 ao_wakeup((void *) &ao_data_head);
130         }
131         ao_adc_ready = 1;
132 }
133
134
135 /*
136  * Start the ADC sequence using burst mode
137  */
138 void
139 ao_adc_poll(void)
140 {
141         if (!ao_adc_ready)
142                 return;
143         ao_adc_ready = 0;
144         ao_adc_sequence = 0;
145         lpc_adc_start();
146 }
147
148 static void
149 ao_adc_dump(void) __reentrant
150 {
151         struct ao_data  packet;
152 #ifndef AO_ADC_DUMP
153         int16_t *d;
154         uint8_t i;
155 #endif
156
157         ao_data_get(&packet);
158 #ifdef AO_ADC_DUMP
159         AO_ADC_DUMP(&packet);
160 #else
161         printf("tick: %5u",  packet.tick);
162         d = (int16_t *) (&packet.adc);
163         for (i = 0; i < AO_ADC_NUM; i++)
164                 printf (" %2d: %5d", i, d[i]);
165         printf("\n");
166 #endif
167 }
168
169 __code struct ao_cmds ao_adc_cmds[] = {
170         { ao_adc_dump,  "a\0Display current ADC values" },
171         { 0, NULL },
172 };
173
174 void
175 ao_adc_init(void)
176 {
177         lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_ADC);
178         lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_ADC_PD);
179
180         /* Enable interrupt when channel is complete */
181         lpc_adc.inten = (1 << LPC_ADC_INTEN_ADGINTEN);
182
183         lpc_nvic_set_enable(LPC_ISR_ADC_POS);
184         lpc_nvic_set_priority(LPC_ISR_ADC_POS, AO_LPC_NVIC_CLOCK_PRIORITY);
185 #if AO_ADC_0
186         ao_enable_analog(0, 11, 0);
187 #endif
188 #if AO_ADC_1
189         ao_enable_analog(0, 12, 1);
190 #endif
191 #if AO_ADC_2
192         ao_enable_analog(0, 13, 2);
193 #endif
194 #if AO_ADC_3
195         ao_enable_analog(0, 14, 3);
196 #endif
197 #if AO_ADC_4
198         ao_enable_analog(0, 15, 4);
199 #endif
200 #if AO_ADC_5
201         ao_enable_analog(0, 16, 5);
202 #endif
203 #if AO_ADC_6
204         ao_enable_analog(0, 22, 6);
205 #endif
206 #if AO_ADC_7
207         ao_enable_analog(0, 23, 7);
208 #endif
209
210         lpc_adc.cr = ((0 << LPC_ADC_CR_SEL) |
211                       (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
212                       (0 << LPC_ADC_CR_BURST) |
213                       (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS));
214
215         ao_cmd_register(&ao_adc_cmds[0]);
216
217         ao_adc_ready = 1;
218 }