msdk icon indicating copy to clipboard operation
msdk copied to clipboard

Relationship between MXC_DMA_AcquireChannel() and DMAn_IRQHandler() is confusing

Open JordanAceto opened this issue 1 month ago • 0 comments

The code

int my_ch = MXC_DMA_AcquireChannel();

gets us the index of the first available DMA channel, this makes sense.

We can also enable the relevant IRQ in the NVIC by

NVIC_EnableIRQ(MXC_DMA_CH_GET_IRQ(my_ch));

But then we don't know which specific DMAn_IRQHandler() function my_ch maps to. It depends on how many times MXC_DMA_AcquireChannel() has been previously called.

Most of the example code always used DMA0_IRQHandler(), and this works because the example only does a single call to MXC_DMA_AcquireChannel(), so the channel is also zero and everything matches up.

For example see the code below from the DMA example for MAX32650. I think this code is confusing because it assumes MXC_DMA_AcquireChannel() will return the integer zero which matches up with NVIC_EnableIRQ(DMA0_IRQn). It works for the demo (if a little confusing/misleading to users) because there is only one call to MXC_DMA_AcquireChannel(). If we also called MXC_DMA_AcquireChannel() before these lines of code to set up a different DMA tranaction, it wouldn't work any more. https://github.com/analogdevicesinc/msdk/blob/8c461c32abbdf597857e317e889a998e10ca736d/Examples/MAX32650/DMA/main.c#L125-L128

So my confusion is: If you want to enable any arbitrary amount of channels beyond one, what is the recommended way of doing it? My first thought is to just override all 16 of the weak DMAn_IRQHandler() functions like

void DMA0_IRQHandler(void)
{
    MXC_DMA_Handler();
}

void DMA1_IRQHandler(void)
{
    MXC_DMA_Handler();
}
...
void DMA15_IRQHandler(void)
{
    MXC_DMA_Handler();
}

This would ensure that whichever index returned by MXC_DMA_AcquireChannel() is served by the DMA handler, but it feels weird. I also might not want a DMA channel to be served by another channels IRQ, I might want to wait until the correct IRQ happens. Due to the way MXC_DMA_Handler() works, it will execute the callback of any channel with a pending interrupt, no matter which DMAn_IRQHandler() called it.

I guess one approach would be to not use MXC_DMA_AcquireChannel() at all, and manually manage which DMAn_IRQHandler() goes with what. But you folks made the API, so it seems like you want us to use it. I am just not sure of the intended way to use this API when you have a more complex DMA arrangement.

Basically, I want to make sure that a specific DMA channel I chose goes with a specific DMAn_IRQHandler(), and I don't know how to easily do that with the API.

Any advice is welcome, thanks.

JordanAceto avatar Nov 07 '25 18:11 JordanAceto