3. USB Operation Guide

3.1. Operational Readiness

USB 2.0 Host/Device is prepared as follows:

  • Use the U-boot and Kernel released by SDK.

  • The file system can use the local file system ext4 or squashfs, or NFS.

  • Shell script“run_usb.sh”.run_usb.sh uses the USB ConfigFS function of the kernel to customize the USB device. Users can refer to and modify run_usb.sh to change the parameters related to PID / VID and function. For detailed operation, refer to the kernel file ” linux/Documentation/usb/gadget_configfs.txt”.

3.2. Uboot Operation Process

3.2.1. The USB Host Operation Process Base on the Uboot

Only pen drive and hard disk storage devices are supported in Uboot. USB host is off by default in uboot. You must enable relevant config.

Step1. enable USB related driver under uboot:

CONFIG_USB=y
CONFIG_DM_USB=y
CONFIG_USB_STORAGE=y
CONFIG_CMD_USB=y

Step2.

2.1) Cv180x series modify include/configs/cv180x-asic.h.

2.2) Cv181x series modify include/configs/cv181x-asic.h, newly add the definition:

#define CONFIG_USB_DWC2
#define CONFIG_USB_DWC2_REG_ADDR        0x04340000

Step3. Compile the drivers. Compile uboot to generate fip.bin

build_uboot

3.2.2. The USB Drive Operation Process Based on the Uboot

The preparation before starting the Uboot USB Host:

USB Host base on the Uboot does not support hot plug. You must plug in the device before starting the USB host. If a USB Hub is installed on the platform, ensure that the Hub’s power source is enabled and the Switch in the USB path is switched to the Host connector.

Take cv180x as an example (the corresponding method is also applicable to cv181x) :

Power the platform, access the uboot Command Line Interface, and run the usb start command to check whether the identification is successful.

phobos_c906# usb start
starting USB...
USB0:   Core Release: 4.00a
scanning bus 0 for devices... Device NOT ready
   Request Sense returned 02 3A 00
2 USB Device(s) found
      scanning usb for storage devices... 2 Storage Device(s) found

If an enumeration error occurs after “usb start” or the device cannot be detected, run the “setenv usb_pgood_delay XXX” command on the uboot command- line interface (CLI), you can adjust the timeout value for a device that is preheated slowly or connected to the Hub. The recommended value ranges from 1000 to 3000.

After completing the recognition, run the “usb tree” command to view the recognition rate. The following is an example of connecting a USB host to a Hub and a storage device:

phobos_c906# usb tree
USB device tree:
1  Hub (480 Mb/s, 0mA)
|   U-Boot Root Hub
|
+-2  Mass Storage (480 Mb/s, 500mA)
      Generic USB3.0 Card Reader 000000001532

Initialization and application:

After completing the recognition you can enter the follow operations:

Step1: check the device information

  • CLI execute: usb info [dev], You can view information about all devices on the controller. The following is an example.

phobos_c906# usb info 1
config for device 1
2: Mass Storage,  USB Revision 2.10
- Generic USB3.0 Card Reader 000000001532
- Class: (from Interface) Mass Storage
- PacketSize: 64  Configurations: 1
- Vendor: 0x05e3  Product 0x0749 Version 21.50
   Configuration: 1
   - Interfaces: 1 Bus Powered 500mA
   Interface: 0
   - Alternate Setting 0, Endpoints: 2
   - Class Mass Storage, Transp. SCSI, Bulk only
   - Endpoint 1 In Bulk MaxPacket 512
   - Endpoint 2 Out Bulk MaxPacket 512

Step2: Read the pen drive

  • Run: usb read addr blk# cnt, in command line to read the data with the starting address of blk and the size of cnt to the DDR address of addr, as shown in the following example:

phobos_c906# usb read 0x90000000 0 10

USB read: device 0 block # 0, count 16 ... 16 blocks read: OK

Step3: Write the pen drive

  • Run: usb write addr blk# cnt, in command line to write the data with DDR address addr and size cnt to the location with the starting address blk of the storage device. The example is as follows:

phobos_c906# usb write 0x90000000 2000 2000
USB read: device 0 block # 8192, count 8192 ... 8192 blocks write: OK

3.3. Linux Host

3.3.1. USB 2.0 Host Operation Process

Step2: start the platform and load ext3 or squashfs. (or use the NFS)

Step3: load the relevant drivers

insmod usb-common.ko

insmod usbcore.ko

insmod udc-core.ko

insmod roles.ko

insmod dwc2.ko

step4: set USB role

