From 207a3d8c9866d640ae9d444ff406d9a0af7622d5 Mon Sep 17 00:00:00 2001 From: Andrey Gusakov Date: Thu, 9 May 2024 14:26:51 +0300 Subject: [PATCH] adc: avoid two lookup tables --- firmware/hw_layer/adc/AdcConfiguration.h | 5 +---- firmware/hw_layer/adc/adc_inputs.cpp | 13 ++++++++----- firmware/hw_layer/adc/adc_inputs.h | 2 -- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/firmware/hw_layer/adc/AdcConfiguration.h b/firmware/hw_layer/adc/AdcConfiguration.h index 1a062dae3d..cb78ce45b7 100644 --- a/firmware/hw_layer/adc/AdcConfiguration.h +++ b/firmware/hw_layer/adc/AdcConfiguration.h @@ -30,7 +30,7 @@ class AdcDevice { public: explicit AdcDevice(ADCConversionGroup* hwConfig, adcsample_t *buf, size_t buf_len); void enableChannel(adc_channel_e hwChannelIndex); - adc_channel_e getAdcHardwareIndexByInternalIndex(int index) const; + adc_channel_e getAdcChannelByInternalIndex(int index) const; uint8_t internalAdcIndexByHardwareIndex[EFI_ADC_TOTAL_CHANNELS]; int size() const; void init(void); @@ -46,9 +46,6 @@ private: * Number of ADC channels in use */ size_t channelCount = 0; - - /* STM32 has up-to 4 additional channels routed to internal voltage sources */ - adc_channel_e hardwareIndexByIndernalAdcIndex[ADC_MAX_CHANNELS_COUNT + 4]; }; #endif /* HAL_USE_ADC */ diff --git a/firmware/hw_layer/adc/adc_inputs.cpp b/firmware/hw_layer/adc/adc_inputs.cpp index b8ed0810bf..905a7b025f 100644 --- a/firmware/hw_layer/adc/adc_inputs.cpp +++ b/firmware/hw_layer/adc/adc_inputs.cpp @@ -65,7 +65,6 @@ AdcDevice::AdcDevice(ADCConversionGroup* p_hwConfig, adcsample_t *p_buf, size_t hwConfig->sqr4 = 0; hwConfig->sqr5 = 0; #endif /* ADC_MAX_CHANNELS_COUNT */ - memset(hardwareIndexByIndernalAdcIndex, EFI_ADC_NONE, sizeof(hardwareIndexByIndernalAdcIndex)); memset(internalAdcIndexByHardwareIndex, 0xFF, sizeof(internalAdcIndexByHardwareIndex)); } @@ -260,7 +259,6 @@ void AdcDevice::enableChannel(adc_channel_e hwChannel) { size_t channelAdcIndex = hwChannel - EFI_ADC_0; internalAdcIndexByHardwareIndex[hwChannel] = logicChannel; - hardwareIndexByIndernalAdcIndex[logicChannel] = hwChannel; if (logicChannel < 6) { hwConfig->sqr3 |= channelAdcIndex << (5 * logicChannel); } else if (logicChannel < 12) { @@ -278,8 +276,13 @@ void AdcDevice::enableChannel(adc_channel_e hwChannel) { #endif /* ADC_MAX_CHANNELS_COUNT */ } -adc_channel_e AdcDevice::getAdcHardwareIndexByInternalIndex(int hwChannel) const { - return hardwareIndexByIndernalAdcIndex[hwChannel]; +adc_channel_e AdcDevice::getAdcChannelByInternalIndex(int hwChannel) const { + for (size_t idx = EFI_ADC_0; idx < EFI_ADC_TOTAL_CHANNELS; idx++) { + if (internalAdcIndexByHardwareIndex[idx] == hwChannel) { + return (adc_channel_e)idx; + } + } + return EFI_ADC_NONE; } #endif // EFI_USE_FAST_ADC @@ -298,7 +301,7 @@ void printFullAdcReport(void) { efiPrintf("fast %d samples", fastAdc.conversionCount); for (int internalIndex = 0; internalIndex < fastAdc.size(); internalIndex++) { - adc_channel_e hwChannel = fastAdc.getAdcHardwareIndexByInternalIndex(internalIndex); + adc_channel_e hwChannel = fastAdc.getAdcChannelByInternalIndex(internalIndex); if (isAdcChannelValid(hwChannel)) { ioportid_t port = getAdcChannelPort("print", hwChannel); diff --git a/firmware/hw_layer/adc/adc_inputs.h b/firmware/hw_layer/adc/adc_inputs.h index 0530a8902d..f61235470d 100644 --- a/firmware/hw_layer/adc/adc_inputs.h +++ b/firmware/hw_layer/adc/adc_inputs.h @@ -66,8 +66,6 @@ void waitForSlowAdc(uint32_t lastAdcCounter = 0); // get a number of completed slowADC samples int getSlowAdcCounter(); -int getAdcHardwareIndexByInternalIndex(int index); - void printFullAdcReportIfNeeded(void); int getInternalAdcValue(const char *msg, adc_channel_e index); float getMCUInternalTemperature(void);