1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-23 16:25:31 +03:00

Merge pull request #4881 from jflyper/bfdev-adc-internal-full-task-version

VREFINT and core temperature support, full task polling version
This commit is contained in:
Michael Keller 2018-01-10 16:12:05 +13:00 committed by GitHub
commit 4258651b3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 321 additions and 42 deletions

View file

@ -0,0 +1,114 @@
/*
* This file is part of Cleanflight.
*
* Cleanflight is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cleanflight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdint.h>
#include "platform.h"
#if defined(USE_ADC) && defined(USE_ADC_INTERNAL)
#include "build/debug.h"
#include "common/utils.h"
#include "drivers/adc.h"
typedef struct movingAverageStateUint16_s {
uint32_t sum;
uint16_t *values;
uint8_t size;
uint8_t pos;
} movingAverageStateUint16_t;
uint16_t updateMovingAverageUint16(movingAverageStateUint16_t *state, uint16_t newValue)
{
state->sum -= state->values[state->pos];
state->values[state->pos] = newValue;
state->sum += newValue;
state->pos = (state->pos + 1) % state->size;
return state->sum / state->size;
}
static uint16_t adcVrefintValue;
static uint16_t adcVrefintValues[8];
movingAverageStateUint16_t adcVrefintAverageState = { 0, adcVrefintValues, 8, 0 } ;
static uint16_t adcTempsensorValue;
static uint16_t adcTempsensorValues[8];
movingAverageStateUint16_t adcTempsensorAverageState = { 0, adcTempsensorValues, 8, 0 } ;
static int16_t coreTemperature;
uint16_t getVrefMv(void)
{
#ifdef ADC_VOLTAGE_REFERENCE_MV
return ADC_VOLTAGE_REFERENCE_MV;
#else
return 3300 * adcVrefintValue / adcVREFINTCAL;
#endif
}
uint16_t getCoreTemperatureCelsius(void)
{
return coreTemperature;
}
void adcInternalProcess(timeUs_t currentTimeUs)
{
UNUSED(currentTimeUs);
if (adcInternalIsBusy()) {
return;
}
uint16_t vrefintSample = adcInternalReadVrefint();
uint16_t tempsensorSample = adcInternalReadTempsensor();
adcVrefintValue = updateMovingAverageUint16(&adcVrefintAverageState, vrefintSample);
adcTempsensorValue = updateMovingAverageUint16(&adcTempsensorAverageState, tempsensorSample);
int32_t adcTempsensorAdjusted = (int32_t)adcTempsensorValue * 3300 / getVrefMv();
coreTemperature = ((adcTempsensorAdjusted - adcTSCAL1) * adcTSSlopeK + 30 * 1000 + 500) / 1000;
DEBUG_SET(DEBUG_CORE_TEMP, 0, coreTemperature);
adcInternalStartConversion(); // Start next conversion
}
void adcInternalInit(void)
{
// Call adcInternalProcess repeatedly to fill moving average array
for (int i = 0 ; i < 9 ; i++) {
while (adcInternalIsBusy()) {
// empty
}
adcInternalProcess(0);
}
}
#else
uint16_t getVrefMv(void)
{
#ifdef ADC_VOLTAGE_REFERENCE_MV
return ADC_VOLTAGE_REFERENCE_MV;
#else
return 3300;
#endif
}
#endif

View file

@ -0,0 +1,25 @@
/*
* This file is part of Cleanflight.
*
* Cleanflight is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cleanflight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "drivers/time.h"
void adcInternalInit(void);
void adcInternalProcess(timeUs_t currentTimeUs);
uint16_t getCoreTemperatureCelsius(void);
uint16_t getVrefMv(void);

View file

@ -32,6 +32,7 @@
#include "pg/pg_ids.h"
#include "config/config_reset.h"
#include "sensors/adcinternal.h"
#include "sensors/current.h"
#include "sensors/esc_sensor.h"
@ -77,8 +78,6 @@ void currentMeterReset(currentMeter_t *meter)
// ADC/Virtual shared
//
#define ADCVREF 3300 // in mV
#define IBAT_LPF_FREQ 0.4f
static biquadFilter_t adciBatFilter;
@ -106,7 +105,7 @@ static int32_t currentMeterADCToCentiamps(const uint16_t src)
const currentSensorADCConfig_t *config = currentSensorADCConfig();
int32_t millivolts = ((uint32_t)src * ADCVREF) / 4096;
int32_t millivolts = ((uint32_t)src * getVrefMv()) / 4096;
// y=x/m+b m is scale in (mV/10A) and b is offset in (mA)
int32_t centiAmps = (millivolts * 10000 / (int32_t)config->scale + (int32_t)config->offset) / 10;

View file

@ -31,6 +31,7 @@
#include "fc/runtime_config.h"
#include "sensors/sensors.h"
#include "sensors/adcinternal.h"
#include "sensors/acceleration.h"
#include "sensors/barometer.h"
#include "sensors/gyro.h"
@ -65,5 +66,9 @@ bool sensorsAutodetect(void)
rangefinderInit();
#endif
#ifdef USE_ADC_INTERNAL
adcInternalInit();
#endif
return gyroDetected;
}

View file

@ -33,6 +33,7 @@
#include "pg/pg_ids.h"
#include "config/config_reset.h"
#include "sensors/adcinternal.h"
#include "sensors/voltage.h"
#include "sensors/esc_sensor.h"
@ -138,7 +139,7 @@ STATIC_UNIT_TESTED uint16_t voltageAdcToVoltage(const uint16_t src, const voltag
{
// calculate battery voltage based on ADC reading
// result is Vbatt in 0.1V steps. 3.3V = ADC Vref, 0xFFF = 12bit adc, 110 = 10:1 voltage divider (10k:1k) * 10 for 0.1V
return ((((uint32_t)src * config->vbatscale * 33 + (0xFFF * 5)) / (0xFFF * config->vbatresdivval)) / config->vbatresdivmultiplier);
return ((((uint32_t)src * config->vbatscale * getVrefMv() / 100 + (0xFFF * 5)) / (0xFFF * config->vbatresdivval)) / config->vbatresdivmultiplier);
}
void voltageMeterADCRefresh(void)