1、在Additional Software中选择上shell
2、选择一个串口作为输出,这里选择串口1
3、生产工程时生成单独.c.h文件
4、在usart.c中添加rtthread.h头文件
5、在usart.c的保护区1添加
rt_hw_console_output和rt_hw_console_getchar
/* USER CODE BEGIN 1 */
void rt_hw_console_output(const char *str)
{
rt_size_t i = 0, size = 0;
char a = '\r';
__HAL_UNLOCK(&huart1);
size = rt_strlen(str);
for (i = 0; i < size; i++)
{
if (*(str + i) == '\n')
{
HAL_UART_Transmit(&huart1, (uint8_t *)&a, 1, 1);
}
HAL_UART_Transmit(&huart1, (uint8_t *)(str + i), 1, 1);
}
}
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(10);
}
return ch;
}
/* USER CODE END 1 */
6、完成以上步骤就可以使用finsh了
使用软件Xshell 6
参考博客:
https://blog.csdn.net/u014779536/article/details/103985723
修改了后半部分的finsh移植
优化了重新配置stm32cubemx引脚时要重新移植finsh的问题