USB Host Library for Sprint Dongles
Dependencies: Socket USBHostWANDongleSprint lwip-sys lwip
Dependents: SprintUSBModemWebsocketTest SprintUSBModemHTTPClientTest SprintUSBModemNTPClientTest SprintUSBModemSMSTest ... more
Fork of SprintUSBModem_bleedingedge by
serial/io/IOSerialStream.cpp@11:7e1056a4ca50, 2012-11-22 (annotated)
- Committer:
- donatien
- Date:
- Thu Nov 22 15:24:50 2012 +0000
- Revision:
- 11:7e1056a4ca50
- Parent:
- 0:8f57713b2147
- Child:
- 14:f6f17843e5ef
Fix for compatibility with mbed rev. 44 library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:8f57713b2147 | 1 | /* IOSerialStream.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__ "IOSerialStream.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 "IOSerialStream.h" |
donatien | 0:8f57713b2147 | 30 | |
donatien | 0:8f57713b2147 | 31 | IOSerialStream::IOSerialStream(mbed::Serial& serial) : m_serial(serial), m_serialTxFifoEmpty(true), |
donatien | 0:8f57713b2147 | 32 | m_availableSphre(1), m_spaceSphre(1), m_inBuf(), m_outBuf() |
donatien | 0:8f57713b2147 | 33 | { |
donatien | 0:8f57713b2147 | 34 | m_availableSphre.wait(); |
donatien | 0:8f57713b2147 | 35 | m_spaceSphre.wait(); |
donatien | 0:8f57713b2147 | 36 | //Attach interrupts |
donatien | 11:7e1056a4ca50 | 37 | m_serial.attach(this, &IOSerialStream::readable, RxIrq); |
donatien | 11:7e1056a4ca50 | 38 | m_serial.attach(this, &IOSerialStream::writeable, TxIrq); |
donatien | 0:8f57713b2147 | 39 | } |
donatien | 0:8f57713b2147 | 40 | |
donatien | 0:8f57713b2147 | 41 | /*virtual*/ IOSerialStream::~IOSerialStream() |
donatien | 0:8f57713b2147 | 42 | { |
donatien | 11:7e1056a4ca50 | 43 | m_serial.attach(NULL, RxIrq); |
donatien | 11:7e1056a4ca50 | 44 | m_serial.attach(NULL, TxIrq); |
donatien | 0:8f57713b2147 | 45 | } |
donatien | 0:8f57713b2147 | 46 | |
donatien | 0:8f57713b2147 | 47 | //0 for non-blocking (returns immediately), osWaitForever for infinite blocking |
donatien | 0:8f57713b2147 | 48 | /*virtual*/ int IOSerialStream::read(uint8_t* buf, size_t* pLength, size_t maxLength, uint32_t timeout/*=osWaitForever*/) |
donatien | 0:8f57713b2147 | 49 | { |
donatien | 0:8f57713b2147 | 50 | DBG("Trying to read at most %d chars", maxLength); |
donatien | 0:8f57713b2147 | 51 | int ret = waitAvailable(timeout); |
donatien | 0:8f57713b2147 | 52 | if(ret) |
donatien | 0:8f57713b2147 | 53 | { |
donatien | 0:8f57713b2147 | 54 | WARN("Error %d while waiting for incoming data", ret); |
donatien | 0:8f57713b2147 | 55 | return ret; |
donatien | 0:8f57713b2147 | 56 | } |
donatien | 0:8f57713b2147 | 57 | int readLen = MIN( available(), maxLength ); |
donatien | 0:8f57713b2147 | 58 | *pLength = readLen; |
donatien | 0:8f57713b2147 | 59 | setupReadableISR(false); |
donatien | 0:8f57713b2147 | 60 | while(readLen--) |
donatien | 0:8f57713b2147 | 61 | { |
donatien | 0:8f57713b2147 | 62 | m_inBuf.dequeue(buf); |
donatien | 0:8f57713b2147 | 63 | buf++; |
donatien | 0:8f57713b2147 | 64 | } |
donatien | 0:8f57713b2147 | 65 | setupReadableISR(true); |
donatien | 0:8f57713b2147 | 66 | DBG("Read %d chars successfully", *pLength); |
donatien | 0:8f57713b2147 | 67 | return OK; |
donatien | 0:8f57713b2147 | 68 | } |
donatien | 0:8f57713b2147 | 69 | |
donatien | 0:8f57713b2147 | 70 | /*virtual*/ size_t IOSerialStream::available() |
donatien | 0:8f57713b2147 | 71 | { |
donatien | 0:8f57713b2147 | 72 | setupReadableISR(false); //m_inBuf.available() is not reentrant |
donatien | 0:8f57713b2147 | 73 | size_t len = m_inBuf.available(); |
donatien | 0:8f57713b2147 | 74 | setupReadableISR(true); |
donatien | 0:8f57713b2147 | 75 | return len; |
donatien | 0:8f57713b2147 | 76 | } |
donatien | 0:8f57713b2147 | 77 | |
donatien | 0:8f57713b2147 | 78 | /*virtual*/ int IOSerialStream::waitAvailable(uint32_t timeout/*=osWaitForever*/) //Wait for data to be available |
donatien | 0:8f57713b2147 | 79 | { |
donatien | 0:8f57713b2147 | 80 | int ret; |
donatien | 0:8f57713b2147 | 81 | if(available()) //Is data already available? |
donatien | 0:8f57713b2147 | 82 | { |
donatien | 0:8f57713b2147 | 83 | m_availableSphre.wait(0); //Clear the queue as data is available |
donatien | 0:8f57713b2147 | 84 | return OK; |
donatien | 0:8f57713b2147 | 85 | } |
donatien | 0:8f57713b2147 | 86 | |
donatien | 0:8f57713b2147 | 87 | DBG("Waiting for data availability %d ms (-1 is infinite)", timeout); |
donatien | 0:8f57713b2147 | 88 | ret = m_availableSphre.wait(timeout); //Wait for data to arrive or for abort |
donatien | 0:8f57713b2147 | 89 | if(ret <= 0) |
donatien | 0:8f57713b2147 | 90 | { |
donatien | 0:8f57713b2147 | 91 | DBG("Timeout"); |
donatien | 0:8f57713b2147 | 92 | return NET_TIMEOUT; |
donatien | 0:8f57713b2147 | 93 | } |
donatien | 0:8f57713b2147 | 94 | if(!available()) //Even if abort has been called, return that data is available |
donatien | 0:8f57713b2147 | 95 | { |
donatien | 0:8f57713b2147 | 96 | DBG("Aborted"); |
donatien | 0:8f57713b2147 | 97 | return NET_INTERRUPTED; |
donatien | 0:8f57713b2147 | 98 | } |
donatien | 0:8f57713b2147 | 99 | DBG("Finished waiting"); |
donatien | 0:8f57713b2147 | 100 | m_availableSphre.wait(0); //Clear the queue as data is available |
donatien | 0:8f57713b2147 | 101 | return OK; |
donatien | 0:8f57713b2147 | 102 | } |
donatien | 0:8f57713b2147 | 103 | |
donatien | 0:8f57713b2147 | 104 | /*virtual*/ int IOSerialStream::abortRead() //Abort current reading (or waiting) operation |
donatien | 0:8f57713b2147 | 105 | { |
donatien | 0:8f57713b2147 | 106 | if( !available() ) //If there is data pending, no need to abort |
donatien | 0:8f57713b2147 | 107 | { |
donatien | 0:8f57713b2147 | 108 | m_availableSphre.release(); //Force exiting the waiting state; kludge to pass a RC directly |
donatien | 0:8f57713b2147 | 109 | } |
donatien | 0:8f57713b2147 | 110 | else |
donatien | 0:8f57713b2147 | 111 | { |
donatien | 0:8f57713b2147 | 112 | DBG("Serial is readable"); ; |
donatien | 0:8f57713b2147 | 113 | } |
donatien | 0:8f57713b2147 | 114 | return OK; |
donatien | 0:8f57713b2147 | 115 | } |
donatien | 0:8f57713b2147 | 116 | |
donatien | 0:8f57713b2147 | 117 | void IOSerialStream::setupReadableISR(bool en) |
donatien | 0:8f57713b2147 | 118 | { |
donatien | 0:8f57713b2147 | 119 | if(en) |
donatien | 0:8f57713b2147 | 120 | { |
donatien | 0:8f57713b2147 | 121 | ((LPC_UART_TypeDef *)(UART_3))->IER |= 1 << 0; |
donatien | 0:8f57713b2147 | 122 | } |
donatien | 0:8f57713b2147 | 123 | else |
donatien | 0:8f57713b2147 | 124 | { |
donatien | 0:8f57713b2147 | 125 | ((LPC_UART_TypeDef *)(UART_3))->IER &= ~(1 << 0); |
donatien | 0:8f57713b2147 | 126 | } |
donatien | 0:8f57713b2147 | 127 | } |
donatien | 0:8f57713b2147 | 128 | |
donatien | 0:8f57713b2147 | 129 | void IOSerialStream::readable() //Callback from m_serial when new data is available |
donatien | 0:8f57713b2147 | 130 | { |
donatien | 0:8f57713b2147 | 131 | do |
donatien | 0:8f57713b2147 | 132 | { |
donatien | 0:8f57713b2147 | 133 | m_inBuf.queue(((LPC_UART_TypeDef *)UART_3)->RBR); //FIXME mbed libraries this is an awful kludge |
donatien | 0:8f57713b2147 | 134 | } while(m_serial.readable()); |
donatien | 0:8f57713b2147 | 135 | m_availableSphre.release(); //Force exiting the waiting state |
donatien | 0:8f57713b2147 | 136 | } |
donatien | 0:8f57713b2147 | 137 | |
donatien | 0:8f57713b2147 | 138 | //0 for non-blocking (returns immediately), osWaitForever for infinite blocking |
donatien | 0:8f57713b2147 | 139 | /*virtual*/ int IOSerialStream::write(uint8_t* buf, size_t length, uint32_t timeout/*=osWaitForever*/) |
donatien | 0:8f57713b2147 | 140 | { |
donatien | 0:8f57713b2147 | 141 | DBG("Trying to write %d chars", length); |
donatien | 0:8f57713b2147 | 142 | int ret = waitSpace(timeout); |
donatien | 0:8f57713b2147 | 143 | if(ret) |
donatien | 0:8f57713b2147 | 144 | { |
donatien | 0:8f57713b2147 | 145 | WARN("Error %d while waiting for space", ret); |
donatien | 0:8f57713b2147 | 146 | return ret; |
donatien | 0:8f57713b2147 | 147 | } |
donatien | 0:8f57713b2147 | 148 | DBG("Writing %d chars", length); |
donatien | 0:8f57713b2147 | 149 | setupWriteableISR(false); |
donatien | 0:8f57713b2147 | 150 | while(length) |
donatien | 0:8f57713b2147 | 151 | { |
donatien | 0:8f57713b2147 | 152 | m_outBuf.queue(*buf); |
donatien | 0:8f57713b2147 | 153 | buf++; |
donatien | 0:8f57713b2147 | 154 | length--; |
donatien | 0:8f57713b2147 | 155 | if(length && !space()) |
donatien | 0:8f57713b2147 | 156 | { |
donatien | 0:8f57713b2147 | 157 | DBG("Waiting to write remaining %d chars", length); |
donatien | 0:8f57713b2147 | 158 | setupWriteableISR(true); |
donatien | 0:8f57713b2147 | 159 | ret = waitSpace(timeout); |
donatien | 0:8f57713b2147 | 160 | if(ret) |
donatien | 0:8f57713b2147 | 161 | { |
donatien | 0:8f57713b2147 | 162 | WARN("Error %d while waiting for space", ret); |
donatien | 0:8f57713b2147 | 163 | return ret; |
donatien | 0:8f57713b2147 | 164 | } |
donatien | 0:8f57713b2147 | 165 | setupWriteableISR(false); |
donatien | 0:8f57713b2147 | 166 | } |
donatien | 0:8f57713b2147 | 167 | } |
donatien | 0:8f57713b2147 | 168 | //If m_serial tx fifo is empty we need to manually tx a byte in order to trigger the interrupt |
donatien | 0:8f57713b2147 | 169 | if( m_outBuf.available() && m_serialTxFifoEmpty ) |
donatien | 0:8f57713b2147 | 170 | { |
donatien | 0:8f57713b2147 | 171 | m_serialTxFifoEmpty = false; |
donatien | 0:8f57713b2147 | 172 | uint8_t c; |
donatien | 0:8f57713b2147 | 173 | m_outBuf.dequeue(&c); |
donatien | 0:8f57713b2147 | 174 | //m_serial.putc((char)c); |
donatien | 0:8f57713b2147 | 175 | ((LPC_UART_TypeDef *)UART_3)->THR = c; //FIXME awful kludge |
donatien | 0:8f57713b2147 | 176 | } |
donatien | 0:8f57713b2147 | 177 | setupWriteableISR(true); |
donatien | 0:8f57713b2147 | 178 | DBG("Write successful"); |
donatien | 0:8f57713b2147 | 179 | return OK; |
donatien | 0:8f57713b2147 | 180 | } |
donatien | 0:8f57713b2147 | 181 | |
donatien | 0:8f57713b2147 | 182 | /*virtual*/ size_t IOSerialStream::space() |
donatien | 0:8f57713b2147 | 183 | { |
donatien | 0:8f57713b2147 | 184 | setupWriteableISR(false); //m_outBuf.available() is not reentrant |
donatien | 0:8f57713b2147 | 185 | size_t len = CIRCBUF_SIZE - m_outBuf.available(); |
donatien | 0:8f57713b2147 | 186 | setupWriteableISR(true); |
donatien | 0:8f57713b2147 | 187 | return len; |
donatien | 0:8f57713b2147 | 188 | } |
donatien | 0:8f57713b2147 | 189 | |
donatien | 0:8f57713b2147 | 190 | /*virtual*/ int IOSerialStream::waitSpace(uint32_t timeout/*=osWaitForever*/) //Wait for space to be available |
donatien | 0:8f57713b2147 | 191 | { |
donatien | 0:8f57713b2147 | 192 | int ret; |
donatien | 0:8f57713b2147 | 193 | if(space()) //Is still space already left? |
donatien | 0:8f57713b2147 | 194 | { |
donatien | 0:8f57713b2147 | 195 | m_spaceSphre.wait(0); //Clear the queue as space is available |
donatien | 0:8f57713b2147 | 196 | return OK; |
donatien | 0:8f57713b2147 | 197 | } |
donatien | 0:8f57713b2147 | 198 | |
donatien | 0:8f57713b2147 | 199 | DBG("Waiting for data space %d ms (-1 is infinite)", timeout); |
donatien | 0:8f57713b2147 | 200 | ret = m_spaceSphre.wait(timeout); //Wait for space to be made or for abort |
donatien | 0:8f57713b2147 | 201 | if(ret <= 0) |
donatien | 0:8f57713b2147 | 202 | { |
donatien | 0:8f57713b2147 | 203 | DBG("Timeout"); |
donatien | 0:8f57713b2147 | 204 | return NET_TIMEOUT; |
donatien | 0:8f57713b2147 | 205 | } |
donatien | 0:8f57713b2147 | 206 | if(!space()) //Even if abort has been called, return that space is available |
donatien | 0:8f57713b2147 | 207 | { |
donatien | 0:8f57713b2147 | 208 | DBG("Aborted"); |
donatien | 0:8f57713b2147 | 209 | return NET_INTERRUPTED; |
donatien | 0:8f57713b2147 | 210 | } |
donatien | 0:8f57713b2147 | 211 | m_spaceSphre.wait(0); //Clear the queue as space is available |
donatien | 0:8f57713b2147 | 212 | return OK; |
donatien | 0:8f57713b2147 | 213 | } |
donatien | 0:8f57713b2147 | 214 | |
donatien | 0:8f57713b2147 | 215 | /*virtual*/ int IOSerialStream::abortWrite() //Abort current writing (or waiting) operation |
donatien | 0:8f57713b2147 | 216 | { |
donatien | 0:8f57713b2147 | 217 | if( !space() ) //If there is space left, no need to abort |
donatien | 0:8f57713b2147 | 218 | { |
donatien | 0:8f57713b2147 | 219 | m_spaceSphre.release(); //Force exiting the waiting state |
donatien | 0:8f57713b2147 | 220 | } |
donatien | 0:8f57713b2147 | 221 | return OK; |
donatien | 0:8f57713b2147 | 222 | } |
donatien | 0:8f57713b2147 | 223 | |
donatien | 0:8f57713b2147 | 224 | void IOSerialStream::setupWriteableISR(bool en) |
donatien | 0:8f57713b2147 | 225 | { |
donatien | 0:8f57713b2147 | 226 | if(en) |
donatien | 0:8f57713b2147 | 227 | { |
donatien | 0:8f57713b2147 | 228 | ((LPC_UART_TypeDef *)(UART_3))->IER |= 1 << 1; |
donatien | 0:8f57713b2147 | 229 | } |
donatien | 0:8f57713b2147 | 230 | else |
donatien | 0:8f57713b2147 | 231 | { |
donatien | 0:8f57713b2147 | 232 | ((LPC_UART_TypeDef *)(UART_3))->IER &= ~(1 << 1); |
donatien | 0:8f57713b2147 | 233 | } |
donatien | 0:8f57713b2147 | 234 | } |
donatien | 0:8f57713b2147 | 235 | |
donatien | 0:8f57713b2147 | 236 | void IOSerialStream::writeable() //Callback from m_serial when new space is available |
donatien | 0:8f57713b2147 | 237 | { |
donatien | 0:8f57713b2147 | 238 | if(m_outBuf.isEmpty()) |
donatien | 0:8f57713b2147 | 239 | { |
donatien | 0:8f57713b2147 | 240 | m_serialTxFifoEmpty = true; |
donatien | 0:8f57713b2147 | 241 | } |
donatien | 0:8f57713b2147 | 242 | else |
donatien | 0:8f57713b2147 | 243 | { |
donatien | 0:8f57713b2147 | 244 | while(m_serial.writeable() && !m_outBuf.isEmpty()) |
donatien | 0:8f57713b2147 | 245 | { |
donatien | 0:8f57713b2147 | 246 | uint8_t c; |
donatien | 0:8f57713b2147 | 247 | m_outBuf.dequeue(&c); |
donatien | 0:8f57713b2147 | 248 | //m_serial.putc((char)c); |
donatien | 0:8f57713b2147 | 249 | ((LPC_UART_TypeDef *)UART_3)->THR = c; //FIXME awful kludge |
donatien | 0:8f57713b2147 | 250 | } |
donatien | 0:8f57713b2147 | 251 | } |
donatien | 0:8f57713b2147 | 252 | m_spaceSphre.release(); //Force exiting the waiting state |
donatien | 0:8f57713b2147 | 253 | } |