felis / USB_Host_Shield_2.0

Revision 2.0 of USB Host Library for Arduino.
https://chome.nerpa.tech
1.8k stars 779 forks source link

Adafruit Feather ESP32 with usb host #458

Open ghhann opened 5 years ago

ghhann commented 5 years ago

I am attempting to get the Adafruit Huzzzah ESP32 to work with the usb minishield. I have already verified that the shield that I am using works correctly with another ESP32 development board I have the following connections: MOSI to pin 18 MISO to pin 19 SCK to pin 5 3v to pin 3v RST to RST GND to GND 5v to BAT According to the pins_arduino.h the SS pin is 33 I defined the INT pin as const int INT = 14; //pin 14

When I run usb_desc I get a OSC did not start message

If I attempt to modify the usbcore.h file to typedef MAX3421e<P33, P14> MAX3421E; // ESP32 boards

I get: exit status 1 'class USB' has no member named 'Init' when I compile the usb_desc example

Any thoughts to get the usb minihost to work with a Feather Huzzzah 32?

Appreciate the assistance....

thaanstad commented 4 years ago

This is a very old thread but I'd just like to document this for anyone else in the future. I was able to achieve communication with the Adafruit ESP32 feather Huzzah by modifying the following in the library:

Change the SS pin in UsbCore.h from 5 to 33:

elif defined(ESP32)

typedef MAX3421e<P33, P17> MAX3421E; // ESP32 boards

else

Change the pin definitions in avrpins.h:

// Pinout for ESP32 dev module -> Modified to adafruit feather Huzzah

MAKE_PIN(P19, 19); // MISO MAKE_PIN(P18, 18); // MOSI MAKE_PIN(P33, 33); // SS MAKE_PIN(P17, 17); // INT MAKE_PIN(P5, 5); //SCK

Change the SPi definition in usbhost.h:

elif defined(ESP32)

typedef SPi< P18, P33, P19, P5 > spi;

I was able to get it to work with your wiring setup described with the difference of reset being tied to digital pin 21 and being toggled at the beginning of the sketch. I also only wired the USB voltage to the VIN of the board as well but I don't think that should make a difference. The modified sketch for USB_desc is below (only difference between example code is in the setup function):

include

include "pgmstrings.h"

// Satisfy the IDE, which needs to see the include statment in the ino too.

include

USB Usb; //USBHub Hub1(&Usb); //USBHub Hub2(&Usb); //USBHub Hub3(&Usb); //USBHub Hub4(&Usb); //USBHub Hub5(&Usb); //USBHub Hub6(&Usb); //USBHub Hub7(&Usb);

void PrintAllAddresses(UsbDevice *pdev) { UsbDeviceAddress adr; adr.devAddress = pdev->address.devAddress; Serial.print("\r\nAddr:"); Serial.print(adr.devAddress, HEX); Serial.print("("); Serial.print(adr.bmHub, HEX); Serial.print("."); Serial.print(adr.bmParent, HEX); Serial.print("."); Serial.print(adr.bmAddress, HEX); Serial.println(")"); }

void PrintAddress(uint8_t addr) { UsbDeviceAddress adr; adr.devAddress = addr; Serial.print("\r\nADDR:\t"); Serial.println(adr.devAddress, HEX); Serial.print("DEV:\t"); Serial.println(adr.bmAddress, HEX); Serial.print("PRNT:\t"); Serial.println(adr.bmParent, HEX); Serial.print("HUB:\t"); Serial.println(adr.bmHub, HEX); }

void setup() { Serial.begin(115200);

if !defined(MIPSEL)

while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection

endif

Serial.println("Start");

pinMode(33, OUTPUT);
digitalWrite(33, HIGH);
pinMode(21, OUTPUT);
digitalWrite(21, LOW);
delay(5);
digitalWrite(21, HIGH);
delay(2000);

if (Usb.Init() == -1)
{
    Serial.println("OSC did not start.");
    for (;;);
}

delay(200);

}

uint8_t getdevdescr(uint8_t addr, uint8_t &num_conf);

void PrintDescriptors(uint8_t addr) { uint8_t rcode = 0; uint8_t num_conf = 0;

rcode = getdevdescr((uint8_t)addr, num_conf);
if (rcode)
{
    printProgStr(Gen_Error_str);
    print_hex(rcode, 8);
}
Serial.print("\r\n");

for (int i = 0; i < num_conf; i++)
{
    rcode = getconfdescr(addr, i);                 // get configuration descriptor
    if (rcode)
    {
        printProgStr(Gen_Error_str);
        print_hex(rcode, 8);
    }
    Serial.println("\r\n");
}

}

