分享

stm32串口重定向printf

 Reset2000 2023-10-05 发布于江苏

在STM32CubeIDE中实现printf函数重定向,在配置完串口后,可见如下图:    

打开syscalls.c,通过代码搜索,方框中的两个函数没有实现,也就是我们需要实现的函数。也可以重写_write函数,实现printf。同理,实现scanf,写__io__getchar()

  

  

  

此处在usart.c中最后实现__io__putchar(int ch)函数。

在main.c中需要#include"stdio.h",然后可以使用printf了  


中断模式下,如何判断是哪个串口引起的中断?

以发送完成函数为例。

参数为UART_HandleTypeDef* 指针,通过其指向的实例instance判断具体是哪个串口引起的中断,从而针对写处理方法。

__weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)

{

/* Prevent unused argument(s) compilation warning */

UNUSED(huart);

/* NOTE : This function should not be modified, when the callback is needed,

the HAL_UART_TxCpltCallback can be implemented in the user file.

*/

if(huart->Instance == USART1){

//serail 1

}

else if(huart->Instance == USART2){

//serial 2

}

else if(huart->Instance == USART3){

//serial 3

}

}

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多