|
-
-
- #include <Arduino.h>
- #include "USBHost_t36.h"
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #if defined(USBHS_PERIODIC_LIST_SIZE)
- #define PERIODIC_LIST_SIZE (USBHS_PERIODIC_LIST_SIZE)
- #else
- #define PERIODIC_LIST_SIZE 32
- #endif
-
-
- static uint32_t periodictable[PERIODIC_LIST_SIZE] __attribute__ ((aligned(4096), used));
- static uint8_t uframe_bandwidth[PERIODIC_LIST_SIZE*8];
-
-
- 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
-
-
- 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 USBDriverTimer *active_timers=NULL;
-
-
- static void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len,
- uint32_t pid, uint32_t data01, bool irq);
- 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);
-
- #define print USBHost::print_
- #define println USBHost::println_
-
- void USBHost::begin()
- {
-
- PORTE_PCR6 = PORT_PCR_MUX(1);
- GPIOE_PDDR |= (1<<6);
- GPIOE_PSOR = (1<<6);
- delay(10);
- println("sizeof Device = ", sizeof(Device_t));
- println("sizeof Pipe = ", sizeof(Pipe_t));
- println("sizeof Transfer = ", sizeof(Transfer_t));
- if ((sizeof(Pipe_t) & 0x1F) || (sizeof(Transfer_t) & 0x1F)) {
- println("ERROR: Pipe_t & Transfer_t must be multiples of 32 bytes!");
- while (1) ;
- }
-
-
- MPU_RGDAAC0 |= 0x30000000;
-
-
-
- MCG_C1 |= MCG_C1_IRCLKEN;
- OSC0_CR |= OSC_ERCLKEN;
- SIM_SOPT2 |= SIM_SOPT2_USBREGEN;
- SIM_SOPT2 &= ~SIM_SOPT2_USBSLSRC;
- println("power up USBHS PHY");
- SIM_USBPHYCTL |= SIM_USBPHYCTL_USBDISILIM;
-
- SIM_SCGC3 |= SIM_SCGC3_USBHSDCD | SIM_SCGC3_USBHSPHY | SIM_SCGC3_USBHS;
- USBHSDCD_CLOCK = 33 << 2;
-
-
- USBPHY_CTRL_CLR = (USBPHY_CTRL_SFTRST | USBPHY_CTRL_CLKGATE);
- USBPHY_CTRL_SET = USBPHY_CTRL_ENUTMILEVEL2 | USBPHY_CTRL_ENUTMILEVEL3;
-
- USBPHY_TRIM_OVERRIDE_EN_SET = 1;
- USBPHY_PLL_SIC = USBPHY_PLL_SIC_PLL_POWER | USBPHY_PLL_SIC_PLL_ENABLE |
- USBPHY_PLL_SIC_PLL_DIV_SEL(1) | USBPHY_PLL_SIC_PLL_EN_USB_CLKS;
-
- int count=0;
- while ((USBPHY_PLL_SIC & USBPHY_PLL_SIC_PLL_LOCK) == 0) {
- count++;
- }
-
-
-
- USBPHY_PWD = 0;
- delay(10);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- USBHS_USBCMD |= USBHS_USBCMD_RST;
-
- while (USBHS_USBCMD & USBHS_USBCMD_RST) {
-
- }
-
-
- init_Device_Pipe_Transfer_memory();
- for (int i=0; i < PERIODIC_LIST_SIZE; i++) {
- periodictable[i] = 1;
- }
- memset(uframe_bandwidth, 0, sizeof(uframe_bandwidth));
- port_state = PORT_STATE_DISCONNECTED;
-
- USBHS_USB_SBUSCFG = 1;
-
-
-
- USBHS_USBMODE = USBHS_USBMODE_CM(3);
- USBHS_USBINTR = 0;
- USBHS_PERIODICLISTBASE = (uint32_t)periodictable;
- USBHS_FRINDEX = 0;
- USBHS_ASYNCLISTADDR = 0;
- USBHS_USBCMD = USBHS_USBCMD_ITC(8) | USBHS_USBCMD_RS |
- USBHS_USBCMD_ASP(3) | USBHS_USBCMD_ASPE | USBHS_USBCMD_PSE |
- #if PERIODIC_LIST_SIZE == 8
- USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(3);
- #elif PERIODIC_LIST_SIZE == 16
- USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(2);
- #elif PERIODIC_LIST_SIZE == 32
- USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(1);
- #elif PERIODIC_LIST_SIZE == 64
- USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(0);
- #elif PERIODIC_LIST_SIZE == 128
- USBHS_USBCMD_FS(3);
- #elif PERIODIC_LIST_SIZE == 256
- USBHS_USBCMD_FS(2);
- #elif PERIODIC_LIST_SIZE == 512
- USBHS_USBCMD_FS(1);
- #elif PERIODIC_LIST_SIZE == 1024
- USBHS_USBCMD_FS(0);
- #else
- #error "Unsupported PERIODIC_LIST_SIZE"
- #endif
-
-
-
- USBHS_PORTSC1 |= USBHS_PORTSC_PP;
-
-
-
-
-
-
-
-
- attachInterruptVector(IRQ_USBHS, isr);
- NVIC_ENABLE_IRQ(IRQ_USBHS);
- USBHS_USBINTR = USBHS_USBINTR_PCE | USBHS_USBINTR_TIE0 | USBHS_USBINTR_TIE1;
- USBHS_USBINTR |= USBHS_USBINTR_UEE | USBHS_USBINTR_SEE;
- USBHS_USBINTR |= USBHS_USBINTR_UPIE | USBHS_USBINTR_UAIE;
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- void USBHost::isr()
- {
- uint32_t stat = USBHS_USBSTS;
- USBHS_USBSTS = stat;
-
- #if 0
- println();
- println("ISR: ", stat, HEX);
-
- if (stat & USBHS_USBSTS_UEI) println(" USB Error");
- if (stat & USBHS_USBSTS_PCI) println(" Port Change");
-
- if (stat & USBHS_USBSTS_SEI) println(" System Error");
-
- if (stat & USBHS_USBSTS_URI) println(" Reset Recv");
-
- if (stat & USBHS_USBSTS_SLI) println(" Suspend");
- if (stat & USBHS_USBSTS_HCH) println(" Host Halted");
-
-
-
- if (stat & USBHS_USBSTS_NAKI) println(" NAK");
- if (stat & USBHS_USBSTS_UAI) println(" USB Async");
- if (stat & USBHS_USBSTS_UPI) println(" USB Periodic");
- if (stat & USBHS_USBSTS_TI0) println(" Timer0");
- if (stat & USBHS_USBSTS_TI1) println(" Timer1");
- #endif
-
- if (stat & USBHS_USBSTS_UAI) {
-
-
- Transfer_t *p = async_followup_first;
- while (p) {
- if (followup_Transfer(p)) {
-
- Transfer_t *next = p->next_followup;
- remove_from_async_followup_list(p);
- free_Transfer(p);
- p = next;
- } else {
-
- p = p->next_followup;
- }
- }
-
- }
- if (stat & USBHS_USBSTS_UPI) {
-
- Transfer_t *p = periodic_followup_first;
- while (p) {
- if (followup_Transfer(p)) {
-
- Transfer_t *next = p->next_followup;
- remove_from_periodic_followup_list(p);
- free_Transfer(p);
- p = next;
- } else {
-
- p = p->next_followup;
- }
- }
- }
- if (stat & USBHS_USBSTS_UEI) {
- followup_Error();
- }
-
- if (stat & USBHS_USBSTS_PCI) {
- const uint32_t portstat = USBHS_PORTSC1;
- println("port change: ", portstat, HEX);
- USBHS_PORTSC1 = portstat | (USBHS_PORTSC_OCC|USBHS_PORTSC_PEC|USBHS_PORTSC_CSC);
- if (portstat & USBHS_PORTSC_OCC) {
- println(" overcurrent change");
- }
- if (portstat & USBHS_PORTSC_CSC) {
- if (portstat & USBHS_PORTSC_CCS) {
- println(" connect");
- if (port_state == PORT_STATE_DISCONNECTED
- || port_state == PORT_STATE_DEBOUNCE) {
-
- port_state = PORT_STATE_DEBOUNCE;
- USBHS_GPTIMER0LD = 100000;
- USBHS_GPTIMER0CTL =
- USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
- stat &= ~USBHS_USBSTS_TI0;
- }
- } else {
- println(" disconnect");
- port_state = PORT_STATE_DISCONNECTED;
- USBPHY_CTRL_CLR = USBPHY_CTRL_ENHOSTDISCONDETECT;
- disconnect_Device(rootdev);
- rootdev = NULL;
- }
- }
- if (portstat & USBHS_PORTSC_PEC) {
-
- println(" disable");
- } else if (port_state == PORT_STATE_RESET && portstat & USBHS_PORTSC_PE) {
- println(" port enabled");
- port_state = PORT_STATE_RECOVERY;
-
- USBHS_GPTIMER0LD = 10000;
- USBHS_GPTIMER0CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
- if (USBHS_PORTSC1 & USBHS_PORTSC_HSP) {
-
- USBPHY_CTRL_SET = USBPHY_CTRL_ENHOSTDISCONDETECT;
- }
- }
- if (portstat & USBHS_PORTSC_FPR) {
- println(" force resume");
-
- }
- }
- if (stat & USBHS_USBSTS_TI0) {
-
- if (port_state == PORT_STATE_DEBOUNCE) {
- port_state = PORT_STATE_RESET;
-
-
-
-
-
- USBHS_PORTSC1 |= USBHS_PORTSC_PR;
- println(" begin reset");
- } else if (port_state == PORT_STATE_RECOVERY) {
- port_state = PORT_STATE_ACTIVE;
- println(" end recovery");
-
- uint32_t speed = (USBHS_PORTSC1 >> 26) & 3;
- rootdev = new_Device(speed, 0, 0);
- }
- }
- if (stat & USBHS_USBSTS_TI1) {
-
- USBDriverTimer *timer = active_timers;
- if (timer) {
- USBDriverTimer *next = timer->next;
- active_timers = next;
- if (next) {
-
- next->prev = NULL;
- USBHS_GPTIMER1LD = next->usec - 1;
- USBHS_GPTIMER1CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
- }
-
- timer->driver->timer_event(timer);
- }
- }
- }
-
- void USBDriverTimer::start(uint32_t microseconds)
- {
- #if 0
- USBHost::print_("start_timer, us = ");
- USBHost::print_(microseconds);
- USBHost::print_(", driver = ");
- USBHost::print_((uint32_t)driver, HEX);
- USBHost::print_(", this = ");
- USBHost::println_((uint32_t)this, HEX);
- #endif
- if (!driver) return;
- if (microseconds < 100) return;
- started_micros = micros();
- if (active_timers == NULL) {
-
- usec = microseconds;
- next = NULL;
- prev = NULL;
- active_timers = this;
- USBHS_GPTIMER1LD = microseconds - 1;
- USBHS_GPTIMER1CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
- return;
- }
- uint32_t remain = USBHS_GPTIMER1CTL & 0xFFFFFF;
-
-
- if (microseconds < remain) {
-
- __disable_irq();
- USBHS_GPTIMER1CTL = 0;
- USBHS_USBSTS = USBHS_USBSTS_TI1;
- usec = microseconds;
- next = active_timers;
- prev = NULL;
- active_timers->usec = remain - microseconds;
- active_timers->prev = this;
- active_timers = this;
- USBHS_GPTIMER1LD = microseconds - 1;
- USBHS_GPTIMER1CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
- __enable_irq();
- return;
- }
-
- microseconds -= remain;
- USBDriverTimer *list = active_timers;
- while (list->next) {
- list = list->next;
- if (microseconds < list->usec) {
-
- list->usec -= microseconds;
- usec = microseconds;
- next = list;
- prev = list->prev;
- list->prev = this;
- prev->next = this;
- return;
- }
- microseconds -= list->usec;
- }
-
- usec = microseconds;
- next = NULL;
- prev = list;
- list->next = this;
- }
-
- void USBDriverTimer::stop()
- {
- __disable_irq();
- if (active_timers) {
- if (active_timers == this) {
- USBHS_GPTIMER1CTL = 0;
- if (next) {
- uint32_t usec_til_next = USBHS_GPTIMER1CTL & 0xFFFFFF;
- usec_til_next += next->usec;
- next->usec = usec_til_next;
- USBHS_GPTIMER1LD = usec_til_next;
- USBHS_GPTIMER1CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
- next->prev = NULL;
- active_timers = next;
- } else {
- active_timers = NULL;
- }
- } else {
- for (USBDriverTimer *t = active_timers->next; t; t = t->next) {
- if (t == this) {
- t->prev->next = t->next;
- if (t->next) {
- t->next->usec += t->usec;
- t->next->prev = t->prev;
- }
- break;
- }
- }
- }
- }
- __enable_irq();
- }
-
-
- 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 * USBHost::new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint,
- uint32_t direction, uint32_t maxlen, uint32_t interval)
- {
- Pipe_t *pipe;
- Transfer_t *halt;
- uint32_t c=0, dtc=0;
-
- println("new_Pipe");
- pipe = allocate_Pipe();
- if (!pipe) return NULL;
- halt = allocate_Transfer();
- if (!halt) {
- free_Pipe(pipe);
- return NULL;
- }
- memset(pipe, 0, sizeof(Pipe_t));
- memset(halt, 0, sizeof(Transfer_t));
- halt->qtd.next = 1;
- halt->qtd.token = 0x40;
- pipe->device = dev;
- pipe->qh.next = (uint32_t)halt;
- pipe->qh.alt_next = 1;
- pipe->direction = direction;
- pipe->type = type;
- if (type == 3) {
-
- if (!allocate_interrupt_pipe_bandwidth(pipe, maxlen, interval)) {
- free_Transfer(halt);
- free_Pipe(pipe);
- return NULL;
- }
- }
- if (endpoint > 0) {
-
- Pipe_t *p = dev->data_pipes;
- if (p == NULL) {
- dev->data_pipes = pipe;
- } else {
- while (p->next) p = p->next;
- p->next = pipe;
- }
- }
- if (type == 0) {
-
- if (dev->speed < 2) c = 1;
- dtc = 1;
- } else if (type == 2) {
-
- } else if (type == 3) {
-
-
- }
- pipe->qh.capabilities[0] = QH_capabilities1(15, c, maxlen, 0,
- dtc, dev->speed, endpoint, 0, dev->address);
- pipe->qh.capabilities[1] = QH_capabilities2(1, dev->hub_port,
- dev->hub_address, pipe->complete_mask, pipe->start_mask);
-
- if (type == 0 || type == 2) {
-
- Pipe_t *list = (Pipe_t *)USBHS_ASYNCLISTADDR;
- if (list == NULL) {
- pipe->qh.capabilities[0] |= 0x8000;
- pipe->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2;
- USBHS_ASYNCLISTADDR = (uint32_t)&(pipe->qh);
- USBHS_USBCMD |= USBHS_USBCMD_ASE;
-
- } else {
-
- pipe->qh.horizontal_link = list->qh.horizontal_link;
- list->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2;
-
- }
- } else if (type == 3) {
-
- add_qh_to_periodic_schedule(pipe);
- }
- return pipe;
- }
-
-
-
-
-
-
-
-
-
-
-
- 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;
- if (data01) data01 = 0x80000000;
- t->qtd.token = data01 | (len << 16) | (irq ? 0x8000 : 0) | (pid << 8) | 0x80;
- 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;
- }
-
-
-
-
-
- bool USBHost::queue_Control_Transfer(Device_t *dev, setup_t *setup, void *buf, USBDriver *driver)
- {
- Transfer_t *transfer, *data, *status;
- uint32_t status_direction;
-
-
- if (setup->wLength > 16384) return false;
- transfer = allocate_Transfer();
- if (!transfer) {
- println(" error allocating setup transfer");
- return false;
- }
- status = allocate_Transfer();
- if (!status) {
- println(" error allocating status transfer");
- free_Transfer(transfer);
- return false;
- }
- if (setup->wLength > 0) {
- data = allocate_Transfer();
- if (!data) {
- println(" error allocating data transfer");
- free_Transfer(transfer);
- free_Transfer(status);
- return false;
- }
- uint32_t pid = (setup->bmRequestType & 0x80) ? 1 : 0;
- init_qTD(data, buf, setup->wLength, pid, 1, false);
- transfer->qtd.next = (uint32_t)data;
- data->qtd.next = (uint32_t)status;
- status_direction = pid ^ 1;
- } else {
- transfer->qtd.next = (uint32_t)status;
- status_direction = 1;
- }
-
- init_qTD(transfer, setup, 8, 2, 0, false);
- init_qTD(status, NULL, 0, status_direction, 1, true);
- status->pipe = dev->control_pipe;
- status->buffer = buf;
- status->length = setup->wLength;
- status->setup.word1 = setup->word1;
- status->setup.word2 = setup->word2;
- status->driver = driver;
- status->qtd.next = 1;
- return queue_Transfer(dev->control_pipe, transfer);
- }
-
-
-
-
- bool USBHost::queue_Data_Transfer(Pipe_t *pipe, void *buffer, uint32_t len, USBDriver *driver)
- {
- Transfer_t *transfer, *data, *next;
- uint8_t *p = (uint8_t *)buffer;
- uint32_t count;
- bool last = false;
-
-
-
-
-
- transfer = allocate_Transfer();
- if (!transfer) return false;
- data = transfer;
- for (count=(len >> 14); count; count--) {
- next = allocate_Transfer();
- if (!next) {
-
- while (1) {
- next = (Transfer_t *)transfer->qtd.next;
- free_Transfer(transfer);
- if (transfer == data) break;
- transfer = next;
- }
- return false;
- }
- data->qtd.next = (uint32_t)next;
- data = next;
- }
-
- data->qtd.next = 1;
- data->pipe = pipe;
- data->buffer = buffer;
- data->length = len;
- data->setup.word1 = 0;
- data->setup.word2 = 0;
- data->driver = driver;
-
- data = transfer;
- while (1) {
- uint32_t count = len;
- if (count > 16384) {
- count = 16384;
- } else {
- last = true;
- }
- init_qTD(data, p, count, pipe->direction, 0, last);
- if (last) break;
- p += count;
- len -= count;
- data = (Transfer_t *)(data->qtd.next);
- }
- return queue_Transfer(pipe, transfer);
- }
-
-
- bool USBHost::queue_Transfer(Pipe_t *pipe, Transfer_t *transfer)
- {
-
- Transfer_t *halt = (Transfer_t *)(pipe->qh.next);
- while (!(halt->qtd.token & 0x40)) halt = (Transfer_t *)(halt->qtd.next);
-
- uint32_t token = transfer->qtd.token;
-
- transfer->qtd.token = 0x40;
-
- halt->qtd.next = transfer->qtd.next;
- halt->qtd.alt_next = transfer->qtd.alt_next;
- halt->qtd.buffer[0] = transfer->qtd.buffer[0];
- halt->qtd.buffer[1] = transfer->qtd.buffer[1];
- halt->qtd.buffer[2] = transfer->qtd.buffer[2];
- halt->qtd.buffer[3] = transfer->qtd.buffer[3];
- halt->qtd.buffer[4] = transfer->qtd.buffer[4];
- halt->pipe = pipe;
- halt->buffer = transfer->buffer;
- halt->length = transfer->length;
- halt->setup = transfer->setup;
- halt->driver = transfer->driver;
-
- Transfer_t *last = halt;
- while ((uint32_t)(last->qtd.next) != 1) last = (Transfer_t *)(last->qtd.next);
-
- last->qtd.next = (uint32_t)transfer;
- transfer->qtd.next = 1;
-
- Transfer_t *prev = NULL;
- Transfer_t *p = halt;
- while (p->qtd.next != (uint32_t)transfer) {
- Transfer_t *next = (Transfer_t *)p->qtd.next;
- p->prev_followup = prev;
- p->next_followup = next;
- prev = p;
- p = next;
- }
- p->prev_followup = prev;
- p->next_followup = NULL;
-
-
- if (pipe->type == 0 || pipe->type == 2) {
-
- add_to_async_followup_list(halt, p);
- } else {
-
- add_to_periodic_followup_list(halt, p);
- }
-
- halt->qtd.token = token;
- return true;
- }
-
- bool USBHost::followup_Transfer(Transfer_t *transfer)
- {
-
-
-
- if (!(transfer->qtd.token & 0x80)) {
-
- if (transfer->qtd.token & 0x8000) {
-
- if (transfer->pipe->callback_function) {
-
- (*(transfer->pipe->callback_function))(transfer);
- }
- }
-
-
- return true;
- }
- return false;
- }
-
- void USBHost::followup_Error(void)
- {
- println("ERROR Followup");
- Transfer_t *p = async_followup_first;
- while (p) {
- if (followup_Transfer(p)) {
-
- Transfer_t *next = p->next_followup;
- remove_from_async_followup_list(p);
- println(" remove from followup list");
- if (p->qtd.token & 0x40) {
- Pipe_t *haltedpipe = p->pipe;
- free_Transfer(p);
-
-
-
- Transfer_t *first = NULL;
- Transfer_t *last = NULL;
- p = next;
- while (p) {
- Transfer_t *next2 = p->next_followup;
- if (p->pipe == haltedpipe) {
- println(" stray halted ", (uint32_t)p, HEX);
- remove_from_async_followup_list(p);
- if (first == NULL) {
- first = p;
- last = p;
- } else {
- last->next_followup = p;
- }
- p->next_followup = NULL;
- if (next == p) next = next2;
- }
- p = next2;
- }
-
-
- p = (Transfer_t *)(haltedpipe->qh.next & ~0x1F);
- while (p && ((p->qtd.token & 0x40) == 0)) {
- print(" qtd: ", (uint32_t)p, HEX);
- print(", token=", (uint32_t)p->qtd.token, HEX);
- println(", next=", (uint32_t)p->qtd.next, HEX);
- p = (Transfer_t *)(p->qtd.next & ~0x1F);
- }
- if (p) {
-
-
- println(" dummy halt: ", (uint32_t)p, HEX);
- haltedpipe->qh.next = (uint32_t)p;
- haltedpipe->qh.current = 0;
- haltedpipe->qh.token = 0;
- } else {
- println(" no dummy halt found, yikes!");
-
- }
-
-
-
-
-
- p = first;
- while (p) {
- uint32_t token = p->qtd.token;
- if (token & 0x8000 && haltedpipe->callback_function) {
-
- p->qtd.token = token | 0x40;
- (*(p->pipe->callback_function))(p);
- }
- Transfer_t *next2 = p->next_followup;
- free_Transfer(p);
- p = next2;
- }
- } else {
- free_Transfer(p);
- }
- p = next;
- } else {
-
- println(" remain on followup list");
- p = p->next_followup;
- }
- }
-
- }
-
- static void add_to_async_followup_list(Transfer_t *first, Transfer_t *last)
- {
- last->next_followup = NULL;
- if (async_followup_last == NULL) {
- first->prev_followup = NULL;
- async_followup_first = first;
- } else {
- first->prev_followup = async_followup_last;
- async_followup_last->next_followup = first;
- }
- async_followup_last = last;
- }
-
- static void remove_from_async_followup_list(Transfer_t *transfer)
- {
- Transfer_t *next = transfer->next_followup;
- Transfer_t *prev = transfer->prev_followup;
- if (prev) {
- prev->next_followup = next;
- } else {
- async_followup_first = next;
- }
- if (next) {
- next->prev_followup = prev;
- } else {
- async_followup_last = prev;
- }
- }
-
- static void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last)
- {
- last->next_followup = NULL;
- if (periodic_followup_last == NULL) {
- first->prev_followup = NULL;
- periodic_followup_first = first;
- } else {
- first->prev_followup = periodic_followup_last;
- periodic_followup_last->next_followup = first;
- }
- periodic_followup_last = last;
- }
-
- static void remove_from_periodic_followup_list(Transfer_t *transfer)
- {
- Transfer_t *next = transfer->next_followup;
- Transfer_t *prev = transfer->prev_followup;
- if (prev) {
- prev->next_followup = next;
- } else {
- periodic_followup_first = next;
- }
- if (next) {
- next->prev_followup = prev;
- } else {
- periodic_followup_last = prev;
- }
- }
-
-
- static uint32_t max4(uint32_t n1, uint32_t n2, uint32_t n3, uint32_t n4)
- {
- if (n1 > n2) {
-
- if (n1 > n3) {
-
- if (n1 > n4) return n1;
- } else {
-
- if (n3 > n4) return n3;
- }
- } else {
-
- if (n2 > n3) {
-
- if (n2 > n4) return n2;
- } else {
-
- if (n3 > n4) return n3;
- }
- }
- return n4;
- }
-
- static uint32_t round_to_power_of_two(uint32_t n, uint32_t maxnum)
- {
- for (uint32_t pow2num=1; pow2num < maxnum; pow2num <<= 1) {
- if (n <= (pow2num | (pow2num >> 1))) return pow2num;
- }
- return maxnum;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- bool USBHost::allocate_interrupt_pipe_bandwidth(Pipe_t *pipe, uint32_t maxlen, uint32_t interval)
- {
- println("allocate_interrupt_pipe_bandwidth");
- if (interval == 0) interval = 1;
- maxlen = (maxlen * 76459) >> 16;
- if (pipe->device->speed == 2) {
-
- println(" ep interval = ", interval);
- if (interval > 15) interval = 15;
- interval = 1 << (interval - 1);
- if (interval > PERIODIC_LIST_SIZE*8) interval = PERIODIC_LIST_SIZE*8;
- println(" interval = ", interval);
- uint32_t pinterval = interval >> 3;
- pipe->periodic_interval = (pinterval > 0) ? pinterval : 1;
- uint32_t stime = (55 + 32 + maxlen) >> 5;
- uint32_t best_offset = 0xFFFFFFFF;
- uint32_t best_bandwidth = 0xFFFFFFFF;
- for (uint32_t offset=0; offset < interval; offset++) {
-
- uint32_t max_bandwidth = 0;
- for (uint32_t i=offset; i < PERIODIC_LIST_SIZE*8; i += interval) {
- uint32_t bandwidth = uframe_bandwidth[i] + stime;
- if (bandwidth > max_bandwidth) max_bandwidth = bandwidth;
- }
-
- if (max_bandwidth < best_bandwidth) {
- best_bandwidth = max_bandwidth;
- best_offset = offset;
- }
- }
- print(" best_bandwidth = ", best_bandwidth);
-
- println(", at offset = ", best_offset);
-
-
-
- if (best_bandwidth > 187) return false;
-
- pipe->bandwidth_interval = interval;
- pipe->bandwidth_offset = best_offset;
- pipe->bandwidth_stime = stime;
- for (uint32_t i=best_offset; i < PERIODIC_LIST_SIZE*8; i += interval) {
- uframe_bandwidth[i] += stime;
- }
- if (interval == 1) {
- pipe->start_mask = 0xFF;
- } else if (interval == 2) {
- pipe->start_mask = 0x55 << (best_offset & 1);
- } else if (interval <= 4) {
- pipe->start_mask = 0x11 << (best_offset & 3);
- } else {
- pipe->start_mask = 0x01 << (best_offset & 7);
- }
- pipe->periodic_offset = best_offset >> 3;
- pipe->complete_mask = 0;
- } else {
-
- interval = round_to_power_of_two(interval, PERIODIC_LIST_SIZE);
- pipe->periodic_interval = interval;
- uint32_t stime, ctime;
- if (pipe->direction == 0) {
-
-
-
- stime = (100 + 32 + maxlen) >> 5;
- ctime = (55 + 32) >> 5;
- } else {
-
- stime = (40 + 32) >> 5;
- ctime = (70 + 32 + maxlen) >> 5;
- }
-
-
-
-
- uint32_t best_shift = 0;
- uint32_t best_offset = 0xFFFFFFFF;
- uint32_t best_bandwidth = 0xFFFFFFFF;
- for (uint32_t offset=0; offset < interval; offset++) {
-
- uint32_t max_bandwidth = 0;
- for (uint32_t i=offset; i < PERIODIC_LIST_SIZE; i += interval) {
- for (uint32_t j=0; j <= 3; j++) {
-
-
- uint32_t n = (i << 3) + j;
- uint32_t bw1 = uframe_bandwidth[n+0] + stime;
- uint32_t bw2 = uframe_bandwidth[n+2] + ctime;
- uint32_t bw3 = uframe_bandwidth[n+3] + ctime;
- uint32_t bw4 = uframe_bandwidth[n+4] + ctime;
- max_bandwidth = max4(bw1, bw2, bw3, bw4);
-
- if (max_bandwidth < best_bandwidth) {
- best_bandwidth = max_bandwidth;
- best_offset = i;
- best_shift = j;
- }
- }
- }
- }
- print(" best_bandwidth = ", best_bandwidth);
-
- print(", at offset = ", best_offset);
-
- println(", shift= ", best_shift);
-
-
-
- if (best_bandwidth > 187) return false;
-
- pipe->bandwidth_interval = interval;
- pipe->bandwidth_offset = best_offset;
- pipe->bandwidth_shift = best_shift;
- pipe->bandwidth_stime = stime;
- pipe->bandwidth_ctime = ctime;
- for (uint32_t i=best_offset; i < PERIODIC_LIST_SIZE; i += interval) {
- uint32_t n = (i << 3) + best_shift;
- uframe_bandwidth[n+0] += stime;
- uframe_bandwidth[n+2] += ctime;
- uframe_bandwidth[n+3] += ctime;
- uframe_bandwidth[n+4] += ctime;
- }
- pipe->start_mask = 0x01 << best_shift;
- pipe->complete_mask = 0x1C << best_shift;
- pipe->periodic_offset = best_offset;
- }
- return true;
- }
-
-
-
-
- void USBHost::add_qh_to_periodic_schedule(Pipe_t *pipe)
- {
-
-
- #if 0
- pipe->qh.horizontal_link = periodictable[0];
- periodictable[0] = (uint32_t)&(pipe->qh) | 2;
- println("init periodictable with ", periodictable[0], HEX);
- #else
- uint32_t interval = pipe->periodic_interval;
- uint32_t offset = pipe->periodic_offset;
-
-
-
-
- for (uint32_t i=offset; i < PERIODIC_LIST_SIZE; i += interval) {
-
-
-
- uint32_t num = periodictable[i];
- Pipe_t *node = (Pipe_t *)(num & 0xFFFFFFE0);
- if ((num & 1) || ((num & 6) == 2 && node->periodic_interval < interval)) {
-
- pipe->qh.horizontal_link = num;
- periodictable[i] = (uint32_t)&(pipe->qh) | 2;
- } else {
-
-
- while (node->periodic_interval >= interval) {
- if (node == pipe) goto nextslot;
-
-
-
- if (node->qh.horizontal_link & 1) break;
- num = node->qh.horizontal_link;
- node = (Pipe_t *)(num & 0xFFFFFFE0);
- }
- Pipe_t *n = node;
- do {
- if (n == pipe) goto nextslot;
- n = (Pipe_t *)(n->qh.horizontal_link & 0xFFFFFFE0);
- } while (n != NULL);
-
-
-
- pipe->qh.horizontal_link = node->qh.horizontal_link;
- node->qh.horizontal_link = (uint32_t)pipe | 2;
-
-
-
- }
- nextslot:
-
-
-
- {}
- }
- #endif
- #if 0
- println("Periodic Schedule:");
- for (uint32_t i=0; i < PERIODIC_LIST_SIZE; i++) {
- if (i < 10) print(" ");
- print(i);
- print(": ");
- print_qh_list((Pipe_t *)(periodictable[i] & 0xFFFFFFE0));
- }
- #endif
- }
-
-
- void USBHost::delete_Pipe(Pipe_t *pipe)
- {
- println("delete_Pipe ", (uint32_t)pipe, HEX);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- bool isasync = (pipe->type == 0 || pipe->type == 2);
- if (isasync) {
-
- Pipe_t *next = (Pipe_t *)(pipe->qh.horizontal_link & 0xFFFFFFE0);
- if (next == pipe) {
-
- println(" shut down async schedule");
- USBHS_USBCMD &= ~USBHS_USBCMD_ASE;
- while (USBHS_USBSTS & USBHS_USBSTS_AS) ;
- USBHS_ASYNCLISTADDR = 0;
- } else {
-
- println(" remove QH from async schedule");
- Pipe_t *prev = next;
- while (1) {
- Pipe_t *n = (Pipe_t *)(prev->qh.horizontal_link & 0xFFFFFFE0);
- if (n == pipe) break;
- prev = n;
- }
-
- if (pipe->qh.capabilities[0] & 0x8000) {
- prev->qh.capabilities[0] |= 0x8000;
- }
-
- prev->qh.horizontal_link = pipe->qh.horizontal_link;
-
-
- USBHS_USBCMD |= USBHS_USBCMD_IAA;
- while (!(USBHS_USBSTS & USBHS_USBSTS_AAI)) ;
- USBHS_USBSTS = USBHS_USBSTS_AAI;
-
- }
-
- println(" Free transfers");
- Transfer_t *t = async_followup_first;
- while (t) {
- print(" * ", (uint32_t)t);
- Transfer_t *next = t->next_followup;
- if (t->pipe == pipe) {
- print(" * remove");
- remove_from_async_followup_list(t);
-
-
- Transfer_t *tr = (Transfer_t *)(pipe->qh.next);
- while (((uint32_t)tr & 0xFFFFFFE0) && (tr != t)){
- tr = (Transfer_t *)(tr->qtd.next);
- }
- if (tr == t) {
- println(" * defer free until QH");
- } else {
- println(" * free");
- free_Transfer(t);
- }
- } else {
- println("");
- }
- t = next;
- }
- } else {
-
- for (uint32_t i=0; i < PERIODIC_LIST_SIZE; i++) {
- uint32_t num = periodictable[i];
- if (num & 1) continue;
- Pipe_t *node = (Pipe_t *)(num & 0xFFFFFFE0);
- if (node == pipe) {
- periodictable[i] = pipe->qh.horizontal_link;
- continue;
- }
- Pipe_t *prev = node;
- while (1) {
- num = node->qh.horizontal_link;
- if (num & 1) break;
- node = (Pipe_t *)(num & 0xFFFFFFE0);
- if (node == pipe) {
- prev->qh.horizontal_link = node->qh.horizontal_link;
- break;
- }
- prev = node;
- }
- }
-
- if (pipe->device->speed == 2) {
- uint32_t interval = pipe->bandwidth_interval;
- uint32_t offset = pipe->bandwidth_offset;
- uint32_t stime = pipe->bandwidth_stime;
- for (uint32_t i=offset; i < PERIODIC_LIST_SIZE*8; i += interval) {
- uframe_bandwidth[i] -= stime;
- }
- } else {
- uint32_t interval = pipe->bandwidth_interval;
- uint32_t offset = pipe->bandwidth_offset;
- uint32_t shift = pipe->bandwidth_shift;
- uint32_t stime = pipe->bandwidth_stime;
- uint32_t ctime = pipe->bandwidth_ctime;
- for (uint32_t i=offset; i < PERIODIC_LIST_SIZE; i += interval) {
- uint32_t n = (i << 3) + shift;
- uframe_bandwidth[n+0] -= stime;
- uframe_bandwidth[n+2] -= ctime;
- uframe_bandwidth[n+3] -= ctime;
- uframe_bandwidth[n+4] -= ctime;
- }
- }
-
-
- println(" Free transfers");
- Transfer_t *t = periodic_followup_first;
- while (t) {
- print(" * ", (uint32_t)t);
- Transfer_t *next = t->next_followup;
- if (t->pipe == pipe) {
- print(" * remove");
- remove_from_periodic_followup_list(t);
-
-
- Transfer_t *tr = (Transfer_t *)(pipe->qh.next);
- while (((uint32_t)tr & 0xFFFFFFE0) && (tr != t)){
- tr = (Transfer_t *)(tr->qtd.next);
- }
- if (tr == t) {
- println(" * defer free until QH");
- } else {
- println(" * free");
- free_Transfer(t);
- }
- } else {
- println("");
- }
- t = next;
- }
- }
-
-
-
-
- println(" Free transfers attached to QH");
- Transfer_t *tr = (Transfer_t *)(pipe->qh.next);
- while ((uint32_t)tr & 0xFFFFFFE0) {
- println(" * ", (uint32_t)tr);
- Transfer_t *next = (Transfer_t *)(tr->qtd.next);
- free_Transfer(tr);
- tr = next;
- }
-
- free_Pipe(pipe);
- println("* Delete Pipe completed");
- }
-
|