mDot AT-Firmware for testing
Dependencies: MTS-Serial libmDot-mbed5
Fork of Dot-AT-Firmware by
ATSerial.cpp@9:ff62b20f7000, 2016-04-04 (annotated)
- Committer:
- Mike Fiore
- Date:
- Mon Apr 04 09:00:31 2016 -0500
- Revision:
- 9:ff62b20f7000
update to version 1.0.8 of AT firmware
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 "ATSerial.h" |
Mike Fiore |
9:ff62b20f7000 | 3 | #include "MTSLog.h" |
Mike Fiore |
9:ff62b20f7000 | 4 | |
Mike Fiore |
9:ff62b20f7000 | 5 | using namespace mts; |
Mike Fiore |
9:ff62b20f7000 | 6 | |
Mike Fiore |
9:ff62b20f7000 | 7 | ATSerial::ATSerial(PinName TXD, PinName RXD, int txBufferSize, int rxBufferSize) |
Mike Fiore |
9:ff62b20f7000 | 8 | : MTSSerial(TXD, RXD, txBufferSize, rxBufferSize) |
Mike Fiore |
9:ff62b20f7000 | 9 | , _serial(new RawSerial(TXD,RXD)), |
Mike Fiore |
9:ff62b20f7000 | 10 | _baudrate(9600), |
Mike Fiore |
9:ff62b20f7000 | 11 | _bits(8), |
Mike Fiore |
9:ff62b20f7000 | 12 | _parity(mbed::SerialBase::None), |
Mike Fiore |
9:ff62b20f7000 | 13 | _stop_bits(1), |
Mike Fiore |
9:ff62b20f7000 | 14 | _last_time(0), |
Mike Fiore |
9:ff62b20f7000 | 15 | _esc_cnt(0), |
Mike Fiore |
9:ff62b20f7000 | 16 | _esc_ch('+'), |
Mike Fiore |
9:ff62b20f7000 | 17 | _escaped(false) |
Mike Fiore |
9:ff62b20f7000 | 18 | { |
Mike Fiore |
9:ff62b20f7000 | 19 | timer.start(); |
Mike Fiore |
9:ff62b20f7000 | 20 | _serial->attach(this, &ATSerial::handleRead, Serial::RxIrq); |
Mike Fiore |
9:ff62b20f7000 | 21 | } |
Mike Fiore |
9:ff62b20f7000 | 22 | |
Mike Fiore |
9:ff62b20f7000 | 23 | ATSerial::~ATSerial() |
Mike Fiore |
9:ff62b20f7000 | 24 | { |
Mike Fiore |
9:ff62b20f7000 | 25 | delete _serial; |
Mike Fiore |
9:ff62b20f7000 | 26 | } |
Mike Fiore |
9:ff62b20f7000 | 27 | |
Mike Fiore |
9:ff62b20f7000 | 28 | |
Mike Fiore |
9:ff62b20f7000 | 29 | |
Mike Fiore |
9:ff62b20f7000 | 30 | void ATSerial::baud(int baudrate) |
Mike Fiore |
9:ff62b20f7000 | 31 | { |
Mike Fiore |
9:ff62b20f7000 | 32 | _baudrate = baudrate; |
Mike Fiore |
9:ff62b20f7000 | 33 | _serial->baud(_baudrate); |
Mike Fiore |
9:ff62b20f7000 | 34 | } |
Mike Fiore |
9:ff62b20f7000 | 35 | |
Mike Fiore |
9:ff62b20f7000 | 36 | void ATSerial::format(int bits, SerialBase::Parity parity, int stop_bits) |
Mike Fiore |
9:ff62b20f7000 | 37 | { |
Mike Fiore |
9:ff62b20f7000 | 38 | _bits = bits; |
Mike Fiore |
9:ff62b20f7000 | 39 | _parity = parity; |
Mike Fiore |
9:ff62b20f7000 | 40 | _stop_bits = stop_bits; |
Mike Fiore |
9:ff62b20f7000 | 41 | _serial->format(_bits, _parity, _stop_bits); |
Mike Fiore |
9:ff62b20f7000 | 42 | } |
Mike Fiore |
9:ff62b20f7000 | 43 | |
Mike Fiore |
9:ff62b20f7000 | 44 | void ATSerial::handleWrite() |
Mike Fiore |
9:ff62b20f7000 | 45 | { |
Mike Fiore |
9:ff62b20f7000 | 46 | while(txBuffer.size() != 0) { |
Mike Fiore |
9:ff62b20f7000 | 47 | if (_serial->writeable()) { |
Mike Fiore |
9:ff62b20f7000 | 48 | char byte; |
Mike Fiore |
9:ff62b20f7000 | 49 | if(txBuffer.read(byte) == 1) { |
Mike Fiore |
9:ff62b20f7000 | 50 | _serial->attach(NULL, Serial::RxIrq); |
Mike Fiore |
9:ff62b20f7000 | 51 | _serial->putc(byte); |
Mike Fiore |
9:ff62b20f7000 | 52 | _serial->attach(this, &ATSerial::handleRead, Serial::RxIrq); |
Mike Fiore |
9:ff62b20f7000 | 53 | } |
Mike Fiore |
9:ff62b20f7000 | 54 | } else { |
Mike Fiore |
9:ff62b20f7000 | 55 | return; |
Mike Fiore |
9:ff62b20f7000 | 56 | } |
Mike Fiore |
9:ff62b20f7000 | 57 | } |
Mike Fiore |
9:ff62b20f7000 | 58 | } |
Mike Fiore |
9:ff62b20f7000 | 59 | |
Mike Fiore |
9:ff62b20f7000 | 60 | void mts::ATSerial::reattach(PinName TXD, PinName RXD) { |
Mike Fiore |
9:ff62b20f7000 | 61 | delete _serial; |
Mike Fiore |
9:ff62b20f7000 | 62 | _serial = new RawSerial(TXD, RXD); |
Mike Fiore |
9:ff62b20f7000 | 63 | _serial->attach(this, &ATSerial::handleRead, Serial::RxIrq); |
Mike Fiore |
9:ff62b20f7000 | 64 | _serial->baud(_baudrate); |
Mike Fiore |
9:ff62b20f7000 | 65 | _serial->format(_bits, _parity, _stop_bits); |
Mike Fiore |
9:ff62b20f7000 | 66 | rxBuffer.clear(); |
Mike Fiore |
9:ff62b20f7000 | 67 | txBuffer.clear(); |
Mike Fiore |
9:ff62b20f7000 | 68 | } |
Mike Fiore |
9:ff62b20f7000 | 69 | |
Mike Fiore |
9:ff62b20f7000 | 70 | void mts::ATSerial::sendBreak() { |
Mike Fiore |
9:ff62b20f7000 | 71 | _serial->send_break(); |
Mike Fiore |
9:ff62b20f7000 | 72 | } |
Mike Fiore |
9:ff62b20f7000 | 73 | |
Mike Fiore |
9:ff62b20f7000 | 74 | bool ATSerial::escaped() { |
Mike Fiore |
9:ff62b20f7000 | 75 | |
Mike Fiore |
9:ff62b20f7000 | 76 | int now = timer.read_ms(); |
Mike Fiore |
9:ff62b20f7000 | 77 | |
Mike Fiore |
9:ff62b20f7000 | 78 | // Have we seen three esc chars and 1 sec end guard has passed |
Mike Fiore |
9:ff62b20f7000 | 79 | if (_escaped || (_esc_cnt == 3 && (now - _last_time > 1000))) { |
Mike Fiore |
9:ff62b20f7000 | 80 | _escaped = true; |
Mike Fiore |
9:ff62b20f7000 | 81 | return true; |
Mike Fiore |
9:ff62b20f7000 | 82 | |
Mike Fiore |
9:ff62b20f7000 | 83 | // Have we seen a couple esc chars but nothing in 500 ms |
Mike Fiore |
9:ff62b20f7000 | 84 | } else if (_esc_cnt > 0 && _esc_cnt != 3 && now - _last_time > 500) { |
Mike Fiore |
9:ff62b20f7000 | 85 | // Write seen esc chars |
Mike Fiore |
9:ff62b20f7000 | 86 | while (_esc_cnt) { |
Mike Fiore |
9:ff62b20f7000 | 87 | rxBuffer.write(_esc_ch); |
Mike Fiore |
9:ff62b20f7000 | 88 | _esc_cnt--; |
Mike Fiore |
9:ff62b20f7000 | 89 | } |
Mike Fiore |
9:ff62b20f7000 | 90 | } |
Mike Fiore |
9:ff62b20f7000 | 91 | |
Mike Fiore |
9:ff62b20f7000 | 92 | return false; |
Mike Fiore |
9:ff62b20f7000 | 93 | } |
Mike Fiore |
9:ff62b20f7000 | 94 | |
Mike Fiore |
9:ff62b20f7000 | 95 | void ATSerial::clearEscaped() { |
Mike Fiore |
9:ff62b20f7000 | 96 | _esc_cnt = 0; |
Mike Fiore |
9:ff62b20f7000 | 97 | _escaped = false; |
Mike Fiore |
9:ff62b20f7000 | 98 | } |
Mike Fiore |
9:ff62b20f7000 | 99 | |
Mike Fiore |
9:ff62b20f7000 | 100 | void ATSerial::handleRead() |
Mike Fiore |
9:ff62b20f7000 | 101 | { |
Mike Fiore |
9:ff62b20f7000 | 102 | char byte = _serial->getc(); |
Mike Fiore |
9:ff62b20f7000 | 103 | int now = timer.read_ms(); |
Mike Fiore |
9:ff62b20f7000 | 104 | |
Mike Fiore |
9:ff62b20f7000 | 105 | // Have we seen 3 esc chars but this char is before 1 sec end guard time |
Mike Fiore |
9:ff62b20f7000 | 106 | if (_esc_cnt == 3 && (now - _last_time < 1000)) { |
Mike Fiore |
9:ff62b20f7000 | 107 | // Write the three chars we held back |
Mike Fiore |
9:ff62b20f7000 | 108 | while (_esc_cnt) { |
Mike Fiore |
9:ff62b20f7000 | 109 | rxBuffer.write(_esc_ch); |
Mike Fiore |
9:ff62b20f7000 | 110 | _esc_cnt--; |
Mike Fiore |
9:ff62b20f7000 | 111 | } |
Mike Fiore |
9:ff62b20f7000 | 112 | } else if (byte == _esc_ch) { |
Mike Fiore |
9:ff62b20f7000 | 113 | // Has 1 second passed before last char |
Mike Fiore |
9:ff62b20f7000 | 114 | if (now - _last_time > 1000) { |
Mike Fiore |
9:ff62b20f7000 | 115 | _esc_cnt = 1; |
Mike Fiore |
9:ff62b20f7000 | 116 | // Is this second or third esc char |
Mike Fiore |
9:ff62b20f7000 | 117 | } else if (_esc_cnt > 0 && _esc_cnt < 3) { |
Mike Fiore |
9:ff62b20f7000 | 118 | _esc_cnt++; |
Mike Fiore |
9:ff62b20f7000 | 119 | } |
Mike Fiore |
9:ff62b20f7000 | 120 | } else if (_esc_cnt > 0) { |
Mike Fiore |
9:ff62b20f7000 | 121 | // Write any esc chars held back |
Mike Fiore |
9:ff62b20f7000 | 122 | while (_esc_cnt) { |
Mike Fiore |
9:ff62b20f7000 | 123 | rxBuffer.write(_esc_ch); |
Mike Fiore |
9:ff62b20f7000 | 124 | _esc_cnt--; |
Mike Fiore |
9:ff62b20f7000 | 125 | } |
Mike Fiore |
9:ff62b20f7000 | 126 | } |
Mike Fiore |
9:ff62b20f7000 | 127 | |
Mike Fiore |
9:ff62b20f7000 | 128 | if(_esc_cnt == 0 && rxBuffer.write(byte) != 1) { |
Mike Fiore |
9:ff62b20f7000 | 129 | // logError("Serial Rx Byte Dropped [%c][0x%02X]", byte, byte); |
Mike Fiore |
9:ff62b20f7000 | 130 | } |
Mike Fiore |
9:ff62b20f7000 | 131 | |
Mike Fiore |
9:ff62b20f7000 | 132 | _last_time = timer.read_ms(); |
Mike Fiore |
9:ff62b20f7000 | 133 | } |
Mike Fiore |
9:ff62b20f7000 | 134 | |
Mike Fiore |
9:ff62b20f7000 | 135 | void ATSerial::escapeChar(char esc) { |
Mike Fiore |
9:ff62b20f7000 | 136 | _esc_ch = esc; |
Mike Fiore |
9:ff62b20f7000 | 137 | } |
Mike Fiore |
9:ff62b20f7000 | 138 | |
Mike Fiore |
9:ff62b20f7000 | 139 | char ATSerial::escapeChar() { |
Mike Fiore |
9:ff62b20f7000 | 140 | return _esc_ch; |
Mike Fiore |
9:ff62b20f7000 | 141 | } |