设为首页收藏本站
查看: 10187|回复: 0

SAMA5D3X系列GPIO测试程序

[复制链接]

25

主题

9

回帖

280

积分

中级会员

积分
280
Willian.Mo 发表于 2015-5-13 15:36:45 | 显示全部楼层 |阅读模式
本帖最后由 Willian.Mo 于 2015-6-30 14:52 编辑

GPIO测试程序如下:
[mw_shl_code=c,true]#include<stdio.h>
#include <stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
#include <poll.h>

#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define MAX_BUF 60
#define OUT 1
#define IN 0

/**
* brief: export the GPIO to user space
* @Param: gpio: the GPIO number
*/
int gpio_export(unsigned int gpio)
{
        int fd ,len;
        char buf[MAX_BUF];

        fd = open(SYSFS_GPIO_DIR "/export" ,O_WRONLY);
        if (fd < 0) {
                perror("gpio/export");
                return fd;
        }
        len = snprintf(buf ,sizeof(buf) ,"%d" ,gpio);
        write(fd ,buf ,len);
        close(fd);
        return 0;
}

/**
* brief: unexport the GPIO to user space
* @Param: gpio: the GPIO number
*/  
int gpio_unexport(unsigned int gpio)
{
        int fd ,len;
        char buf[MAX_BUF];

        fd = open(SYSFS_GPIO_DIR "/unexport" ,O_WRONLY);
        if (fd < 0) {
                perror("gpio/unexport");
                return fd;
        }
        len = snprintf(buf ,sizeof(buf) ,"%d" ,gpio);
        write(fd ,buf ,len);
        close(fd);
        return 0;
}

/**
* brief: configure GPIO for input or output
* @Param: gpio: the GPIO number
* @Param: out_flag: the flag of output or input.It's value can be 1 or 0.  
*/
int gpio_set_dir(unsigned int gpio ,int out_flag)
{
        int fd ,len;
        char buf[MAX_BUF];

        len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
        fd = open(buf ,O_WRONLY);
        if (fd < 0) {
                perror(buf);
                return fd;
        }
        if (out_flag)
                write(fd ,"out" ,4);
        else
                write(fd ,"in" ,3);
        close(fd);
        return 0;
}

/**
* brief: Set the value of GPIO
* @Param: gpio: the GPIO number
* @Param: value: the value of GPIO. Supports values of 0 and 1.
*/  
int gpio_set_value(unsigned int gpio, unsigned int value)  
{  
    int fd, len;  
    char buf[MAX_BUF];  
   
    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);      
    fd = open(buf, O_WRONLY);
        if (fd < 0) {  
        perror("gpio/set-value");  
        return fd;  
    }  
   
    if (value)  
        write(fd, "1", 2);  
    else  
        write(fd, "0", 2);  
   
    close(fd);  
    return 0;  
}

/**
* brief: get the value of GPIO
* @Param: gpio: the GPIO number
* @Param: value: pointer to the value of GPIO
*/
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
        int fd, len;
        char ch;
        char buf[MAX_BUF];

        len = snprintf(buf ,sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value" ,gpio);
        fd = open(buf ,O_RDONLY);
        if (fd < 0) {
                perror("gpio_get_value");
                return fd;
        }
        read(fd ,&ch ,1);
        if (ch == '1')
                *value = 1;
        else if(ch == '0')
                        *value = 0;
        close(fd);
        return 0;
}

/**
* @brief: main function
* @Param: argc: number of parameters
* @Param: argv: parameters list
*/  
int main(int argc, char **argv)  
{  
    if (argc < 2) {  
        printf("Usage: <./gpio gpio-int value>\n");   
        exit(-1);  
    }

    unsigned int gpio = atoi(argv[1]);
    int value = atoi(argv[2]);        
    if (value < 0 || value > 1)
    {
         printf("Value: <0 or 1>\n");   
         exit(-1);  
    }
   
    /* gpio configure */
    gpio_export(gpio);  
    gpio_set_dir(gpio, OUT);
   
    gpio_set_value(gpio, value);
   
    return 0;  
} [/mw_shl_code]
编写Makefile如下:[mw_shl_code=bash,true]CC = arm-linux-gnueabihf-gcc
TARGET = gpio
SRC =  $(TARGET).c
OBJS = $(patsubst %.c ,%.o ,$(SRC))
.PHONY: all
all: $(TARGET)
$(TARGET) : $(OBJS)
        $(CC) -o $@ $^
%.o : %.c
        $(CC) -c $< -o $@
clean:
        $(RM) *.o $(TARGET)[/mw_shl_code]
运行命令(把gpio153管脚拉低):
[mw_shl_code=bash,true]$ ./gpio 153 0[/mw_shl_code]
回复

使用道具 举报

您需要登录后才可以回帖 登录

本版积分规则

Archiver|手机版|小黑屋|米尔科技论坛   

GMT+8, 2024-4-26 18:51 , Processed in 0.039811 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表