NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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