refact: ADC, switches and keys

Add "hardware_defs" target to dump hardware defines from hal.h per target

Fixed hal.h definitions for switches & ADC inputs

Tools to generate hardware definitions from current hal.h

generator: add support for EXTx and analog switches

WiP

Add generated header to hardware_defs target

Add support for ADC input direction and index per GPIO port

Incredible: it compiles

Removed StdPeriph ADC driver

Works on QX7

Works on TX16S (VBat missing)

Switch driver using generated switches for stm32 targets

Switch driver with generated switches for simu

Use switch names from switch driver

Fixed single conversion handling

Fixed horus VBat reading

Added switch name

Fix warnings about bitfield offsets and GCC 4.4

This is caused by bitfields that cross type boundaries. In this particular case, it happens as the bitfield is not defined the same in `rtc_backup.cpp`.

Switches has already been moved to generic driver

Jitter measurement for each sample, not just for oversampled values

Moved periph clock init to the ADC driver

ADC & GPIOs.

Add switchInit() to init pins

Fix NV14 CMake

Fix taranis target

Fix NV14 (again)

Model audio files: use new interface for switch index

Fix X9-lite

Fix simu headers

Fix getSwitchName()

Add support for ADC switches

Fix switch warning YAML

Test only switches which are defined

Conversions tests for QX7 only on QX7

Fix X12S

Fix function switch names

Enable ADC periph clock before initialising channels

NV14: offset inputs for the sticks & fix switches

WiP: get rid of VSRCRAW and some more

Simu shall use normal ADC driver

fix x7

fix nv14

Fix 9x nav

fix 212x64

fix xlite

fix x9e

fix simu lib

fix unit tests

fix x7 tests

PWM stick values should be written directly into adcValues

... just like FlySky sticks.

fix x12s

fix nv14

Add some support for SPI ADC (only unit tests)

Remove `jq` usage

fix generate_yaml.py for newer libclang

fix pot config

fix stick names

Some YAML generator improvements

Max 16 Pots on COLORLCD

Fix some source names

Use 10 bits for switches to prevent overflow

Removed horus constants for switches & ADC inputs

Fix unit tests

Fix curve edit

Fix libsimulator

Fix function switches (tpro)

Refine ADC & analog API

Moved hardware definitions scripts and templates into own dir

cleanup API refine

Fix unit tests

Mixer: apply calibration only on required inputs

Place ADC switches after all other ADC inputs

Fix X12S

Remove useless TR_POTS_VSRCRAW, TR_SW_VSRCRAW, and TR_VSRCRAW

Cleanup redundant generated YAML parsers

Fixes Boxer rebase

Add boxer to generate-hw-defs.sh

Obsolete LUA exports

Add boxer ADC_DIRECTION

Split generate_hw_defs.py into multiple files

Some cleanup (+ LUA switches)

Fix AXIS definitions

Fixed translation strings declarations

Some rebase fixes

Remove some useless StdPeriph drivers

fix: set pin mode to reset state on usart deinit

fix: use canonical names for calibration data

fix: ADC driver does not care about unconfigured inputs

fix X12S ADC

Add support for ADC conversion chaining

Better X12S ADC implem

Moved X12S ADC driver to HAL/LL

Improve ADC driver separation

Fixed taranis & nv14

Moved PWM gimbals to HAL/LL

Removed useless TR_EXTRA_VSRCRAW

Fixed include guards

Small fixes

Renamed control inputs

Moved internal and external module pulse driver def to boards

Move more things to the generic_stm32 board

Remove STR_VMIXTRIMS

Added analog labels

Generate main control names

Misuse labels / short labels for main controls

Backward compatibility: sliders and legacy names

New keys driver

Let's get rid of StdPeriph headers in simu

Add jinja2 to CI workflows

Use `grep` instead of `sed`

chore: add python jinja2 oto msys setup script

Removed more static definitions and use dynamic ones instead

Fixes X9E

Fix x7 nav too

212 switch display

Fix default sliders

Removed NUM_STICKS

212 switch display working nicely

Surface radio 'stick' replacements

fix: switch, trim & rotary pin init

Remove old key drivers

Removed VKEYS

Removed ETX_FOURCC

Fixed SURFACE_NAME

Removed TR_VSWITCHES & TR_TRIMS_SWITCHES

Fix xpot switches

Store calibrated analogs in physical order

Use getSourceString in drawSource

Implement missing YAML read/write

Fix isSourceAvailableInInputs()

Fix isSourceAvailable()

Some surface fixes

Fix CLI debug

Ported colour UI of analog diagnostics to new ADC API

