Android驱动开发读书笔记七

第七章

(一)创建设备文件

1.使用cdev_init函数初始化cdec

描述设备文件需要一个cdev结构体,代码如下:

struct cdev{

struct kobject kobj;

struct module *owener;

const struct file_operations *ops;

struct list_head list;

dev_t dev;

unsigned int count;

}

大多数的成员变量不需要我们自己初始化,调用cdev-init函数即可。

2.指定设备号

分别指定主从设备号,所以需要MKDEV宏

int dev_number=MKDEV(major,minor);

3.使用cdev_add函数将字符设备添加到内核中的字符设备数据中

调用cdev_add函数需要指定设备文件指针(p)、设备号(dev)、设备文件数量(count)还调用了一个重要的函数kobj_map.

4.使用class_create宏创建struct class

Struct class包含了一些与设备文件有关的变量和一些回调函数指针变量。代码:

Struct class *leds_class=NULL;

Leds_class=class_create(THIS_MODULE,”dev_name”);

5.使用device_create函数创建设备文件

Decive_create(leds_class,NULL,dev_number,NULL,DEVICE_NAME)

(二)卸载LED驱动的设备文件

卸载LED驱动的设备文件,依次调用decive_destory、class_destory、unregister_chrdev_region方法。Led_destory_device函数用于卸载LED驱动的设备文件,leds_exit函数是LED驱动的卸载函数,通过调用Led_destory_device函数来完成卸载LED驱动设备文件的工作。

设置LED等的状态

设置寄存器和初始化LED驱动,通过设置寄存器的值可以设置LED引脚的状态以及控制其亮灭。然后要控制LED,可以通过字符串和I/O命令。使用字符串用到file_operations.write函数,使用I/O命令用到file_operations.ioctl,从用户空间像内核中写入数据用到的函数copy_from_user。

LED驱动的模块函数

通过module_param宏可以指定参数文件的访问权限

state int leds_state=1;

State int led_init(void)

{

Int ret;

Ret=led_create_device();

Leds_init_gpm(~led_state);

Printk(DEVICE_NAME” initialized ”);

Return ret;

}

 

Module_param(led_state,int ,S_IRUGO | IWUSR);

然后本章介绍到使用NDK测试LED驱动,使用JAVA测试LED驱动以及LED驱动移植。

http://www.cnblogs.com/xxyue/

 

原文地址:https://www.cnblogs.com/xxyue/p/5559326.html