Hello
I am using IMX6 board. I am trying to configure gpio4 as input by application. its reading 0.
I am new to this board. please help me.
//#include <gpio.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
int gpio_export(unsigned int );
int gpio_unexport(unsigned int);
int gpio_set_dir(unsigned int , unsigned int );
//int gpio_set_value(unsigned int , unsigned int );
int gpio_get_value(unsigned int , unsigned int );
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define MAX_BUF 64
#define PORT_NUM1 4
unsigned int switch_state;
//unsigned char val;
int main ()
{
printf("\nIn main");
gpio_export(PORT_NUM1); // export that gpio 2 to user space
printf("\nGPIO exported successfully");
gpio_set_dir(PORT_NUM1,0); // set direction of gpio 2 as 0=input
printf("\nGPIO direction set as input successfully");
//gpio_set_value(PORT_NUM1,1);
//printf("\nGPIO value set successfully");
while(1)
{
switch_state = gpio_get_value(PORT_NUM1,val);
printf("\nswitch state is %d", switch_state);
sleep(1);
}
}
/*****************Export function***************************/
int gpio_export(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
//opening EXPORT file as Write Only
fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
if (fd < 0) // error in opening file
{
perror("gpio/export");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
/************ gpio_unexport *******************************/
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;
}
/************** gpio_set_direction ****************************/
int gpio_set_dir(unsigned int gpio, unsigned int dir)
{
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("gpio/direction");
return fd;
}
if (dir) // 1=output and 0=input
write(fd, "in", 4);
else
write(fd, "out", 3);
close(fd);
return 0;
}
/*************** gpio_set_value ******************************/
/*int gpio_set_value(unsigned int gpio, unsigned int val)
{
int fd;
char buf[MAX_BUF];
int len;
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 (val)
write(fd, "1", 2);
else
write(fd, "0", 2);
close(fd);
return 0;
}
*/
/*************** gpio_get_value ******************************/
int gpio_get_value(unsigned int gpio int val)
{
int fd;
char ch;
char buf[MAX_BUF];
unsigned int val
int len;
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 != '0')
{
val =1;
}
else
{
val =0;
}
close(fd);
return val;
}