如何使用 開源硬體Banana PI BPI-R2's GPIO-s

BananaPi開源硬體發表於2021-04-06

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 


all GPIO 定義,請參考線上文件 : 


%E9%A6%99%E8%95%89%E6%B4%BE_BPI-R2#BPI-R2_GPIO_.E5.AE.9A.E4.B9.89

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69966964/viewspace-2766755/,如需轉載,請註明出處,否則將追究法律責任。

相關文章