mDot AT-Firmware for testing

Dependencies:   MTS-Serial libmDot-mbed5

Fork of Dot-AT-Firmware by MultiTech

Revision:
4:666017851052
Parent:
2:e5eebd74d36d
Child:
6:e27eaad36a0c
--- a/CommandTerminal/CommandTerminal.cpp	Tue Aug 18 15:58:59 2015 +0000
+++ b/CommandTerminal/CommandTerminal.cpp	Tue Aug 18 11:21:43 2015 -0500
@@ -1,12 +1,10 @@
 #include "ctype.h"
 #include "CommandTerminal.h"
 #include "Command.h"
+#include "MTSLog.h"
 #include <cstdarg>
 
-extern "C" {
-#include "wakeup.h"
-    extern void pin_function(PinName pin, int data);
-}
+#include <deque>
 
 const char CommandTerminal::banner[] = "\r\n\nMultiTech Systems LoRa XBee Module\r\n\n";
 const char CommandTerminal::helpline[] = "Enter '?' for help\r\n";
@@ -36,14 +34,13 @@
   _serial(serial),
   _dot(dot),
   _mode(mDot::COMMAND_MODE),
-  _sleep_standby(false),
+  _idle_thread(idle, NULL, osPriorityLow),
+  _sleep_standby(true),
   _xbee_on_sleep(XBEE_ON_SLEEP),
   _serial_up(false) {
 
     _serialp = &serial;
 
-    wakeup_init(_dot->getSerialWakeInterval());
-
     addCommand(new CmdAttention(_dot));
     addCommand(new CmdIdentification(_dot, serial));
     addCommand(new CmdResetCpu(_dot, serial));
@@ -54,6 +51,7 @@
     addCommand(new CmdSaveConfig(_dot));
     addCommand(new CmdDisplayConfig(_dot, serial));
     addCommand(new CmdDisplayStats(_dot, serial));
+    addCommand(new CmdResetStats(_dot, serial));
     addCommand(new CmdSerialBaudRate(_dot, serial));
     addCommand(new CmdDebugBaudRate(_dot, serial));
     addCommand(new CmdStartUpMode(_dot, serial));
@@ -71,6 +69,7 @@
 
     addCommand(new CmdJoinRequest(_dot, serial));
     addCommand(new CmdJoinRetries(_dot, serial));
+    addCommand(new CmdJoinByteOrder(_dot, serial));
     addCommand(new CmdNetworkJoinMode(_dot, serial));
     addCommand(new CmdNetworkJoinStatus(_dot, serial));
     addCommand(new CmdNetworkLinkCheck(_dot, serial));
@@ -109,10 +108,13 @@
     addCommand(new CmdReceiveOnce(_dot, serial));
     addCommand(new CmdReceiveContinuous(_dot, serial));
 
-    addCommand(new CmdDummy(_dot, "Serial Data Mode", "AT+SD", "Reads serial data and sends Lora packets (escape sequence: +++)"));
-    addCommand(new CmdSerialWakeInterval(_dot, serial));
-    addCommand(new CmdSerialWakeDelay(_dot, serial));
-    addCommand(new CmdSerialReceiveTimeout(_dot, serial));
+    addCommand(new CmdDummy(_dot, "Serial Data Mode", "AT+SD", "Reads serial data, sends packet, then sleeps using wake settings"));
+    addCommand(new CmdDummy(_dot, "Sleep Mode", "AT+SLEEP", "Enter sleep mode"));
+    addCommand(new CmdWakeMode(_dot, serial));
+    addCommand(new CmdWakeInterval(_dot, serial));
+    // addCommand(new CmdWakePin(_dot, serial));
+    addCommand(new CmdWakeDelay(_dot, serial));
+    addCommand(new CmdWakeTimeout(_dot, serial));
     addCommand(new CmdPing(_dot, serial));
     addCommand(new CmdLogLevel(_dot, serial));
 }
