Learn about serial port printing in one article

table of Contents

01, hardware printing

02, IDE printing

2.1 AR IAR

2.2、Keil


The previous article " STM32 Serial Port Detailed Explanation " introduced the serial port driver. The serial port in the embedded field is not only a communication interface, but also a debugging tool, and its ease of use is no less than hardware emulation. In some environments, it is inconvenient to connect Jlink for hardware simulation, or it is not a necessary problem. We need to locate the problem and choose the way to save the log, but it needs to be read later and is limited by the size of the Flash. If you can place one It is undoubtedly the best way to use the serial port to print from a computer to the site. In C language, the printf function outputs various types of data, uses format control to output characters of various lengths, and even outputs various patterns. It is necessary to connect the serial port Redirect to the printf function.

01, hardware printing

In STM32 applications, we often redirect printf to print the information to our serial port assistant. In the MDK environment, we often use MicroLIB+fputc to realize the serial port printing function, namely: serial port remapping

Remember to add the header file to the code

#include < stdio.h >

Compatible with putchar remapping of different IDEs.

#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__ */

Of course, you also need to configure the serial port, without configuring interrupts.

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);
}

Print function

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;
}

Different IDEs must be configured accordingly.

Keil configuration , you need to check the MicroLIB option.

IAR placement

Printing effect

Code and engineering are open source

Code and Keil IAR project open source address:

https://github.com/strongercjd/STM32F207VCT6

 

02, IDE printing

Sometimes, we only need to debug in the office, and the chip does not have an additional serial port to provide us for debugging. It is also a good way to use IDE to print.

2.1 AR IAR

code show as below

  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 printing effect

Configuration method, open TerminalIO: enter debugging- >view->Terminal I/O

2.2、Keil

I haven't found a solution yet. It can be found on the Internet to check Use Simulator, but this is for simulation debugging, not hardware debugging, and does not meet my requirements.

You are welcome to share your own methods and tell you in the comment area.

Code and IAR project open source address:

https://github.com/strongercjd/STM32F207VCT6

 

Click to view the album where this article is located, STM32F207 tutorial

 

Pay attention to the official account, and receive article updates as soon as possible .

 

Guess you like

Origin blog.csdn.net/Firefly_cjd/article/details/109591747