CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 23815|回复: 14

Python GPIO库:用C直接操作GPIO

[复制链接]
发表于 2013-5-20 12:11:19 | 显示全部楼层 |阅读模式
本帖最后由 soloforce 于 2013-5-20 12:13 编辑

在hipboi的这个帖子http://forum.cubietech.com/forum.php?mod=viewthread&tid=456,我们可以用Python GPIO里面的GPIO C库文件直接编写应用程序了。下面是读和写的两个例子。

读GPIO:本例读取PD8的值,并打印提示信息
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <signal.h>

  5. #include "gpio_lib.h"
  6. #define PD0    SUNXI_GPD(0)
  7. #define PD1    SUNXI_GPD(1)
  8. #define PD2    SUNXI_GPD(2)
  9. #define PD3    SUNXI_GPD(3)
  10. #define PD4    SUNXI_GPD(4)
  11. #define PD5    SUNXI_GPD(5)
  12. #define PD6    SUNXI_GPD(6)
  13. #define PD7    SUNXI_GPD(7)
  14. #define PD8    SUNXI_GPD(8)
  15. #define PD9    SUNXI_GPD(9)
  16. #define PD10    SUNXI_GPD(10)
  17. #define PD11    SUNXI_GPD(11)
  18. #define PD12    SUNXI_GPD(12)
  19. #define PD13    SUNXI_GPD(13)
  20. #define PD14    SUNXI_GPD(14)
  21. #define PD15    SUNXI_GPD(15)
  22. #define PD16    SUNXI_GPD(16)
  23. #define PD17    SUNXI_GPD(17)
  24. #define PD18    SUNXI_GPD(18)
  25. #define PD19    SUNXI_GPD(19)
  26. #define PD20    SUNXI_GPD(20)
  27. #define PD21    SUNXI_GPD(21)
  28. #define PD22    SUNXI_GPD(22)
  29. #define PD23    SUNXI_GPD(23)
  30. #define PD24    SUNXI_GPD(24)
  31. #define PD25    SUNXI_GPD(25)
  32. #define PD26    SUNXI_GPD(26)
  33. #define PD27    SUNXI_GPD(27)

  34. #define MISO    SUNXI_GPE(3)
  35. #define MOSI    SUNXI_GPE(2)
  36. #define SCK     SUNXI_GPE(1)
  37. #define CS      SUNXI_GPE(0)

  38. struct sigaction new_act,new_act2, old_act;

  39. /**
  40. * @brief Signal INT handler
  41. * */
  42. void sigint_handler(int sig_num)
  43. {
  44.     printf("Uninitialzing GPIO...\n");

  45.     /* uninitialize the GPIO */
  46.     sunxi_gpio_cleanup();

  47.     /* change back to the original handler */
  48.     sigaction(SIGINT, &old_act,NULL);

  49.     /* emit the SIGINT again */
  50.     kill(0,SIGINT);
  51. }

  52. /**
  53. * @brief Signal TERM handler
  54. * */
  55. void sigterm_handler(int sig_num)
  56. {
  57.     printf("Uninitialzing GPIO...\n");

  58.     /* uninitialize the GPIO */
  59.     sunxi_gpio_cleanup();

  60.     /* change back to the original handler */
  61.     sigaction(SIGTERM, &old_act,NULL);

  62.     /* emit the SIGTERM again */
  63.     kill(0,SIGTERM);
  64. }

  65. int main()
  66. {

  67.     memset(&new_act,0,sizeof(new_act));
  68.     memset(&new_act2,0,sizeof(new_act));
  69.     memset(&old_act,0,sizeof(old_act));
  70.     new_act.sa_handler=&sigint_handler;
  71.     new_act2.sa_handler=&sigterm_handler;
  72.     sigaction(SIGINT, &new_act, &old_act);
  73.     sigaction(SIGTERM, &new_act2, &old_act);


  74.     if(SETUP_OK!=sunxi_gpio_init()){
  75.         printf("Failed to initialize GPIO\n");
  76.         return -1;
  77.     }

  78.     if(SETUP_OK!=sunxi_gpio_set_cfgpin(PD8,INPUT)){
  79.         printf("Failed to config GPIO pin\n");
  80.         return -1;
  81.     }

  82.     while(1){
  83.         if(HIGH==sunxi_gpio_input(PD8)){
  84.             printf("HIGH\n");
  85.         }
  86.         usleep(1000);
  87.     }

  88.     sunxi_gpio_cleanup();

  89.    
  90.     return 0;
  91.    
  92. }
复制代码


