Parcourir la source

Convert to C++ classes

main
PaulStoffregen il y a 8 ans
Parent
révision
dc87eeaaf6
6 fichiers modifiés avec 125 ajouts et 102 suppressions
  1. +52
    -48
      USBHost.h
  2. +30
    -20
      ehci.cpp
  3. +23
    -16
      enumeration.cpp
  4. +3
    -1
      k66_usbhost.ino
  5. +10
    -10
      memory.cpp
  6. +7
    -7
      print.cpp

+ 52
- 48
USBHost.h Voir le fichier



#include <stdint.h> #include <stdint.h>


/************************************************/
/* Data Structure Definitions */
/************************************************/

class USBHost;
class USBHostDriver;
typedef struct Device_struct Device_t; typedef struct Device_struct Device_t;
typedef struct Pipe_struct Pipe_t; typedef struct Pipe_struct Pipe_t;
typedef struct Transfer_struct Transfer_t; typedef struct Transfer_struct Transfer_t;



// setup_t holds the 8 byte USB SETUP packet data.
// These unions & structs allow convenient access to
// the setup fields.
typedef union { typedef union {
struct { struct {
union { union {
}; };
} setup_t; } setup_t;


// Device_t holds all the information about a USB device
struct Device_struct { struct Device_struct {
Pipe_t *control_pipe; Pipe_t *control_pipe;
Device_t *next; Device_t *next;
uint16_t LanguageID; uint16_t LanguageID;
}; };


// Pipe_t holes all information about each USB endpoint/pipe
// The first half is an EHCI QH structure for the pipe.
struct Pipe_struct { struct Pipe_struct {
// Queue Head (QH), EHCI page 46-50 // Queue Head (QH), EHCI page 46-50
struct { // must be aligned to 32 byte boundary struct { // must be aligned to 32 byte boundary
void (*callback_function)(const Transfer_t *); void (*callback_function)(const Transfer_t *);
}; };



// Transfer_t represents a single transaction on the USB bus.
// The first portion is an EHCI qTD structure. Transfer_t are
// allocated as-needed from a memory pool, loaded with pointers
// to the actual data buffers, linked into a followup list,
// and placed on ECHI Queue Heads. When the ECHI interrupt
// occurs, the followup lists are used to find the Transfer_t
// in memory. Callbacks are made, and then the Transfer_t are
// returned to the memory pool.
struct Transfer_struct { struct Transfer_struct {
// Queue Element Transfer Descriptor (qTD), EHCI pg 40-45 // Queue Element Transfer Descriptor (qTD), EHCI pg 40-45
struct { // must be aligned to 32 byte boundary struct { // must be aligned to 32 byte boundary
volatile uint32_t token; volatile uint32_t token;
volatile uint32_t buffer[5]; volatile uint32_t buffer[5];
} qtd; } qtd;
// linked list of queued, not-yet-completed transfers
// Linked list of queued, not-yet-completed transfers
Transfer_t *next_followup; Transfer_t *next_followup;
Transfer_t *prev_followup; Transfer_t *prev_followup;
// data to be used by callback function
// Data to be used by callback function. When a group
// of Transfer_t are created, these fields and the
// interrupt-on-complete bit in the qTD token are only
// set in the last Transfer_t of the list.
Pipe_t *pipe; Pipe_t *pipe;
void *buffer; void *buffer;
uint32_t length; uint32_t length;
uint32_t unused[3]; uint32_t unused[3];
}; };


void begin();
Pipe_t * new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint, uint32_t direction,
uint32_t max_packet_len);
bool new_Transfer(Pipe_t *pipe, void *buffer, uint32_t len);
bool followup_Transfer(Transfer_t *transfer);
void add_to_async_followup_list(Transfer_t *first, Transfer_t *last);
void remove_from_async_followup_list(Transfer_t *transfer);
void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last);
void remove_from_periodic_followup_list(Transfer_t *transfer);


