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

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 7:58 pm

chegewara wrote:
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)?

My final goal is to talk to a network switch that has a USB CDC port. When I connect this switch to a PC I get a cdc_acm port. When I try to use the USB host code with an ESP32S2 (Saola) to connect I get this STALL. However when using the code (on the Saola) to connect to a second ESP32S2 it works fine. There I do not get a STALL.

I just logged the traffic produced between a PC (Ubuntu) and the switch to see what happens there (as the PC is able to connect). Now I see that there are several STALLs also. The difference however is that Ubuntu seems to repeat requests when a STALL is encountered (I have not yet studied the trace):

Code: Select all

31625911-31625981 USB packet: Packets: SETUP ADDR 0 EP 0
31625989-31626187 USB packet: Packets: DATA0 [ 80 06 00 01 00 00 40 00 ]
31626191-31626229 USB packet: Packets: ACK
31626292-31626361 USB packet: Packets: IN ADDR 0 EP 0
31626366-31626404 USB packet: Packets: NAK
31626903-31626972 USB packet: Packets: IN ADDR 0 EP 0
31626977-31627338 USB packet: Packets: DATA1 [ 12 01 00 02 EF 02 01 40 F0 03 3F 01 01 00 01 02 03 01 ]
31627348-31627386 USB packet: Packets: ACK
31627399-31627468 USB packet: Packets: OUT ADDR 0 EP 0
31627477-31627547 USB packet: Packets: DATA1 [ ]
31627552-31627590 USB packet: Packets: ACK
32951865-32951934 USB packet: Packets: SOF 714
... more SOFs
34697501-34697571 USB packet: Packets: SETUP ADDR 0 EP 0
34697579-34697778 USB packet: Packets: DATA0 [ 00 05 0B 00 00 00 00 00 ]
34697783-34697821 USB packet: Packets: ACK
34697833-34697902 USB packet: Packets: IN ADDR 0 EP 0
34697907-34697945 USB packet: Packets: NAK
34698419-34698488 USB packet: Packets: IN ADDR 0 EP 0
34698493-34698563 USB packet: Packets: DATA1 [ ]
34698572-34698610 USB packet: Packets: ACK
34704010-34704079 USB packet: Packets: SOF 787
... more SOFs
35273327-35273396 USB packet: Packets: SETUP ADDR 11 EP 0
35273405-35273603 USB packet: Packets: DATA0 [ 80 06 00 01 00 00 12 00 ]
35273607-35273645 USB packet: Packets: ACK
35273709-35273778 USB packet: Packets: IN ADDR 11 EP 0
35273783-35273821 USB packet: Packets: NAK
35274308-35274377 USB packet: Packets: IN ADDR 11 EP 0
35274383-35274744 USB packet: Packets: DATA1 [ 12 01 00 02 EF 02 01 40 F0 03 3F 01 01 00 01 02 03 01 ]
35274753-35274791 USB packet: Packets: ACK
35274904-35274973 USB packet: Packets: OUT ADDR 11 EP 0
35274983-35275053 USB packet: Packets: DATA1 [ ]
35275057-35275095 USB packet: Packets: ACK
35275685-35275754 USB packet: Packets: SETUP ADDR 11 EP 0
35275764-35275962 USB packet: Packets: DATA0 [ 80 06 00 06 00 00 0A 00 ]
35275966-35276004 USB packet: Packets: ACK
35276168-35276237 USB packet: Packets: IN ADDR 11 EP 0
35276242-35276280 USB packet: Packets: STALL
35277690-35277759 USB packet: Packets: SETUP ADDR 11 EP 0
35277769-35277967 USB packet: Packets: DATA0 [ 80 06 00 06 00 00 0A 00 ]
35277971-35278007 USB packet: Packets: Invalid packet (shorter than 16 bits)
35278071-35278140 USB packet: Packets: IN ADDR 11 EP 0
35278145-35278183 USB packet: Packets: STALL
35280057-35280126 USB packet: Packets: SOF 811
35280136-35280205 USB packet: Packets: SETUP ADDR 11 EP 0
35280214-35280412 USB packet: Packets: DATA0 [ 80 06 00 06 00 00 0A 00 ]
35280416-35280454 USB packet: Packets: ACK
35280509-35280578 USB packet: Packets: IN ADDR 11 EP 0
35280583-35280621 USB packet: Packets: STALL
35282219-35282288 USB packet: Packets: SETUP ADDR 11 EP 0
35282297-35282494 USB packet: Packets: DATA0 [ 80 06 00 02 00 00 09 00 ]
35282499-35282537 USB packet: Packets: ACK
35282700-35282769 USB packet: Packets: IN ADDR 11 EP 0
35282774-35282988 USB packet: Packets: DATA1 [ 09 02 4B 00 02 01 00 20 32 ]
35282997-35283035 USB packet: Packets: ACK
35283149-35283218 USB packet: Packets: OUT ADDR 11 EP 0
35283228-35283298 USB packet: Packets: DATA1 [ ]
35283302-35283340 USB packet: Packets: ACK
35284543-35284612 USB packet: Packets: SETUP ADDR 11 EP 0
35284621-35284819 USB packet: Packets: DATA0 [ 80 06 00 02 00 00 4B 00 ]
35284823-35284861 USB packet: Packets: ACK
35284924-35284993 USB packet: Packets: IN ADDR 11 EP 0
35284998-35285036 USB packet: Packets: NAK
35285549-35285618 USB packet: Packets: IN ADDR 11 EP 0
35285623-35286145 USB packet: Packets: DATA1 [ 09 02 4B 00 02 01 00 20 32 08 0B 00 02 02 02 00 00 09 04 00 00 01 02 02 01 00 05 24 00 ]
35286182-35286717 USB packet: Packets: UNKNOWN
35286727-35286763 USB packet: Packets: Invalid packet (shorter than 16 bits)
35286773-35286842 USB packet: Packets: IN ADDR 11 EP 0
35286847-35286885 USB packet: Packets: NAK
35287397-35287466 USB packet: Packets: IN ADDR 11 EP 0
35287472-35287718 USB packet: Packets: DATA0 [ 02 40 00 00 07 05 81 02 40 00 00 ]
35287727-35287765 USB packet: Packets: ACK
35287798-35287867 USB packet: Packets: OUT ADDR 11 EP 0
35287877-35287947 USB packet: Packets: DATA1 [ ]
35287951-35287989 USB packet: Packets: ACK
35288585-35288654 USB packet: Packets: SETUP ADDR 11 EP 0
35288664-35288864 USB packet: Packets: DATA0 [ 80 06 00 03 00 00 FF 00 ]
35288868-35288906 USB packet: Packets: ACK
35289068-35289137 USB packet: Packets: IN ADDR 11 EP 0
35289142-35289275 USB packet: Packets: DATA1 [ 04 03 09 04 ]
35289285-35289323 USB packet: Packets: ACK
35289438-35289507 USB packet: Packets: OUT ADDR 11 EP 0
35289516-35289586 USB packet: Packets: DATA1 [ ]
35289590-35289628 USB packet: Packets: ACK
35290571-35290640 USB packet: Packets: SETUP ADDR 11 EP 0
35290649-35290849 USB packet: Packets: DATA0 [ 80 06 02 03 09 04 FF 00 ]
35290853-35290891 USB packet: Packets: ACK
35291054-35291123 USB packet: Packets: IN ADDR 11 EP 0
35291129-35291167 USB packet: Packets: NAK
35291838-35291907 USB packet: Packets: IN ADDR 11 EP 0
35291912-35292494 USB packet: Packets: DATA1 [ 20 03 48 00 50 00 4E 00 20 00 53 00 65 00 72 00 69 00 61 00 6C 00 20 00 50 00 6F 00 72 00 74 00 ]
35292503-35292541 USB packet: Packets: ACK
35292555-35292624 USB packet: Packets: OUT ADDR 11 EP 0
35292633-35292703 USB packet: Packets: DATA1 [ ]
35292707-35292745 USB packet: Packets: ACK
35293860-35293929 USB packet: Packets: SETUP ADDR 11 EP 0
35293939-35294139 USB packet: Packets: DATA0 [ 80 06 01 03 09 04 FF 00 ]
35294143-35294181 USB packet: Packets: ACK
35294347-35294416 USB packet: Packets: IN ADDR 11 EP 0
35294421-35294587 USB packet: Packets: DATA1 [ 06 03 48 00 50 00 ]
35294596-35294634 USB packet: Packets: ACK
35294752-35294821 USB packet: Packets: OUT ADDR 11 EP 0
35294830-35294900 USB packet: Packets: DATA1 [ ]
35294904-35294942 USB packet: Packets: ACK
35295770-35295839 USB packet: Packets: SETUP ADDR 11 EP 0
35295848-35296047 USB packet: Packets: DATA0 [ 80 06 03 03 09 04 FF 00 ]
35296053-35296089 USB packet: Packets: Invalid packet (shorter than 16 bits)
35296142-35296211 USB packet: Packets: IN ADDR 11 EP 0
35296216-35296254 USB packet: Packets: NAK
35296758-35296827 USB packet: Packets: IN ADDR 11 EP 0
35296832-35297285 USB packet: Packets: DATA1 [ 18 03 46 00 38 00 30 00 38 00 39 00 38 00 39 00 31 00 32 00 35 00 31 00 ]
35297295-35297333 USB packet: Packets: ACK
35297452-35297521 USB packet: Packets: OUT ADDR 11 EP 0
35297530-35297600 USB packet: Packets: DATA1 [ ]
35297604-35297642 USB packet: Packets: ACK
35304059-35304128 USB packet: Packets: SOF 812
35328061-35328130 USB packet: Packets: SOF 813
35342695-35342764 USB packet: Packets: SETUP ADDR 11 EP 0
35342774-35342971 USB packet: Packets: DATA0 [ 00 09 01 00 00 00 00 00 ]
35342976-35343014 USB packet: Packets: ACK
35343218-35343287 USB packet: Packets: IN ADDR 11 EP 0
35343292-35343362 USB packet: Packets: DATA1 [ ]
35343371-35343409 USB packet: Packets: ACK
35345545-35345614 USB packet: Packets: SETUP ADDR 11 EP 0
35345623-35345820 USB packet: Packets: DATA0 [ 21 20 00 00 00 00 07 00 ]
35345825-35345863 USB packet: Packets: ACK
35345927-35345996 USB packet: Packets: OUT ADDR 11 EP 0
35346005-35346186 USB packet: Packets: DATA1 [ 80 25 00 00 00 00 08 ]
35346191-35346229 USB packet: Packets: ACK
35346281-35346350 USB packet: Packets: IN ADDR 11 EP 0
35346356-35346394 USB packet: Packets: NAK
35346898-35346967 USB packet: Packets: IN ADDR 11 EP 0
35346973-35347011 USB packet: Packets: NAK
35347514-35347583 USB packet: Packets: IN ADDR 11 EP 0
35347589-35347625 USB packet: Packets: Invalid packet (shorter than 16 bits)
35348506-35348575 USB packet: Packets: IN ADDR 11 EP 0
35348581-35348649 USB packet: Packets: DATA1 [ ]
35348659-35348697 USB packet: Packets: ACK

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 8:08 pm

