C027_SupportTest_xively_locationで使用しているC027用ライブラリ

Fork of C027_Support by u-blox

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialPipe.cpp Source File

SerialPipe.cpp

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