This project maintains redirecting the printf() function to an UART. Also, it supports printing debug messages depending on the debug verbose level (DEBUG_LEVEL).
printf()UART redirection- Printing debug messages depending on the verbose level
- STM32 HAL library
- In order to use this feature, copy all
*.cand*.hfiles into your project or add the repository as submodule.
git submodule add https://github.com/NyankoTear/printf-utils.git-
Of course, you must activate at least one UART peripheral.
-
The
printf_config.hneedsusart.h, CheckGenerate peripheral initalization as a pair of '.c/.h' files per peripheralin Project Manager -> Code Generator -> Generated files from a STM32CubeMX application.
Note: If you don't want to use this option or not using the STM32CubeMX, make sure that
printf_config.hincludesUART_HandleTypeDefandHAL_UART_Transmit().
- Include all paths.
- Initialize the UART handler calling
initialize_uart_printf(). - You can finally use
printf().
- First, you must redirect the
printf()function to the UART. Please follow the guideline: How to useprintf(). - Add a preprocessor symbol:
DEBUG_LEVEL=<verbose level> - Depending on your debug verbose level, the
<verbose level>can be set from 0 to 3.
DEBUG_LEVEL=0 // Deactivates all debug messages.
DEBUG_LEVEL=1 // Only activates DEBUG_V(). DEBUG_VV() and DEBUG_VVV() doesn't print anything.
DEBUG_LEVEL=2 // Activates DEBUG_V() and DEBUG_VV().
DEBUG_LEVEL=3 // Activates all debug messages.You may coded like this:
printf("Print something");stdout is line buffered by default. Therefore, it doesn't prompt the text on the screen until a new line escape sequence (\n) met. Adding the \n at the end of the string resolves the issue.
printf("Print something\n");However, the solution from the above forces to move the cursor to the next line. If you not happy with it, there is a great link that explains about stdout and how to print immediately. I would recommend 2 possible ways from it.
- Using
fflush
printf("Print immediately");
fflush(stdout);- Make
stdoutto 'no buffering'
setvbuf(stdout, NULL, _IONBF, 0);
printf("Print immediately");Simply check Use float with printf from newlib-nano (-u _printf_float) option in the project properties.
You must add -u _printf_float to the linker options (LDFLAGS) in the Makefile.