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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
54 #define AO_ADC_NUM (AO_ADC_0 + AO_ADC_1 + AO_ADC_2 + AO_ADC_3 + \
55 AO_ADC_4 + AO_ADC_5 + AO_ADC_6 + AO_ADC_7)
57 /* ADC clock is divided by this value + 1, which ensures that
58 * the ADC clock will be strictly less than 4.5MHz as required
60 #define AO_ADC_CLKDIV (AO_LPC_SYSCLK / 450000)
62 static uint8_t ao_adc_ready;
63 static uint8_t ao_adc_sequence;
65 static const uint8_t ao_adc_mask_seq[AO_ADC_NUM] = {
92 #define sample(id) (*out++ = (uint16_t) lpc_adc.dr[id] >> 1)
94 static inline void lpc_adc_start(void) {
95 lpc_adc.cr = (((uint32_t) ao_adc_mask_seq[ao_adc_sequence] << LPC_ADC_CR_SEL) |
96 (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
97 (0 << LPC_ADC_CR_BURST) |
98 (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS) |
99 (LPC_ADC_CR_START_NOW << LPC_ADC_CR_START));
102 void lpc_adc_isr(void)
106 /* Store converted value in packet */
107 out = (uint16_t *) &ao_data_ring[ao_data_head].adc;
108 out[ao_adc_sequence] = (uint16_t) lpc_adc.gdr >> 1;
109 if (++ao_adc_sequence < AO_ADC_NUM) {
114 AO_DATA_PRESENT(AO_DATA_ADC);
115 ao_data_fill(ao_data_head);
121 * Start the ADC sequence using burst mode
136 struct ao_data packet;
142 ao_data_get(&packet);
144 AO_ADC_DUMP(&packet);
146 printf("tick: %5u", packet.tick);
147 d = (int16_t *) (&packet.adc);
148 for (i = 0; i < AO_ADC_NUM; i++)
149 printf (" %2d: %5d", i, d[i]);
154 const struct ao_cmds ao_adc_cmds[] = {
155 { ao_adc_dump, "a\0Display current ADC values" },
162 lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_ADC);
163 lpc_scb.pdruncfg &= ~(1UL << LPC_SCB_PDRUNCFG_ADC_PD);
165 /* Enable interrupt when channel is complete */
166 lpc_adc.inten = (1 << LPC_ADC_INTEN_ADGINTEN);
168 lpc_nvic_set_enable(LPC_ISR_ADC_POS);
169 lpc_nvic_set_priority(LPC_ISR_ADC_POS, AO_LPC_NVIC_CLOCK_PRIORITY);
171 ao_enable_analog(0, 11, 0);
174 ao_enable_analog(0, 12, 1);
177 ao_enable_analog(0, 13, 2);
180 ao_enable_analog(0, 14, 3);
183 ao_enable_analog(0, 15, 4);
186 ao_enable_analog(0, 16, 5);
189 ao_enable_analog(0, 22, 6);
192 ao_enable_analog(0, 23, 7);
195 lpc_adc.cr = ((0 << LPC_ADC_CR_SEL) |
196 (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
197 (0 << LPC_ADC_CR_BURST) |
198 (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS));
200 ao_cmd_register(&ao_adc_cmds[0]);