[活动专区] 【杰发科技AC7840x测评】+ IAR + printf打印输出

[复制链接]
4064|0
 楼主| ChangjiangSZ 发表于 2023-12-15 13:51 | 显示全部楼层 |阅读模式
本帖最后由 ChangjiangSZ 于 2023-12-15 13:51 编辑

在前面介绍了杰发科技AC7840x IAR环境的搭建:https://bbs.21ic.com/icview-3347532-1-1.html
本次介绍AC7840x IAR环境里面的printf实现,参考了IAR的技术文章“在IAR Embedded Workbench中实现打印输出”:https://www.iar.com/cn/knowledge/support/technical-notes/general/implementing-printf-output/

1.prinf重定向到UART
在IAR里面printf重定向UART需要提供对应的__write函数实现,这在AC7840x的SDK里面的debugout_ac7840x.c已经实现了:
  1. /*!
  2. * [url=home.php?mod=space&uid=247401]@brief[/url] If the __write implementation uses internal buffering, uncomment
  3. * the following line to ensure that we are called with "buffer" as 0
  4. * (i.e. flush) when the application terminates.
  5. *
  6. * @param[in] handle: the char to put
  7. * @param[in] buffer: file pointer for the std input
  8. * @param[in] size:   file pointer for the std input
  9. * [url=home.php?mod=space&uid=266161]@return[/url] return the char of be put
  10. */
  11. size_t __write(int handle, const unsigned char * buffer, size_t size)
  12. {
  13.   /* Remove the #if #endif pair to enable the implementation */
  14. #if 1

  15.   size_t nChars = 0;

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

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

  31.   for (/* Empty */; size != 0; --size)
  32.   {
  33.     if (MyLowLevelPutchar(*buffer++) < 0)
  34.     {
  35.       return _LLIO_ERROR;
  36.     }

  37.     ++nChars;
  38.   }

  39.   return nChars;

  40. #else

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

  43. #endif

  44. }

对应printf里面的内容可以通过UART输出然后在串口终端打印: 44648657be7fa2c004.png

2.prinf通过半主机模式(semihosting)重定向到IAR Embedded Workbench中的Terminal I/O
需要先把__write函数实现注释掉,然后在Project>Options>General Options>Library Configuration>Library low-level interface implementation选项中,选择Semihosted,并选择stdout/stderr选项为Via semihosting:
22878657be86b13e9b.png

在调试时,通过View>Terminal I/O打开Terminal I/O窗口显示对应的打印输出: 29684657be87aedd3f.png

3.prinf通过SWO(Serial Wire Output)重定向到IAR Embedded Workbench中的Terminal I/O
需要先把__write函数实现注释掉,然后在Project>Options>General Options>Library Configuration>Library low-level interface implementation选项中,选择Semihosted,并选择stdout/stderr选项为Via SWO:
95340657be8b882a5c.png

对应的调试接口需要选择为SWD:
88396657be8c6bfe76.png

在调试时,通过View>Terminal I/O打开Terminal I/O窗口显示对应的打印输出: 39151657be8d6b7f46.png

总结:
printf可以通过UART重定向,需要__write函数实现里面对应UART的发送,可以同时工作在调试模式和独立运行模式。
当没有__write函数实现时,可以在调试的时候使用半主机模式(semihosting)或者SWO(Serial Wire Output)重定向到IAR Embedded Workbench中的Terminal I/O。

注意:
半主机模式(semihosting)只能工作在调试模式,在独立运行模式下会导致程序运行异常。
SWO(Serial Wire Output)在独立运行模式下虽然不会导致程序运行异常,但是对应的printf打印信息不能查看。

您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

6

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部