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