fork of VodafoneUSBModem with updated USBHost library
Dependencies: Socket USBHost lwip-sys lwip
Dependents: VodafoneUSBModemSMSTest
IOSerialStream.cpp
00001 /* IOSerialStream.cpp */ 00002 /* Copyright (C) 2012 mbed.org, MIT License 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00005 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00006 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00007 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00008 * furnished to do so, subject to the following conditions: 00009 * 00010 * The above copyright notice and this permission notice shall be included in all copies or 00011 * substantial portions of the Software. 00012 * 00013 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00014 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00015 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00016 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00017 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00018 */ 00019 00020 #define __DEBUG__ 0 //Maximum verbosity 00021 #ifndef __MODULE__ 00022 #define __MODULE__ "IOSerialStream.cpp" 00023 #endif 00024 00025 #include "core/fwk.h" 00026 00027 #include <stdio.h> 00028 00029 #include "IOSerialStream.h" 00030 00031 IOSerialStream::IOSerialStream(mbed::Serial& serial) : m_serial(serial), m_serialTxFifoEmpty(true), 00032 m_availableSphre(1), m_spaceSphre(1), m_inBuf(), m_outBuf() 00033 { 00034 m_availableSphre.wait(); 00035 m_spaceSphre.wait(); 00036 //Attach interrupts 00037 m_serial.attach(this, &IOSerialStream::readable, mbed::Serial::RxIrq); 00038 m_serial.attach(this, &IOSerialStream::writeable, mbed::Serial::TxIrq); 00039 } 00040 00041 /*virtual*/ IOSerialStream::~IOSerialStream() 00042 { 00043 m_serial.attach(NULL, mbed::Serial::RxIrq); 00044 m_serial.attach(NULL, mbed::Serial::TxIrq); 00045 } 00046 00047 //0 for non-blocking (returns immediately), osWaitForever for infinite blocking 00048 /*virtual*/ int IOSerialStream::read(uint8_t* buf, size_t* pLength, size_t maxLength, uint32_t timeout/*=osWaitForever*/) 00049 { 00050 DBG("Trying to read at most %d chars", maxLength); 00051 int ret = waitAvailable(timeout); 00052 if(ret) 00053 { 00054 WARN("Error %d while waiting for incoming data", ret); 00055 return ret; 00056 } 00057 int readLen = MIN( available(), maxLength ); 00058 *pLength = readLen; 00059 setupReadableISR(false); 00060 while(readLen--) 00061 { 00062 m_inBuf.dequeue(buf); 00063 buf++; 00064 } 00065 setupReadableISR(true); 00066 DBG("Read %d chars successfully", *pLength); 00067 return OK; 00068 } 00069 00070 /*virtual*/ size_t IOSerialStream::available() 00071 { 00072 setupReadableISR(false); //m_inBuf.available() is not reentrant 00073 size_t len = m_inBuf.available(); 00074 setupReadableISR(true); 00075 return len; 00076 } 00077 00078 /*virtual*/ int IOSerialStream::waitAvailable(uint32_t timeout/*=osWaitForever*/) //Wait for data to be available 00079 { 00080 int ret; 00081 if(available()) //Is data already available? 00082 { 00083 m_availableSphre.wait(0); //Clear the queue as data is available 00084 return OK; 00085 } 00086 00087 DBG("Waiting for data availability %d ms (-1 is infinite)", timeout); 00088 ret = m_availableSphre.wait(timeout); //Wait for data to arrive or for abort 00089 if(ret <= 0) 00090 { 00091 DBG("Timeout"); 00092 return NET_TIMEOUT; 00093 } 00094 if(!available()) //Even if abort has been called, return that data is available 00095 { 00096 DBG("Aborted"); 00097 return NET_INTERRUPTED; 00098 } 00099 DBG("Finished waiting"); 00100 m_availableSphre.wait(0); //Clear the queue as data is available 00101 return OK; 00102 } 00103 00104 /*virtual*/ int IOSerialStream::abortRead() //Abort current reading (or waiting) operation 00105 { 00106 if( !available() ) //If there is data pending, no need to abort 00107 { 00108 m_availableSphre.release(); //Force exiting the waiting state; kludge to pass a RC directly 00109 } 00110 else 00111 { 00112 DBG("Serial is readable"); ; 00113 } 00114 return OK; 00115 } 00116 00117 void IOSerialStream::setupReadableISR(bool en) 00118 { 00119 if(en) 00120 { 00121 ((LPC_UART_TypeDef *)(UART_3))->IER |= 1 << 0; 00122 } 00123 else 00124 { 00125 ((LPC_UART_TypeDef *)(UART_3))->IER &= ~(1 << 0); 00126 } 00127 } 00128 00129 void IOSerialStream::readable() //Callback from m_serial when new data is available 00130 { 00131 do 00132 { 00133 m_inBuf.queue(((LPC_UART_TypeDef *)UART_3)->RBR); //FIXME mbed libraries this is an awful kludge 00134 } while(m_serial.readable()); 00135 m_availableSphre.release(); //Force exiting the waiting state 00136 } 00137 00138 //0 for non-blocking (returns immediately), osWaitForever for infinite blocking 00139 /*virtual*/ int IOSerialStream::write(uint8_t* buf, size_t length, uint32_t timeout/*=osWaitForever*/) 00140 { 00141 DBG("Trying to write %d chars", length); 00142 int ret = waitSpace(timeout); 00143 if(ret) 00144 { 00145 WARN("Error %d while waiting for space", ret); 00146 return ret; 00147 } 00148 DBG("Writing %d chars", length); 00149 setupWriteableISR(false); 00150 while(length) 00151 { 00152 m_outBuf.queue(*buf); 00153 buf++; 00154 length--; 00155 if(length && !space()) 00156 { 00157 DBG("Waiting to write remaining %d chars", length); 00158 setupWriteableISR(true); 00159 ret = waitSpace(timeout); 00160 if(ret) 00161 { 00162 WARN("Error %d while waiting for space", ret); 00163 return ret; 00164 } 00165 setupWriteableISR(false); 00166 } 00167 } 00168 //If m_serial tx fifo is empty we need to manually tx a byte in order to trigger the interrupt 00169 if( m_outBuf.available() && m_serialTxFifoEmpty ) 00170 { 00171 m_serialTxFifoEmpty = false; 00172 uint8_t c; 00173 m_outBuf.dequeue(&c); 00174 //m_serial.putc((char)c); 00175 ((LPC_UART_TypeDef *)UART_3)->THR = c; //FIXME awful kludge 00176 } 00177 setupWriteableISR(true); 00178 DBG("Write successful"); 00179 return OK; 00180 } 00181 00182 /*virtual*/ size_t IOSerialStream::space() 00183 { 00184 setupWriteableISR(false); //m_outBuf.available() is not reentrant 00185 size_t len = CIRCBUF_SIZE - m_outBuf.available(); 00186 setupWriteableISR(true); 00187 return len; 00188 } 00189 00190 /*virtual*/ int IOSerialStream::waitSpace(uint32_t timeout/*=osWaitForever*/) //Wait for space to be available 00191 { 00192 int ret; 00193 if(space()) //Is still space already left? 00194 { 00195 m_spaceSphre.wait(0); //Clear the queue as space is available 00196 return OK; 00197 } 00198 00199 DBG("Waiting for data space %d ms (-1 is infinite)", timeout); 00200 ret = m_spaceSphre.wait(timeout); //Wait for space to be made or for abort 00201 if(ret <= 0) 00202 { 00203 DBG("Timeout"); 00204 return NET_TIMEOUT; 00205 } 00206 if(!space()) //Even if abort has been called, return that space is available 00207 { 00208 DBG("Aborted"); 00209 return NET_INTERRUPTED; 00210 } 00211 m_spaceSphre.wait(0); //Clear the queue as space is available 00212 return OK; 00213 } 00214 00215 /*virtual*/ int IOSerialStream::abortWrite() //Abort current writing (or waiting) operation 00216 { 00217 if( !space() ) //If there is space left, no need to abort 00218 { 00219 m_spaceSphre.release(); //Force exiting the waiting state 00220 } 00221 return OK; 00222 } 00223 00224 void IOSerialStream::setupWriteableISR(bool en) 00225 { 00226 if(en) 00227 { 00228 ((LPC_UART_TypeDef *)(UART_3))->IER |= 1 << 1; 00229 } 00230 else 00231 { 00232 ((LPC_UART_TypeDef *)(UART_3))->IER &= ~(1 << 1); 00233 } 00234 } 00235 00236 void IOSerialStream::writeable() //Callback from m_serial when new space is available 00237 { 00238 if(m_outBuf.isEmpty()) 00239 { 00240 m_serialTxFifoEmpty = true; 00241 } 00242 else 00243 { 00244 while(m_serial.writeable() && !m_outBuf.isEmpty()) 00245 { 00246 uint8_t c; 00247 m_outBuf.dequeue(&c); 00248 //m_serial.putc((char)c); 00249 ((LPC_UART_TypeDef *)UART_3)->THR = c; //FIXME awful kludge 00250 } 00251 } 00252 m_spaceSphre.release(); //Force exiting the waiting state 00253 }
Generated on Wed Jul 13 2022 19:57:20 by 1.7.2