您的位置:首页 > 其它

FL2440字符设备驱动之LED

2015-09-13 12:35 302 查看
今天开始我们来学习linux驱动的开发,驱动分为字符设备驱动,块设备,网络设备驱动,讲这个之前我说一下我用的虚拟机版本和LInux内核版本,开始我用的redhat 9.0  开始用的好好的,到后来自己编译busybox的时候总是出错误,这个期间我尝试了很多次,在网上也找到了很多方法,可还是解决不了问题,后来找到了原因是虚拟机版本的问题,后来我换了ubuntu9.10,那些错误就没有了。

在这里我我在说明一下FL2440光盘自带的Linux2.6.28.7是不是支持自动创建设备节点的,需要在/etc/init.d/rcS中添加  mdev -s 。学习的同时可以参考韦东山老师的视频,讲得非常好,注意下我们的代码和他的代码有一点区别,就是一些函数可能用的不同(是内核版本的问题),以后我讲的驱动都是用的Linux2.6.28.7,这样也方便你们学习。

这些代码你直接copy上去都是可以用的废话不多说了,直接上代码:

/s3c2440_leds.c

//#include <linux/config.h>

#include <linux/module.h>

#include <linux/kernel.h>

#include <linux/fs.h>

#include <linux/init.h>

#include <linux/device.h>

//#include <linux/devfs_fs_kernel.h>

#include <linux/miscdevice.h>

#include <linux/delay.h>

#include <asm/irq.h>

#include <asm/io.h>

#include <asm/uaccess.h>

#include <mach/regs-gpio.h>

#include <mach/hardware.h>

#include <linux/device.h>

#include <linux/gpio.h>

#define DEVICE_NAME "leds"

#define LED_MAJOR 231

#define LED_OFF         0

#define LED_ON         1

#define ALL_LED_OFF      3

#define ALL_LED_ON       4

MODULE_LICENSE("Dual BSD/GPL");

static struct class *led_2;

static unsigned long led_table [] = {

S3C2410_GPB5,

S3C2410_GPB6,

S3C2410_GPB8,

S3C2410_GPB10,

};

/*等价于设置GPFCON*/

static unsigned int led_cfg_table [] = {

S3C2410_GPB5_OUTP, //0x01<<10 defined in refg-gpio.h

S3C2410_GPB6_OUTP,

S3C2410_GPB8_OUTP,

S3C2410_GPB10_OUTP,

};

static int s3c2440_leds_ioctl( struct inode *inode, struct file *file,unsigned int cmd,unsigned long arg)

{

     int i;
if (arg > 4)
{
return -EINVAL;
}
switch(cmd)
{
case LED_ON:  //set the pin
s3c2410_gpio_setpin(led_table[arg], 0);
break;

case LED_OFF:  //clr the pin
s3c2410_gpio_setpin(led_table[arg], 1);
break;

case ALL_LED_ON:  //set all pin
for (i = 0; i < 4; i++)
s3c2410_gpio_setpin(led_table[i], 0);
break;

case ALL_LED_OFF:  //clr all pin
for (i = 0; i < 4; i++)
s3c2410_gpio_setpin(led_table[i], 1);
break;

default:
return -EINVAL;
}

}

static struct file_operations s3c2440_leds_fops = {

.owner = THIS_MODULE,

.ioctl = s3c2440_leds_ioctl,

};

 static int major;

static int __init s3c2440_leds_init(void)

{

  int i;

major = register_chrdev(0, DEVICE_NAME, &s3c2440_leds_fops);

if (major < 0) 
{

   printk(DEVICE_NAME " can't register major number\n");

   return  major;

}

       led_2 = class_create(THIS_MODULE, DEVICE_NAME);
device_create(led_2, NULL, MKDEV(major, 0), NULL, "led_2"); 

for (i = 0; i < 4 ; i++)
{

s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);

s3c2410_gpio_setpin(led_table[i], 1); 

}

printk(DEVICE_NAME " initialized\n");

return 0;

}

static void __exit s3c2440_leds_exit(void)

{
unregister_chrdev(major, DEVICE_NAME);
device_destroy(led_2,MKDEV(major, 0));
class_destroy(led_2);

}

module_init(s3c2440_leds_init);

module_exit(s3c2440_leds_exit);

测试程序:

#include "stdio.h"

#include "unistd.h"

#include "sys/types.h"

#include "sys/ioctl.h"

#include "stdlib.h"

#include "termios.h"

#include "sys/stat.h"

#include "fcntl.h"

#include "sys/time.h"

int main(int argc, char **argv)

{
unsigned int on;
unsigned int led_num;
int fd;
fd=open("/dev/led_2",O_RDWR);//打开文件返回值是个非负值
if (fd < 0)
{
printf("open device led");
      return -1;
}

      printf("%s <on/off>\n",argv[0]);

  
if (argc == 2)

{
sscanf(argv[1], "%d", &on);
if (on < 2)
{
ioctl(fd, on+3, 0);
}
else
{
printf("Usage: led led_num 0|1  or  led 0|1\n");
exit(1);
}
}

if (argc == 3)
{
sscanf(argv[1], "%d", &led_num);
sscanf(argv[2], "%d", &on);
if ((on < 2) && (led_num>0 || led_num < 4))
ioctl(fd, on, (led_num-1));
else
{
printf("Usage: led led_num 0|1  or  led 0|1\n");
exit(1);
}
}

close(fd);
return 0;

}

在编写个Makefile:

obj-m :=test.o

KERNELDIR ?= /home/work/Linux/linux-2.6.28.7//内核路径

PWD := $(shell pwd)

default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
rm -f  *o  *.mod.o  *mod.c  *.symvers *.order
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: