I would like to use USB host on ESP32-S2 in ESP-IDF

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

Re: I would like to use USB host on ESP32-S2 in ESP-IDF

Postby chegewara » Mon May 10, 2021 11:38 am

As you can see all pipes but EP0 are created with assigned address:
https://github.com/chegewara/esp32-s2-u ... ass.c#L103

Also when you assign address to device you have 2 options:
1) create new control pipe (EP0) with assigned address
2) update address in existing control pipe
https://github.com/chegewara/esp32-s2-u ... main.c#L99

There is no point to have non control pipe with address 0, because you can send messages to device 0 only on control pipe.

ploegmma
Posts: 20
Joined: Mon Jun 08, 2020 9:10 pm

Re: I would like to use USB host on ESP32-S2 in ESP-IDF

Postby ploegmma » Tue May 18, 2021 9:55 am

chegewara wrote:
Wed Apr 28, 2021 5:57 pm
ploegmma wrote:cannot open source file "hal/usb_types_private.h"
Did you update esp-idf to most recent master? Latest update is from yesterday.
Sorry for the delay. Yes, that was the cause, thank you.

Working great with a Saola linked to a custom board using xfer_in_data()/xfer_out_data(). Just two small questions if you don't mind;

- I assume you have the components/usb in there because of your issue #6887, correct?

- I now just used the repeated xfer_in_data() as in your example and "pick up" received data in "case HCD_PIPE_EVENT_IRP_DONE:", that works in my setup. Is that the way to do it or is there a better way (for use other than testing)? I just need simple bi-directional serial communication and don't know all the ins and outs about the underlying code (yet).

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

Re: I would like to use USB host on ESP32-S2 in ESP-IDF

Postby chegewara » Tue May 18, 2021 11:15 am

ploegmma wrote: - I assume you have the components/usb in there because of your issue #6887, correct?
Actually i decided to go this path because esp-idf usb component is not providing header files, they are hidden in private_include. It is kept like that because usb host component is not ready yet (just guessing).
ploegmma wrote: - I now just used the repeated xfer_in_data() as in your example and "pick up" received data in "case HCD_PIPE_EVENT_IRP_DONE:", that works in my setup. Is that the way to do it or is there a better way (for use other than testing)? I just need simple bi-directional serial communication and don't know all the ins and outs about the underlying code (yet).
There is few ways you can do it. I decided to use callbacks and events (one or both together). There is also polling, which may be not efficient:
https://github.com/espressif/esp-idf/bl ... tainers.md

It is enough to send 1 xfer_in_data/xfer_out_data and driver will take care of keep sending it until device respond with data, ACK, NAK or STALL. In any case you should receive event or callback will be called.
I think the CDC example from my repository is simple enough and a good start.

ploegmma
Posts: 20
Joined: Mon Jun 08, 2020 9:10 pm

Re: I would like to use USB host on ESP32-S2 in ESP-IDF

Postby ploegmma » Tue May 18, 2021 5:45 pm

chegewara wrote:
Tue May 18, 2021 11:15 am
I think the CDC example from my repository is simple enough and a good start.
Thnx! Yes, and I think it's time for me to do some reading on the basics first...

ploegmma
Posts: 20
Joined: Mon Jun 08, 2020 9:10 pm

Re: I would like to use USB host on ESP32-S2 in ESP-IDF

Postby ploegmma » Wed May 19, 2021 9:47 am

After successful communication between two boards, I tried to communicate with a network switch (my ultimate goal). But I get a STALL:

[Codebox]Hello world USB host!
I (327) : USB host setup properly
I (327) : Port is power ON now
I (337) : port event: 1
I (587) : HCD_PORT_EVENT_CONNECTION
I (587) : HCD_PORT_STATE_DISABLED
I (647) : USB device reset
I (647) : HCD_PORT_STATE_ENABLED
Creating IRPs and IRP list
I (647) DEVICE descriptor: 12 01 00 02 ef 02 01 40 f0 03 3f 01 01 00 01 02
I (647) DEVICE descriptor: 03 01

