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)
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/>.
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
34 #define ADC_TAG_MAP_COUNT 28
36 #elif defined(STM32G4)
37 #ifdef USE_ADC_INTERNAL
38 #define ADC_TAG_MAP_COUNT 49
40 #define ADC_TAG_MAP_COUNT 47
42 #elif defined(STM32F3)
43 #define ADC_TAG_MAP_COUNT 39
45 #define ADC_TAG_MAP_COUNT 10
48 typedef struct adcTagMap_s
{
50 #if !defined(STM32F1) // F1 pins have uniform connection to ADC instances
54 #if defined(STM32H7) || defined(STM32G4)
55 uint8_t channelOrdinal
;
59 // Encoding for adcTagMap_t.devices
61 #define ADC_DEVICES_1 (1 << ADCDEV_1)
62 #define ADC_DEVICES_2 (1 << ADCDEV_2)
63 #define ADC_DEVICES_3 (1 << ADCDEV_3)
64 #define ADC_DEVICES_4 (1 << ADCDEV_4)
65 #define ADC_DEVICES_5 (1 << ADCDEV_5)
66 #define ADC_DEVICES_12 ((1 << ADCDEV_1)|(1 << ADCDEV_2))
67 #define ADC_DEVICES_34 ((1 << ADCDEV_3)|(1 << ADCDEV_4))
68 #define ADC_DEVICES_123 ((1 << ADCDEV_1)|(1 << ADCDEV_2)|(1 << ADCDEV_3))
69 #define ADC_DEVICES_345 ((1 << ADCDEV_3)|(1 << ADCDEV_4)|(1 << ADCDEV_5))
71 typedef struct adcDevice_s
{
73 rccPeriphTag_t rccADC
;
74 #if !defined(USE_DMA_SPEC)
75 dmaResource_t
* dmaResource
;
76 #if defined(STM32F4) || defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
79 #endif // !defined(USE_DMA_SPEC)
80 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
81 ADC_HandleTypeDef ADCHandle
;
82 DMA_HandleTypeDef DmaHandle
;
84 #if defined(STM32H7) || defined(STM32G4)
90 #ifdef USE_ADC_INTERNAL
91 extern int32_t adcVREFINTCAL
; // ADC value (12-bit) of band gap with Vref = VREFINTCAL_VREF
92 extern int32_t adcTSCAL1
;
93 extern int32_t adcTSCAL2
;
94 extern int32_t adcTSSlopeK
;
97 extern const adcDevice_t adcHardware
[];
98 extern const adcTagMap_t adcTagMap
[ADC_TAG_MAP_COUNT
];
99 extern adcOperatingConfig_t adcOperatingConfig
[ADC_CHANNEL_COUNT
];
100 extern volatile uint16_t adcValues
[ADC_CHANNEL_COUNT
];
102 uint8_t adcChannelByTag(ioTag_t ioTag
);
103 ADCDevice
adcDeviceByInstance(ADC_TypeDef
*instance
);
104 bool adcVerifyPin(ioTag_t tag
, ADCDevice device
);
106 // Marshall values in DMA instance/channel based order to adcChannel based order.
107 // Required for multi DMA instance implementation
108 void adcGetChannelValues(void);
111 // VREFINT and TEMPSENSOR related definitions
112 // These are shared among common adc.c and MCU dependent adc_stm32XXX.c
115 // STM32F7 HAL library V1.12.0 defines VREFINT and TEMPSENSOR in stm32f7xx_ll_adc.h,
116 // which is not included from stm32f7xx_hal_adc.h
117 // We manually copy required lines here.
118 // XXX V1.14.0 may solve this problem
120 #define VREFINT_CAL_VREF ( 3300U) /* Analog voltage reference (Vref+) value with which temperature sensor has been calibrated in production (tolerance: +-10 mV) (unit: mV). */
121 #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). */
122 #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). */
123 #define TEMPSENSOR_CAL_VREFANALOG ( 3300U) /* Analog voltage reference (Vref+) voltage with which temperature sensor has been calibrated in production (+-10 mV) (unit: mV). */
125 // These addresses are incorrectly defined in stm32f7xx_ll_adc.h
126 #if defined(STM32F745xx) || defined(STM32F746xx) || defined(STM32F765xx)
127 // F745xx_F746xx and F765xx_F767xx_F769xx
128 #define VREFINT_CAL_ADDR ((uint16_t*) (0x1FF0F44A))
129 #define TEMPSENSOR_CAL1_ADDR ((uint16_t*) (0x1FF0F44C))
130 #define TEMPSENSOR_CAL2_ADDR ((uint16_t*) (0x1FF0F44E))
131 #elif defined(STM32F722xx)
133 #define VREFINT_CAL_ADDR ((uint16_t*) (0x1FF07A2A))
134 #define TEMPSENSOR_CAL1_ADDR ((uint16_t*) (0x1FF07A2C))
135 #define TEMPSENSOR_CAL2_ADDR ((uint16_t*) (0x1FF07A2E))
140 // STM32F4 stdlib does not define any of these
141 #define VREFINT_CAL_VREF (3300U)
142 #define TEMPSENSOR_CAL_VREFANALOG (3300U)
143 #define TEMPSENSOR_CAL1_TEMP ((int32_t) 30)
144 #define TEMPSENSOR_CAL2_TEMP ((int32_t) 110)