之前学习了简单的字符设备驱动程序的编写,还仅仅是一种抽象的操作。今天来学习通过字符设备驱动来控制led的亮灭。
如果还没有学习过字符设备驱动,还是建议先去掌握那部分的知识,再回来看这些就容易好多了。
驱动程序:
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/module.h>
#include <linux/device.h> //class_create
static struct class *firstdrv_class;
static struct device *firstdrv_device;
volatile unsigned long *gpbcon = NULL;
volatile unsigned long *gpbdat = NULL;
int major;
static int first_drv_open(struct inode * inode, struct file * filp)
{
printk("first_drv_open\n");
/* LED1,LED2,LED3,LED4对应GPB5、GPB6、GPB7、GPB8
* 配置GPB5,6,7,8为输出
*/
*gpbcon &= ~((0x3<<(5*2)) | (0x3<<(6*2)) | (0x3<<(7*2)) | (0x3<<(8*2)));
*gpbcon |= ((0x1<<(5*2)) | (0x1<<(6*2)) | (0x1<<(7*2)) | (0x1<<(8*2)));
return 0;
}
static int first_drv_write(struct file * file, const char __user * buffer, size_t count, loff_t * ppos)
{
int val;
printk("first_drv_write\n");
copy_from_user(&val, buffer, count);//buffer指针指向的内容还在用户空间
if (val == 1)
{
// 点灯
*gpbdat &= ~((1<<5) | (1<<6) | (1<<7) | (1<<8));
}
else
{
// 灭灯
*gpbdat |= (1<<5) | (1<<6) | (1<<7) | (1<<8);
}
return 0;
}
/* File operations struct for character device */
static const struct file_operations first_drv_fops = {
.owner = THIS_MODULE,
.open = first_drv_open,
.write = first_drv_write,
};
/* 驱动入口函数 */
static int first_drv_init(void)
{
/* 主设备号设置为0表示由系统自动分配主设备号 */
major = register_chrdev(0, "first_drv", &first_drv_fops);
/* 创建firstdrv类 */
firstdrv_class = class_create(THIS_MODULE, "firstdrv");
/* 在firstdrv类下创建xxx设备,供应用程序打开设备*/
firstdrv_device = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xxx");
/* 将物理地址映射为虚拟地址 */
gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16);
gpbdat = gpbcon + 1;
return 0;
}
/* 驱动出口函数 */
static void first_drv_exit(void)
{
unregister_chrdev(major, "first_drv");
device_unregister(firstdrv_device); //卸载类下的设备
class_destroy(firstdrv_class); //卸载类
iounmap(gpbcon); //解除映射
}
module_init(first_drv_init); //用于修饰入口函数
module_exit(first_drv_exit); //用于修饰出口函数
MODULE_AUTHOR("CLBIAO");
MODULE_DESCRIPTION("Just for Demon");
MODULE_LICENSE("GPL"); //遵循GPL协议
程序要点分析:
第一点:驱动程序设计框架就是简单字符设备驱动程序框架。
第二点:first_drv_init()函数里边调用了ioremap()函数将物理地址映射到虚拟地址,原因就是:整个内核空间地址是虚拟的地址,因为驱动是内核的一部分,所以驱动里面操作的必须是虚拟地址,在操作寄存器之前一定要记得先完成映射工作。
具体的映射原理在linux内核内存管理那部分有讲到。
第三点:用户程序给内核程序传递数据的方法:使用copy_from_user(内核缓冲区,用户缓冲区,字节数)函数,同理如果内核要传数据给应用空间的应用程序则使用copy_to_user函数。
第四点:自动创建设备节点,会涉及到主设备号和次设备号的分配。下面是创建步骤:(需要头文件<linux/device.h>)
(1)新建一个类class结构体指针和设备device结构体指针
static struct class *firstdrv_class;
static struct device *firstdrv_device;
(2)叫系统自动分配一个可以使用的主设备号
major=register_chrdev(0, "name_drv", &first_drv_fops);
(3)调用class_create()函数创建一个类
函数原型:struct class *class_create(struct module *owner, const char *name);
参数分析:owner 模块所有者;name 类名字符串
函数返回:创建成功的类结构体指针
(4)调用device_create()函数创建设备节点,需要前面已分配好的主设备号和自己安排的次设备号
函数原型:static struct device *device_create(struct class *class, struct device *parent,dev_t devt, void *drvdata, const char *fmt, ...);
参数分析:class 指向创建好的类结构体,即设备所属的类;parent 指向这个设备的父母设备,无就设置为NULL;devt 设备号,包括主和次,通过宏MKDEV(major,minor)来合成设备号;drvdata 设备私有数据;最后面是设备的名字
函数返回:设备结构体指针,指针指向的内容就是最终形成的设备节点文件,即之前我们有尝试通过mknod命令来创建的设备文件
(5)卸载设备后要删除设备节点删除类,依次调用下面三个函数:
学会了这种方法,以后再也不用安装完驱动之后再来苦逼的mknod一个一个来创建设备文件了unregister_chrdev(major, "first_drv") //参数:已申请主设备号+主设备名称
device_unregister(firstdrv_device); //参数:设备结构体指针
class_destroy(firstdrv_class); //参数:类结构体指针
应用程序测试:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
/* first_test on
* first_test off
*/
int main(int argc ,char *argv[])
{
int fd;
int val = 0;
fd = open("/dev/xxx",O_RDWR);
if (fd < 0)
{
printf("open error\n");
}
if (argc != 2)
{
printf("Usage:\n");
printf("%s <on|off>\n",argv[0]);
return 0;
}
if(strncmp(argv[1],"on",2) == 0)
{
val = 1;
}
else if (strncmp(argv[1],"off",3) == 0)
{
val = 0;
}
write(fd,&val,4);
return 0;
}
运行结果:
./first_test off
开发板上4颗led同时熄灭
./first_test on
开发板上4颗led同时点亮
搞定!