2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
25 #include "drivers/io_types.h"
26 #include "drivers/time.h"
29 #define ADC_INSTANCE ADC1
32 #if defined(STM32F4) || defined(STM32F7) || defined(APM32F4)
33 #ifndef ADC1_DMA_STREAM
34 #define ADC1_DMA_STREAM DMA2_Stream4 // ST0 or ST4
37 #ifndef ADC2_DMA_STREAM
38 #define ADC2_DMA_STREAM DMA2_Stream3 // ST2 or ST3
41 #ifndef ADC3_DMA_STREAM
42 #define ADC3_DMA_STREAM DMA2_Stream0 // ST0 or ST1
46 typedef enum ADCDevice
{
64 #define ADC_CFG_TO_DEV(x) ((x) - 1)
65 #define ADC_DEV_TO_CFG(x) ((x) + 1)
72 #if defined(STM32H7) || defined(STM32G4)
73 // On H7 and G4, internal sensors are treated in the similar fashion as regular ADC inputs
74 ADC_CHANNEL_INTERNAL_FIRST_ID
= 4,
79 #elif defined(AT32F435)
80 ADC_CHANNEL_INTERNAL_FIRST_ID
= 4,
90 typedef struct adcOperatingConfig_s
{
92 #if defined(STM32H7) || defined(STM32G4) || defined(AT32F435)
93 ADCDevice adcDevice
; // ADCDEV_x for this input
94 uint32_t adcChannel
; // Channel number for this input. Note that H7 and G4 HAL requires this to be 32-bit encoded number.
96 uint8_t adcChannel
; // ADCy_INxx channel number for this input (XXX May be consolidated with uint32_t case)
98 uint8_t dmaIndex
; // index into DMA buffer in case of sparse channels
101 } adcOperatingConfig_t
;
104 void adcInit(const struct adcConfig_s
*config
);
105 uint16_t adcGetChannel(uint8_t channel
);
107 #ifdef USE_ADC_INTERNAL
108 bool adcInternalIsBusy(void);
109 void adcInternalStartConversion(void);
110 uint16_t adcInternalReadVrefint(void);
111 uint16_t adcInternalReadTempsensor(void);
112 uint16_t adcInternalCompensateVref(uint16_t vrefAdcValue
);
113 int16_t adcInternalComputeTemperature(uint16_t tempAdcValue
, uint16_t vrefValue
);
116 #if !defined(SIMULATOR_BUILD)
117 ADCDevice
adcDeviceByInstance(const ADC_TypeDef
*instance
);