The development board acts as a USB host, responsible for powering connected slave devices and controlling data transmission.
The device operates as a slave device (controlled by the host) and cannot connect to other USB devices.
The device can dynamically switch between Host Mode and Device Mode without a fixed role.
Modify the device tree file according to the following project path:
Add the following content to this file:
x/include/ "system-conf.dtsi"
/ {
};
# Added content/ { usb_phy0: phy0@e0002000 { compatible = "ulpi-phy"; #phy-cells = <0>; reg = <0xe0002000 0x1000>; view-port = <0x0170>; drv-vbus; };};&usb0 { # Select the mode as needed dr_mode = "otg"; /*dr_mode = "peripheral";*/ /*dr_mode = "host";*/ usb-phy = <&usb_phy0>; usb-reset = <&gpio0 10 0>;};References:
1.https://xilinx-wiki.atlassian.net/wiki/spaces/A/pages/18842272/Zynq+Linux+USB+Device+Driver
Use the command:
xxxxxxxxxxpetalinux-config -c kernelIn the character interface that pops up, enable the following content: The driver can be compiled into the kernel or compiled as a kernel module to be dynamically loaded after system startup. It is recommended to compile it as a kernel module.
xxxxxxxxxx#Compile directly into the kernelDevice Drivers --->[*]USB support ---> [*]USB Gadget Support ---> [*]USB Gadget functions configurable through configfs [*]Abstract controlo Model(CDC ACM) [*]USB Gadget precomposed confiurations-----> [*]Mass Storage Gadget [*]Serial Gadget(with CDC ACM and CDC OBEX support) #Compile as kernel moduleDevice Drivers --->[*]USB support ---> [*]USB Gadget Support ---> [M]USB Gadget functions configurable through configfs [M]Abstract controlo Model(CDC ACM) [*]USB Gadget precomposed confiurations-----> [M]Mass Storage Gadget [M]Serial Gadget(with CDC ACM and CDC OBEX support) # [*]: Indicates that the function is selected and enabled, meaning it will be compiled into the kernel and become part of the kernel. This is kernel static compilation. For example, if Mass Storage Gadget under USB Gadget Support is selected, this function will be directly compiled into the kernel and enabled every time the kernel starts, without loading any modules.# [M]: Indicates that the function will be compiled as a kernel module, meaning it will not be directly compiled into the kernel but dynamically loaded after system startup. This configuration provides greater flexibility, allowing modules to be loaded or unloaded as needed without recompiling the kernel. For example, if Serial Gadget (with CDC ACM and CDC OBEX support) is selected with [M], this function will be compiled as a module and can be manually loaded or unloaded using modprobe or insmod commands, facilitating dynamic management. Use the command
xxxxxxxxxxpetalinux-build -c kernelCopy the recompiled kernel and device tree to the SD card. Power on and load the system, then manually load the kernel modules in the console or telnet:
xxxxxxxxxxcd /lib/modules/5.10.0-xilinx-v2021.1/kernel/drivers/usb/gadget/legacyinsmod u_serial.koinsmod libcomposite.koinsmod usb_f_serial.koinsmod usb_f_obex.koinsmod usb_f_acm.koinsmod g_serial.ko
# insmod libcomposite.ko;insmod u_serial.ko;insmod usb_f_serial.ko;insmod usb_f_acm.ko;insmod g_serial.ko# rmmod g_serial;rmmod usb_f_acm;rmmod usb_f_serial;rmmod u_serial;rmmod libcomposite;You will see the /dev/ttyGS0 device:
If you don't want to manually load the kernel modules every time you power on, you can write these commands into a startup script to automatically load it after power-on.
Prepare a USB drive in advance (must be FAT32 format), and create several documents on it using a computer, such as creating two documents named a.txt and b.txt.
Short-circuit JP2 and JP3 on the development board with jumper caps. After powering on and starting up, insert the USB drive into the OTG corresponding Type-C interface (a Type-C to Type-A female adapter is required). The following prompt information will be seen on the console serial port:
Next, enter the command to view the USB drive mounting and memory usage:
xxxxxxxxxxdf -hYou can see the mount point is located at "/run/media/sda1". According to the USB drive position shown in the image above, enter cd /run/media/sda1 to access the USB drive.
xxxxxxxxxxcd /run/media/sda1Next, you can enter "ls" to view the contents of the USB drive:
xxxxxxxxxxlsYou can see there are two files, a.txt and b.txt, in the folder. You can also directly modify the contents of the USB drive.
Since we enabled USB CDC during kernel configuration, when connected to a computer via a USB cable, this USB will be virtualized as a serial port.
After normal power-on and system startup, first load the kernel modules corresponding to the virtual serial port mentioned earlier. Then connect the development board to the computer using a USB cable with Type-C connectors on both ends. You will see two serial ports in the serial debugging assistant, where COM11 is the virtual serial port created by the USB OTG interface, and COM12 is the console serial port of the Zynq Linux system.
The virtual serial port corresponds to /dev/ttyGS0 on the Zynq.
Development board sending data to computer via virtual serial port
Use the following command on the development board's console serial port to send data to the computer:
xxxxxxxxxxecho 123 > /dev/ttyGS0
The received data will be displayed on the computer's serial terminal:
Computer can also send data to development board via virtual serial port
Use the 'cat' command on the development board to read data received by the virtual serial port. Enter: hello\n in the terminal corresponding to the computer's virtual serial port, and the received data will be displayed on the development board's console terminal.
Note: In the terminal, sending and receiving serial data both use '\n' as the termination condition. That is, sent data will append '\n', and received data must echo upon receiving '\n'.