Problem with GPIO as input - always "0"

Hi, i’m really stupid - but dosen’t work :)

im try export gpio 160 as input

sudo echo 160 > /sys/class/gpio/export
sudo echo in > /sys/class/gpio/gpio160/direction
cat /sys/class/gpio/gpio160/value → result 0

how to read “1” on gpio 160? connect to GND or 1.8V? pulled by resisotr to something? I’m trying almost all secure variants. echp gpio160/value still returns 0

example with simple button will be great

as output work well…

I ran into the same thing…pinmux register seems to be the issue.

When I dug into the TK1 kernel, I found that the pinmux controls had been elided from the sysfs GPIO driver.
As a result…I could config the GP{IO to be an input, but the actual physical pin signal was not being seen.

I’ve also found that when I attempt gpio_request() for those PU[00-06] GPIO, many of them seem to already be in use (returns errno -16 “EBUSY” == Device or Resource is already in use)

I’m still looking for the comprehensive solution. There’s a bit of a change needed in the GPIO driver (to include pinmux actions supporting the gpio config requests) … possibly also something in the pinmux driver.

Sorry…no firm answer…just commiseration :-/ I’ll update when I get some good solution.

Hey guys, I am working through this now and have not found a solution. Did anyone get it working for inputs?

It look’s like there is a DTB file that needs some tweaking. I can see that PU0, PU3, and PU4 are default to outputs via pinctrl file, it appears when exporting to sysfs they retain the direction. The other GPIOs are defaulted as inputs and can be changed to outputs and controlled, very strange. Will report back if I fix it.

I can confirm this, too.
GPIO160 always returns 0
GPIO57 always returns 1

I just have a quick trial on R21.2 and gpio160 return 1.

I refer to Jetson/GPIO - eLinux.org
Connect 1.8V from Pin 19 of J3A1 to GPIO_PU0 on pin 40 of J3A2.

sudo su
echo 160 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio160/direction
cat /sys/class/gpio/gpio160/value -> result 1

Weird. Using theImp’s steps, I also get 0 from GPIO 160, although I don’t have anything connected to it.

I got the same result on 21.2 as thelmp’s step as below.

  1. Connect 1.8V from Pin 19 of J3A1 to GPIO_PU0 on pin 40 of J3A2.

  2. set gpio160 as input:
    ubuntu@tegra-ubuntu:~$ sudo su
    [sudo] password for ubuntu:
    root@tegra-ubuntu:/home/ubuntu# echo 160 > /sys/class/gpio/export
    root@tegra-ubuntu:/home/ubuntu# echo in > /sys/class/gpio/gpio160/direction

  3. get the same result “1” as thelmp.
    root@tegra-ubuntu:/home/ubuntu# cat /sys/class/gpio/gpio160/value
    1

  4. remove the connection between “1.8V from Pin 19 of J3A1” and “GPIO_PU0 on pin 40 of J3A2”, get the result “0”
    root@tegra-ubuntu:/home/ubuntu# cat /sys/class/gpio/gpio160/value
    0

  5. connect again and get the result “1”
    root@tegra-ubuntu:/home/ubuntu# cat /sys/class/gpio/gpio160/value
    1

I am interested in the solution to the GPIO input problem as well. Although I am no kernel hacker, I do offer to test anything if necessary. Many thanks.

Is there a problem? I think all the latest posts show that it works as expect (i.e. “1” for 1.8V and “0” for 0V)?

My fault - I was on 21.1 and the value of GPIO 160 would not change as described here. Upgraded to 21.3 and it works perfectly.

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;
}

hello MVV,

sorry, may i know what’s your mean about below.

I am using IMX6 board

are you able to set gpio and reading correct value?
thanks