一、前言
以STM32为例,打开网络上下载的例程或者是购买开发板自带的例程,都会发现应用层中会有stm32f10x.h或者stm32f10x_gpio.h,这些文件严格来时属于硬件层的,如果软件层出现这些文件会显得很乱。
使用过Linux的童鞋们肯定知道linux系统无法直接操作硬件层,打开linux或者rt_thread代码会发现代码中都会有device的源文件,没错,这就是驱动层。
二、实现原理
原理就是将硬件操作的接口全都放到驱动链表上,在驱动层实现device的open、read、write等操作。当然这样做也有弊端,就是驱动find的时候需要遍历一遍驱动链表,这样会增加代码运行时间。
三、代码实现
国际惯例,写代码先写头文件。rt_thread中使用的是双向链表,为了简单在这我只用单向链表。有兴趣的可以自行研究rt_thread
头文件接口:
本次只实现如下接口,device_open 和device_close等剩下的接口可以自行研究。这样就可以在应用层中只调用如下接口可实现:
/*
驱动注册
*/
int cola_device_register(cola_device_t *dev);
/*
驱动查找
*/
cola_device_t *cola_device_find(const char *name);
/*
驱动读
*/
int cola_device_read(cola_device_t *dev, int pos, void *buffer, int size);
/*
驱动写
*/
int cola_device_write(cola_device_t *dev, int pos, const void *buffer, int size);
/*
驱动控制
*/
int cola_device_ctrl(cola_device_t *dev, int cmd, void *arg);;
头文件cola_device.h:
#ifndef _COLA_DEVICE_H_
#define _COLA_DEVICE_H_
enum LED_state
{
LED_OFF,
LED_ON,
LED_TOGGLE,
};
typedef struct cola_device cola_device_t;
struct cola_device_ops
{
int (*init) (cola_device_t *dev);
int (*open) (cola_device_t *dev, int oflag);
int (*close) (cola_device_t *dev);
int (*read) (cola_device_t *dev, int pos, void *buffer, int size);
int (*write) (cola_device_t *dev, int pos, const void *buffer, int size);
int (*control)(cola_device_t *dev, int cmd, void *args);
};
struct cola_device
{
const char * name;
struct cola_device_ops *dops;
struct cola_device *next;
};
/*
驱动注册
*/
int cola_device_register(cola_device_t *dev);
/*
驱动查找
*/
cola_device_t *cola_device_find(const char *name);
/*
驱动读
*/
int cola_device_read(cola_device_t *dev, int pos, void *buffer, int size);
/*
驱动写
*/
int cola_device_write(cola_device_t *dev, int pos, const void *buffer, int size);
/*
驱动控制
*/
int cola_device_ctrl(cola_device_t *dev, int cmd, void *arg);
#endif
源文件cola_device.c:
#include "cola_device.h"
#include <string.h>
#include <stdbool.h>
struct cola_device *device_list = NULL;
/*
查找任务是否存在
*/
static bool cola_device_is_exists( cola_device_t *dev )
{
cola_device_t* cur = device_list;
while( cur != NULL )
{
if( strcmp(cur->name,dev->name)==0)
{
return true;
}
cur = cur->next;
}
return false;
}
static int device_list_inster(cola_device_t *dev)
{
cola_device_t *cur = device_list;
if(NULL == device_list)
{
device_list = dev;
dev->next = NULL;
}
else
{
while(NULL != cur->next)
{
cur = cur->next;
}
cur->next = dev;
dev->next = NULL;
}
return 1;
}
/*
驱动注册
*/
int cola_device_register(cola_device_t *dev)
{
if((NULL == dev) || (cola_device_is_exists(dev)))
{
return 0;
}
if((NULL == dev->name) || (NULL == dev->dops))
{
return 0;
}
return device_list_inster(dev);
}
/*
驱动查找
*/
cola_device_t *cola_device_find(const char *name)
{
cola_device_t* cur = device_list;
while( cur != NULL )
{
if( strcmp(cur->name,name)==0)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
/*
驱动读
*/
int cola_device_read(cola_device_t *dev, int pos, void *buffer, int size)
{
if(dev)
{
if(dev->dops->read)
{
return dev->dops->read(dev, pos, buffer, size);
}
}
return 0;
}
/*
驱动写
*/
int cola_device_write(cola_device_t *dev, int pos, const void *buffer, int size)
{
if(dev)
{
if(dev->dops->write)
{
return dev->dops->write(dev, pos, buffer, size);
}
}
return 0;
}
/*
驱动控制
*/
int cola_device_ctrl(cola_device_t *dev, int cmd, void *arg)
{
if(dev)
{
if(dev->dops->control)
{
return dev->dops->control(dev, cmd, arg);
}
}
return 0;
}
|