当前位置: 代码迷 >> 综合 >> GD32 RT-Thread Nano+FinSH组件
  详细解决方案

GD32 RT-Thread Nano+FinSH组件

热度:77   发布时间:2023-09-23 00:03:02.0

一、FinSH组件的命令执行流程图

GD32 RT-Thread Nano+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中添加文件

GD32 RT-Thread Nano+FinSH组件

2.在rtconfig.h文件中添加宏 #define RT_USING_FINSH

GD32 RT-Thread Nano+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;

}

四、完整代码

GD32103C-START.rar-C/C++文档类资源-CSDN下载

  相关解决方案