Forum: ARM programming with GCC/GNU tools va_args, printf and DMA memory problems

von Kevin n. (Company: na) (fallentemplar)

Rate this post
0 useful
not useful
I've been scratching my head for the past few days as I try to 
understand why and what is going on... So here it goes:

Current config: stm32f407VG (same as stm32f4 discovery)

I set up a project using the default linker and startup files from 

Init the ADC & DMA's
       ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult;
       ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
       ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;

        DMA_InitTypeDef DMA_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;

        DMA_InitStructure.DMA_Channel = DMA_Channel_0;
        DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) &RawADCData.adcRaw1;
        DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) ADC_CCR_ADDRESS;
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
        DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
        DMA_InitStructure.DMA_Priority = DMA_Priority_High;
        DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
        DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
        DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
        DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
        DMA_Init(DMA2_Stream0, &DMA_InitStructure);

        /*Interrupt when half of DMA is done, so we do not overwrite our values when processing*/
        DMA_ITConfig(DMA2_Stream0, DMA_IT_HT | DMA_IT_TC, ENABLE);
        DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TEIF0 | DMA_IT_DMEIF0 | DMA_IT_FEIF0 | DMA_IT_TCIF0 | DMA_IT_HTIF0);

        // Enable the DMA2_Stream0 global Interrupt
        NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

        /* DMA2_Stream0 enable */
        DMA_Cmd(DMA2_Stream0, ENABLE);

--Symptom A:
Compile on GCC with CooCox as IDE, the DMA values are all garbled, after 
looking at the memory, it seems like the DMA values were sometimes 
offset by 1 word.

--Symptom B: Floats do not print correctly, printf works, but they are 
not passed on VA_ARGS correctly, offset by x bytes, x is pretty random.

--Sympton C: I add a struct into my file somewhere, say
struct b 
 float z[40];

Everything begins to work, prints are correct, DMA is correct.

I add anther struct into anther file
struct b 
 float z[80];
 int z[3];

DMA is correct, but printf does not work again. If i twitter around with 
the structs, DMA can be broken, but printf works, or they all can be 

I have a feeling that it is a linker error or alignment error, but do 
not know where else to look.

The entire codebase is here 
if you are curious, built with Coocox IDE with the GNU ARM toolchain.

von Jim M. (turboj)

Rate this post
0 useful
not useful
Try to provide a simple test case. The code in your post is not enough 
to demonstrate the problem, the code you linked to is waaay too complex 
to expect an answer here.

von Kevin n. (Company: na) (fallentemplar)

Rate this post
0 useful
not useful

I revisited test cases based on your comment.

Here is a really simple case:

int main()
  printf("%f", 0.3423f);

This prints correctly.
 float[255] test;
} test;

int main()
  printf("%f", 0.3423f);

This doesnt work, the va_args are shifted.

von co (Guest)

Rate this post
0 useful
not useful
please set the Code Optimization to Level "None -O0" and please reports 
what happend.

von coocox user (Guest)

Rate this post
0 useful
not useful
rich stack + heap


Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]

Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.