Page tree
Skip to end of metadata
Go to start of metadata

在dts中配置好GPIO后,就可以在用户层来使用。Linux的GPIO驱动是在sysfs中来使用,目录是"/sys/class/gpio"。以下是UART3_TX_DATA配置为GPIO1_IO24的例子

# echo 24 > /sys/class/gpio/export
# echo out > /sys/class/gpio/gpio24/direction
# echo 1 > /sys/class/gpio/gpio24/value
# cat /sys/class/gpio/gpio24/value


C语言操作可以直接使用系统函数来操作GPIO,如open(), read(),write(),close()。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
 
#define SYSFS_GPIO_EXPORT           "/sys/class/gpio/export"  
#define SYSFS_GPIO_PIN_VAL      "24"
#define SYSFS_GPIO_DIR          "/sys/class/gpio/gpio24/direction"
#define SYSFS_GPIO_DIR_OUT      "OUT"
#define SYSFS_GPIO_DIR_IN      "IN"
#define SYSFS_GPIO_VAL          "/sys/class/gpio/gpio24/value"
#define SYSFS_GPIO_VAL_H        "1"
#define SYSFS_GPIO_VAL_L        "0"

int main() 
{ 
		int fd; 

         fd = open(SYSFS_GPIO_EXPORT, O_WRONLY);
         if(fd == -1)
         {
                   printf("ERR: Radio hard reset pin open error.\n");
                   return EXIT_FAILURE;
         }
         write(fd, SYSFS_GPIO_PIN_VAL ,sizeof(SYSFS_GPIO_PIN_VAL)); 
         close(fd); 

         //设置端口方向/sys/class/gpio/gpio24  echo out > direction
         fd = open(SYSFS_GPIO_DIR, O_WRONLY);
         if(fd == -1)
         {
                   printf("ERR: Radio hard reset pin direction open error.\n");
                   return EXIT_FAILURE;
         }
         write(fd, SYSFS_GPIO_DIR_OUT, sizeof(SYSFS_GPIO_DIR_OUT)); 
         close(fd); 

         //输出复位信号: 拉高>100ns
         fd = open(SYSFS_GPIO_VAL, O_RDWR);
         if(fd == -1)
         {
                   printf("ERR: Radio hard reset pin value open error.\n");
                   return EXIT_FAILURE;
         }       
         while(1)
         {
                   write(fd, SYSFS_GPIO_VAL_H, sizeof(SYSFS_GPIO_VAL_H));
                   usleep(1000000);
                   write(fd, SYSFS_GPIO_VAL_L, sizeof(SYSFS_GPIO_VAL_L));
                   usleep(1000000);
         }
         close(fd);

         printf("INFO: Radio hard reset pin value open error.\n");
         return 0;

}  


  • No labels
Write a comment…