USB host example

chegewara
Posts: 2207
Joined: Wed Jun 14, 2017 9:00 pm

USB host example

Postby chegewara » Sat Mar 20, 2021 7:40 pm

Quick and simple usb host example tested on saola dev board:

https://github.com/chegewara/esp32s2-usb-host-example

Code: Select all

I (59554) : Port powered ON
Waiting for conenction
I (71057) : HCD_PORT_EVENT_CONNECTION
I (71057) : HCD_PORT_STATE_DISABLED
Resetting
I (71117) : USB device reseted
I (71117) : HCD_PORT_STATE_ENABLED
Full speed enabled
I (71117) Pipe: : XFER status: 0, num bytes: 64, actual bytes: 18

Device descriptor:
Length: 18
Descriptor type: 18
USB version: 2.10
Device class: 0x00 (>ifc)
Device subclass: 0x00
Device protocol: 0x00
EP0 max packet size: 64
VID: 0x0781
PID: 0x5597
Revision number: 1.00
Manufacturer id: 1
Product id: 2
Serial id: 3
Configurations num: 1
I (71147) Pipe: : XFER status: 0, num bytes: 0, actual bytes: 0
I (71164) Pipe: : XFER status: 0, num bytes: 64, actual bytes: 32

Config:
Number of Interfaces: 1
Attributes: 0x80
Max power: 224 mA

Interface:
bInterfaceNumber: 0
bAlternateSetting: 0
bNumEndpoints: 2
bInterfaceClass: 0x08 (Mass Storage)
bInterfaceSubClass: 0x06
bInterfaceProtocol: 0x50

Endpoint:
bEndpointAddress: 0x81
bmAttributes: 0x02
bDescriptorType: 5
wMaxPacketSize: 64
bInterval: 0 ms

Endpoint:
bEndpointAddress: 0x02
bmAttributes: 0x02
bDescriptorType: 5
wMaxPacketSize: 64
bInterval: 0 ms
I (71201) Pipe: : XFER status: 0, num bytes: 64, actual bytes: 16
strings: SanDisk
I (71209) Pipe: : XFER status: 0, num bytes: 64, actual bytes: 34
strings: Cruzer Glide 3.0
I (71219) Pipe: : XFER status: 0, num bytes: 64, actual bytes: 42
strings: 4C530000240507207073

chegewara
Posts: 2207
Joined: Wed Jun 14, 2017 9:00 pm

Re: USB host example

Postby chegewara » Tue Mar 23, 2021 1:11 am

I just added some example class, USB serial CDC-ACM, and here is few logs to show how it works.
Class is not finished yet, because there is still missing INTR support in API, but even without it it is functional device.

Code: Select all

I (1165) Set control line state: 
I (1165) Set line coding: Bitrate: 115200, stop bits: 0, parity: 0, bits: 5
I (1166) Get line coding: Bitrate: 115200, stop bits: 0, parity: 0, bits: 5
I (3491) : t
I (3739) : e
I (3979) : s
I (4131) : t
I (4715) :  
I (5643) : m
I (5875) : e
I (6363) : s
I (6523) : s
I (6803) : a
I (6979) : g
I (7211) : e
I (7387) :  
I (8907) : f
I (9179) : r
I (10059) : o
I (10292) : m
I (10451) :  
I (10739) : p
I (11467) : u
I (11763) : t
I (11915) : t
I (12315) : y
I (13867) : 
I (47567) : t
I (47576) : e
I (47586) : st message from arduino IDE serial monitor
Here is example of long data sent in 1 message. EP data buffer is set to 64 bytes, but can be bigger, for example 256

Code: Select all

