|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
-
-
- #include <Arduino.h>
- #include "USBHost.h"
-
- 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
- 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()
- {
-
- PORTE_PCR6 = PORT_PCR_MUX(1);
- GPIOE_PDDR |= (1<<6);
- GPIOE_PSOR = (1<<6);
- 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;
- Serial.print("MPU_RGDAAC0 = ");
- Serial.println(MPU_RGDAAC0, HEX);
-
-
- MCG_C1 |= MCG_C1_IRCLKEN;
- OSC0_CR |= OSC_ERCLKEN;
- SIM_SOPT2 |= SIM_SOPT2_USBREGEN;
- SIM_SOPT2 &= ~SIM_SOPT2_USBSLSRC;
- print("power up USBHS PHY");
- SIM_USBPHYCTL |= SIM_USBPHYCTL_USBDISILIM;
-
- SIM_SCGC3 |= SIM_SCGC3_USBHSDCD | SIM_SCGC3_USBHSPHY | SIM_SCGC3_USBHS;
- USBHSDCD_CLOCK = 33 << 2;
- print("init USBHS PHY & PLL");
-
- 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++;
- }
- Serial.print("PLL locked, waited ");
- Serial.println(count);
-
-
- USBPHY_PWD = 0;
- delay(10);
-
-
-
-
-
-
-
-
-
-
-
-
-
- print("begin ehci reset");
- USBHS_USBCMD |= USBHS_USBCMD_RST;
- count = 0;
- while (USBHS_USBCMD & USBHS_USBCMD_RST) {
- count++;
- }
- print(" reset waited ", count);
-
- init_Device_Pipe_Transfer_memory();
- for (int i=0; i < 32; i++) {
- periodictable[i] = 1;
- }
- 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_FS2 | USBHS_USBCMD_FS(1);
-
-
-
- USBHS_PORTSC1 |= USBHS_PORTSC_PP;
-
-
-
- Serial.print("USBHS_ASYNCLISTADDR = ");
- Serial.println(USBHS_ASYNCLISTADDR, HEX);
- Serial.print("USBHS_PERIODICLISTBASE = ");
- Serial.println(USBHS_PERIODICLISTBASE, HEX);
- Serial.print("periodictable = ");
- Serial.println((uint32_t)periodictable, HEX);
-
-
- 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;
- USBHS_USBINTR |= USBHS_USBINTR_AAE;
- USBHS_USBINTR |= USBHS_USBINTR_UPIE | USBHS_USBINTR_UAIE;
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- void USBHost::isr()
- {
- uint32_t stat = USBHS_USBSTS;
- USBHS_USBSTS = stat;
-
- Serial.println();
- Serial.print("ISR: ");
- Serial.print(stat, HEX);
- Serial.println();
- if (stat & USBHS_USBSTS_UI) Serial.println(" USB Interrupt");
- if (stat & USBHS_USBSTS_UEI) Serial.println(" USB Error");
- if (stat & USBHS_USBSTS_PCI) Serial.println(" Port Change");
- if (stat & USBHS_USBSTS_FRI) Serial.println(" Frame List Rollover");
- if (stat & USBHS_USBSTS_SEI) Serial.println(" System Error");
- if (stat & USBHS_USBSTS_AAI) Serial.println(" Async Advance (doorbell)");
- if (stat & USBHS_USBSTS_URI) Serial.println(" Reset Recv");
- if (stat & USBHS_USBSTS_SRI) Serial.println(" SOF");
- if (stat & USBHS_USBSTS_SLI) Serial.println(" Suspend");
- if (stat & USBHS_USBSTS_HCH) Serial.println(" Host Halted");
- if (stat & USBHS_USBSTS_RCL) Serial.println(" Reclamation");
- if (stat & USBHS_USBSTS_PS) Serial.println(" Periodic Sched En");
- if (stat & USBHS_USBSTS_AS) Serial.println(" Async Sched En");
- if (stat & USBHS_USBSTS_NAKI) Serial.println(" NAK");
- if (stat & USBHS_USBSTS_UAI) Serial.println(" USB Async");
- if (stat & USBHS_USBSTS_UPI) Serial.println(" USB Periodic");
- if (stat & USBHS_USBSTS_TI0) Serial.println(" Timer0");
- if (stat & USBHS_USBSTS_TI1) Serial.println(" Timer1");
-
- if (stat & USBHS_USBSTS_UAI) {
- Serial.println("Async Followup");
- print(async_followup_first, async_followup_last);
- 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;
- }
- }
- print(async_followup_first, async_followup_last);
- }
- if (stat & USBHS_USBSTS_UPI) {
- Serial.println("Periodic Followup");
- 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_PCI) {
- const uint32_t portstat = USBHS_PORTSC1;
- Serial.print("port change: ");
- Serial.print(portstat, HEX);
- Serial.println();
- USBHS_PORTSC1 = portstat | (USBHS_PORTSC_OCC|USBHS_PORTSC_PEC|USBHS_PORTSC_CSC);
- if (portstat & USBHS_PORTSC_OCC) {
- Serial.println(" overcurrent change");
- }
- if (portstat & USBHS_PORTSC_CSC) {
- if (portstat & USBHS_PORTSC_CCS) {
- Serial.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 {
- Serial.println(" disconnect");
- port_state = PORT_STATE_DISCONNECTED;
- USBPHY_CTRL_CLR = USBPHY_CTRL_ENHOSTDISCONDETECT;
-
- }
- }
- if (portstat & USBHS_PORTSC_PEC) {
-
- Serial.println(" disable");
- } else if (port_state == PORT_STATE_RESET && portstat & USBHS_PORTSC_PE) {
- Serial.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) {
- Serial.println(" force resume");
-
- }
- }
- if (stat & USBHS_USBSTS_TI0) {
- Serial.println("timer");
- if (port_state == PORT_STATE_DEBOUNCE) {
- port_state = PORT_STATE_RESET;
- USBHS_PORTSC1 |= USBHS_PORTSC_PR;
- Serial.println(" begin reset");
- } else if (port_state == PORT_STATE_RECOVERY) {
- port_state = PORT_STATE_ACTIVE;
- Serial.println(" end recovery");
-
-
- uint32_t speed = (USBHS_PORTSC1 >> 26) & 3;
- rootdev = new_Device(speed, 0, 0);
- }
- }
-
- }
-
-
- 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 max_packet_len)
- {
- Pipe_t *pipe;
- Transfer_t *halt;
- uint32_t c=0, dtc=0;
-
- Serial.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 == 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, 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 (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;
- Serial.println(" first in async list");
- } else {
-
- pipe->qh.horizontal_link = list->qh.horizontal_link;
- list->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2;
- Serial.println(" added to async list");
- }
- } else if (type == 3) {
-
-
- }
- 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::new_Transfer(Pipe_t *pipe, void *buffer, uint32_t len)
- {
- Serial.println("new_Transfer");
- Transfer_t *transfer = allocate_Transfer();
- if (!transfer) return false;
- if (pipe->type == 0) {
-
- Transfer_t *data, *status;
- uint32_t status_direction;
- if (len > 16384) {
-
-
- free_Transfer(transfer);
- return false;
- }
- status = allocate_Transfer();
- if (!status) {
- free_Transfer(transfer);
- return false;
- }
- if (len > 0) {
- data = allocate_Transfer();
- if (!data) {
- free_Transfer(transfer);
- free_Transfer(status);
- return false;
- }
- init_qTD(data, buffer, len, pipe->direction, 1, false);
- transfer->qtd.next = (uint32_t)data;
- data->qtd.next = (uint32_t)status;
- status_direction = pipe->direction ^ 1;
- } else {
- transfer->qtd.next = (uint32_t)status;
- status_direction = 1;
- }
- Serial.print("setup address ");
- Serial.println((uint32_t)&pipe->device->setup, HEX);
- init_qTD(transfer, &pipe->device->setup, 8, 2, 0, false);
- init_qTD(status, NULL, 0, status_direction, 1, true);
- status->pipe = pipe;
- status->buffer = buffer;
- status->length = len;
- status->qtd.next = 1;
- } else {
-
- free_Transfer(transfer);
- return false;
- }
-
- 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;
-
- 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;
- print(halt, p);
-
- 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;
- }
-
- static bool followup_Transfer(Transfer_t *transfer)
- {
- Serial.print(" Followup ");
- Serial.println((uint32_t)transfer, HEX);
-
- if (!(transfer->qtd.token & 0x80)) {
-
- if (transfer->qtd.token & 0x8000) {
-
- if (transfer->pipe->callback_function) {
-
- (*(transfer->pipe->callback_function))(transfer);
- }
- }
-
- Serial.println(" completed");
- return true;
- }
- return false;
- }
-
- 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;
- }
- }
-
|