EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Fri Jun 11 16:05:15 2010 +0000
Revision:
0:632c9925f013
Child:
4:fd826cad83c0

        

Who changed what in which revision?

UserRevisionLine numberNew 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 0:632c9925f013 26 #include "netCfg.h"
donatien 0:632c9925f013 27 #if NET_USB
donatien 0:632c9925f013 28
donatien 0:632c9925f013 29 EdPool::EdPool(int size) : m_pos(0)
donatien 0:632c9925f013 30 {
donatien 0:632c9925f013 31 m_pool = new uint8_t[2*size*0x10];
donatien 0:632c9925f013 32 printf("\r\n--HCED* m_pool= %p of size %d--\r\n", m_pool, 2*size*0x10);
donatien 0:632c9925f013 33 }
donatien 0:632c9925f013 34
donatien 0:632c9925f013 35 EdPool::~EdPool()
donatien 0:632c9925f013 36 {
donatien 0:632c9925f013 37 printf("\r\n--~EdPool()--\r\n");
donatien 0:632c9925f013 38 delete[] m_pool;
donatien 0:632c9925f013 39 }
donatien 0:632c9925f013 40
donatien 0:632c9925f013 41 HCED* EdPool::get()
donatien 0:632c9925f013 42 {
donatien 0:632c9925f013 43 printf("\r\n--HCED* &m_pool[m_pos]= %p--\r\n", &m_pool[m_pos]);
donatien 0:632c9925f013 44 while( ( ((uint32_t)&m_pool[m_pos]) & 0xF ) != 0 )
donatien 0:632c9925f013 45 {
donatien 0:632c9925f013 46 m_pos++;
donatien 0:632c9925f013 47 }
donatien 0:632c9925f013 48 HCED* pEd = (HCED*) &m_pool[m_pos];
donatien 0:632c9925f013 49 m_pos += sizeof(HCED);
donatien 0:632c9925f013 50 printf("\r\n--HCED* pEd = %p--\r\n", pEd);
donatien 0:632c9925f013 51 return pEd;
donatien 0:632c9925f013 52 }
donatien 0:632c9925f013 53
donatien 0:632c9925f013 54 TdPool::TdPool(int size) : m_pos(0)
donatien 0:632c9925f013 55 {
donatien 0:632c9925f013 56 m_pool = new uint8_t[2*size*0x10];
donatien 0:632c9925f013 57 printf("\r\n--HCTD* m_pool= %p of size %d--\r\n", m_pool, 2*size*0x10);
donatien 0:632c9925f013 58 }
donatien 0:632c9925f013 59
donatien 0:632c9925f013 60 TdPool::~TdPool()
donatien 0:632c9925f013 61 {
donatien 0:632c9925f013 62 printf("\r\n--~EdPool()--\r\n");
donatien 0:632c9925f013 63 delete[] m_pool;
donatien 0:632c9925f013 64 }
donatien 0:632c9925f013 65
donatien 0:632c9925f013 66 HCTD* TdPool::get()
donatien 0:632c9925f013 67 {
donatien 0:632c9925f013 68 printf("\r\n--HCTD* &m_pool[m_pos]= %p--\r\n", &m_pool[m_pos]);
donatien 0:632c9925f013 69 while( ( ((uint32_t)&m_pool[m_pos]) & 0xF ) != 0 )
donatien 0:632c9925f013 70 {
donatien 0:632c9925f013 71 m_pos++;
donatien 0:632c9925f013 72 }
donatien 0:632c9925f013 73 HCTD* pTd = (HCTD*) &m_pool[m_pos];
donatien 0:632c9925f013 74 m_pos += sizeof(HCTD);
donatien 0:632c9925f013 75 printf("\r\n--HCTD* pTd = %p--\r\n", pTd);
donatien 0:632c9925f013 76 return pTd;
donatien 0:632c9925f013 77 }
donatien 0:632c9925f013 78 //uint8_t TdPool::pool[16*0x10]={0};//Ok for 2 Eps (2*(2Tds/Ep*2Eps))
donatien 0:632c9925f013 79 //int TdPool::pos = 0;
donatien 0:632c9925f013 80
donatien 0:632c9925f013 81 extern volatile HCED* EDBulkHead;
donatien 0:632c9925f013 82 //volatile HCED* UsbEndpoint::m_pNextEd = NULL;
donatien 0:632c9925f013 83
donatien 0:632c9925f013 84 UsbEndpoint::UsbEndpoint( /* UsbDevice*, */ uint8_t ep, bool dir, uint16_t size ) : m_edPool(1), m_tdPool(2), m_done(true), m_len(0), m_pBufStartPtr(NULL)
donatien 0:632c9925f013 85 {
donatien 0:632c9925f013 86 #if 1
donatien 0:632c9925f013 87
donatien 0:632c9925f013 88 m_pEd = m_edPool.get();
donatien 0:632c9925f013 89
donatien 0:632c9925f013 90 #else
donatien 0:632c9925f013 91 if(m_pNextEd == NULL)
donatien 0:632c9925f013 92 {
donatien 0:632c9925f013 93 m_pNextEd = EDBulkHead;
donatien 0:632c9925f013 94 //printf("\r\n--m_pEd = %p < EDBulkHead = %p --\r\n", m_pEd, EDBulkHead);
donatien 0:632c9925f013 95 }
donatien 0:632c9925f013 96
donatien 0:632c9925f013 97 printf("\r\n--Ep Inst--\r\n");
donatien 0:632c9925f013 98 m_pEd = /*new HCED(); // */m_pNextEd;
donatien 0:632c9925f013 99
donatien 0:632c9925f013 100 //printf("\r\n--m_pNextEd = %p--\r\n", m_pNextEd);
donatien 0:632c9925f013 101 m_pNextEd += 1; //TODO: Avail mem check
donatien 0:632c9925f013 102 //printf("\r\n--m_pNextEd = %p--\r\n", m_pNextEd);
donatien 0:632c9925f013 103 #endif
donatien 0:632c9925f013 104
donatien 0:632c9925f013 105 m_pTdHead = m_tdPool.get();//new HCTD();
donatien 0:632c9925f013 106 m_pTdTail = m_tdPool.get();//new HCTD();
donatien 0:632c9925f013 107
donatien 0:632c9925f013 108 printf("\r\n--m_pEd = %p--\r\n", m_pEd);
donatien 0:632c9925f013 109
donatien 0:632c9925f013 110 //Init Ed & Td
donatien 0:632c9925f013 111 printf("\r\n--Ep Init--\r\n");
donatien 0:632c9925f013 112 Host_EDInit(m_pEd);
donatien 0:632c9925f013 113 printf("\r\n--Td Init--\r\n");
donatien 0:632c9925f013 114 Host_TDInit(m_pTdHead);
donatien 0:632c9925f013 115 Host_TDInit(m_pTdTail);
donatien 0:632c9925f013 116
donatien 0:632c9925f013 117 //Setup Ed
donatien 0:632c9925f013 118 printf("\r\n--Ep Setup--\r\n");
donatien 0:632c9925f013 119 m_pEd->Control = 1 | /* USB address */ //FIXME: Device Id
donatien 0:632c9925f013 120 ((ep & 0x7F) << 7) | /* Endpoint address */
donatien 0:632c9925f013 121 ((dir?2:1) << 11) | /* direction : Out = 1, 2 = In */
donatien 0:632c9925f013 122 (size << 16); /* MaxPkt Size */
donatien 0:632c9925f013 123
donatien 0:632c9925f013 124 m_dir = dir;
donatien 0:632c9925f013 125
donatien 0:632c9925f013 126 m_pEd->TailTd = m_pEd->HeadTd = (USB_INT32U) m_pTdTail; //Empty TD list
donatien 0:632c9925f013 127
donatien 0:632c9925f013 128 printf("\r\n--Ep Reg--\r\n");
donatien 0:632c9925f013 129 //Append Ed to bulk Ep list
donatien 0:632c9925f013 130 #if 1
donatien 0:632c9925f013 131 volatile HCED* prevEd = (volatile HCED*) LPC_USB->HcBulkHeadED;
donatien 0:632c9925f013 132 m_pEd->Next = (USB_INT32U) prevEd;
donatien 0:632c9925f013 133 LPC_USB->HcBulkHeadED = (USB_INT32U) m_pEd;
donatien 0:632c9925f013 134 #else
donatien 0:632c9925f013 135 if( LPC_USB->HcBulkHeadED == 0 )
donatien 0:632c9925f013 136 {
donatien 0:632c9925f013 137 LPC_USB->HcBulkHeadED = (USB_INT32U)m_pEd;
donatien 0:632c9925f013 138 printf("\r\n--Bulk head--\r\n");
donatien 0:632c9925f013 139 }
donatien 0:632c9925f013 140 else
donatien 0:632c9925f013 141 {
donatien 0:632c9925f013 142 volatile HCED* prevEd = (volatile HCED*) LPC_USB->HcBulkHeadED;
donatien 0:632c9925f013 143 while(prevEd->Next)
donatien 0:632c9925f013 144 {
donatien 0:632c9925f013 145 prevEd = (volatile HCED*) prevEd->Next;
donatien 0:632c9925f013 146 }
donatien 0:632c9925f013 147 printf("\r\n--Appended to ep list (prevEd = %p)--\r\n", prevEd);
donatien 0:632c9925f013 148 prevEd->Next = (USB_INT32U) m_pEd;
donatien 0:632c9925f013 149 }
donatien 0:632c9925f013 150 #endif
donatien 0:632c9925f013 151 /*
donatien 0:632c9925f013 152 printf("\r\n--Writing config reg--\r\n");
donatien 0:632c9925f013 153 LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | OR_CMD_STATUS_BLF;
donatien 0:632c9925f013 154 LPC_USB->HcControl = LPC_USB->HcControl | OR_CONTROL_BLE;
donatien 0:632c9925f013 155 */
donatien 0:632c9925f013 156 }
donatien 0:632c9925f013 157
donatien 0:632c9925f013 158 UsbEndpoint::~UsbEndpoint()
donatien 0:632c9925f013 159 {
donatien 0:632c9925f013 160 m_pEd->Control |= ED_SKIP; //Skip this Ep in queue
donatien 0:632c9925f013 161
donatien 0:632c9925f013 162 //Remove from queue
donatien 0:632c9925f013 163 if( LPC_USB->HcBulkHeadED == (USB_INT32U) m_pEd )
donatien 0:632c9925f013 164 {
donatien 0:632c9925f013 165 LPC_USB->HcBulkHeadED = m_pEd->Next;
donatien 0:632c9925f013 166 }
donatien 0:632c9925f013 167 else
donatien 0:632c9925f013 168 {
donatien 0:632c9925f013 169 volatile HCED* prevEd = (volatile HCED*) LPC_USB->HcBulkHeadED;
donatien 0:632c9925f013 170 while( prevEd->Next != (USB_INT32U) m_pEd )
donatien 0:632c9925f013 171 {
donatien 0:632c9925f013 172 prevEd = (volatile HCED*) prevEd->Next;
donatien 0:632c9925f013 173 }
donatien 0:632c9925f013 174 prevEd->Next = m_pEd->Next;
donatien 0:632c9925f013 175 }
donatien 0:632c9925f013 176
donatien 0:632c9925f013 177 if( LPC_USB->HcBulkCurrentED == (USB_INT32U) m_pEd )
donatien 0:632c9925f013 178 {
donatien 0:632c9925f013 179 LPC_USB->HcBulkCurrentED = 0;
donatien 0:632c9925f013 180 }
donatien 0:632c9925f013 181
donatien 0:632c9925f013 182
donatien 0:632c9925f013 183 #if 0 //NO WAY! Owned by pool!!!
donatien 0:632c9925f013 184 delete m_pEd;
donatien 0:632c9925f013 185 delete m_pTdHead;
donatien 0:632c9925f013 186 delete m_pTdTail;
donatien 0:632c9925f013 187 #endif
donatien 0:632c9925f013 188 }
donatien 0:632c9925f013 189
donatien 0:632c9925f013 190 RC UsbEndpoint::transfer(volatile uint8_t* buf, uint32_t len)
donatien 0:632c9925f013 191 {
donatien 0:632c9925f013 192 if(!m_done)
donatien 0:632c9925f013 193 return ERR_TD_FAIL;
donatien 0:632c9925f013 194 volatile USB_INT32U token = (m_dir?TD_IN:TD_OUT);
donatien 0:632c9925f013 195 //printf("\r\n--m_pEd->HeadTd = %16x, m_pTdHead = %16x, m_pEd->TailTd = %16x, m_pTdTail = %16x--\r\n",m_pEd->HeadTd,(USB_INT32U)m_pTdHead,m_pEd->TailTd,(USB_INT32U)m_pTdTail);
donatien 0:632c9925f013 196
donatien 0:632c9925f013 197 //Swap Tds
donatien 0:632c9925f013 198 //printf("\r\n--Swap Tds--\r\n");
donatien 0:632c9925f013 199 volatile HCTD* pTdSwap;
donatien 0:632c9925f013 200 pTdSwap = m_pTdTail;
donatien 0:632c9925f013 201 m_pTdTail = m_pTdHead;
donatien 0:632c9925f013 202 m_pTdHead = pTdSwap;
donatien 0:632c9925f013 203
donatien 0:632c9925f013 204
donatien 0:632c9925f013 205 // printf("\r\n--m_pEd->HeadTd = %16x, m_pTdHead = %16x, m_pEd->TailTd = %16x, m_pTdTail = %16x--\r\n",m_pEd->HeadTd,(USB_INT32U)m_pTdHead,m_pEd->TailTd,(USB_INT32U)m_pTdTail);
donatien 0:632c9925f013 206
donatien 0:632c9925f013 207 // printf("\r\n--Tds setup--\r\n");
donatien 0:632c9925f013 208 m_pTdHead->Control = (TD_ROUNDING |
donatien 0:632c9925f013 209 token |
donatien 0:632c9925f013 210 // TD_DELAY_INT(0) |
donatien 0:632c9925f013 211 TD_DELAY_INT(7) |
donatien 0:632c9925f013 212 TD_CC);
donatien 0:632c9925f013 213 m_pTdTail->Control = 0;
donatien 0:632c9925f013 214 m_pTdHead->CurrBufPtr = (USB_INT32U) buf;
donatien 0:632c9925f013 215 m_pBufStartPtr = buf;
donatien 0:632c9925f013 216 m_pTdTail->CurrBufPtr = 0;
donatien 0:632c9925f013 217 m_pTdHead->Next = (USB_INT32U) m_pTdTail;
donatien 0:632c9925f013 218 m_pTdTail->Next = 0;
donatien 0:632c9925f013 219 m_pTdHead->BufEnd = (USB_INT32U)(buf + (len - 1));
donatien 0:632c9925f013 220 m_pTdTail->BufEnd = 0;
donatien 0:632c9925f013 221
donatien 0:632c9925f013 222 // printf("\r\n--Ed setup--\r\n");
donatien 0:632c9925f013 223 // m_pEd->HeadTd = (USB_INT32U)m_pTdHead | ((m_pEd->HeadTd) & 0x00000002);
donatien 0:632c9925f013 224 m_pEd->HeadTd = (USB_INT32U)m_pTdHead | ((m_pEd->HeadTd) & 0x00000002); //Carry bit
donatien 0:632c9925f013 225 m_pEd->TailTd = (USB_INT32U)m_pTdTail;
donatien 0:632c9925f013 226 /*
donatien 0:632c9925f013 227 printf("\r\n--Swap Tds--\r\n");
donatien 0:632c9925f013 228 volatile HCTD* pTdSwap;
donatien 0:632c9925f013 229 pTdSwap = m_pTdTail;
donatien 0:632c9925f013 230 m_pTdTail = m_pTdHead;
donatien 0:632c9925f013 231 m_pTdHead = pTdSwap;
donatien 0:632c9925f013 232 */
donatien 0:632c9925f013 233 // ed->Next = 0;
donatien 0:632c9925f013 234
donatien 0:632c9925f013 235 //printf("\r\n--m_pEd->HeadTd = %16x, m_pTdHead = %16x, m_pEd->TailTd = %16x, m_pTdTail = %16x--\r\n",m_pEd->HeadTd,(USB_INT32U)m_pTdHead,m_pEd->TailTd,(USB_INT32U)m_pTdTail);
donatien 0:632c9925f013 236
donatien 0:632c9925f013 237 // printf("\r\n--Writing config reg--\r\n");
donatien 0:632c9925f013 238 LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | OR_CMD_STATUS_BLF;
donatien 0:632c9925f013 239 LPC_USB->HcControl = LPC_USB->HcControl | OR_CONTROL_BLE;
donatien 0:632c9925f013 240
donatien 0:632c9925f013 241 // printf("\r\n--Processing queue--\r\n");
donatien 0:632c9925f013 242 // __WFI();
donatien 0:632c9925f013 243 m_done = false;
donatien 0:632c9925f013 244 m_len = len;
donatien 0:632c9925f013 245 return PROCESSING;
donatien 0:632c9925f013 246
donatien 0:632c9925f013 247 }
donatien 0:632c9925f013 248
donatien 0:632c9925f013 249 RC UsbEndpoint::status()
donatien 0:632c9925f013 250 {
donatien 0:632c9925f013 251 if( m_done )
donatien 0:632c9925f013 252 {
donatien 0:632c9925f013 253 return OK;
donatien 0:632c9925f013 254 }
donatien 0:632c9925f013 255 //volatile HCTD* pTd = (volatile HCTD*) m_pEd->HeadTd;
donatien 0:632c9925f013 256 //printf("\r\nm_pTdHead->CurrBufPtr = %16x", m_pTdHead->CurrBufPtr);
donatien 0:632c9925f013 257 //printf("\r\n--m_pEd->HeadTd = %16x, m_pTdHead = %16x, m_pEd->TailTd = %16x, m_pTdTail = %16x--\r\n",m_pEd->HeadTd,(USB_INT32U)m_pTdHead,m_pEd->TailTd,(USB_INT32U)m_pTdTail);
donatien 0:632c9925f013 258 //if( pTd->CurrBufPtr == 0 /*m_pEd->HeadTd != (USB_INT32U)m_pTdHead*//*m_pEd->HeadTd == (USB_INT32U)m_pTdTail*/) //Empty queue
donatien 0:632c9925f013 259 else if( (m_pEd->HeadTd & ~0xF) == (USB_INT32U) m_pTdTail )
donatien 0:632c9925f013 260 {
donatien 0:632c9925f013 261 //Done
donatien 0:632c9925f013 262 //printf("\r\n--m_pEd->HeadTd = %16x, m_pTdHead = %16x, m_pEd->TailTd = %16x, m_pTdTail = %16x--\r\n",m_pEd->HeadTd,(USB_INT32U)m_pTdHead,m_pEd->TailTd,(USB_INT32U)m_pTdTail);
donatien 0:632c9925f013 263 int len;
donatien 0:632c9925f013 264 if(m_pTdHead->CurrBufPtr)
donatien 0:632c9925f013 265 len = m_pTdHead->CurrBufPtr - (USB_INT32U)m_pBufStartPtr; //Does not work properly, packet might have been discarded
donatien 0:632c9925f013 266 else
donatien 0:632c9925f013 267 len = m_len;
donatien 0:632c9925f013 268 // printf("\r\nDone w/ len = %d (m_pTdHead->CurrBufPtr=%16x)\r\n", len, m_pTdHead->CurrBufPtr);
donatien 0:632c9925f013 269 if(len == 0) //Packet transfered completely
donatien 0:632c9925f013 270 len = m_len;
donatien 0:632c9925f013 271 //Host_TDInit(m_pTdTail);
donatien 0:632c9925f013 272 //m_pEd->TailTd = m_pEd->HeadTd = (USB_INT32U) m_pTdTail; //Empty TD list
donatien 0:632c9925f013 273
donatien 0:632c9925f013 274 // printf("\r\n--Writing config reg--\r\n");
donatien 0:632c9925f013 275 //LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | OR_CMD_STATUS_BLF;
donatien 0:632c9925f013 276 //LPC_USB->HcControl = LPC_USB->HcControl | OR_CONTROL_BLE;
donatien 0:632c9925f013 277
donatien 0:632c9925f013 278 m_done = true;
donatien 0:632c9925f013 279 return len;
donatien 0:632c9925f013 280 //return OK;
donatien 0:632c9925f013 281 }
donatien 0:632c9925f013 282 else if( m_pEd->HeadTd & 0x1 )
donatien 0:632c9925f013 283 {
donatien 0:632c9925f013 284 printf("\r\nHALTED!!\r\n");
donatien 0:632c9925f013 285 return ERR_TD_FAIL;
donatien 0:632c9925f013 286 }
donatien 0:632c9925f013 287 else
donatien 0:632c9925f013 288 {
donatien 0:632c9925f013 289 // wait(1);
donatien 0:632c9925f013 290 return PROCESSING;
donatien 0:632c9925f013 291 }
donatien 0:632c9925f013 292 }
donatien 0:632c9925f013 293
donatien 0:632c9925f013 294 #endif