Own fork of C027_Support
Dependents: MbedSmartRestMain MbedSmartRestMain
Fork of C027_Support by
SerialPipe.cpp@95:8282dbbe1492, 2014-06-17 (annotated)
- Committer:
- mazgch
- Date:
- Tue Jun 17 07:03:48 2014 +0000
- Revision:
- 95:8282dbbe1492
- Parent:
- 75:ce6e12067d0c
- Child:
- 116:709a6386e685
added use of mutex and wait for Rtos systems
Who changed what in which revision?
User | Revision | Line number | New 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 | 75:ce6e12067d0c | 42 | if (written) { |
mazgch | 75:ce6e12067d0c | 43 | ptr += written; |
mazgch | 75:ce6e12067d0c | 44 | count -= written; |
mazgch | 75:ce6e12067d0c | 45 | txStart(); |
mazgch | 75:ce6e12067d0c | 46 | } |
mazgch | 75:ce6e12067d0c | 47 | else if (!blocking) |
mazgch | 75:ce6e12067d0c | 48 | break; |
mazgch | 95:8282dbbe1492 | 49 | /* nothing / just wait */; |
mazgch | 15:5eda64e5b9d1 | 50 | } |
mazgch | 75:ce6e12067d0c | 51 | while (count); |
mazgch | 9:e7a5959ffae1 | 52 | } |
mazgch | 13:e2446fcdc246 | 53 | return (length - count); |
mazgch | 13:e2446fcdc246 | 54 | } |
mazgch | 13:e2446fcdc246 | 55 | |
mazgch | 70:0a87d256cd24 | 56 | void SerialPipe::txCopy(void) |
mazgch | 9:e7a5959ffae1 | 57 | { |
mazgch | 15:5eda64e5b9d1 | 58 | while (_SerialPipeBase::writeable() && _pipeTx.readable()) |
mazgch | 11:b084552b03fe | 59 | { |
mazgch | 13:e2446fcdc246 | 60 | char c = _pipeTx.getc(); |
mazgch | 15:5eda64e5b9d1 | 61 | _SerialPipeBase::_base_putc(c); |
mazgch | 11:b084552b03fe | 62 | } |
mazgch | 9:e7a5959ffae1 | 63 | } |
mazgch | 9:e7a5959ffae1 | 64 | |
mazgch | 70:0a87d256cd24 | 65 | void SerialPipe::txIrqBuf(void) |
mazgch | 70:0a87d256cd24 | 66 | { |
mazgch | 70:0a87d256cd24 | 67 | txCopy(); |
mazgch | 70:0a87d256cd24 | 68 | // detach tx isr if we are done |
mazgch | 70:0a87d256cd24 | 69 | if (!_pipeTx.readable()) |
mazgch | 70:0a87d256cd24 | 70 | attach(NULL, TxIrq); |
mazgch | 70:0a87d256cd24 | 71 | } |
mazgch | 70:0a87d256cd24 | 72 | |
mazgch | 13:e2446fcdc246 | 73 | void SerialPipe::txStart(void) |
mazgch | 13:e2446fcdc246 | 74 | { |
mazgch | 70:0a87d256cd24 | 75 | // disable the tx isr to avoid interruption |
mazgch | 70:0a87d256cd24 | 76 | attach(NULL, TxIrq); |
mazgch | 70:0a87d256cd24 | 77 | txCopy(); |
mazgch | 70:0a87d256cd24 | 78 | // attach the tx isr to handle the remaining data |
mazgch | 70:0a87d256cd24 | 79 | if (_pipeTx.readable()) |
mazgch | 70:0a87d256cd24 | 80 | attach(this, &SerialPipe::txIrqBuf, TxIrq); |
mazgch | 13:e2446fcdc246 | 81 | } |
mazgch | 13:e2446fcdc246 | 82 | |
mazgch | 9:e7a5959ffae1 | 83 | // rx channel |
mazgch | 9:e7a5959ffae1 | 84 | int SerialPipe::readable(void) |
mazgch | 9:e7a5959ffae1 | 85 | { |
mazgch | 9:e7a5959ffae1 | 86 | return _pipeRx.readable(); |
mazgch | 9:e7a5959ffae1 | 87 | } |
mazgch | 9:e7a5959ffae1 | 88 | |
mazgch | 9:e7a5959ffae1 | 89 | int SerialPipe::getc(void) |
mazgch | 9:e7a5959ffae1 | 90 | { |
mazgch | 15:5eda64e5b9d1 | 91 | if (!_pipeRx.readable()) |
mazgch | 15:5eda64e5b9d1 | 92 | return EOF; |
mazgch | 15:5eda64e5b9d1 | 93 | return _pipeRx.getc(); |
mazgch | 13:e2446fcdc246 | 94 | } |
mazgch | 13:e2446fcdc246 | 95 | |
mazgch | 13:e2446fcdc246 | 96 | int SerialPipe::get(void* buffer, int length, bool blocking) |
mazgch | 13:e2446fcdc246 | 97 | { |
mazgch | 13:e2446fcdc246 | 98 | return _pipeRx.get((char*)buffer,length,blocking); |
mazgch | 13:e2446fcdc246 | 99 | } |
mazgch | 13:e2446fcdc246 | 100 | |
mazgch | 9:e7a5959ffae1 | 101 | void SerialPipe::rxIrqBuf(void) |
mazgch | 9:e7a5959ffae1 | 102 | { |
mazgch | 15:5eda64e5b9d1 | 103 | while (_SerialPipeBase::readable()) |
mazgch | 9:e7a5959ffae1 | 104 | { |
mazgch | 15:5eda64e5b9d1 | 105 | char c = _SerialPipeBase::_base_getc(); |
mazgch | 9:e7a5959ffae1 | 106 | if (_pipeRx.writeable()) |
mazgch | 13:e2446fcdc246 | 107 | _pipeRx.putc(c); |
mazgch | 9:e7a5959ffae1 | 108 | else |
mazgch | 9:e7a5959ffae1 | 109 | /* overflow */; |
mazgch | 9:e7a5959ffae1 | 110 | } |
mazgch | 9:e7a5959ffae1 | 111 | } |
mazgch | 9:e7a5959ffae1 | 112 |