* add C027_Support fork
Fork of C027_Support by
SerialPipe.cpp@18:e5697801df29, 2014-03-14 (annotated)
- Committer:
- mazgch
- Date:
- Fri Mar 14 13:07:48 2014 +0000
- Revision:
- 18:e5697801df29
- Parent:
- 17:296d94a006b4
- Child:
- 19:2b5d097ca15d
extend Api for GPS and GNSS
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 | 15:5eda64e5b9d1 | 5 | SerialPipe::SerialPipe(PinName tx, PinName rx, int rxSize, int txSize) |
mazgch | 15:5eda64e5b9d1 | 6 | : _SerialPipeBase(tx,rx), _pipeRx(rxSize), _pipeTx(txSize) |
mazgch | 9:e7a5959ffae1 | 7 | { |
mazgch | 9:e7a5959ffae1 | 8 | attach(this, &SerialPipe::rxIrqBuf, RxIrq); |
mazgch | 9:e7a5959ffae1 | 9 | attach(this, &SerialPipe::txIrqBuf, TxIrq); |
mazgch | 9:e7a5959ffae1 | 10 | } |
mazgch | 9:e7a5959ffae1 | 11 | |
mazgch | 17:296d94a006b4 | 12 | SerialPipe::SerialPipe(PinName tx, PinName rx, PinName rts, PinName cts, |
mazgch | 17:296d94a006b4 | 13 | int rxSize, int txSize) |
mazgch | 17:296d94a006b4 | 14 | : _SerialPipeBase(tx,rx), _pipeRx(rxSize), _pipeTx(txSize) |
mazgch | 17:296d94a006b4 | 15 | { |
mazgch | 17:296d94a006b4 | 16 | attach(this, &SerialPipe::rxIrqBuf, RxIrq); |
mazgch | 17:296d94a006b4 | 17 | attach(this, &SerialPipe::txIrqBuf, TxIrq); |
mazgch | 17:296d94a006b4 | 18 | |
mazgch | 17:296d94a006b4 | 19 | set_flow_control(RTSCTS, rts, cts); |
mazgch | 17:296d94a006b4 | 20 | } |
mazgch | 17:296d94a006b4 | 21 | |
mazgch | 9:e7a5959ffae1 | 22 | SerialPipe::~SerialPipe(void) |
mazgch | 9:e7a5959ffae1 | 23 | { |
mazgch | 9:e7a5959ffae1 | 24 | attach(NULL, RxIrq); |
mazgch | 9:e7a5959ffae1 | 25 | attach(NULL, TxIrq); |
mazgch | 9:e7a5959ffae1 | 26 | } |
mazgch | 9:e7a5959ffae1 | 27 | |
mazgch | 9:e7a5959ffae1 | 28 | // tx channel |
mazgch | 13:e2446fcdc246 | 29 | int SerialPipe::writeable(void) |
mazgch | 13:e2446fcdc246 | 30 | { |
mazgch | 13:e2446fcdc246 | 31 | return _pipeTx.free(); |
mazgch | 13:e2446fcdc246 | 32 | } |
mazgch | 13:e2446fcdc246 | 33 | |
mazgch | 13:e2446fcdc246 | 34 | int SerialPipe::putc(int c) |
mazgch | 13:e2446fcdc246 | 35 | { |
mazgch | 15:5eda64e5b9d1 | 36 | c = _pipeTx.putc(c); |
mazgch | 15:5eda64e5b9d1 | 37 | txStart(); |
mazgch | 15:5eda64e5b9d1 | 38 | return c; |
mazgch | 13:e2446fcdc246 | 39 | } |
mazgch | 13:e2446fcdc246 | 40 | |
mazgch | 13:e2446fcdc246 | 41 | int SerialPipe::put(const void* buffer, int length, bool blocking) |
mazgch | 9:e7a5959ffae1 | 42 | { |
mazgch | 13:e2446fcdc246 | 43 | int count = length; |
mazgch | 13:e2446fcdc246 | 44 | const char* ptr = (const char*)buffer; |
mazgch | 15:5eda64e5b9d1 | 45 | if (count) |
mazgch | 9:e7a5959ffae1 | 46 | { |
mazgch | 15:5eda64e5b9d1 | 47 | do |
mazgch | 15:5eda64e5b9d1 | 48 | { |
mazgch | 15:5eda64e5b9d1 | 49 | int written = _pipeTx.put(ptr, count, false); |
mazgch | 15:5eda64e5b9d1 | 50 | ptr += written; |
mazgch | 15:5eda64e5b9d1 | 51 | count -= written; |
mazgch | 15:5eda64e5b9d1 | 52 | txStart(); |
mazgch | 15:5eda64e5b9d1 | 53 | } |
mazgch | 15:5eda64e5b9d1 | 54 | while (count && blocking); |
mazgch | 9:e7a5959ffae1 | 55 | } |
mazgch | 13:e2446fcdc246 | 56 | return (length - count); |
mazgch | 13:e2446fcdc246 | 57 | } |
mazgch | 13:e2446fcdc246 | 58 | |
mazgch | 9:e7a5959ffae1 | 59 | void SerialPipe::txIrqBuf(void) |
mazgch | 9:e7a5959ffae1 | 60 | { |
mazgch | 15:5eda64e5b9d1 | 61 | while (_SerialPipeBase::writeable() && _pipeTx.readable()) |
mazgch | 11:b084552b03fe | 62 | { |
mazgch | 13:e2446fcdc246 | 63 | char c = _pipeTx.getc(); |
mazgch | 15:5eda64e5b9d1 | 64 | _SerialPipeBase::_base_putc(c); |
mazgch | 11:b084552b03fe | 65 | } |
mazgch | 9:e7a5959ffae1 | 66 | } |
mazgch | 9:e7a5959ffae1 | 67 | |
mazgch | 13:e2446fcdc246 | 68 | void SerialPipe::txStart(void) |
mazgch | 13:e2446fcdc246 | 69 | { |
mazgch | 13:e2446fcdc246 | 70 | __disable_irq(); |
mazgch | 13:e2446fcdc246 | 71 | txIrqBuf(); |
mazgch | 13:e2446fcdc246 | 72 | __enable_irq(); |
mazgch | 13:e2446fcdc246 | 73 | } |
mazgch | 13:e2446fcdc246 | 74 | |
mazgch | 9:e7a5959ffae1 | 75 | // rx channel |
mazgch | 9:e7a5959ffae1 | 76 | int SerialPipe::readable(void) |
mazgch | 9:e7a5959ffae1 | 77 | { |
mazgch | 9:e7a5959ffae1 | 78 | return _pipeRx.readable(); |
mazgch | 9:e7a5959ffae1 | 79 | } |
mazgch | 9:e7a5959ffae1 | 80 | |
mazgch | 9:e7a5959ffae1 | 81 | int SerialPipe::getc(void) |
mazgch | 9:e7a5959ffae1 | 82 | { |
mazgch | 15:5eda64e5b9d1 | 83 | if (!_pipeRx.readable()) |
mazgch | 15:5eda64e5b9d1 | 84 | return EOF; |
mazgch | 15:5eda64e5b9d1 | 85 | return _pipeRx.getc(); |
mazgch | 13:e2446fcdc246 | 86 | } |
mazgch | 13:e2446fcdc246 | 87 | |
mazgch | 13:e2446fcdc246 | 88 | int SerialPipe::get(void* buffer, int length, bool blocking) |
mazgch | 13:e2446fcdc246 | 89 | { |
mazgch | 13:e2446fcdc246 | 90 | return _pipeRx.get((char*)buffer,length,blocking); |
mazgch | 13:e2446fcdc246 | 91 | } |
mazgch | 13:e2446fcdc246 | 92 | |
mazgch | 9:e7a5959ffae1 | 93 | void SerialPipe::rxIrqBuf(void) |
mazgch | 9:e7a5959ffae1 | 94 | { |
mazgch | 15:5eda64e5b9d1 | 95 | while (_SerialPipeBase::readable()) |
mazgch | 9:e7a5959ffae1 | 96 | { |
mazgch | 15:5eda64e5b9d1 | 97 | char c = _SerialPipeBase::_base_getc(); |
mazgch | 9:e7a5959ffae1 | 98 | if (_pipeRx.writeable()) |
mazgch | 13:e2446fcdc246 | 99 | _pipeRx.putc(c); |
mazgch | 9:e7a5959ffae1 | 100 | else |
mazgch | 9:e7a5959ffae1 | 101 | /* overflow */; |
mazgch | 9:e7a5959ffae1 | 102 | } |
mazgch | 9:e7a5959ffae1 | 103 | } |
mazgch | 9:e7a5959ffae1 | 104 |