Showing results for 
Search instead for 
Did you mean: 

Generate and use an STM32 Hal library, can't override __io_putchar

Associate III

Hi everyone,

I am currently trying to create a library from STM32 Hal driver (Name of this project is Nucleol073rzHalTest).
The aim for this library is to contain all pin configuration to be used in other projetcs.
For this I simply create a stm32 executable project with the desire board/micro and configure pin.
I then convert this project to library (by changing the type of project from "Executable" to "Static Library' and the output file extension from "elf" to "a"),
and call the function "main_app" in the main in order I call define it in other projects.
(I keep the .IOS file in order to modify at demand the pin configuration, but delete .ld and .s files which will depend on executable project)

I then create another stm32 project (or simple C/C++ project) executable project which will use this library (Name of this project is SandboxHal or SandBoxHalTest for the simple C/C++ project).
I add the path to search the library, and necessary header files from the library.
I delete the ioc file, source and driver files in the stm32 project because they are already compiled in the previous library.
I create my own c++ file and define in it the "main_app" function which simply toggle a led and print messages.

It works well (after some adjustement) and I can control the led on the demo board.
However when it comes to overwrite the function __io_putchar in order to get the uart output redirect to the terminal, I didn't suceed.
It works well in a standard project where all hal drivers are already in it, but not for my projects which are linked to the previous library.

In debug, the code doesn't stop into the function __io_putchar if I add breakpoint.
And __io_putchar is added in the section "Discarded input sections" in the map file after compilation for both the STM32 Executable project and the simple C/C++ Executable project.
I don't know why.

I can't succeed to get this function works in this situation, do you have suggestion about it ?

STM32CubeIDE version : Version: 1.13.2
Board used: Nucleo-L073rz

Here the code, and project view:

capture code test use hal library.png


Hi Tesla Guru,
Little update about the post:

As you suggest, the huart2 instance was not correctly initialized.
The wrong HAL_UART_MspInit  is used.

After some further investigation I found out why:
As we can see previously, the fonction _lseek, l_read, l_write and l_close where not included in the code whereas syscall.c is normaly compile in the library and all these function are defined in it.

Actually, it is the same for the function HAL_UART_MspInit which is defined in file Core\Src\stm32l0xx_hal_msp.c but was not included in code so the function used is currently defined via the file Drivers\STM32L0xx_HAL_Driver\Src\stm32l0xx_hal_uart.c, but the definition in it is _weak and does nothing.

I will continue to investigate in order to solve the problem

ST Employee

Hello @YohannDrc ,


You can refer to the UM2609 user manual for more information on redirecting printf() to UART output.
Also, make sure that you have configured the usart2 pins in CubeMX correctly according to the UM1724. In the same way, ensure that the terminal emulator setup is connected to the board with the correct COM, same baud rate as in the pin configuration, data length = 7 bits, one stop bit, parity none and flow control disabled.

Hope this help!




Associate III

I finally found out a solution.
In order the project use the good HAL_UART_MspInit.
I had to generate peripheral initialization as a pair of '.c/.h' files for each peripheral.
It can simply be done by checking a box in the .ioc file of the library.
Prject Manager > Code Generator > Check the box "Generate peripheral initialization as a pair of '.c/.h' files per peripheral"