通過 RT-Thread Studio 配置 AB32VG1 片上外設(shè) UART 的功能,實現(xiàn)開發(fā)板和 PC 進行
通信。
1.2. 模塊介紹
AB32VG1 的串口 0 被用作系統(tǒng)調(diào)試串口,串口 1 可以用作通訊端口。RT-Thread 里做好了
UART0 和 UART1 的驅(qū)動,只要打開相應(yīng)的設(shè)備即可。

開發(fā)板上串口部分的電路圖如下圖所示:

從電路圖上看,串口 1 使用的是 PA3 和 PA4。
新建工程
2.1.1.文件->新鍵->RT-Thread 項目。
2.1.2.選擇基于開發(fā)板,填寫工程名字。
2.1.3.開發(fā)板:AB32VG1-AB-PROUGEN。
2.1.4.BSP:1.0.8。
2.1.3.其他默認,點完成。一個新的項目就建成了。
2.2. 編寫測試程序
在 applications 新鍵 task.c 文件。此例程源自 RT-Thread 文檔中心,引用時有修改。
/* * 程序清單:這是一個 串口 設(shè)備使用例程 * 例程導(dǎo)出了 uart_sample 命令到控制終端 * 命令調(diào)用格式:uart_sample uart1 * 命令解釋:命令第二個參數(shù)是要使用的串口設(shè)備名稱,為空則使用默認的串口設(shè)備 * 程序功能:通過串口輸出字符串"hello RT-Thread!",然后錯位輸出輸入的字符 */ #include #define SAMPLE_UART_NAME "uart1" /* 用于接收消息的信號量 */ static struct rt_semaphore rx_sem; static rt_device_t serial; /* 接收數(shù)據(jù)回調(diào)函數(shù) */ static rt_err_t uart_input(rt_device_t dev, rt_size_t size) { /* 串口接收到數(shù)據(jù)后產(chǎn)生中斷,調(diào)用此回調(diào)函數(shù),然后發(fā)送接收信號量 */ rt_sem_release(&rx_sem); return RT_EOK; } static void serial_thread_entry(void *parameter) { char ch; while (1) { /* 從串口讀取一個字節(jié)的數(shù)據(jù),沒有讀取到則等待接收信號量 */ while (rt_device_read(serial, -1, &ch, 1) != 1) { /* 阻塞等待接收信號量,等到信號量后再次讀取數(shù)據(jù) */ rt_sem_take(&rx_sem, RT_WAITING_FOREVER); } /* 讀取到的數(shù)據(jù)通過串口錯位輸出 */ ch = ch + 1; rt_device_write(serial, 0, &ch, 1); } } static int uart_sample(int argc, char *argv[]) { rt_err_t ret = RT_EOK; char uart_name[RT_NAME_MAX]; char str[] = "hello RT-Thread!\r\n"; if (argc == 2) { rt_strncpy(uart_name, argv[1], RT_NAME_MAX); } else { rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX); } /* 查找系統(tǒng)中的串口設(shè)備 */ serial = rt_device_find(uart_name); if (!serial) { rt_kprintf("find %s failed!\n", uart_name); return RT_ERROR; } /* 初始化信號量 */ rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO); /* 以中斷接收及輪詢發(fā)送模式打開串口設(shè)備 */ rt_device_open(serial, RT_DEVICE_FLAG_INT_RX); /* 設(shè)置接收回調(diào)函數(shù) */ rt_device_set_rx_indicate(serial, uart_input); /* 發(fā)送字符串 */ rt_device_write(serial, 0, str, (sizeof(str) - 1)); /* 創(chuàng)建 serial 線程 */ rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10); /* 創(chuàng)建成功則啟動線程 */ if (thread != RT_NULL) { rt_thread_startup(thread); } else { ret = RT_ERROR; } return ret; } /* 導(dǎo)出到 msh 命令列表中 */ MSH_CMD_EXPORT(uart_sample, uart device sample); 由于在初始化串口時,默認波特率是 1500000,可以在 libraries->hal_drivers->drv_usart.c 中 int rt_hw_usart_init(void)做些修改。 int rt_hw_usart_init(void) { rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct ab32_uart); struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; rt_err_t result = 0; rt_hw_interrupt_install(IRQ_UART0_2_VECTOR, uart_isr, RT_NULL, "ut_isr"); for (int i = 0; i < obj_num; i++) { /* init UART object */ uart_obj[i].config = &uart_config[i]; uart_obj[i].rx_idx = 0; uart_obj[i].rx_idx_prev = 0; uart_obj[i].serial.ops = &ab32_uart_ops; uart_obj[i].serial.config = config; uart_obj[i].serial.config.baud_rate = 1500000; uart_obj[i].rx_buf = rt_malloc(uart_config[i].fifo_size); if (uart_obj[i].rx_buf == RT_NULL) { LOG_E("uart%d malloc failed!", i); continue; } //如果是串口 1,修改波特率位 115200 if (i == 1) { uart_obj[i].serial.config.baud_rate = 115200; } //------------------ /* register UART device */ result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX | uart_obj[i].uart_dma_flag , NULL); RT_ASSERT(result == RT_EOK); } return result; }
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。
舉報投訴
-
嵌入式
+關(guān)注
關(guān)注
5199文章
20451瀏覽量
334206 -
uart
+關(guān)注
關(guān)注
22文章
1314瀏覽量
106671 -
RT-Thread
+關(guān)注
關(guān)注
32文章
1614瀏覽量
44895 -
AB32VG1
+關(guān)注
關(guān)注
1文章
5瀏覽量
796
發(fā)布評論請先 登錄
相關(guān)推薦
熱點推薦
在RT-Thread Studio上配置rtthread CANFD驅(qū)動來控制M3508電機
本文旨在RT-Thread Studio上配置rtthread CANFD驅(qū)動來控制M3508電機,不涉及任何原理
開發(fā)環(huán)境:RT-Thread
發(fā)表于 10-08 11:44
?2158次閱讀
在 RT-Thread Studio 上使用 RT-Thread Nano
本文介紹了如何在 RT-Thread Studio 上使用 RT-Thread Nano,并以創(chuàng)建 stm32f103RB 的 Nano 工程為例。準(zhǔn)備工作安裝
發(fā)表于 05-18 15:59
基于RT-Thread Studio的ADC外設(shè)使用方案介紹
ART-Pi,創(chuàng)建模板工程。2、基于RT-Thread Studio 使用STM32單片機的ADC外設(shè)一、工程建立 工程建立和時鐘配置這里不再贅述,可以看上一篇文章,在 PWM 中詳細
發(fā)表于 03-28 16:57
RT-Thread Studio 主要亮點功能
RT-Thread Studio V1.1.0 快速上手體驗 RT-Thread Studio 主要包括工程創(chuàng)建和管理,代碼編輯,SDK管理器,RT
RT-Thread Studio驅(qū)動SD卡
RT-Thread Studio驅(qū)動SD卡前言一、創(chuàng)建基本工程1、創(chuàng)建Bootloader2、創(chuàng)建項目工程二、配置RT-Thread Settings三、代碼分析1.引入庫2.讀入數(shù)據(jù)
發(fā)表于 12-27 19:13
?20次下載
基于RT-Thread Studio學(xué)習(xí)
前期準(zhǔn)備:從官網(wǎng)下載 RT-Thread Studio,弄個賬號登陸,開啟rt-thread學(xué)習(xí)之旅。
使用RT-Thread Studio進行智能家居終端的設(shè)計
本次方案基于星火一號開發(fā)板開發(fā),使用RT-Thread Studio進行工程創(chuàng)建,代碼編輯,RT-Thread配置,調(diào)試配置,程序下載等功能
如何通過RT-Thread Studio配置片上外設(shè)UART
評論