STM32半主机模式

来源:互联网 发布:怎么在淘宝论坛发帖子 编辑:程序博客网 时间:2024/05/19 02:42
半主机是这么一种机制,它使得在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; 

}