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

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: 嵌入式

2011-04-24 13:45:52

附件:程序     附件.rar 



说明: 目标内核 2.6.33
       开发板   :TQ2440

       tq移植手册中的 内核是2.6.30.4 ,与2.6.33 不一样,在按照tq 移植手册

中的 led.c 移植时候,总出现  下面的错误

  1. error: 'S3C2410_GPB5_OUTP' undeclared here (not in a function)
       所以参考了网上强大 的资源

Linux2.6.33 自己动手写驱动实现了自己 led 功能。




硬件说明:
         tq 开发板 led GPB5 6 7 8 是led 控制管脚,当低电平时,led亮。


1. 编写 led 驱动程序

  1. root@yuweixian:/opt/me_linux/linux-2.6.33# cd drivers/ywx-driver/
  2. root@yuweixian:/opt/me_linux/linux-2.6.33/drivers/ywx-driver# gedit led.c
 led.c 文件内容
  1. #include <linux/miscdevice.h>

  2. #include <linux/delay.h>

  3. #include <asm/irq.h>

  4. #include <mach/regs-gpio.h>

  5. #include <mach/hardware.h>

  6. #include <linux/kernel.h>

  7. #include <linux/module.h>

  8. #include <linux/init.h>

  9. #include <linux/mm.h>

  10. #include <linux/fs.h>

  11. #include <linux/types.h>

  12. #include <linux/delay.h>

  13. #include <linux/moduleparam.h>

  14. #include <linux/slab.h>

  15. #include <linux/errno.h>

  16. #include <linux/ioctl.h>

  17. #include <linux/cdev.h>

  18. #include <linux/string.h>

  19. #include <linux/list.h>

  20. #include <linux/pci.h>

  21. #include <asm/uaccess.h>

  22. #include <asm/atomic.h>

  23. #include <asm/unistd.h>

  24. #include <linux/gpio.h>

  25.  

  26. #define DEVICE_NAME "led-control"


  27. #define S3C2410_GPBCON S3C2410_GPIOREG(0x10)

  28. #define S3C2410_GPBDAT S3C2410_GPIOREG(0x14)

  29. #define S3C2410_GPBUP S3C2410_GPIOREG(0x18)

  30.  
  31. #define S3C2410_GPB5 S3C2410_GPIONO(S3C2410_GPIO_BANKB, 5)

  32. #define S3C2410_GPB6 S3C2410_GPIONO(S3C2410_GPIO_BANKB, 6)

  33. #define S3C2410_GPB7 S3C2410_GPIONO(S3C2410_GPIO_BANKB, 7)

  34. #define S3C2410_GPB8 S3C2410_GPIONO(S3C2410_GPIO_BANKB, 8)

  35.  

  36. #define S3C2410_GPB5_OUTP (0x01 << 10)

  37. #define S3C2410_GPB6_OUTP (0x01 << 12)

  38. #define S3C2410_GPB7_OUTP (0x01 << 14)

  39. #define S3C2410_GPB8_OUTP (0x01 << 16)

  40. /* 应用程序执行ioctl(fd, cmd, arg)时的第2个参数 */

  41. #define IOCTL_GPIO_ON 1

  42. #define IOCTL_GPIO_OFF 0


  43. /* 用来指定LED所用的GPIO引脚 */

  44. static unsigned long gpio_table [] =

  45. {
  46.          S3C2410_GPB(5),

  47.          S3C2410_GPB(6),

  48.          S3C2410_GPB(7),

  49.          S3C2410_GPB(8),

  50. };

  51. /* 用来指定GPIO引脚的功能:输出 */

  52. static unsigned int gpio_cfg_table [] =

  53. {

  54. //由于2.6.33内核中没有定义GPIO的动作,所以我在上面自己定义了一下。

  55.           S3C2410_GPB5_OUTP,

  56.           S3C2410_GPB6_OUTP,

  57.           S3C2410_GPB7_OUTP,

  58.           S3C2410_GPB8_OUTP,

  59. };

  60.  

  61. static int te2440_gpio_ioctl(

  62.          struct inode *inode,

  63.          struct file *file,

  64.          unsigned int cmd,

  65.          unsigned long arg)

  66. {

  67.          if (arg > 4)

  68.          {

  69.                    return -EINVAL;

  70.          }

  71.          switch(cmd)

  72.          {

  73.                    case IOCTL_GPIO_ON:

  74.                             // 设置指定引脚的输出电平为0

  75.                             s3c2410_gpio_setpin(gpio_table[arg], 0);

  76.                             return 0;

  77.  

  78.                    case IOCTL_GPIO_OFF:

  79.                             // 设置指定引脚的输出电平为1

  80.                             s3c2410_gpio_setpin(gpio_table[arg], 1);

  81.                             return 0;

  82.  

  83.                    default:

  84.                             return -EINVAL;

  85.          }

  86. }

  87.  

  88. static struct file_operations dev_fops = {

  89.          .owner = THIS_MODULE,

  90.          .ioctl = te2440_gpio_ioctl,

  91. };

  92.  

  93. static struct miscdevice misc = {

  94.          .minor = MISC_DYNAMIC_MINOR,

  95.          .name = DEVICE_NAME,

  96.          .fops = &dev_fops,

  97. };

  98.  

  99. static int __init dev_init(void)

  100. {

  101.          int ret;
  102.          int i;
  103.          for (i = 0; i < 4; i++)
  104.          {
  105.                    s3c2410_gpio_cfgpin(gpio_table[i], gpio_cfg_table[i]);

  106.                    s3c2410_gpio_setpin(gpio_table[i], 0);

  107.          }
  108.          ret = misc_register(&misc);
  109.          printk (DEVICE_NAME" initialized\n");
  110.          return ret;
  111. }
  112. static void __exit dev_exit(void)
  113. {

  114.          misc_deregister(&misc);

  115. }

  116. module_init(dev_init);
  117. module_exit(dev_exit);

  118. MODULE_LICENSE("GPL");

  119. MODULE_AUTHOR("");

  120. MODULE_DESCRIPTION("GPIO control for Topelec TE2440DEV_I Board");