Device_t * new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port);
void enumeration(const Transfer_t *transfer);
void mk_setup(setup_t &s, uint32_t bmRequestType, uint32_t bRequest,
uint32_t wValue, uint32_t wIndex, uint32_t wLength);
uint32_t assign_addr(void);
void pipe_set_maxlen(Pipe_t *pipe, uint32_t maxlen);
void pipe_set_addr(Pipe_t *pipe, uint32_t addr);
uint32_t pipe_get_addr(Pipe_t *pipe);



void init_Device_Pipe_Transfer_memory(void);
Device_t * allocate_Device(void);
void free_Device(Device_t *q);
Pipe_t * allocate_Pipe(void);
void free_Pipe(Pipe_t *q);
Transfer_t * allocate_Transfer(void);
void free_Transfer(Transfer_t *q);

void print(const Transfer_t *transfer);
void print(const Transfer_t *first, const Transfer_t *last);
void print_token(uint32_t token);
void print(const Pipe_t *pipe);
void print_hexbytes(const void *ptr, uint32_t len);
void print(const char *s);
void print(const char *s, int num);

/************************************************/
/* Main USB EHCI Controller */
/************************************************/


class USBHost { class USBHost {
public: public:
static void begin(); static void begin();
protected: 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_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 void enumeration(const Transfer_t *transfer); static void enumeration(const Transfer_t *transfer);
private:
static void isr(); static void isr();





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);
}; };




/************************************************/
/* USB Device Drivers */
/************************************************/

