Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2117178
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: Android平台

2015-10-30 16:09:26

一.mtk平台的gpio控制
1.1 内核中的gpio配置
//设置gpio18为GPIO模式
mt_set_gpio_mode(GPIO18, GPIO_MODE_GPIO); 
//设置gpio18方向为out
mt_set_gpio_dir(GPIO18, GPIO_DIR_OUT);
//设置gpio18高
mt_set_gpio_out(GPIO18, GPIO_OUT_ONE);  //低是ZERO
1.2 上层应用控制gpio的高低
  1. cong@msi:/work/mtk/temp/gpiotest$ cat gpiotest.c
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <sys/time.h>
  7. #include <dirent.h>
  8. #include <unistd.h>
  9. #include <string.h>
  10. #include <sys/ioctl.h>
  11. #include <linux/ioctl.h>

  12. #define dbmsg(fmt, args ...) printf("%s:%s[%d]: "fmt"\n", __FILE__,__FUNCTION__, __LINE__,##args)

  13. #define GPIO_IOC_MAGIC 0x90
  14. #define GPIO_IOCQMODE _IOR(GPIO_IOC_MAGIC, 0x01, uint32_t)
  15. #define GPIO_IOCTMODE0 _IOW(GPIO_IOC_MAGIC, 0x02, uint32_t)
  16. #define GPIO_IOCTMODE1 _IOW(GPIO_IOC_MAGIC, 0x03, uint32_t)
  17. #define GPIO_IOCTMODE2 _IOW(GPIO_IOC_MAGIC, 0x04, uint32_t)
  18. #define GPIO_IOCTMODE3 _IOW(GPIO_IOC_MAGIC, 0x05, uint32_t)
  19. #define GPIO_IOCQDIR _IOR(GPIO_IOC_MAGIC, 0x06, uint32_t)
  20. #define GPIO_IOCSDIRIN _IOW(GPIO_IOC_MAGIC, 0x07, uint32_t)
  21. #define GPIO_IOCSDIROUT _IOW(GPIO_IOC_MAGIC, 0x08, uint32_t)
  22. #define GPIO_IOCQPULLEN _IOR(GPIO_IOC_MAGIC, 0x09, uint32_t)
  23. #define GPIO_IOCSPULLENABLE _IOW(GPIO_IOC_MAGIC, 0x0A, uint32_t)
  24. #define GPIO_IOCSPULLDISABLE _IOW(GPIO_IOC_MAGIC, 0x0B, uint32_t)
  25. #define GPIO_IOCQPULL _IOR(GPIO_IOC_MAGIC, 0x0C, uint32_t)
  26. #define GPIO_IOCSPULLDOWN _IOW(GPIO_IOC_MAGIC, 0x0D, uint32_t)
  27. #define GPIO_IOCSPULLUP _IOW(GPIO_IOC_MAGIC, 0x0E, uint32_t)
  28. #define GPIO_IOCQINV _IOR(GPIO_IOC_MAGIC, 0x0F, uint32_t)
  29. #define GPIO_IOCSINVENABLE _IOW(GPIO_IOC_MAGIC, 0x10, uint32_t)
  30. #define GPIO_IOCSINVDISABLE _IOW(GPIO_IOC_MAGIC, 0x11, uint32_t)
  31. #define GPIO_IOCQDATAIN _IOR(GPIO_IOC_MAGIC, 0x12, uint32_t)
  32. #define GPIO_IOCQDATAOUT _IOR(GPIO_IOC_MAGIC, 0x13, uint32_t)
  33. #define GPIO_IOCSDATALOW _IOW(GPIO_IOC_MAGIC, 0x14, uint32_t)
  34. #define GPIO_IOCSDATAHIGH _IOW(GPIO_IOC_MAGIC, 0x15, uint32_t)
  35.     
  36. int main ( int argc, char *argv[] )
  37. {
  38.     int fd_gpio;
  39.     int ret;
  40.     
  41.     fd_gpio = open("/dev/mtgpio",O_RDWR);
  42.     if(fd_gpio < 0)
  43.     {
  44.      printf("fd_gpio open error\n");
  45.         return -1;
  46.     }

  47.     while(1)
  48.     {
  49.         ret = ioctl(fd_gpio, GPIO_IOCSDATAHIGH, 18);
  50.         sleep(1);
  51.         ret = ioctl(fd_gpio, GPIO_IOCSDATAHIGH, 18);
  52.         sleep(1);
  53.     }

  54.     close(fd_gpio);
  55.     return 0;
  56. }
1.3 应用代码
 gpiotest.rar (下载后改名为gpiotest.tar.gz)
1.4 查看gpio状态
/sys/class/misc/mtgpio/pin
  1. 130|root@78P01:/ # cat /sys/class/misc/mtgpio/pin
  2. PIN: [MODE] [PULL_SEL] [DIN] [DOUT] [PULL EN] [DIR] [IES]
  3. 3: 3 0 0 0 0 0 1 MI                  -->MODE这一项就是在code.dws中配的MODE
  4. 4: 3 0 0 0 0 1 1 MO
  5. 5: 3 0 1 0 0 1 1 CS
  6. 6: 3 0 0 0 0 1 1 CLK

