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.
21 static uint8_t ao_adc_ready;
23 #define AO_ADC_CR2_VAL ((0 << STM_ADC_CR2_SWSTART) | \
24 (STM_ADC_CR2_EXTEN_DISABLE << STM_ADC_CR2_EXTEN) | \
25 (0 << STM_ADC_CR2_EXTSEL) | \
26 (0 << STM_ADC_CR2_JWSTART) | \
27 (STM_ADC_CR2_JEXTEN_DISABLE << STM_ADC_CR2_JEXTEN) | \
28 (0 << STM_ADC_CR2_JEXTSEL) | \
29 (0 << STM_ADC_CR2_ALIGN) | \
30 (0 << STM_ADC_CR2_EOCS) | \
31 (1 << STM_ADC_CR2_DDS) | \
32 (1 << STM_ADC_CR2_DMA) | \
33 (STM_ADC_CR2_DELS_UNTIL_READ << STM_ADC_CR2_DELS) | \
34 (0 << STM_ADC_CR2_CONT) | \
35 (1 << STM_ADC_CR2_ADON))
38 * Callback from DMA ISR
40 * Mark time in ring, shut down DMA engine
42 static void ao_adc_done(int index)
44 AO_DATA_PRESENT(AO_DATA_ADC);
45 ao_dma_done_transfer(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1));
46 if (ao_data_present == AO_DATA_ALL) {
48 ao_data_ring[ao_data_head].ms5607_raw = ao_ms5607_current;
51 ao_data_ring[ao_data_head].mma655x = ao_mma655x_current;
54 ao_data_ring[ao_data_head].hmc5883 = ao_hmc5883_current;
57 ao_data_ring[ao_data_head].mpu6000 = ao_mpu6000_current;
59 ao_data_ring[ao_data_head].tick = ao_tick_count;
60 ao_data_head = ao_data_ring_next(ao_data_head);
61 ao_wakeup((void *) &ao_data_head);
67 * Start the ADC sequence using the DMA engine
76 ao_dma_set_transfer(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1),
78 (void *) (&ao_data_ring[ao_data_head].adc),
80 (0 << STM_DMA_CCR_MEM2MEM) |
81 (STM_DMA_CCR_PL_HIGH << STM_DMA_CCR_PL) |
82 (STM_DMA_CCR_MSIZE_16 << STM_DMA_CCR_MSIZE) |
83 (STM_DMA_CCR_PSIZE_16 << STM_DMA_CCR_PSIZE) |
84 (1 << STM_DMA_CCR_MINC) |
85 (0 << STM_DMA_CCR_PINC) |
86 (0 << STM_DMA_CCR_CIRC) |
87 (STM_DMA_CCR_DIR_PER_TO_MEM << STM_DMA_CCR_DIR));
88 ao_dma_set_isr(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1), ao_adc_done);
89 ao_dma_start(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1));
91 stm_adc.cr2 = AO_ADC_CR2_VAL | (1 << STM_ADC_CR2_SWSTART);
95 * Fetch a copy of the most recent ADC data
98 ao_adc_get(__xdata struct ao_adc *packet)
101 uint8_t i = ao_data_ring_prev(ao_sample_data);
103 uint8_t i = ao_data_ring_prev(ao_data_head);
105 memcpy(packet, (void *) &ao_data_ring[i].adc, sizeof (struct ao_adc));
109 ao_adc_dump(void) __reentrant
111 struct ao_data packet;
115 ao_data_get(&packet);
117 AO_ADC_DUMP(&packet);
119 printf("tick: %5u", packet.tick);
120 d = (int16_t *) (&packet.adc);
121 for (i = 0; i < AO_NUM_ADC; i++)
122 printf (" %2d: %5d", i, d[i]);
127 __code struct ao_cmds ao_adc_cmds[] = {
128 { ao_adc_dump, "a\0Display current ADC values" },
135 #ifdef AO_ADC_PIN0_PORT
136 stm_rcc.ahbenr |= AO_ADC_RCC_AHBENR;
139 #ifdef AO_ADC_PIN0_PORT
140 stm_moder_set(AO_ADC_PIN0_PORT, AO_ADC_PIN0_PIN, STM_MODER_ANALOG);
142 #ifdef AO_ADC_PIN1_PORT
143 stm_moder_set(AO_ADC_PIN1_PORT, AO_ADC_PIN1_PIN, STM_MODER_ANALOG);
145 #ifdef AO_ADC_PIN2_PORT
146 stm_moder_set(AO_ADC_PIN2_PORT, AO_ADC_PIN2_PIN, STM_MODER_ANALOG);
148 #ifdef AO_ADC_PIN3_PORT
149 stm_moder_set(AO_ADC_PIN3_PORT, AO_ADC_PIN3_PIN, STM_MODER_ANALOG);
151 #ifdef AO_ADC_PIN4_PORT
152 stm_moder_set(AO_ADC_PIN4_PORT, AO_ADC_PIN4_PIN, STM_MODER_ANALOG);
154 #ifdef AO_ADC_PIN5_PORT
155 stm_moder_set(AO_ADC_PIN5_PORT, AO_ADC_PIN5_PIN, STM_MODER_ANALOG);
157 #ifdef AO_ADC_PIN6_PORT
158 stm_moder_set(AO_ADC_PIN6_PORT, AO_ADC_PIN6_PIN, STM_MODER_ANALOG);
160 #ifdef AO_ADC_PIN7_PORT
161 stm_moder_set(AO_ADC_PIN7_PORT, AO_ADC_PIN7_PIN, STM_MODER_ANALOG);
163 #ifdef AO_ADC_PIN8_PORT
164 stm_moder_set(AO_ADC_PIN8_PORT, AO_ADC_PIN8_PIN, STM_MODER_ANALOG);
166 #ifdef AO_ADC_PIN9_PORT
167 stm_moder_set(AO_ADC_PIN9_PORT, AO_ADC_PIN9_PIN, STM_MODER_ANALOG);
169 #ifdef AO_ADC_PIN10_PORT
170 stm_moder_set(AO_ADC_PIN10_PORT, AO_ADC_PIN10_PIN, STM_MODER_ANALOG);
172 #ifdef AO_ADC_PIN11_PORT
173 stm_moder_set(AO_ADC_PIN11_PORT, AO_ADC_PIN11_PIN, STM_MODER_ANALOG);
175 #ifdef AO_ADC_PIN12_PORT
176 stm_moder_set(AO_ADC_PIN12_PORT, AO_ADC_PIN12_PIN, STM_MODER_ANALOG);
179 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_ADC1EN);
181 /* Turn off ADC during configuration */
184 stm_adc.cr1 = ((0 << STM_ADC_CR1_OVRIE ) |
185 (STM_ADC_CR1_RES_12 << STM_ADC_CR1_RES ) |
186 (0 << STM_ADC_CR1_AWDEN ) |
187 (0 << STM_ADC_CR1_JAWDEN ) |
188 (0 << STM_ADC_CR1_PDI ) |
189 (0 << STM_ADC_CR1_PDD ) |
190 (0 << STM_ADC_CR1_DISCNUM ) |
191 (0 << STM_ADC_CR1_JDISCEN ) |
192 (0 << STM_ADC_CR1_DISCEN ) |
193 (0 << STM_ADC_CR1_JAUTO ) |
194 (0 << STM_ADC_CR1_AWDSGL ) |
195 (1 << STM_ADC_CR1_SCAN ) |
196 (0 << STM_ADC_CR1_JEOCIE ) |
197 (0 << STM_ADC_CR1_AWDIE ) |
198 (0 << STM_ADC_CR1_EOCIE ) |
199 (0 << STM_ADC_CR1_AWDCH ));
201 /* 384 cycle sample time for everyone */
202 stm_adc.smpr1 = 0x3ffff;
203 stm_adc.smpr2 = 0x3fffffff;
204 stm_adc.smpr3 = 0x3fffffff;
206 stm_adc.sqr1 = ((AO_NUM_ADC - 1) << 20);
212 stm_adc.sqr5 |= (AO_ADC_SQ1 << 0);
215 stm_adc.sqr5 |= (AO_ADC_SQ2 << 5);
218 stm_adc.sqr5 |= (AO_ADC_SQ3 << 10);
221 stm_adc.sqr5 |= (AO_ADC_SQ4 << 15);
224 stm_adc.sqr5 |= (AO_ADC_SQ5 << 20);
227 stm_adc.sqr5 |= (AO_ADC_SQ6 << 25);
230 stm_adc.sqr4 |= (AO_ADC_SQ7 << 0);
233 stm_adc.sqr4 |= (AO_ADC_SQ8 << 5);
236 stm_adc.sqr4 |= (AO_ADC_SQ9 << 10);
239 stm_adc.sqr4 |= (AO_ADC_SQ10 << 15);
242 stm_adc.sqr4 |= (AO_ADC_SQ11 << 20);
245 stm_adc.sqr4 |= (AO_ADC_SQ12 << 25);
248 #error "need to finish stm_adc.sqr settings"
252 stm_adc.cr2 = AO_ADC_CR2_VAL;
254 /* Wait for ADC to be ready */
255 while (!(stm_adc.sr & (1 << STM_ADC_SR_ADONS)))
259 stm_adc.ccr = ((1 << STM_ADC_CCR_TSVREFE));
263 /* Clear any stale status bits */
266 ao_dma_alloc(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1));
268 ao_cmd_register(&ao_adc_cmds[0]);