class USBHostDriver : public USBHost { class USBHostDriver : public USBHost {
public: public:
virtual bool claim_device(Device_t *device) { virtual bool claim_device(Device_t *device) {

+ 30
- 20
ehci.cpp Voir le fichier

#include <Arduino.h> #include <Arduino.h>
#include "USBHost.h" #include "USBHost.h"



uint32_t periodictable[32] __attribute__ ((aligned(4096), used));
uint8_t port_state;
static uint32_t periodictable[32] __attribute__ ((aligned(4096), used));
static uint8_t port_state;
#define PORT_STATE_DISCONNECTED 0 #define PORT_STATE_DISCONNECTED 0
#define PORT_STATE_DEBOUNCE 1 #define PORT_STATE_DEBOUNCE 1
#define PORT_STATE_RESET 2 #define PORT_STATE_RESET 2
#define PORT_STATE_RECOVERY 3 #define PORT_STATE_RECOVERY 3
#define PORT_STATE_ACTIVE 4 #define PORT_STATE_ACTIVE 4
Device_t *rootdev=NULL;
Transfer_t *async_followup_first=NULL;
Transfer_t *async_followup_last=NULL;
Transfer_t *periodic_followup_first=NULL;
Transfer_t *periodic_followup_last=NULL;

void begin()
static Device_t *rootdev=NULL;
static Transfer_t *async_followup_first=NULL;
static Transfer_t *async_followup_last=NULL;
static Transfer_t *periodic_followup_first=NULL;
static Transfer_t *periodic_followup_last=NULL;


static void isr();
static void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len,
uint32_t pid, uint32_t data01, bool irq);
static bool followup_Transfer(Transfer_t *transfer);
static void add_to_async_followup_list(Transfer_t *first, Transfer_t *last);
static void remove_from_async_followup_list(Transfer_t *transfer);
static void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last);
static void remove_from_periodic_followup_list(Transfer_t *transfer);

void USBHost::begin()
{ {
// Teensy 3.6 has USB host power controlled by PTE6 // Teensy 3.6 has USB host power controlled by PTE6
PORTE_PCR6 = PORT_PCR_MUX(1); PORTE_PCR6 = PORT_PCR_MUX(1);
Serial.println((uint32_t)periodictable, HEX); Serial.println((uint32_t)periodictable, HEX);


// enable interrupts, after this point interruts to all the work // enable interrupts, after this point interruts to all the work
attachInterruptVector(IRQ_USBHS, isr);
NVIC_ENABLE_IRQ(IRQ_USBHS); NVIC_ENABLE_IRQ(IRQ_USBHS);
USBHS_USBINTR = USBHS_USBINTR_PCE | USBHS_USBINTR_TIE0; USBHS_USBINTR = USBHS_USBINTR_PCE | USBHS_USBINTR_TIE0;
USBHS_USBINTR |= USBHS_USBINTR_UEE | USBHS_USBINTR_SEE; USBHS_USBINTR |= USBHS_USBINTR_UEE | USBHS_USBINTR_SEE;
// PORT_STATE_ACTIVE 4 // PORT_STATE_ACTIVE 4




void usbhs_isr()
void USBHost::isr()
{ {
uint32_t stat = USBHS_USBSTS; uint32_t stat = USBHS_USBSTS;
USBHS_USBSTS = stat; // clear pending interrupts USBHS_USBSTS = stat; // clear pending interrupts
// Create a new pipe. It's QH is added to the async or periodic schedule, // Create a new pipe. It's QH is added to the async or periodic schedule,
// and a halt qTD is added to the QH, so we can grow the qTD list later. // and a halt qTD is added to the QH, so we can grow the qTD list later.
// //
Pipe_t * new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint, uint32_t direction,
uint32_t max_packet_len)
Pipe_t * USBHost::new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint,
uint32_t direction, uint32_t max_packet_len)
{ {
Pipe_t *pipe; Pipe_t *pipe;
Transfer_t *halt; Transfer_t *halt;
// data01 value of DATA0/DATA1 toggle on 1st packet // data01 value of DATA0/DATA1 toggle on 1st packet
// irq whether to generate an interrupt when transfer complete // irq whether to generate an interrupt when transfer complete
// //
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)
{ {
t->qtd.alt_next = 1; // 1=terminate t->qtd.alt_next = 1; // 1=terminate


// Create a Transfer and queue it // Create a Transfer and queue it
// //
bool new_Transfer(Pipe_t *pipe, void *buffer, uint32_t len)
bool USBHost::new_Transfer(Pipe_t *pipe, void *buffer, uint32_t len)
{ {
Serial.println("new_Transfer"); Serial.println("new_Transfer");
Transfer_t *transfer = allocate_Transfer(); Transfer_t *transfer = allocate_Transfer();
return true; return true;
} }


bool followup_Transfer(Transfer_t *transfer)
static bool followup_Transfer(Transfer_t *transfer)
{ {
Serial.print(" Followup "); Serial.print(" Followup ");
Serial.println((uint32_t)transfer, HEX); Serial.println((uint32_t)transfer, HEX);
return false; return false;
} }


void add_to_async_followup_list(Transfer_t *first, Transfer_t *last)
static void add_to_async_followup_list(Transfer_t *first, Transfer_t *last)
{ {
last->next_followup = NULL; // always add to end of list last->next_followup = NULL; // always add to end of list
if (async_followup_last == NULL) { if (async_followup_last == NULL) {
async_followup_last = last; async_followup_last = last;
} }


void remove_from_async_followup_list(Transfer_t *transfer)
static void remove_from_async_followup_list(Transfer_t *transfer)
{ {
Transfer_t *next = transfer->next_followup; Transfer_t *next = transfer->next_followup;
Transfer_t *prev = transfer->prev_followup; Transfer_t *prev = transfer->prev_followup;
} }
} }


void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last)
static void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last)
{ {
last->next_followup = NULL; // always add to end of list last->next_followup = NULL; // always add to end of list
if (periodic_followup_last == NULL) { if (periodic_followup_last == NULL) {
periodic_followup_last = last; periodic_followup_last = last;
} }


void remove_from_periodic_followup_list(Transfer_t *transfer)
static void remove_from_periodic_followup_list(Transfer_t *transfer)
{ {
Transfer_t *next = transfer->next_followup; Transfer_t *next = transfer->next_followup;
Transfer_t *prev = transfer->prev_followup; Transfer_t *prev = transfer->prev_followup;

+ 23
- 16
enumeration.cpp Voir le fichier

#include "USBHost.h" #include "USBHost.h"




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);
}

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,
uint32_t wValue, uint32_t wIndex, uint32_t wLength);
static uint32_t assign_addr(void);
static void pipe_set_maxlen(Pipe_t *pipe, uint32_t maxlen);
static void pipe_set_addr(Pipe_t *pipe, uint32_t addr);


// Create a new device and begin the enumeration process // Create a new device and begin the enumeration process
// //
Device_t * 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)
{ {
Device_t *dev; Device_t *dev;








void enumeration(const Transfer_t *transfer)
void USBHost::enumeration(const Transfer_t *transfer)
{ {
uint32_t len; uint32_t len;


} }
} }


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
} }


void pipe_set_maxlen(Pipe_t *pipe, uint32_t maxlen)
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);
}

static void pipe_set_maxlen(Pipe_t *pipe, uint32_t maxlen)
{ {
Serial.print("pipe_set_maxlen "); Serial.print("pipe_set_maxlen ");
Serial.println(maxlen); Serial.println(maxlen);
pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0x8000FFFF) | (maxlen << 16); pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0x8000FFFF) | (maxlen << 16);
} }


