2 * This file is part of Cleanflight.
4 * Cleanflight 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, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
24 #include "build_config.h"
28 #include "sensors/sensors.h" // FIXME dependency into the main code
35 // Driver for STM32F103CB onboard ADC
38 // Battery Voltage (VBAT) is connected to PA4 (ADC1_IN4) with 10k:1k divider
39 // RSSI ADC uses CH2 (PA1, ADC1_IN1)
40 // Current ADC uses CH8 (PB1, ADC1_IN9)
42 // NAZE rev.5 hardware has PA5 (ADC1_IN5) on breakout pad on bottom of board
44 // CC3D Only one ADC channel supported currently, for battery on S5_IN/PA0
46 extern adc_config_t adcConfig
[ADC_CHANNEL_COUNT
];
47 extern volatile uint16_t adcValues
[ADC_CHANNEL_COUNT
];
49 void adcInit(drv_adc_config_t
*init
)
51 #if defined(CJMCU) || defined(CC3D)
57 GPIO_InitTypeDef GPIO_InitStructure
;
60 uint8_t configuredAdcChannels
= 0;
62 memset(&adcConfig
, 0, sizeof(adcConfig
));
64 GPIO_StructInit(&GPIO_InitStructure
);
65 GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_AIN
;
67 #ifdef VBAT_ADC_GPIO_PIN
68 GPIO_InitStructure
.GPIO_Pin
= VBAT_ADC_GPIO_PIN
;
69 GPIO_Init(VBAT_ADC_GPIO
, &GPIO_InitStructure
);
70 adcConfig
[ADC_BATTERY
].adcChannel
= VBAT_ADC_CHANNEL
;
71 adcConfig
[ADC_BATTERY
].dmaIndex
= configuredAdcChannels
++;
72 adcConfig
[ADC_BATTERY
].enabled
= true;
73 adcConfig
[ADC_BATTERY
].sampleTime
= ADC_SampleTime_239Cycles5
;
76 #ifdef EXTERNAL1_ADC_GPIO
77 if (init
->enableExternal1
) {
78 GPIO_InitStructure
.GPIO_Pin
= EXTERNAL1_ADC_GPIO_PIN
;
79 GPIO_Init(EXTERNAL1_ADC_GPIO
, &GPIO_InitStructure
);
80 adcConfig
[ADC_EXTERNAL1
].adcChannel
= EXTERNAL1_ADC_CHANNEL
;
81 adcConfig
[ADC_EXTERNAL1
].dmaIndex
= configuredAdcChannels
++;
82 adcConfig
[ADC_EXTERNAL1
].enabled
= true;
83 adcConfig
[ADC_EXTERNAL1
].sampleTime
= ADC_SampleTime_239Cycles5
;
88 if (init
->enableRSSI
) {
89 GPIO_InitStructure
.GPIO_Pin
= RSSI_ADC_GPIO_PIN
;
90 GPIO_Init(RSSI_ADC_GPIO
, &GPIO_InitStructure
);
91 adcConfig
[ADC_RSSI
].adcChannel
= RSSI_ADC_CHANNEL
;
92 adcConfig
[ADC_RSSI
].dmaIndex
= configuredAdcChannels
++;
93 adcConfig
[ADC_RSSI
].enabled
= true;
94 adcConfig
[ADC_RSSI
].sampleTime
= ADC_SampleTime_239Cycles5
;
98 #ifdef CURRENT_METER_ADC_GPIO
99 if (init
->enableCurrentMeter
) {
100 GPIO_InitStructure
.GPIO_Pin
= CURRENT_METER_ADC_GPIO_PIN
;
101 GPIO_Init(CURRENT_METER_ADC_GPIO
, &GPIO_InitStructure
);
102 adcConfig
[ADC_CURRENT
].adcChannel
= CURRENT_METER_ADC_CHANNEL
;
103 adcConfig
[ADC_CURRENT
].dmaIndex
= configuredAdcChannels
++;
104 adcConfig
[ADC_CURRENT
].enabled
= true;
105 adcConfig
[ADC_CURRENT
].sampleTime
= ADC_SampleTime_239Cycles5
;
109 RCC_ADCCLKConfig(RCC_PCLK2_Div8
); // 9MHz from 72MHz APB2 clock(HSE), 8MHz from 64MHz (HSI)
111 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1
, ENABLE
);
112 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1
, ENABLE
);
114 // FIXME ADC driver assumes all the GPIO was already placed in 'AIN' mode
116 DMA_DeInit(DMA1_Channel1
);
117 dma
.DMA_PeripheralBaseAddr
= (uint32_t)&ADC1
->DR
;
118 dma
.DMA_MemoryBaseAddr
= (uint32_t)adcValues
;
119 dma
.DMA_DIR
= DMA_DIR_PeripheralSRC
;
120 dma
.DMA_BufferSize
= configuredAdcChannels
;
121 dma
.DMA_PeripheralInc
= DMA_PeripheralInc_Disable
;
122 dma
.DMA_MemoryInc
= configuredAdcChannels
> 1 ? DMA_MemoryInc_Enable
: DMA_MemoryInc_Disable
;
123 dma
.DMA_PeripheralDataSize
= DMA_PeripheralDataSize_HalfWord
;
124 dma
.DMA_MemoryDataSize
= DMA_MemoryDataSize_HalfWord
;
125 dma
.DMA_Mode
= DMA_Mode_Circular
;
126 dma
.DMA_Priority
= DMA_Priority_High
;
127 dma
.DMA_M2M
= DMA_M2M_Disable
;
128 DMA_Init(DMA1_Channel1
, &dma
);
129 DMA_Cmd(DMA1_Channel1
, ENABLE
);
131 adc
.ADC_Mode
= ADC_Mode_Independent
;
132 adc
.ADC_ScanConvMode
= configuredAdcChannels
> 1 ? ENABLE
: DISABLE
;
133 adc
.ADC_ContinuousConvMode
= ENABLE
;
134 adc
.ADC_ExternalTrigConv
= ADC_ExternalTrigConv_None
;
135 adc
.ADC_DataAlign
= ADC_DataAlign_Right
;
136 adc
.ADC_NbrOfChannel
= configuredAdcChannels
;
137 ADC_Init(ADC1
, &adc
);
140 for (i
= 0; i
< ADC_CHANNEL_COUNT
; i
++) {
141 if (!adcConfig
[i
].enabled
) {
144 ADC_RegularChannelConfig(ADC1
, adcConfig
[i
].adcChannel
, rank
++, adcConfig
[i
].sampleTime
);
147 ADC_DMACmd(ADC1
, ENABLE
);
148 ADC_Cmd(ADC1
, ENABLE
);
150 ADC_ResetCalibration(ADC1
);
151 while(ADC_GetResetCalibrationStatus(ADC1
));
152 ADC_StartCalibration(ADC1
);
153 while(ADC_GetCalibrationStatus(ADC1
));
155 ADC_SoftwareStartConvCmd(ADC1
, ENABLE
);