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