Removed excess trailing spaces before new lines on licenses.
[betaflight.git] / src / main / drivers / adc.c
blobf0035e8d6c63eefe995fcbf8225c75a4dce2ef44
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];
42 volatile uint16_t adcValues[ADC_CHANNEL_COUNT];
44 #ifdef USE_ADC_INTERNAL
45 uint16_t adcTSCAL1;
46 uint16_t adcTSCAL2;
47 uint16_t adcTSSlopeK;
48 uint16_t adcVREFINTCAL;
49 #endif
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;
57 return 0;
60 ADCDevice adcDeviceByInstance(ADC_TypeDef *instance)
62 if (instance == ADC1) {
63 return ADCDEV_1;
66 #if defined(STM32F3) || defined(STM32F4) || defined(STM32F7)
67 if (instance == ADC2) {
68 return ADCDEV_2;
71 if (instance == ADC3) {
72 return ADCDEV_3;
74 #endif
76 #ifdef STM32F3
77 if (instance == ADC4) {
78 return ADCDEV_4;
80 #endif
82 return ADCINVALID;
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];
100 #endif
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)
108 if (!tag) {
109 return false;
112 for (int map = 0 ; map < ADC_TAG_MAP_COUNT ; map++) {
113 #if defined(STM32F1)
114 UNUSED(device);
115 if ((adcTagMap[map].tag == tag)) {
116 return true;
118 #else
119 if ((adcTagMap[map].tag == tag) && (adcTagMap[map].devices & (1 << device))) {
120 return true;
122 #endif
125 return false;
128 #else
129 uint16_t adcGetChannel(uint8_t channel)
131 UNUSED(channel);
132 return 0;
134 #endif