few changes for RTS/CTS control
Dependencies: MTS-Serial libmDot mbed-rtos mbed
Fork of mDot_AT_firmware by
ATSerialFlowControl.cpp@11:77b7911263da, 2016-10-25 (annotated)
- Committer:
- serg838
- Date:
- Tue Oct 25 05:39:39 2016 +0000
- Revision:
- 11:77b7911263da
- Parent:
- 9:ff62b20f7000
RTS/CTS changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mike Fiore |
9:ff62b20f7000 | 1 | #include "mbed.h" |
Mike Fiore |
9:ff62b20f7000 | 2 | #include "ATSerialFlowControl.h" |
Mike Fiore |
9:ff62b20f7000 | 3 | #include "MTSLog.h" |
Mike Fiore |
9:ff62b20f7000 | 4 | #include "Utils.h" |
Mike Fiore |
9:ff62b20f7000 | 5 | |
Mike Fiore |
9:ff62b20f7000 | 6 | using namespace mts; |
Mike Fiore |
9:ff62b20f7000 | 7 | |
Mike Fiore |
9:ff62b20f7000 | 8 | ATSerialFlowControl::ATSerialFlowControl(PinName TXD, PinName RXD, PinName RTS, PinName CTS, int txBufSize, int rxBufSize) |
Mike Fiore |
9:ff62b20f7000 | 9 | : ATSerial(TXD, RXD, txBufSize, rxBufSize) |
Mike Fiore |
9:ff62b20f7000 | 10 | , rxReadyFlag(false) |
Mike Fiore |
9:ff62b20f7000 | 11 | , rts(RTS) |
Mike Fiore |
9:ff62b20f7000 | 12 | , cts(CTS) |
serg838 | 11:77b7911263da | 13 | , dtr(PA_11) |
Mike Fiore |
9:ff62b20f7000 | 14 | { |
Mike Fiore |
9:ff62b20f7000 | 15 | notifyStartSending(); |
Mike Fiore |
9:ff62b20f7000 | 16 | |
Mike Fiore |
9:ff62b20f7000 | 17 | // Calculate the high and low watermark values |
Mike Fiore |
9:ff62b20f7000 | 18 | highThreshold = mts_max(rxBufSize - 10, rxBufSize * 0.85); |
Mike Fiore |
9:ff62b20f7000 | 19 | lowThreshold = rxBufSize * 0.3; |
Mike Fiore |
9:ff62b20f7000 | 20 | |
Mike Fiore |
9:ff62b20f7000 | 21 | // Setup the low watermark callback on the internal receive buffer |
Mike Fiore |
9:ff62b20f7000 | 22 | rxBuffer.attach(this, &ATSerialFlowControl::notifyStartSending, lowThreshold, LESS); |
Mike Fiore |
9:ff62b20f7000 | 23 | } |
Mike Fiore |
9:ff62b20f7000 | 24 | |
Mike Fiore |
9:ff62b20f7000 | 25 | ATSerialFlowControl::~ATSerialFlowControl() |
Mike Fiore |
9:ff62b20f7000 | 26 | { |
Mike Fiore |
9:ff62b20f7000 | 27 | } |
Mike Fiore |
9:ff62b20f7000 | 28 | |
Mike Fiore |
9:ff62b20f7000 | 29 | //Override the rxClear function to make sure that flow control lines are set correctly. |
Mike Fiore |
9:ff62b20f7000 | 30 | void ATSerialFlowControl::rxClear() |
Mike Fiore |
9:ff62b20f7000 | 31 | { |
Mike Fiore |
9:ff62b20f7000 | 32 | MTSBufferedIO::rxClear(); |
Mike Fiore |
9:ff62b20f7000 | 33 | notifyStartSending(); |
Mike Fiore |
9:ff62b20f7000 | 34 | } |
Mike Fiore |
9:ff62b20f7000 | 35 | |
Mike Fiore |
9:ff62b20f7000 | 36 | void ATSerialFlowControl::notifyStartSending() |
Mike Fiore |
9:ff62b20f7000 | 37 | { |
Mike Fiore |
9:ff62b20f7000 | 38 | if(!rxReadyFlag) { |
Mike Fiore |
9:ff62b20f7000 | 39 | rts.write(0); |
Mike Fiore |
9:ff62b20f7000 | 40 | rxReadyFlag = true; |
Mike Fiore |
9:ff62b20f7000 | 41 | //printf("RTS LOW: READY - RX[%d/%d]\r\n", rxBuffer.size(), rxBuffer.capacity()); |
Mike Fiore |
9:ff62b20f7000 | 42 | } |
Mike Fiore |
9:ff62b20f7000 | 43 | } |
Mike Fiore |
9:ff62b20f7000 | 44 | |
Mike Fiore |
9:ff62b20f7000 | 45 | void ATSerialFlowControl::notifyStopSending() |
Mike Fiore |
9:ff62b20f7000 | 46 | { |
Mike Fiore |
9:ff62b20f7000 | 47 | if(rxReadyFlag) { |
Mike Fiore |
9:ff62b20f7000 | 48 | rts.write(1); |
Mike Fiore |
9:ff62b20f7000 | 49 | rxReadyFlag = false; |
Mike Fiore |
9:ff62b20f7000 | 50 | //printf("RTS HIGH: NOT-READY - RX[%d/%d]\r\n", rxBuffer.size(), rxBuffer.capacity()); |
Mike Fiore |
9:ff62b20f7000 | 51 | } |
Mike Fiore |
9:ff62b20f7000 | 52 | } |
Mike Fiore |
9:ff62b20f7000 | 53 | |
Mike Fiore |
9:ff62b20f7000 | 54 | void ATSerialFlowControl::handleRead() |
Mike Fiore |
9:ff62b20f7000 | 55 | { |
Mike Fiore |
9:ff62b20f7000 | 56 | ATSerial::handleRead(); |
Mike Fiore |
9:ff62b20f7000 | 57 | if (rxBuffer.size() >= highThreshold) { |
Mike Fiore |
9:ff62b20f7000 | 58 | notifyStopSending(); |
Mike Fiore |
9:ff62b20f7000 | 59 | } |
Mike Fiore |
9:ff62b20f7000 | 60 | } |
Mike Fiore |
9:ff62b20f7000 | 61 | |
Mike Fiore |
9:ff62b20f7000 | 62 | void ATSerialFlowControl::handleWrite() |
serg838 | 11:77b7911263da | 63 | { |
serg838 | 11:77b7911263da | 64 | while(txBuffer.size() != 0) { |
serg838 | 11:77b7911263da | 65 | if (serial.writeable() && cts.read() == 0) { |
serg838 | 11:77b7911263da | 66 | dtr.write(0); |
serg838 | 11:77b7911263da | 67 | char byte; |
serg838 | 11:77b7911263da | 68 | if(txBuffer.read(byte) == 1) { |
serg838 | 11:77b7911263da | 69 | serial.attach(NULL, Serial::RxIrq); |
serg838 | 11:77b7911263da | 70 | serial.putc(byte); |
serg838 | 11:77b7911263da | 71 | serial.attach(this, &ATSerialFlowControl::handleRead, Serial::RxIrq); |
serg838 | 11:77b7911263da | 72 | } |
serg838 | 11:77b7911263da | 73 | } |
serg838 | 11:77b7911263da | 74 | else { |
serg838 | 11:77b7911263da | 75 | if (cts.read() == 1) dtr.write(1); |
serg838 | 11:77b7911263da | 76 | return; |
serg838 | 11:77b7911263da | 77 | } |
serg838 | 11:77b7911263da | 78 | } |
Mike Fiore |
9:ff62b20f7000 | 79 | } |
Mike Fiore |
9:ff62b20f7000 | 80 |