# STM32F4 - counting DMA wraps

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?

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;


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

}

• Oh, that's cool - the "transfer complete" still works, even though the transfer keeps going? – Greg d'Eon Feb 26 '15 at 15:41
• It works each completed transfer. But it's important to clear flag of interrupt, as in any other interrupt. See edited post. – Sławomir Kozok Feb 26 '15 at 15:44
• As per usual in STM32 interrupts. Neat - very good to know! – Greg d'Eon Feb 26 '15 at 15:50

if you don't want to send the same array every time. You should wait for the both callbacks, the TxHalfCpltCallback and the TxCpltCallback. When the first half is transmited TxHalfCpltCallback is called, you should write this half of the buffer with new data, then when the TxCpltCallback is called write the second half of the buffer, and keep doing this until needed.