RT-Thread 提供了一套简单的 I/O 设备模型框架,如下图所示,它位于硬件和应用程序之间,共分成三层,从上到下分别是 I/O 设备管理层、设备驱动框架层、设备驱动层。
☐ 应用程序通过 I/O 设备管理接口获得正确的设备驱动,然后通过这个设备驱动与底层 I/O 硬件设备进行交互。
☐ I/O 设备管理层实现了对设备驱动程序的封装
☐ 设备驱动框架层是对同类硬件设备驱动的抽象,将不同厂家的同类硬件设备驱动中相同的部分抽取出来,将不同部分留出接口,由驱动程序实现。
☐ 设备驱动层是一组驱使硬件设备工作的程序,实现访问硬件设备的功能。
简单设备的注册不经过设备驱动框架层,直接将设备注册到I/O设备管理器中
☐ 设备驱动根据设备模型定义,创建出具备硬件访问能力的设备实例,将该设备通过rt_device_register()接口注册到 I/O 设备管理器中
☐ 应用程序通过 rt_device_find()接口查找到设备,然后使用 I/O 设备管理接口来访问硬件,如下图所示:
改掉一些系统本身的bug:
需要自己手动在serial里面添加#include
让编译链编译时能编译到这几个头文件:否则提示找不到头文件,需要添加环境配置,加上这两个路径
:
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-08-27 RT-Thread first version
*/
#include
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include
#include
rt_device_t u1_dev;
struct rt_semaphore sem;
rt_thread_t u1_th;
struct serial_configure u1_configs = RT_SERIAL_CONFIG_DEFAULT;//默认串口配置
rt_err_t rx_callback(rt_device_t dev, rt_size_t size)//接收回调函数
{
rt_sem_release(&sem);//释放
return RT_EOK;
}
void serial_thread_entry(void *parameter)
{
char buffer;
while(1){
while(rt_device_read(u1_dev, 0, &buffer, 1) != 1){//读取字符
rt_sem_take(&sem, RT_WAITING_FOREVER);
}
rt_kprintf("%c",buffer);
}
}
int main(void)
{
rt_err_t ret = 0;
u1_dev = rt_device_find("uart1");//在应用层找到你想要的串口设备
if(u1_dev == RT_NULL){
LOG_E("rt_device_find[uart1] failed...\n");
return -EINVAL;
}
ret = rt_device_open(u1_dev, RT_DEVICE_OFLAG_RDWR|RT_DEVICE_FLAG_INT_RX);//以中断的方式打开你找到的设备
if(ret < 0){
LOG_E("rt_device_open[uart1] failed...\n");
return ret;
}
rt_device_control(u1_dev, RT_DEVICE_CTRL_CONFIG, (void *)&u1_configs);//串口配置
rt_device_set_rx_indicate(u1_dev, rx_callback);//设置回调函数调用rx_callback
rt_sem_init(&sem,"rx_sem", 0, RT_IPC_FLAG_FIFO);
u1_th = rt_thread_create("u1_recv",serial_thread_entry , NULL, 1024, 10, 5);
rt_thread_startup(u1_th);
rt_device_write(u1_dev, 0, "Uart1 config...\n", rt_strlen("Uart1 config...\n"));//写入字符串
return RT_EOK;
}
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-08-27 RT-Thread first version
*/
#include
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include
#include
rt_device_t u1_dev;
struct rt_semaphore sem;
rt_thread_t u1_th;
rt_size_t rx_len = 0;
struct serial_configure u1_configs = RT_SERIAL_CONFIG_DEFAULT;
rt_err_t rx_callback(rt_device_t dev, rt_size_t size)
{
rx_len = size;
rt_sem_release(&sem);
return RT_EOK;
}
void serial_thread_entry(void *parameter)
{
rt_size_t len = 0;
char buffer[512] = {0};
while(1){
rt_sem_take(&sem, RT_WAITING_FOREVER);
len = rt_device_read(u1_dev, 0, buffer, rx_len);
buffer[len] = '\0';
rt_kprintf("buffer:%s\n",buffer);
}
}
int main(void)
{
rt_err_t ret = 0;
u1_dev = rt_device_find("uart1");
if(u1_dev == RT_NULL){
LOG_E("rt_device_find[uart1] failed...\n");
return -EINVAL;
}
ret = rt_device_open(u1_dev, RT_DEVICE_FLAG_DMA_RX);//以DMA的方式打开你的设备
if(ret < 0){
LOG_E("rt_device_open[uart1] failed...\n");
rt_kprintf("ret : %d\n",ret);
return ret;
}
rt_device_control(u1_dev, RT_DEVICE_CTRL_CONFIG, (void *)&u1_configs);
rt_device_set_rx_indicate(u1_dev, rx_callback);
ret = rt_sem_init(&sem,"rx_sem", 0, RT_IPC_FLAG_FIFO);
if(ret < 0){
LOG_E("rt_sem_init failed[%d]...\n",ret);
return ret;
}
u1_th = rt_thread_create("u1_recv",serial_thread_entry , NULL, 1024, 10, 5);
rt_thread_startup(u1_th);
rt_device_write(u1_dev, 0, "Uart1 config...\n", rt_strlen("Uart1 config...\n"));
return RT_EOK;
}