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