IAR 版本小于 9030001的串口 printf重映射实现:
int putchar(int ch)
{
UART_SendData(UART2, (uint8_t)ch);
while (RESET == UART_GetFlagStatus(UART2, UART_FLAG_TXEMPTY))
{
}
return (ch);
}
IAR版本大于等于9030001的串口 printf重映射实现:
#include <stddef.h>
#include <LowLevelIOInterface.h>
size_t __write(int handle, const unsigned char *buf, size_t bufSize)
{
size_t nChars = 0;
/* Check for the command to flush all handles */
if (-1 == handle)
{
return (0);
}
/* Check for stdout and stderr (only necessary if FILE descriptors are enabled.) */
if ((_LLIO_STDOUT != handle) && (_LLIO_STDERR != handle))
{
return (-1);
}
for (/* Empty */; bufSize > 0; --bufSize)
{
UART_SendData(UART2, (uint8_t)*buf);
while (RESET == UART_GetFlagStatus(UART2, UART_FLAG_TXEMPTY))
{
}
++buf;
++nChars;
}
return (nChars);
}
Keil 及 ARMGCC 的串口 printf重映射实现:
/***********************************************************************************************************************
* @brief redefine fputc function
* @note for printf
* @param ch
* @param f
* @retval ch
*********************************************************************************************************************/
int fputc(int ch, FILE *f)
{
UART_SendData(UART2, (uint8_t)ch);
while (RESET == UART_GetFlagStatus(UART2, UART_FLAG_TXEMPTY))
{
}
return (ch);
}
#endif
在同一个文件中通过宏定义判断统一实现方法
#if defined (__ICCARM__)
#if (__VER__ >= 9030001)
/* Files include */
#include <stddef.h>
#include <LowLevelIOInterface.h>
/***********************************************************************************************************************
* @brief redefine __write function
* @note for printf
* @param handle
* @param *buf
* @param bufSize
* @retval nChars
*********************************************************************************************************************/
size_t __write(int handle, const unsigned char *buf, size_t bufSize)
{
size_t nChars = 0;
/* Check for the command to flush all handles */
if (-1 == handle)
{
return (0);
}
/* Check for stdout and stderr (only necessary if FILE descriptors are enabled.) */
if ((_LLIO_STDOUT != handle) && (_LLIO_STDERR != handle))
{
return (-1);
}
for (/* Empty */; bufSize > 0; --bufSize)
{
UART_SendData(UART2, (uint8_t)*buf);
while (RESET == UART_GetFlagStatus(UART2, UART_FLAG_TXEMPTY))
{
}
++buf;
++nChars;
}
return (nChars);
}
#else
/***********************************************************************************************************************
* @brief redefine fputc function
* @note for printf
* @param ch
* @param f
* @retval ch
*********************************************************************************************************************/
int putchar(int ch)
{
UART_SendData(UART2, (uint8_t)ch);
while (RESET == UART_GetFlagStatus(UART2, UART_FLAG_TXEMPTY))
{
}
return (ch);
}
#endif
#elif (defined (__GNUC__) || defined (__CC_ARM))
/***********************************************************************************************************************
* @brief redefine fputc function
* @note for printf
* @param ch
* @param f
* @retval ch
*********************************************************************************************************************/
int fputc(int ch, FILE *f)
{
UART_SendData(UART2, (uint8_t)ch);
while (RESET == UART_GetFlagStatus(UART2, UART_FLAG_TXEMPTY))
{
}
return (ch);
}
#endif