Small fixes

Better input mapping

Minor tpro fixes

Fix trims available

Remove TX mode choice on surface radio

Rename surface controls

Channel order alignement

Fix throttle warning

Fix checkTrims()

Fix pot config casting issue

Fix IS_SWITCH_FS()

Fixed input mapping

Fix fswitch auto-switch

Fixed storing / reading function switches in YAML

Fix enableVBatBridge() / disableVBatBridge()

Fix interesting mode conversion

Fix sources issues

Fix special funnction list

Proper function switch handling

128 GUI improvements

Fix compilation

Boxer cosmetics

128 hardware screen cosmetic

Fix TX12 switches

More switches fixes

Simplify CPU type handling

Fix flash size

Fixed page-up / page-down in libopenui
This commit is contained in:
Raphael Coeffic
2023-06-25 14:32:39 +02:00
committed by Malte Langermann
parent b6dd3a0ef8
commit 2b100e0eb5
323 changed files with 12812 additions and 22301 deletions
+10
View File
@@ -0,0 +1,10 @@
AddHWGenTarget(${HW_DESC_JSON} hal_keys hal_keys.inc)
set(SRC ${SRC}
${CMAKE_CURRENT_BINARY_DIR}/hal_keys.inc
hal/module_port.cpp
hal/adc_driver.cpp
hal/key_driver.cpp
)
+287 -171
View File
@@ -25,189 +25,212 @@
// IS_POT_SLIDER_AVAILABLE()
#include "opentx.h"
const etx_hal_adc_driver_t* etx_hal_adc_driver = nullptr;
#if defined(SIMU)
// not needed
#elif defined(RADIO_T16)
const int8_t adcDirection[NUM_ANALOGS] = {1,-1,1,-1, 1,1,1, -1,1,1,1, -1,1 };
#elif defined(RADIO_T18)
const int8_t adcDirection[NUM_ANALOGS] = {1,-1,1,-1, -1,1,-1, -1,1,1,1, -1,1 };
#elif defined(RADIO_TX16S)
const int8_t adcDirection[NUM_ANALOGS] = {1,-1,1,-1, 1,1,1, -1,1,1,1, -1,1 };
#elif defined(PCBX10)
const int8_t adcDirection[NUM_ANALOGS] = {1,-1,1,-1, -1,1,-1, 1,1,1,1, 1,-1 };
#elif defined(PCBX9E)
const int8_t adcDirection[NUM_ANALOGS] = {
#if defined(HORUS_STICKS)
1,-1,1,-1,
#else
1,1,-1,-1,
#endif // HORUS_STICKS
// other analogs are the same
-1,-1,-1,1, -1,1,1,-1, -1,-1 };
const etx_hal_adc_driver_t* _hal_adc_driver = nullptr;
const etx_hal_adc_inputs_t* _hal_adc_inputs = nullptr;
const uint8_t adcMapping[NUM_ANALOGS] = { 0 /*STICK1*/, 1 /*STICK2*/, 2 /*STICK3*/, 3 /*STICK4*/,
11 /*POT1*/, 4 /*POT2*/, 5 /*POT3*/, 6 /*POT4*/,
12 /*SLIDER1*/, 13 /*SLIDER2*/, 7 /*SLIDER3*/, 8 /*SLIDER4*/,
9 /*TX_VOLTAGE*/, 10 /*TX_VBAT*/ };
#elif defined(PCBX9DP)
const int8_t adcDirection[NUM_ANALOGS] = {1,-1,1,-1, 1,1,-1, 1,1, 1, 1};
#elif defined(PCBX9D)
const int8_t adcDirection[NUM_ANALOGS] = {1,-1,1,-1, 1,1,0, 1,1, 1, 1};
#elif defined(RADIO_TX12)
const int8_t adcDirection[NUM_ANALOGS] = {-1,1,-1,1, -1,-1, 1, 1};
#elif defined(RADIO_TX12MK2)
const int8_t adcDirection[NUM_ANALOGS] = {-1,1,-1,1, 1,-1};
#elif defined(RADIO_BOXER)
const int8_t adcDirection[NUM_ANALOGS] = {1,-1,1,-1, -1,-1};
#elif defined(RADIO_ZORRO)
const int8_t adcDirection[NUM_ANALOGS] = {-1, 1, 1, -1, -1, 1, 1, 1};
#elif defined(RADIO_TPRO)
const int8_t adcDirection[NUM_ANALOGS] = {1,-1,1,-1, 1,1, 1, 1};
#elif defined(PCBX7)
const int8_t adcDirection[NUM_ANALOGS] = {-1,1,-1,1, 1,1, 1, 1};
#elif defined(PCBX9LITE)
const int8_t adcDirection[NUM_ANALOGS] = {-1,1,-1,1, 1,1, 1};
#elif defined(PCBXLITE)
const int8_t adcDirection[NUM_ANALOGS] = {1,-1,-1,1, -1,1, 1, 1};
#elif defined(PCBNV14)
const uint8_t adcMapping[NUM_ANALOGS] = { 0 /*STICK1*/, 1 /*STICK2*/, 2 /*STICK3*/, 3 /*STICK4*/,
4 /*POT1*/, 5 /*POT2*/, 6 /*SWA*/, 14 /*SWB*/,
7 /*SWC*/, 15 /*SWD*/, 8 /*SWE*/, 9 /*SWF*/,
11/*SWG*/, 10/*SWH*/,
12 /*TX_VOLTAGE*/, 13 /* TX_VBAT */ };
const int8_t adcDirection[NUM_ANALOGS] = { 0 /*STICK1*/, 0 /*STICK2*/, 0 /*STICK3*/, 0 /*STICK4*/,
-1 /*POT1*/, 0 /*POT2*/, 0 /*SWA*/, 0 /*SWC*/,
0 /*SWE*/, -1 /*SWF*/, 0 /*SWG*/, -1 /*SWH*/,
0 /*TX_VOLTAGE*/, 0 /*TX_VBAT*/,
0 /*SWB*/, 0 /*SWD*/};
#elif defined(PCBX12S)
const int8_t adcDirection[NUM_ANALOGS] = {1,-1,1,-1, -1,1,-1, -1,-1, -1,1, 0,0,0};
#else
#error "ADC driver does not suppport this target"
#endif
uint16_t adcValues[NUM_ANALOGS] __DMA;
#if defined(PCBX10) || defined(PCBX12S)
uint16_t rtcBatteryVoltage;
#endif
static uint16_t adcValues[MAX_ANALOG_INPUTS] __DMA;
bool adcInit(const etx_hal_adc_driver_t* driver)
{
etx_hal_adc_driver = driver;
if (!etx_hal_adc_driver)
return false;
// If there is an init function
// it should succeed
if (etx_hal_adc_driver->init &&
!etx_hal_adc_driver->init()) {
return false;
// If there is an init function, it MUST succeed
if (driver && (!driver->init || driver->init())) {
_hal_adc_driver = driver;
_hal_adc_inputs = driver->inputs;
return true;
}
return true;
_hal_adc_driver = nullptr;
return false;
}
static bool adcSingleRead()
{
if (!etx_hal_adc_driver)
return false;
if (etx_hal_adc_driver->start_conversion &&
!etx_hal_adc_driver->start_conversion())
if (!_hal_adc_driver)
return false;
if (etx_hal_adc_driver->wait_completion)
etx_hal_adc_driver->wait_completion();
if (_hal_adc_driver->start_conversion &&
!_hal_adc_driver->start_conversion())
return false;
if (_hal_adc_driver->wait_completion)
_hal_adc_driver->wait_completion();
return true;
}
// Declare adcRead() weak so it can be re-declared
#pragma weak adcRead
bool adcRead()
{
uint16_t temp[NUM_ANALOGS] = { 0 };
uint8_t first_analog_adc;
#if defined(FLYSKY_GIMBAL)
if (globalData.flyskygimbals)
{
first_analog_adc = FIRST_ANALOG_ADC_FS;
} else
{
first_analog_adc = FIRST_ANALOG_ADC;
}
#else
first_analog_adc = FIRST_ANALOG_ADC;
#endif
for (int i=0; i<4; i++) {
if (!adcSingleRead())
return false;
for (uint8_t x=first_analog_adc; x<NUM_ANALOGS; x++) {
uint16_t val = adcValues[x];
#if defined(JITTER_MEASURE)
if (JITTER_MEASURE_ACTIVE()) {
rawJitter[x].measure(val);
}
#endif
temp[x] += val;
}
adcSingleRead();
// TODO: this hack needs to go away...
if (isVBatBridgeEnabled()) {
disableVBatBridge();
}
for (uint8_t x=first_analog_adc; x<NUM_ANALOGS; x++) {
adcValues[x] = temp[x] >> 2;
}
#if NUM_PWMSTICKS > 0
if (STICKS_PWM_ENABLED()) {
sticksPwmRead(adcValues);
}
#endif
return true;
}
#if !defined(SIMU)
#define XPOT_DELTA 10
#define XPOT_DELAY 10 /* cycles */
void adcCalibMinMax()
{
// get low and high vals for sticks and pots
for (uint8_t i = 0; i < MAX_ANALOG_INPUTS; i++) {
int16_t vt = anaIn(i);
auto& calib = reusableBuffer.calib.inputs[i];
calib.input.loVal = min(vt, calib.input.loVal);
calib.input.hiVal = max(vt, calib.input.hiVal);
if (i >= MAX_STICKS) {
uint8_t idx = i - MAX_STICKS;
if (IS_POT_WITHOUT_DETENT(idx)) {
calib.input.midVal = (calib.input.hiVal + calib.input.loVal) / 2;
} else if (IS_POT_MULTIPOS(idx)) {
auto& xpot = calib.xpot;
int count = xpot.stepsCount;
if (count <= XPOTS_MULTIPOS_COUNT) {
// use raw analog value for multipos calibraton,
// anaIn() already has multipos decoded value
vt = getAnalogValue(i) >> 1;
if (xpot.lastCount == 0 || vt < xpot.lastPosition - XPOT_DELTA ||
vt > xpot.lastPosition + XPOT_DELTA) {
xpot.lastPosition = vt;
xpot.lastCount = 1;
} else {
if (xpot.lastCount < 255) xpot.lastCount++;
}
if (xpot.lastCount == XPOT_DELAY) {
int16_t position = xpot.lastPosition;
bool found = false;
for (int j = 0; j < count; j++) {
int16_t step = xpot.steps[j];
if (position >= step - XPOT_DELTA &&
position <= step + XPOT_DELTA) {
found = true;
break;
}
}
if (!found) {
if (count < XPOTS_MULTIPOS_COUNT) {
xpot.steps[count] = position;
}
xpot.stepsCount += 1;
}
}
}
}
}
}
}
void adcCalibSetMidPoint()
{
for (uint8_t i = 0; i < MAX_ANALOG_INPUTS; i++) {
auto& calib = reusableBuffer.calib.inputs[i];
if (i < MAX_STICKS || !IS_POT_MULTIPOS(i - MAX_STICKS)) {
calib.input.loVal = 15000;
calib.input.hiVal = -15000;
calib.input.midVal = getAnalogValue(i) >> 1;
} else {
calib.xpot.stepsCount = 0;
calib.xpot.lastCount = 0;
}
}
}
void adcCalibSetMinMax()
{
for (uint8_t i = 0; i < MAX_ANALOG_INPUTS; i++) {
auto& calib = reusableBuffer.calib.inputs[i];
if (abs(calib.input.loVal - calib.input.hiVal) > 50) {
g_eeGeneral.calib[i].mid = calib.input.midVal;
int16_t v = calib.input.midVal - calib.input.loVal;
g_eeGeneral.calib[i].spanNeg = v - v / STICK_TOLERANCE;
v = calib.input.hiVal - calib.input.midVal;
g_eeGeneral.calib[i].spanPos = v - v / STICK_TOLERANCE;
}
}
}
void adcCalibSetXPot()
{
for (uint8_t i = MAX_STICKS; i < MAX_ANALOG_INPUTS; i++) {
int idx = i - MAX_STICKS;
if (!IS_POT_MULTIPOS(idx)) continue;
auto& xpot = reusableBuffer.calib.inputs[i].xpot;
int count = xpot.stepsCount;
if (count > 1 && count <= XPOTS_MULTIPOS_COUNT) {
for (int j = 0; j < count; j++) {
for (int k = j + 1; k < count; k++) {
if (xpot.steps[k] < xpot.steps[j]) {
SWAP(xpot.steps[j], xpot.steps[k]);
}
}
}
StepsCalibData* calib = (StepsCalibData*)&g_eeGeneral.calib[i];
calib->count = count - 1;
for (int j = 0; j < calib->count; j++) {
calib->steps[j] = (xpot.steps[j + 1] + xpot.steps[j]) >> 5;
}
} else {
// TODO: a way to provide to 6POS default calibration values
//
// // load 6pos calib with factory data if 6 pos was not manually calibrated
// constexpr int16_t factoryValues[]= {0x5,0xd,0x16,0x1f,0x28};
// StepsCalibData * calib = (StepsCalibData *) &g_eeGeneral.calib[POT3];
// calib->count = 5;
// for (int j=0; j<calib->count ; j++) {
// calib->steps[j] = factoryValues[j];
// }
// not enough config points -> disable
g_eeGeneral.potsConfig &= POT_CONFIG_DISABLE_MASK(idx);
}
}
}
void adcCalibStore()
{
g_eeGeneral.chkSum = evalChkSum();
storageDirty(EE_GENERAL);
}
uint16_t getRTCBatteryVoltage()
{
#if defined(HAS_TX_RTC_VOLTAGE)
return (getAnalogValue(TX_RTC_VOLTAGE) * ADC_VREF_PREC2) / 2048;
#elif defined(PCBX10) || defined(PCBX12S)
return (rtcBatteryVoltage * 2 * ADC_VREF_PREC2) / 2048;
#elif defined(PCBNV14)
#warning "TODO RTC voltage"
return 330;
// anaIn() outputs value divided by (1 << ANALOG_SCALE)
if (adcGetMaxInputs(ADC_INPUT_RTC_BAT) < 1) return 0;
#if defined(STM32F413xx)
return (anaIn(adcGetInputOffset(ADC_INPUT_RTC_BAT)) * ADC_VREF_PREC2) /
(1024 >> ANALOG_SCALE);
#else
#warning "RTC battery not supported on this target"
return 0;
return (anaIn(adcGetInputOffset(ADC_INPUT_RTC_BAT)) * ADC_VREF_PREC2) /
(2048 >> ANALOG_SCALE);
#endif
}
uint16_t getAnalogValue(uint8_t index)
{
if (IS_POT(index) && !IS_POT_SLIDER_AVAILABLE(index)) {
// Use fixed analog value for non-existing and/or non-connected pots.
// Non-connected analog inputs will slightly follow the adjacent connected analog inputs,
// which produces ghost readings on these inputs.
return 0;
}
#if defined(PCBX9E) || defined(PCBNV14)
index = adcMapping[index];
#endif
if (adcDirection[index] < 0)
return ADCMAXVALUE - adcValues[index];
else
return adcValues[index];
if (index >= MAX_ANALOG_INPUTS) return 0;
return adcValues[index];
}
void setAnalogValue(uint8_t index, uint16_t value)
{
if (index >= MAX_ANALOG_INPUTS) return;
adcValues[index] = value;
}
uint16_t* getAnalogValues()
{
return adcValues;
}
// used by diaganas
uint32_t s_anaFilt[NUM_ANALOGS];
uint32_t s_anaFilt[MAX_ANALOG_INPUTS];
#define ANALOG_MULTIPLIER (1<<ANALOG_SCALE)
#define ANA_FILT(chan) (s_anaFilt[chan] / (JITTER_ALPHA * ANALOG_MULTIPLIER))
@@ -217,20 +240,40 @@ uint32_t s_anaFilt[NUM_ANALOGS];
uint16_t anaIn(uint8_t chan)
{
if (chan >= MAX_ANALOG_INPUTS) return 0;
return ANA_FILT(chan);
}
uint32_t anaIn_diag(uint8_t chan)
{
if (chan >= MAX_ANALOG_INPUTS) return 0;
return s_anaFilt[chan] / JITTER_ALPHA;
}
void anaSetFiltered(uint8_t chan, uint16_t val)
{
val += RESX;
s_anaFilt[chan] = val * (JITTER_ALPHA * ANALOG_MULTIPLIER);
}
void anaResetFiltered()
{
memset(s_anaFilt, 0, sizeof(s_anaFilt));
}
#if defined(JITTER_MEASURE)
JitterMeter<uint16_t> rawJitter[NUM_ANALOGS];
JitterMeter<uint16_t> avgJitter[NUM_ANALOGS];
JitterMeter<uint16_t> rawJitter[MAX_ANALOG_INPUTS];
JitterMeter<uint16_t> avgJitter[MAX_ANALOG_INPUTS];
tmr10ms_t jitterResetTime = 0;
#endif
uint16_t getBatteryVoltage()
{
// using filtered ADC value on purpose
int32_t instant_vbat = anaIn(TX_VOLTAGE);
if (adcGetMaxInputs(ADC_INPUT_VBAT) < 1) return 0;
int32_t instant_vbat = anaIn(adcGetInputOffset(ADC_INPUT_VBAT));
// TODO: remove BATT_SCALE / BATTERY_DIVIDER defines
#if defined(BATT_SCALE)
instant_vbat =
(instant_vbat * BATT_SCALE * (128 + g_eeGeneral.txVoltageCalibration)) /
@@ -245,14 +288,14 @@ uint16_t getBatteryVoltage()
#endif
}
extern uint16_t get_flysky_hall_adc_value(uint8_t ch);
void getADC()
{
uint8_t max_analogs = adcGetMaxInputs(ADC_INPUT_ALL);
#if defined(JITTER_MEASURE)
if (JITTER_MEASURE_ACTIVE() && jitterResetTime < get_tmr10ms()) {
// reset jitter measurement every second
for (uint32_t x=0; x<NUM_ANALOGS; x++) {
for (uint32_t x = 0; x < max_analogs; x++) {
rawJitter[x].reset();
avgJitter[x].reset();
}
@@ -265,22 +308,12 @@ void getADC()
TRACE("adcRead failed");
DEBUG_TIMER_STOP(debugTimerAdcRead);
for (uint8_t x=0; x<NUM_ANALOGS; x++) {
uint32_t v;
#if defined(FLYSKY_GIMBAL)
if (globalData.flyskygimbals)
{
if (x < 4) {
v = get_flysky_hall_adc_value(x) >> (1 - ANALOG_SCALE);
} else {
v = getAnalogValue(x) >> (1 - ANALOG_SCALE);
}
}
else
#endif
{
v = getAnalogValue(x) >> (1 - ANALOG_SCALE);
}
// TODO: jitter filter should probably still be applied
// to VBAT and RTC_BAT, no matter what is configured
//
for (uint8_t x = 0; x < max_analogs; x++) {
uint32_t v = getAnalogValue(x) >> (1 - ANALOG_SCALE);
// Jitter filter:
// * pass trough any big change directly
@@ -362,4 +395,87 @@ void getADC()
}
}
#endif // #if !defined(SIMU)
potconfig_t adcGetDefaultPotsConfig()
{
if (!_hal_adc_driver) return 0;
return _hal_adc_driver->default_pots_cfg;
}
uint8_t adcGetMaxInputs(uint8_t type)
{
if (type > ADC_INPUT_ALL) return 0;
return _hal_adc_inputs[type].n_inputs;
}
uint8_t adcGetInputOffset(uint8_t type)
{
if (type > ADC_INPUT_ALL) return 0;
return _hal_adc_inputs[type].offset;
}
uint16_t adcGetInputValue(uint8_t type, uint8_t idx)
{
if (type >= ADC_INPUT_ALL) return 0;
const auto& inputs = _hal_adc_inputs[type];
auto n_inputs = inputs.n_inputs;
auto offset = inputs.offset;
if (!n_inputs || idx >= n_inputs) return 0;
return getAnalogValue(offset + idx);
}
const char* adcGetInputName(uint8_t type, uint8_t idx)
{
if (type >= ADC_INPUT_ALL ||
idx >= _hal_adc_inputs[type].n_inputs)
return "";
return _hal_adc_inputs[type].inputs[idx].name;
}
const char* adcGetInputName(uint8_t idx)
{
uint8_t type = ADC_INPUT_MAIN;
// find the proper input type
while (_hal_adc_inputs[type].offset + _hal_adc_inputs[type].n_inputs <= idx) {
if (++type > ADC_INPUT_AXIS) return nullptr;
}
idx -= _hal_adc_inputs[type].offset;
return _hal_adc_inputs[type].inputs[idx].name;
}
int adcGetInputIdx(const char* input, uint8_t len)
{
int idx = 0;
uint8_t type = ADC_INPUT_MAIN;
do {
for (uint8_t i = 0; i < _hal_adc_inputs[type].n_inputs; i++, idx++) {
if (!strncmp(_hal_adc_inputs[type].inputs[i].name, input, len))
return idx;
}
} while(++type < ADC_INPUT_ALL);
return -1;
}
const char* adcGetInputLabel(uint8_t type, uint8_t idx)
{
if (type >= ADC_INPUT_ALL ||
idx >= _hal_adc_inputs[type].n_inputs)
return "";
return _hal_adc_inputs[type].inputs[idx].label;
}
const char* adcGetInputShortLabel(uint8_t type, uint8_t idx)
{
if (type >= ADC_INPUT_ALL ||
idx >= _hal_adc_inputs[type].n_inputs)
return "";
return _hal_adc_inputs[type].inputs[idx].short_label;
}
+91 -37
View File
@@ -22,38 +22,62 @@
#pragma once
#include <stdint.h>
#include "opentx_types.h"
// TODO: move this to the targets
#if NUM_PWMSTICKS > 0
#define FIRST_ANALOG_ADC (STICKS_PWM_ENABLED() ? NUM_PWMSTICKS : 0)
#define NUM_ANALOGS_ADC (STICKS_PWM_ENABLED() ? (NUM_ANALOGS - NUM_PWMSTICKS) : NUM_ANALOGS)
#elif defined(PCBX9E)
#define FIRST_ANALOG_ADC 0
#define NUM_ANALOGS_ADC 11
#define NUM_ANALOGS_ADC_EXT (NUM_ANALOGS - NUM_ANALOGS_ADC)
#else
#define FIRST_ANALOG_ADC_FS 4
#if defined (RADIO_BOXER)
#define NUM_ANALOGS_ADC_FS 5
#else
#define NUM_ANALOGS_ADC_FS 10
#define NUM_ANALOGS_ADC_EXT 2
#endif
#define FIRST_ANALOG_ADC 0
#define NUM_ANALOGS_ADC NUM_ANALOGS
#endif
// 12-bit values
#define ADC_MAX_VALUE 4095
#define ADCMAXVALUE 4095
#define ADC_INVERT_VALUE(v) (ADC_MAX_VALUE - (v))
// TODO: get rid of this special case
#if defined(PCBX10) || defined(PCBX12S)
extern uint16_t rtcBatteryVoltage;
#endif
// tune this value, bigger value - more filtering (range: 1-5) (see explanation below)
#define JITTER_FILTER_STRENGTH 4
// tune this value, bigger value - more filtering (range: 0-1) (see explanation below)
#define ANALOG_SCALE 1
#define JITTER_ALPHA (1<<JITTER_FILTER_STRENGTH)
enum {
ADC_INPUT_MAIN=0, // gimbals / wheel + throttle
ADC_INPUT_POT,
ADC_INPUT_AXIS,
ADC_INPUT_VBAT,
ADC_INPUT_RTC_BAT,
ADC_INPUT_ALL,
};
// Air radios
enum {
ADC_MAIN_LH,
ADC_MAIN_LV,
ADC_MAIN_RV,
ADC_MAIN_RH,
};
// Surface radios
enum {
ADC_MAIN_ST,
ADC_MAIN_TH,
};
struct etx_hal_adc_input_t {
const char* name;
const char* label;
const char* short_label;
};
struct etx_hal_adc_inputs_t {
uint8_t n_inputs;
uint8_t offset;
const etx_hal_adc_input_t* inputs;
};
struct etx_hal_adc_driver_t {
const etx_hal_adc_inputs_t* inputs; // ADC_INPUT_ALL + 1;
potconfig_t default_pots_cfg;
bool (*init)();
bool (*start_conversion)();
void (*wait_completion)();
void (*wait_completion)();
};
bool adcInit(const etx_hal_adc_driver_t* driver);
@@ -63,22 +87,52 @@ bool adcRead();
uint16_t getBatteryVoltage();
uint16_t getRTCBatteryVoltage();
uint16_t getAnalogValue(uint8_t index);
void setAnalogValue(uint8_t index, uint16_t value);
uint16_t* getAnalogValues();
#define JITTER_FILTER_STRENGTH 4 // tune this value, bigger value - more filtering (range: 1-5) (see explanation below)
#define ANALOG_SCALE 1 // tune this value, bigger value - more filtering (range: 0-1) (see explanation below)
#define JITTER_ALPHA (1<<JITTER_FILTER_STRENGTH)
// Run calibration steps
void adcCalibMinMax();
void adcCalibSetMidPoint();
void adcCalibSetMinMax();
void adcCalibSetXPot();
void adcCalibStore();
#if defined(JITTER_MEASURE)
extern JitterMeter<uint16_t> rawJitter[NUM_ANALOGS];
extern JitterMeter<uint16_t> avgJitter[NUM_ANALOGS];
#if defined(PCBHORUS) || defined(PCBTARANIS)
#define JITTER_MEASURE_ACTIVE() (menuHandlers[menuLevel] == menuRadioDiagAnalogs)
#elif defined(CLI)
#define JITTER_MEASURE_ACTIVE() (1)
#else
#define JITTER_MEASURE_ACTIVE() (0)
#endif
extern JitterMeter<uint16_t> rawJitter[MAX_ANALOG_INPUTS];
extern JitterMeter<uint16_t> avgJitter[MAX_ANALOG_INPUTS];
#endif
void getADC();
uint16_t anaIn(uint8_t chan);
uint32_t anaIn_diag(uint8_t chan);
uint16_t getBatteryVoltage();
// Warning:
// STM32 uses a 25K+25K voltage divider bridge to measure the battery voltage
// Measuring VBAT puts considerable drain (22 µA) on the battery instead of
// normal drain (~10 nA)
void enableVBatBridge();
void disableVBatBridge();
bool isVBatBridgeEnabled();
potconfig_t adcGetDefaultPotsConfig();
uint8_t adcGetMaxInputs(uint8_t type);
uint8_t adcGetInputOffset(uint8_t type);
uint16_t adcGetInputValue(uint8_t type, uint8_t idx);
const char* adcGetInputName(uint8_t type, uint8_t idx);
const char* adcGetInputName(uint8_t idx);
int adcGetInputIdx(const char* input, uint8_t len);
const char* adcGetInputLabel(uint8_t type, uint8_t idx);
const char* adcGetInputShortLabel(uint8_t type, uint8_t idx);
// To be implemented by the target driver
// int8_t adcGetVRTC();
// int8_t adcGetVBAT();
// const char* adcGetStickName(uint8_t idx);
// const char* adcGetPotName(uint8_t idx);
// uint8_t adcGetMaxSticks();
// uint8_t adcGetMaxPots();
+46
View File
@@ -0,0 +1,46 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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.
*/
#include "key_driver.h"
#include "definitions.h"
#include "hal_keys.inc"
uint32_t keysGetSupported()
{
return _defined_keys;
}
uint8_t keysGetMaxKeys()
{
return _n_keys;
}
uint8_t keysGetMaxTrims()
{
return _n_trims;
}
const char* keysGetLabel(EnumKeys key)
{
return _key_labels[key];
}
+70
View File
@@ -0,0 +1,70 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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.
*/
#pragma once
#include <stdint.h>
enum EnumKeys {
KEY_MENU,
KEY_EXIT,
KEY_ENTER,
KEY_PAGE,
KEY_PAGEUP,
KEY_PAGEDN,
KEY_UP,
KEY_DOWN,
KEY_LEFT,
KEY_RIGHT,
KEY_PLUS,
KEY_MINUS,
KEY_MODEL,
KEY_TELE,
KEY_SYS,
KEY_SHIFT,
KEY_BIND,
MAX_KEYS
};
#define MAX_TRIMS 6
// returns a bit field with each key set as (1 << KEY_xxx)
uint32_t readKeys();
// returns a bit field with each trim key
uint32_t readTrims();
// Init GPIO ports
void keysInit();
uint32_t keysGetSupported();
uint8_t keysGetMaxKeys();
uint8_t keysGetMaxTrims();
const char* keysGetLabel(EnumKeys key);
+44
View File
@@ -0,0 +1,44 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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.
*/
#pragma once
#include <stdint.h>
#define ROTENC_LOWSPEED 1
#define ROTENC_MIDSPEED 5
#define ROTENC_HIGHSPEED 50
#define ROTARY_ENCODER_GRANULARITY 2
typedef int32_t rotenc_t;
void rotaryEncoderInit();
// return impulses / granularity
rotenc_t rotaryEncoderGetValue();
// returns raw # impulses
rotenc_t rotaryEncoderGetRawValue();
int8_t rotaryEncoderGetAccel();
void rotaryEncoderResetAccel();
+82
View File
@@ -0,0 +1,82 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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.
*/
#pragma once
#include <stdint.h>
#include "opentx_types.h"
enum SwitchHwType {
SWITCH_HW_2POS = 0,
SWITCH_HW_3POS,
SWITCH_HW_ADC,
};
enum SwitchHwPos {
SWITCH_HW_UP = 0,
SWITCH_HW_MID,
SWITCH_HW_DOWN,
};
#define SWITCH_HW_INVERTED 1
// init hardware for switches
void switchInit();
// returns the factory configuration for all switches
swconfig_t switchGetDefaultConfig();
// returns the maximum number of regular switches supported in hardware
uint8_t switchGetMaxSwitches();
// returns the number of configured switches
uint8_t getSwitchCount();
// returns the highest row for a specified column
uint8_t switchGetMaxRow(uint8_t col);
// returns the maximum number of function switches supported in hardware
uint8_t switchGetMaxFctSwitches();
struct switch_display_pos_t {
uint8_t col;
uint8_t row;
};
switch_display_pos_t switchGetDisplayPosition(uint8_t idx);
// The functions bellow support regular as well as function switches.
//
// Regular switches are indexed from `0` to `switchGetMaxSwitches() - 1`
// Function switches are indexed above the previous range.
//
// The total number of switches (and max index + 1) is:
// switchGetMaxSwitches() + switchGetMaxFctSwitches()
//
// returns state (0 / 1) of a specific switch position
uint32_t switchState(uint8_t pos_idx);
// returns a position for a switch index
SwitchHwPos switchGetPosition(uint8_t idx);
const char* switchGetName(uint8_t idx);
SwitchHwType switchGetHwType(uint8_t idx);