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.
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)
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
59 #define AO_ADC_CLKDIV (AO_LPC_SYSCLK / 450000)
61 static uint8_t ao_adc_ready;
62 static uint8_t ao_adc_sequence;
64 static const uint8_t ao_adc_mask_seq[AO_ADC_NUM] = {
91 #define sample(id) (*out++ = (uint16_t) lpc_adc.dr[id] >> 1)
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));
101 void lpc_adc_isr(void)
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) {
113 AO_DATA_PRESENT(AO_DATA_ADC);
114 if (ao_data_present == AO_DATA_ALL) {
116 ao_data_ring[ao_data_head].ms5607_raw = ao_ms5607_current;
119 ao_data_ring[ao_data_head].mma655x = ao_mma655x_current;
122 ao_data_ring[ao_data_head].hmc5883 = ao_hmc5883_current;
125 ao_data_ring[ao_data_head].mpu6000 = ao_mpu6000_current;
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);
136 * Start the ADC sequence using burst mode
149 ao_adc_dump(void) __reentrant
151 struct ao_data packet;
157 ao_data_get(&packet);
159 AO_ADC_DUMP(&packet);
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]);
169 __code struct ao_cmds ao_adc_cmds[] = {
170 { ao_adc_dump, "a\0Display current ADC values" },
177 lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_ADC);
178 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_ADC_PD);
180 /* Enable interrupt when channel is complete */
181 lpc_adc.inten = (1 << LPC_ADC_INTEN_ADGINTEN);
183 lpc_nvic_set_enable(LPC_ISR_ADC_POS);
184 lpc_nvic_set_priority(LPC_ISR_ADC_POS, AO_LPC_NVIC_CLOCK_PRIORITY);
186 ao_enable_analog(0, 11, 0);
189 ao_enable_analog(0, 12, 1);
192 ao_enable_analog(0, 13, 2);
195 ao_enable_analog(0, 14, 3);
198 ao_enable_analog(0, 15, 4);
201 ao_enable_analog(0, 16, 5);
204 ao_enable_analog(0, 22, 6);
207 ao_enable_analog(0, 23, 7);
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));
215 ao_cmd_register(&ao_adc_cmds[0]);