RISC-V MCU中文社区

RISC-V单片机快速入门04-基于RT_Thread Nano添加FinSH

分享于 2020-05-24 09:10:40
0
4135

基础知识


1.FinSH简介

 

RT-Thread FinSH 是 RT-Thread 的命令行组件(shell),提供一套供用户在命令行调用的操作接口,主要用于调试或查看系统信息。它可以使用串口 / 以太网 / USB 等与 PC 机进行通信,本文使用串口进行通信,使用 FinSH 组件基本命令的效果图如下所示:

添加步骤


1.导入工程


将上一节内容进行复制,修改.project中工程名字为FinSH,然后重新import进来新的工程


2.添加FinSH源码到工程


将rt-thread-3.1.3/components/finsh下文件添加到RT-Thread下。


添加成功后结果如下:


3.添加头文件路径


右击工程,点击 properties 进入下图所示界面,点击 C/C++ Build -> settings ,添加头文件路径


4.打开宏定义


添加号FinSH组件源码后,可以看到实际功能并没有打开,需要开启RT_USING_FINSH宏定义。


打开rtconfig.h文件,增加宏定义:#define RT_USING_FINSH


5.适配FinSH组件接口


(1) 修改GD32VF103xB.lds文件



在上图.text中添加如下代码:

    /* section information for finsh shell */    . = ALIGN(4);    __fsymtab_start = .;    KEEP(*(FSymTab))    __fsymtab_end = .;    . = ALIGN(4);    __vsymtab_start = .;    KEEP(*(VSymTab))    __vsymtab_end = .;    . = ALIGN(4);        /* section information for initial. */    . = ALIGN(4);    __rt_init_start = .;    KEEP(*(SORT(.rti_fn*)))    __rt_init_end = .;    . = ALIGN(4);
/* section information for modules */ . = ALIGN(4); __rtmsymtab_start = .; KEEP(*(RTMSymTab)) __rtmsymtab_end = .;


修改后如下所示


(2) 移植函数


本文采用中断方式获取串口接收到字符,原理是,在 uart 接收到数据时产生中断,在中断中把数据存入 ringbuffer 缓冲区,然后释放信号量,tshell 线程接收信号量,然后读取存在 ringbuffer 中的数据。

在gd32vf102c_start.c文件中,实现rt_hw_console_getchar如下:

#define UART_RX_BUF_LEN 128rt_uint8_t uart_rx_buf[UART_RX_BUF_LEN] = {0};struct rt_ringbuffer  uart_rxcb;         /* 定义一个 ringbuffer cb */static struct rt_semaphore shell_rx_sem; /* 定义一个静态信号量 */
void gd_eval_com_init(uint32_t com){ uint32_t com_id = 0U; if(EVAL_COM0 == com){ com_id = 0U; }else if(EVAL_COM1 == com){ com_id = 1U; }
/* 初始化串口接收 ringbuffer */ rt_ringbuffer_init(&uart_rxcb, uart_rx_buf, UART_RX_BUF_LEN);
/* 初始化串口接收数据的信号量 */ rt_sem_init(&(shell_rx_sem), "shell_rx", 0, 0);
eclic_irq_enable(USART0_IRQn, 1, 0);
/* enable GPIO clock */ rcu_periph_clock_enable(COM_GPIO_CLK[com_id]);
/* enable USART clock */ rcu_periph_clock_enable(COM_CLK[com_id]);
/* connect port to USARTx_Tx */ gpio_init(COM_GPIO_PORT[com_id], GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, COM_TX_PIN[com_id]);
/* connect port to USARTx_Rx */ gpio_init(COM_GPIO_PORT[com_id], GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, COM_RX_PIN[com_id]);
/* USART configure */ usart_deinit(com); usart_baudrate_set(com, 115200U); usart_word_length_set(com, USART_WL_8BIT); usart_stop_bit_set(com, USART_STB_1BIT); usart_parity_config(com, USART_PM_NONE); usart_hardware_flow_rts_config(com, USART_RTS_DISABLE); usart_hardware_flow_cts_config(com, USART_CTS_DISABLE); usart_receive_config(com, USART_RECEIVE_ENABLE); usart_transmit_config(com, USART_TRANSMIT_ENABLE); usart_enable(com); usart_interrupt_enable(com, USART_INT_RBNE);}

