上一节中,我们讲解了如何自动创建设备节点,这一节我们在上一节的基础上,实现点亮LED。
上一节文章链接:http://blog.csdn.net/lwj103862095/article/details/17470573
驱动里面能够用很多种方法实现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_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);
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("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;
}
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
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