通過(guò) RT-Thread Studio 配置 AB32VG1 片上外設(shè) UART 的功能,實(shí)現(xiàn)開(kāi)發(fā)板和 PC 進(jìn)行
通信。
1.2. 模塊介紹
AB32VG1 的串口 0 被用作系統(tǒng)調(diào)試串口,串口 1 可以用作通訊端口。RT-Thread 里做好了
UART0 和 UART1 的驅(qū)動(dòng),只要打開(kāi)相應(yīng)的設(shè)備即可。
開(kāi)發(fā)板上串口部分的電路圖如下圖所示:
從電路圖上看,串口 1 使用的是 PA3 和 PA4。
新建工程
2.1.1.文件->新鍵->RT-Thread 項(xiàng)目。
2.1.2.選擇基于開(kāi)發(fā)板,填寫(xiě)工程名字。
2.1.3.開(kāi)發(fā)板:AB32VG1-AB-PROUGEN。
2.1.4.BSP:1.0.8。
2.1.3.其他默認(rèn),點(diǎn)完成。一個(gè)新的項(xiàng)目就建成了。
2.2. 編寫(xiě)測(cè)試程序
在 applications 新鍵 task.c 文件。此例程源自 RT-Thread 文檔中心,引用時(shí)有修改。
/* * 程序清單:這是一個(gè) 串口 設(shè)備使用例程 * 例程導(dǎo)出了 uart_sample 命令到控制終端 * 命令調(diào)用格式:uart_sample uart1 * 命令解釋?zhuān)好畹诙€(gè)參數(shù)是要使用的串口設(shè)備名稱(chēng),為空則使用默認(rèn)的串口設(shè)備 * 程序功能:通過(guò)串口輸出字符串"hello RT-Thread!",然后錯(cuò)位輸出輸入的字符 */ #include #define SAMPLE_UART_NAME "uart1" /* 用于接收消息的信號(hào)量 */ 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ā)送接收信號(hào)量 */ rt_sem_release(&rx_sem); return RT_EOK; } static void serial_thread_entry(void *parameter) { char ch; while (1) { /* 從串口讀取一個(gè)字節(jié)的數(shù)據(jù),沒(méi)有讀取到則等待接收信號(hào)量 */ while (rt_device_read(serial, -1, &ch, 1) != 1) { /* 阻塞等待接收信號(hào)量,等到信號(hào)量后再次讀取數(shù)據(jù) */ rt_sem_take(&rx_sem, RT_WAITING_FOREVER); } /* 讀取到的數(shù)據(jù)通過(guò)串口錯(cuò)位輸出 */ 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; } /* 初始化信號(hào)量 */ rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO); /* 以中斷接收及輪詢發(fā)送模式打開(kāi)串口設(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)建成功則啟動(dò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); 由于在初始化串口時(shí),默認(rèn)波特率是 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; }
-
嵌入式
+關(guān)注
關(guān)注
5094文章
19184瀏覽量
307857 -
uart
+關(guān)注
關(guān)注
22文章
1244瀏覽量
101799 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1305瀏覽量
40412 -
AB32VG1
+關(guān)注
關(guān)注
1文章
5瀏覽量
579
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
在RT-Thread Studio上配置rtthread CANFD驅(qū)動(dòng)來(lái)控制M3508電機(jī)
![在<b class='flag-5'>RT-Thread</b> <b class='flag-5'>Studio</b><b class='flag-5'>上</b><b class='flag-5'>配置</b>rtthread CANFD驅(qū)動(dòng)來(lái)控制M3508電機(jī)](https://file1.elecfans.com/web2/M00/A7/58/wKgaomUjXJmAS56lAACUdF06ewQ979.jpg)
在 RT-Thread Studio 上使用 RT-Thread Nano
基于RT-Thread Studio的ADC外設(shè)使用方案介紹
RT-Thread Studio 主要亮點(diǎn)功能
RT-Thread Studio快速配置GPIO進(jìn)行點(diǎn)燈
![<b class='flag-5'>RT-Thread</b> <b class='flag-5'>Studio</b>快速<b class='flag-5'>配置</b>GPIO進(jìn)行點(diǎn)燈](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
RT-Thread Studio驅(qū)動(dòng)SD卡
![<b class='flag-5'>RT-Thread</b> <b class='flag-5'>Studio</b>驅(qū)動(dòng)SD卡](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
基于RT-Thread Studio學(xué)習(xí)
![基于<b class='flag-5'>RT-Thread</b> <b class='flag-5'>Studio</b>學(xué)習(xí)](https://file1.elecfans.com/web2/M00/82/C1/wKgaomRhn_SAM7fdAACQhFt0KEA325.jpg)
使用RT-Thread Studio進(jìn)行智能家居終端的設(shè)計(jì)
![使用<b class='flag-5'>RT-Thread</b> <b class='flag-5'>Studio</b>進(jìn)行智能家居終端的設(shè)計(jì)](https://file1.elecfans.com/web2/M00/8E/3C/wKgaomTDfrKAOUCQAADZr_2qY8Q463.jpg)
評(píng)論