cancel
Showing results for 
Search instead for 
Did you mean: 

what should DMA_MemoryBaseAddr be pointing to?

pandoraems
Associate II
Posted on September 04, 2008 at 19:50

what should DMA_MemoryBaseAddr be pointing to?

5 REPLIES 5
pandoraems
Associate II
Posted on May 17, 2011 at 12:43

following is a snip from STm's example of using DMA with ADC:

 

u16 ADC_ConvertedValue[512];

 

...

 

...

 

DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;

 

I am wondering about why it needs the ampersand in front of ADC_ConvertedValue ? I would think that DMA_MemoryBaseAddr should contain the address of the first element of the array , which what ADC_ConvertedValue is. And not the address to a pointer to the first member of ADC_ConvertedValue[] (which seems like what the example code does and is quite unintuitive)

am i confused? or is the example incorrect?

Thanks

lanchon
Associate II
Posted on May 17, 2011 at 12:43

well, *ADC_ConvertedValue is the first u16, and I guess ADC_ConvertedValue is a const pointer to u16 and since it's const it needn't be allocated in memory at all. I think the result of taking the address of such a const should at lest be undefined and might be illegal (maybe only in C++). so I'd say it's a bug. the horrible structure of the lib which require casts all over may be hiding the error until runtime.

andreas2
Associate II
Posted on May 17, 2011 at 12:43

In ANSI C, &ADC_ConvertedValue is valid and is a pointer of type pointer-to-512-element-array-of-u16, as opposed to using ADC_ConvertedValue without the ampersand, which decays into a pointer of type pointer-to-u16. Both point to the same location, the first element of the array. In this example, the type of the pointer is irrelevant since it is immediately cast to u32 anyway.

pandoraems
Associate II
Posted on May 17, 2011 at 12:43

Thanks for reply

I've tried both and surprisingly both seemed to work! (i.e with and without &),

which boggles my mind.

lanchon
Associate II
Posted on May 17, 2011 at 12:43

> In ANSI C, &ADC_ConvertedValue is valid and is a pointer of type pointer-to-512-element-array-of-u16

thanks. so I guess a pointer-to-512-element-array-of-u16 autoconverts to pointer-to-u16, but size of *pointer-to-512-element-array-of-u16 covers all the array.