I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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