Cannot get printf via USART to work on STM32H745
Hello!
As the title says, I haven't managed to get the printf function to work on my STM32H7 MCU. I am using the Nucleo-H745ZI-Q with STM32Cube MX and Keil. Here is the code I'm using:
#include "stdio.h"
/* USER CODE BEGIN 0 */
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/* USER CODE END 0 */
/* USER CODE BEGIN 4 */
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART3 and Loop until the end of transmission */
HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
/* USER CODE END 4 */When running the code in debug mode, I keep getting stuck on this line in the disassembly:
0x080006XX BEAB BKPT 0xABAfter looking it up online, the most common answer I found was that I needed to add a retarget.c and retarget.h to my project. I tried looking for those files (specific for the H7) online but came up empty-handed. I think it is also worth mentioning that when I comment the line with the printf command, the rest of the code works fine.
I have used the exact same code (except with USART 2 instead of 3) on a Nucleo F446RE board and it worked perfectly fine. I have also read the printf via UART section in the STM32 debug application note (https://www.st.com/resource/en/application_note/dm00354244-stm32-microcontroller-debug-toolbox-stmicroelectronics.pdf) and can't find what I'm missing. Finally, I also tried to find an example code in the STM32H745 github repo (https://github.com/STMicroelectronics/STM32CubeH7/tree/master/Projects/NUCLEO-H745ZI-Q) but there wasn't one for the printf.
Any help would be greatly appreciated!