few changes for RTS/CTS control
Dependencies: MTS-Serial libmDot mbed-rtos mbed
Fork of mDot_AT_firmware by
ATSerial.cpp
00001 #include "mbed.h" 00002 #include "ATSerial.h" 00003 #include "MTSLog.h" 00004 00005 using namespace mts; 00006 00007 ATSerial::ATSerial(PinName TXD, PinName RXD, int txBufferSize, int rxBufferSize) 00008 : MTSSerial(TXD, RXD, txBufferSize, rxBufferSize) 00009 , _serial(new RawSerial(TXD,RXD)), 00010 _baudrate(9600), 00011 _bits(8), 00012 _parity(mbed::SerialBase::None), 00013 _stop_bits(1), 00014 _last_time(0), 00015 _esc_cnt(0), 00016 _esc_ch('+'), 00017 _escaped(false) 00018 { 00019 timer.start(); 00020 _serial->attach(this, &ATSerial::handleRead, Serial::RxIrq); 00021 } 00022 00023 ATSerial::~ATSerial() 00024 { 00025 delete _serial; 00026 } 00027 00028 00029 00030 void ATSerial::baud(int baudrate) 00031 { 00032 _baudrate = baudrate; 00033 _serial->baud(_baudrate); 00034 } 00035 00036 void ATSerial::format(int bits, SerialBase::Parity parity, int stop_bits) 00037 { 00038 _bits = bits; 00039 _parity = parity; 00040 _stop_bits = stop_bits; 00041 _serial->format(_bits, _parity, _stop_bits); 00042 } 00043 00044 void ATSerial::handleWrite() 00045 { 00046 while(txBuffer.size() != 0) { 00047 if (_serial->writeable()) { 00048 char byte; 00049 if(txBuffer.read(byte) == 1) { 00050 _serial->attach(NULL, Serial::RxIrq); 00051 _serial->putc(byte); 00052 _serial->attach(this, &ATSerial::handleRead, Serial::RxIrq); 00053 } 00054 } else { 00055 return; 00056 } 00057 } 00058 } 00059 00060 void mts::ATSerial::reattach(PinName TXD, PinName RXD) { 00061 delete _serial; 00062 _serial = new RawSerial(TXD, RXD); 00063 _serial->attach(this, &ATSerial::handleRead, Serial::RxIrq); 00064 _serial->baud(_baudrate); 00065 _serial->format(_bits, _parity, _stop_bits); 00066 rxBuffer.clear(); 00067 txBuffer.clear(); 00068 } 00069 00070 void mts::ATSerial::sendBreak() { 00071 _serial->send_break(); 00072 } 00073 00074 bool ATSerial::escaped() { 00075 00076 int now = timer.read_ms(); 00077 00078 // Have we seen three esc chars and 1 sec end guard has passed 00079 if (_escaped || (_esc_cnt == 3 && (now - _last_time > 1000))) { 00080 _escaped = true; 00081 return true; 00082 00083 // Have we seen a couple esc chars but nothing in 500 ms 00084 } else if (_esc_cnt > 0 && _esc_cnt != 3 && now - _last_time > 500) { 00085 // Write seen esc chars 00086 while (_esc_cnt) { 00087 rxBuffer.write(_esc_ch); 00088 _esc_cnt--; 00089 } 00090 } 00091 00092 return false; 00093 } 00094 00095 void ATSerial::clearEscaped() { 00096 _esc_cnt = 0; 00097 _escaped = false; 00098 } 00099 00100 void ATSerial::handleRead() 00101 { 00102 char byte = _serial->getc(); 00103 int now = timer.read_ms(); 00104 00105 // Have we seen 3 esc chars but this char is before 1 sec end guard time 00106 if (_esc_cnt == 3 && (now - _last_time < 1000)) { 00107 // Write the three chars we held back 00108 while (_esc_cnt) { 00109 rxBuffer.write(_esc_ch); 00110 _esc_cnt--; 00111 } 00112 } else if (byte == _esc_ch) { 00113 // Has 1 second passed before last char 00114 if (now - _last_time > 1000) { 00115 _esc_cnt = 1; 00116 // Is this second or third esc char 00117 } else if (_esc_cnt > 0 && _esc_cnt < 3) { 00118 _esc_cnt++; 00119 } 00120 } else if (_esc_cnt > 0) { 00121 // Write any esc chars held back 00122 while (_esc_cnt) { 00123 rxBuffer.write(_esc_ch); 00124 _esc_cnt--; 00125 } 00126 } 00127 00128 if(_esc_cnt == 0 && rxBuffer.write(byte) != 1) { 00129 // logError("Serial Rx Byte Dropped [%c][0x%02X]", byte, byte); 00130 } 00131 00132 _last_time = timer.read_ms(); 00133 } 00134 00135 void ATSerial::escapeChar(char esc) { 00136 _esc_ch = esc; 00137 } 00138 00139 char ATSerial::escapeChar() { 00140 return _esc_ch; 00141 }
Generated on Tue Jul 12 2022 16:01:22 by 1.7.2