Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4462675
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: LINUX

2010-12-24 17:47:10

循环控制LED GPB5 的亮灭
  1. #include <linux/module.h>
  2. #include <linux/types.h>
  3. #include <linux/fs.h>
  4. #include <linux/errno.h>
  5. #include <linux/mm.h>
  6. #include <linux/sched.h>
  7. #include <linux/init.h>
  8. #include <linux/cdev.h>
  9. #include <asm/io.h>
  10. #include <asm/system.h>
  11. #include <asm/uaccess.h>

  12. #include<mach/regs-gpio.h>
  13. #include<mach/hardware.h>


  14. #define LED_MAJOR 244

  15. #define IOCTL_LED_ON     1
  16. #define IOCTL_LED_OFF    0

  17. static int led_major = LED_MAJOR;

  18. struct cdev *led_devp; /*设备结构体指针*/

  19. int led_open(struct inode *inode, struct file *filp)
  20. {
  21.   return 0;
  22. }

  23. int led_release(struct inode *inode, struct file *filp)
  24. {
  25.   return 0;
  26. }


  27. static int led_ioctl(struct inode *inodep, struct file *filp, unsigned
  28.   int cmd, unsigned long arg)
  29. {
  30.     switch(cmd)
  31.     {
  32.         case IOCTL_LED_ON:
  33.             s3c2410_gpio_setpin(S3C2410_GPB5,0);
  34.             return 0;
  35.         case IOCTL_LED_OFF:
  36.             s3c2410_gpio_setpin(S3C2410_GPB5,1);
  37.             return 0;
  38.         default:
  39.             return -EINVAL;
  40.             
  41.     }
  42. }

  43. void led_gpio_init(void)
  44. {
  45.     s3c2410_gpio_cfgpin(S3C2410_GPB5,S3C2410_GPB5_OUTP);
  46.     s3c2410_gpio_setpin(S3C2410_GPB5,0);
  47. }
  48. /*文件操作结构体*/
  49. static const struct file_operations led_fops =
  50. {
  51.   .owner = THIS_MODULE,
  52.   .ioctl = led_ioctl,
  53.   .open = led_open,
  54.   .release = led_release,
  55. };
  56. #if 0
  57. /*初始化并注册cdev*/
  58. static void led_setup_cdev(struct led_dev *dev, int index)
  59. {
  60.   int err, devno = MKDEV(led_major, index);

  61.   cdev_init(&dev->cdev, &led_fops);
  62.   dev->cdev.owner = THIS_MODULE;
  63.   dev->cdev.ops = &led_fops;
  64.   err = cdev_add(&dev->cdev, devno, 1);
  65.   if (err)
  66.     printk(KERN_NOTICE "Error %d adding LED%d", err, index);
  67. }

  68. #endif

  69. /*设备驱动模块加载函数*/
  70. int led_init(void)
  71. {
  72.   int result;
  73.   dev_t devno = MKDEV(led_major, 0);

  74.   /* 申请设备号*/
  75.   if (led_major)
  76.     result = register_chrdev_region(devno, 1, "led-control");
  77.   else /* 动态申请设备号 */
  78.   {
  79.     result = alloc_chrdev_region(&devno, 0, 1, "led-control");
  80.     led_major = MAJOR(devno);
  81.   }
  82.   if (result < 0)
  83.     return result;
  84.     
  85.   /* 动态申请设备结构体的内存*/
  86.  // led_devp = kmalloc(sizeof(struct led_dev), GFP_KERNEL);
  87.   led_devp=cdev_alloc();
  88.   if (!led_devp) /*申请失败*/
  89.   {
  90.     result = - ENOMEM;
  91.     goto fail_malloc;
  92.   }
  93.   memset(led_devp, 0, sizeof(struct cdev));
  94.  
  95. #if 0
  96.   led_setup_cdev(led_devp, 0);
  97. #endif
  98. /**********************************************/
  99.   cdev_init(led_devp,&led_fops);
  100.   led_devp->owner=THIS_MODULE;
  101.   led_devp->ops=&led_fops;
  102.   result=cdev_add(led_devp,MKDEV(led_major,0),1);
  103.   if(result)
  104.      printk(KERN_NOTICE "Error %d adding LED%d", result, 0);
  105. /*************************************************/
  106.   led_gpio_init();    
  107.   printk(KERN_INFO "init ok\n");
  108.   return 0;

  109.   fail_malloc: unregister_chrdev_region(devno, 1);
  110.   return result;
  111. }

  112. /*模块卸载函数*/
  113. void led_exit(void)
  114. {
  115.   cdev_del(led_devp); /*注销cdev*/
  116.   kfree(led_devp); /*释放设备结构体内存*/
  117.   unregister_chrdev_region(MKDEV(led_major, 0), 1); /*释放设备号*/
  118.   printk(KERN_INFO "bye the crul world!\n");
  119. }


  120. MODULE_LICENSE("Dual BSD/GPL");
  121. module_init(led_init);
  122. module_exit(led_exit);
应用程序
  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<stdlib.h>
  4. #include<sys/ioctl.h>
  5. #include<sys/types.h>
  6. #include<sys/stat.h>
  7. #include<fcntl.h>
  8. #include<sys/select.h>
  9. #include<sys/time.h>

  10. int main(void)
  11. {
  12.     int fp;
  13.     fp=open("/dev/led-control",0);
  14.     if(fp<0)
  15.     {
  16.         perror("open bad \n");
  17.         return 0;
  18.     }    
  19.     
  20.     while(1)
  21.     {
  22.         ioctl(fp,0);
  23.         printf("led off\n");
  24.         sleep(1);
  25.         ioctl(fp,1);
  26.         printf("led on\n");
  27.         sleep(1);
  28.     }
  29.     return 0;
  30. }


阅读(1165) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~