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

Stable library for mDot.

libmDot

xDot Library

Development library for xDot.

libxDot-dev

Stable library for xDot.

libxDot

Committer:
Jason Reiss
Date:
Wed Dec 06 13:33:52 2023 -0600
Revision:
42:23e2df6714f4
Parent:
36:b586cd6e91f3
TestMode: reset appnonce to 0 on reset to accomodate LCTT 3.12.0 and earlier. If the test suite changes to test AppNonce loaded from NVM this will fail tests.

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