2023-07-08 10:40 AM - edited 2023-07-08 10:41 AM
I'm using STM32F7xx for my project where I use VS Code text editor in combination with CMake project build. I also use CubeMX for code generation. When I generate code using STM32CubeIDE template, the 'syscalls.c' file is generated. It provides the following declaration:
extern int __io_putchar(int ch) __attribute__((weak));
If I provide '__io_putchar()' definition in my 'main.cpp' file nothing happens after using 'printf()'. However, if I provide '__io_putchar()' function definition inside of 'syscalls.c' then 'printf()' operates as should and I get output stream printed out on terminal.
extern int __io_putchar(int ch) __attribute__((weak));
int __io_putchar(int ch)
{
HAL_UART_Transmit(UART_GetHuart3(), (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
I have tried several other methods of 'printf()' retarget, like retargeting '_write' or 'fputc', however nothing really works besides providing function definition inside of 'syscalls.c'. And the problem of providing '__io_putchar()' function definition inside of 'syscalls.c' is that after re-generating code from CubeMX, this definition will be removed. Therefore I need more "clean" solution for this.
Solved! Go to Solution.
2023-07-08 11:02 AM
2023-07-08 10:50 AM
Show how you implement it in main.cpp. I mean you omit extern C
2023-07-08 10:54 AM
Like this:
int __io_putchar(int ch);
int __io_putchar(int ch)
{
HAL_UART_Transmit(UART_GetHuart3(), (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
With extern C, you are referring to C and C++ header guards?
2023-07-08 11:02 AM