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