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