贫穷贵公子 发表于 2023-8-5 08:51:11

【米尔瑞萨RZ/G2L开发板-创新应用】开启ADB devices功能

本文介绍,如何在开发板上开启ADB功能,方便后期的文件上传,等功能。毕竟,ADB功能较多,调试更为方便。

包括如下步骤:
1)kernel开启ADB相关的CONFIG配置宏
1.1)yocto recipes目录结构


1.2)USB gadget相关配置宏
CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
CONFIG_USB_MON=y
USB_GADGET_DEBUG=y
CONFIG_USB_GADGET_DEBUG=y
CONFIG_USB_GADGET_DEBUG_FILES=y
CONFIG_USB_GADGET_DEBUG_FS=y
CONFIG_USB_CONFIGFS=y
CONFIG_USB_FUNCTIONFS=m
CONFIG_USB_G_SERIAL=m
CONFIG_USB_U_ETHER=y
CONFIG_USB_F_ECM=y
CONFIG_USB_F_SUBSET=y
CONFIG_USB_F_RNDIS=y
CONFIG_USB_F_MASS_STORAGE=y
CONFIG_USB_CONFIGFS_SERIAL=y
CONFIG_USB_CONFIGFS_ACM=y
CONFIG_USB_CONFIGFS_OBEX=y
CONFIG_USB_CONFIGFS_NCM=y
CONFIG_USB_CONFIGFS_ECM=y
CONFIG_USB_CONFIGFS_ECM_SUBSET=y
CONFIG_USB_CONFIGFS_RNDIS=y
CONFIG_USB_CONFIGFS_EEM=y
CONFIG_USB_CONFIGFS_MASS_STORAGE=y
CONFIG_USB_CONFIGFS_F_LB_SS=y
CONFIG_USB_CONFIGFS_F_FS=y
CONFIG_USB_CONFIGFS_F_UAC1=y
CONFIG_USB_CONFIGFS_F_MIDI=y
CONFIG_USB_CONFIGFS_F_HID=y
CONFIG_USB_CONFIGFS_F_UVC=y
CONFIG_USB_CONFIGFS_F_PRINTER=y2)增加usb gadget相关的recipes文件

2.1)目录结构如下




增加usb-start_1.0.bb文件,用来编译以及打包usb gadget相关的配置。
2.2)由于当前开发板使用systemd管理开机自启服务,因此需要在usb-start_1.0.bb文件当中,也需要使用systemd管理开机自启服务。
2.3)usb-start_1.0.bb文件内容如下。

#
# USB composite device configuration
#

SUMMARY = "USB composite device configuration"
SECTION = "examples"
LICENSE = "CLOSED"

SRC_URI = " \
    file://launch_adbd \
    file://usb_start.service \
    file://usb_start.sh \
"

S = "${WORKDIR}"

do_install() {
    install -m 0755 usb_start.sh -D ${D}${sysconfdir}/init.d/usb_start.sh
    install -m 0755 launch_adbd-D ${D}${sysconfdir}/launch_adbd

    install -m 0644 usb_start.service -D ${D}${systemd_unitdir}/system/usb_start.service
    if [ ! -d ${D}${systemd_unitdir}/system/multi-user.target.wants];then
      mkdir ${D}${systemd_unitdir}/system/multi-user.target.wants
    else
      echo ${D}${systemd_unitdir}/system/multi-user.target.wants exist
    fi
    ln -sf ${systemd_unitdir}/system/usb_start.service ${D}${systemd_unitdir}/system/multi-user.target.wants/usb_start.service
}

FILES_${PN} += "${systemd_unitdir}/system"
FILES_${PN} += "${systemd_unitdir}/system/local-fs.target.wants/"2.4)launch_adbd文件内容如下

#! /bin/sh
#
# Copyright (c) 2017, The Linux Foundation. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#   * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above
#   copyright notice, this list of conditions and the following
#   disclaimer in the documentation and/or other materials provided
#   with the distribution.
#   * Neither the name of The Linux Foundation nor the names of its
#   contributors may be used to endorse or promote products derived
#   from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
# ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE

export TERM=linux
DAEMON="adbd"
PIDFILE="/var/run/$DAEMON.pid"

if [ -f /etc/default/adbd ]; then
    echo "/etc/default/adbd" > /dev/kmsg
    . /etc/default/adbd
fi
echo "Launch adb is starting adbd... "
start-stop-daemon -b -m -S -q -p "$PIDFILE" -x "/usr/bin/adbd"

# If UDC is not set up set it up.
if [ -z /sys/kernel/config/usb_gadget/g1/UDC ]; then
   udcname=`ls -1 /sys/class/udc | head -n 1`
   echo -n "udcname $udcname" > /dev/kmsg
   echo $udcname > /sys/kernel/config/usb_gadget/g1/UDC
fi

echo -n "Launch adbd. done" > /dev/kmsg

exit 0
2.5)usb_start.service文件内容如下


Description=Renesas usb start
After=auditd.service


Type=simple
RemainAfterExit=yes
#Restart=on-failure
#RestartSec=5s
#Nice=-5
ExecStart=/etc/init.d/usb_start.sh start
ExecStop=/etc/init.d/usb_start.sh stop


WantedBy=multi-user.target
2.6)usb_start.sh文件内容如下

#!/bin/sh

DAEMON="start_usb"
PIDFILE="/var/run/$DAEMON.pid"

UpdateLog()
{
    echo "${1}" 2>&1 | tee /dev/kmsg
}

