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