天嵌 ARM开发社区

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

GPH3_2 & GPH3_4的gpio_direction_input輸出一值為零

[复制链接]
s1005056 发表于 2014-5-9 19:14:51 | 显示全部楼层 |阅读模式
本帖最后由 s1005056 于 2014-5-13 16:59 编辑

版大以及各位版友好,

目前想練習做兩個按鍵,接key 6x6的3.3V以及KP_ROW1(GPH3_5),然後利用KP_ROW2(GPH3_2)跟KP_ROW4(GPH3_4)讀取管腳電平。在參考LED驅動範本後,實作如下的程式碼:

  1. #include <linux/miscdevice.h>
  2. #include <linux/input.h>
  3. #include <linux/clk.h>
  4. #include <linux/delay.h>
  5. #include <asm/io.h>
  6. #include <asm/uaccess.h>

  7. #include <mach/map.h>
  8. #include <mach/gpio.h>
  9. //#include <mach/gpio-bank.h>
  10. #include <mach/regs-gpio.h>
  11. #include <plat/gpio-core.h>
  12. #include <plat/gpio-cfg.h>
  13. #include <plat/gpio-cfg-helpers.h>

  14. #define DEVICE_NAME "button"

  15. #define IOCTL_GPIO_READ_STATUS        1

  16. /*assign pins */
  17. static unsigned long gpio_table [] =
  18. {
  19.         S5PV210_GPH3(5), /* high voltage output*/
  20.         S5PV210_GPH3(2), /* input */
  21.         S5PV210_GPH3(4), /* input */
  22. };

  23. static unsigned int gpio_cfg_table [] =
  24. {
  25.         S3C_GPIO_SFN(1),
  26.         S3C_GPIO_SFN(0),
  27.         S3C_GPIO_SFN(0),
  28. };

  29. static long tq210_btn_ioctl(
  30.         struct file *file,
  31.         unsigned int cmd,
  32.         unsigned long arg)
  33. {
  34.         arg -= 1;
  35.         if (arg > sizeof(gpio_table)/sizeof(unsigned long))
  36.         {
  37.                 return -EINVAL;
  38.         }

  39.         switch(cmd)
  40.         {
  41.                 case IOCTL_GPIO_READ_STATUS:
  42.                         printk(KERN_INFO "pin[%ld] status= %d\n", arg, gpio_direction_input(gpio_table[arg]));
  43.                         return 0;


  44.                 default:
  45.                         return -EINVAL;
  46.         }
  47. }

  48. static int tq210_btn_open(struct inode *inode, struct file *file)
  49. {
  50.         int i;
  51.         int err;
  52.         /* high voltage supply */
  53.         err = gpio_request(gpio_table[0], "GPH3_5");
  54.         if(err)
  55.         {
  56.                 printk(KERN_ERR "failed to request GPH3_5 pin and name\n");
  57.         return err;
  58.         }
  59.         
  60.         /* set as input pin */
  61.         err = gpio_request(gpio_table[1], "GPH3_2");
  62.         if(err)
  63.         {
  64.                 printk(KERN_ERR "failed to request GPH3_2 pin and name\n");
  65.         return err;
  66.         }
  67.         err = gpio_request(gpio_table[2], "GPH3_4");
  68.         if(err)
  69.         {
  70.                 printk(KERN_ERR "failed to request GPH3_4 pin and name\n");
  71.         return err;
  72.         }
  73.         printk(KERN_INFO " buttons opened\n");

  74.         
  75.         for (i = 0; i < sizeof(gpio_table)/sizeof(unsigned long); i++)
  76.         {
  77.                 s3c_gpio_cfgpin(gpio_table[i], gpio_cfg_table[i]);
  78.         }

  79.         return 0;

  80. }

  81. static int tq210_btn_close(struct inode *inode, struct file *file)
  82. {
  83.         int i;
  84.         
  85.         for (i = 0; i < sizeof(gpio_table)/sizeof(unsigned long); i++)
  86.         {
  87.                 gpio_free(gpio_table[i]);
  88.         }
  89.         printk(KERN_INFO "TQ210 BTNs driver successfully close\n");
  90.         return 0;
  91. }

  92. static struct file_operations dev_fops = {
  93.         .owner        =        THIS_MODULE,
  94.         .unlocked_ioctl        =        tq210_btn_ioctl,
  95.         .open = tq210_btn_open,
  96. //                .read = tq210_btn_read,
  97.         .release = tq210_btn_close,
  98. };

  99. static struct miscdevice misc_btn = {
  100.         .minor = MISC_DYNAMIC_MINOR,
  101.         .name = DEVICE_NAME,
  102.         .fops = &dev_fops,
  103. };

  104. static int __init button_init(void){
  105.         int ret;

  106.         ret = misc_register(&misc_btn);

  107.         if(ret < 0)
  108.         {
  109.                
  110.         }else{
  111.                 printk(KERN_INFO "TQ210 buttons driver successfully probed\n");
  112.         }
  113.         

  114.         return ret;
  115. }

  116. static void __exit button_exit(void)
  117. {
  118.         misc_deregister(&misc_btn);

  119.         int i;

  120.         for (i = 0; i < sizeof(gpio_table)/sizeof(unsigned long); i++)
  121.         {
  122.                 gpio_free(gpio_table[i]);
  123.         }
  124.         printk(KERN_INFO "TQ210 buttons driver successfully exit\n");
  125. }

  126. module_init(button_init);
  127. module_exit(button_exit);
  128. MODULE_LICENSE("GPL");
复制代码

應用程式則如下:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <asm/types.h>
  5. #include <linux/fb.h>
  6. #include <linux/kernel.h>
  7. #include <linux/fs.h>
  8. #include <fcntl.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <sys/mman.h>
  12. #include <sys/poll.h>

  13. #define DEV_FILE_NAME                "/dev/button"        //驱动提供

  14. #define IOCTL_GPIO_READ_STATUS        1                //驱动提供,表示亮


  15. int main(){
  16.         int devfd;
  17.         int i,err;
  18.         
  19.         devfd = open(DEV_FILE_NAME,O_RDWR);        
  20.         if(devfd < 0)        
  21.         {
  22.                 printf("can't open dev (%s)",DEV_FILE_NAME);
  23.                 return 0;
  24.         }
  25.         
  26.         for(i=0;i<10;++i)
  27.         {
  28.                 err = ioctl(devfd,IOCTL_GPIO_READ_STATUS,1);
  29.                 if(err<0)
  30.                         printf("GPIO_READ GPH3(2) faild! (%d)\n",err);                        
  31.                 sleep(1);        //休眠1S
  32.                 err = ioctl(devfd,IOCTL_GPIO_READ_STATUS,2);
  33.                 if(err<0)
  34.                         printf("GPIO_READ GPH3(4) faild! (%d)\n",err);                        
  35.                 sleep(1);        //休眠1S
  36.         }
  37.         close(devfd);//关闭设备。对应驱动中的tq210_gpio_close

  38.         
  39.         return 0;
  40. }
复制代码

但是在ternimal中的輸出都一直顯示為0,如下圖:



看了半天還是不知道哪裏出錯了,還請版上各位高手指點! 謝謝!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
 楼主| s1005056 发表于 2014-5-12 15:08:13 | 显示全部楼层
煩請管理員刪掉這篇帖子,已經沒事了,感謝。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

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

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

GMT+8, 2024-5-7 08:04 , Processed in 1.031244 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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