How to use Banana PI BPI-R2's GPIO-s

40 Pin Header (on R2 board V1.2 )

image

image

There are two ways to use the R2’s GPIO-s:

  1. Use the debug interface /sys/devices/platform/1000b000.pinctrl/mt_gpio, please refer to script 1
  2. Use the general interface for GPIO: /sys/class/gpio/export and /sys/class/gpio/unexport, please refer to script 2

Script 1 (it can be used on Ubuntu system with kernel 4.4.x)

SYS_FILE=/sys/devices/platform/1000b000.pinctrl/mt_gpio
mt_gpio_mode()
{
   pin=$1
   mode=$2
  
   if [ ! -f ${SYS_FILE} ]; then
       exit 1
   fi

   echo "mode $pin $mode" > ${SYS_FILE}

   return 0   
}

mt_gpio_dir()
{
   pin=$1
   dir=$2
 
   if [ "x${dir}" == "xout" ]; then
       dir_val=1
   else
       dir_val=0
   fi
 
   if [ ! -f ${SYS_FILE} ]; then
       exit 1
   fi

   echo "dir $pin $dir_val" > ${SYS_FILE}

   return 0   
}

mt_gpio_out()
{
   pin=$1
   out=$2
 
   if [ ! -f ${SYS_FILE} ]; then
       exit 1
   fi

   echo "out $pin $out" > ${SYS_FILE}

   return 0   
}

mt_gpio_in()
{
   pin=$1
 
   if [ ! -f ${SYS_FILE} ]; then
       exit 1
   fi

   echo "start $1" > ${SYS_FILE}
   result=`cat ${SYS_FILE} | grep "$1"`

   if [ "x${result}" == "x" ]; then
       echo "can't get $pin status"
       exit 2
   fi
   echo ${result}
   pin_val=`echo ${result} | awk -F ' |-' '{print $5}'`
   if [ ${pin_val} == "0" ] ; then
       return 0
   else
       return 1
   fi
  
   return 0

}

Script 2 (it can be used on Ubuntu system with kernel 4.4.x, and lede with kernel 4.9.x)

#!/bin/ash

DIR=/sys/class/gpio/

global_gpio_offset=0

mt_gpio_init()
{
    pin=$1

    EXPORT=/sys/class/gpio/export
    UNEXPORT=/sys/class/gpio/unexport


    name=`ls $DIR | grep chip`
    if [ x${name} != x ]; then
        echo "chip id : $name"
    else
        echo "No available gpio chip"
        exit 1
    fi

    base=`cat ${DIR}/${name}/base`

    gpio_offset=`echo $((base+$pin))`
    global_gpio_offset=$gpio_offset

    if [ -d ${DIR}/gpio${gpio_offset} ]; then
        return 0
    else
        echo ${gpio_offset} > ${EXPORT}
    fi
}

mt_gpio_init $1

if [ $2 == 'dir' ]; then
    if [ $3 == 'out' ]; then
        echo out > ${DIR}/gpio${global_gpio_offset}/direction
    else
        echo in > ${DIR}/gpio${global_gpio_offset}/direction
    fi
elif [ $2 == 'val' ]; then
    if [ $# == 2 ]; then
        cat ${DIR}/gpio${global_gpio_offset}/value
    elif [ $# == 3 ]; then
        echo $3 >  ${DIR}/gpio${global_gpio_offset}/value
    fi   
fi 

all GPIO define,please see :

https://bananapi.gitbooks.io/banana-pi-bpi-r2-open-source-smart-router/content/bpi-r2-gpio-pin-define.html

Hi.

How Can I use the GPIO with C++ ? Can I use wiringPi on Banana Pi - R2?

Imho you can use open/read/write-functions also with sysfs/debugfs…wiringpi has to be changed to use the right path

How can I change path of using by WiringPi?

you mean I can use linux standart C open/read/write-functions for control gpio?

i have no tried it yet, but these are simple “files” which should be opened by these standard-functions

have also not digged into wiring pi but gpio-access itself is similar to raspi…

Can you give a usage example of scripts, please? I couldn’t run the script 2.

script also doesn’t work

chip id : gpiochip233
./example_gpio.sh: line 25: base+: syntax error: operand expected (error token is "+")
./example_gpio.sh: line 31: echo: write error: Invalid argument
./example_gpio.sh: line 37: [: ==: unary operator expected
./example_gpio.sh: line 43: [: ==: unary operator expected