CubieBoard中文论坛

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

Cubieboard 4 GPIO控制举例

[复制链接]
发表于 2016-5-13 15:34:49 | 显示全部楼层 |阅读模式
#/bin/bash

mount -t debugfs debugfs /mnt
cd /mnt/sunxi_pinctrl

#set the PH9 pin as output port .If type "echo PH9 0 > function" ,set the PH9 pin as input port
echo PH9 1 > function   

#set the PH9 pin as low level
echo PH9 0 > data
sleep 1

#set the PH9 pin as high level
echo PH9 1 > data

echo PH8 1 > function
echo PH8 1 > data

回复

使用道具 举报

发表于 2018-3-6 10:03:11 | 显示全部楼层
补充其他gpio控制方法:
以控制 cc-a80 的uart4_tx为例子:
新建一个test.c
  1. #include <ctype.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <time.h>
  7. #include <signal.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <sys/mman.h>
  12. #include <sys/select.h>
  13. #include <pthread.h>
  14. #include <unistd.h>
  15. #include <sched.h>
  16. #include <errno.h>


  17. #define SW_PORTC_IO_BASE  0x06000800

  18. int main() {
  19.         unsigned int * pc;
  20.         int fd, i;
  21.         char * ptr;
  22.         unsigned int addr_start, addr_offset, PageSize, PageMask, data;
  23.        
  24.         PageSize = sysconf(_SC_PAGESIZE);
  25.         PageMask = (~(PageSize-1));
  26.         addr_start = SW_PORTC_IO_BASE & PageMask;
  27.         addr_offset = SW_PORTC_IO_BASE & ~PageMask;
  28.        
  29.         fd = open("/dev/mem", O_RDWR);
  30.         if(fd < 0) {
  31.            perror("Unable to open /dev/mem");
  32.            return(-1);
  33.         }
  34.        
  35.         pc = mmap(0, PageSize*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, addr_start);
  36.        
  37.         if(pc == MAP_FAILED) {
  38.            perror("Unable to mmap file");
  39.            printf("pc:%lx\n", (unsigned long)pc);
  40.            return(-1);
  41.         }
  42.         printf("PageSize:%8.8x\tPageMask:%8.8x\naddr_start:%8.8x\taddr_offset:%8.8x\n",PageSize,PageMask,addr_start,addr_offset);
  43.         printf("pc:%8.8x\n", *(unsigned int *)pc);
  44.         ptr = (char *)pc + addr_offset;

  45.         //the PG12 is UART4-TX in the board ,set it as output port
  46.         data = *(unsigned int *)(ptr+0xdc);
  47.         data &= ~(7<<16);                        
  48.         data |= 1<<16;                        
  49.         *(unsigned int *)(ptr+0xdc) = data;

  50.         //set it high level  
  51.         data = *(unsigned int *)(ptr+0xe8);
  52.         data |= 1<<12;                        
  53.         *(unsigned int *)(ptr+0xe8) = data;
  54.         //set it low level
  55.         //data = *(unsigned int *)(ptr+0xe8);
  56.         //data &= ~(1<<12);                        
  57.         //*(unsigned int *)(ptr+0xe8) = data;

  58.        
  59.        
  60.         return 0;
  61.     }
复制代码
最后在板子上gcc test.c -o test。只看下注释理解用法。




回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 04:34 , Processed in 0.022568 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部