AT command firmware for MultiTech Dot devices.

Fork of mDot_AT_firmware by MultiTech

Dot Library Not Included!

Because these example programs can be used for both mDot and xDot devices, the LoRa stack is not included. The libmDot library should be imported if building for mDot devices. The libxDot library should be imported if building for xDot devices. The AT firmware was last tested with mbed-os-5.4.7. Using a version past mbed-os-5.4.7 will cause the build to fail. The library used with the AT firmware has to match the mbed-os version.

Dot Library Version 3 Updates

Dot Library versions 3.x.x require a channel plan to be injected into the stack. The Dot-Examples and Dot-AT-Firmware do this by defining a macro called "CHANNEL_PLAN" that controls the channel plan that will be used in the examples. Available channel plans will be in the Dot Library repository in the plans folder.

Revision 20 and earlier of Dot-Examples and revision 15 and earlier of Dot-AT-Firmware should be used with Dot Library versions prior to 3.0.0.

Fota Library

Th Fota Library must be added to compile for mDot 3.1.0 with Fota support. Latest dev libraries and 3.2.0 release will include Fota with libmDot/libxDot.

AT Firmware Description

This AT Firmware is what ships on mDot and xDot devices. It provides an AT command interface for using the mDot or xDot for LoRa communication.

AT command documentation can be found on Multitech.com.

The firmware changelog can be found here. The library changelog can be found here.

Dot Libraries

Dot Library Limitations

The commit messages in libmDot-mbed5 and libmDot-dev-mbed5 specify the version of the Dot library the commit contains and the version of mbed-os it was compiled against. We recommend building your application with the version of mbed-os specified in the commit message of the version of the Dot library you're using. This will ensure that you don't run into any runtime issues caused by differences in the mbed-os versions.

Stable and development libraries are available for both mDot and xDot platforms. The library chosen must match the target platform. Compiling for the mDot platform with the xDot library or vice versa will not succeed.

mDot Library

Development library for mDot.

libmDot-dev-mbed5

Stable library for mDot.

Import librarylibmDot-mbed5

Stable version of the mDot library for mbed 5. This version of the library is suitable for deployment scenarios. See lastest commit message for version of mbed-os library that has been tested against.

xDot Library

Development library for xDot.

libxDot-dev-mbed5

Stable library for xDot.

Import librarylibxDot-mbed5

Stable version of the xDot library for mbed 5. This version of the library is suitable for deployment scenarios.

