蓝天-彭 发表于 2016-5-13 15:34:49

Cubieboard 4 GPIO控制举例

#/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

@allen 发表于 2018-3-6 10:03:11

补充其他gpio控制方法:
以控制 cc-a80 的uart4_tx为例子:
新建一个test.c#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <pthread.h>
#include <unistd.h>
#include <sched.h>
#include <errno.h>


#define SW_PORTC_IO_BASE0x06000800

int main() {
        unsigned int * pc;
        int fd, i;
        char * ptr;
        unsigned int addr_start, addr_offset, PageSize, PageMask, data;
       
        PageSize = sysconf(_SC_PAGESIZE);
        PageMask = (~(PageSize-1));
        addr_start = SW_PORTC_IO_BASE & PageMask;
        addr_offset = SW_PORTC_IO_BASE & ~PageMask;
       
        fd = open("/dev/mem", O_RDWR);
        if(fd < 0) {
           perror("Unable to open /dev/mem");
           return(-1);
        }
       
        pc = mmap(0, PageSize*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, addr_start);
       
        if(pc == MAP_FAILED) {
           perror("Unable to mmap file");
           printf("pc:%lx\n", (unsigned long)pc);
           return(-1);
        }
        printf("PageSize:%8.8x\tPageMask:%8.8x\naddr_start:%8.8x\taddr_offset:%8.8x\n",PageSize,PageMask,addr_start,addr_offset);
        printf("pc:%8.8x\n", *(unsigned int *)pc);
        ptr = (char *)pc + addr_offset;

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

        //set it high level
        data = *(unsigned int *)(ptr+0xe8);
        data |= 1<<12;                        
        *(unsigned int *)(ptr+0xe8) = data;
        //set it low level
        //data = *(unsigned int *)(ptr+0xe8);
        //data &= ~(1<<12);                        
        //*(unsigned int *)(ptr+0xe8) = data;

       
       
        return 0;
    }最后在板子上gcc test.c -o test。只看下注释理解用法。




页: [1]
查看完整版本: Cubieboard 4 GPIO控制举例