开发平台:farsight s5pc100-a
内核:linux2.6.29
环境搭配:有博文介绍
开发环境:Ubuntu 、Eclipse
首先强调一下要点:
1.编写Android驱动时,首先先要完成linux驱动,因为android驱动其实是在linux驱动基础之上完成了HAL层(硬件抽象层),如果想要测试的话,自己也要编写java程序来测试你的驱动。
2.android的根文件系统是eclair_2.1版本。我会上传做好的根文件系统提供大家。这里要说的是,android底层内核还是linux的内核,只是进行了一些裁剪。做好的linux内核镜像,这个我也会上传给大家。android自己做了一套根文件系统,这才是android自己做的东西。android事实上只是做了一套根文件系统罢了。
假设linux驱动大家都已经做好了。我板子上有四个灯,通过ioctl控制四个灯,给定不同的参数,点亮不同的灯。
相关文件下载:
本文源码与Android根文件系统、内核zIamge下载
下载在Linux公社的1号FTP服务器里,下载地址:
FTP地址:ftp://www.linuxidc.com
用户名:www.linuxidc.com
密码:www.muu.cc
在 2012年LinuxIDC.com\2月\自己动手写最简单的Android驱动---LED驱动的编写
下载方法见 http://www.linuxidc.net/thread-1187-1-1.html
linux驱动代码因平台不同而有所不同,这就不黏代码了。
这是我测试linux驱动编写的驱动,代码如下:
[cpp]
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/ioctl.h>
- #define LED_ON _IO ('k',1)
- #define LED_OFF _IO ('k',2)
- int main()
- {
- int i = 0;
- int dev_fd;
- dev_fd = open("/dev/led",O_RDWR);
- if ( dev_fd == -1 ) {
- printf("Cann't open file /dev/led\n");
- exit(1);
- }
- while(1)
- {
- ioctl(dev_fd,LED_ON,1);
- sleep(1);
- ioctl(dev_fd,LED_OFF,1);
- sleep(1);
- ioctl(dev_fd,LED_ON,2);
- sleep(1);
- ioctl(dev_fd,LED_OFF,2);
- sleep(1);
- ioctl(dev_fd,LED_ON,3);
- sleep(1);
- ioctl(dev_fd,LED_OFF,3);
- sleep(1);
- ioctl(dev_fd,LED_ON,4);
- sleep(1);
- ioctl(dev_fd,LED_OFF,4);
- sleep(1);
- }
- return 0;
- }
下面开始把linux驱动封装成android驱动。
首先介绍一下android驱动用到的三个重要的结构体,
struct hw_module_t;
struct hw_device_t;
struct hw_module_methods_t;
android源码里面结构体的声明
[cpp]
- typedef struct hw_module_t {
- uint 32_t tag;
- uint16_t version_major;
- uint16_t version_minor;
- const char *id;
- const char *name;
- const char *author;
- const hw_module_methods_t *methods;
- void* dso;
- uint32_t reserved[32-7];
- } hw_module_t;
[cpp]
- typedef struct hw_device_t {
- uint32_t tag;
- uint32_t version;
- struct hw_module_t* module;
- uint32_t reserved[12];
- int (*close) (struct hw_device_t *device);
- }hw_device_t;
[cpp]
- typedef struct hw_module_methods_t {
- int (*open) (const struct hw_module_t *module, const char *id,
- struct hw_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