Add RANGEFINDER and OPTICALFLOW MT build option (#14042)
[betaflight.git] / src / main / drivers / adc_impl.h
blobd9b337b45e94cc5adc52dc96b4c4d0140cb1df70
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 "drivers/adc.h"
24 #include "drivers/dma.h"
25 #include "drivers/io_types.h"
26 #include "drivers/rcc_types.h"
28 #if defined(STM32F4) || defined(STM32F7)
29 #define ADC_TAG_MAP_COUNT 16
30 #elif defined(AT32F435)
31 #ifdef USE_ADC_INTERNAL
32 #define ADC_TAG_MAP_COUNT 18
33 #else
34 #define ADC_TAG_MAP_COUNT 16
35 #endif
36 #elif defined(STM32H7)
37 #ifdef USE_ADC_INTERNAL
38 #define ADC_TAG_MAP_COUNT 30
39 #else
40 #define ADC_TAG_MAP_COUNT 28
41 #endif
42 #elif defined(STM32G4)
43 #ifdef USE_ADC_INTERNAL
44 #define ADC_TAG_MAP_COUNT 49
45 #else
46 #define ADC_TAG_MAP_COUNT 47
47 #endif
48 #elif defined(APM32F4)
49 #define ADC_TAG_MAP_COUNT 16
50 #else
51 #define ADC_TAG_MAP_COUNT 10
52 #endif
54 typedef struct adcTagMap_s {
55 ioTag_t tag;
56 uint8_t devices;
57 uint32_t channel;
58 #if defined(STM32H7) || defined(STM32G4) || defined(AT32F435)
59 uint8_t channelOrdinal;
60 #endif
61 } adcTagMap_t;
63 // Encoding for adcTagMap_t.devices
65 #define ADC_DEVICES_1 (1 << ADCDEV_1)
66 #define ADC_DEVICES_2 (1 << ADCDEV_2)
67 #define ADC_DEVICES_3 (1 << ADCDEV_3)
68 #define ADC_DEVICES_4 (1 << ADCDEV_4)
69 #define ADC_DEVICES_5 (1 << ADCDEV_5)
70 #define ADC_DEVICES_12 ((1 << ADCDEV_1)|(1 << ADCDEV_2))
71 #define ADC_DEVICES_34 ((1 << ADCDEV_3)|(1 << ADCDEV_4))
72 #define ADC_DEVICES_123 ((1 << ADCDEV_1)|(1 << ADCDEV_2)|(1 << ADCDEV_3))
73 #define ADC_DEVICES_345 ((1 << ADCDEV_3)|(1 << ADCDEV_4)|(1 << ADCDEV_5))
75 typedef struct adcDevice_s {
76 ADC_TypeDef* ADCx;
77 rccPeriphTag_t rccADC;
78 #if !defined(USE_DMA_SPEC)
79 dmaResource_t* dmaResource;
80 #if defined(STM32F4) || defined(STM32F7) || defined(STM32H7) || defined(STM32G4) || defined(APM32F4)
81 uint32_t channel;
82 #endif
83 #endif // !defined(USE_DMA_SPEC)
84 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4) || defined(APM32F4)
85 ADC_HandleTypeDef ADCHandle;
86 DMA_HandleTypeDef DmaHandle;
87 #endif
88 #if defined(STM32H7) || defined(STM32G4)
89 uint8_t irq;
90 uint32_t channelBits;
91 #endif
92 } adcDevice_t;
94 #ifdef USE_ADC_INTERNAL
95 extern int32_t adcVREFINTCAL; // ADC value (12-bit) of band gap with Vref = VREFINTCAL_VREF
96 extern int32_t adcTSCAL1;
97 extern int32_t adcTSCAL2;
98 extern int32_t adcTSSlopeK;
99 #endif
101 extern const adcDevice_t adcHardware[];
102 extern const adcTagMap_t adcTagMap[ADC_TAG_MAP_COUNT];
103 extern adcOperatingConfig_t adcOperatingConfig[ADC_CHANNEL_COUNT];
104 extern volatile uint16_t adcValues[ADC_CHANNEL_COUNT];
106 uint8_t adcChannelByTag(ioTag_t ioTag);
107 ADCDevice adcDeviceByInstance(const ADC_TypeDef *instance);
108 bool adcVerifyPin(ioTag_t tag, ADCDevice device);
110 // Marshall values in DMA instance/channel based order to adcChannel based order.
111 // Required for multi DMA instance implementation
112 void adcGetChannelValues(void);
115 // VREFINT and TEMPSENSOR related definitions
116 // These are shared among common adc.c and MCU dependent adc_stm32XXX.c
118 #ifdef STM32F7
119 // STM32F7 HAL library V1.12.0 defines VREFINT and TEMPSENSOR in stm32f7xx_ll_adc.h,
120 // which is not included from stm32f7xx_hal_adc.h
121 // We manually copy required lines here.
122 // XXX V1.14.0 may solve this problem
124 #define VREFINT_CAL_VREF ( 3300U) /* Analog voltage reference (Vref+) value with which temperature sensor has been calibrated in production (tolerance: +-10 mV) (unit: mV). */
125 #define TEMPSENSOR_CAL1_TEMP (( int32_t) 30) /* Internal temperature sensor, temperature at which temperature sensor has been calibrated in production for data into TEMPSENSOR_CAL1_ADDR (tolerance: +-5 DegC) (unit: DegC). */
126 #define TEMPSENSOR_CAL2_TEMP (( int32_t) 110) /* Internal temperature sensor, temperature at which temperature sensor has been calibrated in production for data into TEMPSENSOR_CAL2_ADDR (tolerance: +-5 DegC) (unit: DegC). */
127 #define TEMPSENSOR_CAL_VREFANALOG ( 3300U) /* Analog voltage reference (Vref+) voltage with which temperature sensor has been calibrated in production (+-10 mV) (unit: mV). */
129 // These addresses are incorrectly defined in stm32f7xx_ll_adc.h
130 #if defined(STM32F745xx) || defined(STM32F746xx) || defined(STM32F765xx)
131 // F745xx_F746xx and F765xx_F767xx_F769xx
132 #define VREFINT_CAL_ADDR ((uint16_t*) (0x1FF0F44A))
133 #define TEMPSENSOR_CAL1_ADDR ((uint16_t*) (0x1FF0F44C))
134 #define TEMPSENSOR_CAL2_ADDR ((uint16_t*) (0x1FF0F44E))
135 #elif defined(STM32F722xx)
136 // F72x_F73x
137 #define VREFINT_CAL_ADDR ((uint16_t*) (0x1FF07A2A))
138 #define TEMPSENSOR_CAL1_ADDR ((uint16_t*) (0x1FF07A2C))
139 #define TEMPSENSOR_CAL2_ADDR ((uint16_t*) (0x1FF07A2E))
140 #endif
141 #endif // STM32F7
143 #ifdef STM32F4
144 // STM32F4 stdlib does not define any of these
145 #define VREFINT_CAL_VREF (3300U)
146 #define TEMPSENSOR_CAL_VREFANALOG (3300U)
147 #define TEMPSENSOR_CAL1_TEMP ((int32_t) 30)
148 #define TEMPSENSOR_CAL2_TEMP ((int32_t) 110)
149 #endif
151 #ifdef AT32F435
152 #define VREFINT_EXPECTED (1489U) // The raw ADC reading at 12bit resolution expected for the 1V2 internal ref
153 #define VREFINT_CAL_VREF (3300U) // The nominal external Vref+ for the above reading
154 #define TEMPSENSOR_CAL_VREFANALOG (3300U)
155 #define TEMPSENSOR_CAL1_TEMP (25U)
156 #define TEMPSENSOR_CAL1_V (1.27f)
157 #define TEMPSENSOR_SLOPE (-4.13f) // mV/C
158 #endif