Refactor missing prototypes 2 (#14170)
[betaflight.git] / src / main / sensors / adcinternal.c
blob908d1d9ed8341cea759f1f9928c4287dc07433a9
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 #include "adcinternal.h"
28 #if defined(USE_ADC_INTERNAL)
30 #include "build/debug.h"
32 #include "common/utils.h"
34 #include "drivers/adc.h"
36 typedef struct movingAverageStateUint16_s {
37 uint32_t sum;
38 uint16_t *values;
39 uint8_t size;
40 uint8_t pos;
41 } movingAverageStateUint16_t;
43 static uint16_t updateMovingAverageUint16(movingAverageStateUint16_t *state, uint16_t newValue)
45 state->sum -= state->values[state->pos];
46 state->values[state->pos] = newValue;
47 state->sum += newValue;
48 state->pos = (state->pos + 1) % state->size;
50 return state->sum / state->size;
53 static uint16_t adcVrefintValue;
54 static uint16_t adcVrefintValues[8];
55 movingAverageStateUint16_t adcVrefintAverageState = { 0, adcVrefintValues, 8, 0 } ;
57 static uint16_t adcTempsensorValue;
58 static uint16_t adcTempsensorValues[8];
59 movingAverageStateUint16_t adcTempsensorAverageState = { 0, adcTempsensorValues, 8, 0 } ;
61 static int16_t coreTemperature;
62 static uint16_t vrefMv;
64 uint16_t getVrefMv(void)
66 #ifdef ADC_VOLTAGE_REFERENCE_MV
67 return ADC_VOLTAGE_REFERENCE_MV;
68 #else
69 return vrefMv;
70 #endif
73 int16_t getCoreTemperatureCelsius(void)
75 return coreTemperature;
78 void adcInternalProcess(timeUs_t currentTimeUs)
80 UNUSED(currentTimeUs);
82 if (adcInternalIsBusy()) {
83 return;
86 uint16_t vrefintSample = adcInternalReadVrefint();
87 uint16_t tempsensorSample = adcInternalReadTempsensor();
89 adcVrefintValue = updateMovingAverageUint16(&adcVrefintAverageState, vrefintSample);
90 adcTempsensorValue = updateMovingAverageUint16(&adcTempsensorAverageState, tempsensorSample);
92 vrefMv = adcInternalCompensateVref(adcVrefintValue);
93 coreTemperature = adcInternalComputeTemperature(adcTempsensorValue, vrefMv);
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