如何使用 开源硬件Banana PI BPI-R2's GPIO-s


#1

BPI-R2集成MTK7623N 四核 ARM Cortex-A7 MP 支持主频1.3 ghz,BPI-R2 路由器提供丰富各种外设接口,包括 HDMI TX 高清接口, MIPI DSI 屏接口, PCIe2.0 接口, USB2.0 OTG 端口, USB3.0 端口, SATA 端口,5 千兆网口, 板载802.11a/b/g/n wifi 无线& BT4.1蓝牙 , 并且通过mini PCI-e支持 802.11ac/n WLAN

BPI-R2 系统支持 Android5.1,并完善支持 OpenWRT,Debian linux,Ubuntu linux, Raspbian 和其他嵌入式操作系统. 尺寸与BPI-R1完全一样大小, 支持 1080P y高清视频输入,40 PIN GPIO 与 Raspberry Pi 3一致.

同香蕉派其他板一样,Banana Pi BPI-R2 也有40-pin GPIO 接口:

Banana Pi BPI-R2 的40 PIN GPIO 定义与树莓派 3 一亲,下面是 Banana Pi GPIO定义:

Banana pi BPI-R2 40 PIN GPIO 定义
GPIO Pin Name Default Function
CON1-P01 VCC-3V3
CON1-P02 VCC-5V
CON1-P03 I2C_SDA0 GPIO75
CON1-P04 VCC-5V
CON1-P05 I2C_SCL0 GPIO76
CON1-P06 GND
CON1-P07 PWM-003 GPIO206
CON1-P08 UTXD1 GPIO80
CON1-P09 GND
CON1-P10 URXD0 GPIO81
CON1-P11 URXD1 GPIO80
CON1-P12 I2S0_BCK GPIO74
CON1-P13 UTXD0 GPIO79
CON1-P14 GND
CON1-P15 PWM-002 GPIO205
CON1-P16 URTS1 GPIO25
CON1-P17 VCC-3V3
CON1-P18 UCTS1 GPIO24
CON1-P19 SPI0_MO GPIO56
CON1-P20 GND
CON1-P21 SPI0_MI GPIO55
CON1-P22 PCM_CLK GPIO18
CON1-P23 SPI0_CK GPIO54
CON1-P24 SPI0_CSN GPIO53
CON1-P25 GND
CON1-P26 PCM_RX GPIO20
CON1-P27 I2C_SDA1 GPIO57
CON1-P28 I2C_SCL1 GPIO58
CON1-P29 PCM_RST GPIO22
CON1-P30 GND
CON1-P31 SPDIF_OUT GPIO200
CON1-P32 PCM_TX GPIO21
CON1-P33 I2S0_MCLK GPIO126
CON1-P34 GND
CON1-P35 I2S0_LRCK GPIO73
CON1-P36 PCM_SYNC GPIO19
CON1-P37 SPDIF_IN1 GPIO202
CON1-P38 I2S0_DATA_IN GPIO72
CON1-P39 GND
CON1-P40 I2S0_DATA_OUT GPI049

有两种方式使用R2的GPIO-s:

使用调试接口/sys/devices/platform/1000b000。pinctrl/mt_gpio,参考脚本1

使用GPIO的通用接口:/sys/class/ GPIO /export和/sys/class/ GPIO /unexport,请参考脚本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