Don't handle non-MSP characters on VTX MSP port (#14091)
[betaflight.git] / src / main / drivers / adc.h
blob9e272d9e198f9d0e98ea3e0abce8b60622cb387b
1 /*
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)
8 * any later version.
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/>.
21 #pragma once
23 #include <stdbool.h>
25 #include "drivers/io_types.h"
26 #include "drivers/time.h"
28 #ifndef ADC_INSTANCE
29 #define ADC_INSTANCE ADC1
30 #endif
32 #if defined(STM32F4) || defined(STM32F7) || defined(APM32F4)
33 #ifndef ADC1_DMA_STREAM
34 #define ADC1_DMA_STREAM DMA2_Stream4 // ST0 or ST4
35 #endif
37 #ifndef ADC2_DMA_STREAM
38 #define ADC2_DMA_STREAM DMA2_Stream3 // ST2 or ST3
39 #endif
41 #ifndef ADC3_DMA_STREAM
42 #define ADC3_DMA_STREAM DMA2_Stream0 // ST0 or ST1
43 #endif
44 #endif
46 typedef enum ADCDevice {
47 ADCINVALID = -1,
48 ADCDEV_1 = 0,
49 #if defined(ADC2)
50 ADCDEV_2,
51 #endif
52 #if defined(ADC3)
53 ADCDEV_3,
54 #endif
55 #if defined(ADC4)
56 ADCDEV_4,
57 #endif
58 #if defined(ADC5)
59 ADCDEV_5,
60 #endif
61 ADCDEV_COUNT
62 } ADCDevice;
64 #define ADC_CFG_TO_DEV(x) ((x) - 1)
65 #define ADC_DEV_TO_CFG(x) ((x) + 1)
67 typedef enum {
68 ADC_BATTERY = 0,
69 ADC_CURRENT = 1,
70 ADC_EXTERNAL1 = 2,
71 ADC_RSSI = 3,
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,
76 ADC_TEMPSENSOR = 4,
77 ADC_VREFINT = 5,
78 ADC_VBAT4 = 6,
79 #elif defined(AT32F435)
80 ADC_CHANNEL_INTERNAL_FIRST_ID = 4,
82 ADC_TEMPSENSOR = 4,
83 ADC_VREFINT = 5,
84 // ADC_VBAT4 = 6,
86 #endif
87 ADC_CHANNEL_COUNT
88 } AdcChannel;
90 typedef struct adcOperatingConfig_s {
91 ioTag_t tag;
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.
95 #else
96 uint8_t adcChannel; // ADCy_INxx channel number for this input (XXX May be consolidated with uint32_t case)
97 #endif
98 uint8_t dmaIndex; // index into DMA buffer in case of sparse channels
99 bool enabled;
100 uint8_t sampleTime;
101 } adcOperatingConfig_t;
103 struct adcConfig_s;
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);
114 #endif
116 #if !defined(SIMULATOR_BUILD)
117 ADCDevice adcDeviceByInstance(const ADC_TypeDef *instance);
118 #endif