欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Library reports error: __use_no_semihosting was requested, but _tty

发布时间:2025/3/15 编程问答 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Library reports error: __use_no_semihosting was requested, but _tty 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

在向STM32工程中添加文件的时候遇到了报错 Library reports error: __use_no_semihosting was requested, but _ttywrch was referenced
在网上找了好久终于找到了可行的办法,终于知道了,出现这样的错误说明你编写的程序使程序进入了半主机模式
半主机模式的解释如下:
半主机是这么一种机制,它使得在ARM目标上跑的代码,如果主机电脑运行了调试器,那么该代码可以使用该主机电脑的输入输出设备。
这点非常重要,因为开发初期,可能开发者根本不知道该 ARM 器件上有什么输入输出设备,而半主基机制使得你不用知道ARM器件的外设,利用主机电脑的外设就可以实现输入输出调试。
所以要利用目标 ARM器件的输入输出设备,首先要关掉半主机机制。然后再将输入输出重定向到 ARM 器件上,如 printf 和 scanf,你需要重写 fputc和 fgetc 函数。下面就是将 scanf 和 printf 重定向到 uart 的代码。

/**
* @brief Retargets the C library printf function to the USART.
* @param ch: the char to be send.
* @param *f:
* @retval the char that send out.
*/
int fputc(int ch, FILE *f)
{
/* lace your implementation of fputc here */
/* e.g. write a character to the USART */

/* Loop until the end of transmission */ while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) { } USART_SendData(USART1, (uint8_t) ch); return ch;

}

/**
* @brief Retargets the C library printf function to the USART.
* @param *f
* @retval the char that received.
*/
int fgetc(FILE *f)
{
int ch;
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{
}
ch = USART_ReceiveData(USART1);

while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) { } USART_SendData(USART1, (uint8_t) ch); return ch;

}

解决办法在usart.c文件大约50行左右加入一个函数
//__use_no_semihosting was requested, but _ttywrch was
_ttywrch(int ch)
{
ch = ch;
}
或者勾选微库就可以解决上述问题
主要的原因可能是输出函数的重定向引起的,具体的原因不是很确定。
下面的这几句话是KEIL官网上给出的解释
Defined in rt_sys.h, the _ttywrch() function writes a character to the console.

The console might have been redirected. You can use this function as a last resort error handling routine.

总结

以上是生活随笔为你收集整理的Library reports error: __use_no_semihosting was requested, but _tty的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。