EthernetNetIf Compatibility.
Dependents: XBeeWiFi_SPI_example
Fork of NetServicesSource by
drv/usb/UsbHostMgr.cpp@4:fd826cad83c0, 2010-07-09 (annotated)
- Committer:
- donatien
- Date:
- Fri Jul 09 14:46:47 2010 +0000
- Revision:
- 4:fd826cad83c0
- Child:
- 5:dd63a1e02b1b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 4:fd826cad83c0 | 1 | |
donatien | 4:fd826cad83c0 | 2 | /* |
donatien | 4:fd826cad83c0 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
donatien | 4:fd826cad83c0 | 4 | |
donatien | 4:fd826cad83c0 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
donatien | 4:fd826cad83c0 | 6 | of this software and associated documentation files (the "Software"), to deal |
donatien | 4:fd826cad83c0 | 7 | in the Software without restriction, including without limitation the rights |
donatien | 4:fd826cad83c0 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
donatien | 4:fd826cad83c0 | 9 | copies of the Software, and to permit persons to whom the Software is |
donatien | 4:fd826cad83c0 | 10 | furnished to do so, subject to the following conditions: |
donatien | 4:fd826cad83c0 | 11 | |
donatien | 4:fd826cad83c0 | 12 | The above copyright notice and this permission notice shall be included in |
donatien | 4:fd826cad83c0 | 13 | all copies or substantial portions of the Software. |
donatien | 4:fd826cad83c0 | 14 | |
donatien | 4:fd826cad83c0 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
donatien | 4:fd826cad83c0 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
donatien | 4:fd826cad83c0 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
donatien | 4:fd826cad83c0 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
donatien | 4:fd826cad83c0 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 4:fd826cad83c0 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
donatien | 4:fd826cad83c0 | 21 | THE SOFTWARE. |
donatien | 4:fd826cad83c0 | 22 | */ |
donatien | 4:fd826cad83c0 | 23 | |
donatien | 4:fd826cad83c0 | 24 | #include "UsbHostMgr.h" |
donatien | 4:fd826cad83c0 | 25 | |
donatien | 4:fd826cad83c0 | 26 | #include "usb_mem.h" |
donatien | 4:fd826cad83c0 | 27 | |
donatien | 4:fd826cad83c0 | 28 | #include "string.h" //For memcpy, memmove, memset |
donatien | 4:fd826cad83c0 | 29 | |
donatien | 4:fd826cad83c0 | 30 | #include "netCfg.h" |
donatien | 4:fd826cad83c0 | 31 | #if NET_USB |
donatien | 4:fd826cad83c0 | 32 | |
donatien | 4:fd826cad83c0 | 33 | //#define __DEBUG |
donatien | 4:fd826cad83c0 | 34 | #include "dbg/dbg.h" |
donatien | 4:fd826cad83c0 | 35 | |
donatien | 4:fd826cad83c0 | 36 | // bits of the USB/OTG clock control register |
donatien | 4:fd826cad83c0 | 37 | #define HOST_CLK_EN (1<<0) |
donatien | 4:fd826cad83c0 | 38 | #define DEV_CLK_EN (1<<1) |
donatien | 4:fd826cad83c0 | 39 | #define PORTSEL_CLK_EN (1<<3) |
donatien | 4:fd826cad83c0 | 40 | #define AHB_CLK_EN (1<<4) |
donatien | 4:fd826cad83c0 | 41 | |
donatien | 4:fd826cad83c0 | 42 | // bits of the USB/OTG clock status register |
donatien | 4:fd826cad83c0 | 43 | #define HOST_CLK_ON (1<<0) |
donatien | 4:fd826cad83c0 | 44 | #define DEV_CLK_ON (1<<1) |
donatien | 4:fd826cad83c0 | 45 | #define PORTSEL_CLK_ON (1<<3) |
donatien | 4:fd826cad83c0 | 46 | #define AHB_CLK_ON (1<<4) |
donatien | 4:fd826cad83c0 | 47 | |
donatien | 4:fd826cad83c0 | 48 | // we need host clock, OTG/portsel clock and AHB clock |
donatien | 4:fd826cad83c0 | 49 | #define CLOCK_MASK (HOST_CLK_EN | PORTSEL_CLK_EN | AHB_CLK_EN) |
donatien | 4:fd826cad83c0 | 50 | |
donatien | 4:fd826cad83c0 | 51 | static UsbHostMgr* pMgr = NULL; |
donatien | 4:fd826cad83c0 | 52 | |
donatien | 4:fd826cad83c0 | 53 | extern "C" void sUsbIrqhandler(void) __irq |
donatien | 4:fd826cad83c0 | 54 | { |
donatien | 4:fd826cad83c0 | 55 | DBG("\n+Int\n"); |
donatien | 4:fd826cad83c0 | 56 | if(pMgr) |
donatien | 4:fd826cad83c0 | 57 | pMgr->UsbIrqhandler(); |
donatien | 4:fd826cad83c0 | 58 | DBG("\n-Int\n"); |
donatien | 4:fd826cad83c0 | 59 | return; |
donatien | 4:fd826cad83c0 | 60 | } |
donatien | 4:fd826cad83c0 | 61 | |
donatien | 4:fd826cad83c0 | 62 | UsbHostMgr::UsbHostMgr() : m_lpDevices() |
donatien | 4:fd826cad83c0 | 63 | { |
donatien | 4:fd826cad83c0 | 64 | if(!pMgr) |
donatien | 4:fd826cad83c0 | 65 | pMgr = this; |
donatien | 4:fd826cad83c0 | 66 | usb_mem_init(); |
donatien | 4:fd826cad83c0 | 67 | memset(m_lpDevices, NULL, sizeof(UsbDevice*) * USB_HOSTMGR_MAX_DEVS); |
donatien | 4:fd826cad83c0 | 68 | m_pHcca = (HCCA*) usb_get_hcca(); |
donatien | 4:fd826cad83c0 | 69 | memset((void*)m_pHcca, 0, 0x100); |
donatien | 4:fd826cad83c0 | 70 | DBG("Host manager at %p\n", this); |
donatien | 4:fd826cad83c0 | 71 | } |
donatien | 4:fd826cad83c0 | 72 | |
donatien | 4:fd826cad83c0 | 73 | UsbHostMgr::~UsbHostMgr() |
donatien | 4:fd826cad83c0 | 74 | { |
donatien | 4:fd826cad83c0 | 75 | if(pMgr == this) |
donatien | 4:fd826cad83c0 | 76 | pMgr = NULL; |
donatien | 4:fd826cad83c0 | 77 | } |
donatien | 4:fd826cad83c0 | 78 | |
donatien | 4:fd826cad83c0 | 79 | UsbErr UsbHostMgr::init() //Initialize host |
donatien | 4:fd826cad83c0 | 80 | { |
donatien | 4:fd826cad83c0 | 81 | NVIC_DisableIRQ(USB_IRQn); /* Disable the USB interrupt source */ |
donatien | 4:fd826cad83c0 | 82 | |
donatien | 4:fd826cad83c0 | 83 | LPC_SC->PCONP &= ~(1UL<<31); //Cut power |
donatien | 4:fd826cad83c0 | 84 | wait(1); |
donatien | 4:fd826cad83c0 | 85 | |
donatien | 4:fd826cad83c0 | 86 | |
donatien | 4:fd826cad83c0 | 87 | // turn on power for USB |
donatien | 4:fd826cad83c0 | 88 | LPC_SC->PCONP |= (1UL<<31); |
donatien | 4:fd826cad83c0 | 89 | // Enable USB host clock, port selection and AHB clock |
donatien | 4:fd826cad83c0 | 90 | LPC_USB->USBClkCtrl |= CLOCK_MASK; |
donatien | 4:fd826cad83c0 | 91 | // Wait for clocks to become available |
donatien | 4:fd826cad83c0 | 92 | while ((LPC_USB->USBClkSt & CLOCK_MASK) != CLOCK_MASK) |
donatien | 4:fd826cad83c0 | 93 | ; |
donatien | 4:fd826cad83c0 | 94 | |
donatien | 4:fd826cad83c0 | 95 | // it seems the bits[0:1] mean the following |
donatien | 4:fd826cad83c0 | 96 | // 0: U1=device, U2=host |
donatien | 4:fd826cad83c0 | 97 | // 1: U1=host, U2=host |
donatien | 4:fd826cad83c0 | 98 | // 2: reserved |
donatien | 4:fd826cad83c0 | 99 | // 3: U1=host, U2=device |
donatien | 4:fd826cad83c0 | 100 | // NB: this register is only available if OTG clock (aka "port select") is enabled!! |
donatien | 4:fd826cad83c0 | 101 | // since we don't care about port 2, set just bit 0 to 1 (U1=host) |
donatien | 4:fd826cad83c0 | 102 | LPC_USB->OTGStCtrl |= 1; |
donatien | 4:fd826cad83c0 | 103 | |
donatien | 4:fd826cad83c0 | 104 | // now that we've configured the ports, we can turn off the portsel clock |
donatien | 4:fd826cad83c0 | 105 | LPC_USB->USBClkCtrl &= ~PORTSEL_CLK_EN; |
donatien | 4:fd826cad83c0 | 106 | |
donatien | 4:fd826cad83c0 | 107 | // power pins are not connected on mbed, so we can skip them |
donatien | 4:fd826cad83c0 | 108 | /* P1[18] = USB_UP_LED, 01 */ |
donatien | 4:fd826cad83c0 | 109 | /* P1[19] = /USB_PPWR, 10 */ |
donatien | 4:fd826cad83c0 | 110 | /* P1[22] = USB_PWRD, 10 */ |
donatien | 4:fd826cad83c0 | 111 | /* P1[27] = /USB_OVRCR, 10 */ |
donatien | 4:fd826cad83c0 | 112 | /*LPC_PINCON->PINSEL3 &= ~((3<<4) | (3<<6) | (3<<12) | (3<<22)); |
donatien | 4:fd826cad83c0 | 113 | LPC_PINCON->PINSEL3 |= ((1<<4)|(2<<6) | (2<<12) | (2<<22)); // 0x00802080 |
donatien | 4:fd826cad83c0 | 114 | */ |
donatien | 4:fd826cad83c0 | 115 | |
donatien | 4:fd826cad83c0 | 116 | // configure USB D+/D- pins |
donatien | 4:fd826cad83c0 | 117 | /* P0[29] = USB_D+, 01 */ |
donatien | 4:fd826cad83c0 | 118 | /* P0[30] = USB_D-, 01 */ |
donatien | 4:fd826cad83c0 | 119 | LPC_PINCON->PINSEL1 &= ~((3<<26) | (3<<28)); |
donatien | 4:fd826cad83c0 | 120 | LPC_PINCON->PINSEL1 |= ((1<<26)|(1<<28)); // 0x14000000 |
donatien | 4:fd826cad83c0 | 121 | |
donatien | 4:fd826cad83c0 | 122 | DBG("Initializing Host Stack\n"); |
donatien | 4:fd826cad83c0 | 123 | |
donatien | 4:fd826cad83c0 | 124 | wait_ms(100); /* Wait 50 ms before apply reset */ |
donatien | 4:fd826cad83c0 | 125 | LPC_USB->HcControl = 0; /* HARDWARE RESET */ |
donatien | 4:fd826cad83c0 | 126 | LPC_USB->HcControlHeadED = 0; /* Initialize Control list head to Zero */ |
donatien | 4:fd826cad83c0 | 127 | LPC_USB->HcBulkHeadED = 0; /* Initialize Bulk list head to Zero */ |
donatien | 4:fd826cad83c0 | 128 | |
donatien | 4:fd826cad83c0 | 129 | /* SOFTWARE RESET */ |
donatien | 4:fd826cad83c0 | 130 | LPC_USB->HcCommandStatus = OR_CMD_STATUS_HCR; |
donatien | 4:fd826cad83c0 | 131 | LPC_USB->HcFmInterval = DEFAULT_FMINTERVAL; /* Write Fm Interval and Largest Data Packet Counter */ |
donatien | 4:fd826cad83c0 | 132 | |
donatien | 4:fd826cad83c0 | 133 | /* Put HC in operational state */ |
donatien | 4:fd826cad83c0 | 134 | LPC_USB->HcControl = (LPC_USB->HcControl & (~OR_CONTROL_HCFS)) | OR_CONTROL_HC_OPER; |
donatien | 4:fd826cad83c0 | 135 | LPC_USB->HcRhStatus = OR_RH_STATUS_LPSC; /* Set Global Power */ |
donatien | 4:fd826cad83c0 | 136 | |
donatien | 4:fd826cad83c0 | 137 | LPC_USB->HcHCCA = (uint32_t)(m_pHcca); |
donatien | 4:fd826cad83c0 | 138 | LPC_USB->HcInterruptStatus |= LPC_USB->HcInterruptStatus; /* Clear Interrrupt Status */ |
donatien | 4:fd826cad83c0 | 139 | |
donatien | 4:fd826cad83c0 | 140 | |
donatien | 4:fd826cad83c0 | 141 | LPC_USB->HcInterruptEnable = OR_INTR_ENABLE_MIE | |
donatien | 4:fd826cad83c0 | 142 | OR_INTR_ENABLE_WDH | |
donatien | 4:fd826cad83c0 | 143 | OR_INTR_ENABLE_RHSC; |
donatien | 4:fd826cad83c0 | 144 | |
donatien | 4:fd826cad83c0 | 145 | NVIC_SetPriority(USB_IRQn, 0); /* highest priority */ |
donatien | 4:fd826cad83c0 | 146 | /* Enable the USB Interrupt */ |
donatien | 4:fd826cad83c0 | 147 | NVIC_SetVector(USB_IRQn, (uint32_t)(sUsbIrqhandler)); |
donatien | 4:fd826cad83c0 | 148 | LPC_USB->HcRhPortStatus1 = OR_RH_PORT_CSC; |
donatien | 4:fd826cad83c0 | 149 | LPC_USB->HcRhPortStatus1 = OR_RH_PORT_PRSC; |
donatien | 4:fd826cad83c0 | 150 | |
donatien | 4:fd826cad83c0 | 151 | /* Check for any connected devices */ |
donatien | 4:fd826cad83c0 | 152 | if (LPC_USB->HcRhPortStatus1 & OR_RH_PORT_CCS) //Root device connected |
donatien | 4:fd826cad83c0 | 153 | { |
donatien | 4:fd826cad83c0 | 154 | //Device connected |
donatien | 4:fd826cad83c0 | 155 | wait(1); |
donatien | 4:fd826cad83c0 | 156 | DBG("Device connected (%08x)\n", LPC_USB->HcRhPortStatus1); |
donatien | 4:fd826cad83c0 | 157 | onUsbDeviceConnected(0, 1); //Hub 0 (root hub), Port 1 (count starts at 1) |
donatien | 4:fd826cad83c0 | 158 | } |
donatien | 4:fd826cad83c0 | 159 | |
donatien | 4:fd826cad83c0 | 160 | DBG("Enabling IRQ\n"); |
donatien | 4:fd826cad83c0 | 161 | NVIC_EnableIRQ(USB_IRQn); |
donatien | 4:fd826cad83c0 | 162 | DBG("End of host stack initialization\n"); |
donatien | 4:fd826cad83c0 | 163 | return USBERR_OK; |
donatien | 4:fd826cad83c0 | 164 | } |
donatien | 4:fd826cad83c0 | 165 | |
donatien | 4:fd826cad83c0 | 166 | void UsbHostMgr::poll() //Enumerate connected devices, etc |
donatien | 4:fd826cad83c0 | 167 | { |
donatien | 4:fd826cad83c0 | 168 | for(int i = 0; i < devicesCount(); i++) |
donatien | 4:fd826cad83c0 | 169 | { |
donatien | 4:fd826cad83c0 | 170 | if( (m_lpDevices[i]->m_connected) |
donatien | 4:fd826cad83c0 | 171 | && !(m_lpDevices[i]->m_enumerated) ) |
donatien | 4:fd826cad83c0 | 172 | { |
donatien | 4:fd826cad83c0 | 173 | m_lpDevices[i]->enumerate(); |
donatien | 4:fd826cad83c0 | 174 | return; |
donatien | 4:fd826cad83c0 | 175 | } |
donatien | 4:fd826cad83c0 | 176 | } |
donatien | 4:fd826cad83c0 | 177 | } |
donatien | 4:fd826cad83c0 | 178 | |
donatien | 4:fd826cad83c0 | 179 | int UsbHostMgr::devicesCount() |
donatien | 4:fd826cad83c0 | 180 | { |
donatien | 4:fd826cad83c0 | 181 | int i; |
donatien | 4:fd826cad83c0 | 182 | for(i = 0; i < USB_HOSTMGR_MAX_DEVS; i++) |
donatien | 4:fd826cad83c0 | 183 | { |
donatien | 4:fd826cad83c0 | 184 | if (m_lpDevices[i] == NULL) |
donatien | 4:fd826cad83c0 | 185 | break; |
donatien | 4:fd826cad83c0 | 186 | } |
donatien | 4:fd826cad83c0 | 187 | return i; |
donatien | 4:fd826cad83c0 | 188 | } |
donatien | 4:fd826cad83c0 | 189 | |
donatien | 4:fd826cad83c0 | 190 | UsbDevice* UsbHostMgr::getDevice(int item) |
donatien | 4:fd826cad83c0 | 191 | { |
donatien | 4:fd826cad83c0 | 192 | UsbDevice* pDev = m_lpDevices[item]; |
donatien | 4:fd826cad83c0 | 193 | if(!pDev) |
donatien | 4:fd826cad83c0 | 194 | return NULL; |
donatien | 4:fd826cad83c0 | 195 | |
donatien | 4:fd826cad83c0 | 196 | pDev->m_refs++; |
donatien | 4:fd826cad83c0 | 197 | return pDev; |
donatien | 4:fd826cad83c0 | 198 | } |
donatien | 4:fd826cad83c0 | 199 | |
donatien | 4:fd826cad83c0 | 200 | void UsbHostMgr::releaseDevice(UsbDevice* pDev) |
donatien | 4:fd826cad83c0 | 201 | { |
donatien | 4:fd826cad83c0 | 202 | pDev->m_refs--; |
donatien | 4:fd826cad83c0 | 203 | if(pDev->m_refs > 0) |
donatien | 4:fd826cad83c0 | 204 | return; |
donatien | 4:fd826cad83c0 | 205 | //If refs count = 0, delete |
donatien | 4:fd826cad83c0 | 206 | //Find & remove from list |
donatien | 4:fd826cad83c0 | 207 | int i; |
donatien | 4:fd826cad83c0 | 208 | for(i = 0; i < USB_HOSTMGR_MAX_DEVS; i++) |
donatien | 4:fd826cad83c0 | 209 | { |
donatien | 4:fd826cad83c0 | 210 | if (m_lpDevices[i] == pDev) |
donatien | 4:fd826cad83c0 | 211 | break; |
donatien | 4:fd826cad83c0 | 212 | } |
donatien | 4:fd826cad83c0 | 213 | if(i!=USB_HOSTMGR_MAX_DEVS) |
donatien | 4:fd826cad83c0 | 214 | memmove(&m_lpDevices[i], &m_lpDevices[i+1], sizeof(UsbDevice*) * (USB_HOSTMGR_MAX_DEVS - (i + 1))); //Safer than memcpy because of overlapping mem |
donatien | 4:fd826cad83c0 | 215 | m_lpDevices[USB_HOSTMGR_MAX_DEVS - 1] = NULL; |
donatien | 4:fd826cad83c0 | 216 | delete pDev; |
donatien | 4:fd826cad83c0 | 217 | } |
donatien | 4:fd826cad83c0 | 218 | |
donatien | 4:fd826cad83c0 | 219 | void UsbHostMgr::UsbIrqhandler() |
donatien | 4:fd826cad83c0 | 220 | { |
donatien | 4:fd826cad83c0 | 221 | uint32_t int_status; |
donatien | 4:fd826cad83c0 | 222 | uint32_t ie_status; |
donatien | 4:fd826cad83c0 | 223 | |
donatien | 4:fd826cad83c0 | 224 | int_status = LPC_USB->HcInterruptStatus; /* Read Interrupt Status */ |
donatien | 4:fd826cad83c0 | 225 | ie_status = LPC_USB->HcInterruptEnable; /* Read Interrupt enable status */ |
donatien | 4:fd826cad83c0 | 226 | |
donatien | 4:fd826cad83c0 | 227 | if (!(int_status & ie_status)) |
donatien | 4:fd826cad83c0 | 228 | { |
donatien | 4:fd826cad83c0 | 229 | return; |
donatien | 4:fd826cad83c0 | 230 | } |
donatien | 4:fd826cad83c0 | 231 | else |
donatien | 4:fd826cad83c0 | 232 | { |
donatien | 4:fd826cad83c0 | 233 | int_status = int_status & ie_status; |
donatien | 4:fd826cad83c0 | 234 | if (int_status & OR_INTR_STATUS_RHSC) /* Root hub status change interrupt */ |
donatien | 4:fd826cad83c0 | 235 | { |
donatien | 4:fd826cad83c0 | 236 | DBG("LPC_USB->HcRhPortStatus1 = %08x\n", LPC_USB->HcRhPortStatus1); |
donatien | 4:fd826cad83c0 | 237 | if (LPC_USB->HcRhPortStatus1 & OR_RH_PORT_CSC) |
donatien | 4:fd826cad83c0 | 238 | { |
donatien | 4:fd826cad83c0 | 239 | if (LPC_USB->HcRhStatus & OR_RH_STATUS_DRWE) |
donatien | 4:fd826cad83c0 | 240 | { |
donatien | 4:fd826cad83c0 | 241 | /* |
donatien | 4:fd826cad83c0 | 242 | * When DRWE is on, Connect Status Change |
donatien | 4:fd826cad83c0 | 243 | * means a remote wakeup event. |
donatien | 4:fd826cad83c0 | 244 | */ |
donatien | 4:fd826cad83c0 | 245 | //HOST_RhscIntr = 1;// JUST SOMETHING FOR A BREAKPOINT |
donatien | 4:fd826cad83c0 | 246 | } |
donatien | 4:fd826cad83c0 | 247 | else |
donatien | 4:fd826cad83c0 | 248 | { |
donatien | 4:fd826cad83c0 | 249 | /* |
donatien | 4:fd826cad83c0 | 250 | * When DRWE is off, Connect Status Change |
donatien | 4:fd826cad83c0 | 251 | * is NOT a remote wakeup event |
donatien | 4:fd826cad83c0 | 252 | */ |
donatien | 4:fd826cad83c0 | 253 | if (LPC_USB->HcRhPortStatus1 & OR_RH_PORT_CCS) //Root device connected |
donatien | 4:fd826cad83c0 | 254 | { |
donatien | 4:fd826cad83c0 | 255 | //Device connected |
donatien | 4:fd826cad83c0 | 256 | DBG("Device connected (%08x)\n", LPC_USB->HcRhPortStatus1); |
donatien | 4:fd826cad83c0 | 257 | onUsbDeviceConnected(0, 1); //Hub 0 (root hub), Port 1 (count starts at 1) |
donatien | 4:fd826cad83c0 | 258 | } |
donatien | 4:fd826cad83c0 | 259 | else //Root device disconnected |
donatien | 4:fd826cad83c0 | 260 | { |
donatien | 4:fd826cad83c0 | 261 | //Device disconnected |
donatien | 4:fd826cad83c0 | 262 | DBG("Device disconnected\n"); |
donatien | 4:fd826cad83c0 | 263 | onUsbDeviceDisconnected(0, 1); |
donatien | 4:fd826cad83c0 | 264 | } |
donatien | 4:fd826cad83c0 | 265 | //TODO: HUBS |
donatien | 4:fd826cad83c0 | 266 | } |
donatien | 4:fd826cad83c0 | 267 | LPC_USB->HcRhPortStatus1 = OR_RH_PORT_CSC; |
donatien | 4:fd826cad83c0 | 268 | } |
donatien | 4:fd826cad83c0 | 269 | if (LPC_USB->HcRhPortStatus1 & OR_RH_PORT_PRSC) |
donatien | 4:fd826cad83c0 | 270 | { |
donatien | 4:fd826cad83c0 | 271 | LPC_USB->HcRhPortStatus1 = OR_RH_PORT_PRSC; |
donatien | 4:fd826cad83c0 | 272 | } |
donatien | 4:fd826cad83c0 | 273 | } |
donatien | 4:fd826cad83c0 | 274 | if (int_status & OR_INTR_STATUS_WDH) /* Writeback Done Head interrupt */ |
donatien | 4:fd826cad83c0 | 275 | { |
donatien | 4:fd826cad83c0 | 276 | //UsbEndpoint::sOnCompletion((LPC_USB->HccaDoneHead) & 0xFE); |
donatien | 4:fd826cad83c0 | 277 | if(m_pHcca->DoneHead) |
donatien | 4:fd826cad83c0 | 278 | { |
donatien | 4:fd826cad83c0 | 279 | UsbEndpoint::sOnCompletion(m_pHcca->DoneHead); |
donatien | 4:fd826cad83c0 | 280 | m_pHcca->DoneHead = 0; |
donatien | 4:fd826cad83c0 | 281 | LPC_USB->HcInterruptStatus = OR_INTR_STATUS_WDH; |
donatien | 4:fd826cad83c0 | 282 | if(m_pHcca->DoneHead) |
donatien | 4:fd826cad83c0 | 283 | DBG("??????????????????????????????\n\n\n"); |
donatien | 4:fd826cad83c0 | 284 | } |
donatien | 4:fd826cad83c0 | 285 | else |
donatien | 4:fd826cad83c0 | 286 | { |
donatien | 4:fd826cad83c0 | 287 | //Probably an error |
donatien | 4:fd826cad83c0 | 288 | int_status = LPC_USB->HcInterruptStatus; |
donatien | 4:fd826cad83c0 | 289 | DBG("HcInterruptStatus = %08x\n", int_status); |
donatien | 4:fd826cad83c0 | 290 | if (int_status & OR_INTR_STATUS_UE) //Unrecoverable error, disconnect devices and resume |
donatien | 4:fd826cad83c0 | 291 | { |
donatien | 4:fd826cad83c0 | 292 | onUsbDeviceDisconnected(0, 1); |
donatien | 4:fd826cad83c0 | 293 | LPC_USB->HcInterruptStatus = OR_INTR_STATUS_UE; |
donatien | 4:fd826cad83c0 | 294 | LPC_USB->HcCommandStatus = 0x01; //Host Controller Reset |
donatien | 4:fd826cad83c0 | 295 | } |
donatien | 4:fd826cad83c0 | 296 | } |
donatien | 4:fd826cad83c0 | 297 | } |
donatien | 4:fd826cad83c0 | 298 | LPC_USB->HcInterruptStatus = int_status; /* Clear interrupt status register */ |
donatien | 4:fd826cad83c0 | 299 | } |
donatien | 4:fd826cad83c0 | 300 | return; |
donatien | 4:fd826cad83c0 | 301 | } |
donatien | 4:fd826cad83c0 | 302 | |
donatien | 4:fd826cad83c0 | 303 | void UsbHostMgr::onUsbDeviceConnected(int hub, int port) |
donatien | 4:fd826cad83c0 | 304 | { |
donatien | 4:fd826cad83c0 | 305 | int item = devicesCount(); |
donatien | 4:fd826cad83c0 | 306 | if( item == USB_HOSTMGR_MAX_DEVS ) |
donatien | 4:fd826cad83c0 | 307 | return; //List full... |
donatien | 4:fd826cad83c0 | 308 | //Find a free address (not optimized, but not really important) |
donatien | 4:fd826cad83c0 | 309 | int i; |
donatien | 4:fd826cad83c0 | 310 | int addr = 1; |
donatien | 4:fd826cad83c0 | 311 | for(i = 0; i < item; i++) |
donatien | 4:fd826cad83c0 | 312 | { |
donatien | 4:fd826cad83c0 | 313 | addr = MAX( addr, m_lpDevices[i]->m_addr + 1 ); |
donatien | 4:fd826cad83c0 | 314 | } |
donatien | 4:fd826cad83c0 | 315 | m_lpDevices[item] = new UsbDevice( this, hub, port, addr ); |
donatien | 4:fd826cad83c0 | 316 | m_lpDevices[item]->m_connected = true; |
donatien | 4:fd826cad83c0 | 317 | } |
donatien | 4:fd826cad83c0 | 318 | |
donatien | 4:fd826cad83c0 | 319 | void UsbHostMgr::onUsbDeviceDisconnected(int hub, int port) |
donatien | 4:fd826cad83c0 | 320 | { |
donatien | 4:fd826cad83c0 | 321 | for(int i = 0; i < devicesCount(); i++) |
donatien | 4:fd826cad83c0 | 322 | { |
donatien | 4:fd826cad83c0 | 323 | if( (m_lpDevices[i]->m_hub == hub) |
donatien | 4:fd826cad83c0 | 324 | && (m_lpDevices[i]->m_port == port) ) |
donatien | 4:fd826cad83c0 | 325 | { |
donatien | 4:fd826cad83c0 | 326 | m_lpDevices[i]->m_connected = false; |
donatien | 4:fd826cad83c0 | 327 | if(!m_lpDevices[i]->m_enumerated) |
donatien | 4:fd826cad83c0 | 328 | { |
donatien | 4:fd826cad83c0 | 329 | delete m_lpDevices[i]; |
donatien | 4:fd826cad83c0 | 330 | m_lpDevices[i] = NULL; |
donatien | 4:fd826cad83c0 | 331 | } |
donatien | 4:fd826cad83c0 | 332 | return; |
donatien | 4:fd826cad83c0 | 333 | } |
donatien | 4:fd826cad83c0 | 334 | } |
donatien | 4:fd826cad83c0 | 335 | } |
donatien | 4:fd826cad83c0 | 336 | |
donatien | 4:fd826cad83c0 | 337 | void UsbHostMgr::resetPort(int hub, int port) |
donatien | 4:fd826cad83c0 | 338 | { |
donatien | 4:fd826cad83c0 | 339 | DBG("Resetting hub %d, port %d\n", hub, port); |
donatien | 4:fd826cad83c0 | 340 | if(hub == 0) //Root hub |
donatien | 4:fd826cad83c0 | 341 | { |
donatien | 4:fd826cad83c0 | 342 | wait_ms(100); /* USB 2.0 spec says at least 50ms delay before port reset */ |
donatien | 4:fd826cad83c0 | 343 | LPC_USB->HcRhPortStatus1 = OR_RH_PORT_PRS; // Initiate port reset |
donatien | 4:fd826cad83c0 | 344 | DBG("Before loop\n"); |
donatien | 4:fd826cad83c0 | 345 | while (LPC_USB->HcRhPortStatus1 & OR_RH_PORT_PRS) |
donatien | 4:fd826cad83c0 | 346 | ; |
donatien | 4:fd826cad83c0 | 347 | LPC_USB->HcRhPortStatus1 = OR_RH_PORT_PRSC; // ...and clear port reset signal |
donatien | 4:fd826cad83c0 | 348 | DBG("After loop\n"); |
donatien | 4:fd826cad83c0 | 349 | wait_ms(200); /* Wait for 100 MS after port reset */ |
donatien | 4:fd826cad83c0 | 350 | } |
donatien | 4:fd826cad83c0 | 351 | else |
donatien | 4:fd826cad83c0 | 352 | { |
donatien | 4:fd826cad83c0 | 353 | //TODO: Hubs |
donatien | 4:fd826cad83c0 | 354 | } |
donatien | 4:fd826cad83c0 | 355 | DBG("Port reset OK\n"); |
donatien | 4:fd826cad83c0 | 356 | } |
donatien | 4:fd826cad83c0 | 357 | |
donatien | 4:fd826cad83c0 | 358 | #endif |