上一节中,我们讲解了如何自动创建设备节点,这一节我们在上一节的基础上,实现点亮LED。
上一节文章链接:https://blog.csdn.net/qq_37659294/article/details/104308284
驱动里面能够用很多种方法实现LED驱动,其中有本节的字符驱动(最笨的方法)、混杂设备驱动、使用内核GPIO函数接口、使用通用的平台设备驱动的方法等。但是,不要因为本节是最笨的方法,就不学习了,对于初学者来说,循序渐进的学习是一种好习惯,好了,废话不多说,直奔主题。
问:怎么写LED驱动程序?
1.搭建一个字符驱动的框架(上一节已经完成)
2.完善硬件的操作
问:驱动里操作硬件寄存器与单片机操作硬件寄存器有什么不一样的地方?
答:单片机操作的寄存器地址是物理地址,驱动里面操作的必须是虚拟地址,因为驱动是内核的一部分,内核里的地址都是虚拟地址。
问:怎么让物理地址转换为虚拟地址?
答:使用ioremap函数,它的功能就是将物理地址映射为虚拟地址,具体怎么映射需要去看linux内存管理等内容。
问:应用程序如果要传数据给内核怎么办?
答:使用copy_from_user函数,同理如果内核要传数据给应用空间的应用程序则使用copy_to_user函数。
详细请参考驱动源码:
#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_createstatic 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);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_class = class_create(THIS_MODULE, "firstdrv");/* 在“类”里面创建设备* MKDEV(major, 0)指定主设备号为major,次设备号为0(这里的major必须和register_chrdev返回的一致,不然会出错)*/firstdrv_device = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xxx");/* 将物理地址映射为虚拟地址 *///物理地址的起始地址0x56000050,长度16字节gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16);//gpfdat的物理地址和gpfcon相差4字节,前面我们总共映射了16字节,所以它们的虚拟地址也相差4字节,long型指针+1相当于加四字节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("LWJ");
MODULE_DESCRIPTION("Just for Demon");
MODULE_LICENSE("GPL"); //遵循GPL协议
应用测试程序源码:
#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;} /* val是int类型,所以写入4个字节 */write(fd,&val,4);return 0;
}
测试步骤:
[WJ2440]# ls
Qt driver_test lib root udisk
TQLedtest etc linuxrc sbin usr
app_test first_drv.ko mnt sddisk var
bin first_test opt sys web
dev home proc tmp
[WJ2440]# ls -l /dev/xxx //还没有设备节点
ls: /dev/xxx: No such file or directory
[WJ2440]# insmod first_drv.ko
[WJ2440]# lsmod
first_drv 2300 0 - Live 0xbf003000
[WJ2440]# ls -l /dev/xxx //装上驱动程序后自动生成了设备节点/dev/xxx
crw-rw---- 1 root root 252, 0 Jan 2 00:23 /dev/xxx
[WJ2440]# ./first_test
first_drv_open
Usage:
./first_test <on|off>
[WJ2440]# ./first_test off
first_drv_open
first_drv_write
[WJ2440]# ./first_test on
first_drv_open
first_drv_write
[WJ2440]#
可发现,当执行下面语句时,开发板上的4个LED同时被熄灭:
[WJ2440]# ./first_test off
可发现,当执行下面语句时,开发板上的4个LED同时被点亮:
[WJ2440]# ./first_test on
若是要单独控制某个LED灯,当然可以自定义传入某些数据格式,根据输入的参数不同来设置我们的寄存器;也可以通过生成多个设备节点,通过次设备号来分别控制,本质上相当于把4个LED看成一个整体,或是把LED1、LED2(都是LED,主设备号相同,次设备号用来区分不同个体)分别看成一个设备。其代码如下:
//生成4个设备节点,用的是同一个驱动程序,通过次设备号来决定控制哪个灯
#include <linux/module.h>
#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 <asm/arch/regs-gpio.h>
#include <asm/hardware.h>#define DEVICE_NAME "leds" /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
#define LED_MAJOR 231 /* 主设备号 */static struct class *leds_class;
static struct class_device *leds_class_devs[4];/* bit0<=>D10, 0:亮, 1:灭 * bit1<=>D11, 0:亮, 1:灭 * bit2<=>D12, 0:亮, 1:灭 */
static char leds_status = 0x0;
static DECLARE_MUTEX(leds_lock); // 定义赋值//static int minor;
static unsigned long gpio_va;#define GPIO_OFT(x) ((x) - 0x56000000)
#define GPFCON (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000050)))
#define GPFDAT (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000054)))/* 应用程序对设备文件/dev/leds执行open(...)时,* 就会调用s3c24xx_leds_open函数*/
static int s3c24xx_leds_open(struct inode *inode, struct file *file)
{int minor = MINOR(inode->i_rdev); //MINOR(inode->i_cdev);switch(minor){case 0: /* /dev/leds */{// 配置3引脚为输出//s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);GPFCON &= ~(0x3<<(4*2));GPFCON |= (1<<(4*2));//s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);GPFCON &= ~(0x3<<(5*2));GPFCON |= (1<<(5*2));//s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);GPFCON &= ~(0x3<<(6*2));GPFCON |= (1<<(6*2));// 都输出0//s3c2410_gpio_setpin(S3C2410_GPF4, 0);GPFDAT &= ~(1<<4);//s3c2410_gpio_setpin(S3C2410_GPF5, 0);GPFDAT &= ~(1<<5);//s3c2410_gpio_setpin(S3C2410_GPF6, 0);GPFDAT &= ~(1<<6);down(&leds_lock);leds_status = 0x0;up(&leds_lock);break;}case 1: /* /dev/led1 */{s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);s3c2410_gpio_setpin(S3C2410_GPF4, 0);down(&leds_lock);leds_status &= ~(1<<0);up(&leds_lock);break;}case 2: /* /dev/led2 */{s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);s3c2410_gpio_setpin(S3C2410_GPF5, 0);leds_status &= ~(1<<1);break;}case 3: /* /dev/led3 */{s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);s3c2410_gpio_setpin(S3C2410_GPF6, 0);down(&leds_lock);leds_status &= ~(1<<2);up(&leds_lock);break;}}return 0;
}static int s3c24xx_leds_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{int minor = MINOR(filp->f_dentry->d_inode->i_rdev);char val;switch (minor){case 0: /* /dev/leds */{copy_to_user(buff, (const void *)&leds_status, 1); break;}case 1: /* /dev/led1 */{down(&leds_lock);val = leds_status & 0x1;up(&leds_lock);copy_to_user(buff, (const void *)&val, 1);break;}case 2: /* /dev/led2 */{down(&leds_lock);val = (leds_status>>1) & 0x1;up(&leds_lock);copy_to_user(buff, (const void *)&val, 1);break;}case 3: /* /dev/led3 */{down(&leds_lock);val = (leds_status>>2) & 0x1;up(&leds_lock);copy_to_user(buff, (const void *)&val, 1);break;}}return 1;
}static ssize_t s3c24xx_leds_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{//int minor = MINOR(inode->i_rdev); //MINOR(inode->i_cdev);int minor = MINOR(file->f_dentry->d_inode->i_rdev);char val;copy_from_user(&val, buf, 1);switch (minor){case 0: /* /dev/leds */{ s3c2410_gpio_setpin(S3C2410_GPF4, (val & 0x1));s3c2410_gpio_setpin(S3C2410_GPF5, (val & 0x1));s3c2410_gpio_setpin(S3C2410_GPF6, (val & 0x1));down(&leds_lock);leds_status = val;up(&leds_lock);break;}case 1: /* /dev/led1 */{s3c2410_gpio_setpin(S3C2410_GPF4, val);if (val == 0){down(&leds_lock);leds_status &= ~(1<<0);up(&leds_lock);}else{down(&leds_lock);leds_status |= (1<<0); up(&leds_lock);}break;}case 2: /* /dev/led2 */{s3c2410_gpio_setpin(S3C2410_GPF5, val);if (val == 0){down(&leds_lock);leds_status &= ~(1<<1);up(&leds_lock);}else{down(&leds_lock);leds_status |= (1<<1); up(&leds_lock);}break;}case 3: /* /dev/led3 */{s3c2410_gpio_setpin(S3C2410_GPF6, val);if (val == 0){down(&leds_lock);leds_status &= ~(1<<2);up(&leds_lock);}else{down(&leds_lock);leds_status |= (1<<2); up(&leds_lock);}break;}}return 1;
}/* 这个结构是字符设备驱动程序的核心* 当应用程序操作设备文件时所调用的open、read、write等函数,* 最终会调用这个结构中指定的对应函数*/
static struct file_operations s3c24xx_leds_fops = {.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */.open = s3c24xx_leds_open, .read = s3c24xx_leds_read, .write = s3c24xx_leds_write,
};/** 执行insmod命令时就会调用这个函数 */
static int __init s3c24xx_leds_init(void)
//static int __init init_module(void){int ret;int minor = 0;gpio_va = ioremap(0x56000000, 0x100000);if (!gpio_va) {return -EIO;}/* 注册字符设备* 参数为主设备号、设备名字、file_operations结构;* 这样,主设备号就和具体的file_operations结构联系起来了,* 操作主设备为LED_MAJOR的设备文件时,就会调用s3c24xx_leds_fops中的相关成员函数* LED_MAJOR可以设为0,表示由内核自动分配主设备号*/ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c24xx_leds_fops);if (ret < 0) {printk(DEVICE_NAME " can't register major number\n");return ret;}leds_class = class_create(THIS_MODULE, "leds");if (IS_ERR(leds_class))return PTR_ERR(leds_class);/* 在一个“类”里面创建多个设备,说白了就是创建多个设备节点 */leds_class_devs[0] = class_device_create(leds_class, NULL, MKDEV(LED_MAJOR, 0), NULL, "leds"); /* /dev/leds */for (minor = 1; minor < 4; minor++) /* /dev/led1,2,3 */{leds_class_devs[minor] = class_device_create(leds_class, NULL, MKDEV(LED_MAJOR, minor), NULL, "led%d", minor);if (unlikely(IS_ERR(leds_class_devs[minor])))return PTR_ERR(leds_class_devs[minor]);}printk(DEVICE_NAME " initialized\n");return 0;
}/** 执行rmmod命令时就会调用这个函数 */
static void __exit s3c24xx_leds_exit(void)
{int minor;/* 卸载驱动程序 */unregister_chrdev(LED_MAJOR, DEVICE_NAME);for (minor = 0; minor < 4; minor++){class_device_unregister(leds_class_devs[minor]);}class_destroy(leds_class);iounmap(gpio_va);
}/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(s3c24xx_leds_init);
module_exit(s3c24xx_leds_exit);/* 描述驱动程序的一些信息,不是必须的 */
MODULE_AUTHOR("http://www.100ask.net");
MODULE_VERSION("0.1.0");
MODULE_DESCRIPTION("S3C2410/S3C2440 LED Driver");
MODULE_LICENSE("GPL");
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>/** ledtest <dev> <on|off>*/void print_usage(char *file)
{printf("Usage:\n");printf("%s <dev> <on|off>\n",file);printf("eg. \n");printf("%s /dev/leds on\n", file);printf("%s /dev/leds off\n", file);printf("%s /dev/led1 on\n", file);printf("%s /dev/led1 off\n", file);
}int main(int argc, char **argv)
{int fd;char* filename;char val;if (argc != 3){print_usage(argv[0]);return 0;}filename = argv[1];fd = open(filename, O_RDWR);if (fd < 0){printf("error, can't open %s\n", filename);return 0;}if (!strcmp("on", argv[2])){// 亮灯val = 0;write(fd, &val, 1);}else if (!strcmp("off", argv[2])){// 灭灯val = 1;write(fd, &val, 1);}else{print_usage(argv[0]);return 0;}return 0;
}
本文参考于:https://blog.csdn.net/lwj103862095/article/details/17472455