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/>.
25 #include "common/utils.h"
29 #include "build/build_config.h"
30 #include "build/debug.h"
32 #include "drivers/adc_impl.h"
33 #include "drivers/io.h"
39 //#define DEBUG_ADC_CHANNELS
41 adcOperatingConfig_t adcOperatingConfig
[ADC_CHANNEL_COUNT
];
42 volatile uint16_t adcValues
[ADC_CHANNEL_COUNT
];
44 #ifdef USE_ADC_INTERNAL
48 uint16_t adcVREFINTCAL
;
51 uint8_t adcChannelByTag(ioTag_t ioTag
)
53 for (uint8_t i
= 0; i
< ARRAYLEN(adcTagMap
); i
++) {
54 if (ioTag
== adcTagMap
[i
].tag
)
55 return adcTagMap
[i
].channel
;
60 ADCDevice
adcDeviceByInstance(ADC_TypeDef
*instance
)
62 if (instance
== ADC1
) {
66 #if defined(STM32F3) || defined(STM32F4) || defined(STM32F7)
67 if (instance
== ADC2
) {
71 if (instance
== ADC3
) {
77 if (instance
== ADC4
) {
85 uint16_t adcGetChannel(uint8_t channel
)
87 #ifdef DEBUG_ADC_CHANNELS
88 if (adcOperatingConfig
[0].enabled
) {
89 debug
[0] = adcValues
[adcOperatingConfig
[0].dmaIndex
];
91 if (adcOperatingConfig
[1].enabled
) {
92 debug
[1] = adcValues
[adcOperatingConfig
[1].dmaIndex
];
94 if (adcOperatingConfig
[2].enabled
) {
95 debug
[2] = adcValues
[adcOperatingConfig
[2].dmaIndex
];
97 if (adcOperatingConfig
[3].enabled
) {
98 debug
[3] = adcValues
[adcOperatingConfig
[3].dmaIndex
];
101 return adcValues
[adcOperatingConfig
[channel
].dmaIndex
];
104 // Verify a pin designated by tag has connection to an ADC instance designated by device
106 bool adcVerifyPin(ioTag_t tag
, ADCDevice device
)
112 for (int map
= 0 ; map
< ADC_TAG_MAP_COUNT
; map
++) {
115 if ((adcTagMap
[map
].tag
== tag
)) {
119 if ((adcTagMap
[map
].tag
== tag
) && (adcTagMap
[map
].devices
& (1 << device
))) {
129 uint16_t adcGetChannel(uint8_t channel
)