godzilla2 wrote:
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
I will have a look thank you

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 » Sat May 22, 2021 2:21 am

1. You can try to re-send the same request when it STALL
2. can you use "usbview" on linux to see USB device configuration, i am wondering how it looks like

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 » Sat May 22, 2021 9:49 am

chegewara wrote:
Sat May 22, 2021 2:21 am
1. You can try to re-send the same request when it STALL
2. can you use "usbview" on linux to see USB device configuration, i am wondering how it looks like

1. I was thinking about using a flag to signal when the device init is done and when encountering a stall while not done repeat by calling xfer_get_device_desc() after this line: https://github.com/chegewara/esp32s2-us ... ipe.c#L210

Would that work? Or is there a better way?

2. see image here https://postimg.cc/PL4hz5tN

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 » Sat May 22, 2021 10:23 am

ploegmma wrote: 1. I was thinking about using a flag to signal when the device init is done and when encountering a stall while not done repeat by calling xfer_get_device_desc() after this line: https://github.com/chegewara/esp32s2-us ... ipe.c#L210

Would that work? Or is there a better way?
Its up to you, depending what programming style you prefer. Myself i would try to move this few lines down, before break, to let pipe reset:
https://github.com/chegewara/esp32s2-us ... ipe.c#L207
and then in callback just repeat STALLed command. From logs you know this is command that STALL:

