cancel
Showing results for 
Search instead for 
Did you mean: 

Putting STM32F7xx_HAL_Driver and FreeRTOS in Library does not work

malcolm23
Associate III

I have an IAR example STM32F746 project which brings up an LwIP stack on top of FreeRTOS; however I'm trying to turn it into a library and link into around 10-15 applications. This is breaking badly because linking via libraries doesn't appear to obey the __weak attribute.

The STM32F7xx_HAL_Driver uses __weak to expose symbols it allows the user to override, e.g:

__weak void HAL_MspInit(void)

I have an override of this in my application code (the auto-generated stm32f7xx_hal_msp.c file). When compiled as an application, the linker selects my version over the __weak version. When compiled as a library and then linked into an empty application, the linker selects the __weak version.

The only workaround I have found is to comment out the __weak versions of the functions when they have been overridden and my program works again.

Do you have any better suggestions than commenting out chunks of the HAL_Driver library?

---- UPDATE ---

I found a different solution which I believe works better than the current __weak approach. The __weak actually has two meanings:

  1. A __weak symbol may have multiple definitions, and strong definitions override weak
  2. __weak symbols may optionally not be defined, and will have a null address if undefined.

The HAL_Driver library is attempting to use __weak in its first meaning where the user provided function overrides the default implementation. Instead the second approach works better. For example the declaration of HAL_MspInit should be:

__weak void HAL_MspInit(void);

Then its call in HAL_Init() should be replaced with:

/* Init the low level hardware */

if (HAL_MspInit)

HAL_MspInit();

Not only does this approach work for libraries, but this has the potential to be more efficient than the current approach in that the compiler has a chance of totally optimizing the call out for functions not implemented by the user.

There are something like 270 __weak functions in the STM32F7xx HAL_Driver library, and some of them actually do work if not overridden by the user (they need an else to the if in the example above). Food for thought!

1 REPLY 1
ADunc.1
Senior

I know this answer is much too late...

The usual cause of this in my experience, is due to the linker discarding the stm32xxxx_hal_msp.c file completely as there are no direct calls into that file. It thinks nothing calls those functions so they are unused. For example if you added a function into the stm32xxxx_hal_msp.c file and called it from another file, then the stm32xxxx_hal_msp.c file will not be discarded during linking and the weak functions will be overridden by the ones in stm32xxxx_hal_msp.c.

There are linker flags to prevent discarding unused sections, but they cause it to keep ALL unused sections.

This applies for sure with GCC but I don't know about IAR.