cancel
Showing results for 
Search instead for 
Did you mean: 

HAL ADC DMA & UART variable casting question

NicRoberts
Senior II

NUCLEO-F411RE

I have ADC1 to DMA in a circular buffer 4 samples long (ADC_VAL). I then use a TIM1 interrupt to send the contents via UART to a PC.

int16_t ADC_VAL[4];
...
HAL_ADC_STart_DMA(&hadc1, (uint32_t *)ADC_VAL, 4);


/* TIM1 callback */
void HAL_TIM_periodElapsedCallback(TIM_HandleTypeDef *htim)
{
  HAL_UART_Transmit(&huart2, (uint8_t *)ADC_VAL, 16, 10);
}

 

Should I have declared ADC_VAL as a uint32_t ?

 

1 ACCEPTED SOLUTION

Accepted Solutions

> Should I have declared ADC_VAL as a uint32_t ?

In general, no. ADC only needs a uint16_t to store content. Ensure your DMA is set up to half-word size.

> int16_t ADC_VAL[4];

This is an array of 4 uint16_t values, which is a total of 8 bytes, not 16.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

3 REPLIES 3
Saket_Om
ST Employee

Hello @NicRoberts 

Please refer to the example below which use __IO uint16_t variable when calling HAL_ADC_Start_DMA().

STM32CubeF4/Projects/STM32F4-Discovery/Examples/ADC/ADC_RegularConversion_DMA at master · STMicroelectronics/STM32CubeF4

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om

Thanks for the reply, maybe I wasn't being clear enough in the OP.

In the example you linked to (and similarly in my own code) we have

HAL_ADC_Start_DMA(&AdcHandle, (uint32_t*)&uhADCxConvertedValue, 1)

 

where uhADCxConvertedValue  is first declared as 16 bit then cast as 32 bit for the DMA. What is the size of uhADCxConvertedValue in memory?

Is it 16 bits of actual values from the ADC padded with 16 bits of 0s?
or

is it 16 bits? 

I ask as I need to know when transmitting the bytes via UART i.e. If its 16 then I just need 2 bytes...

Does that make sense?

> Should I have declared ADC_VAL as a uint32_t ?

In general, no. ADC only needs a uint16_t to store content. Ensure your DMA is set up to half-word size.

> int16_t ADC_VAL[4];

This is an array of 4 uint16_t values, which is a total of 8 bytes, not 16.

If you feel a post has answered your question, please click "Accept as Solution".