altos/lpc: Stop using burst mode for LPC ADC
[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         int16_t *d;
153         uint8_t i;
154
155         ao_data_get(&packet);
156 #ifdef AO_ADC_DUMP
157         AO_ADC_DUMP(&packet);
158 #else
159         printf("tick: %5u",  packet.tick);
160         d = (int16_t *) (&packet.adc);
161         for (i = 0; i < AO_NUM_ADC; i++)
162                 printf (" %2d: %5d", i, d[i]);
163         printf("\n");
164 #endif
165 }
166
167 __code struct ao_cmds ao_adc_cmds[] = {
168         { ao_adc_dump,  "a\0Display current ADC values" },
169         { 0, NULL },
170 };
171
172 void
173 ao_adc_init(void)
174 {
175         lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_ADC);
176         lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_ADC_PD);
177
178         /* Enable interrupt when channel is complete */
179         lpc_adc.inten = (1 << LPC_ADC_INTEN_ADGINTEN);
180
181         lpc_nvic_set_enable(LPC_ISR_ADC_POS);
182         lpc_nvic_set_priority(LPC_ISR_ADC_POS, AO_LPC_NVIC_CLOCK_PRIORITY);
183 #if AO_ADC_0
184         ao_enable_analog(0, 11, 0);
185 #endif
186 #if AO_ADC_1
187         ao_enable_analog(0, 12, 1);
188 #endif
189 #if AO_ADC_2
190         ao_enable_analog(0, 13, 2);
191 #endif
192 #if AO_ADC_3
193         ao_enable_analog(0, 14, 3);
194 #endif
195 #if AO_ADC_4
196         ao_enable_analog(0, 15, 4);
197 #endif
198 #if AO_ADC_5
199         ao_enable_analog(0, 16, 5);
200 #endif
201 #if AO_ADC_6
202         ao_enable_analog(0, 22, 6);
203 #endif
204 #if AO_ADC_7
205         ao_enable_analog(0, 23, 7);
206 #endif
207
208         lpc_adc.cr = ((0 << LPC_ADC_CR_SEL) |
209                       (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
210                       (0 << LPC_ADC_CR_BURST) |
211                       (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS));
212
213         ao_cmd_register(&ao_adc_cmds[0]);
214
215         ao_adc_ready = 1;
216 }