Axeda Corp
/
AxedaGo-ubloxC027
Axeda demo software for u-blox C027 (GSM)
C027_Support/SerialPipe.cpp@0:a725e8eab383, 2014-08-11 (annotated)
- Committer:
- AxedaCorp
- Date:
- Mon Aug 11 19:02:42 2014 +0000
- Revision:
- 0:a725e8eab383
1st commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AxedaCorp | 0:a725e8eab383 | 1 | #pragma once |
AxedaCorp | 0:a725e8eab383 | 2 | |
AxedaCorp | 0:a725e8eab383 | 3 | #include "SerialPipe.h" |
AxedaCorp | 0:a725e8eab383 | 4 | |
AxedaCorp | 0:a725e8eab383 | 5 | SerialPipe::SerialPipe(PinName tx, PinName rx, int rxSize, int txSize) : |
AxedaCorp | 0:a725e8eab383 | 6 | _SerialPipeBase(tx,rx), |
AxedaCorp | 0:a725e8eab383 | 7 | _pipeRx( (rx!=NC) ? rxSize : 0), |
AxedaCorp | 0:a725e8eab383 | 8 | _pipeTx( (tx!=NC) ? txSize : 0) |
AxedaCorp | 0:a725e8eab383 | 9 | { |
AxedaCorp | 0:a725e8eab383 | 10 | if (rx!=NC) |
AxedaCorp | 0:a725e8eab383 | 11 | attach(this, &SerialPipe::rxIrqBuf, RxIrq); |
AxedaCorp | 0:a725e8eab383 | 12 | } |
AxedaCorp | 0:a725e8eab383 | 13 | |
AxedaCorp | 0:a725e8eab383 | 14 | SerialPipe::~SerialPipe(void) |
AxedaCorp | 0:a725e8eab383 | 15 | { |
AxedaCorp | 0:a725e8eab383 | 16 | attach(NULL, RxIrq); |
AxedaCorp | 0:a725e8eab383 | 17 | attach(NULL, TxIrq); |
AxedaCorp | 0:a725e8eab383 | 18 | } |
AxedaCorp | 0:a725e8eab383 | 19 | |
AxedaCorp | 0:a725e8eab383 | 20 | // tx channel |
AxedaCorp | 0:a725e8eab383 | 21 | int SerialPipe::writeable(void) |
AxedaCorp | 0:a725e8eab383 | 22 | { |
AxedaCorp | 0:a725e8eab383 | 23 | return _pipeTx.free(); |
AxedaCorp | 0:a725e8eab383 | 24 | } |
AxedaCorp | 0:a725e8eab383 | 25 | |
AxedaCorp | 0:a725e8eab383 | 26 | int SerialPipe::putc(int c) |
AxedaCorp | 0:a725e8eab383 | 27 | { |
AxedaCorp | 0:a725e8eab383 | 28 | c = _pipeTx.putc(c); |
AxedaCorp | 0:a725e8eab383 | 29 | txStart(); |
AxedaCorp | 0:a725e8eab383 | 30 | return c; |
AxedaCorp | 0:a725e8eab383 | 31 | } |
AxedaCorp | 0:a725e8eab383 | 32 | |
AxedaCorp | 0:a725e8eab383 | 33 | int SerialPipe::put(const void* buffer, int length, bool blocking) |
AxedaCorp | 0:a725e8eab383 | 34 | { |
AxedaCorp | 0:a725e8eab383 | 35 | int count = length; |
AxedaCorp | 0:a725e8eab383 | 36 | const char* ptr = (const char*)buffer; |
AxedaCorp | 0:a725e8eab383 | 37 | if (count) |
AxedaCorp | 0:a725e8eab383 | 38 | { |
AxedaCorp | 0:a725e8eab383 | 39 | do |
AxedaCorp | 0:a725e8eab383 | 40 | { |
AxedaCorp | 0:a725e8eab383 | 41 | int written = _pipeTx.put(ptr, count, false); |
AxedaCorp | 0:a725e8eab383 | 42 | if (written) { |
AxedaCorp | 0:a725e8eab383 | 43 | ptr += written; |
AxedaCorp | 0:a725e8eab383 | 44 | count -= written; |
AxedaCorp | 0:a725e8eab383 | 45 | txStart(); |
AxedaCorp | 0:a725e8eab383 | 46 | } |
AxedaCorp | 0:a725e8eab383 | 47 | else if (!blocking) |
AxedaCorp | 0:a725e8eab383 | 48 | break; |
AxedaCorp | 0:a725e8eab383 | 49 | /* nothing / just wait */; |
AxedaCorp | 0:a725e8eab383 | 50 | } |
AxedaCorp | 0:a725e8eab383 | 51 | while (count); |
AxedaCorp | 0:a725e8eab383 | 52 | } |
AxedaCorp | 0:a725e8eab383 | 53 | return (length - count); |
AxedaCorp | 0:a725e8eab383 | 54 | } |
AxedaCorp | 0:a725e8eab383 | 55 | |
AxedaCorp | 0:a725e8eab383 | 56 | void SerialPipe::txCopy(void) |
AxedaCorp | 0:a725e8eab383 | 57 | { |
AxedaCorp | 0:a725e8eab383 | 58 | while (_SerialPipeBase::writeable() && _pipeTx.readable()) |
AxedaCorp | 0:a725e8eab383 | 59 | { |
AxedaCorp | 0:a725e8eab383 | 60 | char c = _pipeTx.getc(); |
AxedaCorp | 0:a725e8eab383 | 61 | _SerialPipeBase::_base_putc(c); |
AxedaCorp | 0:a725e8eab383 | 62 | } |
AxedaCorp | 0:a725e8eab383 | 63 | } |
AxedaCorp | 0:a725e8eab383 | 64 | |
AxedaCorp | 0:a725e8eab383 | 65 | void SerialPipe::txIrqBuf(void) |
AxedaCorp | 0:a725e8eab383 | 66 | { |
AxedaCorp | 0:a725e8eab383 | 67 | txCopy(); |
AxedaCorp | 0:a725e8eab383 | 68 | // detach tx isr if we are done |
AxedaCorp | 0:a725e8eab383 | 69 | if (!_pipeTx.readable()) |
AxedaCorp | 0:a725e8eab383 | 70 | attach(NULL, TxIrq); |
AxedaCorp | 0:a725e8eab383 | 71 | } |
AxedaCorp | 0:a725e8eab383 | 72 | |
AxedaCorp | 0:a725e8eab383 | 73 | void SerialPipe::txStart(void) |
AxedaCorp | 0:a725e8eab383 | 74 | { |
AxedaCorp | 0:a725e8eab383 | 75 | // disable the tx isr to avoid interruption |
AxedaCorp | 0:a725e8eab383 | 76 | attach(NULL, TxIrq); |
AxedaCorp | 0:a725e8eab383 | 77 | txCopy(); |
AxedaCorp | 0:a725e8eab383 | 78 | // attach the tx isr to handle the remaining data |
AxedaCorp | 0:a725e8eab383 | 79 | if (_pipeTx.readable()) |
AxedaCorp | 0:a725e8eab383 | 80 | attach(this, &SerialPipe::txIrqBuf, TxIrq); |
AxedaCorp | 0:a725e8eab383 | 81 | } |
AxedaCorp | 0:a725e8eab383 | 82 | |
AxedaCorp | 0:a725e8eab383 | 83 | // rx channel |
AxedaCorp | 0:a725e8eab383 | 84 | int SerialPipe::readable(void) |
AxedaCorp | 0:a725e8eab383 | 85 | { |
AxedaCorp | 0:a725e8eab383 | 86 | return _pipeRx.readable(); |
AxedaCorp | 0:a725e8eab383 | 87 | } |
AxedaCorp | 0:a725e8eab383 | 88 | |
AxedaCorp | 0:a725e8eab383 | 89 | int SerialPipe::getc(void) |
AxedaCorp | 0:a725e8eab383 | 90 | { |
AxedaCorp | 0:a725e8eab383 | 91 | if (!_pipeRx.readable()) |
AxedaCorp | 0:a725e8eab383 | 92 | return EOF; |
AxedaCorp | 0:a725e8eab383 | 93 | return _pipeRx.getc(); |
AxedaCorp | 0:a725e8eab383 | 94 | } |
AxedaCorp | 0:a725e8eab383 | 95 | |
AxedaCorp | 0:a725e8eab383 | 96 | int SerialPipe::get(void* buffer, int length, bool blocking) |
AxedaCorp | 0:a725e8eab383 | 97 | { |
AxedaCorp | 0:a725e8eab383 | 98 | return _pipeRx.get((char*)buffer,length,blocking); |
AxedaCorp | 0:a725e8eab383 | 99 | } |
AxedaCorp | 0:a725e8eab383 | 100 | |
AxedaCorp | 0:a725e8eab383 | 101 | void SerialPipe::rxIrqBuf(void) |
AxedaCorp | 0:a725e8eab383 | 102 | { |
AxedaCorp | 0:a725e8eab383 | 103 | while (_SerialPipeBase::readable()) |
AxedaCorp | 0:a725e8eab383 | 104 | { |
AxedaCorp | 0:a725e8eab383 | 105 | char c = _SerialPipeBase::_base_getc(); |
AxedaCorp | 0:a725e8eab383 | 106 | if (_pipeRx.writeable()) |
AxedaCorp | 0:a725e8eab383 | 107 | _pipeRx.putc(c); |
AxedaCorp | 0:a725e8eab383 | 108 | else |
AxedaCorp | 0:a725e8eab383 | 109 | /* overflow */; |
AxedaCorp | 0:a725e8eab383 | 110 | } |
AxedaCorp | 0:a725e8eab383 | 111 | } |
AxedaCorp | 0:a725e8eab383 | 112 |