void PrintAllDescriptors(UsbDevice *pdev) { Serial.println("\r\n"); print_hex(pdev->address.devAddress, 8); Serial.println("\r\n--"); PrintDescriptors(pdev->address.devAddress); }

void loop() { Usb.Task();

if (Usb.getUsbTaskState() == USB_STATE_RUNNING)
{
    Usb.ForEachUsbDevice(&PrintAllDescriptors);
    Usb.ForEachUsbDevice(&PrintAllAddresses);

    while (1) { // stop

ifdef ESP8266

        yield(); // needed in order to reset the watchdog timer on the ESP8266

endif

    }
}

}

uint8_t getdevdescr(uint8_t addr, uint8_t &num_conf) { USB_DEVICE_DESCRIPTOR buf; uint8_t rcode; rcode = Usb.getDevDescr(addr, 0, 0x12, (uint8_t *)&buf); if (rcode) { return (rcode); } printProgStr(Dev_Header_str); printProgStr(Dev_Length_str); print_hex(buf.bLength, 8); printProgStr(Dev_Type_str); print_hex(buf.bDescriptorType, 8); printProgStr(Dev_Version_str); print_hex(buf.bcdUSB, 16); printProgStr(Dev_Class_str); print_hex(buf.bDeviceClass, 8); printProgStr(Dev_Subclass_str); print_hex(buf.bDeviceSubClass, 8); printProgStr(Dev_Protocol_str); print_hex(buf.bDeviceProtocol, 8); printProgStr(Dev_Pktsize_str); print_hex(buf.bMaxPacketSize0, 8); printProgStr(Dev_Vendor_str); print_hex(buf.idVendor, 16); printProgStr(Dev_Product_str); print_hex(buf.idProduct, 16); printProgStr(Dev_Revision_str); print_hex(buf.bcdDevice, 16); printProgStr(Dev_Mfg_str); print_hex(buf.iManufacturer, 8); printProgStr(Dev_Prod_str); print_hex(buf.iProduct, 8); printProgStr(Dev_Serial_str); print_hex(buf.iSerialNumber, 8); printProgStr(Dev_Nconf_str); print_hex(buf.bNumConfigurations, 8); num_conf = buf.bNumConfigurations; return (0); }

void printhubdescr(uint8_t descrptr, uint8_t addr) { HubDescriptor pHub = (HubDescriptor)descrptr; uint8_t len = ((uint8_t*)descrptr);

printProgStr(PSTR("\r\n\r\nHub Descriptor:\r\n"));
printProgStr(PSTR("bDescLength:\t\t"));
Serial.println(pHub->bDescLength, HEX);

printProgStr(PSTR("bDescriptorType:\t"));
Serial.println(pHub->bDescriptorType, HEX);

printProgStr(PSTR("bNbrPorts:\t\t"));
Serial.println(pHub->bNbrPorts, HEX);

printProgStr(PSTR("LogPwrSwitchMode:\t"));
Serial.println(pHub->LogPwrSwitchMode, BIN);

printProgStr(PSTR("CompoundDevice:\t\t"));
Serial.println(pHub->CompoundDevice, BIN);

printProgStr(PSTR("OverCurrentProtectMode:\t"));
Serial.println(pHub->OverCurrentProtectMode, BIN);

printProgStr(PSTR("TTThinkTime:\t\t"));
Serial.println(pHub->TTThinkTime, BIN);

printProgStr(PSTR("PortIndicatorsSupported:"));
Serial.println(pHub->PortIndicatorsSupported, BIN);

printProgStr(PSTR("Reserved:\t\t"));
Serial.println(pHub->Reserved, HEX);

printProgStr(PSTR("bPwrOn2PwrGood:\t\t"));
Serial.println(pHub->bPwrOn2PwrGood, HEX);

printProgStr(PSTR("bHubContrCurrent:\t"));
Serial.println(pHub->bHubContrCurrent, HEX);

for (uint8_t i = 7; i < len; i++)
    print_hex(descrptr[i], 8);

//for (uint8_t i=1; i<=pHub->bNbrPorts; i++)
//    PrintHubPortStatus(&Usb, addr, i, 1);

}