Committer:
Jason Reiss
Date:
Fri Jan 29 13:04:32 2021 -0600
Revision:
33:5c0252521669
Parent:
28:c222ca8383f4
Remove moved commands

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jason Reiss 28:c222ca8383f4 1
Mike Fiore 9:ff62b20f7000 2 #include "ATSerial.h"
Mike Fiore 9:ff62b20f7000 3 #include "MTSLog.h"
Jason Reiss 28:c222ca8383f4 4 #include "Utils.h"
Mike Fiore 9:ff62b20f7000 5
Mike Fiore 9:ff62b20f7000 6 using namespace mts;
Mike Fiore 9:ff62b20f7000 7
Jason Reiss 28:c222ca8383f4 8
Jason Reiss 28:c222ca8383f4 9 ATSerial::ATSerial(PinName txd, PinName rxd, PinName rts, PinName cts, int baud)
Jason Reiss 28:c222ca8383f4 10 : _serial(txd, rxd, baud),
Jason Reiss 28:c222ca8383f4 11 _tx_irq_enabled(false),
Mike Fiore 9:ff62b20f7000 12 _last_time(0),
Mike Fiore 9:ff62b20f7000 13 _esc_cnt(0),
Mike Fiore 9:ff62b20f7000 14 _esc_ch('+'),
Jason Reiss 28:c222ca8383f4 15 _escaped(false),
Jason Reiss 28:c222ca8383f4 16 _flow(false), // Flow control disabled by default
Jason Reiss 28:c222ca8383f4 17 _rts(rts),
Jason Reiss 28:c222ca8383f4 18 _cts(cts)
Mike Fiore 9:ff62b20f7000 19 {
Jason Reiss 28:c222ca8383f4 20 if (rts != NC && cts != NC) { // RTS and CTS must both be provided for flow control
Jason Reiss 28:c222ca8383f4 21 _flow = true;
Jason Reiss 28:c222ca8383f4 22 _rts = 0; // Start with receive enabled
Jason Reiss 28:c222ca8383f4 23 _cts.fall(callback(this, &ATSerial::startWrite)); // Restart writes when able to send
Jason Reiss 28:c222ca8383f4 24 _hwm = mts_max(MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE - 10, MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE * 0.85);
Jason Reiss 28:c222ca8383f4 25 _lwm = MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE * 0.3;
Jason Reiss 28:c222ca8383f4 26 }
Jason Reiss 28:c222ca8383f4 27 _timer.start();
Jason Reiss 28:c222ca8383f4 28 _serial.attach(callback(this, &ATSerial::handleRead), SerialBase::RxIrq);
Mike Fiore 9:ff62b20f7000 29 }
Mike Fiore 9:ff62b20f7000 30
Mike Fiore 9:ff62b20f7000 31 ATSerial::~ATSerial()
Mike Fiore 9:ff62b20f7000 32 {
Jason Reiss 28:c222ca8383f4 33 }
Jason Reiss 28:c222ca8383f4 34
Jason Reiss 28:c222ca8383f4 35 void ATSerial::baud(int baudrate) {
Jason Reiss 28:c222ca8383f4 36 _mutex.lock();
Jason Reiss 28:c222ca8383f4 37 _serial.baud(baudrate);
Jason Reiss 28:c222ca8383f4 38 _mutex.unlock();
Jason Reiss 28:c222ca8383f4 39 }
Jason Reiss 28:c222ca8383f4 40
Jason Reiss 28:c222ca8383f4 41 void ATSerial::format(int bits, SerialBase::Parity parity, int stop_bits) {
Jason Reiss 28:c222ca8383f4 42 _mutex.lock();
Jason Reiss 28:c222ca8383f4 43 _serial.format(bits, parity, stop_bits);
Jason Reiss 28:c222ca8383f4 44 _mutex.unlock();
Jason Reiss 28:c222ca8383f4 45 }
Jason Reiss 28:c222ca8383f4 46
Jason Reiss 28:c222ca8383f4 47 bool ATSerial::readable() {
Jason Reiss 28:c222ca8383f4 48 return !_rxbuf.empty();
Jason Reiss 28:c222ca8383f4 49 }
Jason Reiss 28:c222ca8383f4 50
Jason Reiss 28:c222ca8383f4 51 bool ATSerial::writeable() {
Jason Reiss 28:c222ca8383f4 52 return !_txbuf.full();
Jason Reiss 28:c222ca8383f4 53 }
Jason Reiss 28:c222ca8383f4 54
Jason Reiss 28:c222ca8383f4 55 void ATSerial::rxClear() {
Jason Reiss 28:c222ca8383f4 56 _mutex.lock();
Jason Reiss 28:c222ca8383f4 57 _rxbuf.reset();
Jason Reiss 28:c222ca8383f4 58 if (_flow) {
Jason Reiss 28:c222ca8383f4 59 _rts = 0; // Allow receiving because receive buffer is now empty
Jason Reiss 28:c222ca8383f4 60 }
Jason Reiss 28:c222ca8383f4 61 _mutex.unlock();
Jason Reiss 28:c222ca8383f4 62 }
Jason Reiss 28:c222ca8383f4 63
Jason Reiss 28:c222ca8383f4 64 void ATSerial::txClear() {
Jason Reiss 28:c222ca8383f4 65 _mutex.lock();
Jason Reiss 28:c222ca8383f4 66 _txbuf.reset();
Jason Reiss 28:c222ca8383f4 67 _mutex.unlock();
Jason Reiss 28:c222ca8383f4 68 }
Jason Reiss 28:c222ca8383f4 69
Jason Reiss 28:c222ca8383f4 70 bool ATSerial::escaped() {
Jason Reiss 28:c222ca8383f4 71 _mutex.lock();
Jason Reiss 28:c222ca8383f4 72 std::chrono::milliseconds now = std::chrono::duration_cast<std::chrono::milliseconds>(_timer.elapsed_time());
Jason Reiss 28:c222ca8383f4 73 std::chrono::milliseconds elapsed_ms = now - _last_time;
Jason Reiss 28:c222ca8383f4 74
Jason Reiss 28:c222ca8383f4 75 // Have we seen three esc chars and 1 sec end guard has passed
Jason Reiss 28:c222ca8383f4 76 if (_escaped || (_esc_cnt == 3 && (elapsed_ms > 1s))) {
Jason Reiss 28:c222ca8383f4 77 _escaped = true;
Jason Reiss 28:c222ca8383f4 78
Jason Reiss 28:c222ca8383f4 79 // Have we seen a couple esc chars but nothing in 500 ms
Jason Reiss 28:c222ca8383f4 80 } else if (_esc_cnt > 0 && _esc_cnt != 3 && elapsed_ms > 500ms) {
Jason Reiss 28:c222ca8383f4 81 // Write seen esc chars
Jason Reiss 28:c222ca8383f4 82 while (_esc_cnt) {
Jason Reiss 28:c222ca8383f4 83 _rxbuf.push(_esc_ch);
Jason Reiss 28:c222ca8383f4 84 _esc_cnt--;
Jason Reiss 28:c222ca8383f4 85 }
Jason Reiss 28:c222ca8383f4 86 _escaped = false;
Jason Reiss 28:c222ca8383f4 87 }
Jason Reiss 28:c222ca8383f4 88 _mutex.unlock();
Jason Reiss 28:c222ca8383f4 89
Jason Reiss 28:c222ca8383f4 90 return _escaped;
Jason Reiss 28:c222ca8383f4 91 }
Jason Reiss 28:c222ca8383f4 92
Jason Reiss 28:c222ca8383f4 93 void ATSerial::clearEscaped() {
Jason Reiss 28:c222ca8383f4 94 _mutex.lock();
Jason Reiss 28:c222ca8383f4 95 _esc_cnt = 0;
Jason Reiss 28:c222ca8383f4 96 _escaped = false;
Jason Reiss 28:c222ca8383f4 97 _mutex.unlock();
Mike Fiore 9:ff62b20f7000 98 }
Mike Fiore 9:ff62b20f7000 99
Jason Reiss 28:c222ca8383f4 100 bool ATSerial::read(char& c) {
Jason Reiss 28:c222ca8383f4 101 return read(&c, 1) == 1;
Mike Fiore 9:ff62b20f7000 102 }
Mike Fiore 9:ff62b20f7000 103
Jason Reiss 28:c222ca8383f4 104 int ATSerial::write(const char *buffer, size_t length) {
Jason Reiss 28:c222ca8383f4 105 _mutex.lock();
Jason Reiss 28:c222ca8383f4 106 size_t i = 0;
Jason Reiss 28:c222ca8383f4 107 while (i < length) {
Jason Reiss 28:c222ca8383f4 108 if (_txbuf.full()) {
Jason Reiss 28:c222ca8383f4 109 do {
Jason Reiss 28:c222ca8383f4 110 _mutex.unlock();
Jason Reiss 28:c222ca8383f4 111 thread_sleep_for(1);
Jason Reiss 28:c222ca8383f4 112 _mutex.lock();
Jason Reiss 28:c222ca8383f4 113 } while (_txbuf.full());
Jason Reiss 28:c222ca8383f4 114 }
Jason Reiss 28:c222ca8383f4 115 while (i < length && !_txbuf.full())
Jason Reiss 28:c222ca8383f4 116 {
Jason Reiss 28:c222ca8383f4 117 _txbuf.push(buffer[i]);
Jason Reiss 28:c222ca8383f4 118 i++;
Jason Reiss 28:c222ca8383f4 119 }
Jason Reiss 28:c222ca8383f4 120 startWrite(); // Start writing data in tx buffer
Jason Reiss 28:c222ca8383f4 121 }
Jason Reiss 28:c222ca8383f4 122 _mutex.unlock();
Jason Reiss 28:c222ca8383f4 123 return i;
Jason Reiss 28:c222ca8383f4 124 }
Jason Reiss 28:c222ca8383f4 125
Jason Reiss 28:c222ca8383f4 126 int ATSerial::writef(const char* format, ... ) {
Jason Reiss 28:c222ca8383f4 127 char buff[256];
Jason Reiss 28:c222ca8383f4 128
Jason Reiss 28:c222ca8383f4 129 va_list ap;
Jason Reiss 28:c222ca8383f4 130 va_start(ap, format);
Jason Reiss 28:c222ca8383f4 131 int size = vsnprintf(buff, 256, format, ap);
Jason Reiss 28:c222ca8383f4 132 int n = write(buff, size);
Jason Reiss 28:c222ca8383f4 133 va_end(ap);
Jason Reiss 28:c222ca8383f4 134
Jason Reiss 28:c222ca8383f4 135 return n;
Jason Reiss 28:c222ca8383f4 136 }
Jason Reiss 28:c222ca8383f4 137
Jason Reiss 28:c222ca8383f4 138 int ATSerial::read(char *buffer, size_t length) {
Jason Reiss 28:c222ca8383f4 139 _mutex.lock();
Jason Reiss 28:c222ca8383f4 140 size_t r = 0;
Jason Reiss 28:c222ca8383f4 141 while (r < length) {
Jason Reiss 28:c222ca8383f4 142 if (_rxbuf.pop(buffer[r])) {
Jason Reiss 28:c222ca8383f4 143 r++;
Jason Reiss 28:c222ca8383f4 144 } else {
Jason Reiss 28:c222ca8383f4 145 break;
Jason Reiss 28:c222ca8383f4 146 }
Jason Reiss 28:c222ca8383f4 147 }
Jason Reiss 28:c222ca8383f4 148 if (_flow && _rts && _rxbuf.size() <= _lwm) {
Jason Reiss 28:c222ca8383f4 149 _rts = 0; // RX buffer has room, clear RTS to continue receiving
Jason Reiss 28:c222ca8383f4 150 }
Jason Reiss 28:c222ca8383f4 151 _mutex.unlock();
Jason Reiss 28:c222ca8383f4 152 return r;
Jason Reiss 28:c222ca8383f4 153 }
Jason Reiss 28:c222ca8383f4 154
Jason Reiss 28:c222ca8383f4 155 void ATSerial::startWrite()
Mike Fiore 9:ff62b20f7000 156 {
Jason Reiss 28:c222ca8383f4 157 core_util_critical_section_enter();
Jason Reiss 28:c222ca8383f4 158 if (!_tx_irq_enabled) {
Jason Reiss 28:c222ca8383f4 159 // only write to hardware in one place
Jason Reiss 28:c222ca8383f4 160 handleWrite();
Jason Reiss 28:c222ca8383f4 161 if (!_txbuf.empty()) {
Jason Reiss 28:c222ca8383f4 162 _serial.attach(callback(this, &ATSerial::handleWrite), SerialBase::TxIrq);
Jason Reiss 28:c222ca8383f4 163 _tx_irq_enabled = true;
Jason Reiss 28:c222ca8383f4 164 }
Jason Reiss 28:c222ca8383f4 165 }
Jason Reiss 28:c222ca8383f4 166 core_util_critical_section_exit();
Mike Fiore 9:ff62b20f7000 167 }
Mike Fiore 9:ff62b20f7000 168
Mike Fiore 9:ff62b20f7000 169 void ATSerial::handleWrite()
Jason Reiss 28:c222ca8383f4 170 {
Jason Reiss 28:c222ca8383f4 171 char c;
Jason Reiss 28:c222ca8383f4 172 while (_serial.writeable()) {
Jason Reiss 28:c222ca8383f4 173 if (_flow && _cts) {
Jason Reiss 28:c222ca8383f4 174 break; // Exit write loop when CTS is set, will resume when it is cleared
Jason Reiss 28:c222ca8383f4 175 }
Jason Reiss 28:c222ca8383f4 176
Jason Reiss 28:c222ca8383f4 177 if (_txbuf.pop(c)) {
Jason Reiss 28:c222ca8383f4 178 _serial.write(&c, 1);
Mike Fiore 9:ff62b20f7000 179 } else {
Jason Reiss 28:c222ca8383f4 180 break;
Mike Fiore 9:ff62b20f7000 181 }
Mike Fiore 9:ff62b20f7000 182 }
Jason Reiss 28:c222ca8383f4 183
Jason Reiss 28:c222ca8383f4 184 // Detach TX IRQ if there's no more data to write or CTS is set
Jason Reiss 28:c222ca8383f4 185 if (_tx_irq_enabled && (_txbuf.empty() || (_flow && _cts))) {
Jason Reiss 28:c222ca8383f4 186 _serial.attach(NULL, SerialBase::TxIrq);
Jason Reiss 28:c222ca8383f4 187 _tx_irq_enabled = false;
Jason Reiss 28:c222ca8383f4 188 }
Mike Fiore 9:ff62b20f7000 189 }
Mike Fiore 9:ff62b20f7000 190
Mike Fiore 9:ff62b20f7000 191
Mike Fiore 9:ff62b20f7000 192 void ATSerial::handleRead()
Mike Fiore 9:ff62b20f7000 193 {
Jason Reiss 28:c222ca8383f4 194 char byte;
Jason Reiss 28:c222ca8383f4 195 if (_serial.read(&byte, 1) < 1) { return; }
Mike Fiore 9:ff62b20f7000 196
Jason Reiss 28:c222ca8383f4 197 std::chrono::milliseconds now = std::chrono::duration_cast<std::chrono::milliseconds>(_timer.elapsed_time());
Jason Reiss 28:c222ca8383f4 198 std::chrono::milliseconds elapsed_ms = now - _last_time;
Jason Reiss 28:c222ca8383f4 199 _last_time = now;
Jason Reiss 28:c222ca8383f4 200
Mike Fiore 9:ff62b20f7000 201 // Have we seen 3 esc chars but this char is before 1 sec end guard time
Jason Reiss 28:c222ca8383f4 202 if (_esc_cnt == 3 && (elapsed_ms < std::chrono::seconds(1))) {
Mike Fiore 9:ff62b20f7000 203 // Write the three chars we held back
Mike Fiore 9:ff62b20f7000 204 while (_esc_cnt) {
Jason Reiss 28:c222ca8383f4 205 _rxbuf.push(_esc_ch);
Mike Fiore 9:ff62b20f7000 206 _esc_cnt--;
Mike Fiore 9:ff62b20f7000 207 }
Mike Fiore 9:ff62b20f7000 208 } else if (byte == _esc_ch) {
Mike Fiore 9:ff62b20f7000 209 // Has 1 second passed before last char
Jason Reiss 28:c222ca8383f4 210 if (elapsed_ms > std::chrono::seconds(1)) {
Mike Fiore 9:ff62b20f7000 211 _esc_cnt = 1;
Mike Fiore 9:ff62b20f7000 212 // Is this second or third esc char
Mike Fiore 9:ff62b20f7000 213 } else if (_esc_cnt > 0 && _esc_cnt < 3) {
Mike Fiore 9:ff62b20f7000 214 _esc_cnt++;
Mike Fiore 9:ff62b20f7000 215 }
Mike Fiore 9:ff62b20f7000 216 } else if (_esc_cnt > 0) {
Mike Fiore 9:ff62b20f7000 217 // Write any esc chars held back
Mike Fiore 9:ff62b20f7000 218 while (_esc_cnt) {
Jason Reiss 28:c222ca8383f4 219 _rxbuf.push(_esc_ch);
Mike Fiore 9:ff62b20f7000 220 _esc_cnt--;
Mike Fiore 9:ff62b20f7000 221 }
Mike Fiore 9:ff62b20f7000 222 }
Mike Fiore 9:ff62b20f7000 223
Jason Reiss 28:c222ca8383f4 224 if(_esc_cnt == 0) {
Jason Reiss 28:c222ca8383f4 225 if (_flow && !_rts && _rxbuf.size() >= _hwm) {
Jason Reiss 28:c222ca8383f4 226 _rts = 1; // RX buffer too full, set RTS to stop receiving
Jason Reiss 28:c222ca8383f4 227 // Data will still be received until the buffer is full
Jason Reiss 28:c222ca8383f4 228 }
Jason Reiss 28:c222ca8383f4 229
Jason Reiss 28:c222ca8383f4 230 if (_rxbuf.full()) {
Jason Reiss 28:c222ca8383f4 231 // Overflow, drop byte
Jason Reiss 28:c222ca8383f4 232 } else {
Jason Reiss 28:c222ca8383f4 233 _rxbuf.push(byte);
Jason Reiss 28:c222ca8383f4 234 }
Mike Fiore 9:ff62b20f7000 235 }
Mike Fiore 9:ff62b20f7000 236
Mike Fiore 9:ff62b20f7000 237 }
Mike Fiore 9:ff62b20f7000 238
Mike Fiore 9:ff62b20f7000 239 void ATSerial::escapeChar(char esc) {
Mike Fiore 9:ff62b20f7000 240 _esc_ch = esc;
Mike Fiore 9:ff62b20f7000 241 }
Mike Fiore 9:ff62b20f7000 242
Mike Fiore 9:ff62b20f7000 243 char ATSerial::escapeChar() {
Mike Fiore 9:ff62b20f7000 244 return _esc_ch;
Mike Fiore 9:ff62b20f7000 245 }
Jason Reiss 27:5fafd3b26ac3 246