From 5837abb4f16df38ae6d20932e3f1b673fe96f60d Mon Sep 17 00:00:00 2001 From: Andrey Gusakov Date: Sun, 12 May 2024 16:13:01 +0300 Subject: [PATCH] ADC: DMA buffers should be quilified volatile --- firmware/hw_layer/adc/AdcConfiguration.h | 4 ++-- firmware/hw_layer/adc/adc_inputs.cpp | 12 +++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/firmware/hw_layer/adc/AdcConfiguration.h b/firmware/hw_layer/adc/AdcConfiguration.h index 7e7d7e1c6f..8646fd68d3 100644 --- a/firmware/hw_layer/adc/AdcConfiguration.h +++ b/firmware/hw_layer/adc/AdcConfiguration.h @@ -23,7 +23,7 @@ class AdcDevice { public: - explicit AdcDevice(ADCConversionGroup* p_hwConfig, adcsample_t *p_buf); + explicit AdcDevice(ADCConversionGroup* p_hwConfig, volatile adcsample_t *p_buf); void enableChannel(adc_channel_e hwChannel); adc_channel_e getAdcChannelByInternalIndex(int index) const; adcsample_t getAvgAdcValue(adc_channel_e hwChannel, size_t bufDepth); @@ -32,7 +32,7 @@ public: void init(void); uint32_t conversionCount = 0; - adcsample_t *samples; + volatile adcsample_t *samples; private: ADCConversionGroup* hwConfig; uint8_t internalAdcIndexByHardwareIndex[EFI_ADC_TOTAL_CHANNELS]; diff --git a/firmware/hw_layer/adc/adc_inputs.cpp b/firmware/hw_layer/adc/adc_inputs.cpp index 2517f51220..f7423666b8 100644 --- a/firmware/hw_layer/adc/adc_inputs.cpp +++ b/firmware/hw_layer/adc/adc_inputs.cpp @@ -33,7 +33,7 @@ float __attribute__((weak)) getAnalogInputDividerCoefficient(adc_channel_e) { #include "periodic_thread_controller.h" #include "protected_gpio.h" -static NO_CACHE adcsample_t slowAdcSamples[SLOW_ADC_CHANNEL_COUNT]; +static volatile NO_CACHE adcsample_t slowAdcSamples[SLOW_ADC_CHANNEL_COUNT]; static adc_channel_mode_e adcHwChannelMode[HW_MAX_ADC_INDEX]; @@ -58,7 +58,7 @@ float getVoltage(const char *msg, adc_channel_e hwChannel) { #define ADC_BUF_DEPTH_FAST 4 #endif -AdcDevice::AdcDevice(ADCConversionGroup* p_hwConfig, adcsample_t *p_buf) { +AdcDevice::AdcDevice(ADCConversionGroup* p_hwConfig, volatile adcsample_t *p_buf) { hwConfig = p_hwConfig; samples = p_buf; @@ -136,7 +136,7 @@ static ADCConversionGroup adcgrpcfgFast = { #endif /* ADC_MAX_CHANNELS_COUNT */ }; -static NO_CACHE adcsample_t fastAdcSampleBuf[ADC_BUF_DEPTH_FAST * ADC_MAX_CHANNELS_COUNT]; +static volatile NO_CACHE adcsample_t fastAdcSampleBuf[ADC_BUF_DEPTH_FAST * ADC_MAX_CHANNELS_COUNT]; AdcDevice fastAdc(&adcgrpcfgFast, fastAdcSampleBuf); @@ -170,7 +170,8 @@ static void fastAdcTrigger(GPTDriver*) { // todo: when? why? criticalError("ADC fast not ready?"); // see notes at https://github.com/rusefi/rusefi/issues/6399 } else { - adcStartConversionI(&ADC_FAST_DEVICE, &adcgrpcfgFast, fastAdc.samples, ADC_BUF_DEPTH_FAST); + /* drop volatile type qualifier - this is safe */ + adcStartConversionI(&ADC_FAST_DEVICE, &adcgrpcfgFast, (adcsample_t *)fastAdc.samples, ADC_BUF_DEPTH_FAST); } chSysUnlockFromISR(); #endif /* EFI_INTERNAL_ADC */ @@ -365,7 +366,8 @@ public: { ScopePerf perf(PE::AdcConversionSlow); - if (!readSlowAnalogInputs(slowAdcSamples)) { + /* drop volatile type qualifier - this is safe */ + if (!readSlowAnalogInputs((adcsample_t *)slowAdcSamples)) { slowAdcErrorsCount++; return; }