EthernetNetIf Compatibility.
Dependents: XBeeWiFi_SPI_example
Fork of NetServicesSource by
drv/usb/UsbEndpoint.cpp@4:fd826cad83c0, 2010-07-09 (annotated)
- Committer:
- donatien
- Date:
- Fri Jul 09 14:46:47 2010 +0000
- Revision:
- 4:fd826cad83c0
- Parent:
- 0:632c9925f013
- Child:
- 5:dd63a1e02b1b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:632c9925f013 | 1 | |
donatien | 0:632c9925f013 | 2 | /* |
donatien | 0:632c9925f013 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
donatien | 0:632c9925f013 | 4 | |
donatien | 0:632c9925f013 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
donatien | 0:632c9925f013 | 6 | of this software and associated documentation files (the "Software"), to deal |
donatien | 0:632c9925f013 | 7 | in the Software without restriction, including without limitation the rights |
donatien | 0:632c9925f013 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
donatien | 0:632c9925f013 | 9 | copies of the Software, and to permit persons to whom the Software is |
donatien | 0:632c9925f013 | 10 | furnished to do so, subject to the following conditions: |
donatien | 0:632c9925f013 | 11 | |
donatien | 0:632c9925f013 | 12 | The above copyright notice and this permission notice shall be included in |
donatien | 0:632c9925f013 | 13 | all copies or substantial portions of the Software. |
donatien | 0:632c9925f013 | 14 | |
donatien | 0:632c9925f013 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
donatien | 0:632c9925f013 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
donatien | 0:632c9925f013 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
donatien | 0:632c9925f013 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
donatien | 0:632c9925f013 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 0:632c9925f013 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
donatien | 0:632c9925f013 | 21 | THE SOFTWARE. |
donatien | 0:632c9925f013 | 22 | */ |
donatien | 0:632c9925f013 | 23 | |
donatien | 0:632c9925f013 | 24 | #include "UsbEndpoint.h" |
donatien | 0:632c9925f013 | 25 | |
donatien | 4:fd826cad83c0 | 26 | #include "UsbDevice.h" |
donatien | 4:fd826cad83c0 | 27 | |
donatien | 4:fd826cad83c0 | 28 | #include "usb_mem.h" |
donatien | 4:fd826cad83c0 | 29 | |
donatien | 0:632c9925f013 | 30 | #include "netCfg.h" |
donatien | 0:632c9925f013 | 31 | #if NET_USB |
donatien | 0:632c9925f013 | 32 | |
donatien | 4:fd826cad83c0 | 33 | //#define __DEBUG |
donatien | 4:fd826cad83c0 | 34 | #include "dbg/dbg.h" |
donatien | 0:632c9925f013 | 35 | |
donatien | 4:fd826cad83c0 | 36 | UsbEndpoint::UsbEndpoint( UsbDevice* pDevice, uint8_t ep, bool dir, UsbEndpointType type, uint16_t size, int addr /*= -1*/ ) : m_pDevice(pDevice), m_result(true), m_status((int)USBERR_OK), m_len(0), m_pBufStartPtr(NULL), |
donatien | 4:fd826cad83c0 | 37 | m_pCbItem(NULL), m_pCbMeth(NULL), m_pNextEp(NULL) |
donatien | 0:632c9925f013 | 38 | { |
donatien | 4:fd826cad83c0 | 39 | //Insert into Eps list |
donatien | 4:fd826cad83c0 | 40 | //FIXME: Assert that no USB interrupt is triggered meanwhile |
donatien | 4:fd826cad83c0 | 41 | if(m_pHeadEp) |
donatien | 0:632c9925f013 | 42 | { |
donatien | 4:fd826cad83c0 | 43 | m_pNextEp = m_pHeadEp; |
donatien | 4:fd826cad83c0 | 44 | m_pHeadEp = this; |
donatien | 4:fd826cad83c0 | 45 | } |
donatien | 4:fd826cad83c0 | 46 | else |
donatien | 4:fd826cad83c0 | 47 | { |
donatien | 4:fd826cad83c0 | 48 | m_pNextEp = NULL; |
donatien | 4:fd826cad83c0 | 49 | m_pHeadEp = this; |
donatien | 4:fd826cad83c0 | 50 | } |
donatien | 0:632c9925f013 | 51 | |
donatien | 4:fd826cad83c0 | 52 | m_pEd = (volatile HCED*)usb_get_ed(); |
donatien | 0:632c9925f013 | 53 | |
donatien | 4:fd826cad83c0 | 54 | m_pTdHead = (volatile HCTD*)usb_get_td(); |
donatien | 4:fd826cad83c0 | 55 | m_pTdTail = (volatile HCTD*)usb_get_td(); |
donatien | 0:632c9925f013 | 56 | |
donatien | 4:fd826cad83c0 | 57 | //printf("\r\n--m_pEd = %p--\r\n", m_pEd); |
donatien | 0:632c9925f013 | 58 | |
donatien | 0:632c9925f013 | 59 | //Init Ed & Td |
donatien | 4:fd826cad83c0 | 60 | //printf("\r\n--Ep Init--\r\n"); |
donatien | 4:fd826cad83c0 | 61 | memset((void*)m_pEd, 0, sizeof(HCED)); |
donatien | 4:fd826cad83c0 | 62 | //printf("\r\n--Td Init--\r\n"); |
donatien | 4:fd826cad83c0 | 63 | |
donatien | 4:fd826cad83c0 | 64 | memset((void*)m_pTdHead, 0, sizeof(HCTD)); |
donatien | 4:fd826cad83c0 | 65 | memset((void*)m_pTdTail, 0, sizeof(HCTD)); |
donatien | 4:fd826cad83c0 | 66 | |
donatien | 4:fd826cad83c0 | 67 | if(addr == -1) |
donatien | 4:fd826cad83c0 | 68 | addr = pDevice->m_addr; |
donatien | 0:632c9925f013 | 69 | |
donatien | 0:632c9925f013 | 70 | //Setup Ed |
donatien | 4:fd826cad83c0 | 71 | //printf("\r\n--Ep Setup--\r\n"); |
donatien | 4:fd826cad83c0 | 72 | m_pEd->Control = addr | /* USB address */ |
donatien | 0:632c9925f013 | 73 | ((ep & 0x7F) << 7) | /* Endpoint address */ |
donatien | 4:fd826cad83c0 | 74 | (type!=USB_CONTROL?((dir?2:1) << 11):0) | /* direction : Out = 1, 2 = In */ |
donatien | 0:632c9925f013 | 75 | (size << 16); /* MaxPkt Size */ |
donatien | 0:632c9925f013 | 76 | |
donatien | 0:632c9925f013 | 77 | m_dir = dir; |
donatien | 4:fd826cad83c0 | 78 | m_setup = false; |
donatien | 4:fd826cad83c0 | 79 | m_type = type; |
donatien | 0:632c9925f013 | 80 | |
donatien | 4:fd826cad83c0 | 81 | m_pEd->TailTd = m_pEd->HeadTd = (uint32_t) m_pTdTail; //Empty TD list |
donatien | 4:fd826cad83c0 | 82 | |
donatien | 4:fd826cad83c0 | 83 | DBG("Before link\n"); |
donatien | 0:632c9925f013 | 84 | |
donatien | 4:fd826cad83c0 | 85 | //printf("\r\n--Ep Reg--\r\n"); |
donatien | 4:fd826cad83c0 | 86 | //Append Ed to Ed list |
donatien | 4:fd826cad83c0 | 87 | volatile HCED* prevEd; |
donatien | 4:fd826cad83c0 | 88 | switch( m_type ) |
donatien | 0:632c9925f013 | 89 | { |
donatien | 4:fd826cad83c0 | 90 | case USB_CONTROL: |
donatien | 4:fd826cad83c0 | 91 | prevEd = (volatile HCED*) LPC_USB->HcControlHeadED; |
donatien | 4:fd826cad83c0 | 92 | break; |
donatien | 4:fd826cad83c0 | 93 | case USB_BULK: |
donatien | 4:fd826cad83c0 | 94 | default: |
donatien | 4:fd826cad83c0 | 95 | prevEd = (volatile HCED*) LPC_USB->HcBulkHeadED; |
donatien | 4:fd826cad83c0 | 96 | break; |
donatien | 4:fd826cad83c0 | 97 | } |
donatien | 4:fd826cad83c0 | 98 | |
donatien | 4:fd826cad83c0 | 99 | DBG("prevEd is %p\n", prevEd); |
donatien | 4:fd826cad83c0 | 100 | |
donatien | 4:fd826cad83c0 | 101 | if(prevEd) |
donatien | 4:fd826cad83c0 | 102 | { |
donatien | 4:fd826cad83c0 | 103 | DBG("prevEd set\n") |
donatien | 4:fd826cad83c0 | 104 | |
donatien | 4:fd826cad83c0 | 105 | while(prevEd->Next) |
donatien | 4:fd826cad83c0 | 106 | { |
donatien | 4:fd826cad83c0 | 107 | DBG("prevEd->Next = %08x\n", prevEd->Next); |
donatien | 4:fd826cad83c0 | 108 | prevEd = (volatile HCED*) prevEd->Next; |
donatien | 4:fd826cad83c0 | 109 | } |
donatien | 4:fd826cad83c0 | 110 | prevEd->Next = (uint32_t) m_pEd; |
donatien | 4:fd826cad83c0 | 111 | |
donatien | 0:632c9925f013 | 112 | } |
donatien | 0:632c9925f013 | 113 | else |
donatien | 0:632c9925f013 | 114 | { |
donatien | 4:fd826cad83c0 | 115 | switch( m_type ) |
donatien | 0:632c9925f013 | 116 | { |
donatien | 4:fd826cad83c0 | 117 | case USB_CONTROL: |
donatien | 4:fd826cad83c0 | 118 | LPC_USB->HcControlHeadED = (uint32_t) m_pEd; |
donatien | 4:fd826cad83c0 | 119 | break; |
donatien | 4:fd826cad83c0 | 120 | case USB_BULK: |
donatien | 4:fd826cad83c0 | 121 | default: |
donatien | 4:fd826cad83c0 | 122 | LPC_USB->HcBulkHeadED = (uint32_t) m_pEd; |
donatien | 4:fd826cad83c0 | 123 | break; |
donatien | 0:632c9925f013 | 124 | } |
donatien | 0:632c9925f013 | 125 | } |
donatien | 4:fd826cad83c0 | 126 | |
donatien | 4:fd826cad83c0 | 127 | DBG("Ep init\n"); |
donatien | 0:632c9925f013 | 128 | } |
donatien | 0:632c9925f013 | 129 | |
donatien | 0:632c9925f013 | 130 | UsbEndpoint::~UsbEndpoint() |
donatien | 0:632c9925f013 | 131 | { |
donatien | 4:fd826cad83c0 | 132 | //Remove from Eps list |
donatien | 4:fd826cad83c0 | 133 | //FIXME: Assert that no USB interrupt is triggered meanwhile |
donatien | 4:fd826cad83c0 | 134 | if(m_pHeadEp != this) |
donatien | 4:fd826cad83c0 | 135 | { |
donatien | 4:fd826cad83c0 | 136 | UsbEndpoint* prevEp = m_pHeadEp; |
donatien | 4:fd826cad83c0 | 137 | while(prevEp->m_pNextEp != this) |
donatien | 4:fd826cad83c0 | 138 | prevEp = prevEp->m_pNextEp; |
donatien | 4:fd826cad83c0 | 139 | prevEp->m_pNextEp = m_pNextEp; |
donatien | 4:fd826cad83c0 | 140 | } |
donatien | 4:fd826cad83c0 | 141 | else |
donatien | 4:fd826cad83c0 | 142 | { |
donatien | 4:fd826cad83c0 | 143 | m_pHeadEp = m_pNextEp; |
donatien | 4:fd826cad83c0 | 144 | } |
donatien | 4:fd826cad83c0 | 145 | |
donatien | 0:632c9925f013 | 146 | m_pEd->Control |= ED_SKIP; //Skip this Ep in queue |
donatien | 0:632c9925f013 | 147 | |
donatien | 0:632c9925f013 | 148 | //Remove from queue |
donatien | 4:fd826cad83c0 | 149 | volatile HCED* prevEd; |
donatien | 4:fd826cad83c0 | 150 | switch( m_type ) |
donatien | 0:632c9925f013 | 151 | { |
donatien | 4:fd826cad83c0 | 152 | case USB_CONTROL: |
donatien | 4:fd826cad83c0 | 153 | prevEd = (volatile HCED*) LPC_USB->HcControlHeadED; |
donatien | 4:fd826cad83c0 | 154 | break; |
donatien | 4:fd826cad83c0 | 155 | case USB_BULK: |
donatien | 4:fd826cad83c0 | 156 | default: |
donatien | 4:fd826cad83c0 | 157 | prevEd = (volatile HCED*) LPC_USB->HcBulkHeadED; |
donatien | 4:fd826cad83c0 | 158 | break; |
donatien | 4:fd826cad83c0 | 159 | } |
donatien | 4:fd826cad83c0 | 160 | if( m_pEd == prevEd ) |
donatien | 4:fd826cad83c0 | 161 | { |
donatien | 4:fd826cad83c0 | 162 | switch( m_type ) |
donatien | 4:fd826cad83c0 | 163 | { |
donatien | 4:fd826cad83c0 | 164 | case USB_CONTROL: |
donatien | 4:fd826cad83c0 | 165 | LPC_USB->HcControlHeadED = m_pEd->Next; |
donatien | 4:fd826cad83c0 | 166 | break; |
donatien | 4:fd826cad83c0 | 167 | case USB_BULK: |
donatien | 4:fd826cad83c0 | 168 | default: |
donatien | 4:fd826cad83c0 | 169 | LPC_USB->HcBulkHeadED = m_pEd->Next; |
donatien | 4:fd826cad83c0 | 170 | break; |
donatien | 4:fd826cad83c0 | 171 | } |
donatien | 0:632c9925f013 | 172 | LPC_USB->HcBulkHeadED = m_pEd->Next; |
donatien | 0:632c9925f013 | 173 | } |
donatien | 0:632c9925f013 | 174 | else |
donatien | 0:632c9925f013 | 175 | { |
donatien | 4:fd826cad83c0 | 176 | while( prevEd->Next != (uint32_t) m_pEd ) |
donatien | 0:632c9925f013 | 177 | { |
donatien | 0:632c9925f013 | 178 | prevEd = (volatile HCED*) prevEd->Next; |
donatien | 0:632c9925f013 | 179 | } |
donatien | 0:632c9925f013 | 180 | prevEd->Next = m_pEd->Next; |
donatien | 0:632c9925f013 | 181 | } |
donatien | 0:632c9925f013 | 182 | |
donatien | 4:fd826cad83c0 | 183 | // |
donatien | 4:fd826cad83c0 | 184 | usb_free_ed((volatile byte*)m_pEd); |
donatien | 0:632c9925f013 | 185 | |
donatien | 4:fd826cad83c0 | 186 | usb_free_td((volatile byte*)m_pTdHead); |
donatien | 4:fd826cad83c0 | 187 | usb_free_td((volatile byte*)m_pTdTail); |
donatien | 4:fd826cad83c0 | 188 | } |
donatien | 4:fd826cad83c0 | 189 | |
donatien | 4:fd826cad83c0 | 190 | void UsbEndpoint::setNextToken(uint32_t token) //Only for control Eps |
donatien | 4:fd826cad83c0 | 191 | { |
donatien | 4:fd826cad83c0 | 192 | switch(token) |
donatien | 4:fd826cad83c0 | 193 | { |
donatien | 4:fd826cad83c0 | 194 | case TD_SETUP: |
donatien | 4:fd826cad83c0 | 195 | m_dir = false; |
donatien | 4:fd826cad83c0 | 196 | m_setup = true; |
donatien | 4:fd826cad83c0 | 197 | break; |
donatien | 4:fd826cad83c0 | 198 | case TD_IN: |
donatien | 4:fd826cad83c0 | 199 | m_dir = true; |
donatien | 4:fd826cad83c0 | 200 | m_setup = false; |
donatien | 4:fd826cad83c0 | 201 | break; |
donatien | 4:fd826cad83c0 | 202 | case TD_OUT: |
donatien | 4:fd826cad83c0 | 203 | m_dir = false; |
donatien | 4:fd826cad83c0 | 204 | m_setup = false; |
donatien | 4:fd826cad83c0 | 205 | break; |
donatien | 4:fd826cad83c0 | 206 | } |
donatien | 0:632c9925f013 | 207 | } |
donatien | 0:632c9925f013 | 208 | |
donatien | 4:fd826cad83c0 | 209 | UsbErr UsbEndpoint::transfer(volatile uint8_t* buf, uint32_t len) |
donatien | 0:632c9925f013 | 210 | { |
donatien | 4:fd826cad83c0 | 211 | if(!m_result) |
donatien | 4:fd826cad83c0 | 212 | return USBERR_BUSY; //The previous trasnfer is not completed |
donatien | 4:fd826cad83c0 | 213 | //FIXME: We should be able to queue the next transfer, still needs to be implemented |
donatien | 4:fd826cad83c0 | 214 | |
donatien | 4:fd826cad83c0 | 215 | if( !m_pDevice->connected() ) |
donatien | 4:fd826cad83c0 | 216 | return USBERR_DISCONNECTED; |
donatien | 4:fd826cad83c0 | 217 | |
donatien | 4:fd826cad83c0 | 218 | m_result = false; |
donatien | 4:fd826cad83c0 | 219 | |
donatien | 4:fd826cad83c0 | 220 | volatile uint32_t token = (m_setup?TD_SETUP:(m_dir?TD_IN:TD_OUT)); |
donatien | 4:fd826cad83c0 | 221 | |
donatien | 4:fd826cad83c0 | 222 | volatile uint32_t td_toggle; |
donatien | 4:fd826cad83c0 | 223 | if (m_type == USB_CONTROL) |
donatien | 4:fd826cad83c0 | 224 | { |
donatien | 4:fd826cad83c0 | 225 | if (m_setup) |
donatien | 4:fd826cad83c0 | 226 | { |
donatien | 4:fd826cad83c0 | 227 | td_toggle = TD_TOGGLE_0; |
donatien | 4:fd826cad83c0 | 228 | } |
donatien | 4:fd826cad83c0 | 229 | else |
donatien | 4:fd826cad83c0 | 230 | { |
donatien | 4:fd826cad83c0 | 231 | td_toggle = TD_TOGGLE_1; |
donatien | 4:fd826cad83c0 | 232 | } |
donatien | 4:fd826cad83c0 | 233 | } |
donatien | 4:fd826cad83c0 | 234 | else |
donatien | 4:fd826cad83c0 | 235 | { |
donatien | 4:fd826cad83c0 | 236 | td_toggle = 0; |
donatien | 4:fd826cad83c0 | 237 | } |
donatien | 0:632c9925f013 | 238 | |
donatien | 0:632c9925f013 | 239 | //Swap Tds |
donatien | 0:632c9925f013 | 240 | volatile HCTD* pTdSwap; |
donatien | 0:632c9925f013 | 241 | pTdSwap = m_pTdTail; |
donatien | 0:632c9925f013 | 242 | m_pTdTail = m_pTdHead; |
donatien | 0:632c9925f013 | 243 | m_pTdHead = pTdSwap; |
donatien | 4:fd826cad83c0 | 244 | |
donatien | 4:fd826cad83c0 | 245 | m_pTdHead->Control = (TD_ROUNDING | |
donatien | 4:fd826cad83c0 | 246 | token | |
donatien | 4:fd826cad83c0 | 247 | TD_DELAY_INT(0) |//7 |
donatien | 4:fd826cad83c0 | 248 | td_toggle | |
donatien | 4:fd826cad83c0 | 249 | TD_CC); |
donatien | 0:632c9925f013 | 250 | |
donatien | 4:fd826cad83c0 | 251 | m_pTdTail->Control = 0; |
donatien | 4:fd826cad83c0 | 252 | m_pTdHead->CurrBufPtr = (uint32_t) buf; |
donatien | 4:fd826cad83c0 | 253 | m_pBufStartPtr = buf; |
donatien | 4:fd826cad83c0 | 254 | m_pTdTail->CurrBufPtr = 0; |
donatien | 4:fd826cad83c0 | 255 | m_pTdHead->Next = (uint32_t) m_pTdTail; |
donatien | 4:fd826cad83c0 | 256 | m_pTdTail->Next = 0; |
donatien | 4:fd826cad83c0 | 257 | m_pTdHead->BufEnd = (uint32_t)(buf + (len - 1)); |
donatien | 4:fd826cad83c0 | 258 | m_pTdTail->BufEnd = 0; |
donatien | 0:632c9925f013 | 259 | |
donatien | 4:fd826cad83c0 | 260 | m_pEd->HeadTd = (uint32_t)m_pTdHead | ((m_pEd->HeadTd) & 0x00000002); //Carry bit |
donatien | 4:fd826cad83c0 | 261 | m_pEd->TailTd = (uint32_t)m_pTdTail; |
donatien | 0:632c9925f013 | 262 | |
donatien | 4:fd826cad83c0 | 263 | //DBG("m_pEd->HeadTd = %08x\n", m_pEd->HeadTd); |
donatien | 4:fd826cad83c0 | 264 | |
donatien | 4:fd826cad83c0 | 265 | if(m_type == USB_CONTROL) |
donatien | 4:fd826cad83c0 | 266 | { |
donatien | 4:fd826cad83c0 | 267 | LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | OR_CMD_STATUS_CLF; |
donatien | 4:fd826cad83c0 | 268 | LPC_USB->HcControl = LPC_USB->HcControl | OR_CONTROL_CLE; //Enable control list |
donatien | 4:fd826cad83c0 | 269 | } |
donatien | 4:fd826cad83c0 | 270 | else //USB_BULK |
donatien | 4:fd826cad83c0 | 271 | { |
donatien | 4:fd826cad83c0 | 272 | LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | OR_CMD_STATUS_BLF; |
donatien | 4:fd826cad83c0 | 273 | LPC_USB->HcControl = LPC_USB->HcControl | OR_CONTROL_BLE; //Enable bulk list |
donatien | 4:fd826cad83c0 | 274 | } |
donatien | 4:fd826cad83c0 | 275 | |
donatien | 4:fd826cad83c0 | 276 | //m_done = false; |
donatien | 0:632c9925f013 | 277 | m_len = len; |
donatien | 4:fd826cad83c0 | 278 | |
donatien | 4:fd826cad83c0 | 279 | return USBERR_PROCESSING; |
donatien | 0:632c9925f013 | 280 | |
donatien | 0:632c9925f013 | 281 | } |
donatien | 0:632c9925f013 | 282 | |
donatien | 4:fd826cad83c0 | 283 | int UsbEndpoint::status() |
donatien | 0:632c9925f013 | 284 | { |
donatien | 4:fd826cad83c0 | 285 | if( !m_pDevice->connected() ) |
donatien | 0:632c9925f013 | 286 | { |
donatien | 4:fd826cad83c0 | 287 | if(!m_result) |
donatien | 4:fd826cad83c0 | 288 | onCompletion(); |
donatien | 4:fd826cad83c0 | 289 | m_result = true; |
donatien | 4:fd826cad83c0 | 290 | return (int)USBERR_DISCONNECTED; |
donatien | 0:632c9925f013 | 291 | } |
donatien | 4:fd826cad83c0 | 292 | else if( !m_result ) |
donatien | 4:fd826cad83c0 | 293 | { |
donatien | 4:fd826cad83c0 | 294 | return (int)USBERR_PROCESSING; |
donatien | 4:fd826cad83c0 | 295 | } |
donatien | 4:fd826cad83c0 | 296 | /*else if( m_done ) |
donatien | 4:fd826cad83c0 | 297 | { |
donatien | 4:fd826cad83c0 | 298 | return (int)USBERR_OK; |
donatien | 4:fd826cad83c0 | 299 | }*/ |
donatien | 4:fd826cad83c0 | 300 | else |
donatien | 0:632c9925f013 | 301 | { |
donatien | 4:fd826cad83c0 | 302 | return m_status; |
donatien | 4:fd826cad83c0 | 303 | } |
donatien | 4:fd826cad83c0 | 304 | } |
donatien | 4:fd826cad83c0 | 305 | |
donatien | 4:fd826cad83c0 | 306 | void UsbEndpoint::updateAddr(int addr) |
donatien | 4:fd826cad83c0 | 307 | { |
donatien | 4:fd826cad83c0 | 308 | DBG("m_pEd->Control = %08x\n", m_pEd->Control); |
donatien | 4:fd826cad83c0 | 309 | m_pEd->Control &= ~0x7F; |
donatien | 4:fd826cad83c0 | 310 | m_pEd->Control |= addr; |
donatien | 4:fd826cad83c0 | 311 | DBG("m_pEd->Control = %08x\n", m_pEd->Control); |
donatien | 4:fd826cad83c0 | 312 | } |
donatien | 4:fd826cad83c0 | 313 | |
donatien | 4:fd826cad83c0 | 314 | void UsbEndpoint::updateSize(uint16_t size) |
donatien | 4:fd826cad83c0 | 315 | { |
donatien | 4:fd826cad83c0 | 316 | DBG("m_pEd->Control = %08x\n", m_pEd->Control); |
donatien | 4:fd826cad83c0 | 317 | m_pEd->Control &= ~0x3FF0000; |
donatien | 4:fd826cad83c0 | 318 | m_pEd->Control |= (size << 16); |
donatien | 4:fd826cad83c0 | 319 | DBG("m_pEd->Control = %08x\n", m_pEd->Control); |
donatien | 4:fd826cad83c0 | 320 | } |
donatien | 4:fd826cad83c0 | 321 | |
donatien | 4:fd826cad83c0 | 322 | #if 0 //For doc only |
donatien | 4:fd826cad83c0 | 323 | template <class T> |
donatien | 4:fd826cad83c0 | 324 | void UsbEndpoint::setOnCompletion( T* pCbItem, void (T::*pCbMeth)() ) |
donatien | 4:fd826cad83c0 | 325 | { |
donatien | 4:fd826cad83c0 | 326 | m_pCbItem = (CDummy*) pCbItem; |
donatien | 4:fd826cad83c0 | 327 | m_pCbMeth = (void (CDummy::*)()) pCbMeth; |
donatien | 4:fd826cad83c0 | 328 | } |
donatien | 4:fd826cad83c0 | 329 | #endif |
donatien | 4:fd826cad83c0 | 330 | |
donatien | 4:fd826cad83c0 | 331 | void UsbEndpoint::onCompletion() |
donatien | 4:fd826cad83c0 | 332 | { |
donatien | 4:fd826cad83c0 | 333 | DBG("Transfer completed\n"); |
donatien | 4:fd826cad83c0 | 334 | if( m_pTdHead->Control >> 28 ) |
donatien | 4:fd826cad83c0 | 335 | { |
donatien | 4:fd826cad83c0 | 336 | DBG("\r\nTD Failed with condition code %01x\r\n", m_pTdHead->Control >> 28 ); |
donatien | 4:fd826cad83c0 | 337 | m_status = (int)USBERR_TDFAIL; |
donatien | 0:632c9925f013 | 338 | } |
donatien | 0:632c9925f013 | 339 | else if( m_pEd->HeadTd & 0x1 ) |
donatien | 0:632c9925f013 | 340 | { |
donatien | 4:fd826cad83c0 | 341 | m_pEd->HeadTd = m_pEd->HeadTd & ~0x1; |
donatien | 4:fd826cad83c0 | 342 | DBG("\r\nHALTED!!\r\n"); |
donatien | 4:fd826cad83c0 | 343 | m_status = (int)USBERR_HALTED; |
donatien | 4:fd826cad83c0 | 344 | } |
donatien | 4:fd826cad83c0 | 345 | else if( (m_pEd->HeadTd & ~0xF) == (uint32_t) m_pTdTail ) |
donatien | 4:fd826cad83c0 | 346 | { |
donatien | 4:fd826cad83c0 | 347 | //Done |
donatien | 4:fd826cad83c0 | 348 | int len; |
donatien | 4:fd826cad83c0 | 349 | DBG("m_pTdHead->CurrBufPtr = %08x, m_pBufStartPtr=%08x\n", m_pTdHead->CurrBufPtr, (uint32_t) m_pBufStartPtr); |
donatien | 4:fd826cad83c0 | 350 | if(m_pTdHead->CurrBufPtr) |
donatien | 4:fd826cad83c0 | 351 | len = m_pTdHead->CurrBufPtr - (uint32_t) m_pBufStartPtr; |
donatien | 4:fd826cad83c0 | 352 | else |
donatien | 4:fd826cad83c0 | 353 | len = m_len; |
donatien | 4:fd826cad83c0 | 354 | /*if(len == 0) //Packet transfered completely |
donatien | 4:fd826cad83c0 | 355 | len = m_len;*/ |
donatien | 4:fd826cad83c0 | 356 | //m_done = true; |
donatien | 4:fd826cad83c0 | 357 | DBG("Transfered %d bytes\n", len); |
donatien | 4:fd826cad83c0 | 358 | m_status = len; |
donatien | 0:632c9925f013 | 359 | } |
donatien | 0:632c9925f013 | 360 | else |
donatien | 4:fd826cad83c0 | 361 | { |
donatien | 4:fd826cad83c0 | 362 | DBG("\r\nUnknown error...\r\n"); |
donatien | 4:fd826cad83c0 | 363 | m_status = (int)USBERR_ERROR; |
donatien | 0:632c9925f013 | 364 | } |
donatien | 4:fd826cad83c0 | 365 | m_result = true; |
donatien | 4:fd826cad83c0 | 366 | if(m_pCbItem && m_pCbMeth) |
donatien | 4:fd826cad83c0 | 367 | (m_pCbItem->*m_pCbMeth)(); |
donatien | 0:632c9925f013 | 368 | } |
donatien | 0:632c9925f013 | 369 | |
donatien | 4:fd826cad83c0 | 370 | void UsbEndpoint::sOnCompletion(uint32_t pTd) |
donatien | 4:fd826cad83c0 | 371 | { |
donatien | 4:fd826cad83c0 | 372 | if(!m_pHeadEp) |
donatien | 4:fd826cad83c0 | 373 | return; |
donatien | 4:fd826cad83c0 | 374 | do |
donatien | 4:fd826cad83c0 | 375 | { |
donatien | 4:fd826cad83c0 | 376 | DBG("sOnCompletion (pTd = %08x)\n", pTd); |
donatien | 4:fd826cad83c0 | 377 | UsbEndpoint* pEp = m_pHeadEp; |
donatien | 4:fd826cad83c0 | 378 | do |
donatien | 4:fd826cad83c0 | 379 | { |
donatien | 4:fd826cad83c0 | 380 | if((uint32_t)pEp->m_pTdHead == pTd) |
donatien | 4:fd826cad83c0 | 381 | { |
donatien | 4:fd826cad83c0 | 382 | pEp->onCompletion(); |
donatien | 4:fd826cad83c0 | 383 | break; |
donatien | 4:fd826cad83c0 | 384 | } |
donatien | 4:fd826cad83c0 | 385 | } while(pEp = pEp->m_pNextEp); |
donatien | 4:fd826cad83c0 | 386 | } while( pTd = (uint32_t)( ((HCTD*)pTd)->Next ) ); //Go around the Done queue |
donatien | 4:fd826cad83c0 | 387 | } |
donatien | 4:fd826cad83c0 | 388 | |
donatien | 4:fd826cad83c0 | 389 | UsbEndpoint* UsbEndpoint::m_pHeadEp = NULL; |
donatien | 4:fd826cad83c0 | 390 | |
donatien | 0:632c9925f013 | 391 | #endif |