char rt_hw_console_getchar(const char str){ int ch = 0; / 从 ringbuffer 中拿出数据 */ while (rt_ringbuffer_getchar(&uart_rxcb, (rt_uint8_t *)&ch) != 1) { rt_sem_take(&shell_rx_sem, RT_WAITING_FOREVER); } return ch;}
void USART0_IRQHandler() { int ch = -1; int recv_flag = 0; /* enter interrupt / rt_interrupt_enter(); if(RESET != usart_interrupt_flag_get(EVAL_COM0, USART_INT_FLAG_RBNE)){ while (1) { ch = -1; if (RESET != usart_interrupt_flag_get(EVAL_COM0, USART_INT_FLAG_RBNE)) { ch = usart_data_receive(EVAL_COM0);// rt_kprintf("recv data is :%xrn", ch); } if (ch == -1) { break; } recv_flag = 1; / 读取到数据,将数据存入 ringbuffer */ rt_ringbuffer_putchar(&uart_rxcb, ch); }// if (1 == recv_flag)// { rt_sem_release(&shell_rx_sem);// } } rt_interrupt_leave();}


新增ringbuffer.c函数

/* 第一部分:ringbuffer 实现部分 */#include #include #include "ringbuffer.h"rt_inline enum rt_ringbuffer_state rt_ringbuffer_status(struct rt_ringbuffer *rb){    if (rb->read_index == rb->write_index)    {        if (rb->read_mirror == rb->write_mirror)            return RT_RINGBUFFER_EMPTY;        else            return RT_RINGBUFFER_FULL;    }    return RT_RINGBUFFER_HALFFULL;}/** * get the size of data in rb */rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb){    switch (rt_ringbuffer_status(rb))    {    case RT_RINGBUFFER_EMPTY:        return 0;    case RT_RINGBUFFER_FULL:        return rb->buffer_size;    case RT_RINGBUFFER_HALFFULL:    default:        if (rb->write_index > rb->read_index)            return rb->write_index - rb->read_index;        else            return rb->buffer_size - (rb->read_index - rb->write_index);    };}
void rt_ringbuffer_init(struct rt_ringbuffer *rb, rt_uint8_t *pool, rt_int16_t size){ RT_ASSERT(rb != RT_NULL); RT_ASSERT(size > 0);
/* initialize read and write index */ rb->read_mirror = rb->read_index = 0; rb->write_mirror = rb->write_index = 0;
/* set buffer pool and size */ rb->buffer_ptr = pool; rb->buffer_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);}
/** * put a character into ring buffer */rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch){ RT_ASSERT(rb != RT_NULL);
/* whether has enough space */ if (!rt_ringbuffer_space_len(rb)) return 0;
rb->buffer_ptr[rb->write_index] = ch;
/* flip mirror */ if (rb->write_index == rb->buffer_size-1) { rb->write_mirror = ~rb->write_mirror; rb->write_index = 0; } else { rb->write_index++; }
return 1;}/** * get a character from a ringbuffer */rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch){ RT_ASSERT(rb != RT_NULL);
/* ringbuffer is empty */ if (!rt_ringbuffer_data_len(rb)) return 0;
/* put character */ *ch = rb->buffer_ptr[rb->read_index];
if (rb->read_index == rb->buffer_size-1) { rb->read_mirror = ~rb->read_mirror; rb->read_index = 0; } else { rb->read_index++; }
return 1;}

运行结果


 使用jlink烧录,通过控制台输入version,运行结果如下所示

结语


如您在使用过程中有任何问题,请加QQ群进一步交流。


QQ交流群:906015840 (备注:物联网项目交流)


资料获取:关注下方公众号,回复risc-v即可


一叶孤沙出品:一沙一世界,一叶一菩提



*免责声明:以上内容仅供交和流学习之用。如有任何疑问或异议,请留言与我们联系。
4135 0

你的回应
heian

heian 未通过实名认证

懒的都不写签名

积分
问答
粉丝
关注
专栏作者
  • RV-STAR 开发板
  • RISC-V处理器设计系列课程
  • 培养RISC-V大学土壤 共建RISC-V教育生态
RV-STAR 开发板