Chinaunix首页 | 论坛 | 博客
  • 博客访问: 404901
  • 博文数量: 83
  • 博客积分: 2011
  • 博客等级: 大尉
  • 技术积分: 741
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-04 22:51
文章分类

全部博文(83)

文章存档

2009年(83)

我的朋友

分类: LINUX

2009-04-22 20:43:40

经过两个小时的编写,终于成功的写完mini2440开发板led的驱动.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "miniled"
#define LED_MAJOR 254
static int led_major = LED_MAJOR;
struct mini_led_dev
{
 struct cdev cdev;
 
};
struct mini_led_dev *devp;
static unsigned long mini_led_table[] = {
 S3C2410_GPB5,
 S3C2410_GPB6,
 S3C2410_GPB7,
 S3C2410_GPB8,
};
static unsigned long mini_led_cfg_table[] = {
 S3C2410_GPB5_OUTP,
 S3C2410_GPB6_OUTP,
 S3C2410_GPB7_OUTP,
 S3C2410_GPB8_OUTP,
};

int led_on(int arg)
{
 s3c2410_gpio_setpin(mini_led_table[arg], 0);
 return 0;
}
int led_off(int arg)
{
 s3c2410_gpio_setpin(mini_led_table[arg], 1);
 return 0;
}
static int mini_led_ioctl(struct inode *inode, struct file *file,
      unsigned int cmd, unsigned long arg)
{
 switch(cmd)
 {
  case 0:
   led_off(arg);
   break;
  case 1:
   led_on(arg);
   break;
  default:
   return -1; 
 }
}
static int mini_led_open(struct inode *inode, struct file *filp)
{
 struct mini_led_dev *devp;
 
 devp = container_of(inode->i_cdev, struct mini_led_dev,cdev);
 filp->private_data = devp;
 
 return 0;
}
static int mini_led_release(struct inode *inode, struct file *filp)
{
 return 0;
}
static struct file_operations mini_led_fops = {
 .owner = THIS_MODULE,
 .ioctl = mini_led_ioctl,
 .open = mini_led_open,
 .release = mini_led_release,
};
static void led_setup_cdev(struct mini_led_dev *dev, int index)
{
 int err;
 int devno = MKDEV(led_major, index);
 cdev_init(&dev->cdev, &mini_led_fops);
 dev->cdev.owner = THIS_MODULE;
 dev->cdev.ops = &mini_led_fops;
 err = cdev_add(&dev->cdev, devno, 1);
 if (err)
 {
  printk(KERN_NOTICE "Error %d adding led %d", err, index);
 }
}
int mini_led_init(void)
{
 int result;
 int i;
 dev_t devno = MKDEV(led_major, 0);
 if (led_major)
 {
  result = register_chrdev_region(devno, 0, DEVICE_NAME); 
 }
 else
 {
  result = alloc_chrdev_region(&devno, 0, 1, DEVICE_NAME);
 }
 if (result < 0)
 { 
  return result;
 }
 devp = kmalloc(sizeof(struct mini_led_dev), GFP_KERNEL);
 if (! devp)
 {
  result = -1;
  goto fail_malloc;
 }
 
 for (i = 0; i < 4; i++)
 {
  s3c2410_gpio_cfgpin(mini_led_table[i], mini_led_cfg_table[i]);
  s3c2410_gpio_setpin(mini_led_table[i], 1);
 }
 memset(devp, 0, sizeof(struct mini_led_dev));
 
 led_setup_cdev(devp, 0);
 
 return 0; 
 
 fail_malloc:
  unregister_chrdev_region(devno, 1);
  return result;
}
void mini_led_exit(void)
{
 cdev_del(&devp->cdev);
 kfree(devp);
 unregister_chrdev_region(MKDEV(led_major, 0), 1);
}
MODULE_AUTHOR("more&high");
MODULE_LICENSE("Dual BSD/GPL");
module_init(mini_led_init);
module_exit(mini_led_exit);
 
 
以下是Makefile:
obj-m := led.o
KDIR := /opt/friendlyarm/mini2440/kernel-2.6.13
PWD := $(shell pwd)
CC = arm-linux-gcc
default:
 $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
 rm -rf *.o
 rm -rf *.ko
 rm -rf .*.cmd
 rm -rf *.mod.*
 
make
 
成功
 
mknod /dev/testled c 254 0
 
insmod led.ko
 
以下是应用程序
 
#include
#include
#include
#include
int main()
{
 int fd;
 int num;
 int ope;
 
 fd = open("/dev/testled", 0);
 if (fd < 0)
 {
  perror("open miniled fail");
  exit(1);
 }
 for ( num = 0; num < 4; num++)
 {
  ioctl(fd, 0, num);
 }
 printf("please print the led num\n");
 scanf("%d", &num);
 printf("please print the operation\n");
 scanf("%d", &ope);
 ioctl(fd, ope, num);
 
 close(fd);
 
 return 0;
}
 
阅读(2894) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~