内核版本:linux-3.0
_________________________________________________________________
在学习LED设备驱动期间经历的问题层出不穷,遇到的问题有以下这些:
1、网上很多的LED驱动几乎都是2.6的内核版本,但是3.0内核和2.6的内核驱动编写有很多地方是不同的在2.6上能运行的设备驱动不一定能在3.0内核上运行。这对于刚刚踏入驱动大门人,急于想了解LED驱动是什么样的,他是怎么工作的,与应用程序又是如何协同工作的呢?对于我们可能第一个要去做的就是去找个例程先跑一跑,了解是怎么回事,增强一些自信和在战略上“藐视”设备驱动的气魄。先介绍一下我的工程结构如下说是,第一个是建立在后面第三个文件s3c_led.c这个LED设备驱动上运行的led运行程序,第二个是Make文件,
我先把可以运行的这个LED驱动贴上来:
(如果你拷贝到你的vim上格式多乱了的话,教你一个快速的方法:在命令行模式下连续按下gg=G这上个字符就能帮你自动调整格式。
你在下面的程序中找到这个nt led[LED_NUM] = {5,6,7,8}; /* Four LEDs use GPB5,GPB6,GPB8,GPB10 */,因为我的led是在GPBIO5、6、7、8这个几个端口,结合你自己的情况修改,其他的不用改
/*********************************************************************************
* Copyright: (C) 2012 Guo Wenxue
* All rights reserved.
*
* Filename: s3c_led.c
* Description: This file
*
* Version: 1.0.0(07/26/2012~)
* Author: Guo Wenxue
* ChangeLog: 1, Release initial version on "07/26/2012 10:03:40 PM"
*
********************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#ifndef __KERNEL__
#include
#endif
//#include
#define DRV_AUTHOR "Guo Wenxue
#define DRV_DESC "S3C24XX LED driver"
#define DEV_NAME "led"
#define LED_NUM 4
/* Set the LED dev major number */
//#define LED_MAJOR 79
#ifndef LED_MAJOR
#define LED_MAJOR 0
#endif
#define DRV_MAJOR_VER 1
#define DRV_MINOR_VER 0
#define DRV_REVER_VER 0
#define DISABLE 0
#define ENABLE 1
#define GPIO_INPUT 0x00
#define GPIO_OUTPUT 0x01
#define PLATDRV_MAGIC 0x60
#define LED_OFF _IO (PLATDRV_MAGIC, 0x18)
#define LED_ON _IO (PLATDRV_MAGIC, 0x19)
#define S3C_GPB_BASE 0x56000010
#define GPBCON_OFFSET 0
#define GPBDAT_OFFSET 4
#define GPBUP_OFFSET 8
#define S3C_GPB_LEN 0x10 /* 0x56000010~0x56000020 */
int led[LED_NUM] = {5,6,7,8}; /* Four LEDs use GPB5,GPB6,GPB8,GPB10 */
static void __iomem *s3c_gpb_membase;
#define s3c_gpio_write(val, reg) __raw_writel((val), (reg)+s3c_gpb_membase)
#define s3c_gpio_read(reg) __raw_readl((reg)+s3c_gpb_membase)
int dev_count = ARRAY_SIZE(led);
int dev_major = LED_MAJOR;
int dev_minor = 0;
int debug = DISABLE;
static struct cdev *led_cdev;
static int s3c_hw_init(void)
{
int i;
volatile unsigned long gpb_con, gpb_dat, gpb_up;
if(!request_mem_region(S3C_GPB_BASE, S3C_GPB_LEN, "s3c2440 led"))
{
return -EBUSY;
}
if( !(s3c_gpb_membase=ioremap(S3C_GPB_BASE, S3C_GPB_LEN)) )
{
release_mem_region(S3C_GPB_BASE, S3C_GPB_LEN);
return -ENOMEM;
}
for(i=0; i
/* Set GPBCON register, set correspond GPIO port as input or output mode */
gpb_con = s3c_gpio_read(GPBCON_OFFSET);
gpb_con &= ~(0x3<<(2*led[i])); /* Clear the currespond LED GPIO configure register */
gpb_con |= GPIO_OUTPUT<<(2*led[i]); /* Set the currespond LED GPIO as output mode */
s3c_gpio_write(gpb_con, GPBCON_OFFSET);
/* Set GPBUP register, set correspond GPIO port pull up resister as enable or disable */
gpb_up = s3c_gpio_read(GPBUP_OFFSET);
//gpb_up &= ~(0x1<
/* Set GPBDAT register, set correspond GPIO port power level as high level or low level */
gpb_dat = s3c_gpio_read(GPBDAT_OFFSET);
//gpb_dat &= ~(0x1<
}
return 0;
}
static void turn_led(int which, unsigned int cmd)
{
volatile unsigned long gpb_dat;
gpb_dat = s3c_gpio_read(GPBDAT_OFFSET);
if(LED_ON == cmd)
{
gpb_dat &= ~(0x1<
else if(LED_OFF == cmd)
{
gpb_dat |= (0x1<
s3c_gpio_write(gpb_dat, GPBDAT_OFFSET);
}
static void s3c_hw_term(void)
{
int i;
volatile unsigned long gpb_dat;
for(i=0; i
gpb_dat = s3c_gpio_read(GPBDAT_OFFSET);
gpb_dat |= (0x1<
}
release_mem_region(S3C_GPB_BASE, S3C_GPB_LEN);
iounmap(s3c_gpb_membase);
}
static int led_open(struct inode *inode, struct file *file)
{
int minor = iminor(inode);
file->private_data = (void *)minor;
printk(KERN_DEBUG "/dev/led%d opened.\n", minor);
return 0;
}
static int led_release(struct inode *inode, struct file *file)
{
printk(KERN_DEBUG "/dev/led%d closed.\n", iminor(inode));
return 0;
}
static void print_help(void)
{
printk("Follow is the ioctl() commands for %s driver:\n", DEV_NAME);
//printk("Enable Driver debug command: %u\n", SET_DRV_DEBUG);
printk("Turn LED on command : %u\n", LED_ON);
printk("Turn LED off command : %u\n", LED_OFF);
return;
}
static long led_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int which = (int)file->private_data;
switch (cmd)
{
case LED_ON:
turn_led(which, LED_ON);
break;
case LED_OFF:
turn_led(which, LED_OFF);
break;
default:
printk(KERN_ERR "%s driver don't support ioctl command=%d\n", DEV_NAME, cmd);
print_help();
break;
}
return 0;
}
static struct file_operations led_fops =
{
.owner = THIS_MODULE,
.open = led_open,
.release = led_release,
.unlocked_ioctl = led_ioctl,
};
static int __init s3c_led_init(void)
{
int result;
dev_t devno;
if( 0 != s3c_hw_init() )
{
printk(KERN_ERR "s3c2440 LED hardware initialize failure.\n");
return -ENODEV;
}
/* Alloc the device for driver */
if (0 != dev_major) /* Static */
{
devno = MKDEV(dev_major, 0);
result = register_chrdev_region (devno, dev_count, DEV_NAME);
}
else
{
result = alloc_chrdev_region(&devno, dev_minor, dev_count, DEV_NAME);
dev_major = MAJOR(devno);
}
/* Alloc for device major failure */
if (result < 0)
{
printk(KERN_ERR "S3C %s driver can't use major %d\n", DEV_NAME, dev_major);
return -ENODEV;
}
printk(KERN_DEBUG "S3C %s driver use major %d\n", DEV_NAME, dev_major);
if(NULL == (led_cdev=cdev_alloc()) )
{
printk(KERN_ERR "S3C %s driver can't alloc for the cdev.\n", DEV_NAME);
unregister_chrdev_region(devno, dev_count);
return -ENOMEM;
}
s3c_led.c
led_cdev->owner = THIS_MODULE;
cdev_init(led_cdev, &led_fops);
result = cdev_add(led_cdev, devno, dev_count);
if (0 != result)
{
printk(KERN_INFO "S3C %s driver can't reigster cdev: result=%d\n", DEV_NAME, result);
goto ERROR;
}
printk(KERN_ERR "S3C %s driver[major=%d] version %d.%d.%d installed successfully!\n",
DEV_NAME, dev_major, DRV_MAJOR_VER, DRV_MINOR_VER,DRV_REVER_VER);
return 0;
ERROR:
printk(KERN_ERR "S3C %s driver installed failure.\n", DEV_NAME);
cdev_del(led_cdev);
unregister_chrdev_region(devno, dev_count);
return result;
}
static void __exit s3c_led_exit(void)
{
dev_t devno = MKDEV(dev_major, dev_minor);
s3c_hw_term();
cdev_del(led_cdev);
unregister_chrdev_region(devno, dev_count);
printk(KERN_ERR "S3C %s driver version %d.%d.%d removed!\n",
DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER,DRV_REVER_VER);
return ;
}
/* These two functions defined in
module_init(s3c_led_init);
module_exit(s3c_led_exit);
module_param(debug, int, S_IRUGO);
module_param(dev_major, int, S_IRUGO);
MODULE_AUTHOR(DRV_AUTHOR);
MODULE_DESCRIPTION(DRV_DESC);
MODULE_LICENSE("GPL");
2、makefile文件的它要和你的内核一起才能工作,这里是linux-3.0,你进入linux-3.0目录后要先make一下才能在led目录下make,makefile文件代码如下所示,你有没有看到绿色的一行,你把它指定到你的内核目录就行,我的目录是home/fulinux/kernel/linux-3.0-fulinux/,所以是这样设置的
obj-m := s3c_led.o
KERNELDIR := ~/kernel/linux-3.0-fulinux/
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~core .depend .*.cmd *.ko *.mod.c .tmp_versions *odule* $(TARGET)
3、应用程序代码如下,编译的时候是没有用makefile文件的,你只需要用arm-linux-gcc -o led_test led_test.c命令或者你的编译器的绝对路径,我的是
/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc -o led_test led_test.c
/*********************************************************************************
* Copyright: (C) 2013 fulinux
* All rights reserved.
*
* Filename: led_test.c
* Description: This file is used to test led
*
* Version: 1.0.0(2013年04月04日~)
* Author: fulinux
* ChangeLog: 1, Release initial version on "2013年04月04日 14时19分53秒"
*
********************************************************************************/
#include
#include
#include
#include
#include
#ifndef __KERNEL__
#include
#endif
#define PLATDRV_MAGIC 0x60
#define LED_OFF _IO (PLATDRV_MAGIC, 0x18)
#define LED_ON _IO (PLATDRV_MAGIC, 0x19)
int main(int argc, char **argv)
{
int on;
int led_no;
int fd;
fd = open("/dev/led", 0);
if (fd < 0) {
fd = open("/dev/led", 0);
}
if (fd < 0) {
perror("open device led");
exit(1);
}
ioctl(fd, LED_ON, 0);
close(fd);
return 0;
}
4、编译完成后通过你的方式将s3c_led.ko 和led_test这两个执行文件送到开发板上,我是通过tftp服务器
tftp -gr s3c_led.ko 192.168.1.4
tftp -gr led_test 192.168.1.4
将文件送到开发板上的。
如果你的开发板网络没有配置好,输入如下命令:
ifconfig eth0 192.168.1.3 netmask 255.255.255.0
route add default gw 192.168.1.1
5、在开发板里执行:
insmod s3c_led.ko
然后显示如下信息说明安装成功
6、好了到了这里不要以为就可以运行led_test了,其实还没。你要先为驱动创建一个设备节点,怎么创建呢,先看下设备驱动的主设备号
我的led主设备号是253,
创建一个设备节点:
mknod /dev/led c 253 0
7、修改一下应用程序的权限,记住在PC上改变权限不行,要在开发板里改变
chmod 777 led_test
如果不修改权限会有出现如下信息:
8、最后执行运行程序
./led_test
你就会看到你的开发板上的LED亮了:-)
后话:
上面的程序执行后你会看到只有第一盏灯是亮着的,如果要想让第二盏灯亮,分析一下代码:
看一下高亮的地方,前面我们指定的次设备号(minor)是0,第二盏灯对应的次设备号是1,同时你要在应用程序里修改如下
编译后送到开发板中
就会发现开发板亮了第二个灯,这个应用程序很简陋,要继续优化才行:-(
************************************************************************************
下 篇:linux设备驱动篇之LED驱动(二)http://blog.csdn.net/sonbai/article/details/8761796
PC 平 台:ubuntu12.10
开 发 板:tq2440 fl2440
内核版本:linux-3.0
作 者:fulinux
************************************************************************************