Fix filter cutoff frequency limits
[betaflight.git] / src / main / sensors / adcinternal.c
blob5e5904e90d42abd04aada141f6d8c21609c91585
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 = 3300 * adcVrefintValue / adcVREFINTCAL;
92 int32_t adcTempsensorAdjusted = (int32_t)(adcTempsensorValue * 3300) / vrefMv;
93 coreTemperature = ((adcTempsensorAdjusted - adcTSCAL1) * adcTSSlopeK + 30 * 1000 + 500) / 1000;
95 DEBUG_SET(DEBUG_ADC_INTERNAL, 0, coreTemperature);
96 DEBUG_SET(DEBUG_ADC_INTERNAL, 1, vrefintSample);
97 DEBUG_SET(DEBUG_ADC_INTERNAL, 2, tempsensorSample);
98 DEBUG_SET(DEBUG_ADC_INTERNAL, 3, vrefMv);
100 adcInternalStartConversion(); // Start next conversion
103 void adcInternalInit(void)
105 // Call adcInternalProcess repeatedly to fill moving average array
106 for (int i = 0 ; i < 9 ; i++) {
107 while (adcInternalIsBusy()) {
108 // empty
110 adcInternalProcess(0);
113 #else
114 uint16_t getVrefMv(void)
116 #ifdef ADC_VOLTAGE_REFERENCE_MV
117 return ADC_VOLTAGE_REFERENCE_MV;
118 #else
119 return 3300;
120 #endif
122 #endif