From e8f0e0d97cdcc051aef8603c199d8e07593402e0 Mon Sep 17 00:00:00 2001 From: Petr Ledvina Date: Fri, 9 Oct 2015 13:10:03 +0200 Subject: [PATCH] use `#defines` for ADC on stm32f10x Make stm32f103 ADC more similar to stm32f30x --- src/main/drivers/adc_stm32f10x.c | 80 ++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/src/main/drivers/adc_stm32f10x.c b/src/main/drivers/adc_stm32f10x.c index a710fbba8..11dec5bfc 100644 --- a/src/main/drivers/adc_stm32f10x.c +++ b/src/main/drivers/adc_stm32f10x.c @@ -33,6 +33,13 @@ #include "adc.h" #include "adc_impl.h" +#ifndef ADC_INSTANCE +#define ADC_INSTANCE ADC1 +#define ADC_ABP2_PERIPHERAL RCC_APB2Periph_ADC1 +#define ADC_AHB_PERIPHERAL RCC_AHBPeriph_DMA1 +#define ADC_DMA_CHANNEL DMA1_Channel1 +#endif + // Driver for STM32F103CB onboard ADC // // Naze32 @@ -50,15 +57,13 @@ void adcInit(drv_adc_config_t *init) UNUSED(init); #endif - ADC_InitTypeDef adc; - DMA_InitTypeDef dma; - GPIO_InitTypeDef GPIO_InitStructure; uint8_t i; uint8_t configuredAdcChannels = 0; memset(&adcConfig, 0, sizeof(adcConfig)); + GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; @@ -107,50 +112,53 @@ void adcInit(drv_adc_config_t *init) #endif RCC_ADCCLKConfig(RCC_PCLK2_Div8); // 9MHz from 72MHz APB2 clock(HSE), 8MHz from 64MHz (HSI) - - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); - RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); + RCC_AHBPeriphClockCmd(ADC_AHB_PERIPHERAL, ENABLE); + RCC_APB2PeriphClockCmd(ADC_ABP2_PERIPHERAL, ENABLE); // FIXME ADC driver assumes all the GPIO was already placed in 'AIN' mode - DMA_DeInit(DMA1_Channel1); - dma.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; - dma.DMA_MemoryBaseAddr = (uint32_t)adcValues; - dma.DMA_DIR = DMA_DIR_PeripheralSRC; - dma.DMA_BufferSize = configuredAdcChannels; - dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable; - dma.DMA_MemoryInc = configuredAdcChannels > 1 ? DMA_MemoryInc_Enable : DMA_MemoryInc_Disable; - dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; - dma.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; - dma.DMA_Mode = DMA_Mode_Circular; - dma.DMA_Priority = DMA_Priority_High; - dma.DMA_M2M = DMA_M2M_Disable; - DMA_Init(DMA1_Channel1, &dma); - DMA_Cmd(DMA1_Channel1, ENABLE); - - adc.ADC_Mode = ADC_Mode_Independent; - adc.ADC_ScanConvMode = configuredAdcChannels > 1 ? ENABLE : DISABLE; - adc.ADC_ContinuousConvMode = ENABLE; - adc.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; - adc.ADC_DataAlign = ADC_DataAlign_Right; - adc.ADC_NbrOfChannel = configuredAdcChannels; - ADC_Init(ADC1, &adc); + DMA_DeInit(ADC_DMA_CHANNEL); + DMA_InitTypeDef DMA_InitStructure; + DMA_StructInit(&DMA_InitStructure); + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC_INSTANCE->DR; + DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcValues; + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; + DMA_InitStructure.DMA_BufferSize = configuredAdcChannels; + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_MemoryInc = configuredAdcChannels > 1 ? DMA_MemoryInc_Enable : DMA_MemoryInc_Disable; + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; + DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; + DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; + DMA_InitStructure.DMA_Priority = DMA_Priority_High; + DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; + DMA_Init(ADC_DMA_CHANNEL, &DMA_InitStructure); + DMA_Cmd(ADC_DMA_CHANNEL, ENABLE); + + ADC_InitTypeDef ADC_InitStructure; + ADC_StructInit(&ADC_InitStructure); + ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; + ADC_InitStructure.ADC_ScanConvMode = configuredAdcChannels > 1 ? ENABLE : DISABLE; + ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; + ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; + ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; + ADC_InitStructure.ADC_NbrOfChannel = configuredAdcChannels; + ADC_Init(ADC_INSTANCE, &ADC_InitStructure); uint8_t rank = 1; for (i = 0; i < ADC_CHANNEL_COUNT; i++) { if (!adcConfig[i].enabled) { continue; } - ADC_RegularChannelConfig(ADC1, adcConfig[i].adcChannel, rank++, adcConfig[i].sampleTime); + ADC_RegularChannelConfig(ADC_INSTANCE, adcConfig[i].adcChannel, rank++, adcConfig[i].sampleTime); } - ADC_DMACmd(ADC1, ENABLE); - ADC_Cmd(ADC1, ENABLE); + ADC_DMACmd(ADC_INSTANCE, ENABLE); + ADC_Cmd(ADC_INSTANCE, ENABLE); - ADC_ResetCalibration(ADC1); - while(ADC_GetResetCalibrationStatus(ADC1)); - ADC_StartCalibration(ADC1); - while(ADC_GetCalibrationStatus(ADC1)); + ADC_ResetCalibration(ADC_INSTANCE); + while(ADC_GetResetCalibrationStatus(ADC_INSTANCE)); + ADC_StartCalibration(ADC_INSTANCE); + while(ADC_GetCalibrationStatus(ADC_INSTANCE)); - ADC_SoftwareStartConvCmd(ADC1, ENABLE); + ADC_SoftwareStartConvCmd(ADC_INSTANCE, ENABLE); } -- 2.11.4.GIT