uint8_t getconfdescr(uint8_t addr, uint8_t conf) { uint8_t buf[BUFSIZE]; uint8_t buf_ptr = buf; uint8_t rcode; uint8_t descr_length; uint8_t descr_type; uint16_t total_length; rcode = Usb.getConfDescr(addr, 0, 4, conf, buf); //get total length LOBYTE(total_length) = buf[2]; HIBYTE(total_length) = buf[3]; if (total_length > 256) { //check if total length is larger than buffer printProgStr(Conf_Trunc_str); total_length = 256; } rcode = Usb.getConfDescr(addr, 0, total_length, conf, buf); //get the whole descriptor while (buf_ptr < buf + total_length) { //parsing descriptors descr_length = (buf_ptr); descr_type = (buf_ptr + 1); switch (descr_type) { case (USB_DESCRIPTOR_CONFIGURATION): printconfdescr(buf_ptr); break; case (USB_DESCRIPTOR_INTERFACE): printintfdescr(buf_ptr); break; case (USB_DESCRIPTOR_ENDPOINT): printepdescr(buf_ptr); break; case 0x29: printhubdescr(buf_ptr, addr); break; default: printunkdescr(buf_ptr); break; }//switch( descr_type buf_ptr = (buf_ptr + descr_length); //advance buffer pointer }//while( buf_ptr <=... return (rcode); } / prints hex numbers with leading zeroes */ // copyright, Peter H Anderson, Baltimore, MD, Nov, '07 // source: http://www.phanderson.com/arduino/arduino_display.html void print_hex(int v, int num_places) { int mask = 0, n, num_nibbles, digit;

for (n = 1; n <= num_places; n++) {
    mask = (mask << 1) | 0x0001;
}
v = v & mask; // truncate v to specified number of places

num_nibbles = num_places / 4;
if ((num_places % 4) != 0) {
    ++num_nibbles;
}
do {
    digit = ((v >> (num_nibbles - 1) * 4)) & 0x0f;
    Serial.print(digit, HEX);
} while (--num_nibbles);

} / function to print configuration descriptor / void printconfdescr(uint8_t descr_ptr) { USB_CONFIGURATION_DESCRIPTOR conf_ptr = (USB_CONFIGURATION_DESCRIPTOR)descr_ptr; printProgStr(Conf_Header_str); printProgStr(Conf_Totlen_str); print_hex(conf_ptr->wTotalLength, 16); printProgStr(Conf_Nint_str); print_hex(conf_ptr->bNumInterfaces, 8); printProgStr(Conf_Value_str); print_hex(conf_ptr->bConfigurationValue, 8); printProgStr(Conf_String_str); print_hex(conf_ptr->iConfiguration, 8); printProgStr(Conf_Attr_str); print_hex(conf_ptr->bmAttributes, 8); printProgStr(Conf_Pwr_str); print_hex(conf_ptr->bMaxPower, 8); return; } / function to print interface descriptor / void printintfdescr(uint8_t descr_ptr) { USB_INTERFACE_DESCRIPTOR intf_ptr = (USB_INTERFACE_DESCRIPTOR)descr_ptr; printProgStr(Int_Header_str); printProgStr(Int_Number_str); print_hex(intf_ptr->bInterfaceNumber, 8); printProgStr(Int_Alt_str); print_hex(intf_ptr->bAlternateSetting, 8); printProgStr(Int_Endpoints_str); print_hex(intf_ptr->bNumEndpoints, 8); printProgStr(Int_Class_str); print_hex(intf_ptr->bInterfaceClass, 8); printProgStr(Int_Subclass_str); print_hex(intf_ptr->bInterfaceSubClass, 8); printProgStr(Int_Protocol_str); print_hex(intf_ptr->bInterfaceProtocol, 8); printProgStr(Int_String_str); print_hex(intf_ptr->iInterface, 8); return; } / function to print endpoint descriptor / void printepdescr(uint8_t descr_ptr) { USB_ENDPOINT_DESCRIPTOR ep_ptr = (USB_ENDPOINT_DESCRIPTOR*)descr_ptr; printProgStr(End_Header_str); printProgStr(End_Address_str); print_hex(ep_ptr->bEndpointAddress, 8); printProgStr(End_Attr_str); print_hex(ep_ptr->bmAttributes, 8); printProgStr(End_Pktsize_str); print_hex(ep_ptr->wMaxPacketSize, 16); printProgStr(End_Interval_str); print_hex(ep_ptr->bInterval, 8);

return;

} /function to print unknown descriptor / void printunkdescr(uint8_t descr_ptr) { uint8_t length = descr_ptr; uint8_t i; printProgStr(Unk_Header_str); printProgStr(Unk_Length_str); print_hex(descr_ptr, 8); printProgStr(Unk_Type_str); print_hex((descr_ptr + 1), 8); printProgStr(Unk_Contents_str); descr_ptr += 2; for (i = 0; i < length; i++) { print_hex(*descr_ptr, 8); descr_ptr++; } }

/ Print a string from Program Memory directly to save RAM / void printProgStr(const char* str) { char c; if (!str) return; while ((c = pgm_read_byte(str++))) Serial.print(c); }