PHS module SMA-01 library. see: https://developer.mbed.org/users/phsfan/notebook/abitusbmodem/

Dependencies:   Socket lwip-sys lwip

Dependents:   AbitUSBModem_HTTPTest AbitUSBModem_MQTTTest AbitUSBModem_WebsocketTest AbitUSBModem_SMSTest

Fork of VodafoneUSBModem by mbed official

/media/uploads/phsfan/sma01_003.png

Committer:
donatien
Date:
Fri Jul 06 08:56:32 2012 +0000
Revision:
10:21a6f09d5631
Parent:
8:04b6a042595f
Child:
22:06fb2a93a1f6
Dual serial ports support

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:3b2f052c333b 1 /* USBSerialStream.cpp */
donatien 0:3b2f052c333b 2 /*
donatien 0:3b2f052c333b 3 Copyright (C) 2012 ARM Limited.
donatien 0:3b2f052c333b 4
donatien 0:3b2f052c333b 5 Permission is hereby granted, free of charge, to any person obtaining a copy of
donatien 0:3b2f052c333b 6 this software and associated documentation files (the "Software"), to deal in
donatien 0:3b2f052c333b 7 the Software without restriction, including without limitation the rights to
donatien 0:3b2f052c333b 8 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
donatien 0:3b2f052c333b 9 of the Software, and to permit persons to whom the Software is furnished to do
donatien 0:3b2f052c333b 10 so, subject to the following conditions:
donatien 0:3b2f052c333b 11
donatien 0:3b2f052c333b 12 The above copyright notice and this permission notice shall be included in all
donatien 0:3b2f052c333b 13 copies or substantial portions of the Software.
donatien 0:3b2f052c333b 14
donatien 0:3b2f052c333b 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
donatien 0:3b2f052c333b 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
donatien 0:3b2f052c333b 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
donatien 0:3b2f052c333b 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
donatien 0:3b2f052c333b 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
donatien 0:3b2f052c333b 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
donatien 0:3b2f052c333b 21 SOFTWARE.
donatien 0:3b2f052c333b 22 */
donatien 0:3b2f052c333b 23
donatien 0:3b2f052c333b 24 #define __DEBUG__ 0 //Maximum verbosity
donatien 0:3b2f052c333b 25 #ifndef __MODULE__
donatien 0:3b2f052c333b 26 #define __MODULE__ "USBSerialStream.cpp"
donatien 0:3b2f052c333b 27 #endif
donatien 0:3b2f052c333b 28
donatien 0:3b2f052c333b 29 #include "core/fwk.h"
donatien 0:3b2f052c333b 30
donatien 0:3b2f052c333b 31 #include <stdio.h>
donatien 0:3b2f052c333b 32
donatien 0:3b2f052c333b 33 #include "USBSerialStream.h"
donatien 0:3b2f052c333b 34
donatien 0:3b2f052c333b 35
donatien 0:3b2f052c333b 36 USBSerialStream::USBSerialStream(IUSBHostSerial& serial) : m_serial(serial), m_serialTxFifoEmpty(true),
donatien 0:3b2f052c333b 37 m_availableSphre(1), m_spaceSphre(1), m_inBuf()
donatien 0:3b2f052c333b 38 {
donatien 0:3b2f052c333b 39 m_availableSphre.wait();
donatien 0:3b2f052c333b 40 m_spaceSphre.wait();
donatien 0:3b2f052c333b 41 //Attach interrupts
donatien 0:3b2f052c333b 42 m_serial.attach(this);
donatien 0:3b2f052c333b 43 }
donatien 0:3b2f052c333b 44
donatien 0:3b2f052c333b 45 /*virtual*/ USBSerialStream::~USBSerialStream()
donatien 0:3b2f052c333b 46 {
donatien 0:3b2f052c333b 47 m_serial.attach(NULL);
donatien 0:3b2f052c333b 48 }
donatien 0:3b2f052c333b 49
donatien 0:3b2f052c333b 50 //0 for non-blocking (returns immediately), -1 for infinite blocking
donatien 0:3b2f052c333b 51 /*virtual*/ int USBSerialStream::read(uint8_t* buf, size_t* pLength, size_t maxLength, uint32_t timeout/*=osWaitForever*/)
donatien 0:3b2f052c333b 52 {
donatien 0:3b2f052c333b 53 DBG("Trying to read at most %d chars", maxLength);
donatien 0:3b2f052c333b 54 int ret = waitAvailable(timeout);
donatien 0:3b2f052c333b 55 if(ret)
donatien 0:3b2f052c333b 56 {
donatien 0:3b2f052c333b 57 WARN("Error %d while waiting for incoming data", ret);
donatien 0:3b2f052c333b 58 return ret;
donatien 0:3b2f052c333b 59 }
donatien 10:21a6f09d5631 60 int a = available(); //Prevent macro issues
donatien 10:21a6f09d5631 61 int readLen = MIN( a, maxLength );
donatien 0:3b2f052c333b 62 *pLength = readLen;
donatien 0:3b2f052c333b 63
donatien 0:3b2f052c333b 64 setupReadableISR(false);
donatien 0:3b2f052c333b 65 while(readLen--)
donatien 0:3b2f052c333b 66 {
donatien 0:3b2f052c333b 67 m_inBuf.dequeue(buf);
donatien 0:3b2f052c333b 68 buf++;
donatien 0:3b2f052c333b 69 }
donatien 0:3b2f052c333b 70 setupReadableISR(true);
donatien 0:3b2f052c333b 71 DBG("Read %d chars successfully", *pLength);
donatien 0:3b2f052c333b 72 return OK;
donatien 0:3b2f052c333b 73 }
donatien 0:3b2f052c333b 74
donatien 0:3b2f052c333b 75 /*virtual*/ size_t USBSerialStream::available()
donatien 0:3b2f052c333b 76 {
donatien 0:3b2f052c333b 77 setupReadableISR(false); //m_inBuf.available() is not reentrant
donatien 0:3b2f052c333b 78 size_t len = m_inBuf.available();
donatien 0:3b2f052c333b 79 setupReadableISR(true);
donatien 0:3b2f052c333b 80 return len;
donatien 0:3b2f052c333b 81 }
donatien 0:3b2f052c333b 82
donatien 0:3b2f052c333b 83 /*virtual*/ int USBSerialStream::waitAvailable(uint32_t timeout/*=osWaitForever*/) //Wait for data to be available
donatien 0:3b2f052c333b 84 {
donatien 0:3b2f052c333b 85 int ret;
donatien 0:3b2f052c333b 86 if(available()) //Is data already available?
donatien 0:3b2f052c333b 87 {
donatien 10:21a6f09d5631 88 while( m_availableSphre.wait(0) > 0 ); //Clear the queue as data is available
donatien 0:3b2f052c333b 89 return OK;
donatien 0:3b2f052c333b 90 }
donatien 0:3b2f052c333b 91
donatien 0:3b2f052c333b 92 DBG("Waiting for data availability %d ms (-1 is infinite)", timeout);
donatien 0:3b2f052c333b 93 ret = m_availableSphre.wait(timeout); //Wait for data to arrive or for abort
donatien 0:3b2f052c333b 94 if(ret <= 0)
donatien 0:3b2f052c333b 95 {
donatien 0:3b2f052c333b 96 DBG("Timeout");
donatien 0:3b2f052c333b 97 return NET_TIMEOUT;
donatien 0:3b2f052c333b 98 }
donatien 0:3b2f052c333b 99 if(!m_inBuf.available()) //Even if abort has been called, return that data is available
donatien 0:3b2f052c333b 100 {
donatien 0:3b2f052c333b 101 DBG("Aborted");
donatien 0:3b2f052c333b 102 return NET_INTERRUPTED;
donatien 0:3b2f052c333b 103 }
donatien 0:3b2f052c333b 104 DBG("Finished waiting");
donatien 10:21a6f09d5631 105 while( m_availableSphre.wait(0) > 0 ); //Clear the queue as data is available
donatien 0:3b2f052c333b 106 return OK;
donatien 0:3b2f052c333b 107 }
donatien 0:3b2f052c333b 108
donatien 0:3b2f052c333b 109 /*virtual*/ int USBSerialStream::abortRead() //Abort current reading (or waiting) operation
donatien 0:3b2f052c333b 110 {
donatien 0:3b2f052c333b 111 if( /*!available()*/true ) //If there is data pending, no need to abort
donatien 0:3b2f052c333b 112 {
donatien 0:3b2f052c333b 113 m_availableSphre.release(); //Force exiting the waiting state
donatien 0:3b2f052c333b 114 }
donatien 0:3b2f052c333b 115 else
donatien 0:3b2f052c333b 116 {
donatien 0:3b2f052c333b 117 DBG("Serial is readable"); ;
donatien 0:3b2f052c333b 118 }
donatien 0:3b2f052c333b 119 return OK;
donatien 0:3b2f052c333b 120 }
donatien 0:3b2f052c333b 121
donatien 0:3b2f052c333b 122 void USBSerialStream::setupReadableISR(bool en)
donatien 0:3b2f052c333b 123 {
donatien 0:3b2f052c333b 124 m_serial.setupIrq(en, IUSBHostSerial::RxIrq);
donatien 0:3b2f052c333b 125 }
donatien 0:3b2f052c333b 126
donatien 0:3b2f052c333b 127 void USBSerialStream::readable() //Callback from m_serial when new data is available
donatien 0:3b2f052c333b 128 {
donatien 0:3b2f052c333b 129 while(m_serial.readable())
donatien 0:3b2f052c333b 130 {
donatien 0:3b2f052c333b 131 m_inBuf.queue(m_serial.getc());
donatien 0:3b2f052c333b 132 }
donatien 0:3b2f052c333b 133 m_serial.readPacket(); //Start read of next packet
donatien 0:3b2f052c333b 134 m_availableSphre.release(); //Force exiting the waiting state
donatien 0:3b2f052c333b 135 }
donatien 0:3b2f052c333b 136
donatien 0:3b2f052c333b 137 //0 for non-blocking (returns immediately), -1 for infinite blocking
donatien 0:3b2f052c333b 138 /*virtual*/ int USBSerialStream::write(uint8_t* buf, size_t length, uint32_t timeout/*=-1*/)
donatien 0:3b2f052c333b 139 {
donatien 0:3b2f052c333b 140 DBG("Trying to write %d chars", length);
donatien 0:3b2f052c333b 141 do
donatien 0:3b2f052c333b 142 {
donatien 0:3b2f052c333b 143 int ret = waitSpace(timeout);
donatien 0:3b2f052c333b 144 if(ret)
donatien 0:3b2f052c333b 145 {
donatien 10:21a6f09d5631 146 WARN("Error %d while waiting for space", ret);
donatien 0:3b2f052c333b 147 return ret;
donatien 0:3b2f052c333b 148 }
donatien 10:21a6f09d5631 149 int s = space(); //Prevent macro issues
donatien 10:21a6f09d5631 150 int writeLen = MIN( s, length );
donatien 10:21a6f09d5631 151 DBG("Writing %d chars", writeLen);
donatien 0:3b2f052c333b 152 setupWriteableISR(false);
donatien 0:3b2f052c333b 153 while(writeLen)
donatien 0:3b2f052c333b 154 {
donatien 0:3b2f052c333b 155 m_outBuf.queue(*buf);
donatien 0:3b2f052c333b 156 buf++;
donatien 0:3b2f052c333b 157 length--;
donatien 0:3b2f052c333b 158 writeLen--;
donatien 0:3b2f052c333b 159 }
donatien 10:21a6f09d5631 160 //If m_serial tx fifo is empty we need to start the packet write
donatien 10:21a6f09d5631 161 if( m_outBuf.available() && m_serialTxFifoEmpty )
donatien 10:21a6f09d5631 162 {
donatien 10:21a6f09d5631 163 writeable();
donatien 10:21a6f09d5631 164 }
donatien 0:3b2f052c333b 165 setupWriteableISR(true);
donatien 0:3b2f052c333b 166 } while(length);
donatien 10:21a6f09d5631 167
donatien 0:3b2f052c333b 168 DBG("Write successful");
donatien 0:3b2f052c333b 169 return OK;
donatien 0:3b2f052c333b 170 }
donatien 0:3b2f052c333b 171
donatien 0:3b2f052c333b 172 /*virtual*/ size_t USBSerialStream::space()
donatien 0:3b2f052c333b 173 {
donatien 0:3b2f052c333b 174 setupWriteableISR(false); //m_outBuf.available() is not reentrant
donatien 0:3b2f052c333b 175 size_t len = CIRCBUF_SIZE - m_outBuf.available();
donatien 0:3b2f052c333b 176 setupWriteableISR(true);
donatien 0:3b2f052c333b 177 return len;
donatien 0:3b2f052c333b 178 }
donatien 0:3b2f052c333b 179
donatien 0:3b2f052c333b 180 /*virtual*/ int USBSerialStream::waitSpace(uint32_t timeout/*=-1*/) //Wait for space to be available
donatien 0:3b2f052c333b 181 {
donatien 0:3b2f052c333b 182 int ret;
donatien 0:3b2f052c333b 183 if(space()) //Is still space already left?
donatien 0:3b2f052c333b 184 {
donatien 10:21a6f09d5631 185 while( m_spaceSphre.wait(0) > 0); //Clear the queue as space is available
donatien 0:3b2f052c333b 186 return OK;
donatien 0:3b2f052c333b 187 }
donatien 0:3b2f052c333b 188
donatien 0:3b2f052c333b 189 DBG("Waiting for data space %d ms (-1 is infinite)", timeout);
donatien 0:3b2f052c333b 190 ret = m_spaceSphre.wait(timeout); //Wait for space to be made or for abort
donatien 0:3b2f052c333b 191 if(ret <= 0)
donatien 0:3b2f052c333b 192 {
donatien 0:3b2f052c333b 193 DBG("Timeout");
donatien 0:3b2f052c333b 194 return NET_TIMEOUT;
donatien 0:3b2f052c333b 195 }
donatien 0:3b2f052c333b 196 if(!space()) //Even if abort has been called, return that space is available
donatien 0:3b2f052c333b 197 {
donatien 0:3b2f052c333b 198 DBG("Aborted");
donatien 0:3b2f052c333b 199 return NET_INTERRUPTED;
donatien 0:3b2f052c333b 200 }
donatien 10:21a6f09d5631 201 while( m_spaceSphre.wait(0) > 0); //Clear the queue as space is available
donatien 0:3b2f052c333b 202 return OK;
donatien 0:3b2f052c333b 203 }
donatien 0:3b2f052c333b 204
donatien 0:3b2f052c333b 205 /*virtual*/ int USBSerialStream::abortWrite() //Abort current writing (or waiting) operation
donatien 0:3b2f052c333b 206 {
donatien 0:3b2f052c333b 207 if( !space() ) //If there is space left, no need to abort
donatien 0:3b2f052c333b 208 {
donatien 0:3b2f052c333b 209 m_spaceSphre.release(); //Force exiting the waiting state
donatien 0:3b2f052c333b 210 }
donatien 0:3b2f052c333b 211 return OK;
donatien 0:3b2f052c333b 212 }
donatien 0:3b2f052c333b 213
donatien 0:3b2f052c333b 214 void USBSerialStream::setupWriteableISR(bool en)
donatien 0:3b2f052c333b 215 {
donatien 0:3b2f052c333b 216 m_serial.setupIrq(en, IUSBHostSerial::TxIrq);
donatien 0:3b2f052c333b 217 }
donatien 0:3b2f052c333b 218
donatien 0:3b2f052c333b 219 void USBSerialStream::writeable() //Callback from m_serial when new space is available
donatien 0:3b2f052c333b 220 {
donatien 0:3b2f052c333b 221 if(m_outBuf.isEmpty())
donatien 0:3b2f052c333b 222 {
donatien 0:3b2f052c333b 223 m_serialTxFifoEmpty = true;
donatien 0:3b2f052c333b 224 }
donatien 0:3b2f052c333b 225 else
donatien 0:3b2f052c333b 226 {
donatien 0:3b2f052c333b 227 m_serialTxFifoEmpty = false;
donatien 0:3b2f052c333b 228 while(m_serial.writeable() && !m_outBuf.isEmpty())
donatien 0:3b2f052c333b 229 {
donatien 0:3b2f052c333b 230 uint8_t c;
donatien 0:3b2f052c333b 231 m_outBuf.dequeue(&c);
donatien 0:3b2f052c333b 232 m_serial.putc((char)c);
donatien 0:3b2f052c333b 233 }
donatien 0:3b2f052c333b 234 m_serial.writePacket(); //Start packet write
donatien 0:3b2f052c333b 235 }
donatien 10:21a6f09d5631 236 if(!m_outBuf.isFull())
donatien 10:21a6f09d5631 237 {
donatien 10:21a6f09d5631 238 m_spaceSphre.release(); //Force exiting the waiting state
donatien 10:21a6f09d5631 239 }
donatien 0:3b2f052c333b 240 }