diff --git a/host.h b/host.h new file mode 100644 index 0000000..d1de75f --- /dev/null +++ b/host.h @@ -0,0 +1,113 @@ +#include + + +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; + uint8_t speed; // 0=12, 1=1.5, 2=480 Mbit/sec + uint8_t address; + uint8_t hub_address; + uint8_t hub_port; +}; + +struct Pipe_struct { + // Queue Head (QH), EHCI page 46-50 + struct { // must be aligned to 32 byte boundary + uint32_t horizontal_link; + uint32_t capabilities[2]; + uint32_t current; + uint32_t next; + uint32_t alt_next; + uint32_t token; + uint32_t buffer[5]; + } qh; + Device_t *device; + uint8_t type; // 0=control, 1=isochronous, 2=bulk, 3=interrupt + uint8_t direction; // 0=out, 1=in + //uint8_t endpoint; // 0 to 15 + //uint8_t data01; // next packet DATA0 or DATA1 + uint8_t unusedbyte[2]; + uint32_t unused[2]; +}; + + +struct Transfer_struct { + // Queue Element Transfer Descriptor (qTD), EHCI pg 40-45 + struct { // must be aligned to 32 byte boundary + uint32_t next; + uint32_t alt_next; + uint32_t token; + uint32_t buffer[5]; + } qtd; + Pipe_t *pipe; + void *callback; + void *callback_arg; + uint32_t unused[5]; +}; + +#if 0 +#define EHCI_QH_CAPABILITIES1( \ + nak_count_reload, \ + control_endpoint_flag, \ + max_packet_length, \ + head_of_list, \ + data_toggle_control, \ + speed, \ + endpoint_number, \ + inactivate, \ + address) \ + ( ((nak_count_reload) << 28) | \ + ((control_endpoint_flag) << 27) | \ + ((max_packet_length) << 16) | \ + ((head_of_list) << 15) | \ + ((data_toggle_control) << 14) | \ + ((speed) << 12) | \ + ((endpoint_number) << 8) | \ + ((inactivate) << 7) | \ + ((address) << 0) ) + +#define EHCI_QH_CAPABILITIES2( \ + high_bw_mult, \ + hub_port_number, \ + hub_address, \ + split_completion_mask, \ + interrupt_schedule_mask) \ + ( ((high_bw_mult) << 30) | \ + ((hub_port_number) << 23) | \ + ((hub_address) << 16) | \ + ((split_completion_mask) << 8) | \ + ((interrupt_schedule_mask) << 0) ) +#endif + +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); + diff --git a/k66_usbhost.ino b/k66_usbhost.ino index 682b210..94a4d72 100644 --- a/k66_usbhost.ino +++ b/k66_usbhost.ino @@ -1,20 +1,15 @@ // usb host experiments.... +#include "host.h" -uint32_t periodictable[64] __attribute__ ((aligned(4096), used)); -volatile uint32_t qh[12] __attribute__ ((aligned(64))); -uint32_t qtd_dummy[8] __attribute__ ((aligned(32))); -uint32_t qtd_setup[8] __attribute__ ((aligned(32))); -uint32_t qtd_in[8] __attribute__ ((aligned(32))); -uint32_t qtd_outack[8] __attribute__ ((aligned(32))); -uint32_t setupbuf[2] __attribute__ ((aligned(8))); -uint32_t inbuf[16] __attribute__ ((aligned(64))); +uint32_t periodictable[32] __attribute__ ((aligned(4096), used)); 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; void setup() { @@ -28,8 +23,14 @@ void setup() GPIOE_PDDR |= (1<<6); GPIOE_PSOR = (1<<6); // turn on USB host power while (!Serial) ; // wait - print("USB Host Testing"); - print_mpu(); + Serial.println("USB Host Testing"); + Serial.print("sizeof Device = "); + Serial.println(sizeof(Device_t)); + Serial.print("sizeof Pipe = "); + Serial.println(sizeof(Pipe_t)); + Serial.print("sizeof Transfer = "); + Serial.println(sizeof(Transfer_t)); + MPU_RGDAAC0 |= 0x30000000; MCG_C1 |= MCG_C1_IRCLKEN; // enable MCGIRCLK 32kHz OSC0_CR |= OSC_ERCLKEN; @@ -75,29 +76,9 @@ void setup() } print(" reset waited ", count); - for (int i=0; i < 64; i++) { + for (int i=0; i < 32; i++) { periodictable[i] = 1; } - qh[0] = ((uint32_t)qh) | 2; - qh[1] = 0x0040E000; // addr=0, ep=0 - qh[2] = 0x40000000; - qh[3] = 0; - qh[4] = 1; - qh[5] = 1; - qh[6] = 0x40; - qh[7] = 0; - qh[8] = 0; - qh[9] = 0; - qh[10] = 0; - qh[11] = 0; - qtd_dummy[0] = 1; - qtd_dummy[1] = 1; - qtd_dummy[2] = 0x40; // 0x40 = halted - qtd_dummy[3] = 0; - qtd_dummy[4] = 0; - qtd_dummy[5] = 0; - qtd_dummy[6] = 0; - qtd_dummy[7] = 0; port_state = PORT_STATE_DISCONNECTED; // turn on the USBHS controller @@ -105,11 +86,9 @@ void setup() USBHS_USBINTR = 0; USBHS_PERIODICLISTBASE = (uint32_t)periodictable; USBHS_FRINDEX = 0; - USBHS_ASYNCLISTADDR = (uint32_t)qh; + USBHS_ASYNCLISTADDR = 0; USBHS_USBCMD = USBHS_USBCMD_ITC(0) | USBHS_USBCMD_RS | USBHS_USBCMD_ASP(3) | - USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(0) | // periodic table is 64 pointers - // USBHS_USBCMD_PSE | - USBHS_USBCMD_ASE; + USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(1); // periodic table is 32 pointers //USBHS_PORTSC1 = USBHS_PORTSC_PP; USBHS_PORTSC1 |= USBHS_PORTSC_PP; @@ -131,6 +110,11 @@ void setup() digitalWrite(32, HIGH); // connect device } +void loop() +{ +} + + void pulse(int usec) { // connect oscilloscope to see these pulses.... @@ -157,7 +141,7 @@ void pulse(int usec) // PORT_STATE_RECOVERY 3 // PORT_STATE_ACTIVE 4 -void usbhs_isr(void) // USBHS_ISR_HOST +void usbhs_isr(void) { uint32_t stat = USBHS_USBSTS; USBHS_USBSTS = stat; // clear pending interrupts @@ -202,13 +186,11 @@ void usbhs_isr(void) // USBHS_ISR_HOST // 10 ms reset recover (USB 2.0: TRSTRCY, page 151 & 188) USBHS_GPTIMER0LD = 10000; // microseconds USBHS_GPTIMER0CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN; - } if (portstat & USBHS_PORTSC_FPR) { Serial.println(" force resume"); } - pulse(1); } if (stat & USBHS_USBSTS_TI0) { // timer 0 @@ -222,162 +204,214 @@ void usbhs_isr(void) // USBHS_ISR_HOST port_state = PORT_STATE_ACTIVE; Serial.println(" end recovery"); + // HCSPARAMS TTCTRL page 1671 + rootdev = new_Device((USBHS_PORTSC1 >> 26) & 3, 0, 0); } } } - - - -void port_status() +Device_t * new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port) { - uint32_t n; + Device_t *dev; - Serial.print("Port: "); - n = USBHS_PORTSC1; - if (n & USBHS_PORTSC_PR) { - Serial.print("reset "); + Serial.print("new_Device: "); + switch (speed) { + case 0: Serial.print("12"); break; + case 1: Serial.print("1.5"); break; + case 2: Serial.print("480"); break; + default: Serial.print("??"); } - if (n & USBHS_PORTSC_PP) { - Serial.print("on "); - } else { - Serial.print("off "); + Serial.println(" Mbit/sec"); + dev = allocate_Device(); + if (!dev) return NULL; + dev->speed = speed; + dev->address = 1; // TODO: dynamic assign address + dev->hub_address = hub_addr; + dev->hub_port = hub_port; + dev->control_pipe = new_Pipe(dev, 0, 0, 0, 8); + if (!dev->control_pipe) { + free_Device(dev); + return NULL; } - if (n & USBHS_PORTSC_PHCD) { - Serial.print("phyoff "); + + static uint8_t buffer[8]; + dev->control_pipe->direction = 1; // 1=IN + dev->setup.bmRequestType = 0x80; + dev->setup.bRequest = 0x06; // 6=GET_DESCRIPTOR + dev->setup.wValue = 0x0100; + dev->setup.wIndex = 0x0000; + dev->setup.wLength = 8; + Transfer_t *transfer = new_Transfer(dev->control_pipe, buffer, 8); + if (transfer) queue_Transfer(transfer); + + return dev; +} + +static uint32_t QH_capabilities1(uint32_t nak_count_reload, uint32_t control_endpoint_flag, + uint32_t max_packet_length, uint32_t head_of_list, uint32_t data_toggle_control, + uint32_t speed, uint32_t endpoint_number, uint32_t inactivate, uint32_t address) +{ + return ( (nak_count_reload << 28) | (control_endpoint_flag << 27) | + (max_packet_length << 16) | (head_of_list << 15) | + (data_toggle_control << 14) | (speed << 12) | (endpoint_number << 8) | + (inactivate << 7) | (address << 0) ); +} + +static uint32_t QH_capabilities2(uint32_t high_bw_mult, uint32_t hub_port_number, + uint32_t hub_address, uint32_t split_completion_mask, uint32_t interrupt_schedule_mask) +{ + return ( (high_bw_mult << 30) | (hub_port_number << 23) | (hub_address << 16) | + (split_completion_mask << 8) | (interrupt_schedule_mask << 0) ); +} + + + +Pipe_t * new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint, uint32_t direction, + uint32_t max_packet_len) +{ + Pipe_t *pipe; + uint32_t c=0, dtc=0; + + Serial.println("new_Pipe"); + pipe = allocate_Pipe(); + if (!pipe) return NULL; + pipe->qh.current = 0; + pipe->qh.next = 1; + pipe->qh.alt_next = 1; + pipe->qh.token = 0; + pipe->qh.buffer[0] = 0; + pipe->qh.buffer[1] = 0; + pipe->qh.buffer[2] = 0; + pipe->qh.buffer[3] = 0; + pipe->qh.buffer[4] = 0; + if (pipe->type == 0) { + // control + if (dev->speed < 2) c = 1; + dtc = 1; + } else if (pipe->type == 2) { + // bulk + } else if (pipe->type == 3) { + // interrupt } - if (n & USBHS_PORTSC_PE) { - if (n & USBHS_PORTSC_SUSP) { - Serial.print("suspend "); + pipe->qh.capabilities[0] = QH_capabilities1(15, c, max_packet_len, 0, + dtc, dev->speed, endpoint, 0, dev->address); + pipe->qh.capabilities[1] = QH_capabilities2(1, dev->hub_port, + dev->hub_address, 0, 0); + if (pipe->type == 0 || pipe->type == 2) { + // control or bulk: add to async queue + Pipe_t *list = (Pipe_t *)USBHS_ASYNCLISTADDR; + if (list == NULL) { + pipe->qh.capabilities[0] |= 0x8000; // H bit + pipe->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2; // 2=QH + USBHS_ASYNCLISTADDR = (uint32_t)&(pipe->qh); + USBHS_USBCMD |= USBHS_USBCMD_ASE; // enable async schedule + Serial.println(" first in async list"); } else { - Serial.print("enable "); + // EHCI 1.0: section 4.8.1, page 72 + pipe->qh.horizontal_link = list->qh.horizontal_link; + list->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2; + Serial.println(" added to async list"); } - } else { - Serial.print("disable "); - } - Serial.print("speed="); - switch ((n >> 26) & 3) { - case 0: Serial.print("12 Mbps "); break; - case 1: Serial.print("1.5 Mbps "); break; - case 2: Serial.print("480 Mbps "); break; - default: Serial.print("(undef) "); - } - if (n & USBHS_PORTSC_HSP) { - Serial.print("highspeed "); - } - if (n & USBHS_PORTSC_OCA) { - Serial.print("overcurrent "); - } - if (n & USBHS_PORTSC_CCS) { - Serial.print("connected "); - } else { - Serial.print("not-connected "); - } - // print info about the EHCI status - Serial.print(" run="); - Serial.print(USBHS_USBCMD & 1); // running mode - Serial.print(",halt="); - Serial.print((USBHS_USBSTS >> 12) & 1); // halted mode - Serial.print(",err="); - Serial.print((USBHS_USBSTS >> 4) & 1); // error encountered! - Serial.print(",asyn="); - Serial.print((USBHS_USBSTS >> 15) & 1); // running the async schedule - Serial.print(",per="); - Serial.print((USBHS_USBSTS >> 14) & 1); // running the periodic schedule - Serial.print(",index="); - Serial.print(USBHS_FRINDEX); // periodic index - - Serial.println(); - if (USBHS_USBSTS & 16) { - print_mpu(); - USBHS_USBSTS = 16; // clear error + } else if (pipe->type == 3) { + // interrupt: add to periodic schedule + // TODO: link it into the periodic table } + return pipe; } -void read_descriptor(uint16_t value, uint16_t index, uint32_t len) + +// Fill in the qTD fields (token & data) +// t the Transfer qTD to initialize +// buf data to transfer +// len length of data +// pid type of packet: 0=OUT, 1=IN, 2=SETUP +// 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, + uint32_t pid, uint32_t data01, bool irq) { - uint32_t token; - - if (len > 512) len = 512; - Serial.println("Read Device Descriptor..."); - - qtd_setup[0] = (uint32_t)qtd_in; - qtd_setup[1] = 1; - qtd_setup[2] = 0x00080E80; - qtd_setup[3] = (uint32_t)setupbuf; - - setupbuf[0] = (value << 16) | (0x06 << 8) | 0x80; - setupbuf[1] = (len << 16) | index; - - qtd_in[0] = (uint32_t)qtd_outack; - qtd_in[1] = 1; - qtd_in[2] = 0x80000000 | (len << 16) | 0x0D80; - qtd_in[3] = (uint32_t)inbuf; - - qtd_outack[0] = 1; - qtd_outack[1] = 1; - qtd_outack[2] = 0x80400C80; - qtd_outack[3] = 0; - - // add to QH - - // Save the content of the token field of the first qTD to be added - token = qtd_setup[2]; - - // Change the token of the first qTD so its Halted bit is set as 1 - // and all other bits are zero - qtd_setup[2] = 0x40; - - // copy the content of the first qTD to the dummy qTD - memcpy(qtd_dummy, qtd_setup, 32); - - // Link the first qTD to the last of the qTD of the newly qTD list - qtd_outack[0] = (uint32_t)qtd_setup; - - // Restore the token value to the previous dummy qTD's oken field - qtd_dummy[2] = token; - // qtd_setup becomes the dummy token... so this only works once! - - delay(1); - Serial.println(qtd_dummy[2], HEX); - Serial.println(qtd_in[2], HEX); - Serial.println(qtd_outack[2], HEX); - Serial.println(qtd_setup[2], HEX); - + t->qtd.alt_next = 1; // 1=terminate + if (data01) data01 = 0x80000000; + t->qtd.token = data01 | (len << 16) | (irq ? 0x8000 : 0) | (pid << 8); + uint32_t addr = (uint32_t)buf; + t->qtd.buffer[0] = addr; + addr &= 0xFFFFF000; + t->qtd.buffer[1] = addr + 0x1000; + t->qtd.buffer[2] = addr + 0x2000; + t->qtd.buffer[3] = addr + 0x3000; + t->qtd.buffer[4] = addr + 0x4000; } - -void loop() +// Create a list of Transfers +// +Transfer_t * new_Transfer(Pipe_t *pipe, void *buffer, uint32_t len) { -/* - static unsigned int count=0; - - port_status(); - delay(1); - count++; - if (count == 2) { - Serial.println("Plug in device..."); - digitalWrite(32, HIGH); // connect device + Serial.println("new_Transfer"); + Transfer_t *transfer = allocate_Transfer(); + if (!transfer) return NULL; + transfer->pipe = pipe; + if (pipe->type == 0) { + // control transfer + Transfer_t *data, *status; + uint32_t status_direction; + if (len > 16384) { + free_Transfer(transfer); + return NULL; + } + status = allocate_Transfer(); + if (!status) { + free_Transfer(transfer); + return NULL; + } + if (len > 0) { + data = allocate_Transfer(); + if (!data) { + free_Transfer(transfer); + free_Transfer(status); + return NULL; + } + init_qTD(data, buffer, len, pipe->direction, 1, false); + transfer->qtd.next = (uint32_t)data; + data->qtd.next = (uint32_t)status; + data->pipe = pipe; + status_direction = pipe->direction ^ 1; + } else { + transfer->qtd.next = (uint32_t)status; + status_direction = 1; // always IN, USB 2.0 page 226 + } + init_qTD(transfer, &(pipe->device->setup), 8, 2, 0, false); + init_qTD(status, NULL, 0, status_direction, 1, true); + status->pipe = pipe; + status->qtd.next = 1; + } else { + // bulk, interrupt or isochronous transfer + free_Transfer(transfer); + return NULL; } - if (count == 5) { - Serial.println("Initiate Reset Sequence..."); - USBHS_PORTSC1 |= USBHS_PORTSC_PR; - } - if (count == 15) { - Serial.println("End Reset Sequence..."); - USBHS_PORTSC1 &= ~USBHS_PORTSC_PR; - } - if (count == 22) { - read_descriptor(1, 0, 8); // device descriptor - } - if (count > 500) { - while (1) ; // stop here - } -*/ + return transfer; } + +void queue_Transfer(Transfer_t *transfer) +{ + Serial.println("queue_Transfer"); + Pipe_t *pipe = transfer->pipe; + Transfer_t *last = (Transfer_t *)(pipe->qh.next); + if ((uint32_t)last & 1) { + pipe->qh.next = (uint32_t)transfer; + Serial.println(" first on QH"); + } else { + while ((last->qtd.next & 1) == 0) last = (Transfer_t *)(last->qtd.next); + last->qtd.next = (uint32_t)transfer; + Serial.println(" added to qTD list"); + } +} + + + void print(const char *s) { Serial.println(s); @@ -391,8 +425,42 @@ void print(const char *s, int num) delay(10); } -void print_mpu() + +// Memory allocation... for now, just simplest leaky way to get started + +Device_t * allocate_Device(void) { - Serial.print("MPU: CESR="); - Serial.println(MPU_CESR, HEX); + static Device_t mem[3]; + static size_t count=0; + if (count >= sizeof(mem)/sizeof(Device_t)) return NULL; + return &mem[count++]; } + +void free_Device(Device_t *q) +{ +} + +Pipe_t * allocate_Pipe(void) +{ + static Pipe_t mem[6] __attribute__ ((aligned(64))); + static size_t count=0; + if (count >= sizeof(mem)/sizeof(Pipe_t)) return NULL; + return &mem[count++]; +} + +void free_Pipe(Pipe_t *q) +{ +} + +Transfer_t * allocate_Transfer(void) +{ + static Transfer_t mem[22] __attribute__ ((aligned(64))); + static size_t count=0; + if (count >= sizeof(mem)/sizeof(Transfer_t)) return NULL; + return &mem[count++]; +} + +void free_Transfer(Transfer_t *q) +{ +} +