Skip to content

Commit

Permalink
Better X12S ADC implem
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelcoeffic committed Feb 13, 2023
1 parent 6098dfe commit 22752f8
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 60 deletions.
1 change: 0 additions & 1 deletion radio/src/targets/common/arm/stm32/adc_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const etx_hal_adc_driver_t _adc_driver = {
};

// Exports for SPI ADC driver
uint8_t adc_get_n_inputs() { return n_inputs; }
const stm32_adc_input_t* adc_get_inputs() { return _ADC_inputs; }

const stm32_spi_adc_t* adc_spi_get()
Expand Down
1 change: 0 additions & 1 deletion radio/src/targets/common/arm/stm32/adc_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@

#include "stm32_hal_adc.h"

uint8_t adc_get_n_inputs();
const stm32_adc_input_t* adc_get_inputs();
const stm32_spi_adc_t* adc_spi_get();
6 changes: 3 additions & 3 deletions radio/src/targets/horus/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@
#define ADC_SPI_SLIDER1 7
#define ADC_SPI_SLIDER2 8
#define ADC_SPI_BATT 9
#define ADC_SPI_SLIDER3 10
#define ADC_SPI_SLIDER4 11
#define ADC_SPI_SLIDER3 11
#define ADC_SPI_SLIDER4 10

#define ADC_MAIN ADC3
#define ADC_GPIO_PIN_MOUSE1 GPIO_Pin_8 // PF.08 ADC3_IN6 J5 MOUSE_X
Expand Down Expand Up @@ -395,7 +395,7 @@
#elif defined(PCBX10)
#define ADC_DIRECTION {1,-1,1,-1, -1,1,-1, 1,1,1,1, 1,-1 }
#elif defined(PCBX12S)
#define ADC_DIRECTION {1,-1,1,-1, -1,1,-1, -1,-1, -1,1, 0,0,0}
#define ADC_DIRECTION {1,-1,1,-1, -1,1,-1, 1,-1, -1,-1, 0,0,0}
#else
#error "Missing ADC_DIRECTION array"
#endif
Expand Down
63 changes: 9 additions & 54 deletions radio/src/targets/horus/x12s_adc_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,6 @@
#define ADC_CS_HIGH() LL_GPIO_SetOutputPin(ADC_SPI_GPIO, ADC_SPI_PIN_CS)
#define ADC_CS_LOW() LL_GPIO_ResetOutputPin(ADC_SPI_GPIO, ADC_SPI_PIN_CS)

#define SPI_STICK1 0
#define SPI_STICK2 1
#define SPI_STICK3 2
#define SPI_STICK4 3
#define SPI_S1 4
#define SPI_6POS 5
#define SPI_S2 6
#define SPI_LS 7
#define SPI_RS 8
#define SPI_TX_VOLTAGE 9
#define SPI_L2 10
#define SPI_L1 11
#define RESETCMD 0x4000
#define MANUAL_MODE 0x1000 // manual mode channel 0
#define MANUAL_MODE_CHANNEL(x) (MANUAL_MODE | ((x) << 7))
Expand Down Expand Up @@ -109,11 +97,6 @@ static bool x12s_adc_init()
// init SPI ADC
ADS7952_Init();

// we're going to do it ourselves
stm32_hal_adc_disable_oversampling();

// TODO: fetch internal ADC channel mapping

// init onboard ADC
return _adc_driver.init();
}
Expand Down Expand Up @@ -175,7 +158,7 @@ static uint32_t adcReadNextSPIChannel(uint8_t index, const stm32_adc_input_t* in

bool x12s_adc_start_read()
{
// onboard ADC
// start onboard ADC
_adc_driver.start_conversion();

adcReadSPIDummy();
Expand All @@ -191,13 +174,6 @@ void x12s_adc_wait_completion()
auto chans = spi_adc->channels;
auto inputs = adc_get_inputs();

auto all_channels = adc_get_n_inputs();
auto adc_channels = all_channels - spi_channels;

uint8_t noInternalReads = 0;
uint8_t adc_idx[adc_channels] = { 11, 12, 14 }; // TODO
uint16_t temp[adc_channels] = { 0 };

// Fetch buffer from generic ADC driver
auto adcValues = getAnalogValues();

Expand All @@ -208,38 +184,17 @@ void x12s_adc_wait_completion()
for (uint32_t i = 0; i < spi_channels; i++) {

// read SPI channel
adcValues[chans[i]] = adcReadNextSPIChannel(i, inputs, chans, spi_channels);

// check if not enough internal ADC samples
// or one just finished (TC cleared on new sample started)
if (noInternalReads < 4) {

_adc_driver.wait_completion();

// for each internal ADC channel
for (uint8_t x = 0; x < adc_channels; x++) {
auto input_channel = chans[i];
auto adc_value = adcReadNextSPIChannel(i, inputs, chans, spi_channels);

// do the averaging math
// TODO: fetch proper index! (-> from inputs?) (11, 12, 14)
temp[x] += adcValues[adc_idx[x]];
}

// restart internal ADC if not yet done
if (++noInternalReads < 4) {
_adc_driver.start_conversion();
}
}
if (inputs[input_channel].inverted)
adcValues[input_channel] = ADC_INVERT_VALUE(adc_value);
else
adcValues[input_channel] = adc_value;
}

#if defined(DEBUG)
if (noInternalReads != 4) {
TRACE("Internal ADC problem: reads: %d", noInternalReads);
}
#endif

for (uint8_t x = 0; x < adc_channels; x++) {
adcValues[adc_idx[x]] = temp[x] >> 2;
}
// onboard ADC
_adc_driver.wait_completion();
}

const etx_hal_adc_driver_t x12s_adc_driver = {
Expand Down
2 changes: 1 addition & 1 deletion radio/util/hw_defs/stm32_adc_inputs.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static const stm32_spi_adc_t _ADC_spi[] = {
{% endfor %}
};

{% for adc in adc_inputs.adcs %}
{% for adc in adc_inputs.adcs | selectattr('name', '!=', 'SPI') %}
{% if adc.dma %}
extern "C" void {{ adc.dma_stream_irq_handler }} (void)
{
Expand Down

0 comments on commit 22752f8

Please sign in to comment.