此文章方法可以一并结局重定向问题[[解决“_close is not implemented and will always fail
”等编译警告]]。
重写__io_getchar函数和_read函数,即可重定向scanf输入:
int __io_getchar(void)
{
uint8_t ch = 0;
// Clear the Overrun flag just before receiving the first character
__HAL_UART_CLEAR_OREFLAG(&huart1);
HAL_UART_Receive(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
__attribute__((weak)) int _read(int file, char *ptr, int len)
{
(void)file;
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
*ptr++ = __io_getchar();
}
return len;
}