mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-17 21:35:44 +03:00
CHEBUZZF3 - Implement ADC driver for 3 ADC channels. Use STM32F3 linker
script so it uses the right values for RAM and FLASH size.
This commit is contained in:
parent
b3ee895f97
commit
4be9d953ac
9 changed files with 398 additions and 89 deletions
|
@ -4,95 +4,33 @@
|
|||
#include "platform.h"
|
||||
#include "system_common.h"
|
||||
|
||||
#include "sensors_common.h" // FIXME dependency into the main code
|
||||
|
||||
#include "accgyro_common.h"
|
||||
|
||||
#include "adc_common.h"
|
||||
|
||||
// Driver for STM32F103CB onboard ADC
|
||||
// VBAT is connected to PA4 (ADC1_IN4) with 10k:1k divider
|
||||
// rev.5 hardware has PA5 (ADC1_IN5) on breakout pad on bottom of board
|
||||
// Additional channel can be stolen from RC_CH2 (PA1, ADC1_IN1) or
|
||||
// RC_CH8 (PB1, ADC1_IN9) by using set power_adc_channel=1|9
|
||||
adc_config_t adcConfig[ADC_CHANNEL_COUNT];
|
||||
volatile uint16_t adcValues[ADC_CHANNEL_COUNT];
|
||||
uint8_t adcChannelCount = 0;
|
||||
|
||||
typedef struct adc_config_t {
|
||||
uint8_t adcChannel; // ADC1_INxx channel number
|
||||
uint8_t dmaIndex; // index into DMA buffer in case of sparse channels
|
||||
} adc_config_t;
|
||||
|
||||
static adc_config_t adcConfig[ADC_CHANNEL_MAX];
|
||||
static volatile uint16_t adcValues[ADC_CHANNEL_MAX];
|
||||
|
||||
void adcInit(drv_adc_config_t *init)
|
||||
{
|
||||
#ifndef STM32F3DISCOVERY
|
||||
ADC_InitTypeDef adc;
|
||||
DMA_InitTypeDef dma;
|
||||
int numChannels = 1, i;
|
||||
|
||||
// configure always-present battery index (ADC4)
|
||||
adcConfig[ADC_BATTERY].adcChannel = ADC_Channel_4;
|
||||
adcConfig[ADC_BATTERY].dmaIndex = numChannels - 1;
|
||||
|
||||
// optional ADC5 input on rev.5 hardware
|
||||
if (hse_value == 12000000) {
|
||||
numChannels++;
|
||||
adcConfig[ADC_EXTERNAL1].adcChannel = ADC_Channel_5;
|
||||
adcConfig[ADC_EXTERNAL1].dmaIndex = numChannels - 1;
|
||||
}
|
||||
// another channel can be stolen from PWM for current measurement or other things
|
||||
if (init->powerAdcChannel > 0) {
|
||||
numChannels++;
|
||||
adcConfig[ADC_EXTERNAL2].adcChannel = init->powerAdcChannel;
|
||||
adcConfig[ADC_EXTERNAL2].dmaIndex = numChannels - 1;
|
||||
}
|
||||
|
||||
// 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 = numChannels;
|
||||
dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
||||
dma.DMA_MemoryInc = numChannels > 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 = numChannels > 1 ? ENABLE : DISABLE;
|
||||
adc.ADC_ContinuousConvMode = ENABLE;
|
||||
adc.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
|
||||
adc.ADC_DataAlign = ADC_DataAlign_Right;
|
||||
adc.ADC_NbrOfChannel = numChannels;
|
||||
ADC_Init(ADC1, &adc);
|
||||
|
||||
// fixed ADC4
|
||||
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_28Cycles5);
|
||||
// configure any additional ADC channels (2 + n)
|
||||
for (i = 1; i < numChannels; i++)
|
||||
ADC_RegularChannelConfig(ADC1, adcConfig[i].adcChannel, i + 1, ADC_SampleTime_28Cycles5);
|
||||
ADC_DMACmd(ADC1, ENABLE);
|
||||
|
||||
ADC_Cmd(ADC1, ENABLE);
|
||||
|
||||
// Calibrate ADC
|
||||
ADC_ResetCalibration(ADC1);
|
||||
while(ADC_GetResetCalibrationStatus(ADC1));
|
||||
ADC_StartCalibration(ADC1);
|
||||
while(ADC_GetCalibrationStatus(ADC1));
|
||||
|
||||
// Fire off ADC
|
||||
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
|
||||
#endif
|
||||
}
|
||||
extern int16_t debug[4];
|
||||
|
||||
uint16_t adcGetChannel(uint8_t channel)
|
||||
{
|
||||
#if 0
|
||||
switch(adcChannelCount) {
|
||||
case 3:
|
||||
debug[2] = adcValues[adcConfig[2].dmaIndex];
|
||||
/* no break */
|
||||
case 2:
|
||||
debug[1] = adcValues[adcConfig[1].dmaIndex];
|
||||
/* no break */
|
||||
case 1:
|
||||
debug[0] = adcValues[adcConfig[0].dmaIndex];
|
||||
/* no break */
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
return adcValues[adcConfig[channel].dmaIndex];
|
||||
}
|
||||
|
||||
|
|
|
@ -4,9 +4,16 @@ typedef enum {
|
|||
ADC_BATTERY = 0,
|
||||
ADC_EXTERNAL1 = 1,
|
||||
ADC_EXTERNAL2 = 2,
|
||||
ADC_CHANNEL_MAX = 3
|
||||
ADC_CHANNEL_MAX = ADC_EXTERNAL2
|
||||
} AdcChannel;
|
||||
|
||||
#define ADC_CHANNEL_COUNT (ADC_CHANNEL_MAX + 1)
|
||||
|
||||
typedef struct adc_config_t {
|
||||
uint8_t adcChannel; // ADC1_INxx channel number
|
||||
uint8_t dmaIndex; // index into DMA buffer in case of sparse channels
|
||||
} adc_config_t;
|
||||
|
||||
typedef struct drv_adc_config_t {
|
||||
uint8_t powerAdcChannel; // which channel used for current monitor, allowed PA1, PB1 (ADC_Channel_1, ADC_Channel_9)
|
||||
} drv_adc_config_t;
|
||||
|
|
88
src/drivers/adc_stm32f10x.c
Normal file
88
src/drivers/adc_stm32f10x.c
Normal file
|
@ -0,0 +1,88 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "platform.h"
|
||||
#include "system_common.h"
|
||||
|
||||
#include "sensors_common.h" // FIXME dependency into the main code
|
||||
|
||||
#include "accgyro_common.h"
|
||||
|
||||
#include "adc_common.h"
|
||||
|
||||
// Driver for STM32F103CB onboard ADC
|
||||
// VBAT is connected to PA4 (ADC1_IN4) with 10k:1k divider
|
||||
// rev.5 hardware has PA5 (ADC1_IN5) on breakout pad on bottom of board
|
||||
// Additional channel can be stolen from RC_CH2 (PA1, ADC1_IN1) or
|
||||
// RC_CH8 (PB1, ADC1_IN9) by using set power_adc_channel=1|9
|
||||
|
||||
extern adc_config_t adcConfig[ADC_CHANNEL_COUNT];
|
||||
extern volatile uint16_t adcValues[ADC_CHANNEL_COUNT];
|
||||
|
||||
uint8_t adcChannelCount;
|
||||
|
||||
void adcInit(drv_adc_config_t *init)
|
||||
{
|
||||
ADC_InitTypeDef adc;
|
||||
DMA_InitTypeDef dma;
|
||||
uint8_t i;
|
||||
|
||||
// configure always-present battery index (ADC4)
|
||||
adcConfig[ADC_BATTERY].adcChannel = ADC_Channel_4;
|
||||
adcConfig[ADC_BATTERY].dmaIndex = adcChannelCount - 1;
|
||||
|
||||
// optional ADC5 input on rev.5 hardware
|
||||
if (hse_value == 12000000) {
|
||||
adcChannelCount++;
|
||||
adcConfig[ADC_EXTERNAL1].adcChannel = ADC_Channel_5;
|
||||
adcConfig[ADC_EXTERNAL1].dmaIndex = adcChannelCount - 1;
|
||||
}
|
||||
// another channel can be stolen from PWM for current measurement or other things
|
||||
if (init->powerAdcChannel > 0) {
|
||||
adcChannelCount++;
|
||||
adcConfig[ADC_EXTERNAL2].adcChannel = init->powerAdcChannel;
|
||||
adcConfig[ADC_EXTERNAL2].dmaIndex = adcChannelCount - 1;
|
||||
}
|
||||
|
||||
// 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 = adcChannelCount;
|
||||
dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
||||
dma.DMA_MemoryInc = adcChannelCount > 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 = adcChannelCount > 1 ? ENABLE : DISABLE;
|
||||
adc.ADC_ContinuousConvMode = ENABLE;
|
||||
adc.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
|
||||
adc.ADC_DataAlign = ADC_DataAlign_Right;
|
||||
adc.ADC_NbrOfChannel = adcChannelCount;
|
||||
ADC_Init(ADC1, &adc);
|
||||
|
||||
// fixed ADC4
|
||||
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_28Cycles5);
|
||||
// configure any additional ADC channels (2 + n)
|
||||
for (i = 1; i < adcChannelCount; i++)
|
||||
ADC_RegularChannelConfig(ADC1, adcConfig[i].adcChannel, i + 1, ADC_SampleTime_28Cycles5);
|
||||
ADC_DMACmd(ADC1, ENABLE);
|
||||
|
||||
ADC_Cmd(ADC1, ENABLE);
|
||||
|
||||
// Calibrate ADC
|
||||
ADC_ResetCalibration(ADC1);
|
||||
while(ADC_GetResetCalibrationStatus(ADC1));
|
||||
ADC_StartCalibration(ADC1);
|
||||
while(ADC_GetCalibrationStatus(ADC1));
|
||||
|
||||
// Fire off ADC
|
||||
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
|
||||
}
|
111
src/drivers/adc_stm32f30x.c
Normal file
111
src/drivers/adc_stm32f30x.c
Normal file
|
@ -0,0 +1,111 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "platform.h"
|
||||
#include "system_common.h"
|
||||
|
||||
#include "gpio_common.h"
|
||||
|
||||
#include "sensors_common.h" // FIXME dependency into the main code
|
||||
|
||||
#include "accgyro_common.h"
|
||||
|
||||
#include "adc_common.h"
|
||||
|
||||
extern adc_config_t adcConfig[ADC_CHANNEL_COUNT];
|
||||
extern volatile uint16_t adcValues[ADC_CHANNEL_COUNT];
|
||||
uint8_t adcChannelCount;
|
||||
|
||||
void adcInit(drv_adc_config_t *init)
|
||||
{
|
||||
ADC_InitTypeDef ADC_InitStructure;
|
||||
DMA_InitTypeDef DMA_InitStructure;
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
|
||||
uint8_t i;
|
||||
|
||||
adcConfig[ADC_BATTERY].adcChannel = ADC_Channel_6;
|
||||
adcConfig[ADC_BATTERY].dmaIndex = adcChannelCount;
|
||||
adcChannelCount++;
|
||||
|
||||
adcConfig[ADC_EXTERNAL1].adcChannel = ADC_Channel_7;
|
||||
adcConfig[ADC_EXTERNAL1].dmaIndex = adcChannelCount;
|
||||
adcChannelCount++;
|
||||
|
||||
adcConfig[ADC_EXTERNAL2].adcChannel = ADC_Channel_8;
|
||||
adcConfig[ADC_EXTERNAL2].dmaIndex = adcChannelCount;
|
||||
adcChannelCount++;
|
||||
|
||||
DMA_DeInit(DMA1_Channel1);
|
||||
|
||||
DMA_StructInit(&DMA_InitStructure);
|
||||
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
|
||||
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcValues;
|
||||
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
|
||||
DMA_InitStructure.DMA_BufferSize = adcChannelCount;
|
||||
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
||||
DMA_InitStructure.DMA_MemoryInc = adcChannelCount > 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(DMA1_Channel1, &DMA_InitStructure);
|
||||
|
||||
DMA_Cmd(DMA1_Channel1, ENABLE);
|
||||
|
||||
GPIO_StructInit(&GPIO_InitStructure);
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
|
||||
|
||||
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
||||
|
||||
// calibrate
|
||||
|
||||
ADC_VoltageRegulatorCmd(ADC1, ENABLE);
|
||||
delay(10);
|
||||
ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);
|
||||
ADC_StartCalibration(ADC1);
|
||||
while(ADC_GetCalibrationStatus(ADC1) != RESET);
|
||||
ADC_VoltageRegulatorCmd(ADC1, DISABLE);
|
||||
|
||||
|
||||
ADC_CommonInitTypeDef ADC_CommonInitStructure;
|
||||
|
||||
ADC_CommonStructInit(&ADC_CommonInitStructure);
|
||||
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
|
||||
ADC_CommonInitStructure.ADC_Clock = ADC_Clock_SynClkModeDiv4;
|
||||
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
|
||||
ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular;
|
||||
ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0;
|
||||
ADC_CommonInit(ADC1, &ADC_CommonInitStructure);
|
||||
|
||||
ADC_StructInit(&ADC_InitStructure);
|
||||
|
||||
ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable;
|
||||
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
|
||||
ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;
|
||||
ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
|
||||
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
|
||||
ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;
|
||||
ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;
|
||||
ADC_InitStructure.ADC_NbrOfRegChannel = adcChannelCount;
|
||||
|
||||
ADC_Init(ADC1, &ADC_InitStructure);
|
||||
|
||||
for (i = 0; i < adcChannelCount; i++)
|
||||
ADC_RegularChannelConfig(ADC1, adcConfig[i].adcChannel, i + 1, ADC_SampleTime_181Cycles5);
|
||||
|
||||
ADC_Cmd(ADC1, ENABLE);
|
||||
|
||||
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY));
|
||||
|
||||
ADC_DMAConfig(ADC1, ADC_DMAMode_Circular);
|
||||
|
||||
ADC_DMACmd(ADC1, ENABLE);
|
||||
|
||||
ADC_StartConversion(ADC1);
|
||||
}
|
||||
|
|
@ -103,6 +103,8 @@ void systemInit(bool overclock)
|
|||
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
|
||||
#endif
|
||||
#ifdef STM32F303xC
|
||||
RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div256); // 72 MHz divided by 256 = 281.25 kHz
|
||||
|
||||
RCC_APB1PeriphClockCmd(
|
||||
RCC_APB1Periph_TIM2 |
|
||||
RCC_APB1Periph_TIM3 |
|
||||
|
@ -143,7 +145,7 @@ void systemInit(bool overclock)
|
|||
gpio.mode = Mode_AIN;
|
||||
gpio.pin = Pin_All;
|
||||
#ifdef STM32F3DISCOVERY
|
||||
gpio.pin = Pin_All & ~(Pin_13|Pin_14);
|
||||
gpio.pin = Pin_All & ~(Pin_13|Pin_14|Pin_15); // Leave JTAG pins alone
|
||||
gpioInit(GPIOA, &gpio);
|
||||
gpio.pin = Pin_All;
|
||||
#else
|
||||
|
@ -151,6 +153,13 @@ void systemInit(bool overclock)
|
|||
#endif
|
||||
gpioInit(GPIOB, &gpio);
|
||||
gpioInit(GPIOC, &gpio);
|
||||
#ifdef STM32F303xC
|
||||
gpioInit(GPIOD, &gpio);
|
||||
gpioInit(GPIOE, &gpio);
|
||||
#ifdef CHEBUZZF3
|
||||
gpioInit(GPIOF, &gpio);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef STM32F10X_MD
|
||||
// Turn off JTAG port 'cause we're using the GPIO for leds
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue