Redirecting printf to UART in STM32CubeIDE and Printing Floating-Point Numbers
Redirecting printf to UART
usart.c
/* USER CODE BEGIN 0 */
#include "stdio.h"
/* USER CODE END 0 */
/* USER CODE BEGIN 1 */
// The _write function is defined in syscalls.c using __weak, allowing you to define it in other files.
__attribute__((weak)) int _write(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
while ((USART1->SR & 0X40) == 0); // Wait for transmission to complete
USART1->DR = (uint8_t) *ptr++;
}
return len;
}
/* USER CODE END 1 */
Printing Floating-Point Numbers in STM32CubeIDE
- In the STM32CubeIDE, select your project from the sidebar, right-click, and choose
Properties
-C/C++ Build
-Settings
-MCU GCC Linker
-Miscellaneous
. - Add a new entry in the
Other flags
section and enter-u_printf_float
. - Recompile your project.
Fixing Garbled Output in HAL_UART_Receive_IT
To fix the issue of garbled output when using HAL_UART_Transmit(&huart1, (uint8_t *)aRxBuffer, 10,0xFFFF);
, change the data length (10
) to 1
.
References and Acknowledgments
- Redirecting printf Output to UART in STM32CubeIDE
- Modifying printf Redirection and UART Output of Floating-Point Data in STM32CubeIDE
- Revisiting the HAL_UART_Receive_IT Function
Original: https://wiki-power.com/ This post is protected by CC BY-NC-SA 4.0 agreement, should be reproduced with attribution.
This post is translated using ChatGPT, please feedback if any omissions.