2 * Copyright © 2015 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 #define AO_ADC_DEBUG 0
23 static uint8_t ao_adc_ready;
26 * Callback from DMA ISR
28 * Mark time in ring, shut down DMA engine
30 static void ao_adc_done(int index)
34 stm_adc.isr = ((1 << STM_ADC_ISR_AWD) |
35 (1 << STM_ADC_ISR_OVR) |
36 (1 << STM_ADC_ISR_EOSEQ) |
37 (1 << STM_ADC_ISR_EOC));
39 AO_DATA_PRESENT(AO_DATA_ADC);
40 ao_dma_done_transfer(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC_1));
41 if (ao_data_present == AO_DATA_ALL) {
43 ao_data_ring[ao_data_head].ms5607_raw = ao_ms5607_current;
46 ao_data_ring[ao_data_head].mma655x = ao_mma655x_current;
49 ao_data_ring[ao_data_head].hmc5883 = ao_hmc5883_current;
52 ao_data_ring[ao_data_head].mpu6000 = ao_mpu6000_current;
54 ao_data_ring[ao_data_head].tick = ao_tick_count;
55 ao_data_head = ao_data_ring_next(ao_data_head);
56 ao_wakeup((void *) &ao_data_head);
62 * Start the ADC sequence using the DMA engine
71 ao_dma_set_transfer(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC_1),
73 (void *) (&ao_data_ring[ao_data_head].adc),
75 (0 << STM_DMA_CCR_MEM2MEM) |
76 (STM_DMA_CCR_PL_HIGH << STM_DMA_CCR_PL) |
77 (STM_DMA_CCR_MSIZE_16 << STM_DMA_CCR_MSIZE) |
78 (STM_DMA_CCR_PSIZE_16 << STM_DMA_CCR_PSIZE) |
79 (1 << STM_DMA_CCR_MINC) |
80 (0 << STM_DMA_CCR_PINC) |
81 (0 << STM_DMA_CCR_CIRC) |
82 (STM_DMA_CCR_DIR_PER_TO_MEM << STM_DMA_CCR_DIR) |
83 (1 << STM_DMA_CCR_TCIE));
84 ao_dma_set_isr(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC_1), ao_adc_done);
85 ao_dma_start(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC_1));
87 stm_adc.cr |= (1 << STM_ADC_CR_ADSTART);
93 struct ao_data packet;
106 ch = ao_cmd_decimal();
107 if (ao_cmd_status != ao_cmd_success)
109 if (ch < 0 || AO_NUM_ADC <= ch) {
110 ao_cmd_status = ao_cmd_syntax_error;
114 ao_timer_set_adc_interval(0);
117 printf("At top, data %u isr %04x cr %04x\n", stm_adc.dr, stm_adc.isr, stm_adc.cr);
119 if (stm_adc.cr & (1 << STM_ADC_CR_ADEN)) {
120 printf("Disabling\n"); flush();
121 stm_adc.cr |= (1 << STM_ADC_CR_ADDIS);
122 while (stm_adc.cr & (1 << STM_ADC_CR_ADDIS))
124 printf("Disabled\n"); flush();
127 /* Turn off everything */
128 stm_adc.cr &= ~((1 << STM_ADC_CR_ADCAL) |
129 (1 << STM_ADC_CR_ADSTP) |
130 (1 << STM_ADC_CR_ADSTART) |
131 (1 << STM_ADC_CR_ADEN));
133 printf("After disable, ADC status %04x\n", stm_adc.cr);
136 stm_adc.cfgr1 = ((0 << STM_ADC_CFGR1_AWDCH) | /* analog watchdog channel 0 */
137 (0 << STM_ADC_CFGR1_AWDEN) | /* Disable analog watchdog */
138 (0 << STM_ADC_CFGR1_AWDSGL) | /* analog watchdog on all channels */
139 (0 << STM_ADC_CFGR1_DISCEN) | /* Not discontinuous mode. All channels converted with one trigger */
140 (0 << STM_ADC_CFGR1_AUTOOFF) | /* Leave ADC running */
141 (1 << STM_ADC_CFGR1_WAIT) | /* Wait for data to be read before next conversion */
142 (0 << STM_ADC_CFGR1_CONT) | /* only one set of conversions per trigger */
143 (1 << STM_ADC_CFGR1_OVRMOD) | /* overwrite on overrun */
144 (STM_ADC_CFGR1_EXTEN_DISABLE << STM_ADC_CFGR1_EXTEN) | /* SW trigger */
145 (0 << STM_ADC_CFGR1_ALIGN) | /* Align to LSB */
146 (STM_ADC_CFGR1_RES_12 << STM_ADC_CFGR1_RES) | /* 12 bit resolution */
147 (STM_ADC_CFGR1_SCANDIR_UP << STM_ADC_CFGR1_SCANDIR) | /* scan 0 .. n */
148 (STM_ADC_CFGR1_DMACFG_ONESHOT << STM_ADC_CFGR1_DMACFG) | /* one set of conversions then stop */
149 (0 << STM_ADC_CFGR1_DMAEN)); /* disable DMA */
151 stm_adc.chselr = (1 << ch);
153 /* Longest sample time */
154 stm_adc.smpr = STM_ADC_SMPR_SMP_41_5 << STM_ADC_SMPR_SMP;
156 printf("Before enable, ADC status %04x\n", stm_adc.cr); flush();
158 stm_adc.cr |= (1 << STM_ADC_CR_ADEN);
159 while ((stm_adc.isr & (1 << STM_ADC_ISR_ADRDY)) == 0)
163 stm_adc.cr |= (1 << STM_ADC_CR_ADSTART);
165 /* Wait for conversion complete */
166 while (!(stm_adc.isr & (1 << STM_ADC_ISR_EOC)))
170 printf ("value %u, cr is %04x isr is %04x\n",
171 value, stm_adc.cr, stm_adc.isr);
175 stm_adc.isr = ((1 << STM_ADC_ISR_AWD) |
176 (1 << STM_ADC_ISR_OVR) |
177 (1 << STM_ADC_ISR_EOSEQ) |
178 (1 << STM_ADC_ISR_EOC));
182 const struct ao_cmds ao_adc_cmds[] = {
183 { ao_adc_dump, "a\0Display current ADC values" },
185 { ao_adc_one, "A ch\0Display one ADC channel" },
196 stm_rcc.apb2rstr |= (1 << STM_RCC_APB2RSTR_ADCRST);
197 stm_rcc.apb2rstr &= ~(1 << STM_RCC_APB2RSTR_ADCRST);
199 /* Turn on ADC pins */
200 stm_rcc.ahbenr |= AO_ADC_RCC_AHBENR;
202 #ifdef AO_ADC_PIN0_PORT
203 stm_moder_set(AO_ADC_PIN0_PORT, AO_ADC_PIN0_PIN, STM_MODER_ANALOG);
204 stm_pupdr_set(AO_ADC_PIN0_PORT, AO_ADC_PIN0_PIN, STM_PUPDR_NONE);
206 #ifdef AO_ADC_PIN1_PORT
207 stm_moder_set(AO_ADC_PIN1_PORT, AO_ADC_PIN1_PIN, STM_MODER_ANALOG);
208 stm_pupdr_set(AO_ADC_PIN1_PORT, AO_ADC_PIN1_PIN, STM_PUPDR_NONE);
210 #ifdef AO_ADC_PIN2_PORT
211 stm_moder_set(AO_ADC_PIN2_PORT, AO_ADC_PIN2_PIN, STM_MODER_ANALOG);
212 stm_pupdr_set(AO_ADC_PIN2_PORT, AO_ADC_PIN2_PIN, STM_PUPDR_NONE);
214 #ifdef AO_ADC_PIN3_PORT
215 stm_moder_set(AO_ADC_PIN3_PORT, AO_ADC_PIN3_PIN, STM_MODER_ANALOG);
216 stm_pupdr_set(AO_ADC_PIN3_PORT, AO_ADC_PIN3_PIN, STM_PUPDR_NONE);
218 #ifdef AO_ADC_PIN4_PORT
219 stm_moder_set(AO_ADC_PIN4_PORT, AO_ADC_PIN4_PIN, STM_MODER_ANALOG);
220 stm_pupdr_set(AO_ADC_PIN4_PORT, AO_ADC_PIN4_PIN, STM_PUPDR_NONE);
222 #ifdef AO_ADC_PIN5_PORT
223 stm_moder_set(AO_ADC_PIN5_PORT, AO_ADC_PIN5_PIN, STM_MODER_ANALOG);
224 stm_pupdr_set(AO_ADC_PIN5_PORT, AO_ADC_PIN5_PIN, STM_PUPDR_NONE);
226 #ifdef AO_ADC_PIN6_PORT
227 stm_moder_set(AO_ADC_PIN6_PORT, AO_ADC_PIN6_PIN, STM_MODER_ANALOG);
228 stm_pupdr_set(AO_ADC_PIN6_PORT, AO_ADC_PIN6_PIN, STM_PUPDR_NONE);
230 #ifdef AO_ADC_PIN7_PORT
231 stm_moder_set(AO_ADC_PIN7_PORT, AO_ADC_PIN7_PIN, STM_MODER_ANALOG);
232 stm_pupdr_set(AO_ADC_PIN7_PORT, AO_ADC_PIN7_PIN, STM_PUPDR_NONE);
234 #ifdef AO_ADC_PIN24_PORT
235 #error "Too many ADC ports"
238 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_ADCEN);
242 chselr |= (1 << AO_ADC_PIN0_CH);
245 chselr |= (1 << AO_ADC_PIN1_CH);
248 chselr |= (1 << AO_ADC_PIN2_CH);
251 chselr |= (1 << AO_ADC_PIN3_CH);
254 chselr |= (1 << AO_ADC_PIN4_CH);
257 chselr |= (1 << AO_ADC_PIN5_CH);
260 chselr |= (1 << AO_ADC_PIN6_CH);
263 chselr |= (1 << AO_ADC_PIN7_CH);
266 #error Need more ADC defines
269 /* Wait for ADC to be idle */
270 while (stm_adc.cr & ((1 << STM_ADC_CR_ADCAL) |
271 (1 << STM_ADC_CR_ADDIS)))
275 if (stm_adc.cr & (1 << STM_ADC_CR_ADEN)) {
276 stm_adc.cr |= (1 << STM_ADC_CR_ADDIS);
277 while (stm_adc.cr & (1 << STM_ADC_CR_ADDIS))
281 /* Turn off everything */
282 stm_adc.cr &= ~((1 << STM_ADC_CR_ADCAL) |
283 (1 << STM_ADC_CR_ADSTP) |
284 (1 << STM_ADC_CR_ADSTART) |
285 (1 << STM_ADC_CR_ADEN));
288 stm_adc.cfgr1 = ((0 << STM_ADC_CFGR1_AWDCH) | /* analog watchdog channel 0 */
289 (0 << STM_ADC_CFGR1_AWDEN) | /* Disable analog watchdog */
290 (0 << STM_ADC_CFGR1_AWDSGL) | /* analog watchdog on all channels */
291 (0 << STM_ADC_CFGR1_DISCEN) | /* Not discontinuous mode. All channels converted with one trigger */
292 (0 << STM_ADC_CFGR1_AUTOOFF) | /* Leave ADC running */
293 (1 << STM_ADC_CFGR1_WAIT) | /* Wait for data to be read before next conversion */
294 (0 << STM_ADC_CFGR1_CONT) | /* only one set of conversions per trigger */
295 (1 << STM_ADC_CFGR1_OVRMOD) | /* overwrite on overrun */
296 (STM_ADC_CFGR1_EXTEN_DISABLE << STM_ADC_CFGR1_EXTEN) | /* SW trigger */
297 (0 << STM_ADC_CFGR1_ALIGN) | /* Align to LSB */
298 (STM_ADC_CFGR1_RES_12 << STM_ADC_CFGR1_RES) | /* 12 bit resolution */
299 (STM_ADC_CFGR1_SCANDIR_UP << STM_ADC_CFGR1_SCANDIR) | /* scan 0 .. n */
300 (STM_ADC_CFGR1_DMACFG_ONESHOT << STM_ADC_CFGR1_DMACFG) | /* one set of conversions then stop */
301 (1 << STM_ADC_CFGR1_DMAEN)); /* enable DMA */
304 stm_adc.cfgr2 = STM_ADC_CFGR2_CKMODE_PCLK_2 << STM_ADC_CFGR2_CKMODE;
306 /* Shortest sample time */
307 stm_adc.smpr = STM_ADC_SMPR_SMP_71_5 << STM_ADC_SMPR_SMP;
309 stm_adc.chselr = chselr;
311 stm_adc.ccr = ((0 << STM_ADC_CCR_VBATEN) |
312 (0 << STM_ADC_CCR_TSEN) |
313 (0 << STM_ADC_CCR_VREFEN));
316 stm_adc.cr |= (1 << STM_ADC_CR_ADCAL);
317 while ((stm_adc.cr & (1 << STM_ADC_CR_ADCAL)) != 0)
321 stm_adc.cr |= (1 << STM_ADC_CR_ADEN);
322 while ((stm_adc.isr & (1 << STM_ADC_ISR_ADRDY)) == 0)
325 /* Clear any stale status bits */
329 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGCOMPEN);
331 /* Set ADC to use DMA channel 1 (option 1) */
332 stm_syscfg.cfgr1 &= ~(1 << STM_SYSCFG_CFGR1_ADC_DMA_RMP);
334 ao_dma_alloc(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC_1));
336 ao_cmd_register(&ao_adc_cmds[0]);