RT Thread之 Uart2 操作
生活随笔
收集整理的這篇文章主要介紹了
RT Thread之 Uart2 操作
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
官網(wǎng)連接:https://docs.rt-thread.org/#/rt-thread-version/rt-thread-standard/programming-manual/device/uart/uart
通過前面的學習,基本上RT Thread操作步驟都是,先配置單片機底層,然后再通過應用層映射到底層,最后再根據(jù)實際情況進行操作即可;
一、配置步驟:
1、配置cubemx打開uart2、中斷;
2、替換cubemx配置好的文件;
.3、修改Kconfig文件,添加uart2配置選項;
4、用env工具添加uart2配置;
5、修改mdk并添加程序;
6、驗證并展示效果;
?
二、配置cubemx打開uart2、中斷;
三、替換cubemx配置好的文件;
四、修改Kconfig文件,添加uart2配置選項;
五、用env工具添加uart2配置;
?
六、修改mdk芯片配置并添加程序;
/** Copyright (c) 2006-2018, RT-Thread Development Team** SPDX-License-Identifier: Apache-2.0** Change Logs:* Date Author Notes* 2018-11-06 SummerGift first version*/#include <rtthread.h> #include <rtdevice.h> #include <board.h>//******************************* main function ******************************************* /****************************************************************************************** *** 函數(shù)名稱: main *** 輸入?yún)?shù): 無 *** 返 回 值: 無 *** 調(diào)度周期:無 *** 說 明:main函數(shù) *** 鏈 接:無 *** 編者 時間 版本 *** wagnlu 2021/04/02 V0.1 ******************************************************************************************/ int main(void) {int count = 1;while (count++){rt_thread_mdelay(100);}return RT_EOK; }//******************************* GPIO 應用示例 ******************************************* /****************************************************************************************** *** 函數(shù)名稱: functest *** 輸入?yún)?shù): 無 *** 返 回 值: 無 *** 調(diào)度周期:無 *** 說 明:led測試函數(shù) *** 鏈 接:https://docs.rt-thread.org/#/rt-thread-version/rt-thread-standard/programming-manual/device/pin/pin *** 編者 時間 版本 *** wagnlu 2021/04/02 V0.1 ******************************************************************************************//* defined the LED0 pin: PC13 */ #define LED0_PIN_NUM GET_PIN(C, 13) void test_led(void) {uint8_t count =0; //tset count/* set LED0 pin mode to output */rt_pin_mode(LED0_PIN_NUM, PIN_MODE_OUTPUT);while(count <5){count++;rt_kprintf("LED計數(shù):%d \t", count);rt_pin_write(LED0_PIN_NUM, PIN_HIGH); //輸出高rt_kprintf("引腳輸出高電平 \t");rt_thread_mdelay(500);rt_pin_write(LED0_PIN_NUM, PIN_LOW); //輸出低rt_kprintf("引腳輸出底電平\r\n");rt_thread_mdelay(500);} }MSH_CMD_EXPORT(test_led, function test led control 5 times );//******************************* 串口 設(shè)備 應用示例 ******************************************* /****************************************************************************************** *** 函數(shù)名稱: uart_sample *** 輸入?yún)?shù): 無 *** 返 回 值: 無 *** 調(diào)度周期:無 *** 說 明:串口測試函數(shù) *** 鏈 接:https://docs.rt-thread.org/#/rt-thread-version/rt-thread-standard/programming-manual/device/adc/adc *** 編者 時間 版本 *** wagnlu 2021/04/04 V0.1 ******************************************************************************************/#define SAMPLE_UART_NAME "uart2" static struct rt_semaphore rx_sem; /* 用于接收消息的信號量 */ static rt_device_t serial;static rt_err_t uart_input(rt_device_t dev, rt_size_t size) /* 接收數(shù)據(jù)回調(diào)函數(shù) */ {rt_sem_release(&rx_sem); /* 串口接收到數(shù)據(jù)后產(chǎn)生中斷,調(diào)用此回調(diào)函數(shù),然后發(fā)送接收信號量 */return RT_EOK; }static void serial_thread_entry(void *parameter) /* 串口接收線程函數(shù) */ {char ch;while (1){ while (rt_device_read(serial, -1, &ch, 1) != 1) /* 從串口讀取一個字節(jié)的數(shù)據(jù),沒有讀取到則等待接收信號量 */{ rt_sem_take(&rx_sem, RT_WAITING_FOREVER); /* 阻塞等待接收信號量,等到信號量后再次讀取數(shù)據(jù) */}rt_device_write(serial, 0, &ch, 1);rt_kprintf("uart recive:\t%c\r\n", ch); /* finsh 控制臺查看 */} }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); /* 初始化信號量 */ rt_device_open(serial, RT_DEVICE_FLAG_INT_RX); /* 以中斷接收及輪詢發(fā)送模式打開串口設(shè)備 */ rt_device_set_rx_indicate(serial, uart_input); /* 設(shè)置接收回調(diào)函數(shù) */ rt_device_write(serial, 0, str, (sizeof(str) - 1)); /* 發(fā)送字符串 */rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10); /* 創(chuàng)建 serial 線程 */ if (thread != RT_NULL) /* 創(chuàng)建成功則啟動線程 */{rt_thread_startup(thread);}else{ret = RT_ERROR;}return ret; }/* 導出到 msh 命令列表中 */ MSH_CMD_EXPORT(uart_sample, uart device sample);?
七、驗證并展示效果;
總結(jié)
以上是生活随笔為你收集整理的RT Thread之 Uart2 操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IDEA 2022.01 安装教程
- 下一篇: 使用Velocity作为邮件的模板