天嵌 ARM开发社区

 找回密码
 注册
查看: 1328|回复: 1

arm开发板的linux下led驱动问题,求大神看看!!!!!

[复制链接]
xmkk 发表于 2013-11-14 11:52:35 | 显示全部楼层 |阅读模式
写了一个TQ2440的led驱动,加载后运行测试程序,板子上的led灯还是按照开机时那样闪烁,是不是因为设备被占用,导致我的测试程序无效;另外,开机时led灯为什么会那样二进制计数那样的亮呢?请大神解答!!
驱动程序
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/fs.h>
  4. #include <linux/init.h>
  5. #include <linux/delay.h>
  6. #include <asm/uaccess.h>
  7. #include <asm/irq.h>
  8. #include <asm/io.h>
  9. #include <linux/device.h>       //for udev
  10. #include <mach/regs-gpio.h>  //gpio

  11. MODULE_LICENSE("GPL");               

  12. static struct class *led_driv_class;                //定义一个类

  13. static unsigned int led_table[]=
  14. {
  15.         S3C2410_GPB5,
  16.         S3C2410_GPB6,
  17.         S3C2410_GPB7,
  18.         S3C2410_GPB8
  19. };

  20. static unsigned int led_cfg_table[]=
  21. {
  22.         S3C2410_GPB5_OUTP,
  23.         S3C2410_GPB6_OUTP,
  24.         S3C2410_GPB7_OUTP,
  25.         S3C2410_GPB8_OUTP
  26. };

  27. static int led_driv_open(struct inode *inode,struct file *file)
  28. {
  29.         int i;
  30.        
  31.         //配置GPB5,6,7,8为输出,且输出0,低电平
  32.         for(i=0; i<4; i++)
  33.         {
  34.                 s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
  35.                 s3c2410_gpio_setpin(led_table[i], 0);
  36.         }
  37.        
  38.         return 0;
  39. }

  40. static int led_driv_write(struct file *file,const char __user *buf,size_t count,loff_t *ppos)
  41. {
  42.         int val,i;

  43.         copy_from_user(&val, buf, count);    //从用户空间拷贝数据
  44.         if(val==1)
  45.         {
  46.                 //点灯
  47.                 for(i=0;i<4;i++)
  48.                 {
  49.                         s3c2410_gpio_setpin(led_table[i], 0);
  50.                 }
  51.         }
  52.         else
  53.         {
  54.                 //灭灯
  55.                 for(i=0;i<4;i++)
  56.                 {
  57.                         s3c2410_gpio_setpin(led_table[i], 1);
  58.                 }
  59.         }
  60.        
  61.         return 0;
  62. }

  63. static struct file_operations led_driv_fops={
  64.         .owner = THIS_MODULE,
  65.         .open = led_driv_open,
  66.         .write = led_driv_write,
  67. };

  68. int major;

  69. static int __init led_driv_init(void)
  70. {

  71.     printk("<1>\n     Hello,Led drive!\n");
  72.     major=register_chrdev(0, "led_dev", &led_driv_fops);
  73.        
  74.     //新建类
  75.     led_driv_class = class_create(THIS_MODULE, "led_dev");
  76.     if(IS_ERR(led_driv_class))
  77.                 return PTR_ERR(led_driv_class);
  78.     //创建设备
  79.    device_create(led_driv_class,NULL,MKDEV(major, 0),NULL,"led_dev");

  80.     return 0;
  81. }

  82. static void __exit led_driv_exit(void)
  83. {
  84.     printk("<1>\n     Exit!\n");
  85.     //删除设备结点
  86.     device_destroy(led_driv_class,MKDEV(major, 0));
  87.     class_destroy(led_driv_class);
  88.        
  89.     unregister_chrdev(major, "led_dev");
  90. }                                    

  91. module_init(led_driv_init);
  92. module_exit(led_driv_exit);

  93. MODULE_LICENSE("GPL");
复制代码

测试程序代码
  1. #include<stdio.h>
  2. #include<sys/types.h>
  3. #include<sys/stat.h>
  4. #include<fcntl.h>
  5. int main()
  6. {
  7.         int fd,val;

  8.         if((fd=open("/dev/led_dev",O_RDWR))<0)
  9.         {
  10.                  printf("open device failed");
  11.         }
  12.         printf("type 1 to turn on the led,0 to turn of it!");
  13.         scanf("%d",&val);
  14.         write(fd,&val,4);

  15.         return 0;
  16. }
复制代码
亚瑟王 发表于 2013-11-14 18:21:59 | 显示全部楼层
修改文件系统中的/etc/init.d/rcS文件,
在/etc/rc.d/init.d/leds start前加#号屏蔽掉该行即可。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号-2 )

GMT+8, 2024-9-28 11:23 , Processed in 1.040079 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表