美文网首页
AT32_IAR的printf打印重定义问题

AT32_IAR的printf打印重定义问题

作者: 飞向深空 | 来源:发表于2023-12-10 15:18 被阅读0次

最近在搞AT32,IAR9.xx以后重定向不再跟MDK一样重定义fputc而是重定义write函数

/*******************
 *
 * Copyright 1998-2017 IAR Systems AB.
 *
 * This is a template implementation of the "__write" function used by
 * the standard library.  Replace it with a system-specific
 * implementation.
 *
 * The "__write" function should output "size" number of bytes from
 * "buffer" in some application-specific way.  It should return the
 * number of characters written, or _LLIO_ERROR on failure.
 *
 * If "buffer" is zero then __write should perform flushing of
 * internal buffers, if any.  In this case "handle" can be -1 to
 * indicate that all handles should be flushed.
 *
 * The template implementation below assumes that the application
 * provides the function "MyLowLevelPutchar".  It should return the
 * character written, or -1 on failure.
 *
 ********************/

#include <LowLevelIOInterface.h>
#include "at32f403a_407.h"
#include "system_at32f403a_407.h"
#include <stdio.h>
#include <stdlib.h>

#pragma module_name = "?__write"

int MyLowLevelPutchar(int ch)
{
   while(usart_flag_get(USART1, USART_TDBE_FLAG) == RESET);
    usart_data_transmit(USART1, ch);
    return ch;
}



/*
 * If the __write implementation uses internal buffering, uncomment
 * the following line to ensure that we are called with "buffer" as 0
 * (i.e. flush) when the application terminates.
 */

size_t __write(int handle, const unsigned char * buffer, size_t size)
{
  /* Remove the #if #endif pair to enable the implementation */
#if 1

  size_t nChars = 0;

  if (buffer == 0)
  {
    /*
     * This means that we should flush internal buffers.  Since we
     * don't we just return.  (Remember, "handle" == -1 means that all
     * handles should be flushed.)
     */
    return 0;
  }

  /* This template only writes to "standard out" and "standard err",
   * for all other file handles it returns failure. */
  if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR)
  {
    return _LLIO_ERROR;
  }

  for (/* Empty */; size != 0; --size)
  {
    if (MyLowLevelPutchar(*buffer++) < 0)
    {
      return _LLIO_ERROR;
    }

    ++nChars;
  }

  return nChars;

#else

  /* Always return error code when implementation is disabled. */
  return _LLIO_ERROR;

#endif

}

把此write函数加入AT32工程.

参考链接:https://blog.csdn.net/weixin_42030296/article/details/126337760

相关文章

网友评论

      本文标题:AT32_IAR的printf打印重定义问题

      本文链接:https://www.haomeiwen.com/subject/qrhpgdtx.html