echo host > /proc/cviusb/otg_role

3.3.2. USB Pen Drive Operation Process

Insert detection:

Insert the USB drive directly and observe whether the enumeration is successful. Normally, the UART is printed as:

[   72.061964] usb 1-1: new high-speed USB device number 2 using dwc2
[   72.315816] usb-storage 1-1:1.0: USB Mass Storage device detected
[   72.335934] scsi host0: usb-storage 1-1:1.0
[   73.363027] scsi 0:0:0:0: Direct-Access     Generic  STORAGE DEVICE   1532 PQ: 0 ANSI: 6
[   73.374407] sd 0:0:0:0: Attached scsi generic sg0 type 0
[   73.558597] sd 0:0:0:0: [sda] 30253056 512-byte logical blocks: (15.5 GB/14.4 GiB)
[   73.566961] sd 0:0:0:0: [sda] Write Protect is off
[   73.571922] sd 0:0:0:0: [sda] Mode Sense: 21 00 00 00
[   73.577899] sd 0:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[   73.593961]  sda: sda1
[   73.602607] sd 0:0:0:0: [sda] Attached SCSI removable disk

sda1 represents the first partition on the USB drive or portable hard drive. When there are multiple partitions, the words sda1, sd2, sda3, etc. will appear

Initialization and application:

After inserting the storage device, perform the following operations:

In sdXY, X is the disk number and Y is the partition number. Please modify it according to the specific system environment。

  • The device node for partition command operation is sdX, example: $fdisk /dev/sda.

  • The specific partition formatted with mkdosfs tool is sdXY: ~$ mkdosfs -F 32 /dev/sda1。

  • The specific partition of mount is sdXY: ~$ mount /dev/sda1 /mnt

  1. View partition information

    • Run the command “ls /dev” to view the system device files, and if there is no partition information sdXY, there is no partition. Please partition the storage device with fdisk and go to step 2.

    • If there is partition information sdXY, the pen drive partition has been detected and entered step 2.。

  2. View formatted information

    • If it is not formatted, use mkdosfs to format and then go to step 3.

    • If formatted, go to step 3

  3. Mount the directory

    • Run the “mount /dev/sdaXY /mnt” mount directory.。

  4. Read and write the storage device.

3.4. Linux Device

3.4.1. USB 2.0 Device Operation Process

Step1. Compile the kernel driver module of USB2.0 Device

  • Enter the following path of menuconfig and configure it as follows.

Device Driver --->
 [*]   USB support --->
     <*>   USB Gadget Support  --->
         <M>   USB functions configurable through configfs
         [*]     Abstract Control Model (CDC ACM)
         [*]     Mass storage
  • Compile kernel module and generate .ko file

Step 2: Start the platform, load ext4 or squashfs file system, or use NFS

Step 3: When the platform is used as a device, the USB2.0 device module must be loaded to be recognized as a USB device on the Host side. Please refer to “operation example” for specific operation.

All USB 2.0 device drivers are listed below.

3.4.2. Examples of storage device operations in USB device

Step4: As a Device, the platform supports both eMMC and SD storage media as follows:

Step5: Load below kernel modules.

insmod configfs.ko

insmod usb-common.ko

insmod udc-core.ko

insmod libcomposite.ko

insmod usbcore.ko

insmod roles.ko

insmod dwc2.ko

step6: The paths of USB Device related modules under kernels are:

drivers/usb/gadget/libcomposite.ko

drivers/usb/gadget/function/usb_f_mass_storage.ko

fs/configfs/configfs.ko

step7: switch otg controller to device mode

echo device > /proc/cviusb/otg_role

step8: run shell script “usb_usb.sh”

run_usb.sh probe msc /dev/mmcblkXY

run_usb.sh start

mmcblkXY is the Yth partition in eMMC or SD of the Xth disk. Please select it according to your specific situation.

Step9: The path of the USB Device related module under rootfs is: /etc/run_usb.sh

Step10: By connecting the platform to the Host side through USB, the platform can be recognized as a USB storage device on the Host side, and the corresponding device nodes can be generated in the / dev directory.

Step11: On the Host side, the platform can be treated as a common USB storage device, partitioning, formatting, reading and writing.

3.4.3. Example of Terminal Device Operation in USB device

The platform acts as a device as a terminal device by doing the following:

Setp1: insmod below kernel modules.

insmod configfs.ko

insmod libcomposite.ko

insmod u_serial.ko

insmod usb_f_acm.ko

insmod usb_f_serial.ko