I (1165) Set control line state: 
I (1165) Set line coding: Bitrate: 115200, stop bits: 0, parity: 0, bits: 5
I (1166) Get line coding: Bitrate: 115200, stop bits: 0, parity: 0, bits: 5
I (3560) : "
I (3566) : L
I (3576) : orem ipsum dolor sit amet, consectetur adipiscing elit, sed do e
I (3586) : iusmod tempor incididunt ut labore et dolore magna aliqua. Ut en
I (3596) : im ad minim veniam, quis nostrud exercitation ullamco laboris ni
I (3606) : si ut aliquip ex ea commodo consequat. Duis aute irure dolor in 
I (3616) : reprehenderit in voluptate velit esse cillum dolore eu fugiat nu
I (3626) : lla pariatur. Excepteur sint occaecat cupidatat non proident, su
I (3636) : nt in culpa qui officia deserunt mollit anim id est laborum."

Code: Select all

I (4578) : "
I (4586) : L
I (4596) : orem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut eni
I (4606) : m ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in re
I (4616) : prehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt 
I (4626) : in culpa qui officia deserunt mollit anim id est labo
I (4636) : rum."

chegewara
Posts: 2207
Joined: Wed Jun 14, 2017 9:00 pm

Re: USB host examples

Postby chegewara » Wed Mar 24, 2021 8:07 am

Library has been refactored to use events. Now it is just base code to build any USB class on top of it.

Next example is build on top of previous example and it is fully functional CDC-ACM example:
https://github.com/chegewara/esp32-s2-usb-host-cdc

In this example host is awaiting IN data from device and it is printing to logs, also when "?" is sent from device then short message ("test\n") will be send to CDC device. By default serial speed is set to 115200.

aegard
Posts: 3
Joined: Tue May 21, 2019 12:52 pm

Re: USB host example

Postby aegard » Thu Mar 25, 2021 6:34 pm

Why does esp keep rebooting when I connect usb device.

Code: Select all

