dev
Dependencies: MTS-Serial libmDot-mbed5
Fork of Dot-AT-Firmware by
ATSerialFlowControl.cpp
00001 #include "mbed.h" 00002 #include "ATSerialFlowControl.h" 00003 #include "MTSLog.h" 00004 #include "Utils.h" 00005 00006 using namespace mts; 00007 00008 ATSerialFlowControl::ATSerialFlowControl(PinName TXD, PinName RXD, PinName RTS, PinName CTS, int txBufSize, int rxBufSize) 00009 : ATSerial(TXD, RXD, txBufSize, rxBufSize) 00010 , rxReadyFlag(false) 00011 , rts(RTS) 00012 , cts(CTS) 00013 { 00014 notifyStartSending(); 00015 00016 // Calculate the high and low watermark values 00017 highThreshold = mts_max(rxBufSize - 10, rxBufSize * 0.85); 00018 lowThreshold = rxBufSize * 0.3; 00019 00020 // Setup the low watermark callback on the internal receive buffer 00021 rxBuffer.attach(this, &ATSerialFlowControl::notifyStartSending, lowThreshold, LESS); 00022 } 00023 00024 ATSerialFlowControl::~ATSerialFlowControl() 00025 { 00026 } 00027 00028 //Override the rxClear function to make sure that flow control lines are set correctly. 00029 void ATSerialFlowControl::rxClear() 00030 { 00031 MTSBufferedIO::rxClear(); 00032 notifyStartSending(); 00033 } 00034 00035 void ATSerialFlowControl::notifyStartSending() 00036 { 00037 if(!rxReadyFlag) { 00038 rts.write(0); 00039 rxReadyFlag = true; 00040 //printf("RTS LOW: READY - RX[%d/%d]\r\n", rxBuffer.size(), rxBuffer.capacity()); 00041 } 00042 } 00043 00044 void ATSerialFlowControl::notifyStopSending() 00045 { 00046 if(rxReadyFlag) { 00047 rts.write(1); 00048 rxReadyFlag = false; 00049 //printf("RTS HIGH: NOT-READY - RX[%d/%d]\r\n", rxBuffer.size(), rxBuffer.capacity()); 00050 } 00051 } 00052 00053 void ATSerialFlowControl::handleRead() 00054 { 00055 ATSerial::handleRead(); 00056 if (rxBuffer.size() >= highThreshold) { 00057 notifyStopSending(); 00058 } 00059 } 00060 00061 void ATSerialFlowControl::handleWrite() 00062 { 00063 while(txBuffer.size() != 0) { 00064 if (_serial->writeable() && cts.read() == 0) { 00065 char byte; 00066 if(txBuffer.read(byte) == 1) { 00067 _serial->attach(NULL, Serial::RxIrq); 00068 _serial->putc(byte); 00069 _serial->attach(this, &ATSerialFlowControl::handleRead, Serial::RxIrq); 00070 } 00071 } else { 00072 return; 00073 } 00074 } 00075 }
Generated on Tue Jul 12 2022 13:49:30 by 1.7.2