Linux驱动开发 file_operations ioctl read write驱动及Android应用程序调用
资源含Android应用程序->JNI->C语言层->驱动程序。
硬件开发板为讯为电子的exynos4412开发板。
主要实现从驱动到Android应用程序的逐层调用过程。关键是file_operations!!
通过Android应用程序,能够控制GPIO LED,写入数据,以及去读已写入的数据。
Android应用程序中有两个BUTTON用不上,无关紧要,这就不删掉了。
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2016-04-19 22:23 资源
目录 0 2016-04-19 22:23 资源MyledsTest
文件 475 2016-03-13 21:46 资源MyledsTest.classpath
文件 3683 2016-03-13 22:00 资源MyledsTest.cproject
文件 1470 2016-03-13 22:00 资源MyledsTest.project
目录 0 2016-04-19 22:22 资源MyledsTest.settings
文件 177 2016-03-13 21:46 资源MyledsTest.settingsorg.eclipse.jdt.core.prefs
文件 865 2016-03-13 21:46 资源MyledsTestAndroidManifest.xml
目录 0 2016-04-19 22:25 资源MyledsTestassets
目录 0 2016-04-19 22:22 资源MyledsTestin
文件 865 2016-03-13 21:46 资源MyledsTestinAndroidManifest.xml
文件 776031 2016-04-19 22:17 资源MyledsTestinMyledsTest.apk
文件 30735 2016-04-19 22:16 资源MyledsTestinR.txt
目录 0 2016-04-19 22:22 资源MyledsTestinclasses
目录 0 2016-04-19 22:22 资源MyledsTestinclassesandroid
目录 0 2016-04-19 22:22 资源MyledsTestinclassesandroidsupport
目录 0 2016-04-19 22:22 资源MyledsTestinclassesandroidsupportv7
目录 0 2016-04-19 22:22 资源MyledsTestinclassesandroidsupportv7appcompat
文件 629 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$anim.class
文件 5136 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$attr.class
文件 763 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$bool.class
文件 594 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$color.class
文件 1530 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$dimen.class
文件 5191 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$drawable.class
文件 2556 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$id.class
文件 445 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$integer.class
文件 1629 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$layout.class
文件 1141 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$string.class
文件 9582 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$style.class
文件 6541 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$styleable.class
文件 1015 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR.class
............此处省略102个文件信息
/*
本文件目标:
1.学习字符设备驱动 注册主次设备
2.用IO内存直接驱动GPIO LED
*/
#include
#include
#include
#include //主次设备
#include
#include //释放和分配设备编号
#include //字符设备驱动
#include //分配scull内存使用
#include //包含device_create函数 class结构体的头文件
//IO端口分配以及IO内存分配 ARM内核中与IO内存一样
#include
//IO内存读写
#include
//CPU 物理地址
#include
//copy_to_user函数头文件
#include
#define CHRDEV_NAME “tarkelc_chrdev“//所分配设备号的名称
#define DEVICE_NAME “tarkelc_learn“//设备节点名称(在/dev中)
#define DRIVER_NAME “mygpio“
#define CLASS_NAME “tarkelc_class“///sys/class中可查到
#define Myled2 EXYNOS4_GPL2(0)
#define Myled3 EXYNOS4_GPK1(1)
#define GPL2base_PA (EXYNOS4_PA_GPIO2+0x0100)
#define GPK1base_PA (EXYNOS4_PA_GPIO2+0x0060)
#define GPL2LEN_PA (0X20)
#define GPK1LEN_PA (0X20)
dev_t mdev_t;
void *p1;
char buffer[1024];
static struct class *myclass;
//操作SPI寄存器结构体
typedef struct{
unsigned long CON; //0x00
unsigned long DAT; //0x04
unsigned long PUD; //0x08
unsigned long DRV; //0x0c
unsigned long CONPND; //0x10
unsigned long PUDPDN; //0x14
unsigned long RESERVED; //0x18
unsigned long RESERVED1; //0x1c
}GPIO_TypeDef;
GPIO_TypeDef *GPL2;
/* 表示每一个设备 */
struct scull_dev {
char *data;
unsigned int size;
struct cdev cdev;
};
struct scull_dev *mydevice;
static ssize_t myspi_read(struct file *f char __user *buf size_t len loff_t *loff)
{
int ret=0;
/*char *pp;//
pp=buffer;
for(ret=0;(ret *pp=(char)ret;
pp++;
}*/
ret=copy_to_user(bufbufferlen);
if(ret!=0){
return -1;
}
return len;
}
static ssize_t myspi_write(struct file *f const char __user *buf size_t len loff_t *loff)
{
int ret;
ret=copy_from_user(bufferbuflen);
if(ret!=0){
return -1;
}
return len;
}
static int myspi_open(struct inode * node struct file *f) {
printk(KERN_ALERT “myspi open!“);
return 0;
}
static int myspi_release(struct inode *node struct file *f){
printk(KERN_ALERT “myspi release!“);
return 0;
}
/* 暂时用于debug */
static long myspi_ioctl(struct file *f unsigned int cmd unsigned long arg)
{
int stateret;
state=ioread32(&GPL2->DAT);
iowrite32(state|0x00000001&GPL2->DAT);//配置IO输出高电平
if(cmd==0){
state=ioread32(&GPL2->DAT);
ret=state&0x01;
if(ret!=0){
iowrite32(state&0xfffffffe&GPL2->DAT);//配置IO输出低电平
}
else {
iowrite32(state|0x00000001&GPL2->DAT);//配置IO输出高电平
}
return 0;
}
else {
}
return 0;
}
struct file_operations my_fops={
.owner = THIS_MODULE
.read = myspi_read
.write = myspi_write
.open = myspi_open
.release = myspi_release
.unlocked_ioctl = myspi_ioctl
};
/* 自定义函数 */
static void gpio_init(void)
{
unsigned int state;
//GPL2base寄存器向IO内存的映射
if(request_mem_region(GPL2base_PAGPL2LEN_PADRIVER_NAME)==
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2016-04-19 22:23 资源
目录 0 2016-04-19 22:23 资源MyledsTest
文件 475 2016-03-13 21:46 资源MyledsTest.classpath
文件 3683 2016-03-13 22:00 资源MyledsTest.cproject
文件 1470 2016-03-13 22:00 资源MyledsTest.project
目录 0 2016-04-19 22:22 资源MyledsTest.settings
文件 177 2016-03-13 21:46 资源MyledsTest.settingsorg.eclipse.jdt.core.prefs
文件 865 2016-03-13 21:46 资源MyledsTestAndroidManifest.xm
目录 0 2016-04-19 22:25 资源MyledsTestassets
目录 0 2016-04-19 22:22 资源MyledsTestin
文件 865 2016-03-13 21:46 资源MyledsTestinAndroidManifest.xm
文件 776031 2016-04-19 22:17 资源MyledsTestinMyledsTest.apk
文件 30735 2016-04-19 22:16 资源MyledsTestinR.txt
目录 0 2016-04-19 22:22 资源MyledsTestinclasses
目录 0 2016-04-19 22:22 资源MyledsTestinclassesandroid
目录 0 2016-04-19 22:22 资源MyledsTestinclassesandroidsupport
目录 0 2016-04-19 22:22 资源MyledsTestinclassesandroidsupportv7
目录 0 2016-04-19 22:22 资源MyledsTestinclassesandroidsupportv7appcompat
文件 629 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$anim.class
文件 5136 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$attr.class
文件 763 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$bool.class
文件 594 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$color.class
文件 1530 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$dimen.class
文件 5191 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$drawable.class
文件 2556 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$id.class
文件 445 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$integer.class
文件 1629 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$layout.class
文件 1141 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$string.class
文件 9582 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$st
文件 6541 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR$st
文件 1015 2016-04-19 22:16 资源MyledsTestinclassesandroidsupportv7appcompatR.class
............此处省略102个文件信息
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)