Library reports error: __use_no_semihosting was requested, but _tty

7 篇文章 1 订阅

在向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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Achou.Wang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值