Electronic – STM32F4 – counting DMA wraps

cdmastm32f4

I am using DMA on an STM32F4 Discovery board to push values from an array into a peripheral. This needs to be done repeatedly, so I am using the circular DMA mode:

DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;

which gets the DMA source address to jump back to the start of the array automatically.

I would like to count how many times this wrap has happened. Is there an interrupt or other feature built in to do this?

Best Answer

STM32 DMA has some interrupts. You can try to use the DMA_IT_TC flag. I have not tested it with mode from array to periph, but it worked with periph to array (example from my code):

DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE);

You should have also to set up NVIC interrupt (example):

ADCNVICConfig.NVIC_IRQChannel = DMA2_Stream0_IRQn;
ADCNVICConfig.NVIC_IRQChannelPreemptionPriority = 0;
ADCNVICConfig.NVIC_IRQChannelSubPriority = 1;
ADCNVICConfig.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&ADCNVICConfig);

TC flag means "transfer completed".

Example interrupt:

void DMA2_Stream0_IRQHandler(void)
{

    //Something

    if(DMA_GetITStatus(DMA2_Stream0, DMA_IT_TCIF0) != RESET)
    {
        DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TCIF0);
        //Something
    }

    //Something

}