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
];
44 volatile FAST_DATA_ZERO_INIT
uint16_t adcValues
[ADC_CHANNEL_COUNT
];
46 volatile uint16_t adcValues
[ADC_CHANNEL_COUNT
];
49 uint8_t adcChannelByTag(ioTag_t ioTag
)
51 for (uint8_t i
= 0; i
< ARRAYLEN(adcTagMap
); i
++) {
52 if (ioTag
== adcTagMap
[i
].tag
)
53 return adcTagMap
[i
].channel
;
58 ADCDevice
adcDeviceByInstance(ADC_TypeDef
*instance
)
60 if (instance
== ADC1
) {
65 if (instance
== ADC2
) {
70 if (instance
== ADC3
) {
75 if (instance
== ADC4
) {
80 if (instance
== ADC5
) {
88 uint16_t adcGetChannel(uint8_t channel
)
90 adcGetChannelValues();
92 #ifdef DEBUG_ADC_CHANNELS
93 if (adcOperatingConfig
[0].enabled
) {
94 debug
[0] = adcValues
[adcOperatingConfig
[0].dmaIndex
];
96 if (adcOperatingConfig
[1].enabled
) {
97 debug
[1] = adcValues
[adcOperatingConfig
[1].dmaIndex
];
99 if (adcOperatingConfig
[2].enabled
) {
100 debug
[2] = adcValues
[adcOperatingConfig
[2].dmaIndex
];
102 if (adcOperatingConfig
[3].enabled
) {
103 debug
[3] = adcValues
[adcOperatingConfig
[3].dmaIndex
];
106 return adcValues
[adcOperatingConfig
[channel
].dmaIndex
];
109 // Verify a pin designated by tag has connection to an ADC instance designated by device
111 bool adcVerifyPin(ioTag_t tag
, ADCDevice device
)
117 for (int map
= 0 ; map
< ADC_TAG_MAP_COUNT
; map
++) {
118 if ((adcTagMap
[map
].tag
== tag
) && (adcTagMap
[map
].devices
& (1 << device
))) {
126 #ifdef USE_ADC_INTERNAL
128 int32_t adcVREFINTCAL
; // ADC value (12-bit) of band gap with Vref = VREFINTCAL_VREF
133 uint16_t adcInternalCompensateVref(uint16_t vrefAdcValue
)
135 // This is essentially a tuned version of
136 // __HAL_ADC_CALC_VREFANALOG_VOLTAGE(vrefAdcValue, ADC_RESOLUTION_12B);
137 return (uint16_t)((uint32_t)(adcVREFINTCAL
* VREFINT_CAL_VREF
) / vrefAdcValue
);
140 int16_t adcInternalComputeTemperature(uint16_t tempAdcValue
, uint16_t vrefValue
)
142 // This is essentially a tuned version of
143 // __HAL_ADC_CALC_TEMPERATURE(vrefValue, tempAdcValue, ADC_RESOLUTION_12B);
145 return ((((int32_t)((tempAdcValue
* vrefValue
) / TEMPSENSOR_CAL_VREFANALOG
) - adcTSCAL1
) * adcTSSlopeK
) + 500) / 1000 + TEMPSENSOR_CAL1_TEMP
;
147 #endif // USE_ADC_INTERNAL
150 uint16_t adcGetChannel(uint8_t channel
)