Skip to content

drivers: dma: stm32: take "stream offset" into account when DMAV1 + DMAMUX is used #93673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

erwango
Copy link
Member

@erwango erwango commented Jul 24, 2025

Depending on the series in use dma_config should take into account a stream offset for the channel in use.

Fixes #93617

Depending on the series in use dma_config should take into account
a stream offset for the channel in use.

Signed-off-by: Erwan Gouriou <erwan.gouriou@st.com>
@zephyrbot zephyrbot added platform: STM32 ST Micro STM32 size: XS A PR changing only a single line of code area: ADC Analog-to-Digital Converter (ADC) labels Jul 24, 2025
@erwango
Copy link
Member Author

erwango commented Jul 24, 2025

Probably we need to review this whole offset handling and see why it can't be directly dealt from inside dma driver.

@mathieuchopstm
Copy link
Contributor

mathieuchopstm commented Jul 24, 2025

Probably we need to review this whole offset handling and see why it can't be directly dealt from inside dma driver.

From

#if defined(CONFIG_DMA_STM32U5)
/* from DTS the dma stream id is in range 0..N-1 */
#define STM32_DMA_STREAM_OFFSET 0
#elif !defined(CONFIG_DMA_STM32_V1)
/* from DTS the dma stream id is in range 1..N */
/* so decrease to set range from 0 from now on */
#define STM32_DMA_STREAM_OFFSET 1
#elif defined(CONFIG_DMA_STM32_V1) && defined(CONFIG_DMAMUX_STM32)
/* typically on the stm32H7 series, DMA V1 with mux */
#define STM32_DMA_STREAM_OFFSET 1
#else
/* from DTS the dma stream id is in range 0..N-1 */
#define STM32_DMA_STREAM_OFFSET 0
#endif /* ! CONFIG_DMA_STM32_V1 */

DMAv1 + DMAMUX ==> STM32_DMA_STREAM_OFFSET = 1 (this is the HW configuration on STM32G0)

However, neither dmamux_stm32.c nor dma_stm32_v1.c use STM32_DMA_STREAM_OFFSET.

dma_stm32.c does:

#ifdef CONFIG_DMAMUX_STM32
callback_arg = stream->mux_channel;
#else
callback_arg = id + STM32_DMA_STREAM_OFFSET;
#endif /* CONFIG_DMAMUX_STM32 */

Possibly, this #ifdef CONFIG_DMAMUX_STM32 is wrong.

@rjackson159
Copy link

Probably we need to review this whole offset handling and see why it can't be directly dealt from inside dma driver.

From

#if defined(CONFIG_DMA_STM32U5)
/* from DTS the dma stream id is in range 0..N-1 */
#define STM32_DMA_STREAM_OFFSET 0
#elif !defined(CONFIG_DMA_STM32_V1)
/* from DTS the dma stream id is in range 1..N */
/* so decrease to set range from 0 from now on */
#define STM32_DMA_STREAM_OFFSET 1
#elif defined(CONFIG_DMA_STM32_V1) && defined(CONFIG_DMAMUX_STM32)
/* typically on the stm32H7 series, DMA V1 with mux */
#define STM32_DMA_STREAM_OFFSET 1
#else
/* from DTS the dma stream id is in range 0..N-1 */
#define STM32_DMA_STREAM_OFFSET 0
#endif /* ! CONFIG_DMA_STM32_V1 */

DMAv1 + DMAMUX ==> STM32_DMA_STREAM_OFFSET = 1 (this is the HW configuration on STM32G0)

However, neither dmamux_stm32.c nor dma_stm32_v1.c use STM32_DMA_STREAM_OFFSET.

dma_stm32.c does:

#ifdef CONFIG_DMAMUX_STM32
callback_arg = stream->mux_channel;
#else
callback_arg = id + STM32_DMA_STREAM_OFFSET;
#endif /* CONFIG_DMAMUX_STM32 */

Possibly, this #ifdef CONFIG_DMAMUX_STM32 is wrong.

This seems to be the case, if the STM32_DMA_STREAM_OFFSET is applied to both cases:

 #ifdef CONFIG_DMAMUX_STM32 
 	callback_arg = stream->mux_channel + STM32_DMA_STREAM_OFFSET; 
 #else 
 	callback_arg = id + STM32_DMA_STREAM_OFFSET; 
 #endif /* CONFIG_DMAMUX_STM32 */ 

The end result appears to operate correctly

IRQ handler should take into account STM32_DMA_STREAM_OFFSET
also when DMAMUX is present.

Signed-off-by: Erwan Gouriou <erwan.gouriou@st.com>
@erwango erwango assigned mathieuchopstm and unassigned erwango Jul 25, 2025
@zephyrbot zephyrbot added the area: DMA Direct Memory Access label Jul 25, 2025
@zephyrbot zephyrbot requested a review from teburd July 25, 2025 08:56
@erwango
Copy link
Member Author

erwango commented Jul 25, 2025

The end result appears to operate correctly

Thanks for this feedback. I've just pushed an update.

I've also performed a satisfying (and logically harmless) clean up on U5 driver.

@djiatsaf-st could you perform a non regression on this whole PR to remain on the safe side ? I'm particularly worried about the potential impact of the dmamux related change as potentially this misalignment was compensated in other clients drivers to achieve functional state.

@erwango erwango added the block: HW Test Testing on hardware required before merging label Jul 25, 2025
Copy link
Contributor

@mathieuchopstm mathieuchopstm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM (though possibly now we need to remove the id + STM32_DMA_STREAM_OFFSET in the few drivers that have it...)

#ifdef CONFIG_DMAMUX_STM32
callback_arg = stream->mux_channel;
callback_arg = stream->mux_channel + STM32_DMA_STREAM_OFFSET;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should instead apply the offset here:

#ifdef CONFIG_DMAMUX_STM32
/* Each further stream->mux_channel is fixed here */
config->streams[i].mux_channel = i + config->offset;
#endif /* CONFIG_DMAMUX_STM32 */

#ifdef CONFIG_DMAMUX_STM32
 		/* Each further stream->mux_channel is fixed here */
- 		config->streams[i].mux_channel = i + config->offset;
+ 		config->streams[i].mux_channel = i + config->offset + STM32_DMA_STREAM_OFFSET;
#endif /* CONFIG_DMAMUX_STM32 */

@mathieuchopstm mathieuchopstm changed the title drivers: adc: stm32: Take into account "stream offset" in dma config drivers: dma: stm32: take "stream offset" into account when DMAV1 + DMAMUX is used Jul 25, 2025
STM32_DMA_STREAM_OFFSET is defined as 0 in case "dma u5" is in use.
Clean up code relating to this define.

Signed-off-by: Erwan Gouriou <erwan.gouriou@st.com>
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: ADC Analog-to-Digital Converter (ADC) area: DMA Direct Memory Access block: HW Test Testing on hardware required before merging platform: STM32 ST Micro STM32 size: XS A PR changing only a single line of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

STM32 ADC DMA Callback off by 1
4 participants