使用c库中通用的printf,调用流程为先将需要打印的东西按照所需的格式放入到char型的buffer中,然后再将buffer中的内容发送至stdout中.
如果我们更换了串口,可能需要对相应的串口做重定向工作.如stm32
struct __FILE
{
int handle;
};
FILE __stdout;
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);//更具实际情况更改驱动
return (ch);
}
如xmc4700
#if defined(GNUC)
int _write(int fd, const void *buf, size_t count)
{
(void)fd;
for (size_t i = 0; i < count; ++i)
{
XMC_UART_CH_Transmit(SERIAL_UART, *(const uint8_t *)buf);
buf++;
}
return count;
}
int _read(int fd, void *buf, size_t count)
{
int char_cnt = 0;
(void)fd;
for (size_t i = 0; i < count; ++i)
{
if (ring_buffer_get(&serial_buffer, (uint8_t *)buf) != 0)
{
break;
}
char_cnt++;
/* Stop reading if CR (Ox0D) character is received */
if (*(uint8_t *)buf == 0x0DU)
{
/* New line character (CR) received ? */
*(uint8_t*)buf = '\n'; /* Yes, convert LF to '\n' char. */
break; /* Stop loop and return received char(s) */
}
buf++;
}
“retarget_io.c” [Modified] 131 lines --43%–