cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407G Discovery board HAL ADC DMA - Multi Channel continuous conversion mode with dma confuses

MKara.13
Associate
uint32_t dma_buffer_for_adc;
uint32_t adc_value_1;
 
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
adc_value_1 = dma_buffer_for_adc ;// 
}
 
HAL_ADC_Start_DMA (&hadc1, &dma_buffer_for_adc, 1);

here I have implemented a single channel continuous conversion mode.I wrote the address of the dma buffer and the code works great. adc doesn't work if I don't specify it here as a pointer.

uint32_t dma_buffer_for_adc[2]; //We have created an array with two elements, this array has 2 elements, element 0 and element 1.
uint32_t adc_value_1;
uint32_t adc_value_2;
 
//-----------------------------------------------------------------------------------------------------------------------------------
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
adc_value_1 = dma_buffer_for_adc[0];  //We have defined the buffer where the / adc value is kept to a variable
adc_value_2 = dma_buffer_for_adc[1];
}
//-----------------------------------------------------------------------------------------------------------------------------------
HAL_ADC_Start_DMA (&hadc1, dma_buffer_for_adc, 2);

here I have implemented a multichannel channel continuous conversion mode.I wrote the address of the dma buffer and the code works great.

Here is the thing that confuses me. Why do we need to specify addresses when doing a single channel conversion and not if we use multiple channels?

 0693W00000JOsyiQAD.jpg

1 ACCEPTED SOLUTION

Accepted Solutions
Muhammed Güler
Senior III

dma can only work with array or pointer. pointer and array are very close concepts. You can access pointer elements like arrays and array elements like pointers.The main difference between pointer and array is that one is stored in the heap and the other in the stack. In your first example, you are telling the compiler to treat your variable as an array with the & symbol. Ignoring the [] symbols, you don't need to do anything special since you are already using arrays in your 2nd example. I guess you are thinking of pointer and array as completely different concepts.

the following two notations produce the same result

char MyArray[2];
*(MyArray) = 'a';
*(MyArray+1) = 'b';
 
 
char *MyPointer;
char a = MyPointer[0];

View solution in original post

2 REPLIES 2
TDK
Guru

> uint32_t dma_buffer_for_adc[1]; // We have created an array with two elements, this array has 2 elements, element 0 and element 1.

This array has one element, not two.

> adc_value_2 = dma_buffer_for_adc[1];

This access is out of bounds, and will likely take the value of adc_value_1 if they're allocated consecutively.

> Here is the thing that confuses me. Why do we need to specify addresses when doing a single channel conversion and not if we use multiple channels?

Not sure what you mean. You need to specify an address (i.e. a pointer to data) in both cases if you're using HAL_ADC_Start_DMA.

> HAL_ADC_Start_DMA (&hadc1, dma_buffer_for_adc[], 2);

This isn't valid syntax and throws an error in GCC.

main.cpp: In function 'int main(int, char**)':
main.cpp:78:49: error: expected primary-expression before ']' token
HAL_ADC_Start_DMA(nullptr, dma_buffer_for_adc[], 2);
                                              ^

If you lose the "[]", then dma_buffer_for_adc is a pointer of type (uint32_t *).

If you feel a post has answered your question, please click "Accept as Solution".
Muhammed Güler
Senior III

dma can only work with array or pointer. pointer and array are very close concepts. You can access pointer elements like arrays and array elements like pointers.The main difference between pointer and array is that one is stored in the heap and the other in the stack. In your first example, you are telling the compiler to treat your variable as an array with the & symbol. Ignoring the [] symbols, you don't need to do anything special since you are already using arrays in your 2nd example. I guess you are thinking of pointer and array as completely different concepts.

the following two notations produce the same result

char MyArray[2];
*(MyArray) = 'a';
*(MyArray+1) = 'b';
 
 
char *MyPointer;
char a = MyPointer[0];