modify driver framework description
This commit is contained in:
parent
1b78625f92
commit
24fcbe421e
|
@ -1,458 +1,545 @@
|
||||||
# 驱动模型
|
# 驱动模型
|
||||||
|
|
||||||
## 概述
|
## 概述
|
||||||
多数嵌入式操作系统对驱动的抽象采用以物理外设实体为中心的方式,采用驱动(driver)-设备(device)的模式开发驱动。这种抽象带来的缺陷在面对一些兼容多平台的操作系统开发驱动时尤为突出。
|
多数嵌入式操作系统对驱动的抽象采用以物理外设实体为中心的方式,采用驱动(DRIVER)-设备(DEVICE)的模式开发驱动。这种抽象带来的缺陷在面对一些兼容多平台的操作系统开发驱动时尤为突出。
|
||||||
|
|
||||||
对于实时复杂的操作系统,代码的重用性非常重要,否则的话就会在操作系统内核中存在大量无意义的重复代码。尤其是驱动程序,因为驱动程序占用了操作系统代码量的绝大部分,如果不对驱动程序加以管理,任由重复的代码肆意增加,那么随着操作系统的发展,其文件数量就庞大到无法接受的地步,进而增加了驱动开发难度和周期。
|
对于实时复杂的操作系统,代码的重用性非常重要,否则的话就会在操作系统内核中存在大量无意义的重复代码。尤其是驱动程序,因为驱动程序占用了操作系统代码量的绝大部分,如果不对驱动程序加以管理,任由重复的代码肆意增加,那么随着操作系统的发展,其文件数量就庞大到无法接受的地步,进而增加了驱动开发难度和周期。
|
||||||
|
|
||||||
以I2C接口的ATH10的温湿度传感器驱动为例,每种平台下都有一个主机驱动和设备驱动,主机驱动是必要的,它随着每个平台的IIC控制器不同而不同。但是对于不同平台下的同一个设备ATH10(温湿度传感器),没必要每个平台都写一个设备驱动,因为不管对于哪个架构的 SOC 来说, ATH10温湿度传感器都是一样,通过I2C接口读写数据就行了,只需要公用一个ATH10的驱动程序即可。基于上述思想,XiuOS将驱动进行分隔设计以求简化驱动的开发,采用驱动(driver)-总线(bus)-设备(device)的架构对驱动进行管理和组织。对于同一类设备,每个平台的同种外设控制器都提供一个统一的接口,即主机驱动。每个设备的话也只提供一个驱动程序(设备驱动),每个设备通过统一的 I2C接口驱动来访问,这样就可以大大简化驱动文件。
|
针对这种情况,我们了解到对于嵌入式系统外设来说,不外乎几类常用的外设,如I2C外设、SPI外设、字符设备外设、块设备外设等,同一类外设使用的外设接口相同,这就意味着这类外设可以复用同一套DRIVER驱动代码和DEVICE操作代码。而对于上层应用来说,最理想的情况下是这类外设的接口控制器在不同SOC平台上可以复用,即应用程序通过统一的API接口操作外设,可以大大减少系统开发和维护工作量。
|
||||||
|
|
||||||
对于ATH10的温湿度传感器,我们只需要提供设备信息即可,比如设备连接到了哪个I2C 接口上,I2C的速度是多少等等。相当于将设备信息从设备驱动中剥离开来,驱动使用标准方法去获取到设备信息,然后根据获取到的设备信息来初始化设备。这样就相当于驱动只负责驱动,设备只负责设备,想办法将两者进行匹配即可,总线负责将驱动和设备信息链接起来。
|
因此,XiUOS集成了一套BUS驱动模型,即利用一套BUS总线管理DEVICE设备和DRIVER驱动的框架,以BUS为基类,分发多种BUS子类对应一类外设,覆盖目前常使用的外设接口,如SPI_BUS、I2C_BUS、SERIAL_BUS等。以SPI_BUS总线为例,SPI_BUS总线创建后,首先会注册SPI_DRIVER驱动相关实现,之后系统接入的所有SPI_DEVICE都会注册挂载到SPI_BUS总线上,DRIVER驱动和DEVICE设备通过bus_name绑定上,实现设备和驱动相互对应的配置,这样即可实现用一套BUS数据结构管理其挂载的DEVICE设备和DRIVER驱动的目的。对于上层应用来说,通过获取SPI_BUS数据结构即可知道当前系统中挂载的SPI_DEVICE个数,利用BUS回调函数便可满足配置、数据交互需求,而不用关心具体的硬件细节,且底层新增或删减外设的操作由框架层统一管理,上层应用模块依旧可以复用,这套框架层次化管理界限清晰,驱动和应用解耦、复用功能凸显,应用层开发友好。
|
||||||
|
|
||||||
|
## 目录结构和框图
|
||||||
|
* 驱动框架resources/xxx/文件目录结构
|
||||||
|
|
||||||
其中 xs_Bus 结构体被设计为可以采用类似面向对象的方法,它集合了所有总线的共有特征,为总线的基类。具体的总线以它为基类进行派生,如 xs_IIC_bus 、xs_USB_bus 、xs_SPI_bus 、xs_CAN_bus等。从而为众多不同性质的总线架构实现统一的管理架构。在应用开发的过程中只需要使用对应外设的总线实例,无需关心传感器的硬件细节,从而实现数据采样功能与底层硬件的解耦。
|
| 文件名 | 描述 |
|
||||||
|
| --- | --- |
|
||||||
|
| bus.c | bus驱动框架实现,提供给上层应用调用 |
|
||||||
|
| bus_xxx.c | xxx_bus子类实现,如spi_bus、i2c_bus、serial_bus等 |
|
||||||
|
| dev_xxx.c | xxx_dev子类实现,如spi_dev、i2c_dev、serial_dev等 |
|
||||||
|
| drv_xxx.c | xxx_drv子类实现,如spi_drv、i2c_drv、serial_drv等 |
|
||||||
|
|
||||||
xs_Device 结构体同样采用类似面向对象的方法,它集合了所有设备的共有特征,为设备的基类。平台上接入的具体设备以它为基类进行派生,如 IICHardwareDevice 、USBHardwareDevice 、SPIHardwareDevice 、CANHardwareDevice等。从而为众多不同性质的设备实现统一的管理架构。
|
* 驱动框架框图(以SPI_Bus为例)
|
||||||
|
|
||||||
|
<img src="./imagesrc/spi_bus.png" width =100%/>
|
||||||
|
|
||||||
## 关键数据结构
|
## 关键数据结构
|
||||||
|
|
||||||
* struct xs_Bus结构
|
* struct Bus结构
|
||||||
```c
|
```c
|
||||||
struct xs_Bus {
|
struct Bus
|
||||||
char Bus_name[XS_NAME_MAX]; /* name of bus */
|
{
|
||||||
enum xs_BusType type; /* type of bus */
|
int8 bus_name[NAME_NUM_MAX];
|
||||||
enum xs_BUSSTATE flag;
|
enum BusType bus_type;
|
||||||
list_head device;
|
enum BusState bus_state;
|
||||||
list_head driver;
|
|
||||||
int (*match)(const struct xs_DeviceDriver * drv,const struct xs_Device dev);
|
|
||||||
int (*probe)(void * bus);
|
|
||||||
int (*remove)(void * bus);
|
|
||||||
int (*shutdown)(void * bus);
|
|
||||||
int (*resume)(void * bus);
|
|
||||||
|
|
||||||
int (bus_register)(void * bus);
|
int32 (*match)(struct Driver *driver, struct HardwareDev *device);
|
||||||
int (bus_unregister)(void * bus);
|
|
||||||
void * private_data;
|
|
||||||
};
|
|
||||||
```
|
|
||||||
Bus_name成员是一个可读的名字,用于唯一标识一个xs_Bus结构,
|
|
||||||
type成员表示该xs_Bus为什么类型的总线,用一个枚举变量表示。
|
|
||||||
```c
|
|
||||||
enum xs_BusType {
|
|
||||||
XS_IIC_BUS = 0, /* IIC */
|
|
||||||
XS_SPI_BUS, /* SPI */
|
|
||||||
XS_USB_BUS, /* USB */
|
|
||||||
XS_CAN_BUS, /* CAN */
|
|
||||||
/* ...... */
|
|
||||||
XS_BUS_END,
|
|
||||||
};
|
|
||||||
```
|
|
||||||
xs_BusType成员表示不同的总线类型,其具体定义在上文给出。
|
|
||||||
|
|
||||||
|
int bus_lock;
|
||||||
|
|
||||||
* enum Bus_priority枚举结构
|
struct HardwareDev *owner_haldev;
|
||||||
```c
|
struct Driver *owner_driver;
|
||||||
enum Bus_priority{
|
|
||||||
PriorityLevelOne = 1,
|
|
||||||
PriorityLevelTwo,
|
|
||||||
PriorityLevelThree,
|
|
||||||
PriorityLevelforth,
|
|
||||||
PriorityLevelFive,
|
|
||||||
PriorityLevelSix,
|
|
||||||
PriorityLevelSeven
|
|
||||||
};
|
|
||||||
```
|
|
||||||
Bus_priority变量表示总线优先级,数值越低优先级越高
|
|
||||||
|
|
||||||
|
|
||||||
* struct xs_IIC_bus结构
|
|
||||||
```c
|
|
||||||
struct xs_I2CBus {
|
|
||||||
struct xs_Bus bus; /* Basic properties of the bus */
|
|
||||||
unsigned int speed; /* IIC communication rate*/
|
|
||||||
};
|
|
||||||
```
|
|
||||||
speed成员记录I2C总线与传感器设备通信的速率
|
|
||||||
|
|
||||||
* struct xs_SPIBus结构
|
|
||||||
```c
|
|
||||||
struct xs_SPIBus {
|
|
||||||
struct xs_Bus bus; /* Basic properties of the bus */
|
|
||||||
enum Bus_priority priority /* Bus priority */
|
|
||||||
unsigned char CPHA; /* SPI clock phase*/
|
|
||||||
unsigned char CPOL; /* SPI clock polarity*/
|
|
||||||
};
|
|
||||||
```
|
|
||||||
CPHA成员记录SPI总线的时钟相位,CPOL成员记录SPI总线的时钟极性
|
|
||||||
|
|
||||||
* struct xs_CANBus结构
|
|
||||||
```c
|
|
||||||
struct xs_CAN_bus {
|
|
||||||
struct xs_Bus bus; /* Basic properties of the bus */
|
|
||||||
enum Bus_priority priority /* Bus priority */
|
|
||||||
unsigned int speed; /* CAN communication rate*/
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
* struct xs_USBBus结构
|
|
||||||
```c
|
|
||||||
struct xs_USB_bus {
|
|
||||||
struct xs_Bus bus; /* Basic properties of the bus */
|
|
||||||
enum Bus_priority priority /* Bus priority */
|
|
||||||
unsigned int speed; /* USB communication rate*/
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
* struct xs_Device结构
|
|
||||||
```c
|
|
||||||
typedef struct xs_Device {
|
|
||||||
char dev_name[XS_NAME_MAX]; /* name of device */
|
|
||||||
enum xs_DeviceType type;
|
|
||||||
struct mutex mut; /* Mutually Exclusive Semaphore*/
|
|
||||||
xs_uint32 ref_count; /* the number of open */
|
|
||||||
void * driver; /* Mounted driver*/
|
|
||||||
struct device_ops ops; /* Device operation function set */
|
|
||||||
struct xs_Device * prev;
|
|
||||||
struct xs_Device * next;
|
|
||||||
void * data;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
typedef struct xs_Device * xs_DevicePointer ;
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
* struct device_ops结构
|
|
||||||
```c
|
|
||||||
struct device_ops{
|
|
||||||
int (*init)(struct xs_Device * dev);
|
|
||||||
int (*open)(struct xs_Device * dev);
|
|
||||||
int (*close)(struct xs_Device * dev);
|
|
||||||
int (*read)(struct xs_Device * dev, void * buf, int unm);
|
|
||||||
int (*write)(struct xs_Device * dev, void *buf, int num);
|
|
||||||
int (*ioctl)(struct xs_Device * dev, int cmd);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
device_ops包含统一的、类似文件系统的API,用于对具体外设进行实际的数据读写。如在使用一个传感器前后需要打开(open)/关闭(close)该传感器,read、write分别用与从传感器接收数据与向传感器发送数据,ioctl用于配置传感器属性(如波特率)
|
|
||||||
|
|
||||||
* struct xs_HardwareDev结构
|
|
||||||
```c
|
|
||||||
struct xs_HardwareDev{
|
|
||||||
struct xs_Device dev;
|
|
||||||
struct device_ops ops;
|
|
||||||
|
|
||||||
int (*probe)(struct xs_Device * dev);
|
|
||||||
int (*remove)(struct xs_Device * dev);
|
|
||||||
void (*shutdown)(struct xs_Device * dev);
|
|
||||||
int (*suspend)(struct xs_Device * dev, int state);
|
|
||||||
int (*resume)(struct xs_Device * dev);
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
* struct IICHardwareDevice结构
|
|
||||||
```c
|
|
||||||
struct IICHardwareDevice{
|
|
||||||
struct xs_HardwareDev hardware;
|
|
||||||
unsigned short IIC_addr;
|
|
||||||
void * PrivData;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
* struct SPIHardwareDevice结构
|
|
||||||
```c
|
|
||||||
struct SPIHardwareDevice{
|
|
||||||
struct xs_HardwareDev hardware;
|
|
||||||
unsigned short SPI_addr;
|
|
||||||
void * PrivData;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
* struct CANHardwareDevice结构
|
|
||||||
```c
|
|
||||||
struct CANHardwareDevice{
|
|
||||||
struct xs_HardwareDev hardware;
|
|
||||||
unsigned short CAN_addr;
|
|
||||||
void * PrivData;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
* struct USBHardwareDevice结构
|
|
||||||
```c
|
|
||||||
struct USBHardwareDevice{
|
|
||||||
struct xs_HardwareDev hardware;
|
|
||||||
unsigned short USB_addr;
|
|
||||||
void * PrivData;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
* struct xs_DeviceDriver结构
|
|
||||||
```c
|
|
||||||
struct xs_DeviceDriver{
|
|
||||||
char driver_name[XS_NAME_MAX];
|
|
||||||
struct bus_type * bus;
|
|
||||||
enum driver_state falg;
|
|
||||||
|
|
||||||
int (*add)(struct xs_DeviceDriver *dev);
|
void *private_data;
|
||||||
int (*remove)(struct xs_DeviceDriver *dev);
|
|
||||||
int (*probe)(struct xs_HardwareDev *dev);
|
|
||||||
void (*shutdown)(struct xs_HardwareDev *dev);
|
|
||||||
struct list_drv{
|
|
||||||
struct xs_DeviceDriver * prev;
|
|
||||||
struct xs_DeviceDriver * next;
|
|
||||||
void * data;
|
|
||||||
};
|
|
||||||
} Device_driver_head;
|
|
||||||
```
|
|
||||||
|
|
||||||
|
/*manage the drv of the bus*/
|
||||||
|
uint8 driver_cnt;
|
||||||
|
uint8 bus_drvlink_flag;
|
||||||
|
DoubleLinklistType bus_drvlink;
|
||||||
|
|
||||||
|
/*manage the dev of the bus*/
|
||||||
|
uint8 haldev_cnt;
|
||||||
|
uint8 bus_devlink_flag;
|
||||||
|
DoubleLinklistType bus_devlink;
|
||||||
|
|
||||||
|
uint8 bus_cnt;
|
||||||
|
uint8 bus_link_flag;
|
||||||
|
DoubleLinklistType bus_link;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
* enum BusType枚举结构
|
||||||
|
```c
|
||||||
|
enum BusType
|
||||||
|
{
|
||||||
|
TYPE_I2C_BUS = 0,
|
||||||
|
TYPE_SPI_BUS,
|
||||||
|
TYPE_HWTIMER_BUS,
|
||||||
|
TYPE_USB_BUS,
|
||||||
|
TYPE_CAN_BUS,
|
||||||
|
TYPE_WDT_BUS,
|
||||||
|
TYPE_SDIO_BUS,
|
||||||
|
TYPE_TOUCH_BUS,
|
||||||
|
TYPE_LCD_BUS,
|
||||||
|
TYPE_PIN_BUS,
|
||||||
|
TYPE_RTC_BUS,
|
||||||
|
TYPE_SERIAL_BUS,
|
||||||
|
TYPE_BUS_END,
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
* enum BusState枚举结构
|
||||||
|
```c
|
||||||
|
enum BusState
|
||||||
|
{
|
||||||
|
BUS_INIT = 0,
|
||||||
|
BUS_INSTALL,
|
||||||
|
BUS_UNINSTALL,
|
||||||
|
};
|
||||||
|
```
|
||||||
|
* struct HardwareDev结构
|
||||||
|
```c
|
||||||
|
struct HardwareDev
|
||||||
|
{
|
||||||
|
int8 dev_name[NAME_NUM_MAX];
|
||||||
|
enum DevType dev_type;
|
||||||
|
enum DevState dev_state;
|
||||||
|
|
||||||
|
const struct HalDevDone *dev_done;
|
||||||
|
|
||||||
|
int (*dev_recv_callback) (void *dev, x_size_t length);
|
||||||
|
int (*dev_block_control) (struct HardwareDev *dev, struct HalDevBlockParam *block_param);
|
||||||
|
|
||||||
|
struct Bus *owner_bus;
|
||||||
|
void *private_data;
|
||||||
|
|
||||||
|
int32 dev_sem;
|
||||||
|
|
||||||
|
DoubleLinklistType dev_link;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
* struct HalDevDone结构
|
||||||
|
```c
|
||||||
|
struct HalDevDone
|
||||||
|
{
|
||||||
|
uint32 (*open) (void *dev);
|
||||||
|
uint32 (*close) (void *dev);
|
||||||
|
uint32 (*write) (void *dev, struct BusBlockWriteParam *write_param);
|
||||||
|
uint32 (*read) (void *dev, struct BusBlockReadParam *read_param);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
HalDevDone包含统一的、类似文件系统的API,用于对具体外设进行实际的开关和数据读写。如在使用一个外设前后需要打开(open)/关闭(close)该外设,read、write分别用与从外设接收数据与向外设发送数据。
|
||||||
|
* struct Driver结构
|
||||||
|
```c
|
||||||
|
struct Driver
|
||||||
|
{
|
||||||
|
int8 drv_name[NAME_NUM_MAX];
|
||||||
|
enum DriverType driver_type;
|
||||||
|
enum DriverState driver_state;
|
||||||
|
|
||||||
|
uint32 (*configure)(void *drv, struct BusConfigureInfo *configure_info);
|
||||||
|
|
||||||
|
struct Bus *owner_bus;
|
||||||
|
void *private_data;
|
||||||
|
|
||||||
|
DoubleLinklistType driver_link;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
回调函数configure用于对具体外设进行实际的配置。
|
||||||
|
|
||||||
## 使用场景
|
## 使用场景
|
||||||
在获取i2c数据前,要匹配设备驱动,在设备驱动打开后对设备进行读写。使用完毕后,关闭设备驱动。
|
在获取外设数据前,要先获取外设bus,匹配driver和device数据结构,若有需要应配置外设driver,在外设device打开后对外设进行读写,使用完毕后,关闭设备。
|
||||||
|
|
||||||
完整的使用过程示例如下:
|
以SERIAL为例,完整的使用过程示例如下:
|
||||||
```c
|
```c
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int ret;
|
int ret = EOK;
|
||||||
struct xs_I2cDevice *dev;
|
char test_str[] = "Hello AIIT!\r\n";
|
||||||
|
|
||||||
/* find the i2c device instance */
|
struct Bus *bus;
|
||||||
dev = (struct xs_I2cDevice*)xs_DeviceFind("i2c_temp",XS_IIC_BUS); //获取设备句柄
|
struct HardwareDev *dev;
|
||||||
if(dev == NULL)
|
struct Driver *drv;
|
||||||
{
|
|
||||||
xs_kprintf("find iic device error\n");
|
/* find the serial bus pointer */
|
||||||
|
bus = BusFind(SERIAL_BUS_NAME);
|
||||||
|
if (NONE == bus) {
|
||||||
|
KPrintf("BusFind %s failed\n", SERIAL_BUS_NAME);
|
||||||
|
return ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* find the serial driver pointer */
|
||||||
|
drv = BusFindDriver(bus, SERIAL_DRV_NAME);
|
||||||
|
if (NONE == drv) {
|
||||||
|
KPrintf("BusFindDriver %s failed\n", SERIAL_DRV_NAME);
|
||||||
|
return ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* find the serial device pointer */
|
||||||
|
dev = BusFindDevice(bus, SERIAL_DEVICE_NAME);
|
||||||
|
if (NONE == dev) {
|
||||||
|
KPrintf("BusFindDevice %s failed\n", SERIAL_DEVICE_NAME);
|
||||||
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* open the device instance */
|
/*step 1: init bus_driver, change struct SerialCfgParam if necessary*/
|
||||||
ret = xs_DeviceOpen(dev); //打开设备
|
struct SerialCfgParam serial_cfg;
|
||||||
XS_ASSERT(ret == XS_EOK);
|
memset(&serial_cfg, 0, sizeof(struct SerialCfgParam));
|
||||||
|
configure_info.configure_cmd = OPE_INT;
|
||||||
|
configure_info.private_data = &serial_cfg;
|
||||||
|
ret = BusDrvConfigure(drv, &configure_info);
|
||||||
|
if (EOK != ret) {
|
||||||
|
KPrintf("BusDrvConfigure OPE_INT failed error code %d\n", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/* read temperature sensor data for 5 times */
|
/*step 2: match serial bus_driver with bus_device*/
|
||||||
for (int i = 0; i < 5; i++)
|
bus->match(drv, dev);
|
||||||
xs_kprintf("Current CO2 concentration is %u ppm\n", xs_DeviceRead(dev)); //读取设备数据
|
|
||||||
xs_DeviceClose(dev); //关闭数据
|
|
||||||
|
|
||||||
return 0;
|
/*step 3: open bus_device, configure struct SerialDevParam if necessary*/
|
||||||
|
serial_dev_param->serial_set_mode = SIGN_OPER_INT_RX;
|
||||||
|
serial_dev_param->serial_stream_mode = SIGN_OPER_STREAM;
|
||||||
|
ret = BusDevOpen(dev);
|
||||||
|
if (EOK != ret) {
|
||||||
|
KPrintf("BusDevOpen failed error code %d\n", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*step 4: write serial data, configure struct BusBlockWriteParam*/
|
||||||
|
struct BusBlockWriteParam write_param;
|
||||||
|
write_param.pos = 0;
|
||||||
|
write_param.buffer = (void *)test_str;
|
||||||
|
write_param.size = sizeof(test_str) - 1;
|
||||||
|
BusDevWriteData(bus_device, &write_param);
|
||||||
|
|
||||||
|
/*step 5: close bus_device*/
|
||||||
|
BusDevClose(bus_device);
|
||||||
|
|
||||||
|
return EOK;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 函数接口
|
## 函数接口
|
||||||
### 设备初始化函数
|
* BUS注册函数
|
||||||
```c
|
```c
|
||||||
/**
|
/**
|
||||||
* This function will initialize the specified device
|
* @Description: support to register bus pointer with linklist
|
||||||
*
|
* @param bus - bus pointer
|
||||||
* @param dev the pointer of device driver structure
|
* @return successful:EOK,failed:NONE
|
||||||
*
|
|
||||||
* @return the result
|
|
||||||
*/
|
|
||||||
xs_err_t xs_DeviceInit(xs_DevicePointer dev)
|
|
||||||
{
|
|
||||||
xs_err_t result = XS_EOK;
|
|
||||||
|
|
||||||
XS_ASSERT(dev != XS_NULL);
|
|
||||||
|
|
||||||
/*driver init*/
|
|
||||||
if(dev->driver && dev->driver->flag & DRIVER_FLAG_MOUNTED){
|
|
||||||
device_register(dev)
|
|
||||||
if (!(dev->flag & DEVICE_FLAG_ACTIVATED))
|
|
||||||
{
|
|
||||||
result = device_init(dev);/* Macro */
|
|
||||||
if (result != XS_EOK)
|
|
||||||
{
|
|
||||||
xs_kprintf("Initialize device:%s failed. The error code is %d\n",
|
|
||||||
dev->name, result);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
dev->flag |= XS_DEVICE_FLAG_ACTIVATED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}else{
|
|
||||||
return DRIVER_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 设备查找函数
|
|
||||||
|
|
||||||
```c
|
|
||||||
/**
|
|
||||||
* This function finds a device driver by specified name.
|
|
||||||
*/
|
|
||||||
xs_DevicePointer xs_DeviceFind(const char *name)
|
|
||||||
{
|
|
||||||
enum xs_BusType e;
|
|
||||||
xs_DevicePointer dev;
|
|
||||||
for(e=0;e !=XS_BUS_END; ++e){
|
|
||||||
if(dev = __xs_DeviceFind_by_type(name,e) && dev !=BUS_ERROR)
|
|
||||||
return dev;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
xs_DevicePointer xs_DeviceFind(const char *name,enum xs_BusType bus_type){
|
|
||||||
return __xs_DeviceFind_by_type(name,bus_type);
|
|
||||||
};
|
|
||||||
|
|
||||||
xs_DevicePointer __xs_DeviceFind_by_type(const char *name,enum xs_BusType bus_type)
|
|
||||||
{
|
|
||||||
struct xs_Device *device;
|
|
||||||
struct xs_ListNode *node;
|
|
||||||
|
|
||||||
xs_Bus bus = get_bus_from_type(bus_type);
|
|
||||||
if(!(bus && bus->falg = BUS_FLAG_ACTIVED))
|
|
||||||
return BUS_ERROR;/*a point to speical area */
|
|
||||||
|
|
||||||
/* enter critical */
|
|
||||||
if (xs_get_kthread_descriptor() != XS_NULL)
|
|
||||||
xs_critical_enter();
|
|
||||||
|
|
||||||
/* try to find device */
|
|
||||||
for (node = bus->device.next;
|
|
||||||
node != &(bus->device);
|
|
||||||
node = node->next)
|
|
||||||
{
|
|
||||||
device = xs_list_entry(node, struct xs_Device, list);
|
|
||||||
if (xs_strncmp(device->name, name, XS_NAME_MAX) == 0)
|
|
||||||
{
|
|
||||||
/* leave critical */
|
|
||||||
if (xs_get_kthread_descriptor() != XS_NULL)
|
|
||||||
xs_critical_exit();
|
|
||||||
|
|
||||||
return (xs_DevicePointer)device;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* leave critical */
|
|
||||||
if (xs_get_kthread_descriptor() != XS_NULL)
|
|
||||||
xs_critical_exit();
|
|
||||||
|
|
||||||
/* not found */
|
|
||||||
return XS_NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
### 设备打开函数
|
|
||||||
```c
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This function open a device
|
|
||||||
*/
|
*/
|
||||||
xs_err_t xs_DeviceOpen(xs_DevicePointer dev)
|
int BusRegister(struct Bus *bus)
|
||||||
{
|
{
|
||||||
xs_err_t result = XS_EOK;
|
x_err_t ret = EOK;
|
||||||
|
NULL_PARAM_CHECK(bus);
|
||||||
|
|
||||||
XS_ASSERT(dev != XS_NULL);
|
bus->match = BusMatchDrvDev;
|
||||||
if(!(dev->bus))
|
|
||||||
return BUS_ERROR;
|
|
||||||
if(!(dev->driver && dev->bus->match(dev->driver,dev)))
|
|
||||||
return DRIVER_ERROR;
|
|
||||||
|
|
||||||
/* if device is not initialized, initialize it. */
|
BusLinkInit(bus);
|
||||||
if (!(dev->flag & DEVICE_FLAG_ACTIVATED))
|
|
||||||
{
|
|
||||||
xs_DeviceInit(dev);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* call device_open interface */
|
bus->bus_lock = KMutexCreate();
|
||||||
if (device_open != XS_NULL)
|
|
||||||
{
|
|
||||||
result = device_open(dev, oflag);
|
|
||||||
dev->ref_count++;
|
|
||||||
XS_ASSERT(dev->ref_count != 0);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
### 设备关闭函数
|
|
||||||
|
|
||||||
```c
|
DoubleLinkListInsertNodeAfter(&bus_linklist, &(bus->bus_link));
|
||||||
xs_err_t xs_DeviceClose(xs_DevicePointer dev)
|
|
||||||
{
|
|
||||||
xs_err_t result = XS_EOK;
|
|
||||||
|
|
||||||
XS_ASSERT(dev != XS_NULL);
|
return ret;
|
||||||
|
|
||||||
if (dev->ref_count == 0)
|
|
||||||
return XS_ERROR;
|
|
||||||
|
|
||||||
dev->ref_count--;
|
|
||||||
|
|
||||||
if (dev->ref_count != 0)
|
|
||||||
return XS_EOK;
|
|
||||||
|
|
||||||
/* call device_close interface */
|
|
||||||
if (device_close != XS_NULL)
|
|
||||||
{
|
|
||||||
result = device_close(dev);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* BUS删除函数
|
||||||
|
|
||||||
## i2c设备注册与卸载
|
|
||||||
```c
|
```c
|
||||||
int int xs_I2cDeviceRegister(struct xs_I2cDevice *dev,const char *name, xs_uint16 flags);
|
/**
|
||||||
int xs_I2cDeviceunRegister(struct xs_I2cDevice *dev);
|
* @Description: support to unregister bus pointer and delete its linklist node
|
||||||
|
* @param bus - bus pointer
|
||||||
|
* @return successful:EOK,failed:NONE
|
||||||
|
*/
|
||||||
|
int BusUnregister(struct Bus *bus)
|
||||||
|
{
|
||||||
|
NULL_PARAM_CHECK(bus);
|
||||||
|
|
||||||
|
bus->bus_cnt--;
|
||||||
|
|
||||||
|
DoubleLinkListRmNode(&(bus->bus_link));
|
||||||
|
|
||||||
|
return EOK;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### xs_I2cDeviceRegister函数具体实现
|
* DRIVER注册到BUS函数
|
||||||
```c
|
```c
|
||||||
int xs_I2cDeviceRegister(struct xs_I2cDevice *dev,const char *name, xs_uint16 flags)
|
/**
|
||||||
|
* @Description: support to register driver pointer to bus pointer
|
||||||
|
* @param bus - bus pointer
|
||||||
|
* @param driver - driver pointer
|
||||||
|
* @return successful:EOK,failed:NONE
|
||||||
|
*/
|
||||||
|
int DriverRegisterToBus(struct Bus *bus, struct Driver *driver)
|
||||||
{
|
{
|
||||||
if(dev != NULL)
|
NULL_PARAM_CHECK(bus);
|
||||||
{
|
NULL_PARAM_CHECK(driver);
|
||||||
return xs_DeviceRegister(dev,name,0);
|
|
||||||
}
|
driver->owner_bus = bus;
|
||||||
return -XS_ERROR;
|
bus->driver_cnt++;
|
||||||
|
|
||||||
|
DoubleLinkListInsertNodeAfter(&bus->bus_drvlink, &(driver->driver_link));
|
||||||
|
|
||||||
|
return EOK;
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
xs_err_t xs_DeviceRegister(void * dev,const char *name, xs_uint16 flags)
|
* DRIVER从BUS中删除函数
|
||||||
|
```c
|
||||||
|
/**
|
||||||
|
* @Description: support to delete driver pointer from bus pointer
|
||||||
|
* @param bus - bus pointer
|
||||||
|
* @param driver - driver pointer
|
||||||
|
* @return successful:EOK,failed:NONE
|
||||||
|
*/
|
||||||
|
int DriverDeleteFromBus(struct Bus *bus, struct Driver *driver)
|
||||||
{
|
{
|
||||||
|
NULL_PARAM_CHECK(bus);
|
||||||
|
NULL_PARAM_CHECK(driver);
|
||||||
|
|
||||||
if (dev == XS_NULL)
|
bus->driver_cnt--;
|
||||||
return -XS_ERROR;
|
|
||||||
|
|
||||||
if (xs_DeviceFind(name) != XS_NULL)
|
DoubleLinkListRmNode(&(driver->driver_link));
|
||||||
return -XS_ERROR;
|
|
||||||
|
|
||||||
xs_CriticalEnter();
|
free(driver);
|
||||||
for (node = Device_driver_head.NodeNext;
|
|
||||||
node != &(xiaoshan_device_head);
|
return EOK;
|
||||||
node = node->NodeNext)
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* DEVICE注册到BUS函数
|
||||||
|
```c
|
||||||
|
/**
|
||||||
|
* @Description: support to register dev pointer to bus pointer
|
||||||
|
* @param bus - bus pointer
|
||||||
|
* @param device - device pointer
|
||||||
|
* @return successful:EOK,failed:NONE
|
||||||
|
*/
|
||||||
|
int DeviceRegisterToBus(struct Bus *bus, struct HardwareDev *device)
|
||||||
|
{
|
||||||
|
NULL_PARAM_CHECK(bus);
|
||||||
|
NULL_PARAM_CHECK(device);
|
||||||
|
|
||||||
|
device->owner_bus = bus;
|
||||||
|
bus->haldev_cnt++;
|
||||||
|
|
||||||
|
DoubleLinkListInsertNodeAfter(&bus->bus_devlink, &(device->dev_link));
|
||||||
|
|
||||||
|
return EOK;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* DEVICE从BUS中删除函数
|
||||||
|
```c
|
||||||
|
/**
|
||||||
|
* @Description: support to delete dev pointer from bus pointer
|
||||||
|
* @param bus - bus pointer
|
||||||
|
* @param device - device pointer
|
||||||
|
* @return successful:EOK,failed:NONE
|
||||||
|
*/
|
||||||
|
int DeviceDeleteFromBus(struct Bus *bus, struct HardwareDev *device)
|
||||||
|
{
|
||||||
|
NULL_PARAM_CHECK(bus);
|
||||||
|
NULL_PARAM_CHECK(device);
|
||||||
|
|
||||||
|
bus->haldev_cnt--;
|
||||||
|
|
||||||
|
DoubleLinkListRmNode(&(device->dev_link));
|
||||||
|
|
||||||
|
free(device);
|
||||||
|
|
||||||
|
return EOK;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* 查找BUS函数
|
||||||
|
```c
|
||||||
|
/**
|
||||||
|
* @Description: support to find bus pointer by bus name
|
||||||
|
* @param bus_name - bus name
|
||||||
|
* @return successful:bus pointer,failed:NONE
|
||||||
|
*/
|
||||||
|
BusType BusFind(const char *bus_name)
|
||||||
|
{
|
||||||
|
struct Bus *bus = NONE;
|
||||||
|
|
||||||
|
DoubleLinklistType *node = NONE;
|
||||||
|
DoubleLinklistType *head = &bus_linklist;
|
||||||
|
|
||||||
|
for (node = head->node_next; node != head; node = node->node_next)
|
||||||
{
|
{
|
||||||
struct xs_Device *device;
|
bus = SYS_DOUBLE_LINKLIST_ENTRY(node, struct Bus, bus_link);
|
||||||
|
if(!strcmp(bus->bus_name, bus_name)) {
|
||||||
device = XS_DOUBLE_LINKLIST_ENTRY(node, struct xs_Device, link);
|
return bus;
|
||||||
if (device)
|
|
||||||
{
|
|
||||||
XS_ASSERT(device != dev);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
xs_CriticalExit();
|
|
||||||
xs_strncpy(dev->dev_string, name, XS_NAME_MAX);
|
KPrintf("BusFind cannot find the %s bus.return NULL\n", bus_name);
|
||||||
register xs_base temp = xs_DisableHwInterrupt();
|
return NONE;
|
||||||
{
|
|
||||||
xs_DoubleLinkListInsertNodeAfter(&xiaoshan_device_head, &(dev->link));
|
|
||||||
}
|
|
||||||
xs_EnableHwInterrupt(temp);
|
|
||||||
dev->sign = flags;
|
|
||||||
dev->call_cnt = 0;
|
|
||||||
dev->status = 0;
|
|
||||||
xs_InitWqueue(&(dev->wait_queue));
|
|
||||||
return XS_EOK;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* 查找DRIVER函数
|
||||||
|
```c
|
||||||
|
/**
|
||||||
|
* @Description: support to find driver pointer of certain bus by driver name
|
||||||
|
* @param bus - bus pointer
|
||||||
|
* @param driver_name - driver name
|
||||||
|
* @return successful:EOK,failed:NONE
|
||||||
|
*/
|
||||||
|
DriverType BusFindDriver(struct Bus *bus, const char *driver_name)
|
||||||
|
{
|
||||||
|
NULL_PARAM_CHECK(bus);
|
||||||
|
struct Driver *driver = NONE;
|
||||||
|
|
||||||
|
DoubleLinklistType *node = NONE;
|
||||||
|
DoubleLinklistType *head = &bus->bus_drvlink;
|
||||||
|
|
||||||
|
for (node = head->node_next; node != head; node = node->node_next)
|
||||||
|
{
|
||||||
|
driver = SYS_DOUBLE_LINKLIST_ENTRY(node, struct Driver, driver_link);
|
||||||
|
if(!strcmp(driver->drv_name, driver_name)) {
|
||||||
|
return driver;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
KPrintf("BusFindDriver cannot find the %s driver.return NULL\n", driver_name);
|
||||||
|
return NONE;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* 查找DEVICE函数
|
||||||
|
```c
|
||||||
|
/**
|
||||||
|
* @Description: support to find device pointer of certain bus by device name
|
||||||
|
* @param bus - bus pointer
|
||||||
|
* @param device_name - device name
|
||||||
|
* @return successful:EOK,failed:NONE
|
||||||
|
*/
|
||||||
|
HardwareDevType BusFindDevice(struct Bus *bus, const char *device_name)
|
||||||
|
{
|
||||||
|
NULL_PARAM_CHECK(bus);
|
||||||
|
struct HardwareDev *device = NONE;
|
||||||
|
|
||||||
|
DoubleLinklistType *node = NONE;
|
||||||
|
DoubleLinklistType *head = &bus->bus_devlink;
|
||||||
|
|
||||||
|
for (node = head->node_next; node != head; node = node->node_next)
|
||||||
|
{
|
||||||
|
device = SYS_DOUBLE_LINKLIST_ENTRY(node, struct HardwareDev, dev_link);
|
||||||
|
|
||||||
|
if(!strcmp(device->dev_name, device_name)) {
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
KPrintf("BusFindDevice cannot find the %s device.return NULL\n", device_name);
|
||||||
|
return NONE;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* 打开DEVICE函数
|
||||||
|
```c
|
||||||
|
/**
|
||||||
|
* @Description: support to open dev
|
||||||
|
* @param dev - dev pointer
|
||||||
|
* @return successful:EOK,failed:ERROR
|
||||||
|
*/
|
||||||
|
uint32 BusDevOpen(struct HardwareDev *dev)
|
||||||
|
{
|
||||||
|
NULL_PARAM_CHECK(dev);
|
||||||
|
|
||||||
|
x_err_t ret = EOK;
|
||||||
|
|
||||||
|
if (dev->dev_done->open) {
|
||||||
|
ret = dev->dev_done->open(dev);
|
||||||
|
if(ret) {
|
||||||
|
KPrintf("BusDevOpen error ret %u\n", ret);
|
||||||
|
return ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* 关闭DEVICE函数
|
||||||
|
```c
|
||||||
|
/**
|
||||||
|
* @Description: support to close dev
|
||||||
|
* @param dev - dev pointer
|
||||||
|
* @return successful:EOK,failed:ERROR
|
||||||
|
*/
|
||||||
|
uint32 BusDevClose(struct HardwareDev *dev)
|
||||||
|
{
|
||||||
|
NULL_PARAM_CHECK(dev);
|
||||||
|
|
||||||
|
x_err_t ret = EOK;
|
||||||
|
|
||||||
|
if (dev->dev_done->close) {
|
||||||
|
ret = dev->dev_done->close(dev);
|
||||||
|
if(ret) {
|
||||||
|
KPrintf("BusDevClose error ret %u\n", ret);
|
||||||
|
return ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* DEVICE写函数
|
||||||
|
```c
|
||||||
|
/**
|
||||||
|
* @Description: support to write data to dev
|
||||||
|
* @param dev - dev pointer
|
||||||
|
* @param write_param - BusBlockWriteParam
|
||||||
|
* @return successful:EOK,failed:NONE
|
||||||
|
*/
|
||||||
|
uint32 BusDevWriteData(struct HardwareDev *dev, struct BusBlockWriteParam *write_param)
|
||||||
|
{
|
||||||
|
NULL_PARAM_CHECK(dev);
|
||||||
|
|
||||||
|
if (dev->dev_done->write) {
|
||||||
|
return dev->dev_done->write(dev, write_param);
|
||||||
|
}
|
||||||
|
|
||||||
|
return EOK;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* DEVICE读函数
|
||||||
|
```c
|
||||||
|
/**
|
||||||
|
* @Description: support to read data from dev
|
||||||
|
* @param dev - dev pointer
|
||||||
|
* @param read_param - BusBlockReadParam
|
||||||
|
* @return successful:EOK,failed:NONE
|
||||||
|
*/
|
||||||
|
uint32 BusDevReadData(struct HardwareDev *dev, struct BusBlockReadParam *read_param)
|
||||||
|
{
|
||||||
|
NULL_PARAM_CHECK(dev);
|
||||||
|
|
||||||
|
if (dev->dev_done->read) {
|
||||||
|
return dev->dev_done->read(dev, read_param);
|
||||||
|
}
|
||||||
|
|
||||||
|
return EOK;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* DRIVER配置函数
|
||||||
|
```c
|
||||||
|
/**
|
||||||
|
* @Description: support to configure drv, include OPE_CFG and OPE_INT
|
||||||
|
* @param drv - drv pointer
|
||||||
|
* @param configure_info - BusConfigureInfo
|
||||||
|
* @return successful:EOK,failed:NONE
|
||||||
|
*/
|
||||||
|
uint32 BusDrvConfigure(struct Driver *drv, struct BusConfigureInfo *configure_info)
|
||||||
|
{
|
||||||
|
NULL_PARAM_CHECK(drv);
|
||||||
|
NULL_PARAM_CHECK(configure_info);
|
||||||
|
|
||||||
|
x_err_t ret = EOK;
|
||||||
|
|
||||||
|
if (drv->configure) {
|
||||||
|
ret = drv->configure(drv, configure_info);
|
||||||
|
if(ret) {
|
||||||
|
KPrintf("BusDrvCfg error, ret %u\n", ret);
|
||||||
|
return ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
Binary file not shown.
After Width: | Height: | Size: 37 KiB |
Loading…
Reference in New Issue