Mac addr added ICRS

Dependencies:   mbed

Fork of Email2Screen by Oliver Mattos

Committer:
je310
Date:
Wed Nov 20 21:22:22 2013 +0000
Revision:
1:b38b745d1ea8
Parent:
0:1619a6b826d7
This is a maced version of olivers code ICRS

Who changed what in which revision?

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