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 #define sample(id) (*out++ = lpc_adc.dr[id] >> 1)
100 void lpc_adc_isr(void)
102 uint32_t stat = lpc_adc.stat;
105 /* Turn off burst mode */
109 /* Store converted values in packet */
111 out = (uint16_t *) &ao_data_ring[ao_data_head].adc;
137 AO_DATA_PRESENT(AO_DATA_ADC);
138 if (ao_data_present == AO_DATA_ALL) {
140 ao_data_ring[ao_data_head].ms5607_raw = ao_ms5607_current;
143 ao_data_ring[ao_data_head].mma655x = ao_mma655x_current;
146 ao_data_ring[ao_data_head].hmc5883 = ao_hmc5883_current;
149 ao_data_ring[ao_data_head].mpu6000 = ao_mpu6000_current;
151 ao_data_ring[ao_data_head].tick = ao_tick_count;
152 ao_data_head = ao_data_ring_next(ao_data_head);
153 ao_wakeup((void *) &ao_data_head);
160 * Start the ADC sequence using the DMA engine
169 lpc_adc.cr = ((AO_ADC_MASK << LPC_ADC_CR_SEL) |
170 (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
171 (1 << LPC_ADC_CR_BURST) |
172 (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS));
176 ao_adc_dump(void) __reentrant
178 struct ao_data packet;
182 ao_data_get(&packet);
184 AO_ADC_DUMP(&packet);
186 printf("tick: %5u", packet.tick);
187 d = (int16_t *) (&packet.adc);
188 for (i = 0; i < AO_NUM_ADC; i++)
189 printf (" %2d: %5d", i, d[i]);
194 __code struct ao_cmds ao_adc_cmds[] = {
195 { ao_adc_dump, "a\0Display current ADC values" },
202 lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_ADC);
203 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_ADC_PD);
205 /* Enable interrupt when last channel is complete */
206 lpc_adc.inten = (1 << AO_ADC_LAST);
208 lpc_nvic_set_enable(LPC_ISR_ADC_POS);
209 lpc_nvic_set_priority(LPC_ISR_ADC_POS, AO_LPC_NVIC_CLOCK_PRIORITY);
211 ao_enable_analog(0, 11, 0);
214 ao_enable_analog(0, 12, 1);
217 ao_enable_analog(0, 13, 2);
220 ao_enable_analog(0, 14, 3);
223 ao_enable_analog(0, 15, 4);
226 ao_enable_analog(0, 16, 5);
229 ao_enable_analog(0, 22, 6);
232 ao_enable_analog(0, 23, 7);
235 lpc_adc.cr = ((AO_ADC_MASK << LPC_ADC_CR_SEL) |
236 (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
237 (0 << LPC_ADC_CR_BURST) |
238 (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS));
240 ao_cmd_register(&ao_adc_cmds[0]);