RISC-V單片機快速入門04-基于RT_Thread Nano添加FinSH
前言:
本文引用地址:http://butianyuan.cn/article/202006/414406.htm上一節(jié),我們適配了控制臺輸出,可以打印調試信息,本節(jié)我們?yōu)橄到y(tǒng)增加FinSH功能,增加FinSH組件后,用戶可輸入命令調試或查看系統(tǒng)信息。
一、基礎知識
1.FinSH簡介
RT-Thread FinSH 是 RT-Thread 的命令行組件(shell),提供一套供用戶在命令行調用的操作接口,主要用于調試或查看系統(tǒng)信息。它可以使用串口 / 以太網(wǎng) / USB 等與 PC 機進行通信,本文使用串口進行通信,使用 FinSH 組件基本命令的效果圖如下所示:
二、添加步驟
1.導入工程
將上一節(jié)內容進行復制,修改.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) 移植函數(shù)
本文采用中斷方式獲取串口接收到字符,原理是,在 uart 接收到數(shù)據(jù)時產(chǎn)生中斷,在中斷中把數(shù)據(jù)存入 ringbuffer 緩沖區(qū),然后釋放信號量,tshell 線程接收信號量,然后讀取存在 ringbuffer 中的數(shù)據(jù)。
在gd32vf102c_start.c文件中,實現(xiàn)rt_hw_console_getchar如下:
#define UART_RX_BUF_LEN 128
rt_uint8_t uart_rx_buf[UART_RX_BUF_LEN] = {0};
struct rt_ringbuffer uart_rxcb; /* 定義一個 ringbuffer cb */
static struct rt_semaphore shell_rx_sem; /* 定義一個靜態(tài)信號量 */
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);
/* 初始化串口接收數(shù)據(jù)的信號量 */
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 中拿出數(shù)據(jù) */
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;
/ 讀取到數(shù)據(jù),將數(shù)據(jù)存入 ringbuffer */
rt_ringbuffer_putchar(&uart_rxcb, ch);
}
// if (1 == recv_flag)
// {
rt_sem_release(&shell_rx_sem);
// }
}
rt_interrupt_leave();
}
新增ringbuffer.c函數(shù)
/* 第一部分:ringbuffer 實現(xiàn)部分 */
#include <rtthread.h>
#include <string.h>
#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,運行結果如下所示
評論