Printf redirection for stm32 Series Single-Chip Microcomputer

Source: Internet
Author: User

In the process of program debugging, apart from the debugging methods on the top, printf is undoubtedly the most familiar debugging method. By using printf, we can easily and intuitively obtain the running status of the current program.

Printf () is a formatting output function, which is generally used to output information to the standard output device in the specified format. However, in Single-Chip Microcomputer Development, there is generally no standard output device, so we need to redirect the output information of printf, that is, output to other output devices.

There are two methods to implement redirection on the stm32 platform: redirect to UART

Or redirect printf to SWO pin output in jtag sw mode.

First, we will introduce the first method, redirection to UART. We are familiar with this method, which is also used in the Firmware Library officially provided by St.

The Code is as follows: after the UART is initialized, use the following code to redirect the printf

int fputc(int ch, FILE *f){  USART_SendData(USART1, (uint8_t) ch);  /* Loop until the end of transmission */  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)  {}  return ch;}
1. add the ITM port register definition in the source code # define itm_port8 (N) (* (volatile unsigned char *) (0xe0000000 + 4 * n) # define itm_port16 (N) (* (volatile unsigned short *) (0xe0000000 + 4 * n) # define itm_port32 (N) (* (volatile unsigned long *) (0xe0000000 + 4 * n) # define demcr (* (volatile unsigned long *) (0xe000edfc) # define trcena 0x00000002. use the following code to redirect the printf output to the port 0int fputc (int ch, file * f) {If (demcr & trcena) {While (ITM _ Port32 (0) = 0); itm_port8 (0) = CH;} return (CH);} 3. use printf to output the debugging information printf ("ad value = 0x % 04x \ r \ n", ad_value); 4. set JTAG to SW mode and ITM port 0 to obtain information.

Printf redirection for stm32 Series Single-Chip Microcomputer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.