void pipe_set_addr(Pipe_t *pipe, uint32_t addr)
static void pipe_set_addr(Pipe_t *pipe, uint32_t addr)
{ {
Serial.print("pipe_set_addr "); Serial.print("pipe_set_addr ");
Serial.println(addr); Serial.println(addr);
pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0xFFFFFF80) | addr; pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0xFFFFFF80) | addr;
} }


uint32_t pipe_get_addr(Pipe_t *pipe)
{
return pipe->qh.capabilities[0] & 0xFFFFFF80;
}
//static uint32_t pipe_get_addr(Pipe_t *pipe)
//{
// return pipe->qh.capabilities[0] & 0xFFFFFF80;
//}





+ 3
- 1
k66_usbhost.ino Voir le fichier



#include "USBHost.h" #include "USBHost.h"


USBHost myusb;

void setup() void setup()
{ {
// Test board has a USB data mux (this won't be on final Teensy 3.6) // Test board has a USB data mux (this won't be on final Teensy 3.6)
while (!Serial) ; // wait for Arduino Serial Monitor while (!Serial) ; // wait for Arduino Serial Monitor
Serial.println("USB Host Testing"); Serial.println("USB Host Testing");


begin();
myusb.begin();


delay(25); delay(25);
Serial.println("Plug in device..."); Serial.println("Plug in device...");

+ 10
- 10
memory.cpp Voir le fichier

static Pipe_t memory_Pipe[6] __attribute__ ((aligned(64))); static Pipe_t memory_Pipe[6] __attribute__ ((aligned(64)));
static Transfer_t memory_Transfer[24] __attribute__ ((aligned(64))); static Transfer_t memory_Transfer[24] __attribute__ ((aligned(64)));


Device_t * free_Device_list = NULL;
Pipe_t * free_Pipe_list = NULL;
Transfer_t * free_Transfer_list = NULL;
static Device_t * free_Device_list = NULL;
static Pipe_t * free_Pipe_list = NULL;
static Transfer_t * free_Transfer_list = NULL;


void init_Device_Pipe_Transfer_memory(void)
void USBHost::init_Device_Pipe_Transfer_memory(void)
{ {
Device_t *end_device = memory_Device + sizeof(memory_Device)/sizeof(Device_t); Device_t *end_device = memory_Device + sizeof(memory_Device)/sizeof(Device_t);
for (Device_t *device = memory_Device; device < end_device; device++) { for (Device_t *device = memory_Device; device < end_device; device++) {
} }
} }


Device_t * allocate_Device(void)
Device_t * USBHost::allocate_Device(void)
{ {
Device_t *device = free_Device_list; Device_t *device = free_Device_list;
if (device) free_Device_list = *(Device_t **)device; if (device) free_Device_list = *(Device_t **)device;
return device; return device;
} }


void free_Device(Device_t *device)
void USBHost::free_Device(Device_t *device)
{ {
*(Device_t **)device = free_Device_list; *(Device_t **)device = free_Device_list;
free_Device_list = device; free_Device_list = device;
} }


Pipe_t * allocate_Pipe(void)
Pipe_t * USBHost::allocate_Pipe(void)
{ {
Pipe_t *pipe = free_Pipe_list; Pipe_t *pipe = free_Pipe_list;
if (pipe) free_Pipe_list = *(Pipe_t **)pipe; if (pipe) free_Pipe_list = *(Pipe_t **)pipe;
return pipe; return pipe;
} }


void free_Pipe(Pipe_t *pipe)
void USBHost::free_Pipe(Pipe_t *pipe)
{ {
*(Pipe_t **)pipe = free_Pipe_list; *(Pipe_t **)pipe = free_Pipe_list;
free_Pipe_list = pipe; free_Pipe_list = pipe;
} }


Transfer_t * allocate_Transfer(void)
Transfer_t * USBHost::allocate_Transfer(void)
{ {
Transfer_t *transfer = free_Transfer_list; Transfer_t *transfer = free_Transfer_list;
if (transfer) free_Transfer_list = *(Transfer_t **)transfer; if (transfer) free_Transfer_list = *(Transfer_t **)transfer;
return transfer; return transfer;
} }


void free_Transfer(Transfer_t *transfer)
void USBHost::free_Transfer(Transfer_t *transfer)
{ {
*(Transfer_t **)transfer = free_Transfer_list; *(Transfer_t **)transfer = free_Transfer_list;
free_Transfer_list = transfer; free_Transfer_list = transfer;

+ 7
- 7
print.cpp Voir le fichier

#include "USBHost.h" #include "USBHost.h"




void print(const Transfer_t *transfer)
void USBHost::print(const Transfer_t *transfer)
{ {
if (!((uint32_t)transfer & 0xFFFFFFE0)) return; if (!((uint32_t)transfer & 0xFFFFFFE0)) return;
Serial.print("Transfer @ "); Serial.print("Transfer @ ");
Serial.println(); Serial.println();
} }


void print(const Transfer_t *first, const Transfer_t *last)
void USBHost::print(const Transfer_t *first, const Transfer_t *last)
{ {
Serial.print("Transfer Followup List "); Serial.print("Transfer Followup List ");
Serial.print((uint32_t)first, HEX); Serial.print((uint32_t)first, HEX);
} }
} }


void print_token(uint32_t token)
void USBHost::print_token(uint32_t token)
{ {
switch ((token >> 8) & 3) { switch ((token >> 8) & 3) {
case 0: case 0:
} }
} }


void print(const Pipe_t *pipe)
void USBHost::print(const Pipe_t *pipe)
{ {
if (!((uint32_t)pipe & 0xFFFFFFE0)) return; if (!((uint32_t)pipe & 0xFFFFFFE0)) return;
Serial.print("Pipe "); Serial.print("Pipe ");
} }




void print_hexbytes(const void *ptr, uint32_t len)
void USBHost::print_hexbytes(const void *ptr, uint32_t len)
{ {
if (ptr == NULL || len == 0) return; if (ptr == NULL || len == 0) return;
const uint8_t *p = (const uint8_t *)ptr; const uint8_t *p = (const uint8_t *)ptr;
Serial.println(); Serial.println();
} }


void print(const char *s)
void USBHost::print(const char *s)
{ {
Serial.println(s); Serial.println(s);
delay(10); delay(10);
} }


void 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);

Chargement…
Annuler
Enregistrer