Search code examples
linuxgpioopenwrtu-boot

How to read a GPIO value from UBoot built by OpenWRT


I'm trying to implement a recovery mode on a board powered by OpenWRT. I have a button connected to some GPIO: GPIO_PL4.

I cast the gpio name to a number representation using the next expression:

(position of letter in alphabet - 1) * 32 + pin number

So I got PL4 -- 356 mapping.

I tried to add the next lines to an uboot environment file uEnv-default.txt:

  if gpio input 356; then
     echo "The button is pressed";
  fi;

But got an error message during booting:

  gpio: requesting pin 356 failed
  gpio - query and control gpio pins

  Usage:
  gpio <input|set|clear|toggle> <pin>
      - input/set/clear/toggle the specified pin
  gpio status [-a] [<bank> | <pin>]  - show [all/claimed] GPIOs

Then I tried to get information about GPIOs using gpio status -a:

gpio status -a (within uEnv-default.txt)

And got the next output:

Bank PA:
...
Bank PL:
PL0: func
...
PL4: func     // does it mean that I'm not able to use it as an input
...
PL11: func
PL12: input: 0 [ ]
PL13: input: 0 [ ]
....

So the question is: Am I able to use PL4 gpio pin as an input with the current gpio configuration, and if not, how can I change gpio configuration for this pin, if it's possible at all?

I guess, I have to apply a patch over U-Boot to make a proper gpio configurarion, am I right?


Solution

  • I've found a solution. There were two problems:

    1. U-boot (2018.11) for sunxi-8 devices, at least, doesn't use the above expression to cast a gpio name to a number representation (it's possible to type a gpio name for u-boot gpio command: gpio input pl4).
    2. Port L, by default is unclocked / unpowered on sunxi-8 cpus, at least sun8i-h2-plus-orangepi-zero board, so it has to be clocked/powered by prcm_apb0_enable. Look at https://forum.armbian.com/topic/10111-orange-pi-zero-u-boot-gpio-access/?tab=comments#comment-76996