RT-Thread Stm32f103開啓UART2 使用RT-Thread Studio
1. 使用RT-Thread Studio新建RT-Thread項目
2. 修改dricer->doard.h
*STEP 1, define macro define related to the serial port opening based on the serial port number
* such as #define BSP_USING_UATR1
*
* STEP 2, according to the corresponding pin of serial port, modify the related serial port information
* such as #define UART1_TX_PORT GPIOX -> GPIOA
* #define UART1_RX_PORT GPIOX -> GPIOA
* #define UART1_TX_PIN GPIO_PIN_X -> GPIO_PIN_9
* #define UART1_RX_PIN GPIO_PIN_X -> GPIO_PIN_10
增加UART2的宏定義
設置gpio接口
*STEP 3, if you want using SERIAL DMA, you must open it in the RT-Thread Setting.
* RT-Thread Setting -> Components -> Device Drivers -> Serial Device Drivers -> Enable Serial DMA Mode
*
* STEP 4, according to serial port number to define serial port tx/rx DMA function in the board.h file
* such as #define BSP_UART1_RX_USING_DMA
3. 串口設備使用示例
DMA 接收及輪詢發送
當串口接收到一批數據後會調用接收回調函數,接收回調函數會把此時緩衝區的數據大小通過消息隊列發送給等待的數據處理線程。線程獲取到消息後被激活,並讀取數據。一般情況下 DMA 接收模式會結合 DMA 接收完成中斷和串口空閒中斷完成數據接收。
此示例代碼不侷限於特定的 BSP,根據 BSP 註冊的串口設備,修改示例代碼宏定義 SAMPLE_UART_NAME 對應的串口設備名稱即可運行。
運行序列圖如下圖所示:
main.c
#include <rtthread.h>
#include <board.h>
#include <rtdevice.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#define SAMPLE_UART_NAME "uart2" /* 串口設備名稱 */
/* 串口接收消息結構*/
struct rx_msg
{
rt_device_t dev;
rt_size_t size;
};
/* 串口設備句柄 */
static rt_device_t serial;
/* 初始化配置參數 */
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
/* 消息隊列控制塊 */
static struct rt_messagequeue rx_mq;
/* 接收數據回調函數 */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
struct rx_msg msg;
rt_err_t result;
msg.dev = dev;
msg.size = size;
result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
if ( result == -RT_EFULL)
{
/* 消息隊列滿 */
rt_kprintf("message queue full!\n");
}
return result;
}
static void serial_thread_entry(void *parameter)
{
struct rx_msg msg;
rt_err_t result;
rt_uint32_t rx_length;
static char rx_buffer[RT_SERIAL_RB_BUFSZ + 1];
while (1)
{
rt_memset(&msg, 0, sizeof(msg));
/* 從消息隊列中讀取消息*/
result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
if (result == RT_EOK)
{
/* 從串口讀取數據*/
rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
rx_buffer[rx_length] = '\0';
/* 通過串口設備 serial 輸出讀取到的消息 */
rt_device_write(serial, 0, rx_buffer, rx_length);
/* 打印數據 */
rt_kprintf("%s\n",rx_buffer);
}
}
}
static int uart_dma_sample()
{
rt_err_t ret = RT_EOK;
char uart_name[RT_NAME_MAX];
static char msg_pool[256];
char str[] = "hello RT-Thread!\r\n";
/* 查找串口設備 */
serial = rt_device_find(SAMPLE_UART_NAME);
if (!serial)
{
rt_kprintf("find %s failed!\n", uart_name);
return RT_ERROR;
}
/* 初始化消息隊列 */
rt_mq_init(&rx_mq, "rx_mq",
msg_pool, /* 存放消息的緩衝區 */
sizeof(struct rx_msg), /* 一條消息的最大長度 */
sizeof(msg_pool), /* 存放消息的緩衝區大小 */
RT_IPC_FLAG_FIFO); /* 如果有多個線程等待,按照先來先得到的方法分配消息 */
/* 修改串口配置參數 */
config.baud_rate = BAUD_RATE_9600; //修改波特率爲 9600
config.data_bits = DATA_BITS_8; //數據位 8
config.stop_bits = STOP_BITS_1; //停止位 1
config.bufsz = 128; //修改緩衝區 buff size 爲 128
config.parity = PARITY_NONE; //無奇偶校驗位
/*控制串口設備。通過控制接口傳入命令控制字,與控制參數 */
rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
/* 以 DMA 接收及輪詢發送方式打開串口設備 */
rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX);
/* 設置接收回調函數 */
rt_device_set_rx_indicate(serial, uart_input);
/* 發送字符串 */
rt_device_write(serial, 0, str, (sizeof(str) - 1));
/* 創建 serial 線程 */
rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
/* 創建成功則啓動線程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
ret = RT_ERROR;
}
return ret;
}
int main(void)
{
uart_dma_sample();
return RT_EOK;
}
4. 運行結果
代碼中設置的串口波特率爲9600