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 "UsbSerial.h"
donatien 0:632c9925f013 25 #include "usbserialif.h"
donatien 0:632c9925f013 26 #include "rpc.h"
donatien 0:632c9925f013 27
donatien 0:632c9925f013 28 #include "netCfg.h"
donatien 0:632c9925f013 29 #if NET_USB_SERIAL
donatien 0:632c9925f013 30
donatien 0:632c9925f013 31 namespace mbed {
donatien 0:632c9925f013 32
donatien 0:632c9925f013 33 #define BUF_LEN 64
donatien 0:632c9925f013 34 #define FLUSH_TMOUT 10000 //US
donatien 0:632c9925f013 35
donatien 0:632c9925f013 36 UsbSerial::UsbSerial(int usbDev, int usbIf, const char *name /*= NULL*/) : Stream(name), m_txTimeout() {
donatien 0:632c9925f013 37 m_inBufEven = new char[BUF_LEN];
donatien 0:632c9925f013 38 m_inBufOdd = new char[BUF_LEN];
donatien 0:632c9925f013 39 m_pInBufPos = m_inBufUsr = m_inBufEven;
donatien 0:632c9925f013 40 m_inBufTrmt = m_inBufOdd;
donatien 0:632c9925f013 41
donatien 0:632c9925f013 42 m_outBufEven = new char[BUF_LEN];
donatien 0:632c9925f013 43 m_outBufOdd = new char[BUF_LEN];
donatien 0:632c9925f013 44 m_pOutBufPos = m_outBufUsr = m_outBufEven;
donatien 0:632c9925f013 45 m_outBufTrmt = m_outBufOdd;
donatien 0:632c9925f013 46
donatien 0:632c9925f013 47 m_inBufLen = m_outBufLen = 0;
donatien 0:632c9925f013 48
donatien 0:632c9925f013 49 // startRx();
donatien 0:632c9925f013 50 }
donatien 0:632c9925f013 51
donatien 0:632c9925f013 52 UsbSerial::~UsbSerial()
donatien 0:632c9925f013 53 {
donatien 0:632c9925f013 54 delete[] m_inBufEven;
donatien 0:632c9925f013 55 delete[] m_inBufOdd;
donatien 0:632c9925f013 56 delete[] m_outBufEven;
donatien 0:632c9925f013 57 delete[] m_outBufOdd;
donatien 0:632c9925f013 58 }
donatien 0:632c9925f013 59
donatien 0:632c9925f013 60 void UsbSerial::baud(int baudrate) {
donatien 0:632c9925f013 61 //
donatien 0:632c9925f013 62 }
donatien 0:632c9925f013 63
donatien 0:632c9925f013 64 void UsbSerial::format(int bits, int parity, int stop) {
donatien 0:632c9925f013 65 //
donatien 0:632c9925f013 66 }
donatien 0:632c9925f013 67
donatien 0:632c9925f013 68 int UsbSerial::_getc() {
donatien 0:632c9925f013 69 char c;
donatien 0:632c9925f013 70 c = *m_pInBufPos;
donatien 0:632c9925f013 71 m_pInBufPos++;
donatien 0:632c9925f013 72 return c;
donatien 0:632c9925f013 73 }
donatien 0:632c9925f013 74
donatien 0:632c9925f013 75 int UsbSerial::_putc(int c) {
donatien 0:632c9925f013 76 m_txTimeout.detach(); //Do not want to be interrupted, accessing shared data here...
donatien 0:632c9925f013 77 *m_pOutBufPos = (char) c;
donatien 0:632c9925f013 78 m_pOutBufPos++;
donatien 0:632c9925f013 79 if( (m_pOutBufPos - m_outBufUsr) == BUF_LEN) //Must flush
donatien 0:632c9925f013 80 {
donatien 0:632c9925f013 81 startTx();
donatien 0:632c9925f013 82 }
donatien 0:632c9925f013 83 else
donatien 0:632c9925f013 84 {
donatien 0:632c9925f013 85 m_txTimeout.attach_us(this, &UsbSerial::startTx, FLUSH_TMOUT);
donatien 0:632c9925f013 86 }
donatien 0:632c9925f013 87 return c;
donatien 0:632c9925f013 88 }
donatien 0:632c9925f013 89
donatien 0:632c9925f013 90 int UsbSerial::readable() {
donatien 0:632c9925f013 91 if( (m_pInBufPos - m_inBufUsr) < m_inBufLen )
donatien 0:632c9925f013 92 {
donatien 0:632c9925f013 93 // printf("\r\nREADABLE\r\n");
donatien 0:632c9925f013 94 return true;
donatien 0:632c9925f013 95 }
donatien 0:632c9925f013 96 else
donatien 0:632c9925f013 97 {
donatien 0:632c9925f013 98 // printf("\r\nNOT READABLE\r\n");
donatien 0:632c9925f013 99 startRx(); //Try to swap packets & start another transmission
donatien 0:632c9925f013 100 return ((m_pInBufPos - m_inBufUsr) < m_inBufLen )?true:false;
donatien 0:632c9925f013 101 }
donatien 0:632c9925f013 102 }
donatien 0:632c9925f013 103
donatien 0:632c9925f013 104 int UsbSerial::writeable() {
donatien 0:632c9925f013 105 // printf("\r\nWRITEABLE???\r\n");
donatien 0:632c9925f013 106 return (bool)( (m_pOutBufPos - m_outBufUsr) < BUF_LEN);
donatien 0:632c9925f013 107 }
donatien 0:632c9925f013 108
donatien 0:632c9925f013 109
donatien 0:632c9925f013 110 void UsbSerial::startTx()
donatien 0:632c9925f013 111 {
donatien 0:632c9925f013 112 if( SerialTransmitted() < 0 )
donatien 0:632c9925f013 113 {
donatien 0:632c9925f013 114 //Wait & retry
donatien 0:632c9925f013 115 m_txTimeout.attach_us(this, &UsbSerial::startTx, FLUSH_TMOUT);
donatien 0:632c9925f013 116 return;
donatien 0:632c9925f013 117 }
donatien 0:632c9925f013 118
donatien 0:632c9925f013 119 m_outBufLen = m_pOutBufPos - m_outBufUsr;
donatien 0:632c9925f013 120
donatien 0:632c9925f013 121 //Swap buffers
donatien 0:632c9925f013 122 volatile char* swapBuf = m_outBufUsr;
donatien 0:632c9925f013 123 m_outBufUsr = m_outBufTrmt;
donatien 0:632c9925f013 124 m_outBufTrmt = swapBuf;
donatien 0:632c9925f013 125
donatien 0:632c9925f013 126 SerialTx((volatile USB_INT08U*)m_outBufTrmt, m_outBufLen);
donatien 0:632c9925f013 127
donatien 0:632c9925f013 128 m_pOutBufPos = m_outBufUsr;
donatien 0:632c9925f013 129
donatien 0:632c9925f013 130 }
donatien 0:632c9925f013 131
donatien 0:632c9925f013 132 void UsbSerial::startRx()
donatien 0:632c9925f013 133 {
donatien 0:632c9925f013 134 if( (m_pInBufPos - m_inBufUsr) < m_inBufLen )
donatien 0:632c9925f013 135 {
donatien 0:632c9925f013 136 //User buf is not empty, cannot swap now...
donatien 0:632c9925f013 137 return;
donatien 0:632c9925f013 138 }
donatien 0:632c9925f013 139 int len = SerialReceived();
donatien 0:632c9925f013 140 if( len < 0 )
donatien 0:632c9925f013 141 {
donatien 0:632c9925f013 142 //Previous transmission not completed
donatien 0:632c9925f013 143 return;
donatien 0:632c9925f013 144 }
donatien 0:632c9925f013 145
donatien 0:632c9925f013 146 m_inBufLen = len;
donatien 0:632c9925f013 147
donatien 0:632c9925f013 148 //Swap buffers
donatien 0:632c9925f013 149 volatile char* swapBuf = m_inBufUsr;
donatien 0:632c9925f013 150 m_inBufUsr = m_inBufTrmt;
donatien 0:632c9925f013 151 m_inBufTrmt = swapBuf;
donatien 0:632c9925f013 152
donatien 0:632c9925f013 153 SerialRx((volatile USB_INT08U*)m_inBufTrmt, BUF_LEN); //Start another transmission
donatien 0:632c9925f013 154
donatien 0:632c9925f013 155 m_pInBufPos = m_inBufUsr;
donatien 0:632c9925f013 156
donatien 0:632c9925f013 157 }
donatien 0:632c9925f013 158
donatien 0:632c9925f013 159
donatien 0:632c9925f013 160
donatien 0:632c9925f013 161 const struct rpc_method *UsbSerial::get_rpc_methods() {
donatien 0:632c9925f013 162 static const rpc_method methods[] = {
donatien 0:632c9925f013 163 { "baud", rpc_method_caller<UsbSerial, int, &UsbSerial::baud> },
donatien 0:632c9925f013 164 { "format", rpc_method_caller<UsbSerial, int, int, int, &UsbSerial::format> },
donatien 0:632c9925f013 165 { "readable", rpc_method_caller<int, UsbSerial, &UsbSerial::readable> },
donatien 0:632c9925f013 166 { "writeable", rpc_method_caller<int, UsbSerial, &UsbSerial::writeable> },
donatien 0:632c9925f013 167 RPC_METHOD_SUPER(Stream)
donatien 0:632c9925f013 168 };
donatien 0:632c9925f013 169 return methods;
donatien 0:632c9925f013 170 }
donatien 0:632c9925f013 171
donatien 0:632c9925f013 172 struct rpc_class *UsbSerial::get_rpc_class() {
donatien 0:632c9925f013 173 static const rpc_function funcs[] = {
donatien 0:632c9925f013 174 { "new", rpc_function_caller<const char*, int, int, const char*, Base::construct<UsbSerial,int,int,const char*> > },
donatien 0:632c9925f013 175 RPC_METHOD_END
donatien 0:632c9925f013 176 };
donatien 0:632c9925f013 177 static rpc_class c = { "UsbSerial", funcs, NULL };
donatien 0:632c9925f013 178 return &c;
donatien 0:632c9925f013 179 }
donatien 0:632c9925f013 180
donatien 0:632c9925f013 181 } // namespace mbed
donatien 0:632c9925f013 182
donatien 0:632c9925f013 183 #endif
donatien 0:632c9925f013 184