一文了解串口打印

以前的文章《STM32 串口详解》介绍了串口驱动,串口在嵌入式领域不只是一个通信接口,仍是一种调试工具,其好用程度不亚于硬件仿真。有些环境不方便链接Jlink进行硬件仿真,或者并非必现的问题,咱们须要定位出现问题的地方,能够选择保存log的方式,可是须要后续读取,且受到Flash大小的限制,若是能够放置一台计算机到现场,使用串口打印无疑是最好的办法,在C语言中 printf函数输出各类类型的数据,使用格式控制输出各类长度的字符,甚至输出各类各样的图案,须要将串口重定向到printf函数。git

0一、硬件打印

在STM32的应用中,咱们经常对printf进行重定向的方式来把打印信息printf到咱们的串口助手。在MDK环境中,咱们经常使用MicroLIB+fputc的方式实现串口打印功能,即:串口重映射github

代码中记得添加一下头文件app

#include < stdio.h >函数

兼容不一样IDE的putchar重映射。工具

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

固然也须要配置下串口,不须要配置中断。oop

void UART_Init(void)
{
  USART_InitTypeDef USART_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
 
  /* Enable GPIO clock */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  /* Enable UART1 clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  /* Connect PXx to USARTx_Tx*/
  GPIO_PinAFConfig(GPIOA, 9, GPIO_AF_USART1);
  
  /* Connect PXx to USARTx_Rx*/
  GPIO_PinAFConfig(GPIOA, 10, GPIO_AF_USART1);
  
  /* Configure USART Tx as alternate function  */
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  /* Configure USART Rx as alternate function  */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  
  /* USART configuration */
  USART_Init(USART1, &USART_InitStructure);
  
  /* Enable USART */
  USART_Cmd(USART1, ENABLE);
}

打印函数ui

PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (uint8_t) ch);
 
  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {}
 
  return ch;
}

不一样的IDE也要对应的的配置。spa

Keil配置,须要勾选MicroLIB选项。调试

 

 

 IAR配置code

 

 

 打印效果

 

 

 代码和工程已经开源

代码和Keil IAR工程开源地址:

https://github.com/strongercjd/STM32F207VCT6

 

0二、IDE打印

有些时候,咱们须要只是在办公室调试,芯片没有额外的串口提供咱们调试,使用IDE打印也不失为一条好办法。

2.一、IAR

代码以下

  printf("\r\n======================================================================");
  printf("\r\n=               (C) COPYRIGHT 2020                                   =");
  printf("\r\n=                                                                    =");
  printf("\r\n=                ST207 USART_Printf                                  =");
  printf("\r\n=                                                                    =");
  printf("\r\n=                                           By Firefly               =");
  printf("\r\n======================================================================");
  printf("\r\n\r\n");

IAR打印效果

 

 配置方法,打开TerminalIO:进入调试->view->Terminal I/O

 

 

2.二、Keil

目前我尚未找到解决办法。网上能够找到勾选Use Simulator,但这是模拟调试的,并非硬件调试,不符合个人要求。

欢迎你们分享本身的办法,在评论区告诉你们。

代码和IAR工程开源地址:

https://github.com/strongercjd/STM32F207VCT6

 

点击查看本文所在的专辑,STM32F207教程

相关文章
相关标签/搜索