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
@@ -0,0 +1,173 @@
/*
* 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 "ads79xx.h"
#include "delays_driver.h"
#include "stm32_hal_ll.h"
#define RESETCMD 0x4000
#define MANUAL_MODE 0x1000 // manual mode channel 0
#define MANUAL_MODE_CHANNEL(x) (MANUAL_MODE | ((x) << 7))
static uint16_t ads79xx_rw(SPI_TypeDef* SPIx, uint16_t value)
{
while (!LL_SPI_IsActiveFlag_TXE(SPIx));
LL_SPI_TransmitData16(SPIx, value);
while (!LL_SPI_IsActiveFlag_RXNE(SPIx));
return LL_SPI_ReceiveData16(SPIx);
}
void ads79xx_init(const stm32_spi_adc_t* adc)
{
LL_GPIO_InitTypeDef pinInit;
LL_GPIO_StructInit(&pinInit);
pinInit.Pin = adc->GPIO_Pins;
pinInit.Mode = LL_GPIO_MODE_ALTERNATE;
pinInit.Alternate = adc->GPIO_AF;
LL_GPIO_Init(adc->GPIOx, &pinInit);
pinInit.Pin = adc->GPIO_CS;
pinInit.Mode = LL_GPIO_MODE_OUTPUT;
pinInit.Alternate = LL_GPIO_AF_0;
LL_GPIO_Init(adc->GPIOx, &pinInit);
auto SPIx = adc->SPIx;
LL_SPI_DeInit(SPIx);
LL_SPI_InitTypeDef spiInit;
LL_SPI_StructInit(&spiInit);
spiInit.TransferDirection = LL_SPI_FULL_DUPLEX;
spiInit.Mode = LL_SPI_MODE_MASTER;
spiInit.DataWidth = LL_SPI_DATAWIDTH_16BIT;
spiInit.NSS = LL_SPI_NSS_SOFT;
spiInit.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV8;
LL_SPI_Init(SPIx, &spiInit);
LL_SPI_Enable(SPIx);
LL_SPI_DisableIT_TXE(SPIx);
LL_SPI_DisableIT_RXNE(SPIx);
_SPI_ADC_CS_HIGH(adc);
delay_01us(1);
_SPI_ADC_CS_LOW(adc);
ads79xx_rw(SPIx, RESETCMD);
_SPI_ADC_CS_HIGH(adc);
delay_01us(1);
_SPI_ADC_CS_LOW(adc);
ads79xx_rw(SPIx, MANUAL_MODE);
_SPI_ADC_CS_HIGH(adc);
}
static void ads79xx_dummy_read(const stm32_spi_adc_t* adc, uint16_t start_channel)
{
// A dummy command to get things started
// (because the sampled data is lagging behind for two command cycles)
_SPI_ADC_CS_LOW(adc);
delay_01us(1);
ads79xx_rw(adc->SPIx, MANUAL_MODE_CHANNEL(start_channel));
_SPI_ADC_CS_HIGH(adc);
delay_01us(1);
}
static uint32_t ads79xx_read_next_channel(const stm32_spi_adc_t* adc,
uint8_t index,
const stm32_adc_input_t* inputs,
const uint8_t* chan, uint8_t nconv)
{
uint32_t result = 0;
// This delay is to allow charging of ADC input capacitor
// after the MUX changes from one channel to the other.
// It was determined experimentally. Biggest problem seems to be
// the cross-talk between A4:S1 and A5:MULTIPOS. Changing S1 for one extreme
// to the other resulted in A5 change of:
//
// delay value A5 change Time needed for adcRead()
// 1 16 0.154ms - 0.156ms
// 38 5 0.197ms - 0.199ms
// 62 0 0.225ms - 0.243ms
delay_01us(40);
for (uint8_t i = 0; i < 4; i++) {
_SPI_ADC_CS_LOW(adc);
delay_01us(1);
// command is changed to the next index for the last two readings
// (because the sampled data is lagging behind for two command cycles)
uint8_t chan_idx = (i > 1 ? index + 1 : index) % nconv;
auto input_idx = chan[chan_idx];
auto spi_chan = inputs[input_idx].ADC_Channel;
uint16_t val = (0x0fff & ads79xx_rw(adc->SPIx, MANUAL_MODE_CHANNEL(spi_chan)));
#if defined(JITTER_MEASURE)
if (JITTER_MEASURE_ACTIVE()) {
rawJitter[index].measure(val);
}
#endif
_SPI_ADC_CS_HIGH(adc);
delay_01us(1);
result += val;
}
return result >> 2;
}
bool ads79xx_adc_start_read(const stm32_spi_adc_t* adc, const stm32_adc_input_t* inputs)
{
auto start_channel = inputs[adc->channels[0]].ADC_Channel;
ads79xx_dummy_read(adc, start_channel);
ads79xx_dummy_read(adc, start_channel);
return true;
}
void ads79xx_adc_wait_completion(const stm32_spi_adc_t* adc, const stm32_adc_input_t* inputs)
{
auto spi_channels = adc->n_channels;
auto chans = adc->channels;
// Fetch buffer from generic ADC driver
auto adcValues = getAnalogValues();
// At this point, the first conversion has been started
// in ads79xx_adc_start_read()
// for each SPI channel
for (uint32_t i = 0; i < spi_channels; i++) {
// read SPI channel
auto input_channel = chans[i];
auto adc_value = ads79xx_read_next_channel(adc, i, inputs, chans, spi_channels);
if (inputs[input_channel].inverted)
adcValues[input_channel] = ADC_INVERT_VALUE(adc_value);
else
adcValues[input_channel] = adc_value;
}
}