Fix function brace style
[betaflight.git] / src / main / drivers / adc.c
blob269b17b63ca8bf36377aefa1322ebb7b9dd936a7
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 #include <stdbool.h>
22 #include <stdint.h>
24 #include "platform.h"
25 #include "common/utils.h"
27 #ifdef USE_ADC
29 #include "build/build_config.h"
30 #include "build/debug.h"
32 #include "drivers/adc_impl.h"
33 #include "drivers/io.h"
35 #include "pg/adc.h"
37 #include "adc.h"
39 //#define DEBUG_ADC_CHANNELS
41 adcOperatingConfig_t adcOperatingConfig[ADC_CHANNEL_COUNT];
43 #if defined(STM32F7)
44 volatile FAST_DATA_ZERO_INIT uint16_t adcValues[ADC_CHANNEL_COUNT];
45 #else
46 volatile uint16_t adcValues[ADC_CHANNEL_COUNT];
47 #endif
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;
55 return 0;
58 ADCDevice adcDeviceByInstance(ADC_TypeDef *instance)
60 if (instance == ADC1) {
61 return ADCDEV_1;
64 #if defined(ADC2)
65 if (instance == ADC2) {
66 return ADCDEV_2;
68 #endif
69 #if defined(ADC3)
70 if (instance == ADC3) {
71 return ADCDEV_3;
73 #endif
74 #if defined(ADC4)
75 if (instance == ADC4) {
76 return ADCDEV_4;
78 #endif
79 #if defined(ADC5)
80 if (instance == ADC5) {
81 return ADCDEV_5;
83 #endif
85 return ADCINVALID;
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];
105 #endif
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)
113 if (!tag) {
114 return false;
117 for (int map = 0 ; map < ADC_TAG_MAP_COUNT ; map++) {
118 if ((adcTagMap[map].tag == tag) && (adcTagMap[map].devices & (1 << device))) {
119 return true;
123 return false;
126 #ifdef USE_ADC_INTERNAL
128 int32_t adcVREFINTCAL; // ADC value (12-bit) of band gap with Vref = VREFINTCAL_VREF
129 int32_t adcTSCAL1;
130 int32_t adcTSCAL2;
131 int32_t adcTSSlopeK;
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
149 #else
150 uint16_t adcGetChannel(uint8_t channel)
152 UNUSED(channel);
153 return 0;
155 #endif