| uint8_t bDeviceClass; | uint8_t bDeviceClass; | ||||
| uint8_t bDeviceSubClass; | uint8_t bDeviceSubClass; | ||||
| uint8_t bDeviceProtocol; | uint8_t bDeviceProtocol; | ||||
| uint8_t bmAttributes; | |||||
| uint8_t bMaxPower; | |||||
| uint16_t idVendor; | uint16_t idVendor; | ||||
| uint16_t idProduct; | uint16_t idProduct; | ||||
| uint16_t LanguageID; | uint16_t LanguageID; | ||||
| static bool new_Transfer(Pipe_t *pipe, void *buffer, uint32_t len); | static bool new_Transfer(Pipe_t *pipe, void *buffer, uint32_t len); | ||||
| static Device_t * new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port); | static Device_t * new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port); | ||||
| static void enumeration(const Transfer_t *transfer); | static void enumeration(const Transfer_t *transfer); | ||||
| static void driver_ready_for_device(USBHostDriver *driver); | |||||
| private: | private: | ||||
| static void isr(); | static void isr(); | ||||
| static void init_Device_Pipe_Transfer_memory(void); | static void init_Device_Pipe_Transfer_memory(void); | ||||
| class USBHostDriver : public USBHost { | class USBHostDriver : public USBHost { | ||||
| public: | public: | ||||
| virtual bool claim_device(Device_t *device) { | |||||
| virtual bool claim_device(Device_t *device, const uint8_t *descriptors) { | |||||
| return false; | return false; | ||||
| } | } | ||||
| virtual bool claim_interface(Device_t *device) { | |||||
| virtual bool claim_interface(Device_t *device, const uint8_t *descriptors) { | |||||
| return false; | return false; | ||||
| } | } | ||||
| virtual void disconnect() { | virtual void disconnect() { | ||||
| }; | }; | ||||
| class USBHub : public USBHostDriver { | class USBHub : public USBHostDriver { | ||||
| public: | |||||
| USBHub(); // { driver_ready_for_device(this); } | |||||
| virtual bool claim_device(Device_t *device, const uint8_t *descriptors); | |||||
| }; | }; | ||||
| static Transfer_t *periodic_followup_last=NULL; | static Transfer_t *periodic_followup_last=NULL; | ||||
| static void isr(); | |||||
| static void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len, | static void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len, | ||||
| uint32_t pid, uint32_t data01, bool irq); | uint32_t pid, uint32_t data01, bool irq); | ||||
| static bool followup_Transfer(Transfer_t *transfer); | static bool followup_Transfer(Transfer_t *transfer); |
| #include "USBHost.h" | #include "USBHost.h" | ||||
| static USBHostDriver *available_drivers = NULL; | |||||
| static uint8_t enumbuf[256] __attribute__ ((aligned(16))); | static uint8_t enumbuf[256] __attribute__ ((aligned(16))); | ||||
| static void mk_setup(setup_t &s, uint32_t bmRequestType, uint32_t bRequest, | static void mk_setup(setup_t &s, uint32_t bmRequestType, uint32_t bRequest, | ||||
| uint32_t wValue, uint32_t wIndex, uint32_t wLength); | uint32_t wValue, uint32_t wIndex, uint32_t wLength); | ||||
| static void claim_drivers(Device_t *dev); | |||||
| static uint32_t assign_addr(void); | static uint32_t assign_addr(void); | ||||
| static void pipe_set_maxlen(Pipe_t *pipe, uint32_t maxlen); | static void pipe_set_maxlen(Pipe_t *pipe, uint32_t maxlen); | ||||
| static void pipe_set_addr(Pipe_t *pipe, uint32_t addr); | static void pipe_set_addr(Pipe_t *pipe, uint32_t addr); | ||||
| void USBHost::driver_ready_for_device(USBHostDriver *driver) | |||||
| { | |||||
| driver->next = NULL; | |||||
| if (available_drivers == NULL) { | |||||
| available_drivers = driver; | |||||
| } else { | |||||
| // append to end of list | |||||
| USBHostDriver *last = available_drivers; | |||||
| while (last->next) last = last->next; | |||||
| last->next = driver; | |||||
| } | |||||
| } | |||||
| // Create a new device and begin the enumeration process | // Create a new device and begin the enumeration process | ||||
| // | // | ||||
| Device_t * USBHost::new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port) | Device_t * USBHost::new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port) | ||||
| } | } | ||||
| dev->control_pipe->callback_function = &enumeration; | dev->control_pipe->callback_function = &enumeration; | ||||
| dev->control_pipe->direction = 1; // 1=IN | dev->control_pipe->direction = 1; // 1=IN | ||||
| // TODO: exclusive access to enumeration process | |||||
| // any new devices detected while enumerating would | |||||
| // go onto a waiting list | |||||
| mk_setup(dev->setup, 0x80, 6, 0x0100, 0, 8); // 6=GET_DESCRIPTOR | mk_setup(dev->setup, 0x80, 6, 0x0100, 0, 8); // 6=GET_DESCRIPTOR | ||||
| new_Transfer(dev->control_pipe, enumbuf, 8); | new_Transfer(dev->control_pipe, enumbuf, 8); | ||||
| dev->enum_state = 14; | dev->enum_state = 14; | ||||
| return; | return; | ||||
| case 14: // device is now configured | case 14: // device is now configured | ||||
| // TODO: initialize drivers?? | |||||
| claim_drivers(dev); | |||||
| dev->enum_state = 15; | dev->enum_state = 15; | ||||
| // TODO: unlock exclusive access to enumeration process | |||||
| // if any detected devices are waiting, start the first | |||||
| return; | return; | ||||
| case 15: // control transfers for other stuff?? | case 15: // control transfers for other stuff?? | ||||
| default: | default: | ||||
| } | } | ||||
| } | } | ||||
| static void claim_drivers(Device_t *dev) | |||||
| { | |||||
| USBHostDriver *driver, *prev=NULL; | |||||
| // first check if any driver wishes to claim the entire device | |||||
| for (driver=available_drivers; driver != NULL; driver = driver->next) { | |||||
| if (driver->claim_device(dev, enumbuf + 9)) { | |||||
| if (prev) { | |||||
| prev->next = driver->next; | |||||
| } else { | |||||
| available_drivers = driver->next; | |||||
| } | |||||
| driver->next = NULL; | |||||
| return; | |||||
| } | |||||
| prev = driver; | |||||
| } | |||||
| // TODO: parse interfaces from config descriptor | |||||
| // try claim_interface on drivers | |||||
| } | |||||
| static uint32_t assign_addr(void) | static uint32_t assign_addr(void) | ||||
| { | { | ||||
| return 29; // TODO: when multiple devices, assign a unique address | return 29; // TODO: when multiple devices, assign a unique address |
| /* USB EHCI Host for Teensy 3.6 | |||||
| * Copyright 2017 Paul Stoffregen (paul@pjrc.com) | |||||
| * | |||||
| * Permission is hereby granted, free of charge, to any person obtaining a | |||||
| * copy of this software and associated documentation files (the | |||||
| * "Software"), to deal in the Software without restriction, including | |||||
| * without limitation the rights to use, copy, modify, merge, publish, | |||||
| * distribute, sublicense, and/or sell copies of the Software, and to | |||||
| * permit persons to whom the Software is furnished to do so, subject to | |||||
| * the following conditions: | |||||
| * | |||||
| * The above copyright notice and this permission notice shall be included | |||||
| * in all copies or substantial portions of the Software. | |||||
| * | |||||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |||||
| * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |||||
| * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |||||
| * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |||||
| * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |||||
| * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |||||
| * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |||||
| */ | |||||
| #include <Arduino.h> | |||||
| #include "USBHost.h" | |||||
| USBHub::USBHub() | |||||
| { | |||||
| // TODO: free Device_t, Pipe_t & Transfer_t we will need | |||||
| driver_ready_for_device(this); | |||||
| } | |||||
| bool USBHub::claim_device(Device_t *dev, const uint8_t *descriptors) | |||||
| { | |||||
| Serial.print("USBHub claim_device this="); | |||||
| Serial.println((uint32_t)this, HEX); | |||||
| if (dev->bDeviceClass != 9 || dev->bDeviceSubClass != 0) return false; | |||||
| // bDeviceProtocol = 0 is full speed | |||||
| // bDeviceProtocol = 1 is high speed single TT | |||||
| // bDeviceProtocol = 2 is high speed multiple TT | |||||
| Serial.print("bDeviceClass = "); | |||||
| Serial.println(dev->bDeviceClass); | |||||
| Serial.print("bDeviceSubClass = "); | |||||
| Serial.println(dev->bDeviceSubClass); | |||||
| Serial.print("bDeviceProtocol = "); | |||||
| Serial.println(dev->bDeviceProtocol); | |||||
| return true; | |||||
| } | |||||
| #include "USBHost.h" | #include "USBHost.h" | ||||
| USBHost myusb; | USBHost myusb; | ||||
| USBHub hub1; | |||||
| USBHub hub2; | |||||
| USBHub hub3; | |||||
| void setup() | void setup() | ||||
| { | { |
| void USBHost::print(const char *s) | void USBHost::print(const char *s) | ||||
| { | { | ||||
| Serial.println(s); | Serial.println(s); | ||||
| delay(10); | |||||
| } | } | ||||
| void USBHost::print(const char *s, int num) | void USBHost::print(const char *s, int num) | ||||
| { | { | ||||
| Serial.print(s); | Serial.print(s); | ||||
| Serial.println(num); | Serial.println(num); | ||||
| delay(10); | |||||
| } | } | ||||