2. 修改 Kconfig  Makefile

修改 drivers/ywx-driver/Kconfig

  1. config LED
  2.     tristate "led test "    
  3.     help
  4.      this is led test
修改 drivers/ywx-driver/Makefile
  1. obj-$(CONFIG_HELLO)        += hello.o
  2. obj-$(CONFIG_LED)        += led.o

3.编译
    make menuconfig,
    make zImage

    下载到开发板中

4.生成 .ko文件


    make SUBDIR=drivers/ywx-driver/ modules

    在 dirvers/ywx-driver/ 生成 led.ko 文件,复制到开发板 /lib/mouldes/2.6.33-yuweixian/



5. 编写 应用程序,测试驱动 可用否

   应用程序 是 tq 提供的代码 leds.c
   arm-linux-gcc -o leds leds.c
   cp leds /mnt/hgfs/share-linux/down/leds-application

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/ioctl.h>

  5. int main(int argc, char **argv)
  6. {
  7.     int on;
  8.     int led_no;
  9.     int fd;
  10.     if (argc != 3 || sscanf(argv[1], "%d", &led_no) != 1 || sscanf(argv[2],"%d", &on) != 1 ||
  11.      on < 0 || on > 1 || led_no < 1 || led_no > 4) {
  12.         fprintf(stderr, "Usage: leds led_no 0|1\n");
  13.         exit(1);
  14.     }
  15.     fd = open("/dev/led-control", 0);
  16.     if (fd < 0) {
  17.         perror("open device leds");
  18.         exit(1);
  19.     }
  20.     ioctl(fd, on, (led_no-1));
  21.     close(fd);
  22.     return 0;
  23. }


6.测试

  1. [root@yuweixian 2.6.33-yuweixian]# ls
  2. hello.ko led.ko leds-applicaiton modules.dep.bb
  3. [root@yuweixian 2.6.33-yuweixian]# insmod led.ko
  4. led-control initialized
  5. [root@yuweixian 2.6.33-yuweixian]# lsmod
  6. led 881 0 - Live 0xbf00c000
  7. [root@yuweixian 2.6.33-yuweixian]# ./leds-applicaiton 2 0
  8. [root@yuweixian 2.6.33-yuweixian]# ./leds-applicaiton 3 0
  9. [root@yuweixian 2.6.33-yuweixian]# ./leds-applicaiton 3 1
  10. [root@yuweixian 2.6.33-yuweixian]# rmmod led
  11. [root@yuweixian 2.6.33-yuweixian]# lsmod
  12. [root@yuweixian 2.6.33-yuweixian]#



























 


































      









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