Procházet zdrojové kódy

Convert to C++ classes

main
PaulStoffregen před 7 roky
rodič
revize
dc87eeaaf6
6 změnil soubory, kde provedl 125 přidání a 102 odebrání
  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 Zobrazit soubor

@@ -26,11 +26,19 @@

#include <stdint.h>

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

class USBHost;
class USBHostDriver;
typedef struct Device_struct Device_t;
typedef struct Pipe_struct Pipe_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 {
struct {
union {
@@ -50,7 +58,7 @@ typedef union {
};
} setup_t;

// Device_t holds all the information about a USB device
struct Device_struct {
Pipe_t *control_pipe;
Device_t *next;
@@ -68,6 +76,8 @@ struct Device_struct {
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 {
// Queue Head (QH), EHCI page 46-50
struct { // must be aligned to 32 byte boundary
@@ -87,7 +97,14 @@ struct Pipe_struct {
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 {
// Queue Element Transfer Descriptor (qTD), EHCI pg 40-45
struct { // must be aligned to 32 byte boundary
@@ -96,69 +113,56 @@ struct Transfer_struct {
volatile uint32_t token;
volatile uint32_t buffer[5];
} qtd;
// linked list of queued, not-yet-completed transfers
// Linked list of queued, not-yet-completed transfers
Transfer_t *next_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;
void *buffer;
uint32_t length;
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 {
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_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);
private:
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 {
public:
virtual bool claim_device(Device_t *device) {

+ 30
- 20
ehci.cpp Zobrazit soubor

@@ -24,21 +24,30 @@
#include <Arduino.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_DEBOUNCE 1
#define PORT_STATE_RESET 2
#define PORT_STATE_RECOVERY 3
#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
PORTE_PCR6 = PORT_PCR_MUX(1);
@@ -139,6 +148,7 @@ void begin()
Serial.println((uint32_t)periodictable, HEX);

// enable interrupts, after this point interruts to all the work
attachInterruptVector(IRQ_USBHS, isr);
NVIC_ENABLE_IRQ(IRQ_USBHS);
USBHS_USBINTR = USBHS_USBINTR_PCE | USBHS_USBINTR_TIE0;
USBHS_USBINTR |= USBHS_USBINTR_UEE | USBHS_USBINTR_SEE;
@@ -167,7 +177,7 @@ void begin()
// PORT_STATE_ACTIVE 4


void usbhs_isr()
void USBHost::isr()
{
uint32_t stat = USBHS_USBSTS;
USBHS_USBSTS = stat; // clear pending interrupts
@@ -316,8 +326,8 @@ static uint32_t QH_capabilities2(uint32_t high_bw_mult, uint32_t hub_port_number
// 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.
//
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;
Transfer_t *halt;
@@ -386,7 +396,7 @@ Pipe_t * new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint, uint32_t dire
// data01 value of DATA0/DATA1 toggle on 1st packet
// 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)
{
t->qtd.alt_next = 1; // 1=terminate
@@ -404,7 +414,7 @@ void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len,

// 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");
Transfer_t *transfer = allocate_Transfer();
@@ -500,7 +510,7 @@ bool new_Transfer(Pipe_t *pipe, void *buffer, uint32_t len)
return true;
}

bool followup_Transfer(Transfer_t *transfer)
static bool followup_Transfer(Transfer_t *transfer)
{
Serial.print(" Followup ");
Serial.println((uint32_t)transfer, HEX);
@@ -521,7 +531,7 @@ bool followup_Transfer(Transfer_t *transfer)
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
if (async_followup_last == NULL) {
@@ -534,7 +544,7 @@ void add_to_async_followup_list(Transfer_t *first, Transfer_t *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 *prev = transfer->prev_followup;
@@ -550,7 +560,7 @@ void remove_from_async_followup_list(Transfer_t *transfer)
}
}

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
if (periodic_followup_last == NULL) {
@@ -563,7 +573,7 @@ void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *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 *prev = transfer->prev_followup;

+ 23
- 16
enumeration.cpp Zobrazit soubor

@@ -25,19 +25,19 @@
#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 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
//
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;

@@ -71,7 +71,7 @@ Device_t * new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port)



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

@@ -203,28 +203,35 @@ void enumeration(const Transfer_t *transfer)
}
}

uint32_t assign_addr(void)
static uint32_t assign_addr(void)
{
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.println(maxlen);
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.println(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 Zobrazit soubor

@@ -23,6 +23,8 @@

#include "USBHost.h"

USBHost myusb;

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

begin();
myusb.begin();

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

+ 10
- 10
memory.cpp Zobrazit soubor

@@ -31,11 +31,11 @@ static Device_t memory_Device[3];
static Pipe_t memory_Pipe[6] __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);
for (Device_t *device = memory_Device; device < end_device; device++) {
@@ -51,40 +51,40 @@ void init_Device_Pipe_Transfer_memory(void)
}
}

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

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

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

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

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

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

+ 7
- 7
print.cpp Zobrazit soubor

@@ -25,7 +25,7 @@
#include "USBHost.h"


void print(const Transfer_t *transfer)
void USBHost::print(const Transfer_t *transfer)
{
if (!((uint32_t)transfer & 0xFFFFFFE0)) return;
Serial.print("Transfer @ ");
@@ -44,7 +44,7 @@ void print(const Transfer_t *transfer)
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((uint32_t)first, HEX);
@@ -66,7 +66,7 @@ void print(const Transfer_t *first, const Transfer_t *last)
}
}

void print_token(uint32_t token)
void USBHost::print_token(uint32_t token)
{
switch ((token >> 8) & 3) {
case 0:
@@ -85,7 +85,7 @@ void print_token(uint32_t token)
}
}

void print(const Pipe_t *pipe)
void USBHost::print(const Pipe_t *pipe)
{
if (!((uint32_t)pipe & 0xFFFFFFE0)) return;
Serial.print("Pipe ");
@@ -126,7 +126,7 @@ void print(const Pipe_t *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;
const uint8_t *p = (const uint8_t *)ptr;
@@ -138,13 +138,13 @@ void print_hexbytes(const void *ptr, uint32_t len)
Serial.println();
}

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

void print(const char *s, int num)
void USBHost::print(const char *s, int num)
{
Serial.print(s);
Serial.println(num);

Načítá se…
Zrušit
Uložit