[4.4.2] Remove 15 m/s limit on estimated vario (#12788)
[betaflight.git] / src / main / sensors / adcinternal.c
blobcad059dc89ef1daa289481fcbbd82ec9ae0d41f4
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"
26 #if defined(USE_ADC_INTERNAL)
28 #include "build/debug.h"
30 #include "common/utils.h"
32 #include "drivers/adc.h"
34 typedef struct movingAverageStateUint16_s {
35 uint32_t sum;
36 uint16_t *values;
37 uint8_t size;
38 uint8_t pos;
39 } movingAverageStateUint16_t;
41 uint16_t updateMovingAverageUint16(movingAverageStateUint16_t *state, uint16_t newValue)
43 state->sum -= state->values[state->pos];
44 state->values[state->pos] = newValue;
45 state->sum += newValue;
46 state->pos = (state->pos + 1) % state->size;
48 return state->sum / state->size;
51 static uint16_t adcVrefintValue;
52 static uint16_t adcVrefintValues[8];
53 movingAverageStateUint16_t adcVrefintAverageState = { 0, adcVrefintValues, 8, 0 } ;
55 static uint16_t adcTempsensorValue;
56 static uint16_t adcTempsensorValues[8];
57 movingAverageStateUint16_t adcTempsensorAverageState = { 0, adcTempsensorValues, 8, 0 } ;
59 static int16_t coreTemperature;
60 static uint16_t vrefMv;
62 uint16_t getVrefMv(void)
64 #ifdef ADC_VOLTAGE_REFERENCE_MV
65 return ADC_VOLTAGE_REFERENCE_MV;
66 #else
67 return vrefMv;
68 #endif
71 int16_t getCoreTemperatureCelsius(void)
73 return coreTemperature;
76 void adcInternalProcess(timeUs_t currentTimeUs)
78 UNUSED(currentTimeUs);
80 if (adcInternalIsBusy()) {
81 return;
84 uint16_t vrefintSample = adcInternalReadVrefint();
85 uint16_t tempsensorSample = adcInternalReadTempsensor();
87 adcVrefintValue = updateMovingAverageUint16(&adcVrefintAverageState, vrefintSample);
88 adcTempsensorValue = updateMovingAverageUint16(&adcTempsensorAverageState, tempsensorSample);
90 vrefMv = adcInternalCompensateVref(adcVrefintValue);
91 coreTemperature = adcInternalComputeTemperature(adcTempsensorValue, vrefMv);
93 DEBUG_SET(DEBUG_ADC_INTERNAL, 0, coreTemperature);
94 DEBUG_SET(DEBUG_ADC_INTERNAL, 1, vrefintSample);
95 DEBUG_SET(DEBUG_ADC_INTERNAL, 2, tempsensorSample);
96 DEBUG_SET(DEBUG_ADC_INTERNAL, 3, vrefMv);
98 adcInternalStartConversion(); // Start next conversion
101 void adcInternalInit(void)
103 // Call adcInternalProcess repeatedly to fill moving average array
104 for (int i = 0 ; i < 9 ; i++) {
105 while (adcInternalIsBusy()) {
106 // empty
108 adcInternalProcess(0);
111 #else
112 uint16_t getVrefMv(void)
114 #ifdef ADC_VOLTAGE_REFERENCE_MV
115 return ADC_VOLTAGE_REFERENCE_MV;
116 #else
117 return 3300;
118 #endif
120 #endif