The paths of USB Device related modules under kernels are:

  • drivers/usb/gadget/libcomposite.ko

  • drivers/usb/gadget/function/usb_f_serial.ko

  • drivers/usb/gadget/function/usb_f_acm.ko

  • drivers/usb/gadget/function/u_serial.ko

  • fs/configfs/configfs.ko

Switch otg controller to device mode

echo device > /proc/cviusb/otg_role

run script “run_usb.sh” run_usb.sh probe acm run_usb.sh start

The path of the USB Device related module under rootfs is:

/etc/run_usb.sh

Step2: By connecting the platform to the Host through USB, the platform can be recognized as a USB terminal device in the Host side, and the corresponding device node ttyACMX, X, is the same type of terminal device number, is generated in the /dev directory. ttyGSY is generated in the device side /dev directory, Y is the same type of terminal device number.

Host and Device can transmit data through the terminal device.

3.4.4. Example of RNDIS Device Operation in USB Device

The platform acts as an RNDIS device as follows

<<<<<<< HEAD Step3. Load below kernel modules. ======= Step1. insmod below kernel modules. >>>>>>> 8360b7f… [fix](peripheral): Fix errors of Peripheral_Driver_Operation_Guide

insmod configfs.ko

insmod libcomposite.ko

insmod u_ether.ko

insmod usb_f_ecm.ko

insmod usb_f_eem.ko

insmod usb_f_rndis.ko

The paths of USB Device related modules under kernels are:

  • drivers/usb/gadget/libcomposite.ko

  • drivers/usb/gadget/function/usb_f_ecm.ko

  • drivers/usb/gadget/function/usb_f_ecm.ko

  • drivers/usb/gadget/function/usb_f_rndis.ko

  • drivers/usb/gadget/function/u_ether.ko

  • fs/configfs/configfs.ko

Switch otg controller to device mode

echo device > /proc/cviusb/otg_role

run script “run_usb.sh” run_usb.sh probe rndis run_usb.sh start

The path of the USB Device related module under rootfs is:

/etc/run_usb.sh

Step2: By connecting the platform to USB Host size via USB, you can recognize the platform as a USB Remote NDIS device on the Host side and install the Remote NDIS Compatible Device driver on Windows.

USBOpe002

USBOpe003

Step3: Set IP Address on Single Board, for example”ifconfig usb0 192.168.3.101 up”

Step4: Set the IP address on Windows.

USBOpe004

Host and Device can transmit data through RNDIS devices.

3.4.5. Operation Example of CVITEK USB GADGET in USB device

The platform works as a Device using a custom CVTEK USB Gadget (CVG) as follows:

Step1: Insert below kernel modules

insmod configfs.ko

insmod libcomposite.ko

insmod usb_f_cvg.ko

The paths of USB Device related modules under kernels are:

  • drivers/usb/gadget/libcomposite.ko

  • drivers/usb/gadget/function/usb_f_cvg.ko

  • fs/configfs/configfs.ko

Switch otg controller to device mode

echo device > /proc/cviusb/otg_role

run script “run_usb.sh”

run_usb.sh probe cvg

run_usb.sh start

The path of the USB Device related module under rootfs is:

/etc/run_usb.sh

Step2: Connect the platform to the Host side via USB and use Zadig to install libusb (WinUSB) as the driver of the device.

USBOpe005

Step3: Run test program sample_cvg [#TEST] on a single board.

Step4: Execute cvg/pctool/gen_patterns.sh on PC generates test Patterns. Execute ”cvg/pctool/cvg_test.py”to start testing.

Step5: Refer to the CVITEK USB Gadget Usage Guide.docx for detailed usage files.

3.5. Points to Pay Attention to in Operation

  • The following points should be noted in the operation: The system is preset to be Host mode after boot-up. To use Device mode, modules must be insmoded and USB ConfigFS scripts executed. Before switching to device mode, users must confirm the following:

    • The USB Cable is not connected to the Host.

    • The hardware on the platform has to switch to the corresponding USB mode. For example, before switching to Device mode, turn off the USB 5V power supply on the platform. If there is a Hub on the platform, turn off the Hub power and switch the path to Device mode connector

  • After switching to Device mode, to use Host mode again, users must restart the platform

  • When the platform is used as a terminal device, due to the TTY terminal characteristics, if a large amount of data is transmitted in a short time, it may cause data loss. Users should be aware of this limitation when using this feature.

  • When reading a pen drive using USB Host under Uboot, be aware that if there is a Hub on the platform, you must turn on the Hub power and switch the path to the correct Connector.