本例写PD16脚,如果上面接了一个led,且负极接地,则会周期闪烁。
  1. #include <stdlib.h>
  2. #include <stdio.h>

  3. #include "gpio_lib.h"
  4. #define PD0    SUNXI_GPD(0)
  5. #define PD1    SUNXI_GPD(1)
  6. #define PD2    SUNXI_GPD(2)
  7. #define PD3    SUNXI_GPD(3)
  8. #define PD4    SUNXI_GPD(4)
  9. #define PD5    SUNXI_GPD(5)
  10. #define PD6    SUNXI_GPD(6)
  11. #define PD7    SUNXI_GPD(7)
  12. #define PD8    SUNXI_GPD(8)
  13. #define PD9    SUNXI_GPD(9)
  14. #define PD10    SUNXI_GPD(10)
  15. #define PD11    SUNXI_GPD(11)
  16. #define PD12    SUNXI_GPD(12)
  17. #define PD13    SUNXI_GPD(13)
  18. #define PD14    SUNXI_GPD(14)
  19. #define PD15    SUNXI_GPD(15)
  20. #define PD16    SUNXI_GPD(16)
  21. #define PD17    SUNXI_GPD(17)
  22. #define PD18    SUNXI_GPD(18)
  23. #define PD19    SUNXI_GPD(19)
  24. #define PD20    SUNXI_GPD(20)
  25. #define PD21    SUNXI_GPD(21)
  26. #define PD22    SUNXI_GPD(22)
  27. #define PD23    SUNXI_GPD(23)
  28. #define PD24    SUNXI_GPD(24)
  29. #define PD25    SUNXI_GPD(25)
  30. #define PD26    SUNXI_GPD(26)
  31. #define PD27    SUNXI_GPD(27)

  32. #define MISO    SUNXI_GPE(3)
  33. #define MOSI    SUNXI_GPE(2)
  34. #define SCK     SUNXI_GPE(1)
  35. #define CS      SUNXI_GPE(0)

  36. int main()
  37. {
  38.     if(SETUP_OK!=sunxi_gpio_init()){
  39.         printf("Failed to initialize GPIO\n");
  40.         return -1;
  41.     }

  42.     if(SETUP_OK!=sunxi_gpio_set_cfgpin(PD16,OUTPUT)){
  43.         printf("Failed to config GPIO pin\n");
  44.         return -1;
  45.     }

  46.     int i;
  47.     for(i=0;i<5;i++){
  48.         if(sunxi_gpio_output(PD16,HIGH)){
  49.             printf("Failed to set GPIO pin value\n");
  50.             return -1;
  51.         }

  52.         usleep(500000);
  53.         if(sunxi_gpio_output(PD16,LOW)){
  54.             printf("Failed to set GPIO pin value\n");
  55.             return -1;
  56.         }
  57.         usleep(500000);
  58.     }

  59.     sunxi_gpio_cleanup();

  60.     return 0;
  61.    
  62. }

复制代码
回复

使用道具 举报

发表于 2013-6-6 20:32:06 | 显示全部楼层
gpio_lib.h  找不到
回复 支持 反对

使用道具 举报

发表于 2013-7-12 23:25:19 | 显示全部楼层
SUNROC1 发表于 2013-6-6 20:32
gpio_lib.h  找不到

在pySUNXI包里
回复 支持 反对

使用道具 举报

发表于 2013-7-30 21:34:00 | 显示全部楼层
本帖最后由 yzbx 于 2013-7-31 08:55 编辑

楼主你好,在PC上的ubuntu系统下编译,出现了以下问题:请问是不是少安装了什么?
  1. yzbx@:pySUNXI-0.1.12$ arm-linux-gnueabihf-gcc readPD8.c -static -o readPD8
  2. /tmp/cc603gTU.o: In function `sigint_handler':
  3. readPD8.c:(.text+0x14): undefined reference to `sunxi_gpio_cleanup'
  4. /tmp/cc603gTU.o: In function `sigterm_handler':
  5. readPD8.c:(.text+0x58): undefined reference to `sunxi_gpio_cleanup'
  6. /tmp/cc603gTU.o: In function `main':
  7. readPD8.c:(.text+0x132): undefined reference to `sunxi_gpio_init'
  8. readPD8.c:(.text+0x156): undefined reference to `sunxi_gpio_set_cfgpin'
  9. readPD8.c:(.text+0x176): undefined reference to `sunxi_gpio_input'
  10. collect2: ld 返回 1
  11. yzbx@:pySUNXI-0.1.12$
复制代码
回复 支持 反对

使用道具 举报

发表于 2013-8-5 23:38:40 | 显示全部楼层
yzbx 发表于 2013-7-30 21:34
楼主你好,在PC上的ubuntu系统下编译,出现了以下问题:请问是不是少安装了什么? ...

正好,我也这样,解决方法是把include的h改成c,但是有时候要反过来,不知道为啥
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-8-6 09:54:51 | 显示全部楼层
$ gcc gpio_lib.c -c
$ gcc gpio_test.c -c
$ gcc gpio_test.o gpio_lib.o -o gpio_test
回复 支持 反对

使用道具 举报

发表于 2014-2-15 11:01:59 | 显示全部楼层
我也是做按键输入,但io口一直都是高电平啊。。。
回复 支持 反对

使用道具 举报

发表于 2014-8-20 10:29:32 | 显示全部楼层
这个是不是要交叉编译在放到板子上测试啊  我这交叉编译放过去 执行不了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-8-20 15:27:20 | 显示全部楼层
管叫我小狄 发表于 2014-8-20 10:29
这个是不是要交叉编译在放到板子上测试啊  我这交叉编译放过去 执行不了
...

在板子系统上安装个gcc,直接在板子上编译运行就可以了。
回复 支持 反对

使用道具 举报

发表于 2014-9-30 13:13:34 | 显示全部楼层
如果是CT的話,是只要define CT的腳位就可以了嗎  還是script.fex 的腳位也要修改
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|粤ICP备13051116号|cubie.cc---深刻的嵌入式技术讨论社区

GMT+8, 2024-4-26 20:07 , Processed in 0.038521 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部