|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
-
-
- #ifndef USB_HOST_TEENSY36_
- #define USB_HOST_TEENSY36_
-
- #include <stdint.h>
-
-
-
-
-
- class USBHost;
- class USBDriver;
- typedef struct Device_struct Device_t;
- typedef struct Pipe_struct Pipe_t;
- typedef struct Transfer_struct Transfer_t;
-
-
-
-
- typedef union {
- struct {
- union {
- struct {
- uint8_t bmRequestType;
- uint8_t bRequest;
- };
- uint16_t wRequestAndType;
- };
- uint16_t wValue;
- uint16_t wIndex;
- uint16_t wLength;
- };
- struct {
- uint32_t word1;
- uint32_t word2;
- };
- } setup_t;
-
-
- struct Device_struct {
- Pipe_t *control_pipe;
- Device_t *next;
- setup_t setup;
- USBDriver *drivers;
- uint8_t speed;
- uint8_t address;
- uint8_t hub_address;
- uint8_t hub_port;
- uint8_t enum_state;
- uint8_t bDeviceClass;
- uint8_t bDeviceSubClass;
- uint8_t bDeviceProtocol;
- uint8_t bmAttributes;
- uint8_t bMaxPower;
- uint16_t idVendor;
- uint16_t idProduct;
- uint16_t LanguageID;
- };
-
-
-
- struct Pipe_struct {
-
- struct {
- volatile uint32_t horizontal_link;
- volatile uint32_t capabilities[2];
- volatile uint32_t current;
- volatile uint32_t next;
- volatile uint32_t alt_next;
- volatile uint32_t token;
- volatile uint32_t buffer[5];
- } qh;
- Device_t *device;
- uint8_t type;
- uint8_t direction;
- uint8_t unusedbyte[2];
- USBDriver *callback_object;
- void (*callback_function)(const Transfer_t *);
- };
-
-
-
-
-
-
-
-
-
- struct Transfer_struct {
-
- struct {
- volatile uint32_t next;
- volatile uint32_t alt_next;
- volatile uint32_t token;
- volatile uint32_t buffer[5];
- } qtd;
-
- Transfer_t *next_followup;
- Transfer_t *prev_followup;
-
-
-
-
- Pipe_t *pipe;
- void *buffer;
- uint32_t length;
- uint32_t unused[3];
- };
-
-
-
-
-
- class USBHost {
- public:
- static void begin();
- protected:
- static Pipe_t * new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint,
- uint32_t direction, uint32_t max_packet_len);
- static bool new_Control_Transfer(Device_t *dev, setup_t *setup,
- void *buf, USBDriver *driver=NULL);
- static bool new_Data_Transfer(Pipe_t *pipe, void *buffer,
- uint32_t len, USBDriver *driver);
- 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 driver_ready_for_device(USBDriver *driver);
- private:
- static void isr();
- static void claim_drivers(Device_t *dev);
- static bool queue_Transfer(Pipe_t *pipe, Transfer_t *transfer);
- static void init_Device_Pipe_Transfer_memory(void);
- static Device_t * allocate_Device(void);
- static void free_Device(Device_t *q);
- static Pipe_t * allocate_Pipe(void);
- static void free_Pipe(Pipe_t *q);
- static Transfer_t * allocate_Transfer(void);
- static void free_Transfer(Transfer_t *q);
- protected:
- static void print(const Transfer_t *transfer);
- static void print(const Transfer_t *first, const Transfer_t *last);
- static void print_token(uint32_t token);
- static void print(const Pipe_t *pipe);
- static void print_hexbytes(const void *ptr, uint32_t len);
- static void print(const char *s);
- static void print(const char *s, int num);
- static void mk_setup(setup_t &s, uint32_t bmRequestType, uint32_t bRequest,
- uint32_t wValue, uint32_t wIndex, uint32_t wLength) {
- s.word1 = bmRequestType | (bRequest << 8) | (wValue << 16);
- s.word2 = wIndex | (wLength << 16);
- }
- };
-
-
-
-
-
-
-
- class USBDriver : public USBHost {
- protected:
- USBDriver() : next(NULL), device(NULL) {}
-
-
-
-
-
-
-
-
-
- virtual bool claim(Device_t *device, int type, const uint8_t *descriptors) {
- return false;
- }
-
-
-
-
- virtual bool control(const Transfer_t *transfer) {
- return false;
- }
-
-
- virtual void disconnect() {
- }
-
-
-
-
- USBDriver *next;
-
- Device_t *device;
- friend class USBHost;
- public:
-
-
-
-
- };
-
-
-
-
-
-
- class USBHub : public USBDriver {
- public:
- USBHub();
- protected:
- virtual bool claim(Device_t *device, int type, const uint8_t *descriptors);
- virtual bool control(const Transfer_t *transfer);
- void poweron(uint32_t port);
- setup_t setup;
- uint8_t hub_desc[16];
- uint8_t endpoint;
- uint8_t numports;
- uint8_t characteristics;
- uint8_t powertime;
- uint8_t state;
- Pipe_t *changepipe;
- uint32_t changebits;
- uint32_t status;
- };
-
-
-
- #endif
|