cancel
Showing results for 
Search instead for 
Did you mean: 

STM3F2 DMA Circular Mode Overwrites Data

ckern
Associate II
Posted on March 22, 2015 at 23:55

Hello,

I have a global structure that contains an array of bytes of 1024 elements. I have declared this array towards the beginning of the structure. 

I have set up DMA to import USART3 data into this array. I have set up the DMA in circular mode. The data is placed into the array and loops back around as expected. Unfortunately, it also seems to continue on in the memory address space as I can see (in debug mode) other variables of the structure being overwritten. I am not sure how the DMA writes to two different addresses at the same time, but that's what appears to be happening. Ideas?

void DMA_Config_Rx(uint32_t BaseAddr)

{

    DMA_InitTypeDef  DMA_InitStructure;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); 

    DMA_DeInit(DMA1_Stream1);

    DMA_StructInit(&DMA_InitStructure);

    DMA_InitStructure.DMA_Channel = DMA_Channel_4; 

    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; // Receive

    DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)BaseAddr;

    DMA_InitStructure.DMA_BufferSize = (uint16_t)1024;

    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART3->DR);

    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;

    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;

    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;

    DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;

    DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;

    DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;

    DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

    DMA_Init(DMA1_Stream1, &DMA_InitStructure); 

    /* Enable the USART Rx DMA request */

    USART_DMACmd(USART3, USART_DMAReq_Rx, ENABLE);

    /* Enable the DMA RX Stream */

    DMA_Cmd(DMA1_Stream1, ENABLE);

}

I pass the address of the array to this function: DMA_Config_Rx(&My_Struct.RX_Data_Buffer);

As I stated, in debug mode, I can see the RX_Data_Buffer data being written over and over as expected per the circular mode, but I also see other variables in the structure changing. Please advise. Thanks. 
1 REPLY 1
Posted on March 23, 2015 at 02:18

Yeah, something else going on here. I guess you'd want to look at the stack/heap allocations. Where the structure is located, and how the structure is defined. For example is the buffer within the structure, or is it a pointer to some place else?

Insufficient context, but sounds like something else is at play.

Place some signature data in guard locations at the front/rear of the buffer, confirm if those are walked over. Send a consistent single byte pattern, do you see that, or something else, outside the buffer's bounds.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..