Device descriptor:
Length: 18
Descriptor type: 1
USB version: 2.00
Device class: 0xef (Miscellaneous)
Device subclass: 0x02
Device protocol: 0x01
EP0 max packet size: 64
VID: 0x03f0
PID: 0x013f
Revision number: 0.01
Manufacturer id: 1
Product id: 2
Serial id: 3
Configurations num: 1
I (677) ADDRESS: 1
I (677) SET CONFIG: 1
W (687) STALLED: 80 06 01 02 00 00 00 01
W (687) : Device stalled: CTRL pipe, state: 1
I (697) Ctrl data: 80 06 01 02 00 00 00 01
[/Codebox]

If I interpret this right this means stalled on GET_DESCRIPTOR (0x06). But I have no clue of where to begin in finding the cause of such. Are there ways to debug this?

If I connect the device to my mac I get a device (/dev/cu.usbmodemF80898912511) and can connect to it.

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

Re: I would like to use USB host on ESP32-S2 in ESP-IDF

Postby chegewara » Thu May 20, 2021 4:15 am

Yes, it is get configuration descriptor request.
http://eleccelerator.com/usbdescreqparser/
This request STALLed:
https://github.com/chegewara/esp32s2-us ... #L285-L299
I would try to decrease TRANSFER_DATA_MAX_BYTES, just in case, even if that should not be a problem.
Also read more on CTRL pipe and response:
https://www.beyondlogic.org/usbnutshell ... ml#Control

ploegmma
Posts: 20
Joined: Mon Jun 08, 2020 9:10 pm

Re: I would like to use USB host on ESP32-S2 in ESP-IDF

Postby ploegmma » Thu May 20, 2021 1:35 pm

Thnx for the pointers, I'm learning a lot here...

I hooked up the switch to my Ubuntu machine and captured the bus I connect it to, to see what is used by Ubuntu. This is what I got:

Code: Select all

ffff92b7f09610c0 3979297729 S Ci:3:074:0 s 80 06 0100 0000 0012 18 <
ffff92b7f09610c0 3979297898 C Ci:3:074:0 0 18 = 12010002 ef020140 f0033f01 01000102 0301
I then adjusted TRANSFER_DATA_MAX_BYTES to 4608 and in xfer_get_desc() I hacked USB_CTRL_REQ_INIT_GET_CFG_DESC() to match the request exactly as done by Ubuntu:

Code: Select all

// Commented out USB_CTRL_REQ_INIT_GET_CFG_DESC()
// and instead using this to match ubuntu request:
 ({
     ((usb_ctrl_req_t *) ctrl_irps[0]->data_buffer)->bRequestType = USB_B_REQUEST_TYPE_DIR_IN | USB_B_REQUEST_TYPE_TYPE_STANDARD | USB_B_REQUEST_TYPE_RECIP_DEVICE;
     ((usb_ctrl_req_t *) ctrl_irps[0]->data_buffer)->bRequest = USB_B_REQUEST_GET_DESCRIPTOR;
//     ((usb_ctrl_req_t *) ctrl_irps[0]->data_buffer)->wValue = (USB_W_VALUE_DT_CONFIG << 8) | ((1) & 0xFF);
     ((usb_ctrl_req_t *) ctrl_irps[0]->data_buffer)->wValue = 1; // match ubuntu
     ((usb_ctrl_req_t *) ctrl_irps[0]->data_buffer)->wIndex = 0;
     ((usb_ctrl_req_t *) ctrl_irps[0]->data_buffer)->wLength = (4608);
 });
Now I get the exact same request but still STALLED:

Code: Select all

W (7134) STALLED: 80 06 01 00 00 00 00 12
W (7134) : Device stalled: CTRL pipe, state: 1
I (7134) Ctrl data: 80 06 01 00 00 00 00 12
Maybe a timing issue? I see other people doing funny things like:
https://stackoverflow.com/questions/519 ... escriptors
https://community.st.com/s/question/0D5 ... ptor-stall

I think I will try to hookup a logic analyzer (not sure if my cheap analyzer is fast enough though).

ploegmma
Posts: 20
Joined: Mon Jun 08, 2020 9:10 pm