@@ -233,10 +235,11 @@
     }
 
     if (_serial_up) {
-        osDelay(_dot->getSerialWakeDelay());
+        osDelay(_dot->getWakeDelay());
 
         serial_read_timer.start();
-        while (_serial_up && serial_read_timer.read_ms() < _dot->getSerialReceiveTimeout()) {
+        uint16_t timeout = _dot->getWakeTimeout();
+        while (_serial_up && serial_read_timer.read_ms() < timeout) {
             while (readable() && serial_buffer.size() < max_send_size) {
                 serial_buffer.push_back(read());
                 serial_read_timer.reset();
@@ -251,7 +254,7 @@
                 std::vector<uint8_t> recv_buffer;
                 DEBUG_PRINTF("Received serial data, sending out radio.\r\n");
 
-                if (!_dot->send(serial_buffer))
+                if (_dot->send(serial_buffer) != mDot::MDOT_OK)
                     DEBUG_PRINTF("Send failed.\r\n");
                 if (_dot->recv(recv_buffer))
                     _serial.writef("%s\r\n", formatPacketData(recv_buffer, _dot->getRxOutput()).c_str());
@@ -352,12 +355,14 @@
 
 void CommandTerminal::start() {
 
-    _dot->resetNetworkSession();
+    wakeup(_sleep_standby);
 
     char ch;
     bool running = true;
     bool echo = _dot->getEcho();
     std::string command;
+    std::deque<std::string> history;
+    int history_index = -1;
     std::vector<std::string> args;
 
     if (_dot->getStartUpMode() == mDot::SERIAL_MODE) {
@@ -423,12 +428,54 @@
         if (readable()) {
             ch = read();
 
-            if (ch == '\b') {
+            if (ch == '\b' || ch == 0x7f) {
                 if (!command.empty()) {
                     writef("\b \b");
                     command.erase(command.size() - 1);
                 }
                 continue;
+            } else if (ch == 0x1b || ch == 0x09) {
+                osDelay(20);
+                // catch escape sequence, or tab
+                char ch1, ch2;
+
+                if (readable()) {
+                    ch1 = read();
+                    if (readable())
+                        ch2 = read();
+
+                    if (ch1 == 0x5b && ch2 == 0x41) {
+                        // up key
+                        for (int i = 0; i < command.size()+1; i++) {
+                            writef("\b \b");
+                        }
+                        if (history.size() > 0) {
+                            if (++history_index >= history.size() - 1)
+                                history_index = history.size() - 1;
+
+                            command = history[history_index];
+                            writef("%s", history[history_index].c_str());
+                        } else {
+                            command.clear();
+                        }
+                    } else if (ch1 == 0x5b && ch2 == 0x42) {
+
+                        // down key
+                        for (int i = 0; i < command.size()+1; i++) {
+                            writef("\b \b");
+                        }
+
+                        if (--history_index < 0) {
+                            history_index = -1;
+                            command.clear();
+                        } else {
+                            command = history[history_index];
+                            writef("%s", history[history_index].c_str());
+                        }
+                    }
+                }
+                while (readable()) read();
+                continue;
             } else {
                 command += ch;
             }
@@ -501,6 +548,21 @@
         } else if (args[0] == "AT+SD") {
             DEBUG_PRINTF("Enter Serial Mode\r\n");
             _mode = mDot::SERIAL_MODE;
+        } else if (args[0] == "AT+SLEEP") {
+            if (args.size() > 1 && (args[1] != "?")) {
+                write("Invalid argument\r\n");
+                write(error);
+            } else {
+                if (args.size() > 1 && args[1] == "?") {
+                    write("AT+SLEEP: NONE\r\n");
+                    write(done);
+                } else {
+                    _sleep_standby = !(args.size() > 1 && args[1] == "1");
+                    this->sleep(_sleep_standby);
+                    wait(0.1);
+                    write(done);
+                }
+            }
         } else {
             bool found = false;
             bool query = false;
@@ -552,8 +614,13 @@
             }
         }
 
+        if (history.size() == 0 || history.front() != command)
+            history.push_front(command);
+        history_index = -1;
         command.clear();
 
+        while (history.size() > 10)
+            history.pop_back();
     }
 }
 
@@ -568,65 +635,10 @@
     _serial_up = false;
     _xbee_on_sleep = GPIO_PIN_RESET;
 
-    HAL_PWREx_EnableMainRegulatorLowVoltage();
-    HAL_PWREx_EnableFlashPowerDown();
-    HAL_PWREx_EnableLowRegulatorLowVoltage();
-
-    DEBUG_PRINTF("Sleep\r\n");
-    _dot->sleep();
-
-    if (standby) {
-
-        DEBUG_PRINTF("RECORD UPLINK: %lu\r\n", _dot->getUpLinkCounter());
-
-        RTC_WriteBackupRegister(RTC_BKP_DR0, _dot->getUpLinkCounter());
-
-        HAL_PWR_EnableBkUpAccess();
-
-        HAL_EnableDBGStandbyMode();
-
-        HAL_PWR_DisableWakeUpPin (PWR_WAKEUP_PIN1);
-
-        __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); // InterruptIn wakeon_serial(XBEE_DIN);
-        // Application is reloaded after wake-up from standby
-HAL_PWR_EnterSTANDBYMode        ();
+    _serial.rxClear();
+    _serial.txClear();
 
-    } else {
-        pin_function(PA_0, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        pin_function(PA_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        // pin_function(PA_2, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        // pin_function(PA_3, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        pin_function(PA_4, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        pin_function(PA_5, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        pin_function(PA_6, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        pin_function(PA_7, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        // pin_function(PA_8, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        // pin_function(PA_9, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        // pin_function(PA_10, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        pin_function(PA_11, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        pin_function(PA_12, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        pin_function(PA_15, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-
-        pin_function(PB_0, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        pin_function(PB_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        pin_function(PB_2, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-
-        pin_function(PC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        pin_function(PC_4, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        pin_function(PC_5, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        pin_function(PC_9, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-        // pin_function(PC_13, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-
-        pin_function(PD_2, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
-
-        HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
-    }
-
-    wakeup_clear();
-    EXTI->PR = (1 << 22);
-
-    // After wake-up from STOP reconfigure the PLL
-    SetSysClock();
+    _dot->sleep(_dot->getWakeInterval(), _dot->getWakeMode(), standby);
 }
 
 bool CommandTerminal::waitForEscape(int timeout, mDot* dot, WaitType wait) {
@@ -665,16 +677,5 @@
 }
 
 void CommandTerminal::wakeup(bool standby) {
-    HAL_PWREx_DisableMainRegulatorLowVoltage();
-    HAL_PWREx_DisableFlashPowerDown();
-    HAL_PWREx_DisableLowRegulatorLowVoltage();
-
-    if (standby) {
-        HAL_DisableDBGStandbyMode();
-
-        HAL_PWR_EnableWakeUpPin (PWR_WAKEUP_PIN1);
-    }
-
-    _dot->wakeup();
 
 }