2023年11月30日 星期四

STM32於IAR 9.3以後的版本將uart導向至printf

首先,請先參考如以下IAR提供的"在IAR Embedded Workbench中实现打印输出技術資料"

https://www.iar.com/cn/knowledge/support/technical-notes/general/implementing-printf-output/

本文要說明的是重新定向到MCU的周邊(UART)



STM32在IAR9.2以前的版本printf時,在main.c的主程式之前通常會加入如以下的程式

#ifdef __GNUC__

/* With GCC, 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__ */


並執行以下宣告如於主程式之後

/**

  * @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 USART2 and Loop until the end of transmission */

  HAL_UART_Transmit(&UartHandle, (uint8_t *)&ch, 1, 0xFFFF);

  return ch;

}

但是在IAR9.32以後,已經不支援此方式了,因此在這邊大致上來記錄一下我查到的資訊與方法跟同樣遇到問題的人分享,

1.先到IAR安裝後的資料夾找出write.c,下圖是write.c原始的內容


2.COPY write.c到自行設定的專案資料夾內

3.在您的IAR專案內加入write.c

4.修改write.c以下內容,把main.h這個head file include進來,然後把您的UART底層驅動的函式放在MyLowLevelPutChar內,大致上就可以了,記住您的main.h裡面必須include stdio.h進來,才會有printf相關的函式,由於我用的底層選擇為STM32K的LL,您也可以換成STM32 HAL或是Standard Peripheral Libraries的底層函式








STM32於IAR 9.3以後的版本將uart導向至printf

首先,請先參考如以下IAR提供的"在IAR Embedded Workbench中实现打印输出技術資料" https://www.iar.com/cn/knowledge/support/technical-notes/general/implementing-printf-ou...