start pipe event task
start pipe event task
Hello world USB host!
[0;32mI (376) : USB host setup properly[0m
[0;32mI (376) : Port is power ON now[0m
[0;32mI (386) : port event: 1[0m
[0;32mI (636) : HCD_PORT_EVENT_CONNECTION[0m
[0;32mI (636) : HCD_PORT_STATE_DISABLED[0m
[0;32mI (696) : USB device reset[0m
[0;32mI (696) : HCD_PORT_STATE_ENABLED[0m

Device descriptor:
Length: 18
Descriptor type: 18
USB version: 1.10
Device class: 0xff (Vendor specific)
Device subclass: 0x00
Device protocol: 0x00
EP0 max packet size: 8
VID: 0x0000
PID: 0x0000
Revision number: 0.00
Manufacturer id: 0
Product id: 0
Serial id: 0
Configurations num: 0
[0;32mI (716) : address set: 1[0m
[0;32mI (716) : set current configuration: 1[0m

Config:
Number of Interfaces: 1
Attributes: 0x80
Max power: 96 mA

Interface:
bInterfaceNumber: 0
bAlternateSetting: 0
bNumEndpoints: 3
bInterfaceClass: 0xff (Vendor specific)
bInterfaceSubClass: 0x01
bInterfaceProtocol: 0x02

Endpoint:
bEndpointAddress: 0x82
bmAttributes: 0x02
bDescriptorType: 5
wMaxPacketSize: 32
bInterval: 0 ms

Endpoint:
bEndpointAddress: 0x02
bmAttributes: 0x02
bDescriptorType: 5
wMaxPacketSize: 32
bInterval: 0 ms

Endpoint:
bEndpointAddress: 0x81
bmAttributes: 0x03
bDescriptorType: 5
wMaxPacketSize: 8
bInterval: 1 ms
[0;33mW (776) : Device stalled: CTRL pipe, state: 1[0m
Guru Meditation Error: Core  0 panic'ed (StoreProhibited). Exception was unhandled.

Core  0 register dump:
PC      : 0x4001ad62  PS      : 0x00060930  A0      : 0x80085e16  A1      : 0x3ffc1830  
A2      : 0x00000000  A3      : 0x00000000  A4      : 0x00000040  A5      : 0x00000000  
A6      : 0x00000040  A7      : 0x00000004  A8      : 0x00060023  A9      : 0x3ffbda20  
A10     : 0x00000000  A11     : 0x00000000  A12     : 0x000000a5  A13     : 0x000000a5  
A14     : 0x3ffbdaa4  A15     : 0x00000001  SAR     : 0x0000001f  EXCCAUSE: 0x0000001d  
EXCVADDR: 0x00000000  LBEG    : 0x000000a5  LEND    : 0x000000a5  LCOUNT  : 0x40023f31  

Backtrace:0x4001ad5f:0x3ffc1830 0x40085e13:0x3ffc1840 0x4008505a:0x3ffc1870 0x40094322:0x3ffc18a0 0x4002913d:0x3ffc18c0


ELF file SHA256: 9a65a27172614c3e

Rebooting...

chegewara
Posts: 2207
Joined: Wed Jun 14, 2017 9:00 pm

Re: USB host example

Postby chegewara » Fri Mar 26, 2021 3:37 am

Its my bad, sorry. Ive been playing a bit with espressif code trying to add INTR endpoints support and i pushed wrong files to github.
Please try to use original files from here in project components folder:
https://github.com/espressif/esp-idf/tr ... onents/usb

Or try to remove this code, which may sporadically cause crash, i didnt have that crash before so i missed it:

Code: Select all

            xfer_get_string(1);
            vTaskDelay(1);
            xfer_get_string(2);
            vTaskDelay(1);
            xfer_get_string(3);

aegard
Posts: 3
Joined: Tue May 21, 2019 12:52 pm

Re: USB host example

Postby aegard » Tue Apr 13, 2021 9:12 pm

any idea on why this happens?

Code: Select all

[0;32mI (8082948) STRING CB: [28] USB2.0-Serial
strings: USB2.0-Serial
[0;32mI (8082958) STRING CB: [28] USB2.0-Serial
strings: USB2.0-Serial
[0;32mI (8082968) STRING CB: [28] USB2.0-Serial
strings: USB2.0-Serial
[0;32mI (8082968) GET CONFIG: 1
[0;33mW (8082978) STALLED: 21 22 03 00 00 00 00 00 
[0;33mW (8082978) : Device stalled: CTRL pipe, state: 1
[0;32mI (8082988) Ctrl data: 21 22 03 00 00 00 00 00

chegewara
Posts: 2207
Joined: Wed Jun 14, 2017 9:00 pm

Re: USB host example

Postby chegewara » Wed Apr 14, 2021 5:02 pm

USB device cant respond on that request:
Endpoint is halted, or control pipe request is not supported.
https://www.usbmadesimple.co.uk/ums_3.htm

tongshezheng
Posts: 3
Joined: Sun Apr 25, 2021 1:12 am

Re: USB host example

Postby tongshezheng » Sun Apr 25, 2021 1:43 am

你好,请问,怎么才能启动中断传输呢?
QQ截图20210425093707.jpg
QQ截图20210425093707.jpg (85.39 KiB) Viewed 15134 times
QQ截图20210425093545.jpg
QQ截图20210425093545.jpg (70.06 KiB) Viewed 15134 times
我现在启动中断传输后,chan不产生中断。

chegewara
Posts: 2207
Joined: Wed Jun 14, 2017 9:00 pm

Re: USB host example

Postby chegewara » Sun Apr 25, 2021 4:17 pm

Sorry, but INT endpoints are not supported yet. Ive been playing a bit with code, but i failed, and i thought i removed all changes i made.
You can replace content of that file with original content from espressif:
https://github.com/espressif/esp-idf/bl ... 1621-L1665

tongshezheng
Posts: 3
Joined: Sun Apr 25, 2021 1:12 am

Re: USB host example

Postby tongshezheng » Mon Apr 26, 2021 3:23 am

你好,是否有ESP32-S2 USB HOST 周期性请求队列的操作说明或相关文档呢?
目前的参考手册没有这方面的说明。

Who is online

Users browsing this forum: No registered users and 41 guests