Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / adc.h
blobf122d2dea0fe4f08af3ac739b50662cac9b2ed1c
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)
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 #endif
80 ADC_CHANNEL_COUNT
81 } AdcChannel;
83 typedef struct adcOperatingConfig_s {
84 ioTag_t tag;
85 #if defined(STM32H7) || defined(STM32G4)
86 ADCDevice adcDevice; // ADCDEV_x for this input
87 uint32_t adcChannel; // Channel number for this input. Note that H7 and G4 HAL requires this to be 32-bit encoded number.
88 #else
89 uint8_t adcChannel; // ADCy_INxx channel number for this input (XXX May be consolidated with uint32_t case)
90 #endif
91 uint8_t dmaIndex; // index into DMA buffer in case of sparse channels
92 bool enabled;
93 uint8_t sampleTime;
94 } adcOperatingConfig_t;
96 struct adcConfig_s;
97 void adcInit(const struct adcConfig_s *config);
98 uint16_t adcGetChannel(uint8_t channel);
100 #ifdef USE_ADC_INTERNAL
101 bool adcInternalIsBusy(void);
102 void adcInternalStartConversion(void);
103 uint16_t adcInternalReadVrefint(void);
104 uint16_t adcInternalReadTempsensor(void);
105 uint16_t adcInternalCompensateVref(uint16_t vrefAdcValue);
106 int16_t adcInternalComputeTemperature(uint16_t tempAdcValue, uint16_t vrefValue);
107 #endif
109 #if !defined(SIMULATOR_BUILD)
110 ADCDevice adcDeviceByInstance(ADC_TypeDef *instance);
111 #endif