cancel
Showing results for 
Search instead for 
Did you mean: 

Another silly bug in the ''great'' HAL Library

pj
Associate II
Posted on January 13, 2018 at 18:32

Hi Guys.

I am the bare register programmer but yesterday I was asked to find the error in someone's code.

And the second function called :

SystemClock_Config()

The timeouts are counted using the SysTick counter updated in the interrupt but the problem is that the interrupt is enabled at the end of this function. For example if HSE fails the program stacks in the HSE enable attempt .

I

#rcc #stm32 #hal-bug
6 REPLIES 6
Pavel A.
Evangelist III
Posted on January 15, 2018 at 00:36

If HSE fails, a NMI should normally occur, it's priority is higher than systick. There you handle such things.

-- pa

pj
Associate II
Posted on January 15, 2018 at 02:26

Only if the CSS is activated. This bug is different.

        while(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)

        {

          if((HAL_GetTick() - tickstart ) > HSE_TIMEOUT_VALUE)

          {

            return HAL_TIMEOUT;

          }

        }

As you see here: it uses the HAL_GetTick() function. But at this point SysTick is not activated and its interrupt is disabled so the HAL_GetTick() function will return zero all the time and here we have the dead loop. It is an obvious mistake.

Posted on January 15, 2018 at 04:44

At lease in my F4Cube and L4Cube hal library, 

usually

HAL_Init(), called in the very beginning of main,  would initialize the SysTick timer and set its interrupt priority. Then comes the

SystemClockConfig().  You may check why your SysTick is not yet initialized at this moment. (as the whole HAL library always requires SysTick to check timeout.)
pj
Associate II
Posted on January 15, 2018 at 10:23

The function is called HAL_InitTick but it does not actually enable the tick  interrupt.

__weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)

{

  /*Configure the SysTick to have interrupt in 1ms time basis*/

  HAL_SYSTICK_Config(SystemCoreClock / 1000U);

  /*Configure the SysTick IRQ priority */

  HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0U);

   /* Return function status */

  return HAL_OK;
Posted on January 15, 2018 at 10:51

The Cortex core interrupts (IRQn < 0) don't have a NVIC enable bit, unlike the vendor-specific interrupt lines, NVIC_EnableIRQ() or

NVIC_DisableIRQ()

 therefore shall not be called with these. The SysTick_Config() /

HAL_SYSTICK_Config()

 enables the interrupt generation of the SysTick's counter overflow (

SysTick/

CTRL/TICKINT bit), which is the only thing necessary to get SysTick interrupts.
pj
Associate II
Posted on January 15, 2018 at 12:11

At least in the F3xx library it does not and the  HSE failure causes the dead loop. Tested & debugged.