STM32cubeIDE printf debug message to console

Thread Starter

zazas321

Joined Nov 29, 2015
936
Hey. I have recently started learning stm32. The first thing I need to setup before I do any is to get printf print out a message to a console so it is easier for me to debug.

I have found a numerous forum threads regarding this issue but none of them are fully solved. From what I understood, there are few minor changes that needs to be implemented to get printf to work to print a message to a console:
First, defining sine functions:

Code:
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
and then further down the code:

Code:
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART1 and Loop until the end of transmission */
  HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 0xFFFF);

  return ch;
}
Now my actual code:

Code:
int main(void)
{
  /* USER CODE BEGIN 1 */
    uint8_t uart3_data[20] = "hello from uart3";
    uint8_t uart1_data[20] = "hello from uart1";
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART3_UART_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
      HAL_Delay(1000);
      printf("UART Printf Example: retarget the C library printf function to the UART \n\r");
      printf("** Test finished successfully. ** \n\r");
      HAL_UART_Transmit(&huart3,uart3_data, sizeof(uart3_data), 50); // just to see what happens

      //HAL_UART_Transmit(&huart1,uart1_data, sizeof(uart1_data), 50); // just to see what happens


  }
  /* USER CODE END 3 */
}
Keep in mind that I have used CubeMX program to generate all the required peripherals so I can be certain that I did not make any mistake initializing something myself.

I am able to sucessfully upload the program to the device and I can see that printf fuction invokes the _write and HAL_UART_Transmit functions by using a breakpoints but the message does not show up in the console
1610085014052.png



I have tried to open a terminal program and it works with the terminal, I can see the messages appear as programmed every 1 second:
1610085068146.png

But how do I get it working in the cubeIDE console instead?
Any tips appreciated...
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
Just an update on my research on printf to console:

I have come across this discussion regarding same issue:
https://electronics.stackexchange.com/questions/445815/stm32-swd-printf-not-working

I have tried to enable tracing and change the frequency to sysclk as suggested, however, I still cant get the message to show up in console:
1610110635696.png


Also, I would like to know whether there is any difference on what debugging option I choose:
1610110663523.png
 
Top