DESK-MP1-L/Peripherals/USB OTG

From DAVE Developer's Wiki
Jump to: navigation, search
History
Issue Date Notes
2023/08/02 DESK-MP1-L-1.0.0 release


Peripheral USB OTG[edit | edit source]

Device tree configuration[edit | edit source]

Here below an example of device tree configuration used on standard DAVE's kit for the ETRA SOM

OTG port can be forced using the 4th DIP-switch (see the DIP settings section on the Boot configurations page):

  • OFF: otg mode, the connected peripheral has its ID pin properly set and it is able to change the working mode according to the pin logic value
  • ON: host force the host mode.

From kernel device tree stm32mp157a-dcsb5000dd1r.dts:

...
...
&usbotg_hs{
	pinctrl-names = "default", "sleep";
	pinctrl-0 = <&usb_otg_hs_pins_mx>;
	pinctrl-1 = <&usb_otg_hs_sleep_pins_mx>;
	status = "okay";

	/* USER CODE BEGIN usbotg_hs */
	compatible = "st,stm32mp15-hsotg", "snps,dwc2";
	phys = <&usbphyc_port1 0>;
	phy-names = "usb2-phy";
	vbus-supply = <&vbus_otg>;
	dr_mode = "otg";
	/* USER CODE END usbotg_hs */
};

&usbphyc_port1{
	status = "okay";

	/* USER CODE BEGIN usbphyc_port1 */
	phy-supply = <&vdd_usb>;
	/* USER CODE END usbphyc_port1 */
};
...
...
pinctrl {
	...
	...
	usb_otg_hs_pins_mx: usb_otg_hs_mx-0 {
		pins {
			pinmux = <STM32_PINMUX('A', 10, ANALOG)>; /* USB_OTG_HS_ID */
		};
	};

	usb_otg_hs_sleep_pins_mx: usb_otg_hs_sleep_mx-0 {
		pins {
			pinmux = <STM32_PINMUX('A', 10, ANALOG)>; /* USB_OTG_HS_ID */
		};
	};
    ...
    ...
};
...
...

Accessing the peripheral[edit | edit source]

Linux messages at boot time[edit | edit source]

root@desk-mp1:~# dmesg | grep otg
[    2.202854] vbus_otg: supplied by bst_out
[    2.591726] dwc2 49000000.usb-otg: supply vusb_d not found, using dummy regulator
[    2.598534] dwc2 49000000.usb-otg: supply vusb_a not found, using dummy regulator
[    2.746347] dwc2 49000000.usb-otg: EPs: 9, dedicated fifos, 952 entries in SPRAM
[    2.753429] dwc2 49000000.usb-otg: DWC OTG Controller
[    2.757541] dwc2 49000000.usb-otg: new USB bus registered, assigned bus number 1
[    2.765060] dwc2 49000000.usb-otg: irq 74, io mem 0x49000000
[   13.812830] dwc2 49000000.usb-otg: bound driver configfs-gadget

Usage in Host mode[edit | edit source]

When a USB peripheral is inserted, in the following example a memory mass storage device, the kernel recognizes the device (i.e. class, vendor id, product id, etc.)

[  109.857720] usb 1-1: new high-speed USB device number 2 using dwc2
[  110.117799] usb-storage 1-1:1.0: USB Mass Storage device detected
[  110.127163] scsi host1: usb-storage 1-1:1.0
[  111.447577] scsi 1:0:0:0: Direct-Access     ADATA    USB Flash Drive  1100 PQ: 0 ANSI: 0 CCS
[  111.462507] sd 1:0:0:0: [sdb] 7913472 512-byte logical blocks: (4.05 GB/3.77 GiB)
[  111.471019] sd 1:0:0:0: Attached scsi generic sg1 type 0
[  111.491495] sd 1:0:0:0: [sdb] Write Protect is off
[  111.495087] sd 1:0:0:0: [sdb] Mode Sense: 43 00 00 00
[  111.501814] sd 1:0:0:0: [sdb] No Caching mode page found
[  111.505814] sd 1:0:0:0: [sdb] Assuming drive cache: write through
[  111.520046]  sdb: sdb1
[  111.528131] sd 1:0:0:0: [sdb] Attached SCSI removable disk

Usage with mass-storage[edit | edit source]

root@desk-mp1:~# dd if=/dev/zero of=/tmp/mass count=16 bs=1M
16+0 records in
16+0 records out
16777216 bytes (17 MB, 16 MiB) copied, 0.111651 s, 150 MB/s
root@desk-mp1:~# mkfs.ext4 /tmp/mass
mke2fs 1.46.5 (30-Dec-2021)
Discarding device blocks: done
Creating filesystem with 16384 1k blocks and 4096 inodes
Filesystem UUID: 354cc3e0-2b27-4573-b1d6-d7fb99c3a535
Superblock backups stored on blocks:
        8193

Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

root@desk-mp1:~# mkdir -p /tmp/loop
root@desk-mp1:~# mount /tmp/mass /tmp/loop
root@desk-mp1:~# dd if=/dev/urandom of=/tmp/loop/pippo bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB, 10 MiB) copied, 0.715313 s, 14.7 MB/s
root@desk-mp1:~# md5sum /tmp/loop/pippo
31743a261ce01c14f9c28f6529caba72  /tmp/loop/pippo
root@desk-mp1:~# umount /tmp/loop
root@desk-mp1:~# echo "" > /sys/kernel/config/usb_gadget/g1/UDC
root@desk-mp1:~# rm /sys/kernel/config/usb_gadget/g1/configs/c.1/rndis.0
root@desk-mp1:~# mkdir /sys/kernel/config/usb_gadget/g1/functions/mass_storage.1
root@desk-mp1:~# echo "/tmp/mass" > /sys/kernel/config/usb_gadget/g1/functions/mass_storage.1/lun.0/file
root@desk-mp1:~# ln -s /sys/kernel/config/usb_gadget/g1/functions/mass_storage.1 /sys/kernel/config/usb_gadget/g1/configs/c.1
root@desk-mp1:~# echo 49000000.usb-otg > /sys/kernel/config/usb_gadget/g1/UDC 
root@desk-mp1:~# lsblk
NAME         MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda            8:0    1   16M  0 disk 
mmcblk1      179:0    0 14.9G  0 disk 
|-mmcblk1p1  179:1    0  256K  0 part 
|-mmcblk1p2  179:2    0  256K  0 part 
|-mmcblk1p3  179:3    0  256K  0 part 
|-mmcblk1p4  179:4    0  256K  0 part 
|-mmcblk1p5  179:5    0    4M  0 part 
|-mmcblk1p6  179:6    0    4M  0 part 
|-mmcblk1p7  179:7    0  512K  0 part 
|-mmcblk1p8  179:8    0   64M  0 part /boot
|-mmcblk1p9  179:9    0   16M  0 part /vendor
|-mmcblk1p10 179:10   0    2G  0 part /
`-mmcblk1p11 179:11   0  128M  0 part /usr/local
mmcblk2      179:16   0  3.5G  0 disk 
mmcblk2boot0 179:32   0    2M  1 disk 
mmcblk2boot1 179:48   0    2M  1 disk 
root@desk-mp1:~# mkdir -p /tmp/usb
root@desk-mp1:~# mount /dev/sda /tmp/usb/
root@desk-mp1:~# md5sum /tmp/usb/pippo
31743a261ce01c14f9c28f6529caba72  /tmp/usb/pippo