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