Chinaunix首页 | 论坛 | 博客
  • 博客访问: 361062
  • 博文数量: 79
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 1370
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-12 08:48
个人简介

freedom~~~~~~~~~~

文章分类

全部博文(79)

文章存档

2014年(10)

2013年(2)

2012年(13)

2011年(54)

分类: 嵌入式

2012-02-13 01:16:43

led.c
  1. //#include <linux/config.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/fs.h>
  5. #include <linux/init.h>
  6. //#include <linux/devfs_fs_kernel.h>
  7. #include <linux/miscdevice.h>
  8. #include <linux/delay.h>
  9. #include <linux/gpio.h>
  10. #include <asm/irq.h>
  11. #include "regs-gpio.h"
  12. #include "hardware.h"
  13. #include <linux/cdev.h>
  14. #include <linux/mm.h>
  15. #define DEVICE_NAME "miniled"
  16. #define LED_MAJOR 254

  17. static int led_major = LED_MAJOR;
  18. struct mini_led_dev
  19. {
  20.      struct cdev cdev;
  21. };
  22. struct mini_led_dev *devp;

  23. static unsigned long mini_led_table[] =
  24. {
  25.     S3C2410_GPB(5),
  26.     S3C2410_GPB(6),
  27.     S3C2410_GPB(7),
  28.     S3C2410_GPB(8),

  29. };

  30. static unsigned long mini_led_cfg_table[] =
  31. {
  32.     S3C2410_GPIO_OUTPUT,
  33.     S3C2410_GPIO_OUTPUT,
  34.     S3C2410_GPIO_OUTPUT,
  35.     S3C2410_GPIO_OUTPUT,
  36. };

  37. int led_on(int arg)
  38. {
  39.     s3c2410_gpio_setpin(mini_led_table[arg], 0);
  40.     return 0;

  41. }
  42. int led_off(int arg)
  43. {
  44.     s3c2410_gpio_setpin(mini_led_table[arg], 1);
  45.     return 0;

  46. }
  47. static int mini_led_ioctl(struct inode *inode, struct file *file,
  48.                          unsigned int cmd, unsigned long arg)
  49. {
  50.     switch (cmd)

  51.     {
  52.     case 0:
  53.         led_off(arg);
  54.         break;
  55.     case 1:
  56.         led_on(arg);
  57.         break;
  58.     default:
  59.         return -1;


  60.     }

  61. }
  62. static int mini_led_open(struct inode *inode, struct file *filp)
  63. {
  64.     struct mini_led_dev *devp;

  65.     devp = container_of(inode->i_cdev, struct mini_led_dev,cdev);
  66.     filp->private_data = devp;

  67.     return 0;

  68. }
  69. static int mini_led_release(struct inode *inode, struct file *filp)
  70. {
  71.     return 0;

  72. }
  73. static struct file_operations mini_led_fops =
  74. {
  75.     .owner = THIS_MODULE,
  76.     .ioctl = mini_led_ioctl,
  77.     .open = mini_led_open,
  78.     .release = mini_led_release,

  79. };
  80. static void led_setup_cdev(struct mini_led_dev *dev, int index)

  81. {
  82.     //(借助结构体成员名初始化结构体)
  83.     int err;
  84.     int devno = MKDEV(led_major, index);
  85.     cdev_init(&dev->cdev, &mini_led_fops);
  86.     dev->cdev.owner = THIS_MODULE;
  87.     dev->cdev.ops = &mini_led_fops;
  88.     err = cdev_add(&dev->cdev, devno, 1);
  89.     if (err)

  90.     {
  91.         printk(KERN_NOTICE "Error %d adding led %d", err, index);

  92.     }

  93. }
  94. int mini_led_init(void)//(模块加载函数)

  95. {
  96.     int result;
  97.     int i;
  98.     dev_t devno = MKDEV(led_major, 0);
  99.     if (led_major)

  100.     {
  101.         result = register_chrdev_region(devno, 0, DEVICE_NAME);


  102.     }
  103.     else


  104.     {
  105.         result = alloc_chrdev_region(&devno, 0, 1, DEVICE_NAME);


  106.     }
  107.     if (result < 0)
  108.     {
  109.         return result;

  110.     }
  111.     devp = kmalloc(sizeof(struct mini_led_dev), GFP_KERNEL);
  112.     if (! devp)

  113.     {
  114.         result = -1;
  115.         goto fail_malloc;

  116.     }

  117.     for (i = 0; i < 4; i++)
  118.     {
  119.         s3c2410_gpio_cfgpin(mini_led_table[i], mini_led_cfg_table[i]);
  120.         s3c2410_gpio_setpin(mini_led_table[i], 1);

  121.     }
  122.     memset(devp, 0, sizeof(struct mini_led_dev));

  123.     led_setup_cdev(devp, 0);

  124.     return 0;
  125. fail_malloc:
  126.     unregister_chrdev_region(devno, 1);
  127.     return result;
  128. }
  129. void mini_led_exit(void)
  130. {
  131.     cdev_del(&devp->cdev);
  132.     kfree(devp);
  133.     unregister_chrdev_region(MKDEV(led_major, 0), 1);
  134. }
  135. MODULE_AUTHOR("more&high");
  136. MODULE_LICENSE("Dual BSD/GPL");
  137. module_init(mini_led_init);
  138. module_exit(mini_led_exit);

Makefile
  1. obj-m:=led.o
  2. KERNELDIR:=/work/coding/linux-2.6.32.10
  3. PWD:=$(shell pwd)
  4. modules:
  5. $(MAKE) -C $(KERNELDIR) M=$(PWD) ARCH=arm CROSS_COMPILE=arm-linux-
  6. #$(MAKE) -C $(KERNELDIR) M=$(PWD) ARCH=i386 CROSS_COMPILE=
  7. clean:
  8. rm *.o *mod.o *.order *.mod.c *symvers -f

app-led.c
  1. #include
  2. #include
  3. #include
  4. #include
  5. int main()
  6. {
  7. int fd;
  8. int num;
  9. int ope;
  10. fd = open("/dev/testled", 0);
  11. if (fd < 0)
  12. {
  13. perror("open miniled fail");
  14. exit(1);
  15. }
  16. for ( num = 0; num < 4; num++)
  17. {
  18. ioctl(fd, 0, num);
  19. }
  20. printf("please print the led num\n");
  21. scanf("%d", &num);
  22. printf("please print the operation\n");
  23. scanf("%d", &ope);
  24. ioctl(fd, ope, num);
  25. close(fd);
  26. return 0;
  27. }

其中""的头文件是我拷贝到本目录下面的
阅读(1098) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~