fork of VodafoneUSBModem with updated USBHost library

Dependencies:   Socket USBHost lwip-sys lwip

Dependents:   VodafoneUSBModemSMSTest

Committer:
donatien
Date:
Thu Nov 22 15:04:30 2012 +0000
Revision:
64:a44f64dc2879
Parent:
62:6f42a974eea6
Child:
68:07ac4e1ea5b0
Fix for compatibility with mbed rev. 44 library

Who changed what in which revision?

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