support library for C027 helper functions for Buffer Pipes, Buffered Serial Port (rtos capable) and GPS parsing. It includes modem APIs for USSD, SMS and Sockets.

Dependents:   HTTPClient_Cellular_HelloWorld Cellular_HelloMQTT MbedSmartRestMain Car_Bon_car_module ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialPipe.cpp Source File

SerialPipe.cpp

00001 #include "SerialPipe.h"
00002 
00003 SerialPipe::SerialPipe(PinName tx, PinName rx, int baudrate, int rxSize, int txSize) : 
00004             _SerialPipeBase(tx,rx,baudrate), 
00005             _pipeRx( (rx!=NC) ? rxSize : 0), 
00006             _pipeTx( (tx!=NC) ? txSize : 0)
00007 {
00008     if (rx!=NC)
00009         attach(this, &SerialPipe::rxIrqBuf, RxIrq);
00010 }
00011 
00012 SerialPipe::~SerialPipe(void)
00013 {
00014     attach(NULL, RxIrq);
00015     attach(NULL, TxIrq);
00016 }
00017 
00018 // tx channel
00019 int SerialPipe::writeable(void)    
00020 {
00021     return _pipeTx.free();
00022 }
00023 
00024 int SerialPipe::putc(int c)    
00025 {
00026     c = _pipeTx.putc(c);
00027     txStart();
00028     return c;
00029 }
00030 
00031 int SerialPipe::put(const void* buffer, int length, bool blocking)    
00032 { 
00033     int count = length;
00034     const char* ptr = (const char*)buffer;
00035     if (count)
00036     {
00037         do
00038         {
00039             int written = _pipeTx.put(ptr, count, false);
00040             if (written) {
00041                 ptr += written;
00042                 count -= written;
00043                 txStart();
00044             }
00045             else if (!blocking)
00046                 break;
00047             /* nothing / just wait */;
00048         }
00049         while (count);
00050     }
00051     return (length - count);
00052 }
00053 
00054 void SerialPipe::txCopy(void)
00055 {
00056     while (_SerialPipeBase::writeable() && _pipeTx.readable())
00057     {
00058         char c = _pipeTx.getc();
00059         _SerialPipeBase::_base_putc(c);
00060     }
00061 }
00062 
00063 void SerialPipe::txIrqBuf(void)
00064 {
00065     txCopy();
00066     // detach tx isr if we are done 
00067     if (!_pipeTx.readable())
00068         attach(NULL, TxIrq);
00069 }
00070 
00071 void SerialPipe::txStart(void)
00072 {
00073     // disable the tx isr to avoid interruption
00074     attach(NULL, TxIrq);
00075     txCopy();
00076     // attach the tx isr to handle the remaining data
00077     if (_pipeTx.readable())
00078         attach(this, &SerialPipe::txIrqBuf, TxIrq);
00079 }
00080 
00081 // rx channel
00082 int SerialPipe::readable(void)                      
00083 { 
00084     return _pipeRx.readable(); 
00085 } 
00086 
00087 int SerialPipe::getc(void)                          
00088 { 
00089     if (!_pipeRx.readable())
00090         return EOF;
00091     return _pipeRx.getc(); 
00092 } 
00093 
00094 int SerialPipe::get(void* buffer, int length, bool blocking) 
00095 { 
00096     return _pipeRx.get ((char*)buffer,length,blocking); 
00097 }
00098 
00099 void SerialPipe::rxIrqBuf(void)
00100 {
00101     while (_SerialPipeBase::readable())
00102     {
00103         char c = _SerialPipeBase::_base_getc();
00104         if (_pipeRx.writeable())
00105             _pipeRx.putc(c);
00106         else 
00107             /* overflow */;
00108     }
00109 }
00110