新闻  |   论坛  |   博客  |   在线研讨会
zynq linux 下控制gpio的c代码
lolo84 | 2016-12-28 16:57:43    阅读:3545   发布文章

在linux下控制gpio可以先用简单的命令行去控制下看看,

1. devmem 0x41200000 32
    0x00000005

2.devicetree的结构如下

dip0: gpio_dip_sw@41200000 { 
               compatible = "generic-uio","uio";
               reg =<0x41200000 0x1000>;
               text_data = "GPIO_DIP_SWITCH"; //for test
               bin_data=; //for test
};
需要注意的点是 reg=后面的一个参数必须要大于等于0x1000 ;
3. c语言源码
fd_led=open(filename,O_RDONLY);
      if(fd_led<0)
        return 1;

	base_address = mmap(NULL, 0x10, PROT_READ | PROT_WRITE, MAP_SHARED, fd_led, 0);
    if(base_address==MAP_FAILED)
      {perror("mmap");
      }

总结一下:
下面是完整的API函数
#include 
#include 
#include 
 
 
// The specific GPIO being used must be setup and replaced thru
// this code.  The GPIO of 240 is in the path of most the sys dirs
// and in the export write.
//
// Figuring out the exact GPIO was not totally obvious when there
// were multiple GPIOs in the system. One way to do is to go into
// the gpiochips in /sys/class/gpio and view the label as it should
// reflect the address of the GPIO in the system. The name of the
// the chip appears to be the 1st GPIO of the controller.
//
// The export causes the gpio240 dir to appear in /sys/class/gpio.
// Then the direction and value can be changed by writing to them.
 
// The performance of this is pretty good, using a nfs mount,
// running on open source linux, on the ML507 reference system,
// the GPIO can be toggled about every 4 usec.
 
// The following commands from the console setup the GPIO to be
// exported, set the direction of it to an output and write a 1
// to the GPIO.
//
// bash> echo 240 > /sys/class/gpio/export
// bash> echo out > /sys/class/gpio/gpio240/direction
// bash> echo 1 > /sys/class/gpio/gpio240/value
 
// if sysfs is not mounted on your system, the you need to mount it
// bash> mount -t sysfs sysfs /sys
 
// the following bash script to toggle the gpio is also handy for
// testing
//
// while [ 1 ]; do
//  echo 1 > /sys/class/gpio/gpio240/value
//  echo 0 > /sys/class/gpio/gpio240/value
// done
 
// to compile this, use the following command
// gcc gpio.c -o gpio
 
// The kernel needs the following configuration to make this work.
//
// CONFIG_GPIO_SYSFS=y
// CONFIG_SYSFS=y
// CONFIG_EXPERIMENTAL=y
// CONFIG_GPIO_XILINX=y
 
int main()
{
    int valuefd, exportfd, directionfd;
 
    printf("GPIO test running...\n");
 
    // The GPIO has to be exported to be able to see it
    // in sysfs
 
    exportfd = open("/sys/class/gpio/export", O_WRONLY);
    if (exportfd < 0)
    {
        printf("Cannot open GPIO to export it\n");
        exit(1);
    }
 
    write(exportfd, "240", 4);
    close(exportfd);
 
    printf("GPIO exported successfully\n");
 
    // Update the direction of the GPIO to be an output
 
    directionfd = open("/sys/class/gpio/gpio240/direction", O_RDWR);
    if (directionfd < 0)
    {
        printf("Cannot open GPIO direction it\n");
        exit(1);
    }
 
    write(directionfd, "out", 4);
    close(directionfd);
 
    printf("GPIO direction set as output successfully\n");
 
    // Get the GPIO value ready to be toggled
 
    valuefd = open("/sys/class/gpio/gpio240/value", O_RDWR);
    if (valuefd < 0)
    {
        printf("Cannot open GPIO value\n");
        exit(1);
    }
 
    printf("GPIO value opened, now toggling...\n");
 
    // toggle the GPIO as fast a possible forever, a control c is needed
    // to stop it
 
    while (1)
    {
        write(valuefd,"1", 2);
        write(valuefd,"0", 2);
    }
}
 


*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
推荐文章
最近访客