串口的printf函数重定向分为2种情况:使用MicroLIB库与不使用MicroLIB库,具体实现代码见下:
#if 1 /*****************不使用MicroLIB库需加上该部分****************************/ // 取消ARM的半主机工作模式 //#pragma import(__use_no_semihosting) // 确保没有从C库链接使用半主机的函数 // //struct __FILE // 标准库需要的支持函数 //{ // int handle; //}; //FILE __stdout; // FILE is typedef'd in stdio.h // //void _sys_exit(int x) // 定义_sys_exit()以避免使用半主机模式 //{ // x = x; //} /**********************************************************************/ int fputc(int ch, FILE *f) { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1,GPIO_PIN_SET); HAL_UART_Transmit(&huart2,(uint8_t*) &ch, 1, 1); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1,GPIO_PIN_RESET); return ch; } #endif参考资料:
https://blog.csdn.net/tao658/article/details/81204094
https://blog.csdn.net/qq_29344757/article/details/75363639