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

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 » Fri Mar 19, 2021 8:39 pm

Thanks for the response. Is hcd an independent open source project, an Espressif project, else?
I wish to find references to the USB host stack and maybe test it like chegewara is doing.
chegewara, thanks for trailblazing the testing. I'm interested in trying as well, maybe on a separate machine. If I understand correctly I have installed ESP-IDF V4.3 beta-1. Not sure how I can go to the master branch. Maybe it's safer for basic git users like me to just install the master branch and toolchain on a separate computer?

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 Mar 19, 2021 9:31 pm

Ok, mystery solved. I sent as port calback_arg pointer to port even queue, but i should pass handle to event queue. Now it is working with Samsung S9+ and pendrive, but is problem with CP2102N.

Code: Select all

I (632) : USB host setup properly
I (732) : hcd_port_state_t: 0
I (732) : Port powered ON
I (732) : hcd_port_state_t: 1
Waiting for conenction
port callback
        -> Port event: 1
I (45157) : HCD_PORT_EVENT_CONNECTION
I (45157) : HCD_PORT_STATE_DISABLED
Resetting
I (45217) : USB device reseted
I (45217) : HCD_PORT_STATE_ENABLED
Full speed enabled

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 Mar 20, 2021 9:12 am

I am trying to get USB host to work for over 12 hours. Basic stuff is working, but there is something wrong when i am trying to assign address to device. My procedure:
- use logic analyzer to check packets flow,
- init host, works,
- after connection established, get device descriptor, works,
- i am using pipe callbacks, but i found that i need to add this code to dequeue all pipe events (i would expect to get HCD_PIPE_EVENT_NONE instead):

Code: Select all

        while(1){
            req_hdl = hcd_xfer_req_dequeue(default_pipe);
            if(req_hdl == NULL) break;
            hcd_xfer_req_get_target(req_hdl, &pipe_hdl, &irp, &context);
            ESP_LOGI("Pipe: ", "XFER status: %d, num bytes: %d, actual bytes: %d", irp->status, irp->num_bytes, irp->actual_num_bytes);
        }
- then i am sending to set address, which is working, the send part

Code: Select all

USB_CTRL_REQ_INIT_SET_ADDR((usb_ctrl_req_t *) data_buffers[0], addr);
- from now on it is something wrong:
-- host is not sending PID IN requests, as expected, to get set address confirmation,
-- instead i see only PID OUT requests in LA, which are followed by PID DATA1 device descriptor packet, which is NAK by host (S2),
-- i also dont see any new pipe events in code.


EDIT i could to "solve" the problem with set address, sort of
what i did was a "cheat" stack and force esp32 host stack to listen IN PID packets. This is required to receive set address ACK, then i could update pipe with new address, and send USB_CTRL_REQ_INIT_GET_CFG_DESC xfer. Like i said, it sort of works, because i can see on LA that device is responding on request on setup device address, but USB host stack is not receiving any pipe events, or just is ignoring such events, also sending cheated set address packet is causing host stack to emit pipe event ERROR

Code: Select all

I (627) : USB host setup properly
I (627) : hcd_port_state_t1: 0
I (627) : Port powered ON
I (627) : hcd_port_state_t2: 1
Waiting for conenction
port callback
E (732) : port event: 1
I (732) : HCD_PORT_EVENT_CONNECTION
Resetting
I (1042) : USB device reseted
I (1042) : HCD_PORT_STATE_ENABLED
Full speed enabled
I (1042) xfer: Get dev desc
E (1042) :      -> Pipe [0] event: 1

I (1047) Pipe: : XFER status: 0, num bytes: 64, actual bytes: 18
I (1054) Ctrl data: 80 06 00 01 00 00 12 00 
I (1059) Actual data: 12 01 00 02 ef 02 01 40 3a 30 02 00 00 01 01 02 
I (1066) Actual data: 03 01 
Length: 18
Descriptor type: 18
USB version: 2.00
Device class: 0xef
Device subclass: 0x02
Device protocol: 0x01
EP0 max packet size: 64
VID: 0x303a
PID: 0x0002
Revision number: 1.00
Manufacturer id: 1
Product id: 2
Serial id: 3
Configurations num: 1
I (1093) : Set address
E (1093) :      -> Pipe [4] event: 3
E (1097) : XFER error: 1
W (1103) : pipe state: 0
W (1106) : pipe event: 3
// hcd_pipe_command(msg.pipe_hdl, HCD_PIPE_CMD_RESET);
W (1110) : pipe state: 0
W (1113) : pipe event: 0
I (1126) xfer 2: USB_CTRL_REQ_INIT_GET_CFG_DESC

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 Mar 20, 2021 3:20 pm

