本帖最后由 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已经实现了:
/*!
* [url=home.php?mod=space&uid=247401]@brief[/url] 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.
*
* @param[in] handle: the char to put
* @param[in] buffer: file pointer for the std input
* @param[in] size: file pointer for the std input
* [url=home.php?mod=space&uid=266161]@return[/url] return the char of be put
*/
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
}
对应printf里面的内容可以通过UART输出然后在串口终端打印:
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:
在调试时,通过View>Terminal I/O打开Terminal I/O窗口显示对应的打印输出:
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:
对应的调试接口需要选择为SWD:
在调试时,通过View>Terminal I/O打开Terminal I/O窗口显示对应的打印输出:
总结:
printf可以通过UART重定向,需要__write函数实现里面对应UART的发送,可以同时工作在调试模式和独立运行模式。
当没有__write函数实现时,可以在调试的时候使用半主机模式(semihosting)或者SWO(Serial Wire Output)重定向到IAR Embedded Workbench中的Terminal I/O。
注意:
半主机模式(semihosting)只能工作在调试模式,在独立运行模式下会导致程序运行异常。
SWO(Serial Wire Output)在独立运行模式下虽然不会导致程序运行异常,但是对应的printf打印信息不能查看。
|