Use the debug interface /sys/devices/platform/1000b000.pinctrl/mt_gpio, please refer to script 1
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
Hi, I was missing support in WiringPi for BPI R2 so I forked @chaotaklon repo from this post and I added support for GPIO for now. I tested few GPIO ports and it works fine. I also had to fix port mapping.
When you use wiringPiSetup(), you can use ports from column wPi from gpio readall or you can use physical pin numbering by calling wiringPiSetupPhys().