二.利用sys控制gpio口
2.1 代码如下
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/device.h>
  4. #include <linux/ioport.h>
  5. #include <linux/errno.h>
  6. #include <linux/workqueue.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/types.h>
  9. #include <linux/io.h>
  10. #include <mach/mt_gpio.h>

  11. #define GPIO_POWER_EN GPIO123 //

  12. typedef struct {
  13.     int gpio_power;
  14. }hello_priv;

  15. static ssize_t hello_set_power(struct device *dev,struct device_attribute *attr,const char *buf, size_t count)
  16. {
  17.     hello_priv* prv = dev_get_drvdata(dev);
  18.     if ('0' == buf[0]) //shutdown
  19.     { //process: pin low-->high
  20.      mt_set_gpio_out(prv->gpio_power, GPIO_OUT_ZERO);
  21.       msleep(1500);
  22.       mt_set_gpio_out(prv->gpio_power, GPIO_OUT_ONE);
  23.       printk(KERN_NOTICE "cong: %s:%s[%d]: buf=%s", __FILE__,__FUNCTION__, __LINE__,buf);
  24.     }else if ('1' == buf[0]) //poweron
  25.     { //process: power pin high --> low
  26.      mt_set_gpio_out(prv->gpio_power, GPIO_OUT_ONE);
  27.       msleep(1500);
  28.       mt_set_gpio_out(prv->gpio_power, GPIO_OUT_ZERO);
  29.       printk(KERN_NOTICE "cong: %s:%s[%d]: buf=%s", __FILE__,__FUNCTION__, __LINE__,buf);
  30.     }
  31.     return count;
  32. }

  33. static DEVICE_ATTR(power_on, S_IRUGO | S_IWUSR,NULL, hello_set_power);

  34. static struct attribute *hello_attributes[] = {
  35.     &dev_attr_power_on.attr,
  36.     NULL
  37. };

  38. static const struct attribute_group hello_attr_group = {
  39.     .attrs = hello_attributes,
  40. };

  41. static int __init hello_probe(struct platform_device *pdev)
  42. {
  43.     int rc;
  44.     hello_priv* prv;

  45.     prv = devm_kzalloc(&pdev->dev, sizeof(hello_priv), GFP_KERNEL);
  46.     if (!prv) {
  47.         dev_err(&pdev->dev, "failed to allocate hello\n");
  48.         return -ENOMEM;
  49.     }

  50.     prv->gpio_power = GPIO_POWER_EN ;

  51.     mt_set_gpio_mode(prv->gpio_power, GPIO_MODE_GPIO);

  52.     mt_set_gpio_dir(prv->gpio_power, GPIO_DIR_OUT);
  53.     platform_set_drvdata(pdev,prv);

  54.     rc = sysfs_create_group(&pdev->dev.kobj, &hello_attr_group);
  55.     if (rc) {
  56.         dev_err(&pdev->dev,"failed to create hello sysfs group\n");
  57.         goto fail;
  58.     }

  59.     return 0;
  60. fail:
  61.     return rc;
  62. }

  63. struct platform_driver hello_driver = {
  64.     .driver = {
  65.         .name = "hello",
  66.         .owner = THIS_MODULE,
  67.     },
  68.     .probe = hello_probe,
  69. };

  70. static struct platform_device hello_device =
  71. {
  72.     .name = "hello",
  73.     .id = -1,
  74. };

  75. static int __init hello_init(void)
  76. {
  77.     int ret;

  78.     ret = platform_device_register(&hello_device);
  79.     if (ret != 0)
  80.       printk(KERN_NOTICE "cong: %s:%s[%d]: error", __FILE__,__FUNCTION__, __LINE__);

  81.     ret = platform_driver_register(&hello_driver);
  82.     return ret;
  83. }

  84. static void __init hello_exit(void)
  85. {
  86.     platform_driver_unregister(&hello_driver);
  87. }

  88. module_init(hello_init);
  89. module_exit(hello_exit);

  90. MODULE_DESCRIPTION("GPIO Controller Driver");
  91. MODULE_AUTHOR("cong");
  92. MODULE_LICENSE("GPL");
2.2 则可以在
echo 1 > /sys/devices/platform/hello/power_on
2.3 测试的应用程序如下:
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/time.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <sys/ioctl.h>
  9. #include <linux/ioctl.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>

  13. #define dbmsg(fmt, args ...) printf("%s:%s[%d]: "fmt"\n", __FILE__,__FUNCTION__, __LINE__,##args)    

  14. #define POWER_ON "power_on"

  15. int usage()
  16. {
  17.     printf("usage:\n");
  18.     printf("./test power_on <0/1> : 0-->poweron; 1-->poweroff \n");
  19.     return 0;
  20. }

  21. int power_on(char* arg)
  22. {
  23.     int fd;
  24.     if( ('0'!=arg[0]) && ('1'!=arg[0]))
  25.     {
  26.         dbmsg("bad args");
  27.         return -1;
  28.     }
  29.     fd = open("/sys/devices/platform/hello/power_on", O_RDWR);
  30.     if(fd < 0)
  31.     {
  32.         dbmsg("open error");
  33.         return -1;
  34.     }
  35.     dbmsg("arg=%s", arg);
  36.     write(fd, arg, 1);
  37.     return 0;
  38. }

  39. int main ( int argc, char *argv[] )
  40. {
  41.     int ret;
  42.     int num;
  43.     int fd_gpio;
  44.     if(argc < 2)
  45.     {
  46.         usage();
  47.         return -1;
  48.     }

  49.     if(strncmp(argv[1],POWER_ON, sizeof(POWER_ON)) == 0)
  50.     {
  51.         dbmsg("%s", POWER_ON);
  52.         if(argc != 3)
  53.         {
  54.             usage();
  55.             return -1;
  56.         }
  57.         power_on(argv[2]);
  58.     }
  59.     return 0;
  60. }
2.4 测试过程
  1. root@cong:/home/cong/driver/app# ./test power_on 0
  2. test.c:main[57]: power_on
  3. test.c:power_on[39]: arg=0
  4. root@cong:/home/cong/driver/app# ./test power_on 1
  5. test.c:main[57]: power_on
  6. test.c:power_on[39]: arg=1




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