Viacheslav Kaloshin

STM32Code/HAL bug in ADC routines?

Discussion created by Viacheslav Kaloshin on Aug 11, 2017
Latest reply on Oct 4, 2017 by Tilen MAJERLE

Steps to reproduce

 

1. Open STM32CubeMX and config ADC to use multiple (not ONE) channels. Regular.

2. Try to continuously read value of only one channel. You should get something like this

 

<p>sConfig.Channel=Channel;
HAL_ADC_ConfigChannel(&hadc,&sConfig);
HAL_ADC_PollForConversion(&hadc,1); // only for regular
val = HAL_ADC_GetValue(&hadc);</p>

 

But you got in val garbage from all channels.

 

its caused by bug in STM/HAL .. In HAL_ADC_ConfigChannel

 

<p>if (sConfig->Rank != ADC_RANK_NONE)
{
hadc->Instance->CHSELR |= ADC_CHSELR_CHANNEL(sConfig->Channel);</p>

 

So you never can choice only one channel. Only ADD channels to list. But if you set channel rank to ADC_RANK_NONE, you will only REMOVE channels from list. 

 

<p>hadc->Instance->CHSELR &= ~ADC_CHSELR_CHANNEL(sConfig->Channel);</p>

 

3. Solution in simple - change '&=' and '|=' to '=' (and possible remove duplicate code) - now HAL_ADC_ConfigChannel will config one channel, not all existing at one

Outcomes