How to get GPIO value from u-boot for futher processing

Hi,

I am trying to do something very simple from the u-boot command line. I want to read a value of a gpio and set a variable accordingly. Like:

setenv X gpio input 2;

if X = 1 ; then bootcmd =…
else if X = 0; then bootcmd =…
fi;

Hi @mcoch

You can do the following in uboot:

setexpr X 'input gpio2'
if test "${X}" = '0' ; then setenv bootcmd 'command 0' ; else setenv bootcmd 'command 1' fi

Best regards, Jaski

Hi @jaski.tx,

the code above always leads to ‘command 0’ on my system.
However, I have had success with the following:

if gpio input 2;  then setenv bootcmd 'command 0' ; else setenv bootcmd 'command 1'; fi

Regards,
Matthias

Thanks for the Input.

I observed that it is reversed, different than any usual if condition. If gpio input 2 returns 1 the else gets executed and if gpio input 2 returns 0, it is then

1 Like

Ok, I did not get what exactly is the issue?

Please keep in mind that the return code 1 of gpio input can mean both “gpio is high” and “read failed”. U-Boot 2022.07 will include a gpio read command that will allow to reliably use gpio values in scripts: