Merge pull request #11939 from blckmn/flash-fix
[betaflight.git] / src / main / drivers / adc_impl.h
blob15844a9cfca04697e14d1f840b5e9112af4aa824
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(STM32H7)
31 #ifdef USE_ADC_INTERNAL
32 #define ADC_TAG_MAP_COUNT 30
33 #else
34 #define ADC_TAG_MAP_COUNT 28
35 #endif
36 #elif defined(STM32G4)
37 #ifdef USE_ADC_INTERNAL
38 #define ADC_TAG_MAP_COUNT 49
39 #else
40 #define ADC_TAG_MAP_COUNT 47
41 #endif
42 #else
43 #define ADC_TAG_MAP_COUNT 10
44 #endif
46 typedef struct adcTagMap_s {
47 ioTag_t tag;
48 uint8_t devices;
49 uint32_t channel;
50 #if defined(STM32H7) || defined(STM32G4)
51 uint8_t channelOrdinal;
52 #endif
53 } adcTagMap_t;
55 // Encoding for adcTagMap_t.devices
57 #define ADC_DEVICES_1 (1 << ADCDEV_1)
58 #define ADC_DEVICES_2 (1 << ADCDEV_2)
59 #define ADC_DEVICES_3 (1 << ADCDEV_3)
60 #define ADC_DEVICES_4 (1 << ADCDEV_4)
61 #define ADC_DEVICES_5 (1 << ADCDEV_5)
62 #define ADC_DEVICES_12 ((1 << ADCDEV_1)|(1 << ADCDEV_2))
63 #define ADC_DEVICES_34 ((1 << ADCDEV_3)|(1 << ADCDEV_4))
64 #define ADC_DEVICES_123 ((1 << ADCDEV_1)|(1 << ADCDEV_2)|(1 << ADCDEV_3))
65 #define ADC_DEVICES_345 ((1 << ADCDEV_3)|(1 << ADCDEV_4)|(1 << ADCDEV_5))
67 typedef struct adcDevice_s {
68 ADC_TypeDef* ADCx;
69 rccPeriphTag_t rccADC;
70 #if !defined(USE_DMA_SPEC)
71 dmaResource_t* dmaResource;
72 #if defined(STM32F4) || defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
73 uint32_t channel;
74 #endif
75 #endif // !defined(USE_DMA_SPEC)
76 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
77 ADC_HandleTypeDef ADCHandle;
78 DMA_HandleTypeDef DmaHandle;
79 #endif
80 #if defined(STM32H7) || defined(STM32G4)
81 uint8_t irq;
82 uint32_t channelBits;
83 #endif
84 } adcDevice_t;
86 #ifdef USE_ADC_INTERNAL
87 extern int32_t adcVREFINTCAL; // ADC value (12-bit) of band gap with Vref = VREFINTCAL_VREF
88 extern int32_t adcTSCAL1;
89 extern int32_t adcTSCAL2;
90 extern int32_t adcTSSlopeK;
91 #endif
93 extern const adcDevice_t adcHardware[];
94 extern const adcTagMap_t adcTagMap[ADC_TAG_MAP_COUNT];
95 extern adcOperatingConfig_t adcOperatingConfig[ADC_CHANNEL_COUNT];
96 extern volatile uint16_t adcValues[ADC_CHANNEL_COUNT];
98 uint8_t adcChannelByTag(ioTag_t ioTag);
99 ADCDevice adcDeviceByInstance(ADC_TypeDef *instance);
100 bool adcVerifyPin(ioTag_t tag, ADCDevice device);
102 // Marshall values in DMA instance/channel based order to adcChannel based order.
103 // Required for multi DMA instance implementation
104 void adcGetChannelValues(void);
107 // VREFINT and TEMPSENSOR related definitions
108 // These are shared among common adc.c and MCU dependent adc_stm32XXX.c
110 #ifdef STM32F7
111 // STM32F7 HAL library V1.12.0 defines VREFINT and TEMPSENSOR in stm32f7xx_ll_adc.h,
112 // which is not included from stm32f7xx_hal_adc.h
113 // We manually copy required lines here.
114 // XXX V1.14.0 may solve this problem
116 #define VREFINT_CAL_VREF ( 3300U) /* Analog voltage reference (Vref+) value with which temperature sensor has been calibrated in production (tolerance: +-10 mV) (unit: mV). */
117 #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). */
118 #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). */
119 #define TEMPSENSOR_CAL_VREFANALOG ( 3300U) /* Analog voltage reference (Vref+) voltage with which temperature sensor has been calibrated in production (+-10 mV) (unit: mV). */
121 // These addresses are incorrectly defined in stm32f7xx_ll_adc.h
122 #if defined(STM32F745xx) || defined(STM32F746xx) || defined(STM32F765xx)
123 // F745xx_F746xx and F765xx_F767xx_F769xx
124 #define VREFINT_CAL_ADDR ((uint16_t*) (0x1FF0F44A))
125 #define TEMPSENSOR_CAL1_ADDR ((uint16_t*) (0x1FF0F44C))
126 #define TEMPSENSOR_CAL2_ADDR ((uint16_t*) (0x1FF0F44E))
127 #elif defined(STM32F722xx)
128 // F72x_F73x
129 #define VREFINT_CAL_ADDR ((uint16_t*) (0x1FF07A2A))
130 #define TEMPSENSOR_CAL1_ADDR ((uint16_t*) (0x1FF07A2C))
131 #define TEMPSENSOR_CAL2_ADDR ((uint16_t*) (0x1FF07A2E))
132 #endif
133 #endif // STM32F7
135 #ifdef STM32F4
136 // STM32F4 stdlib does not define any of these
137 #define VREFINT_CAL_VREF (3300U)
138 #define TEMPSENSOR_CAL_VREFANALOG (3300U)
139 #define TEMPSENSOR_CAL1_TEMP ((int32_t) 30)
140 #define TEMPSENSOR_CAL2_TEMP ((int32_t) 110)
141 #endif