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