run_usb_configfs() {
    usb_vid=0x0BB8
    usb_pid=0x5300
    manu_string="Renesas_Manufacturer"
    prod_string="Renesas_Product"
    usb_maxpower=500
    #serialnum=`devmem 0x5c005238`
    serialnum="123456789"
    #func_str="ecm.ecm,ffs.adb,gser.0,uac1.uac1,mass_storage.0,ncm.0,rndis.rndis"
    #func_str="ecm.ecm,ffs.adb,gser.0,uac1.uac1,mass_storage.0,ncm.0"
    #func_str="gser.0,gser.1,gser.2,gser.3,ffs.adb,uac1.uac1"
    #func_str="ecm.ecm,gser.0,ffs.adb,uac1.uac1"
    #func_str="ffs.adb,uac1.uac1"
    func_str="ffs.adb"

    UpdateLog "start adb"
    pkill adbd
    /etc/launch_adbd start
    sleep 1

    cd /sys/kernel/config/usb_gadget/g1
    echo $usb_vid > idVendor
    echo $usb_pid > idProduct
    echo 0xA0 > configs/c.1/bmAttributes
    echo "UAC_SER_RMNET" > configs/c.1/strings/0x409/configuration
    echo $manu_string > strings/0x409/manufacturer
    echo $prod_string > strings/0x409/product
    echo $serialnum   > strings/0x409/serialnumber
    echo $usb_maxpower > /sys/kernel/config/usb_gadget/g1/configs/c.1/MaxPower

    funcs=$(echo $func_str |tr "," "\n")
    func_index=1
    for func in $funcs
    do
      echo "ln $func $func_index"
      ln -s functions/$func configs/c.1/f$func_index
      func_index=$((func_index+1))
    done
    UpdateLog "binding UDC with Gadget ...\n" $1
    echo $1 > UDC
    cd /
}

start() {
    UpdateLog 'Starting %s: \n' $DAEMON
    #depmod
    #modprobe libcomposite
    #sleep 1
    #mount -t configfs none /sys/kernel/config
    if [ -d /sys/kernel/config/usb_gadget ];then
      UpdateLog "Configuring the USB gadget using ConfigFS"
      cd /sys/kernel/config/usb_gadget/
      mkdir g1
      cd g1
      mkdir strings/0x409
      mkdir configs/c.1
      mkdir configs/c.1/strings/0x409
      mkdir functions/mass_storage.0
      mkdir functions/mass_storage.1
      mkdir functions/ffs.adb
      mkdir functions/gser.0
      mkdir functions/gser.1
      mkdir functions/gser.2
      mkdir functions/gser.3
      mkdir functions/ncm.0
      mkdir functions/uac1.uac1
      mkdir functions/rndis.rndis
      mkdir functions/ecm.ecm

      if [ ! -s "/etc/adb_devid" ]; then
            #serialnum=`devmem 0x5c005238`
            serialnum="123456789"
            echo $serialnum > /etc/adb_devid
            sync
      fi
   
      udcname=`ls /sys/class/udc`
      UpdateLog "udcname=$udcname"
      run_usb_configfs $udcname &

      cd /
    fi

    #mount functionfs
    mkdir -p /dev/usb-ffs/adb
    mount -o uid=2000,gid=2000 -t functionfs adb /dev/usb-ffs/adb
    #mount -o remount,gid=5,mod=620 /dev/pts

    UpdateLog "usb.service initialized successfully"
}

stop() {
    UpdateLog 'Stopping %s: ' "$DAEMON"
    if [ -d /sys/kernel/config/usb_gadget ];then
      UpdateLog "Stop USB Android Gadget"
      echo none > /sys/kernel/config/usb_gadget/g1/UDC
      UpdateLog -n "usb.service stopped successfully"
    fi
}

restart() {
    stop
    sleep 1
    start
}

case "$1" in
    start|stop|restart)
      "$1";;
    reload)
      # Restart, since there is no true "reload" feature.
      restart;;
    *)
      echo "Usage: $0 {start|stop|restart|reload}"
      exit 1
esac
3)打包usb-start相关的recipes文件到根文件系统当中

4)USB gadget ADB功能测试



贫穷贵公子 发表于 2023-8-5 08:59:03

顺便请教一个问题,当前使用123456789作为ADB设备的serialnumber,如果多个开发板同时接入一台host设备,那么就会
因为serialnumber统一,而导致无法区分adb devices。因此,需要使用SOC芯片的 Unique ID作为serialnumber。
我在r01uh0914ej0110-rzg2l-rzg2lc.pdf文件当中,并没有找到Unique ID的寄存器地址,以及读取方法,有知道的,方便告知一下,
万分感谢!!!:handshake

贫穷贵公子 发表于 2023-8-17 22:32:58

找到了这个寄存器,LSI Device ID Register (SYS_DEVID)
设备ID,devmem 0x11020A04
root@myir-yg2lx-1g:/lib/systemd/system# devmem 0x11020A04
0x4841C447

lint019 发表于 2024-3-5 23:21:22

你能上传一下adbd这个执行文件吗?多谢!:P

lint019 发表于 2024-3-6 15:31:55

lint019 发表于 2024-3-5 23:21
你能上传一下adbd这个执行文件吗?多谢!

我已经编译好了,见附件。哈哈。

结合上面的步骤,可以使用adb了

贫穷贵公子 发表于 2024-3-28 12:00:14

yocto构建系统当中,增加一个android-tools依赖,即可。
页: [1]
查看完整版本: 【米尔瑞萨RZ/G2L开发板-创新应用】开启ADB devices功能