Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of C027_Support_New by
SerialPipe.cpp@35:9275215a3a5b, 2014-04-10 (annotated)
- Committer:
- mazgch
- Date:
- Thu Apr 10 13:05:45 2014 +0000
- Revision:
- 35:9275215a3a5b
- Parent:
- 19:2b5d097ca15d
- Child:
- 45:ebc2fd8dcf21
fix low power mode
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 | 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 | 15:5eda64e5b9d1 | 40 | ptr += written; | 
| mazgch | 15:5eda64e5b9d1 | 41 | count -= written; | 
| mazgch | 15:5eda64e5b9d1 | 42 | txStart(); | 
| mazgch | 15:5eda64e5b9d1 | 43 | } | 
| mazgch | 15:5eda64e5b9d1 | 44 | while (count && blocking); | 
| mazgch | 9:e7a5959ffae1 | 45 | } | 
| mazgch | 13:e2446fcdc246 | 46 | return (length - count); | 
| mazgch | 13:e2446fcdc246 | 47 | } | 
| mazgch | 13:e2446fcdc246 | 48 | |
| mazgch | 9:e7a5959ffae1 | 49 | void SerialPipe::txIrqBuf(void) | 
| mazgch | 9:e7a5959ffae1 | 50 | { | 
| mazgch | 15:5eda64e5b9d1 | 51 | while (_SerialPipeBase::writeable() && _pipeTx.readable()) | 
| mazgch | 11:b084552b03fe | 52 | { | 
| mazgch | 13:e2446fcdc246 | 53 | char c = _pipeTx.getc(); | 
| mazgch | 15:5eda64e5b9d1 | 54 | _SerialPipeBase::_base_putc(c); | 
| mazgch | 11:b084552b03fe | 55 | } | 
| mazgch | 9:e7a5959ffae1 | 56 | } | 
| mazgch | 9:e7a5959ffae1 | 57 | |
| mazgch | 13:e2446fcdc246 | 58 | void SerialPipe::txStart(void) | 
| mazgch | 13:e2446fcdc246 | 59 | { | 
| mazgch | 13:e2446fcdc246 | 60 | __disable_irq(); | 
| mazgch | 13:e2446fcdc246 | 61 | txIrqBuf(); | 
| mazgch | 13:e2446fcdc246 | 62 | __enable_irq(); | 
| mazgch | 13:e2446fcdc246 | 63 | } | 
| mazgch | 13:e2446fcdc246 | 64 | |
| mazgch | 9:e7a5959ffae1 | 65 | // rx channel | 
| mazgch | 9:e7a5959ffae1 | 66 | int SerialPipe::readable(void) | 
| mazgch | 9:e7a5959ffae1 | 67 | { | 
| mazgch | 9:e7a5959ffae1 | 68 | return _pipeRx.readable(); | 
| mazgch | 9:e7a5959ffae1 | 69 | } | 
| mazgch | 9:e7a5959ffae1 | 70 | |
| mazgch | 9:e7a5959ffae1 | 71 | int SerialPipe::getc(void) | 
| mazgch | 9:e7a5959ffae1 | 72 | { | 
| mazgch | 15:5eda64e5b9d1 | 73 | if (!_pipeRx.readable()) | 
| mazgch | 15:5eda64e5b9d1 | 74 | return EOF; | 
| mazgch | 15:5eda64e5b9d1 | 75 | return _pipeRx.getc(); | 
| mazgch | 13:e2446fcdc246 | 76 | } | 
| mazgch | 13:e2446fcdc246 | 77 | |
| mazgch | 13:e2446fcdc246 | 78 | int SerialPipe::get(void* buffer, int length, bool blocking) | 
| mazgch | 13:e2446fcdc246 | 79 | { | 
| mazgch | 13:e2446fcdc246 | 80 | return _pipeRx.get((char*)buffer,length,blocking); | 
| mazgch | 13:e2446fcdc246 | 81 | } | 
| mazgch | 13:e2446fcdc246 | 82 | |
| mazgch | 9:e7a5959ffae1 | 83 | void SerialPipe::rxIrqBuf(void) | 
| mazgch | 9:e7a5959ffae1 | 84 | { | 
| mazgch | 15:5eda64e5b9d1 | 85 | while (_SerialPipeBase::readable()) | 
| mazgch | 9:e7a5959ffae1 | 86 | { | 
| mazgch | 15:5eda64e5b9d1 | 87 | char c = _SerialPipeBase::_base_getc(); | 
| mazgch | 9:e7a5959ffae1 | 88 | if (_pipeRx.writeable()) | 
| mazgch | 13:e2446fcdc246 | 89 | _pipeRx.putc(c); | 
| mazgch | 9:e7a5959ffae1 | 90 | else | 
| mazgch | 9:e7a5959ffae1 | 91 | /* overflow */; | 
| mazgch | 9:e7a5959ffae1 | 92 | } | 
| mazgch | 9:e7a5959ffae1 | 93 | } | 
| mazgch | 9:e7a5959ffae1 | 94 | 
