mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-16 04:45:24 +03:00
use #defines
for ADC on stm32f10x
Make stm32f103 ADC more similar to stm32f30x
This commit is contained in:
parent
48cdf650d9
commit
e8f0e0d97c
1 changed files with 43 additions and 35 deletions
|
@ -33,6 +33,13 @@
|
||||||
#include "adc.h"
|
#include "adc.h"
|
||||||
#include "adc_impl.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
|
// Driver for STM32F103CB onboard ADC
|
||||||
//
|
//
|
||||||
// Naze32
|
// Naze32
|
||||||
|
@ -50,15 +57,13 @@ void adcInit(drv_adc_config_t *init)
|
||||||
UNUSED(init);
|
UNUSED(init);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ADC_InitTypeDef adc;
|
|
||||||
DMA_InitTypeDef dma;
|
|
||||||
GPIO_InitTypeDef GPIO_InitStructure;
|
|
||||||
|
|
||||||
uint8_t i;
|
uint8_t i;
|
||||||
uint8_t configuredAdcChannels = 0;
|
uint8_t configuredAdcChannels = 0;
|
||||||
|
|
||||||
memset(&adcConfig, 0, sizeof(adcConfig));
|
memset(&adcConfig, 0, sizeof(adcConfig));
|
||||||
|
|
||||||
|
GPIO_InitTypeDef GPIO_InitStructure;
|
||||||
GPIO_StructInit(&GPIO_InitStructure);
|
GPIO_StructInit(&GPIO_InitStructure);
|
||||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
|
||||||
|
|
||||||
|
@ -107,50 +112,53 @@ void adcInit(drv_adc_config_t *init)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
RCC_ADCCLKConfig(RCC_PCLK2_Div8); // 9MHz from 72MHz APB2 clock(HSE), 8MHz from 64MHz (HSI)
|
RCC_ADCCLKConfig(RCC_PCLK2_Div8); // 9MHz from 72MHz APB2 clock(HSE), 8MHz from 64MHz (HSI)
|
||||||
|
RCC_AHBPeriphClockCmd(ADC_AHB_PERIPHERAL, ENABLE);
|
||||||
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
|
RCC_APB2PeriphClockCmd(ADC_ABP2_PERIPHERAL, ENABLE);
|
||||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
|
|
||||||
|
|
||||||
// FIXME ADC driver assumes all the GPIO was already placed in 'AIN' mode
|
// FIXME ADC driver assumes all the GPIO was already placed in 'AIN' mode
|
||||||
|
|
||||||
DMA_DeInit(DMA1_Channel1);
|
DMA_DeInit(ADC_DMA_CHANNEL);
|
||||||
dma.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
|
DMA_InitTypeDef DMA_InitStructure;
|
||||||
dma.DMA_MemoryBaseAddr = (uint32_t)adcValues;
|
DMA_StructInit(&DMA_InitStructure);
|
||||||
dma.DMA_DIR = DMA_DIR_PeripheralSRC;
|
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC_INSTANCE->DR;
|
||||||
dma.DMA_BufferSize = configuredAdcChannels;
|
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcValues;
|
||||||
dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
|
||||||
dma.DMA_MemoryInc = configuredAdcChannels > 1 ? DMA_MemoryInc_Enable : DMA_MemoryInc_Disable;
|
DMA_InitStructure.DMA_BufferSize = configuredAdcChannels;
|
||||||
dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
|
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
||||||
dma.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
|
DMA_InitStructure.DMA_MemoryInc = configuredAdcChannels > 1 ? DMA_MemoryInc_Enable : DMA_MemoryInc_Disable;
|
||||||
dma.DMA_Mode = DMA_Mode_Circular;
|
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
|
||||||
dma.DMA_Priority = DMA_Priority_High;
|
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
|
||||||
dma.DMA_M2M = DMA_M2M_Disable;
|
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
|
||||||
DMA_Init(DMA1_Channel1, &dma);
|
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
|
||||||
DMA_Cmd(DMA1_Channel1, ENABLE);
|
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
|
||||||
|
DMA_Init(ADC_DMA_CHANNEL, &DMA_InitStructure);
|
||||||
|
DMA_Cmd(ADC_DMA_CHANNEL, ENABLE);
|
||||||
|
|
||||||
adc.ADC_Mode = ADC_Mode_Independent;
|
ADC_InitTypeDef ADC_InitStructure;
|
||||||
adc.ADC_ScanConvMode = configuredAdcChannels > 1 ? ENABLE : DISABLE;
|
ADC_StructInit(&ADC_InitStructure);
|
||||||
adc.ADC_ContinuousConvMode = ENABLE;
|
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
|
||||||
adc.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
|
ADC_InitStructure.ADC_ScanConvMode = configuredAdcChannels > 1 ? ENABLE : DISABLE;
|
||||||
adc.ADC_DataAlign = ADC_DataAlign_Right;
|
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
|
||||||
adc.ADC_NbrOfChannel = configuredAdcChannels;
|
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
|
||||||
ADC_Init(ADC1, &adc);
|
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
|
||||||
|
ADC_InitStructure.ADC_NbrOfChannel = configuredAdcChannels;
|
||||||
|
ADC_Init(ADC_INSTANCE, &ADC_InitStructure);
|
||||||
|
|
||||||
uint8_t rank = 1;
|
uint8_t rank = 1;
|
||||||
for (i = 0; i < ADC_CHANNEL_COUNT; i++) {
|
for (i = 0; i < ADC_CHANNEL_COUNT; i++) {
|
||||||
if (!adcConfig[i].enabled) {
|
if (!adcConfig[i].enabled) {
|
||||||
continue;
|
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_DMACmd(ADC_INSTANCE, ENABLE);
|
||||||
ADC_Cmd(ADC1, ENABLE);
|
ADC_Cmd(ADC_INSTANCE, ENABLE);
|
||||||
|
|
||||||
ADC_ResetCalibration(ADC1);
|
ADC_ResetCalibration(ADC_INSTANCE);
|
||||||
while(ADC_GetResetCalibrationStatus(ADC1));
|
while(ADC_GetResetCalibrationStatus(ADC_INSTANCE));
|
||||||
ADC_StartCalibration(ADC1);
|
ADC_StartCalibration(ADC_INSTANCE);
|
||||||
while(ADC_GetCalibrationStatus(ADC1));
|
while(ADC_GetCalibrationStatus(ADC_INSTANCE));
|
||||||
|
|
||||||
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
|
ADC_SoftwareStartConvCmd(ADC_INSTANCE, ENABLE);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue