본문 바로가기

STM32

printf UART로 출력

c 함수 printf()를 UART로 출력하게 해서 디버깅을 할 수 있습니다.

 

아래의 링크를 참고하면 간단히 해결됩니다.

The ST-LINKs embedded on ST Nucleo boards have a virtual com port feature, and we can easily get debugging information on a terminal using printf redirected to the UART of the STM32 connected to the ST-LINK pins used for the virtual COM port.

 

https://community.st.com/t5/stm32-mcus/how-to-redirect-the-printf-function-to-a-uart-for-debug-messages/ta-p/49865

 

How to redirect the printf function to a UART for debug messages

Introduction It can be especially useful during code development to display messages in a terminal window about various system parameters to help with debugging. An easy way to do that is to use the printf function and redirect the output to a UART for dis

community.st.com

 

#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
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(&huart2, (uint8_t *)&ch, 1, 0xFFFF);

	return ch;
}

 

__io_putchar() 함수 호출 시 UART로 문자를 전송합니다.

맥 환경이라 아래와 같이 연결했습니다.

 

Core/Src/syscalls.c 은 STM32CubeIDE Minimal System calls file 입니다.

이 파일에 있는 _write()가 __io_putchar()를 호출하게 됩니다.

__attribute__((weak)) int _write(int file, char *ptr, int len)
{
  (void)file;
  int DataIdx;

  for (DataIdx = 0; DataIdx < len; DataIdx++)
  {
    __io_putchar(*ptr++);
  }
  return len;
}

 

이 파일에서는 _read() 정도를 제외한 대부분은 newlib 호환을 위해 정의만 해놓은 함수들로 보입니다.

 

'STM32' 카테고리의 다른 글

FreeRTOS Task priority  (0) 2025.01.13
STM32CubeIDE 업그레이드 함부로 한 댓가...  (0) 2025.01.11
현재 Mac 15.0.1 에서 ST-Link server 연결 문제  (0) 2024.11.18
CAN bus 환경 테스트  (0) 2024.11.17
NUCLEO-F446RE 보드  (0) 2024.10.22