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