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 MTS-Serial by
MTSSerialFlowControl.cpp@12:e12b79a4ab4f, 2017-03-23 (annotated)
- Committer:
- Leon Lindenfelser
- Date:
- Thu Mar 23 16:15:14 2017 -0500
- Revision:
- 12:e12b79a4ab4f
- Parent:
- 10:b18a1064dfc6
Update to match gitlab commit 90a9e07c8d77b31bb36e641b4361184ef2ac8558 Feb 2 2016.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Mike Fiore |
4:d348d001283b | 1 | #include "mbed.h" |
| Mike Fiore |
1:d34b566d6f47 | 2 | #include "MTSSerialFlowControl.h" |
| Mike Fiore |
1:d34b566d6f47 | 3 | #include "MTSLog.h" |
| Mike Fiore |
3:8e3cb3371b09 | 4 | #include "Utils.h" |
| Mike Fiore |
1:d34b566d6f47 | 5 | |
| Mike Fiore |
1:d34b566d6f47 | 6 | using namespace mts; |
| Mike Fiore |
1:d34b566d6f47 | 7 | |
| Mike Fiore |
1:d34b566d6f47 | 8 | MTSSerialFlowControl::MTSSerialFlowControl(PinName TXD, PinName RXD, PinName RTS, PinName CTS, int txBufSize, int rxBufSize) |
| Mike Fiore |
1:d34b566d6f47 | 9 | : MTSSerial(TXD, RXD, txBufSize, rxBufSize) |
| Mike Fiore |
1:d34b566d6f47 | 10 | , rxReadyFlag(false) |
| Mike Fiore |
1:d34b566d6f47 | 11 | , rts(RTS) |
| Mike Fiore |
1:d34b566d6f47 | 12 | , cts(CTS) |
| Mike Fiore |
1:d34b566d6f47 | 13 | { |
| Mike Fiore |
1:d34b566d6f47 | 14 | notifyStartSending(); |
| Mike Fiore |
1:d34b566d6f47 | 15 | |
| Mike Fiore |
1:d34b566d6f47 | 16 | // Calculate the high and low watermark values |
| Mike Fiore |
10:b18a1064dfc6 | 17 | highThreshold = mts_max(rxBufSize - 10, rxBufSize * 0.85); |
| Mike Fiore |
1:d34b566d6f47 | 18 | lowThreshold = rxBufSize * 0.3; |
| Mike Fiore |
1:d34b566d6f47 | 19 | |
| Mike Fiore |
1:d34b566d6f47 | 20 | // Setup the low watermark callback on the internal receive buffer |
| Mike Fiore |
1:d34b566d6f47 | 21 | rxBuffer.attach(this, &MTSSerialFlowControl::notifyStartSending, lowThreshold, LESS); |
| Mike Fiore |
1:d34b566d6f47 | 22 | } |
| Mike Fiore |
1:d34b566d6f47 | 23 | |
| Mike Fiore |
1:d34b566d6f47 | 24 | MTSSerialFlowControl::~MTSSerialFlowControl() |
| Mike Fiore |
1:d34b566d6f47 | 25 | { |
| Mike Fiore |
1:d34b566d6f47 | 26 | } |
| Mike Fiore |
1:d34b566d6f47 | 27 | |
| Mike Fiore |
1:d34b566d6f47 | 28 | //Override the rxClear function to make sure that flow control lines are set correctly. |
| Mike Fiore |
1:d34b566d6f47 | 29 | void MTSSerialFlowControl::rxClear() |
| Mike Fiore |
1:d34b566d6f47 | 30 | { |
| Mike Fiore |
1:d34b566d6f47 | 31 | MTSBufferedIO::rxClear(); |
| Mike Fiore |
1:d34b566d6f47 | 32 | notifyStartSending(); |
| Mike Fiore |
1:d34b566d6f47 | 33 | } |
| Mike Fiore |
1:d34b566d6f47 | 34 | |
| Mike Fiore |
1:d34b566d6f47 | 35 | void MTSSerialFlowControl::notifyStartSending() |
| Mike Fiore |
1:d34b566d6f47 | 36 | { |
| Mike Fiore |
1:d34b566d6f47 | 37 | if(!rxReadyFlag) { |
| Mike Fiore |
1:d34b566d6f47 | 38 | rts.write(0); |
| Mike Fiore |
1:d34b566d6f47 | 39 | rxReadyFlag = true; |
| Mike Fiore |
1:d34b566d6f47 | 40 | //printf("RTS LOW: READY - RX[%d/%d]\r\n", rxBuffer.size(), rxBuffer.capacity()); |
| Mike Fiore |
1:d34b566d6f47 | 41 | } |
| Mike Fiore |
1:d34b566d6f47 | 42 | } |
| Mike Fiore |
1:d34b566d6f47 | 43 | |
| Mike Fiore |
1:d34b566d6f47 | 44 | void MTSSerialFlowControl::notifyStopSending() |
| Mike Fiore |
1:d34b566d6f47 | 45 | { |
| Mike Fiore |
1:d34b566d6f47 | 46 | if(rxReadyFlag) { |
| Mike Fiore |
1:d34b566d6f47 | 47 | rts.write(1); |
| Mike Fiore |
1:d34b566d6f47 | 48 | rxReadyFlag = false; |
| Mike Fiore |
1:d34b566d6f47 | 49 | //printf("RTS HIGH: NOT-READY - RX[%d/%d]\r\n", rxBuffer.size(), rxBuffer.capacity()); |
| Mike Fiore |
1:d34b566d6f47 | 50 | } |
| Mike Fiore |
1:d34b566d6f47 | 51 | } |
| Mike Fiore |
1:d34b566d6f47 | 52 | |
| Mike Fiore |
1:d34b566d6f47 | 53 | void MTSSerialFlowControl::handleRead() |
| Mike Fiore |
1:d34b566d6f47 | 54 | { |
| Leon Lindenfelser |
12:e12b79a4ab4f | 55 | char byte = _serial->getc(); |
| Mike Fiore |
1:d34b566d6f47 | 56 | if(rxBuffer.write(byte) != 1) { |
| Mike Fiore |
1:d34b566d6f47 | 57 | logError("Serial Rx Byte Dropped [%c][0x%02X]", byte, byte); |
| Mike Fiore |
1:d34b566d6f47 | 58 | } |
| Mike Fiore |
1:d34b566d6f47 | 59 | if (rxBuffer.size() >= highThreshold) { |
| Mike Fiore |
1:d34b566d6f47 | 60 | notifyStopSending(); |
| Mike Fiore |
1:d34b566d6f47 | 61 | } |
| Mike Fiore |
1:d34b566d6f47 | 62 | } |
| Mike Fiore |
1:d34b566d6f47 | 63 | |
| Mike Fiore |
1:d34b566d6f47 | 64 | void MTSSerialFlowControl::handleWrite() |
| Mike Fiore |
1:d34b566d6f47 | 65 | { |
| Mike Fiore |
1:d34b566d6f47 | 66 | while(txBuffer.size() != 0) { |
| Leon Lindenfelser |
12:e12b79a4ab4f | 67 | if (_serial->writeable() && cts.read() == 0) { |
| Mike Fiore |
1:d34b566d6f47 | 68 | char byte; |
| Mike Fiore |
1:d34b566d6f47 | 69 | if(txBuffer.read(byte) == 1) { |
| Leon Lindenfelser |
12:e12b79a4ab4f | 70 | _serial->attach(NULL, Serial::RxIrq); |
| Leon Lindenfelser |
12:e12b79a4ab4f | 71 | _serial->putc(byte); |
| Leon Lindenfelser |
12:e12b79a4ab4f | 72 | _serial->attach(this, &MTSSerialFlowControl::handleRead, Serial::RxIrq); |
| Mike Fiore |
1:d34b566d6f47 | 73 | } |
| Mike Fiore |
1:d34b566d6f47 | 74 | } else { |
| Mike Fiore |
1:d34b566d6f47 | 75 | return; |
| Mike Fiore |
1:d34b566d6f47 | 76 | } |
| Mike Fiore |
1:d34b566d6f47 | 77 | } |
| Mike Fiore |
1:d34b566d6f47 | 78 | } |
| Mike Fiore |
1:d34b566d6f47 | 79 |
