字符设备驱动-轮询方式操控按键
扫描二维码
随时随地手机看文章
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
②将open、read驱动函数框架写出
static int second_drv_open(struct inode *inode,struct file *file)
{
return 0;
}
③定义fileoperation
static struct file_operations second_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = second_drv_open,
.read = second_drv_read,
};
④
入口函数编写(注册驱动程序)
int major;
static int second_drv_init(void)
{
major = register_chrdev(0,"second_drv",&second_drv_fops);
return 0;
}
出口函数编写(卸载驱动程序)
static int second_drv_exit(void)
{
unregister_chrdev(major,"second_drv");
return 0;
}
入口函数和出口函数也只是普通的函数,何以达到入口出口的功能!需要通过module_来修饰。
module_init(second_drv_init);
module_exit(second_drv_exit);
⑤给sysfs提供更多信息,利用udev机制创建设备节点
Ⅰ定义两个结构体
static struct class *seconddrv_class;
static struct class_device *seconddrv_class_dev;
Ⅱ 入口函数中创建一个类=>类下面创建一个设备
int major;
static int second_drv_init(void)
{
major = register_chrdev(0,"second_drv",&second_drv_fops);
seconddrv_class = class_create(THIS_MODULE,"seconddrv");
seconddrv_class_dev = class_device_create(seconddrv_class,NULL,MKDEV(major,0),NULL,"buttons");
}
Ⅲ 出口函数中卸载类补: mdev是udev的简化版本,mdev应用程序会被内核来调用,根据⑤提供的信息来创建/dev/buttons 设备节点。
static int second_drv_exit(void)
{
unregister_chrdev(major,"second_drv");
class_device_unregister(seconddrv_class_dev);
class_destroy(seconddrv_class);
return 0;
}
⑥ MODULE_LICENSE("GPL");
测试:通过 “GPL” 指明 这是GNU General Public License的任意版本
修改Makefile
cp second_drv.ko /work/nfs_root/czg
insmod ./second_drv.ko
lsmod
cat /proc/devices
ls /dev/buttons -l
原理图: 2440手册:VA = ioremap(PA,size)
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;
② 地址映射建立和解除
入口函数中建立:
static int second_drv_init(void)
{
major = register_chrdev(0,"second_drv",&second_drv_fops);
seconddrv_class = class_create(THIS_MODULE,"seconddrv");
seconddrv_class_dev = class_device_create(seconddrv_class,NULL,MKDEV(major,0),NULL,"buttons");
gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060,16);
gpgdat = gpgcon + 1;
return 0;
}
出口函数中解除:
static int second_drv_exit(void)
{
unregister_chrdev(major,"second_drv");
class_device_unregister(seconddrv_class_dev);
class_destroy(seconddrv_class);
iounmap(gpfcon);
iounmap(gpgcon);
return 0;
}
③ open函数中配置引脚
static int second_drv_open(struct inode *inode,struct file *file)
{
/* 配置GPF0,2为输入引脚 */
*gpfcon &= ~((0x3<<(0*2)) | (0x3<<(2*2)));
/* 配置GPG3为输入引脚 */
*gpgcon &= ~((0x3<<(3*2)));
return 0;
}
④ read函数中返回引脚状态
static ssize_t second_drv_read (struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
/* 返回4个引脚的电平状态 */
unsigned char key_vals[3];
int regval;
//看用户需要读取的空间,和这里的是否相同
if(count != sizeof(key_vals))
return -EINVAL;
/* 读GPF0,2 */
regval = *gpfdat;
key_vals[0] = (regval & (1<<0)) ? 1 : 0;
key_vals[1] = (regval & (1<<2)) ? 1 : 0;
/* 读GPF3 */
regval = *gpgdat;
key_vals[2] = (regval & (1<<3)) ? 1 : 0;
//将值返回给用户程序
copy_to_user(buf,key_vals,sizeof(key_vals));
return sizeof(key_vals);
}
完整代码:
Makefile
KERN_DIR = /work/system/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += second_drv.o
驱动程序:second_drv.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static struct class *seconddrv_class;
static struct class_device *seconddrv_class_dev;
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;
static int second_drv_open(struct inode *inode,struct file *file)
{
/* 配置GPF0,2为输入引脚 */
*gpfcon &= ~((0x3<<(0*2)) | (0x3<<(2*2)));
/* 配置GPG3为输入引脚 */
*gpgcon &= ~((0x3<<(3*2)));
return 0;
}
static ssize_t second_drv_read (struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
/* 返回4个引脚的电平状态 */
unsigned char key_vals[3];
int regval;
//看用户需要读取的空间,和这里的是否相同
if(count != sizeof(key_vals))
return -EINVAL;
/* 读GPF0,2 */
regval = *gpfdat;
key_vals[0] = (regval & (1<<0)) ? 1 : 0;
key_vals[1] = (regval & (1<<2)) ? 1 : 0;
/* 读GPF3 */
regval = *gpgdat;
key_vals[2] = (regval & (1<<3)) ? 1 : 0;
//将值返回给用户程序
copy_to_user(buf,key_vals,sizeof(key_vals));
return sizeof(key_vals);
}
static struct file_operations second_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = second_drv_open,
.read = second_drv_read,
};
int major;
static int second_drv_init(void)
{
major = register_chrdev(0,"second_drv",&second_drv_fops);
seconddrv_class = class_create(THIS_MODULE,"seconddrv");
seconddrv_class_dev = class_device_create(seconddrv_class,NULL,MKDEV(major,0),NULL,"buttons");
gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060,16);
gpgdat = gpgcon + 1;
return 0;
}
static int second_drv_exit(void)
{
unregister_chrdev(major,"second_drv");
class_device_unregister(seconddrv_class_dev);
class_destroy(seconddrv_class);
iounmap(gpfcon);
iounmap(gpgcon);
return 0;
}
module_init(second_drv_init);
module_exit(second_drv_exit);
MODULE_LICENSE("GPL");
驱动测试程序:seconddrvtest.c
#include
#include
#include
#include
/*
* firstdrvtest
*/
int main(int argc, char **argv)
{
int fd;
unsigned char key_vals[3];
int cnt = 0;
fd = open("/dev/buttons",O_RDWR);
if(fd < 0)
{
printf("can't open!n");
}
while(1)
{
read(fd,key_vals,sizeof(key_vals));
if(!key_vals[0] || !key_vals[1] || !key_vals[2])
{
printf("%04d key pressed: %d %d %d.n",cnt++,key_vals[0],key_vals[1],key_vals[2]);
}
}
return 0;
}
测试:
后台运行:
./seconddrvtest &
查看后台程序:top
放在前台:fg
insmod second_drv.ko
lsmod
cat /proc/devices
ls /dev/buttons -l
./seconddrvtest