Code: Select all

80 06 01 02 00 00 00 01
You should be able to easy detect it in callback. Of course i am assuming you are using callbacks.
Another thing i would try is to add small delay before sending STALLed commands (1-5 ms).
Code seems to work, just you need to add some more errors handling, like STALLed request/response and device look like standard CDC ACM, which should works with my example. I am also assuming you will have to add device specific requests, but you seem to have tools and knowledge how to use logic analyzer.

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 » Sat May 22, 2021 11:21 am

Much appreciated. I will dig into it (will take some time) and report what I find.

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 » Sat May 22, 2021 7:30 pm

chegewara wrote:
Sat May 22, 2021 10:23 am
and then in callback just repeat STALLed command.
Another thing i would try is to add small delay before sending STALLed commands (1-5 ms).

I did exactly that and upped the delay up to 50 ms but no change:

Code: Select all

void usbh_ctrl_pipe_stalled_cb(usb_ctrl_req_t* ctrl)
{
    ESP_LOG_BUFFER_HEX_LEVEL("STALLED, retrying after 50ms", ctrl, 8, ESP_LOG_WARN);
    vTaskDelay( 50 / portTICK_PERIOD_MS );
    if(stall_retry > 0) {
        stall_retry--;
        if((ctrl->bRequestType == USB_B_REQUEST_TYPE_DIR_IN) && (ctrl->bRequest == USB_B_REQUEST_GET_DESCRIPTOR) && (((ctrl->wValue >> 8) & 0xff) == USB_W_VALUE_DT_CONFIG)) {
            xfer_get_desc();            
        }
    }
}
But then I noticed you use a Descriptor Index of 1 in USB_CTRL_REQ_INIT_GET_CFG_DESC (here https://github.com/chegewara/esp32s2-us ... ipe.c#L287). But Ubuntu uses 0. So I changed it to 0. And guess what: no STALL :)

I get a nice dump of the config. So I'm now on the right track. Not fully yet because now I get a stall in the next step. But I will try to figure that out also. If you're interested I will report on that too.
But for now I would like to know if there is a special reason why you used a Descriptor Index of 1. It's not clear to me what it does.

On usbmadesimple I only read:
A Get Configuration Descriptor fetches the descriptors for just one configuration depending on the descriptor index in wValue of the SETUP packet. Most devices only have one configuration, because built-in Windows drivers always select the first configuration.

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 » Sat May 22, 2021 9:47 pm

No special reason. I was not sure if enumeration starts from 0 or from 1 and it did work with esp32 S2 CDC, so i didnt change 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 » Mon Jun 07, 2021 10:48 pm

I should announce it a bit earlier, but i missed the fact that espressif updated USB host component and figured it out just today.
In most recent update we get working INTR and ISO endpoints, at least when we read component changes.
I would like to share that INTR seems to work and work pretty good, dont know about ISO yet.
Today i could make simple app that can work with regular USB mouse:

Code: Select all

Hello world USB host!
start pipe event task
I (332) : USB host setup properly
I (332) : Port is power ON now
I (342) : port event: 1
start pipe event task
I (592) : HCD_PORT_EVENT_CONNECTION
I (592) : HCD_PORT_STATE_DISABLED
I (652) : USB device reset
I (652) : HCD_PORT_STATE_ENABLED
I (652) : Creating default pipe

I (658) DEVICE descriptor: 12 01 10 01 00 00 00 08 00 00 00 68 00 01 00 01 
I (669) DEVICE descriptor: 00 01 

Device descriptor:
Length: 18
Descriptor type: 1
USB version: 1.10
Device class: 0x00 (>ifc)
Device subclass: 0x00
Device protocol: 0x00
EP0 max packet size: 8
VID: 0x0000
PID: 0x6800
Revision number: 1.00
Manufacturer id: 0
Product id: 1
Serial id: 0
Configurations num: 1
I (725) STRING CB: [4] 
strings: 
I (735) STRING CB: [4] 
strings: 
I (745) STRING CB: [46] . USB OPTICAL MOUSE
strings:  USB OPTICAL MOUSE
I (758) ADDRESS: 1
I (772) SET CONFIG: 1
I (786) CONFIG descriptor: 09 02 22 00 01 01 00 a0 32 09 04 00 00 01 03 01 
I (794) CONFIG descriptor: 02 00 09 21 11 01 00 01 22 42 00 07 05 81 03 06 
I (801) CONFIG descriptor: 00 0a 

Config:
Number of Interfaces: 1
Attributes: 0xa0
Max power: 100 mA

Interface:
bInterfaceNumber: 0
bAlternateSetting: 0
bNumEndpoints: 1
bInterfaceClass: 0x03 (HID)
bInterfaceSubClass: 0x01
bInterfaceProtocol: 0x02

Endpoint:
bEndpointAddress: 0x81
bmAttributes: 0x03
bDescriptorType: 5
wMaxPacketSize: 6
bInterval: 10 ms

I (4009) : 01 01 00 00 00 00  // left button down
I (4185) : 01 00 00 00 00 00 
I (6433) : 01 02 00 00 00 00  // right button down
I (6601) : 01 00 00 00 00 00 
I (7777) : 01 00 00 00 00 00 
I (7785) : 01 00 d5 2f fe 00 
I (7793) : 01 00 da ff fd 00 
I (7801) : 01 00 e4 7f fe 00 
I (7809) : 01 00 e8 cf fe 00 
I (7817) : 01 00 e5 1f ff 00 
I (7825) : 01 00 eb 0f ff 00 
I (7833) : 01 00 ef 4f ff 00 
I (7865) : 01 00 00 00 00 00 
From what i could see with logic analyzer 1 INTR XFER will be repeated as long as it wont get ACK-nowledged by device. Requests are automatically with interval similar i could detect on linux PC.
Now i have to buy regular USB keyboard, but i think that the same code can be used with mouse and with keyboard.

zliudr
Posts: 360
Joined: Thu Oct 03, 2019 5:15 am

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

Postby zliudr » Tue Jun 08, 2021 1:46 am

I've not done my share of exploring yet but I'm excited to see this. I'm still working on my own specialized host stack on MAX3421E.

So are you saying that the software driver does the periodic polling of INTR or the hardware with "repeating thingies" that I read about on hardware spec sheet? I'm asking because I want to know whether the responsibility of the INTR polling lies, with software or hardware. For instance, on MAX3421E host IC I'm working on, there is no hardware enabled method to repeatedly polling at a fixed interval. Thus the host firmware must run a scheduler to do poll with either an NAK (most of the time) or ACK and data (say keyboard report). This is a lot of work on the main MCU. With ESP32 running RTOS, I can imagine a high-privilege task runs the schedules but would be happy to be wrong if there is a hardware scheduler that does that.

Who is online

Users browsing this forum: Google [Bot] and 146 guests