2023-11-21 06:51 AM
I use STM32H750B-DK board. It has UART3 connected to the ST-LINK USB.
I tried
uint8_t Test[] = "Hello World !!!\r\n";
HAL_UART_Transmit(&huart3,Test,sizeof(Test),10);
printf("Hello World 1 !!!\r\n");
HAL_UART_Transmit output has passed to the terminal.
printf doesn't print anything.
syscalls.c defines
extern int __io_putchar(int ch) __attribute__((weak));
The implementation is hidden and it definitely doesn't deal with the UART3.
The actual function used in the printf implementation is:
__attribute__((weak)) int _write(int file, char *ptr, int len)
{
(void)file;
int DataIdx;
// UART3_TX((uint8_t *)ptr, len, 1000);
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
__io_putchar(*ptr++);
}
return len;
}
I redefined the weak _write() in the main.c
int _write(int file, char *data, int len)
{
if ((file != STDOUT_FILENO) && (file != STDERR_FILENO))
{
errno = EBADF;
return -1;
}
// arbitrary timeout 1000
HAL_StatusTypeDef status =
HAL_UART_Transmit(&huart3, (uint8_t*)data, len, 1000);
// return # of bytes written - as best we can tell
return (status == HAL_OK ? len : 0);
}
Surprise!!!
Breakpoint in main.c defined _write() has not hit. The weak _write() of syscalls.c continues working.
I tried a dirty solution.
Defined in the main.c following function:
HAL_StatusTypeDef UART3_TX(uint8_t *data, uint16_t len, uint32_t Timeout)
{
return HAL_UART_Transmit(&huart3, data, len, Timeout);
}
I tried to call the function In the syscalls's version of the _write. The linker doesn't see UART3_TX().
What is going on? How can I make my debug output working?
Why it became unexpectedly complicated?
2023-11-21 07:01 AM
The linker is pretty methodical, you're going to need to look at what it is being fed as input, and what the .MAP describes in the resultant output.
Kind of suggests that syscalls.c isn't being pulled in.
use strlen() not sizeof() for strings, with trailing NULs
2023-11-21 07:28 AM - edited 2023-11-21 08:02 AM
Thank you.
This response doesn't help me getting printf working.
2023-11-21 08:59 AM - edited 2023-11-21 09:43 AM
I found the mistake. The _write() in the main.c does redefine _write() in the syscalls and the printf works. No help is required anymore.