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