一、FinSH组件的命令执行流程图
二、执行环境
1.芯片:GD32F10x
2.串口:
#define FINSH_UART |
USART1 |
#define FINSH_UART_CLK |
RCU_USART1 |
#define FINSH_TX_PIN |
GPIO_PIN_2 |
#define FINSH_RX_PIN |
GPIO_PIN_3 |
#define FINSH_GPIO_PORT |
GPIOA |
#define FINSH_GPIO_CLK |
RCU_GPIOA |
3.编译环境:KEIL
4.RT-Thread:RT-Thread Nano(3.1.3版本)
三、添加步骤
1.添加FinSH组件相关文件
1)路径:../rt-thread-3.1.3/components/finsh
2)KEIL中添加文件
2.在rtconfig.h文件中添加宏 #define RT_USING_FINSH
3.配置对应的串口
#define FINSH_UART USART1
#define FINSH_UART_CLK RCU_USART1
#define FINSH_TX_PIN GPIO_PIN_2
#define FINSH_RX_PIN GPIO_PIN_3
#define FINSH_GPIO_PORT GPIOA
#define FINSH_GPIO_CLK RCU_GPIOA
/*初始化FINSH组件对应的串口,同时进行初始化*/
void rt_thread_finsh_uart_init(void)
{
/* enable GPIO clock */
rcu_periph_clock_enable(FINSH_GPIO_CLK);
/* enable USART clock */
rcu_periph_clock_enable(FINSH_UART_CLK);
/* connect port to USARTx_Tx */
gpio_init(FINSH_GPIO_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, FINSH_TX_PIN);
/* connect port to USARTx_Rx */
gpio_init(FINSH_GPIO_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, FINSH_RX_PIN);
/* USART configure */
usart_deinit(FINSH_UART);
usart_baudrate_set(FINSH_UART, 115200U);
usart_word_length_set(FINSH_UART, USART_WL_8BIT);
usart_stop_bit_set(FINSH_UART, USART_STB_1BIT);
usart_parity_config(FINSH_UART, USART_PM_NONE);
usart_hardware_flow_rts_config(FINSH_UART, USART_RTS_DISABLE);
usart_hardware_flow_cts_config(FINSH_UART, USART_CTS_DISABLE);
usart_receive_config(FINSH_UART, USART_RECEIVE_ENABLE);
usart_transmit_config(FINSH_UART, USART_TRANSMIT_ENABLE);
usart_enable(FINSH_UART);
}
4.编写void rt_hw_console_output(const char *str)和rt_hw_console_getchar函数
/* 实现 2:输出一个字符,系统函数,函数名不可更改 */
void rt_hw_console_output(const char *str)
{
rt_enter_critical();//进入临界段
while(*str != '\0')
{
if(*str == '\n')
{
usart_data_transmit(FINSH_UART, (uint8_t)'\r');
while(RESET == usart_flag_get(FINSH_UART, USART_FLAG_TBE)); //等待发送结束
}
usart_data_transmit(FINSH_UART, (uint8_t)*str++);//发送接收到的数据
while(RESET == usart_flag_get(FINSH_UART, USART_FLAG_TBE)); //等待发送结束
}
rt_exit_critical();//退出临界段
}
char rt_hw_console_getchar(void)
{
int ch = -1;
if(usart_flag_get(FINSH_UART, USART_FLAG_RBNE) != RESET)
{
ch = (uint8_t)usart_data_receive(FINSH_UART);
}
else
{
if(usart_flag_get(FINSH_UART, USART_FLAG_RBNE) != RESET)
{
usart_flag_clear(FINSH_UART, USART_FLAG_RBNE);
}
rt_thread_mdelay(10);
}
return ch;
}
四、完整代码
版权声明:本文为CSDN博主「游走在01的海洋」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_34442618/article/details/121703595
暂无评论