Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...


gpio.c
/************************************************************/
// gpio.c
/************************************************************/
#include "gpio.h"
#define SYS_GPIO_PFX "/sys/class/gpio/"
int fd = -1;

//gpio init
uint8 gpio_init(int _pin)
{
    DIR *dp;
    char fpath[100];
    int port,num;


    port = _pin/8;
    num = _pin%8;
    printf("port=%d, num=%d \n",port,num);
    sprintf(fpath, SYS_GPIO_PFX"P%d_0%d", port, num);
    if(!(dp=opendir(fpath)))
    {
        FILE *fp;
        if((fp = fopen(SYS_GPIO_PFX"export", "w")) == NULL) return(-1);
        fprintf(fp, "%d", _pin);
        fclose(fp);
    }
    else
    {
        closedir(dp);
    }
    return(0);
}

uint8 gpio_read(int _pin)
{
    char ret, ss[2];
    char fpath[100];
    int port,num;


    port = _pin/8;
    num = _pin%8;
    sprintf(fpath, SYS_GPIO_PFX"P%d_0%d/value", port, num);
    printf("fpath=%s \n", fpath);
    fd = open(fpath, O_RDWR); // open file and enable read and write
    if(fd < 0)
    {
        perror("Can't open gpio file \n"); // open i2c dev file fail
        exit(1);
    }
    printf("open gpio file success !\n"); // open i2c dev file succes
    ret = read(fd, ss, 1);
    close(fd);
    if(ret < 0) return(0);
        return((ss[0] == '0'?0:1));
}


uint8 gpio_write(int _pin, int _val)
{
    int ret;
    char x[2];
    char fpath[100];
    int port,num;


    port = _pin/8;
    num = _pin%8;
    sprintf(fpath, SYS_GPIO_PFX"P%d_0%d/value", port, num);
    printf("fpath=%s \n", fpath);
    fd = open(fpath, O_RDWR); // open file and enable read and write
    if(fd < 0)
    {
        perror("Can't open gpio file \n"); // open i2c dev file fail
        exit(1);
    }
    printf("open gpio file success !\n"); // open i2c dev file succes
    x[1] = '\0';
    x[0] = (_val>0 ? '1':'0');
    ret = write(fd, x, 1);
    close(fd);
    return(ret);
}


uint8 gpio_dir_get(int _pin)
{
    static char ss[2];
    char fpath[100];
    int ret,port,num;


    port = _pin/8;
    num = _pin%8;
    sprintf(fpath, SYS_GPIO_PFX"P%d_0%d/direction", port, num);
    printf("fpath=%s \n", fpath);
    fd = open(fpath, O_RDWR); // open file and enable read and write
    if(fd < 0)
    {
        perror("Can't open gpio file \n"); // open i2c dev file fail
        exit(1);
    }
    printf("open gpio file success !\n"); // open i2c dev file succes
    ret = read(fd, ss, 2);
    close(fd);
    if(ret < 0) return(0);
        return((ss[0] == 'i'? PIN_DIR_I:PIN_DIR_O));
}


uint8 gpio_dir_set(int _pin, int _dir)
{
    int ret=0;
    const char *ss="in";
    char fpath[100];
    int port,num;


    port = _pin/8;
    num = _pin%8;
    sprintf(fpath, SYS_GPIO_PFX"P%d_0%d/direction", port, num);
    printf("fpath=%s \n", fpath);
    fd = open(fpath, O_RDWR); // open file and enable read and write
    if(fd < 0)
    {
        perror("Can't open gpio file \n"); // open i2c dev file fail
        exit(1);
    }
    printf("open gpio file success !\n"); // open i2c dev file succes
    if(_dir == PIN_DIR_O) ss="out";
    ret = write(fd, ss, strlen(ss)*sizeof(char));
    close(fd);
    return((ret<2 ? -1:0));
}


gpio.h
/************************************************************/
// gpio.h
/************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/ioctl.h>
#include<fcntl.h>
#include<dirent.h>
#include<string.h>


#define PIN_DIR_I 0
#define PIN_DIR_O 1
typedef unsigned char uint8;
uint8 gpio_init(int);
uint8 gpio_read(int);
uint8 gpio_write(int,int);
uint8 gpio_dir_get(int);
uint8 gpio_dir_set(int,int);


main.c
/************************************************************/
// main.c
/************************************************************/
#include "gpio.h"
// main
int main(int argc, char *argv[])
{
    int pinnum1,pinnum2;


    printf("GPIO test main start \n");
    pinnum1 = 22; //mean GPIO_P2_6=MX[22]
    pinnum2 = 24; //mean GPIO_P3_0=MX[24]
    gpio_init(pinnum1);
    gpio_init(pinnum2);
    gpio_dir_set(pinnum1,PIN_DIR_O);
    gpio_dir_set(pinnum2,PIN_DIR_O);
    gpio_write(pinnum1,1);
    gpio_write(pinnum2,0);
    usleep(3000*1000);
    while(1)
    {
        gpio_write(pinnum1,1);
        gpio_write(pinnum2,0);
        usleep(1000*1000);
        gpio_write(pinnum1,0);
        gpio_write(pinnum2,1);
        usleep(1000*1000);
    }
}


3.Makefile


/******************************/

...