添加自定义驱动效果
驱动
my_test_driver.c
#include <stdint.h>
#include <stdio.h>
#include <rtthread.h>
/**
* @brief 驱动初始化设备
*
* @param device 需要初始化的设备
* @return rt_err_t 返回初始化状态
*/
rt_err_t my_test_driver_init(struct rt_device *device)
{
rt_kprintf("Hello my_test_driver_init\n");
return 0;
}
/**
* @brief 驱动打开设备
*
* @param device 需要打开的设备
* @param oflag 打开标志类型
* @return rt_err_t 返回打开状态
*/
rt_err_t my_test_driver_open(struct rt_device *device, rt_uint16_t oflag)
{
rt_kprintf("Hello my_test_driver_open\n");
return 0;
}
/**
* @brief 驱动关闭设备
*
* @param device 需要关闭的设备
* @return rt_err_t 返回关闭状态
*/
rt_err_t my_test_driver_close(struct rt_device *device)
{
rt_kprintf("Hello my_test_driver_close\n");
return 0;
}
/**
* @brief 驱动读取设备
*
* @param device 读取设备
* @param pos 读取数据偏移量
* @param buffer 读取数据buffer
* @param size 读取数据大小
* @return rt_ssize_t 返回读到数据的实际大小
*/
rt_ssize_t my_test_driver_read(struct rt_device *device, rt_off_t pos, void *buffer, rt_size_t size)
{
rt_kprintf("Hello my_test_driver_read\n");
return 0;
}
/**
* @brief 驱动写设备
*
* @param device 写入的设备
* @param pos 写入数据偏移量
* @param buffer 写入数据buffer
* @param size 写入数据大小
* @return rt_ssize_t 返回写入数据的实际大小
*/
rt_ssize_t my_test_driver_write(struct rt_device *device, rt_off_t pos, const void *buffer, rt_size_t size)
{
rt_kprintf("Hello my_test_driver_write\n");
return 0;
}
/**
* @brief 驱动控制控制设备
*
* @param device 控制设备
* @param cmd 命令控制字,这个参数通常与设备驱动程序相关
* @param args 控制参数
* @return rt_err_t 返回控制状态
*/
rt_err_t my_test_driver_control(struct rt_device *device, int cmd, void *args)
{
rt_kprintf("Hello my_test_driver_control\n");
return 0;
}
设备
my_tset_device.c
#include <my_test_driver.h>
struct drv_mytest_device
{
struct rt_device parent;
};
/**
* @brief 定义的设备
*
*/
static struct drv_mytest_device my_test_device;
#ifdef RT_USING_DEVICE_OPS
/**
* @brief 驱动ops接口
*
*/
const static struct rt_device_ops my_test_driver_ops =
{
my_test_driver_init,
my_test_driver_open,
my_test_driver_close,
my_test_driver_read,
my_test_driver_write,
my_test_driver_control};
#endif
/**
* @brief 设备驱动注册
*
* @return int
*/
int my_test_device_hw_init(void)
{
my_test_device.parent.type = RT_Device_Class_Char;
#ifdef RT_USING_DEVICE_OPS
my_test_device.parent.ops = &my_test_driver_ops;
#else
my_test_device.init = my_test_driver_init;
#endif
rt_device_register((rt_device_t)&my_test_device, "my_test", RT_DEVICE_FLAG_RDWR);
return 0;
}
//添加到设备初始化列表
INIT_DEVICE_EXPORT(my_test_device_hw_init);
app使用设备驱动
my_tset_device_app.c
#include <stdint.h>
#include <stdio.h>
#include <rtthread.h>
#include <my_tset_device.h>
#include <rtdef.h>
/**
* @brief 设备指针
*
*/
static rt_device_t my_test_device_p = 0;
/**
* @brief 设备初始化接口
*
*/
int my_test_device_init(void)
{
my_test_device_p = rt_device_find("my_test");
if (my_test_device_p == 0)
{
rt_kprintf("error!\n");
/* get device information failed */
return RT_ERROR;
}
my_test_device_p->ops->init(my_test_device_p);
return RT_EOK;
}
void my_test_device_open(void)
{
my_test_device_p->ops->open(my_test_device_p,RT_DEVICE_OFLAG_RDWR );
}
void my_test_device_close(void)
{
my_test_device_p->ops->close(my_test_device_p);
}
//添加app初始化列表
INIT_APP_EXPORT(my_test_device_init);