Skip to content

Commit

Permalink
adc clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
mck1117 committed Jul 22, 2024
1 parent c5b9f53 commit 1245ddc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 42 deletions.
4 changes: 0 additions & 4 deletions firmware/config/boards/prometheus/efifeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@

#undef CONSOLE_MODE_SWITCH_PORT

//!!!!!!!!!!!!!!!
//#undef EFI_INTERNAL_ADC
//#define EFI_INTERNAL_ADC FALSE

#undef ADC_VCC
#define ADC_VCC 3.275f

Expand Down
58 changes: 20 additions & 38 deletions firmware/hw_layer/adc/adc_inputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,30 +244,22 @@ void AdcDevice::enableChannel(adc_channel_e hwChannel) {
return;
}

int logicChannel = channelCount++;

/* TODO: following is correct for STM32 ADC1/2.
* ADC3 has another input to gpio mapping
* and should be handled separately */
size_t channelAdcIndex = hwChannel - EFI_ADC_0;

internalAdcIndexByHardwareIndex[hwChannel] = logicChannel;
hardwareIndexByIndernalAdcIndex[logicChannel] = hwChannel;
if (logicChannel < 6) {
m_hwConfig->sqr3 |= channelAdcIndex << (5 * logicChannel);
} else if (logicChannel < 12) {
m_hwConfig->sqr2 |= channelAdcIndex << (5 * (logicChannel - 6));
} else if (logicChannel < 18) {
m_hwConfig->sqr1 |= channelAdcIndex << (5 * (logicChannel - 12));
// hwChannel = which external pin are we using
// adcChannelIndex = which ADC channel are we using
// adcIndex = which index does that get in sampling order
size_t adcChannelIndex = hwChannel - EFI_ADC_0;
size_t adcIndex = channelCount++;

internalAdcIndexByHardwareIndex[hwChannel] = adcIndex;
hardwareIndexByIndernalAdcIndex[adcIndex] = hwChannel;

if (adcIndex < 6) {
m_hwConfig->sqr3 |= adcChannelIndex << (5 * adcIndex);
} else if (adcIndex < 12) {
m_hwConfig->sqr2 |= adcChannelIndex << (5 * (adcIndex - 6));
} else if (adcIndex < 18) {
m_hwConfig->sqr1 |= adcChannelIndex << (5 * (adcIndex - 12));
}
#if ADC_MAX_CHANNELS_COUNT > 16
else if (logicChannel < 24) {
m_hwConfig->sqr4 |= channelAdcIndex << (5 * (logicChannel - 18));
}
else if (logicChannel < 30) {
m_hwConfig->sqr5 |= channelAdcIndex << (5 * (logicChannel - 24));
}
#endif /* ADC_MAX_CHANNELS_COUNT */
}

adc_channel_e AdcDevice::getAdcHardwareIndexByInternalIndex(int index) const {
Expand Down Expand Up @@ -350,27 +342,17 @@ void removeFastAdcChannel(const char *name, adc_channel_e setting) {
// Weak link a stub so that every board doesn't have to implement this function
__attribute__((weak)) void setAdcChannelOverrides() { }

static void configureInputs() {
memset(adcHwChannelEnabled, 0, sizeof(adcHwChannelEnabled));
static CCM_OPTIONAL SlowAdcController slowAdcController;

/**
* order of analog channels here is totally random and has no meaning
* we also have some weird implementation with internal indices - that all has no meaning, it's just a random implementation
* which does not mean anything.
*/
void initAdcInputs() {
efiPrintf("initAdcInputs()");

memset(adcHwChannelEnabled, 0, sizeof(adcHwChannelEnabled));

addFastAdcChannel("MAP", engineConfiguration->map.sensor.hwChannel);
addFastAdcChannel("AUXF#1", engineConfiguration->auxFastSensor1_adcChannel);

setAdcChannelOverrides();
}

static CCM_OPTIONAL SlowAdcController slowAdcController;

void initAdcInputs() {
efiPrintf("initAdcInputs()");

configureInputs();

#if EFI_INTERNAL_ADC
portInitAdc();
Expand Down

0 comments on commit 1245ddc

Please sign in to comment.