Re: I would like to use USB host on ESP32-S2 in ESP-IDF

Postby ploegmma » Fri May 21, 2021 6:09 pm

The Logic Analyzer worked. This is what I see:

Connected to another ESP32S2 module, the host code works as expected:

Code: Select all

SETUP ADDR 0 EP 0
DATA0 [ 80 06 00 01 00 00 12 00 ]
ACK
IN ADDR 0 EP 0
NAK
   ... more attempts with NAK
IN ADDR 0 EP 0
DATA1 [ 12 01 00 02 EF 02 01 40 3A 30 01 40 00 01 01 02 03 01 ]
ACK
OUT ADDR 0 EP 0
DATA1 [ ]
NAK
OUT ADDR 0 EP 0
DATA1 [ ]
ACK
SOF 30
   ... more SOFs and a few UNKNOWNs
SETUP ADDR 0 EP 0
DATA0 [ 00 05 01 00 00 00 00 00 ]
ACK
SOF 67
IN ADDR 0 EP 0
NAK
   ... few more attempts with NAK
IN ADDR 0 EP 0
DATA1 [ ]
ACK
SOF 68
SOF 69
SETUP ADDR 1 EP 0
DATA0 [ 00 09 01 00 00 00 00 00 ]
ACK
SOF 70
IN ADDR 1 EP 0
UNKNOWN
   ... more attempts with NAK/UNKNOWN
IN ADDR 1 EP 0
DATA1 [ ]
ACK
SOF 71
SOF 72
UNKNOWN
SETUP ADDR 1 EP 0
DATA0 [ 80 06 01 02 00 00 00 01 ]
ACK
IN ADDR 1 EP 0
NAK
   ... more attempts with NAK/UNKNOWN
IN ADDR 1 EP 0
DATA1 [ 09 02 4B 00 02 01 00 A0 32 08 0B 00 02 02 02 00 00 09 04 00 00 01 02 02 00 04 05 24 00 20 01 05 24 01 00 01 04 24 02 02 05 24 06 00 01 07 05 81 03 08 00 10 09 04 01 00 02 0A 00 00 00 07 05 02 ]
ACK
Connected to a network switch, looks almost identical (quicker) but STALLs:

Code: Select all

SETUP ADDR 0 EP 0
DATA0 [ 80 06 00 01 00 00 12 00 ]
ACK
IN ADDR 0 EP 0
DATA1 [ 12 01 00 02 EF 02 01 40 F0 03 3F 01 01 00 01 02 03 01 ]
ACK
OUT ADDR 0 EP 0
DATA1 [ ]
ACK
SOF 30
Invalid packet (shorter than 8 bits)
Invalid packet (shorter than 16 bits)
SOF 32
   ... more SOFs and UNKNOWNS
SETUP ADDR 0 EP 0
DATA0 [ 00 05 01 00 00 00 00 00 ]
ACK
IN ADDR 0 EP 0
DATA1 [ ]
ACK
SOF 68
SOF 69
UNKNOWN
SETUP ADDR 1 EP 0
DATA0 [ 00 09 01 00 00 00 00 00 ]
ACK
IN ADDR 1 EP 0
DATA1 [ ]
ACK
UNKNOWN
SOF 72
SOF 73
SETUP ADDR 1 EP 0
DATA0 [ 80 06 01 02 00 00 00 01 ]
ACK
IN ADDR 1 EP 0
STALL
But the reason for the stall is a mystery to me. I need to study the code to come up with some next thing to try (like repeating the last setup after the stall).

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

Re: I would like to use USB host on ESP32-S2 in ESP-IDF

Postby chegewara » Fri May 21, 2021 6:46 pm

I think i dont understand, what is the difference between good and bad configuration (from hardware point of view)?

godzilla2
Posts: 19
Joined: Wed Apr 28, 2021 5:15 am

Re: I would like to use USB host on ESP32-S2 in ESP-IDF

Postby godzilla2 » Fri May 21, 2021 7:51 pm

You can also try my library, which is a bit friendlier to use IMO:

https://github.com/chipweinberger/xesp-usbh

Who is online

Users browsing this forum: RichardFalk and 89 guests