Loading...
 

Zephyr project on STM32

   Zephyr Workbench, a VSCode extension to manage Zephyr on STM32.
It enables users to easily create, develop, and debug Zephyr applications.
Main features:
  • Install host dependencies.
  • Import toolchain and SDK.
  • Create, configure, build and manage apps.
  • Debug STM32.
You can directly download it from the VSCode marketplace
For more details, visit the Zephyr Workbench

System Workbench for STM32


Retargeting printf

I’m trying to retarget the printf function in order to print formatted output to USART3 on my STM32 NUCLEO F746ZG.

This is the code in the uart.h file I created:

1 #ifdef GNUC
2 /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf set to ‘Yes’) calls __io_putchar() */
3 #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
4 #else
5 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
6 #endif /* GNUC */

(Please note that the lines 1 and 6 have a couple of underscores before and after the GNUC keyword, but I didn’t succeed in innserting it in the post...)

This is the code in the corresponding uart.c file:

1 PUTCHAR_PROTOTYPE {
2 HAL_UART_Transmit (&huart3, (uint8_t *)&ch, 1, 0xFFFF);
3 return ch;
4 }

Nothing happens if I run the application........

In particular:
If I directly call the HAL_UART_Transmit function, I can send characters.
If I call the printf function, the PUTCHAR_PROTOTYPE function is not executed.

I guess the printf is calling something else than the PUTCHAR_PROTOTYPE function.
By the way, I didn’t see the “small printf” option in the System Workbench linker options.

What’s wrong with this code?

Thanks in advance

Hi flavio.renga,

For using printf, you need also to have the syscalls.c file in the src directory of your project.

Souleymane
AC6

Thank you very much Souleymane!
Now it works!

A simple curiosity: why CubeMX doesn’t add this file automatically?!

Cheers

Flavio

What about retargeting getchar() function?

My simple goal is, for the moment, to wait until the ‘p’ key is pressed on the PC console.
I’m having problem with it...

Infact, I’ve tried to retarget the getchar() function in the same way I’ve done for the printf() function, but without success.

If I use directly the HAL_UART_Receive() function, all is right.
If I try redefining the io_getchar(void), function, I get problems.


---------------

Hereafter the working code:

> // Wait for ‘p’ char pressed
> HAL_UART_Receive (&huart3, (uint8_t *)&c, 1, 0xFFFF);
> while (c != ‘p’) {
> HAL_UART_Receive (&huart3, (uint8_t *)&c, 1, 0xFFFF);
> }

---------------

Instead, if I use the following code, the program stucks into the GETCHAR_PROTOTYPE routine:

> #ifdef GNUC
> /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
> set to ‘Yes’) calls __io_putchar() */
> #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
> #define GETCHAR_PROTOTYPE int __io_getchar(void)
> #else
> #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
> #define GETCHAR_PROTOTYPE int fgetc(FILE *f)
> #endif /* GNUC */

...

> GETCHAR_PROTOTYPE {
> int ch;
> HAL_UART_Receive (&huart3, (uint8_t *)&ch, 1, 0xFFFF);
> return ch;
> }

...

> // Wait for ‘p’ char pressed
> c = getchar();
> while (c != ‘p’) {
> c = getchar();
> }


In particular, the char is correctly captured by HAL_UART_Receive (I can see the char by using the breackpoints), but the GETCHAR_PROTOTYPE repeats infinitely without exiting and the while loop is never reached.

What is wrong with the code?

Thanks in advance!

Flavio

Thank you! With these instructions I was able to make printf work.

This thread is almost identical:
https://community.st.com/thread/10610Question
From there I was able to get a hint of where to get the file. I copied syscalls.c file directly from an example project for F4 that can be found in:
STM32Cube_FW_F4_V1.8.0\Projects\STM32F401RENucleo\Examples\UART\UART_Printf\SW4STM32


 

Newest Forum Posts

  1. Монтаж камина с грилем в Москве - установка и барбекю by KpddomErorb, 2025-05-10 18:28
  2. SPI on Nucleo_STMH533RE by royjamil, 2025-05-04 20:13
  3. SPI on Nucleo_STMH533RE by higginsa1, 2025-03-25 07:37
  4. SPI on Nucleo_STMH533RE by royjamil, 2025-03-23 11:31
  5. SPI on Nucleo_STMH533RE by higginsa1, 2025-03-23 09:33
  6. Configuring DMA for ADC in SW? by sam.hodgson, 2025-03-04 12:58
  7. Build a project in "release" mode by info@creosrl.it, 2025-02-20 18:12
  8. Build a project in "release" mode by info@creosrl.it, 2025-02-20 17:05
  9. Build a project in "release" mode by tang, 2025-02-20 10:36
  10. Build a project in "release" mode by info@creosrl.it, 2025-02-19 17:35

Last-Modified Blogs