Android驱动LED驱动的编写汇总Word格式文档下载.docx
- 文档编号:12959534
- 上传时间:2022-10-01
- 格式:DOCX
- 页数:21
- 大小:1.55MB
Android驱动LED驱动的编写汇总Word格式文档下载.docx
《Android驱动LED驱动的编写汇总Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《Android驱动LED驱动的编写汇总Word格式文档下载.docx(21页珍藏版)》请在冰豆网上搜索。
stdio.h>
stdlib.h>
unistd.h>
fcntl.h>
string.h>
sys/types.h>
sys/stat.h>
sys/ioctl.h>
#defineLED_ON_IO('
k'
1)
#defineLED_OFF_IO('
2)
intmain()
{
inti=0;
intdev_fd;
dev_fd=open("
/dev/led"
O_RDWR);
if(dev_fd==-1){
printf("
Cann'
topenfile/dev/led\n"
);
exit
(1);
}
while
(1)
{
ioctl(dev_fd,LED_ON,1);
sleep
(1);
ioctl(dev_fd,LED_OFF,1);
ioctl(dev_fd,LED_ON,2);
ioctl(dev_fd,LED_OFF,2);
ioctl(dev_fd,LED_ON,3);
ioctl(dev_fd,LED_OFF,3);
ioctl(dev_fd,LED_ON,4);
ioctl(dev_fd,LED_OFF,4);
return0;
}
下面开始把linux驱动封装成android驱动。
首先介绍一下android驱动用到的三个重要的结构体,
structhw_module_t;
structhw_device_t;
structhw_module_methods_t;
android源码里面结构体的声明
typedefstructhw_module_t{
uint32_ttag;
uint16_tversion_major;
uint16_tversion_minor;
constchar*id;
constchar*name;
constchar*author;
consthw_module_methods_t*methods;
void*dso;
uint32_treserved[32-7];
}hw_module_t;
typedefstructhw_device_t{
uint32_ttag;
uint32_tversion;
structhw_module_t*module;
uint32_treserved[12];
int(*close)(structhw_device_t*device);
}hw_device_t;
typedefstructhw_module_methods_t{
int(*open)(conststructhw_module_t*module,constchar*id,
structhw_device_t**device);
}hw_module_methods_t;
我们经常会用到这三个结构体。
android驱动目录结构:
led
|---hal
||----jni
||-----Android.mk
||----com_farsgiht_server_ledServer.cpp
||----stub
||----include
|||-----led.h
||-----module
||-----Android.mk
||-----led.c
|---linux_drv
首先我们要编写一个stub(代理),代理的意思是,针对你所特有的设备,你找一个代理人就可以帮你完成,它是操作linux驱动的第一层。
编写头文件,名字led.h
代码如下;
hardware/hardware.h>
errno.h>
cutils/log.h>
cutils/atomic.h>
#defineLED_HARDWARE_MODULE_ID"
led"
structled_module_t{
structhw_module_tcommon;
};
structled_control_device_t{
structhw_device_tcommon;
int(*set_on)(structled_control_device_t*dev,intarg);
int(*set_off)(structled_control_device_t*dev,intarg);
structled_control_context_t{
structled_control_device_tdevice;
structhw_module_tsturcthw_device_t这两个结构体不能直接使用,所以进行了一下封装(继承)。
led_module_t继承hw_module_t
led_control_device_t继承hw_device_t
led_control_context_t继承led_control_device_t
在led_control_device_t结构体有函数指针的声明,因为后面代码中会给这些函数指针赋值
编写led.c
代码如下:
#defineLOG_TAG"
LedStub"
#include"
../include/led.h"
intfd;
staticintled_set_on(structled_control_device_t*dev,intarg)
LOGI("
led_set_on"
ioctl(fd,LED_ON,arg);
staticintled_set_off(structled_control_device_t*dev,intarg)
led_set_off"
ioctl(fd,LED_OFF,arg);
staticintled_device_close(structhw_device_t*device)
structled_control_context_t*context=(structled_control_context_t*)device;
if(context)free(context);
close(fd);
staticintled_device_open(conststructhw_module_t*module,constchar*name,
structhw_device_t**device)
structled_control_context_t*context;
LOGD("
led_device_open"
context=(structled_control_context_t*)malloc(sizeof(*context));
memset(context,0,sizeof(*context));
context->
mon.tag=HARDWARE_DEVICE_TAG;
mon.version=0;
mon.module=module;
mon.close=led_device_close;
device.set_on=led_set_on;
device.set_off=led_set_off;
*device=(structhw_device_t*)&
(context->
device);
if((fd=open("
O_RDWR))==-1)
ERROR:
open"
}else{
openleddeviceok\n"
staticstructhw_module_methods_tled_module_methods={
open:
led_device_open
conststructled_module_tHAL_MODULE_INFO_SYM={
common:
tag:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android 驱动 LED 编写 汇总
![提示](https://static.bdocx.com/images/bang_tan.gif)