2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
54 # define AO_ADC_LAST 7
57 # define AO_ADC_LAST 6
60 # define AO_ADC_LAST 5
63 # define AO_ADC_LAST 4
66 # define AO_ADC_LAST 3
69 # define AO_ADC_LAST 2
72 # define AO_ADC_LAST 1
75 # define AO_ADC_LAST 0
85 #define AO_ADC_MASK ((AO_ADC_0 << 0) | \
94 #define AO_ADC_CLKDIV (AO_LPC_SYSCLK / 4500000)
96 static uint8_t ao_adc_ready;
98 static uint16_t ao_adc_prev[AO_NUM_ADC];
100 #define sample(id) do { \
101 uint16_t p = *prev; \
102 uint16_t v = lpc_adc.dr[id]; \
109 void lpc_adc_isr(void)
111 uint32_t stat = lpc_adc.stat;
115 /* Turn off burst mode */
119 /* Store converted values in packet */
121 out = (uint16_t *) &ao_data_ring[ao_data_head].adc;
148 AO_DATA_PRESENT(AO_DATA_ADC);
149 if (ao_data_present == AO_DATA_ALL) {
151 ao_data_ring[ao_data_head].ms5607_raw = ao_ms5607_current;
154 ao_data_ring[ao_data_head].mma655x = ao_mma655x_current;
157 ao_data_ring[ao_data_head].hmc5883 = ao_hmc5883_current;
160 ao_data_ring[ao_data_head].mpu6000 = ao_mpu6000_current;
162 ao_data_ring[ao_data_head].tick = ao_tick_count;
163 ao_data_head = ao_data_ring_next(ao_data_head);
164 ao_wakeup((void *) &ao_data_head);
171 * Start the ADC sequence using the DMA engine
180 lpc_adc.cr = ((AO_ADC_MASK << LPC_ADC_CR_SEL) |
181 (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
182 (1 << LPC_ADC_CR_BURST) |
183 (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS));
187 ao_adc_dump(void) __reentrant
189 struct ao_data packet;
193 ao_data_get(&packet);
195 AO_ADC_DUMP(&packet);
197 printf("tick: %5u", packet.tick);
198 d = (int16_t *) (&packet.adc);
199 for (i = 0; i < AO_NUM_ADC; i++)
200 printf (" %2d: %5d", i, d[i]);
205 __code struct ao_cmds ao_adc_cmds[] = {
206 { ao_adc_dump, "a\0Display current ADC values" },
213 lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_ADC);
214 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_ADC_PD);
216 /* Enable interrupt when last channel is complete */
217 lpc_adc.inten = (1 << AO_ADC_LAST);
219 lpc_nvic_set_enable(LPC_ISR_ADC_POS);
220 lpc_nvic_set_priority(LPC_ISR_ADC_POS, AO_LPC_NVIC_CLOCK_PRIORITY);
222 ao_enable_analog(0, 11, 0);
225 ao_enable_analog(0, 12, 1);
228 ao_enable_analog(0, 13, 2);
231 ao_enable_analog(0, 14, 3);
234 ao_enable_analog(0, 15, 4);
237 ao_enable_analog(0, 16, 5);
240 ao_enable_analog(0, 22, 6);
243 ao_enable_analog(0, 23, 7);
246 lpc_adc.cr = ((AO_ADC_MASK << LPC_ADC_CR_SEL) |
247 (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
248 (0 << LPC_ADC_CR_BURST) |
249 (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS));
251 ao_cmd_register(&ao_adc_cmds[0]);