C027 updated to work with latest mBed libraries

Dependents:   Cellular_HelloMQTT UBLOXModemDriver UBLOXMQTTDriver

Fork of C027_Support by u-blox

Committer:
mazgch
Date:
Thu May 15 22:20:42 2014 +0000
Revision:
74:208e3e32d263
Parent:
70:0a87d256cd24
Child:
75:ce6e12067d0c
initial version of restructured u-blox library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mazgch 9:e7a5959ffae1 1 #pragma once
mazgch 9:e7a5959ffae1 2
mazgch 9:e7a5959ffae1 3 #include "SerialPipe.h"
mazgch 9:e7a5959ffae1 4
mazgch 74:208e3e32d263 5 SerialPipe::SerialPipe(PinName tx, PinName rx, int rxSize, int txSize) :
mazgch 74:208e3e32d263 6 _SerialPipeBase(tx,rx),
mazgch 74:208e3e32d263 7 _pipeRx( (rx!=NC) ? rxSize : 0),
mazgch 74:208e3e32d263 8 _pipeTx( (tx!=NC) ? txSize : 0)
mazgch 9:e7a5959ffae1 9 {
mazgch 74:208e3e32d263 10 if (rx!=NC)
mazgch 74:208e3e32d263 11 attach(this, &SerialPipe::rxIrqBuf, RxIrq);
mazgch 9:e7a5959ffae1 12 }
mazgch 9:e7a5959ffae1 13
mazgch 9:e7a5959ffae1 14 SerialPipe::~SerialPipe(void)
mazgch 9:e7a5959ffae1 15 {
mazgch 9:e7a5959ffae1 16 attach(NULL, RxIrq);
mazgch 9:e7a5959ffae1 17 attach(NULL, TxIrq);
mazgch 9:e7a5959ffae1 18 }
mazgch 9:e7a5959ffae1 19
mazgch 9:e7a5959ffae1 20 // tx channel
mazgch 13:e2446fcdc246 21 int SerialPipe::writeable(void)
mazgch 13:e2446fcdc246 22 {
mazgch 13:e2446fcdc246 23 return _pipeTx.free();
mazgch 13:e2446fcdc246 24 }
mazgch 13:e2446fcdc246 25
mazgch 13:e2446fcdc246 26 int SerialPipe::putc(int c)
mazgch 13:e2446fcdc246 27 {
mazgch 15:5eda64e5b9d1 28 c = _pipeTx.putc(c);
mazgch 15:5eda64e5b9d1 29 txStart();
mazgch 15:5eda64e5b9d1 30 return c;
mazgch 13:e2446fcdc246 31 }
mazgch 13:e2446fcdc246 32
mazgch 13:e2446fcdc246 33 int SerialPipe::put(const void* buffer, int length, bool blocking)
mazgch 9:e7a5959ffae1 34 {
mazgch 13:e2446fcdc246 35 int count = length;
mazgch 13:e2446fcdc246 36 const char* ptr = (const char*)buffer;
mazgch 15:5eda64e5b9d1 37 if (count)
mazgch 9:e7a5959ffae1 38 {
mazgch 15:5eda64e5b9d1 39 do
mazgch 15:5eda64e5b9d1 40 {
mazgch 15:5eda64e5b9d1 41 int written = _pipeTx.put(ptr, count, false);
mazgch 15:5eda64e5b9d1 42 ptr += written;
mazgch 15:5eda64e5b9d1 43 count -= written;
mazgch 15:5eda64e5b9d1 44 txStart();
mazgch 15:5eda64e5b9d1 45 }
mazgch 15:5eda64e5b9d1 46 while (count && blocking);
mazgch 9:e7a5959ffae1 47 }
mazgch 13:e2446fcdc246 48 return (length - count);
mazgch 13:e2446fcdc246 49 }
mazgch 13:e2446fcdc246 50
mazgch 70:0a87d256cd24 51 void SerialPipe::txCopy(void)
mazgch 9:e7a5959ffae1 52 {
mazgch 15:5eda64e5b9d1 53 while (_SerialPipeBase::writeable() && _pipeTx.readable())
mazgch 11:b084552b03fe 54 {
mazgch 13:e2446fcdc246 55 char c = _pipeTx.getc();
mazgch 15:5eda64e5b9d1 56 _SerialPipeBase::_base_putc(c);
mazgch 11:b084552b03fe 57 }
mazgch 9:e7a5959ffae1 58 }
mazgch 9:e7a5959ffae1 59
mazgch 70:0a87d256cd24 60 void SerialPipe::txIrqBuf(void)
mazgch 70:0a87d256cd24 61 {
mazgch 70:0a87d256cd24 62 txCopy();
mazgch 70:0a87d256cd24 63 // detach tx isr if we are done
mazgch 70:0a87d256cd24 64 if (!_pipeTx.readable())
mazgch 70:0a87d256cd24 65 attach(NULL, TxIrq);
mazgch 70:0a87d256cd24 66 }
mazgch 70:0a87d256cd24 67
mazgch 13:e2446fcdc246 68 void SerialPipe::txStart(void)
mazgch 13:e2446fcdc246 69 {
mazgch 70:0a87d256cd24 70 // disable the tx isr to avoid interruption
mazgch 70:0a87d256cd24 71 attach(NULL, TxIrq);
mazgch 70:0a87d256cd24 72 txCopy();
mazgch 70:0a87d256cd24 73 // attach the tx isr to handle the remaining data
mazgch 70:0a87d256cd24 74 if (_pipeTx.readable())
mazgch 70:0a87d256cd24 75 attach(this, &SerialPipe::txIrqBuf, TxIrq);
mazgch 13:e2446fcdc246 76 }
mazgch 13:e2446fcdc246 77
mazgch 9:e7a5959ffae1 78 // rx channel
mazgch 9:e7a5959ffae1 79 int SerialPipe::readable(void)
mazgch 9:e7a5959ffae1 80 {
mazgch 9:e7a5959ffae1 81 return _pipeRx.readable();
mazgch 9:e7a5959ffae1 82 }
mazgch 9:e7a5959ffae1 83
mazgch 9:e7a5959ffae1 84 int SerialPipe::getc(void)
mazgch 9:e7a5959ffae1 85 {
mazgch 15:5eda64e5b9d1 86 if (!_pipeRx.readable())
mazgch 15:5eda64e5b9d1 87 return EOF;
mazgch 15:5eda64e5b9d1 88 return _pipeRx.getc();
mazgch 13:e2446fcdc246 89 }
mazgch 13:e2446fcdc246 90
mazgch 13:e2446fcdc246 91 int SerialPipe::get(void* buffer, int length, bool blocking)
mazgch 13:e2446fcdc246 92 {
mazgch 13:e2446fcdc246 93 return _pipeRx.get((char*)buffer,length,blocking);
mazgch 13:e2446fcdc246 94 }
mazgch 13:e2446fcdc246 95
mazgch 9:e7a5959ffae1 96 void SerialPipe::rxIrqBuf(void)
mazgch 9:e7a5959ffae1 97 {
mazgch 15:5eda64e5b9d1 98 while (_SerialPipeBase::readable())
mazgch 9:e7a5959ffae1 99 {
mazgch 15:5eda64e5b9d1 100 char c = _SerialPipeBase::_base_getc();
mazgch 9:e7a5959ffae1 101 if (_pipeRx.writeable())
mazgch 13:e2446fcdc246 102 _pipeRx.putc(c);
mazgch 9:e7a5959ffae1 103 else
mazgch 9:e7a5959ffae1 104 /* overflow */;
mazgch 9:e7a5959ffae1 105 }
mazgch 9:e7a5959ffae1 106 }
mazgch 9:e7a5959ffae1 107