这两天跟着一个视频照着写了一个驱动,算不上原创,但觉得还是要记录下来......
硬件环境:FL2440开发板
软件环境:linux2.6.38,飞凌提供的文件系统(不太满意,以后会自己编译一个)
以下是源码,主要用来控制LED的亮和暗,
版本一:创建一个设备名,主设备号自动生成,次设备号为0,并自动创建设备节点(由于文件系统原因,热插拔驱动支持的不是很好每次装载完驱动要用mdev -s扫描)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");
//struct cdev first_dev;
static struct class *firstdrv_class;
//static struct class_device *firstdrv_class_dev;
volatile unsigned long *gpbcon = NULL;
volatile unsigned long *gpbdat = NULL;
static int first_drv_open(struct inode *inode, struct file *file)
{
printk("Open device OK!\n");
/*GPB 5 6 8 10为输出*/
*gpbcon &= ~((0x3 << (5*2)) | (0x3 << (6*2)) | (0x3 << (8*2)) | (0x3 << (10*2)));
*gpbcon |= (0x1 << (5*2) | 0x1 << (6*2) | 0x1 << (8*2) | 0x1 << (10*2));
return 0;
}
static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
int val;
//开启LED灯
copy_from_user(&val, buf, 1);
if(val == 0)
{
*gpbdat &= ~(0x1 << 5 | 0x1 << 6 | 0x1 << 8 | 0x1 << 10);
printk("Write 0..\n");
}
else
{
*gpbdat |= 0x1 << 5 | 0x1 << 6 | 0x1 << 8 | 0x1 << 10;
printk("Write 1..\n");
}
return 0;
}
static struct file_operations first_drv_fops = {
.owner = THIS_MODULE,
.open = first_drv_open,
.write = first_drv_write
};
unsigned int major;
unsigned int minor;
dev_t firstdev;
struct cdev first_cdev;
static int first_drv_init(void)//Èë¿Úº¯Êý
{
major = register_chrdev(0, "first_drv", &first_drv_fops); // 注册, 告诉内核
firstdrv_class = class_create(THIS_MODULE, "firstdrv");
device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */
gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16);//映射0x5600010物理地址到虚拟地址
gpbdat = gpbcon + 1;
printk("Hello kernel..\n");
return 0;
}
static void first_drv_exit(void)
{
unregister_chrdev(major, "first_drv");
device_destroy(firstdrv_class, MKDEV(major, 0));
class_destroy(firstdrv_class);
iounmap(gpbcon);
printk("Bye Bye kernel..\n");
}
module_init(first_drv_init);
module_exit(first_drv_exit);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");
//struct cdev first_dev;
static struct class *firstdrv_class;
static struct class_device *firstdrv_class_dev[4];
volatile unsigned long *gpbcon = NULL;
volatile unsigned long *gpbdat = NULL;
static int first_drv_open(struct inode *inode, struct file *file)
{
printk("Open device OK!\n");
/*GPB 5 6 8 10为输出*/
*gpbcon &= ~((0x3 << (5*2)) | (0x3 << (6*2)) | (0x3 << (8*2)) | (0x3 << (10*2)));
*gpbcon |= (0x1 << (5*2) | 0x1 << (6*2) | 0x1 << (8*2) | 0x1 << (10*2));
return 0;
}
static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
char val;//注意,此处要使用char型,原因没弄清楚,用int输出的数据不同,这点不是很理解。
//查询打开的是哪个设备
int iminor = MINOR(file->f_dentry->d_inode->i_rdev);
//开启LED灯
copy_from_user(&val, buf, 1);
if(iminor == 0)
{
if(val == 0)
{
*gpbdat &= ~(0x1 << 5 | 0x1 << 6 | 0x1 << 8 | 0x1 << 10);
printk("Write 0..\n");
}
else
{
*gpbdat |= 0x1 << 5 | 0x1 << 6 | 0x1 << 8 | 0x1 << 10;
printk("Write 1..\n");
}
}
if(iminor == 1)
{
if(val == 0)
{
*gpbdat &= ~(0x1 << 5);
}
else
{
*gpbdat |= 0x1 << 5;
}
}
if(iminor == 2)
{
if(val == 0)
{
*gpbdat &= ~(0x1 << 6);
}
else
{
*gpbdat |= 0x1 << 6;
}
}
if(iminor == 3)
{
if(val == 0)
{
*gpbdat &= ~(0x1 << 8);
}
else
{
*gpbdat |= 0x1 << 8;
}
}
if(iminor == 4)
{
if(val == 0)
{
*gpbdat &= ~(0x1 << 10);
}
else
{
*gpbdat |= 0x1 << 10;
}
}
return 0;
}
static struct file_operations first_drv_fops = {
.owner = THIS_MODULE,
.open = first_drv_open,
.write = first_drv_write
};
unsigned int major;
unsigned int minor;
dev_t firstdev;
struct cdev first_cdev;
static int first_drv_init(void)//Èë¿Úº¯Êý
{
major = register_chrdev(0, "first_drv", &first_drv_fops); // 注册, 告诉内核
firstdrv_class = class_create(THIS_MODULE, "firstdrv");
firstdrv_class_dev[0] = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */
for(minor = 1; minor < 4; minor++)
{
firstdrv_class_dev[minor] = device_create(firstdrv_class, NULL, MKDEV(major, minor), NULL, "xyz%d", minor);
}
gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16);//映射0x5600010物理地址到虚拟地址
gpbdat = gpbcon + 1;
printk("Hello kernel..\n");
return 0;
}
static void first_drv_exit(void)
{
unregister_chrdev(major, "first_drv");
for(minor = 0; minor < 4; minor++)
{
device_destroy(firstdrv_class, MKDEV(major, minor));
}
class_destroy(firstdrv_class);
iounmap(gpbcon);
printk("Bye Bye kernel..\n");
}
module_init(first_drv_init);
module_exit(first_drv_exit);
#include
#include
#include
int main(int argc, char **argv)
{
int fd;
int val = 0;
fd = open("/dev/xyz1", O_RDWR);
if(fd < 0)
{
printf("Open error..\n");
return -1;
}
if(argc < 2)
{
printf("Usage:\n");
printf("led_driver \n");
return 0;
}
if(strncmp(argv[1], "on", 2) == 0)
{
val = 0;
printf("00000");
write(fd, &val, 4);
}
else
{
val = 1;
printf("11111");
write(fd, &val, 4);
}
return 0;
}
小结:
1.控制硬件时需要分配虚拟地址,以前也写过驱动不过直接用的是硬件的物理地址了,本例中使用了ioremap函数
2.逻辑运算的学习
3.从用户空间向内核空间传递数值时用copy_from_user,内核向用户传递时用copy_to_user,
原本是想在write函数中直接操作buf的,但通过上面的驱动实例,我输出的buf是空的,所以用到了copy_from_user,更深层次的没有深入了,目前没必要深入理解copy_from_user,先把内核中的函数用熟悉了
4.linux内提供了许多操作函数,比如设置引脚时,linux会提供,这些函数也是对我上面的操作做了封装,我的驱动中的地址和引脚都是通过查看原理图得知的。
5.驱动与设备节点的关系:在linux中注册了驱动后会在/proc/devices中找到注册的驱动名,包括主设备号和次设备号,创建了设备节点后会将设备名与驱动关联起来,用户层可以根据设备名操作硬件。
6.设备的注册与注销......参照版本2