After long battle i passed host setup and set address. It was really tricky, but reading code helps:

Code: Select all

    USB_CTRL_REQ_INIT_SET_ADDR((usb_ctrl_req_t *) data_buffers, addr);
    irps->num_bytes = 0; // tricky part

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 Mar 20, 2021 5:14 pm

Here we can see parsed device (pendrive):

Code: Select all

I (627) : USB host setup properly
I (627) : hcd_port_state_t: 0
I (627) : Port powered ON
I (627) : hcd_port_state_t: 1
Waiting for conenction
E (732) : port event: 1
I (732) : HCD_PORT_EVENT_CONNECTION
Resetting
I (1042) : USB device reset
I (1042) : HCD_PORT_STATE_ENABLED
Full speed enabled
I (1076) Pipe: : XFER status: 0, num bytes: 64, actual bytes: 18
I (1077) Ctrl data: 80 06 00 01 00 00 12 00 
I (1077) Actual data: 12 01 10 02 00 00 00 40 81 07 97 55 00 01 01 02 
I (1084) Actual data: 03 01 

Length: 18
Descriptor type: 18
USB version: 2.10
Device class: 0x00
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 (1112) Pipe: : XFER status: 0, num bytes: 0, actual bytes: 0
I (1128) Pipe: : XFER status: 0, num bytes: 64, actual bytes: 32
I (1128) Ctrl data: 80 06 00 02 00 00 00 01 
I (1130) Actual data: 09 02 20 00 01 01 00 80 70 09 04 00 00 02 08 06 
I (1137) Actual data: 50 00 07 05 81 02 40 00 00 07 05 02 02 40 00 00 

Number of Interfaces: 1
Attributes: 128
Max power: 224

bInterfaceNumber: 0
bAlternateSetting: 0
bNumEndpoints: 2
bInterfaceClass: 8
bInterfaceSubClass: 6
bInterfaceProtocol: 80

bEndpointAddress: 129
bmAttributes: 2
bDescriptorType: 5
wMaxPacketSize: 64
bInterval: 0

bEndpointAddress: 2
bmAttributes: 2
bDescriptorType: 5
wMaxPacketSize: 64
bInterval: 0
I (1179) Pipe: : XFER status: 0, num bytes: 64, actual bytes: 16
I (1186) Ctrl data: 80 06 01 03 00 00 00 01 
I (1191) Actual data: 10 03 53 00 61 00 6e 00 44 00 69 00 73 00 6b 00 

strings: SanDisk
I (1201) Pipe: : XFER status: 0, num bytes: 64, actual bytes: 34
I (1208) Ctrl data: 80 06 02 03 00 00 00 01 
I (1213) Actual data: 22 03 43 00 72 00 75 00 7a 00 65 00 72 00 20 00 
I (1220) Actual data: 47 00 6c 00 69 00 64 00 65 00 20 00 33 00 2e 00 
I (1227) Actual data: 30 00 

strings: Cruzer Glide 3.0
I (1234) Pipe: : XFER status: 0, num bytes: 64, actual bytes: 42
I (1240) Ctrl data: 80 06 03 03 00 00 00 01 
I (1245) Actual data: 2a 03 34 00 43 00 35 00 33 00 30 00 30 00 30 00 
I (1253) Actual data: 30 00 32 00 34 00 30 00 35 00 30 00 37 00 32 00 
I (1260) Actual data: 30 00 37 00 30 00 37 00 33 00 

strings: 4C530000240507207073

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 » Wed Apr 28, 2021 5:18 am

chegewara,

any chance you could post your USB host code? It would be a great great help!

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 Apr 28, 2021 8:56 am

I would be very interested in the code too.

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 » Wed Apr 28, 2021 9:13 am

Is that enough?
https://github.com/chegewara/esp32s2-usb-host-example
https://github.com/chegewara/esp32-s2-usb-host-cdc

Code has been updated to work with current master branch.

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 Apr 28, 2021 2:41 pm

Thank you!

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 » Wed Apr 28, 2021 3:14 pm

Thank you! There is a misprint in the README file of both examples, the device descriptor type is not 18, but 1. 18 is length. I hope it's not caused by issues with structs or else.

So how certain are you that this host interface will be eventually become release version? I'm eager to pick it apart to see how it's done. Anything special to connect the wires? Serial resistors? Be great to have a photo in your repo to show how it's done.

Who is online

Users browsing this forum: No registered users and 80 guests