stm32 串口的printf函数重定向

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/lee_xuwei/article/details/83380676

 串口的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

猜你喜欢

转载自blog.csdn.net/lee_xuwei/article/details/83380676