一、生成工程后修改
1.在stm32f4xx_it.c文件里面删除如下程序
/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */
  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}
2.加入下面两句
/* USER CODE BEGIN PTD */ extern void rt_exit_critical(void); extern void rt_enter_critical(void); /* USER CODE END PTD */
3.加入串口打印函数
/* USER CODE BEGIN 4 */
//重映射串口1到rt_kprintf
void rt_hw_console_output(const char *str)
{
    /* empty console output */
	
	rt_enter_critical();
	while(*str!=‘\0‘)
	{
		if(*str==‘\n‘)
		{
			USART1->DR = (uint8_t)  ‘\r‘; 
			while((USART1->SR&0X40)==0);
		}
		USART1->DR =*str++;
		while((USART1->SR&0X40)==0);	
	}
	rt_exit_critical();
}
/* USER CODE END 4 */
4.编译后还有一个错误在shell文件里面
linking... RT_thread\RT_thread.axf: Error: L6218E: Undefined symbol rt_hw_console_getchar (referred from shell.o). Not enough information to list image symbols. Not enough information to list load addresses in the image map. Finished: 2 information, 0 warning and 1 error messages. "RT_thread\RT_thread.axf" - 1 Error(s), 0 Warning(s). Target not created. Build Time Elapsed: 00:00:25
 
在main.c中加入如下函数
char rt_hw_console_getchar(void)
{
    int ch = -1;
 
    if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET)
    {
        ch = huart1.Instance->DR & 0xff;
    }
    else
    {
        if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_ORE) != RESET)
        {
            __HAL_UART_CLEAR_OREFLAG(&huart1);
        }
        rt_thread_mdelay(50);
    }
    return ch;
}
此时编译就没有报错了
Build started: Project: RT_thread
*** Using Compiler ‘V5.06 update 6 (build 750)‘, folder: ‘C:\User Software\keil5\ARM\ARMCC\Bin‘
Build target ‘RT_thread‘
"RT_thread\RT_thread.axf" - 0 Error(s), 0 Warning(s).
Build Time Elapsed:  00:00:01
原文:https://www.cnblogs.com/hyaiwx/p/14736994.html