From ec835263701505e9932b9b33aa541473a26c2c2a Mon Sep 17 00:00:00 2001 From: rusefillc <48498823+rusefillc@users.noreply.github.com> Date: Wed, 20 Nov 2024 09:18:28 -0500 Subject: [PATCH] modularize knock adc for h7 (#7086) * modularize knock adc for h7 only:uaefi * modularize knock adc for h7 only:uaefi --- .../sensors/impl/software_knock.cpp | 95 ++----------------- firmware/hw_layer/adc/adc_inputs.h | 2 + .../hw_layer/ports/stm32/stm32_adc_v2.cpp | 93 ++++++++++++++++++ 3 files changed, 102 insertions(+), 88 deletions(-) diff --git a/firmware/controllers/sensors/impl/software_knock.cpp b/firmware/controllers/sensors/impl/software_knock.cpp index fdb6125de0..ca84bf0b4e 100644 --- a/firmware/controllers/sensors/impl/software_knock.cpp +++ b/firmware/controllers/sensors/impl/software_knock.cpp @@ -38,93 +38,13 @@ static volatile size_t sampleCount = 0; chibios_rt::BinarySemaphore knockSem(/* taken =*/ true); -static void completionCallback(ADCDriver* adcp) { - if (adcp->state == ADC_COMPLETE) { - knockNeedsProcess = true; +void onKnockSamplingComplete() { + knockNeedsProcess = true; - // Notify the processing thread that it's time to process this sample - chSysLockFromISR(); - knockSem.signalI(); - chSysUnlockFromISR(); - } -} - -static void errorCallback(ADCDriver*, adcerror_t) { -} - -static const uint32_t smpr1 = - ADC_SMPR1_SMP_AN10(KNOCK_SAMPLE_TIME) | - ADC_SMPR1_SMP_AN11(KNOCK_SAMPLE_TIME) | - ADC_SMPR1_SMP_AN12(KNOCK_SAMPLE_TIME) | - ADC_SMPR1_SMP_AN13(KNOCK_SAMPLE_TIME) | - ADC_SMPR1_SMP_AN14(KNOCK_SAMPLE_TIME) | - ADC_SMPR1_SMP_AN15(KNOCK_SAMPLE_TIME); - -static const uint32_t smpr2 = - ADC_SMPR2_SMP_AN0(KNOCK_SAMPLE_TIME) | - ADC_SMPR2_SMP_AN1(KNOCK_SAMPLE_TIME) | - ADC_SMPR2_SMP_AN2(KNOCK_SAMPLE_TIME) | - ADC_SMPR2_SMP_AN3(KNOCK_SAMPLE_TIME) | - ADC_SMPR2_SMP_AN4(KNOCK_SAMPLE_TIME) | - ADC_SMPR2_SMP_AN5(KNOCK_SAMPLE_TIME) | - ADC_SMPR2_SMP_AN6(KNOCK_SAMPLE_TIME) | - ADC_SMPR2_SMP_AN7(KNOCK_SAMPLE_TIME) | - ADC_SMPR2_SMP_AN8(KNOCK_SAMPLE_TIME) | - ADC_SMPR2_SMP_AN9(KNOCK_SAMPLE_TIME); - -static const ADCConversionGroup adcConvGroupCh1 = { - .circular = FALSE, - .num_channels = 1, - .end_cb = &completionCallback, - .error_cb = &errorCallback, - .cr1 = 0, - .cr2 = ADC_CR2_SWSTART, - // sample times for channels 10...18 - .smpr1 = smpr1, - // sample times for channels 0...9 - .smpr2 = smpr2, - - .htr = 0, - .ltr = 0, - - .sqr1 = 0, - .sqr2 = 0, - .sqr3 = ADC_SQR3_SQ1_N(KNOCK_ADC_CH1) -}; - -// Not all boards have a second channel - configure it if it exists -#if KNOCK_HAS_CH2 -static const ADCConversionGroup adcConvGroupCh2 = { - .circular = FALSE, - .num_channels = 1, - .end_cb = &completionCallback, - .error_cb = &errorCallback, - .cr1 = 0, - .cr2 = ADC_CR2_SWSTART, - // sample times for channels 10...18 - .smpr1 = smpr1, - // sample times for channels 0...9 - .smpr2 = smpr2, - - .htr = 0, - .ltr = 0, - - .sqr1 = 0, - .sqr2 = 0, - .sqr3 = ADC_SQR3_SQ1_N(KNOCK_ADC_CH2) -}; -#endif // KNOCK_HAS_CH2 - -static const ADCConversionGroup* getConversionGroup(uint8_t channelIdx) { -#if KNOCK_HAS_CH2 - if (channelIdx == 1) { - return &adcConvGroupCh2; - } -#else - (void)channelIdx; -#endif // KNOCK_HAS_CH2 - - return &adcConvGroupCh1; + // Notify the processing thread that it's time to process this sample + chSysLockFromISR(); + knockSem.signalI(); + chSysUnlockFromISR(); } void onStartKnockSampling(uint8_t cylinderNumber, float samplingSeconds, uint8_t channelIdx) { @@ -149,7 +69,7 @@ void onStartKnockSampling(uint8_t cylinderNumber, float samplingSeconds, uint8_t sampleCount = 0xFFFFFFFE & static_cast(clampF(100, samplingSeconds * sampleRate, efi::size(sampleBuffer))); // Select the appropriate conversion group - it will differ depending on which sensor this cylinder should listen on - auto conversionGroup = getConversionGroup(channelIdx); + auto conversionGroup = getKnockConversionGroup(channelIdx); //current chanel number for spectrum TS plugin channelNumber = channelIdx; @@ -181,7 +101,6 @@ void initSoftwareKnock() { } knockFilter.configureBandpass(KNOCK_SAMPLE_RATE, frequencyHz, 3); - adcStart(&KNOCK_ADC, nullptr); #ifdef KNOCK_SPECTROGRAM if(engineConfiguration->enableKnockSpectrogram) diff --git a/firmware/hw_layer/adc/adc_inputs.h b/firmware/hw_layer/adc/adc_inputs.h index 31eb885ab6..f494e4a4ab 100644 --- a/firmware/hw_layer/adc/adc_inputs.h +++ b/firmware/hw_layer/adc/adc_inputs.h @@ -112,6 +112,8 @@ static constexpr AdcToken invalidAdcToken = (AdcToken)(-1); AdcToken enableFastAdcChannel(const char* msg, adc_channel_e channel); adcsample_t getFastAdc(AdcToken token); +const ADCConversionGroup* getKnockConversionGroup(uint8_t channelIdx); +void onKnockSamplingComplete(); #endif // HAL_USE_ADC void printFullAdcReport(void); diff --git a/firmware/hw_layer/ports/stm32/stm32_adc_v2.cpp b/firmware/hw_layer/ports/stm32/stm32_adc_v2.cpp index 1979125fdf..1ca62e2e86 100644 --- a/firmware/hw_layer/ports/stm32/stm32_adc_v2.cpp +++ b/firmware/hw_layer/ports/stm32/stm32_adc_v2.cpp @@ -52,6 +52,10 @@ void portInitAdc() { nvicDisableVector(STM32_ADC_NUMBER); } #endif + + #ifdef KNOCK_ADC + adcStart(&KNOCK_ADC, nullptr); + #endif // KNOCK_ADC } /* @@ -227,4 +231,93 @@ adcsample_t getFastAdc(AdcToken token) { #endif +#ifdef EFI_SOFTWARE_KNOCK +#include "knock_config.h" + +static void knockCompletionCallback(ADCDriver* adcp) { + if (adcp->state == ADC_COMPLETE) { + onKnockSamplingComplete(); + } +} + +static void knockErrorCallback(ADCDriver*, adcerror_t) { +} + +static const uint32_t smpr1 = + ADC_SMPR1_SMP_AN10(KNOCK_SAMPLE_TIME) | + ADC_SMPR1_SMP_AN11(KNOCK_SAMPLE_TIME) | + ADC_SMPR1_SMP_AN12(KNOCK_SAMPLE_TIME) | + ADC_SMPR1_SMP_AN13(KNOCK_SAMPLE_TIME) | + ADC_SMPR1_SMP_AN14(KNOCK_SAMPLE_TIME) | + ADC_SMPR1_SMP_AN15(KNOCK_SAMPLE_TIME); + +static const uint32_t smpr2 = + ADC_SMPR2_SMP_AN0(KNOCK_SAMPLE_TIME) | + ADC_SMPR2_SMP_AN1(KNOCK_SAMPLE_TIME) | + ADC_SMPR2_SMP_AN2(KNOCK_SAMPLE_TIME) | + ADC_SMPR2_SMP_AN3(KNOCK_SAMPLE_TIME) | + ADC_SMPR2_SMP_AN4(KNOCK_SAMPLE_TIME) | + ADC_SMPR2_SMP_AN5(KNOCK_SAMPLE_TIME) | + ADC_SMPR2_SMP_AN6(KNOCK_SAMPLE_TIME) | + ADC_SMPR2_SMP_AN7(KNOCK_SAMPLE_TIME) | + ADC_SMPR2_SMP_AN8(KNOCK_SAMPLE_TIME) | + ADC_SMPR2_SMP_AN9(KNOCK_SAMPLE_TIME); + +static const ADCConversionGroup adcConvGroupCh1 = { + .circular = FALSE, + .num_channels = 1, + .end_cb = &knockCompletionCallback, + .error_cb = &knockErrorCallback, + .cr1 = 0, + .cr2 = ADC_CR2_SWSTART, + // sample times for channels 10...18 + .smpr1 = smpr1, + // sample times for channels 0...9 + .smpr2 = smpr2, + + .htr = 0, + .ltr = 0, + + .sqr1 = 0, + .sqr2 = 0, + .sqr3 = ADC_SQR3_SQ1_N(KNOCK_ADC_CH1) +}; + +// Not all boards have a second channel - configure it if it exists +#if KNOCK_HAS_CH2 +static const ADCConversionGroup adcConvGroupCh2 = { + .circular = FALSE, + .num_channels = 1, + .end_cb = &knockCompletionCallback, + .error_cb = &knockErrorCallback, + .cr1 = 0, + .cr2 = ADC_CR2_SWSTART, + // sample times for channels 10...18 + .smpr1 = smpr1, + // sample times for channels 0...9 + .smpr2 = smpr2, + + .htr = 0, + .ltr = 0, + + .sqr1 = 0, + .sqr2 = 0, + .sqr3 = ADC_SQR3_SQ1_N(KNOCK_ADC_CH2) +}; +#endif // KNOCK_HAS_CH2 + +const ADCConversionGroup* getKnockConversionGroup(uint8_t channelIdx) { +#if KNOCK_HAS_CH2 + if (channelIdx == 1) { + return &adcConvGroupCh2; + } +#else + (void)channelIdx; +#endif // KNOCK_HAS_CH2 + + return &adcConvGroupCh1; +} + +#endif // EFI_SOFTWARE_KNOCK + #endif // HAL_USE_ADC