Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:350011bf8be7 1
simon 0:350011bf8be7 2 /*
simon 0:350011bf8be7 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
simon 0:350011bf8be7 4
simon 0:350011bf8be7 5 Permission is hereby granted, free of charge, to any person obtaining a copy
simon 0:350011bf8be7 6 of this software and associated documentation files (the "Software"), to deal
simon 0:350011bf8be7 7 in the Software without restriction, including without limitation the rights
simon 0:350011bf8be7 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 0:350011bf8be7 9 copies of the Software, and to permit persons to whom the Software is
simon 0:350011bf8be7 10 furnished to do so, subject to the following conditions:
simon 0:350011bf8be7 11
simon 0:350011bf8be7 12 The above copyright notice and this permission notice shall be included in
simon 0:350011bf8be7 13 all copies or substantial portions of the Software.
simon 0:350011bf8be7 14
simon 0:350011bf8be7 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 0:350011bf8be7 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 0:350011bf8be7 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 0:350011bf8be7 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 0:350011bf8be7 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 0:350011bf8be7 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 0:350011bf8be7 21 THE SOFTWARE.
simon 0:350011bf8be7 22 */
simon 0:350011bf8be7 23
simon 0:350011bf8be7 24 #include "rpc.h"
simon 0:350011bf8be7 25
simon 0:350011bf8be7 26 #include "UsbSerial.h"
simon 0:350011bf8be7 27
simon 0:350011bf8be7 28 //#define __DEBUG
simon 0:350011bf8be7 29 #include "dbg/dbg.h"
simon 0:350011bf8be7 30
simon 0:350011bf8be7 31 #include "netCfg.h"
simon 0:350011bf8be7 32 #if NET_USB_SERIAL
simon 0:350011bf8be7 33
simon 0:350011bf8be7 34 namespace mbed {
simon 0:350011bf8be7 35
simon 0:350011bf8be7 36 #define BUF_LEN 64
simon 0:350011bf8be7 37 #define FLUSH_TMOUT 100000 //US
simon 0:350011bf8be7 38
simon 0:350011bf8be7 39 UsbSerial::UsbSerial(UsbDevice* pDevice, int epIn, int epOut, const char* name /*= NULL*/) : Stream(name), m_epIn(pDevice, epIn, true, USB_BULK, BUF_LEN), m_epOut(pDevice, epOut, false, USB_BULK, BUF_LEN),
simon 0:350011bf8be7 40 m_pInCbItem(NULL), m_pInCbMeth(NULL), m_pOutCbItem(NULL), m_pOutCbMeth(NULL)
simon 0:350011bf8be7 41 {
simon 0:350011bf8be7 42 m_inBufEven = new char[BUF_LEN];
simon 0:350011bf8be7 43 m_inBufOdd = new char[BUF_LEN];
simon 0:350011bf8be7 44 m_pInBufPos = m_inBufUsr = m_inBufEven;
simon 0:350011bf8be7 45 m_inBufTrmt = m_inBufOdd;
simon 0:350011bf8be7 46
simon 0:350011bf8be7 47 m_outBufEven = new char[BUF_LEN];
simon 0:350011bf8be7 48 m_outBufOdd = new char[BUF_LEN];
simon 0:350011bf8be7 49 m_pOutBufPos = m_outBufUsr = m_outBufEven;
simon 0:350011bf8be7 50 m_outBufTrmt = m_outBufOdd;
simon 0:350011bf8be7 51
simon 0:350011bf8be7 52 m_inBufLen = m_outBufLen = 0;
simon 0:350011bf8be7 53
simon 0:350011bf8be7 54 DBG("Starting RX'ing on in ep\n");
simon 0:350011bf8be7 55
simon 0:350011bf8be7 56 m_timeout = false;
simon 0:350011bf8be7 57
simon 0:350011bf8be7 58 m_epIn.setOnCompletion(this, &UsbSerial::onEpInTransfer);
simon 0:350011bf8be7 59 m_epOut.setOnCompletion(this, &UsbSerial::onEpOutTransfer);
simon 0:350011bf8be7 60
simon 0:350011bf8be7 61 startRx();
simon 0:350011bf8be7 62 }
simon 0:350011bf8be7 63
simon 0:350011bf8be7 64 UsbSerial::~UsbSerial()
simon 0:350011bf8be7 65 {
simon 0:350011bf8be7 66 delete[] m_inBufEven;
simon 0:350011bf8be7 67 delete[] m_inBufOdd;
simon 0:350011bf8be7 68 delete[] m_outBufEven;
simon 0:350011bf8be7 69 delete[] m_outBufOdd;
simon 0:350011bf8be7 70 }
simon 0:350011bf8be7 71
simon 0:350011bf8be7 72 void UsbSerial::baud(int baudrate) {
simon 0:350011bf8be7 73 //
simon 0:350011bf8be7 74 }
simon 0:350011bf8be7 75
simon 0:350011bf8be7 76 void UsbSerial::format(int bits, int parity, int stop) {
simon 0:350011bf8be7 77 //
simon 0:350011bf8be7 78 }
simon 0:350011bf8be7 79
simon 0:350011bf8be7 80 #if 0 //For doc only
simon 0:350011bf8be7 81 template <class T>
simon 0:350011bf8be7 82 void attach(T* pCbItem, void (T::*pCbMeth)())
simon 0:350011bf8be7 83 {
simon 0:350011bf8be7 84 m_pCbItem = (CDummy*) pCbItem;
simon 0:350011bf8be7 85 m_pCbMeth = (void (CDummy::*)()) pCbMeth;
simon 0:350011bf8be7 86 }
simon 0:350011bf8be7 87 #endif
simon 0:350011bf8be7 88
simon 0:350011bf8be7 89 int UsbSerial::_getc() {
simon 0:350011bf8be7 90 NVIC_DisableIRQ(US_TICKER_TIMER_IRQn);
simon 0:350011bf8be7 91 NVIC_DisableIRQ(USB_IRQn);
simon 0:350011bf8be7 92 char c;
simon 0:350011bf8be7 93 c = *m_pInBufPos;
simon 0:350011bf8be7 94 m_pInBufPos++;
simon 0:350011bf8be7 95 NVIC_EnableIRQ(USB_IRQn);
simon 0:350011bf8be7 96 NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
simon 0:350011bf8be7 97 return c;
simon 0:350011bf8be7 98 }
simon 0:350011bf8be7 99
simon 0:350011bf8be7 100 int UsbSerial::_putc(int c) {
simon 0:350011bf8be7 101 NVIC_DisableIRQ(US_TICKER_TIMER_IRQn);
simon 0:350011bf8be7 102 NVIC_DisableIRQ(USB_IRQn);
simon 0:350011bf8be7 103 if( (m_pOutBufPos - m_outBufUsr) < BUF_LEN )
simon 0:350011bf8be7 104 {
simon 0:350011bf8be7 105 *m_pOutBufPos = (char) c;
simon 0:350011bf8be7 106 m_pOutBufPos++;
simon 0:350011bf8be7 107 }
simon 0:350011bf8be7 108 else
simon 0:350011bf8be7 109 {
simon 0:350011bf8be7 110 DBG("NO WAY!!!\n");
simon 0:350011bf8be7 111 }
simon 0:350011bf8be7 112 #if 1
simon 0:350011bf8be7 113 if( (m_pOutBufPos - m_outBufUsr) >= BUF_LEN ) //Must flush
simon 0:350011bf8be7 114 {
simon 0:350011bf8be7 115 if(m_timeout)
simon 0:350011bf8be7 116 m_txTimeout.detach();
simon 0:350011bf8be7 117 startTx();
simon 0:350011bf8be7 118 }
simon 0:350011bf8be7 119 else
simon 0:350011bf8be7 120 {
simon 0:350011bf8be7 121 /*if(m_timeout)
simon 0:350011bf8be7 122 m_txTimeout.detach();
simon 0:350011bf8be7 123 m_timeout = true;
simon 0:350011bf8be7 124 m_txTimeout.attach_us(this, &UsbSerial::startTx, FLUSH_TMOUT);*/
simon 0:350011bf8be7 125 if(!m_timeout)
simon 0:350011bf8be7 126 {
simon 0:350011bf8be7 127 m_timeout = true;
simon 0:350011bf8be7 128 m_txTimeout.attach_us(this, &UsbSerial::startTx, FLUSH_TMOUT);
simon 0:350011bf8be7 129 }
simon 0:350011bf8be7 130 }
simon 0:350011bf8be7 131 #endif
simon 0:350011bf8be7 132 //startTx();
simon 0:350011bf8be7 133 NVIC_EnableIRQ(USB_IRQn);
simon 0:350011bf8be7 134 NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
simon 0:350011bf8be7 135 return c;
simon 0:350011bf8be7 136 }
simon 0:350011bf8be7 137
simon 0:350011bf8be7 138 int UsbSerial::readable() {
simon 0:350011bf8be7 139 NVIC_DisableIRQ(US_TICKER_TIMER_IRQn);
simon 0:350011bf8be7 140 NVIC_DisableIRQ(USB_IRQn);
simon 0:350011bf8be7 141 int res;
simon 0:350011bf8be7 142 if( (m_pInBufPos - m_inBufUsr) < m_inBufLen )
simon 0:350011bf8be7 143 {
simon 0:350011bf8be7 144 //DBG("\r\nREADABLE\r\n");
simon 0:350011bf8be7 145 res = true;
simon 0:350011bf8be7 146 }
simon 0:350011bf8be7 147 else
simon 0:350011bf8be7 148 {
simon 0:350011bf8be7 149 //DBG("\r\nNOT READABLE\r\n");
simon 0:350011bf8be7 150 startRx(); //Try to swap packets & start another transmission
simon 0:350011bf8be7 151 res = ((m_pInBufPos - m_inBufUsr) < m_inBufLen )?true:false;
simon 0:350011bf8be7 152 }
simon 0:350011bf8be7 153 NVIC_EnableIRQ(USB_IRQn);
simon 0:350011bf8be7 154 NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
simon 0:350011bf8be7 155 return (bool)res;
simon 0:350011bf8be7 156 }
simon 0:350011bf8be7 157
simon 0:350011bf8be7 158 int UsbSerial::writeable() {
simon 0:350011bf8be7 159 NVIC_DisableIRQ(US_TICKER_TIMER_IRQn);
simon 0:350011bf8be7 160 NVIC_DisableIRQ(USB_IRQn);
simon 0:350011bf8be7 161 // DBG("\r\nWRITEABLE???\r\n");
simon 0:350011bf8be7 162 int res = (bool)( (m_pOutBufPos - m_outBufUsr) < BUF_LEN);
simon 0:350011bf8be7 163 NVIC_EnableIRQ(USB_IRQn);
simon 0:350011bf8be7 164 NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
simon 0:350011bf8be7 165 return res;
simon 0:350011bf8be7 166 }
simon 0:350011bf8be7 167
simon 0:350011bf8be7 168 void UsbSerial::onReadable()
simon 0:350011bf8be7 169 {
simon 0:350011bf8be7 170 if(m_pInCbItem && m_pInCbMeth)
simon 0:350011bf8be7 171 (m_pInCbItem->*m_pInCbMeth)();
simon 0:350011bf8be7 172 }
simon 0:350011bf8be7 173
simon 0:350011bf8be7 174 void UsbSerial::onWriteable()
simon 0:350011bf8be7 175 {
simon 0:350011bf8be7 176 if(m_pOutCbItem && m_pOutCbMeth)
simon 0:350011bf8be7 177 (m_pOutCbItem->*m_pOutCbMeth)();
simon 0:350011bf8be7 178 }
simon 0:350011bf8be7 179
simon 0:350011bf8be7 180 void UsbSerial::onEpInTransfer()
simon 0:350011bf8be7 181 {
simon 0:350011bf8be7 182 int len = m_epIn.status();
simon 0:350011bf8be7 183 DBG("RX transfer completed w len=%d\n",len);
simon 0:350011bf8be7 184 startRx();
simon 0:350011bf8be7 185 if(len > 0)
simon 0:350011bf8be7 186 onReadable();
simon 0:350011bf8be7 187 }
simon 0:350011bf8be7 188
simon 0:350011bf8be7 189 void UsbSerial::onEpOutTransfer()
simon 0:350011bf8be7 190 {
simon 0:350011bf8be7 191 int len = m_epOut.status();
simon 0:350011bf8be7 192 DBG("TX transfer completed w len=%d\n",len);
simon 0:350011bf8be7 193 if(m_timeout)
simon 0:350011bf8be7 194 m_txTimeout.detach();
simon 0:350011bf8be7 195 startTx();
simon 0:350011bf8be7 196 if(len > 0)
simon 0:350011bf8be7 197 onWriteable();
simon 0:350011bf8be7 198 }
simon 0:350011bf8be7 199
simon 0:350011bf8be7 200 void UsbSerial::startTx()
simon 0:350011bf8be7 201 {
simon 0:350011bf8be7 202
simon 0:350011bf8be7 203 DBG("Transfer>\n");
simon 0:350011bf8be7 204
simon 0:350011bf8be7 205 m_timeout = false;
simon 0:350011bf8be7 206
simon 0:350011bf8be7 207 // m_txTimeout.detach();
simon 0:350011bf8be7 208
simon 0:350011bf8be7 209 if(!(m_pOutBufPos - m_outBufUsr))
simon 0:350011bf8be7 210 {
simon 0:350011bf8be7 211 DBG("?!?!?\n");
simon 0:350011bf8be7 212 return;
simon 0:350011bf8be7 213 }
simon 0:350011bf8be7 214
simon 0:350011bf8be7 215 if( m_epOut.status() == USBERR_PROCESSING )
simon 0:350011bf8be7 216 {
simon 0:350011bf8be7 217 //Wait & retry
simon 0:350011bf8be7 218 //m_timeout = true;
simon 0:350011bf8be7 219 //m_txTimeout.attach_us(this, &UsbSerial::startTx, FLUSH_TMOUT);
simon 0:350011bf8be7 220 DBG("Ep is busy...\n");
simon 0:350011bf8be7 221 return;
simon 0:350011bf8be7 222 }
simon 0:350011bf8be7 223
simon 0:350011bf8be7 224 if( m_epOut.status() < 0 )
simon 0:350011bf8be7 225 {
simon 0:350011bf8be7 226 DBG("Tx trying again...\n");
simon 0:350011bf8be7 227 m_epOut.transfer((volatile uint8_t*)m_outBufTrmt, m_outBufLen);
simon 0:350011bf8be7 228 return;
simon 0:350011bf8be7 229 }
simon 0:350011bf8be7 230
simon 0:350011bf8be7 231 m_outBufLen = m_pOutBufPos - m_outBufUsr;
simon 0:350011bf8be7 232
simon 0:350011bf8be7 233 //Swap buffers
simon 0:350011bf8be7 234 volatile char* swapBuf = m_outBufUsr;
simon 0:350011bf8be7 235 m_outBufUsr = m_outBufTrmt;
simon 0:350011bf8be7 236 m_outBufTrmt = swapBuf;
simon 0:350011bf8be7 237
simon 0:350011bf8be7 238 m_epOut.transfer((volatile uint8_t*)m_outBufTrmt, m_outBufLen);
simon 0:350011bf8be7 239
simon 0:350011bf8be7 240 m_pOutBufPos = m_outBufUsr;
simon 0:350011bf8be7 241
simon 0:350011bf8be7 242 }
simon 0:350011bf8be7 243
simon 0:350011bf8be7 244 void UsbSerial::startRx()
simon 0:350011bf8be7 245 {
simon 0:350011bf8be7 246 if( (m_pInBufPos - m_inBufUsr) < m_inBufLen )
simon 0:350011bf8be7 247 {
simon 0:350011bf8be7 248 //User buf is not empty, cannot swap now...
simon 0:350011bf8be7 249 return;
simon 0:350011bf8be7 250 }
simon 0:350011bf8be7 251 int len = m_epIn.status();
simon 0:350011bf8be7 252 if( len == USBERR_PROCESSING )
simon 0:350011bf8be7 253 {
simon 0:350011bf8be7 254 //Previous transmission not completed
simon 0:350011bf8be7 255 return;
simon 0:350011bf8be7 256 }
simon 0:350011bf8be7 257 if( len < 0 )
simon 0:350011bf8be7 258 {
simon 0:350011bf8be7 259 DBG("Rx trying again...\n");
simon 0:350011bf8be7 260 m_epIn.transfer((volatile uint8_t*)m_inBufTrmt, BUF_LEN); //Start another transmission
simon 0:350011bf8be7 261 return;
simon 0:350011bf8be7 262 }
simon 0:350011bf8be7 263
simon 0:350011bf8be7 264 m_inBufLen = len;
simon 0:350011bf8be7 265
simon 0:350011bf8be7 266 //Swap buffers
simon 0:350011bf8be7 267 volatile char* swapBuf = m_inBufUsr;
simon 0:350011bf8be7 268 m_inBufUsr = m_inBufTrmt;
simon 0:350011bf8be7 269 m_inBufTrmt = swapBuf;
simon 0:350011bf8be7 270 m_pInBufPos = m_inBufUsr;
simon 0:350011bf8be7 271
simon 0:350011bf8be7 272 DBG("Starting new transfer\n");
simon 0:350011bf8be7 273 m_epIn.transfer((volatile uint8_t*)m_inBufTrmt, BUF_LEN); //Start another transmission
simon 0:350011bf8be7 274
simon 0:350011bf8be7 275 }
simon 0:350011bf8be7 276
simon 0:350011bf8be7 277 #ifdef MBED_RPC
simon 0:350011bf8be7 278 const struct rpc_method *UsbSerial::get_rpc_methods() {
simon 0:350011bf8be7 279 static const rpc_method methods[] = {
simon 0:350011bf8be7 280 { "readable", rpc_method_caller<int, UsbSerial, &UsbSerial::readable> },
simon 0:350011bf8be7 281 { "writeable", rpc_method_caller<int, UsbSerial, &UsbSerial::writeable> },
simon 0:350011bf8be7 282 RPC_METHOD_SUPER(Stream)
simon 0:350011bf8be7 283 };
simon 0:350011bf8be7 284 return methods;
simon 0:350011bf8be7 285 }
simon 0:350011bf8be7 286
simon 0:350011bf8be7 287 struct rpc_class *UsbSerial::get_rpc_class() {
simon 0:350011bf8be7 288 static const rpc_function funcs[] = {
simon 0:350011bf8be7 289 /*{ "new", rpc_function_caller<const char*, UsbDevice*, int, int, const char*, Base::construct<UsbSerial,UsbDevice*,int,int,const char*> > },*/ //RPC is buggy
simon 0:350011bf8be7 290 RPC_METHOD_END
simon 0:350011bf8be7 291 };
simon 0:350011bf8be7 292 static rpc_class c = { "UsbSerial", funcs, NULL };
simon 0:350011bf8be7 293 return &c;
simon 0:350011bf8be7 294 }
simon 0:350011bf8be7 295 #endif
simon 0:350011bf8be7 296
simon 0:350011bf8be7 297 } // namespace mbed
simon 0:350011bf8be7 298
simon 0:350011bf8be7 299 #endif
simon 0:350011bf8be7 300