New SPI API supporting DMA
[betaflight.git] / src / main / drivers / adc.h
blob15abf7fdb5829426f6547f0f0e82a5f0e10b82ca
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 = 4,
75 ADC_TEMPSENSOR = 4,
76 ADC_VREFINT = 5,
77 #endif
78 ADC_CHANNEL_COUNT
79 } AdcChannel;
81 typedef struct adcOperatingConfig_s {
82 ioTag_t tag;
83 #if defined(STM32H7) || defined(STM32G4)
84 ADCDevice adcDevice; // ADCDEV_x for this input
85 uint32_t adcChannel; // Channel number for this input. Note that H7 and G4 HAL requires this to be 32-bit encoded number.
86 #else
87 uint8_t adcChannel; // ADCy_INxx channel number for this input (XXX May be consolidated with uint32_t case)
88 #endif
89 uint8_t dmaIndex; // index into DMA buffer in case of sparse channels
90 bool enabled;
91 uint8_t sampleTime;
92 } adcOperatingConfig_t;
94 struct adcConfig_s;
95 void adcInit(const struct adcConfig_s *config);
96 uint16_t adcGetChannel(uint8_t channel);
98 #ifdef USE_ADC_INTERNAL
99 bool adcInternalIsBusy(void);
100 void adcInternalStartConversion(void);
101 uint16_t adcInternalReadVrefint(void);
102 uint16_t adcInternalReadTempsensor(void);
103 uint16_t adcInternalCompensateVref(uint16_t vrefAdcValue);
104 int16_t adcInternalComputeTemperature(uint16_t tempAdcValue, uint16_t vrefValue);
105 #endif
107 #if !defined(SIMULATOR_BUILD)
108 ADCDevice adcDeviceByInstance(ADC_TypeDef *instance);
109 #endif