A simple serial shell running on its own thread (stack is statically allocated).

Dependents:   FRDM_K64F_IOT lpc1768_blinky

A simple serial shell running on its own thread. The thread is not started until a call to start(). A start() call creates a thread either using static allocation (pre-defined stack size).

Sample Usage

#include "mbed.h"
#include "rtos.h"
#include "Shell.h"

Serial pc(p9,p10);

#define SHELL_STACK_SIZ 1024
// Pre-allocate the shell's stack (on global mem)
unsigned char shellStack[SHELL_STACK_SIZ];
Shell shell(&pc);

// Shell Commands
/**
 *  \brief Gets the amount of free memory
 *  \param none
 *  \return none
 **/
static void cmd_mem(Stream * strm, int argc, char * argv[])
{
   // In order to get free mem within RTOS
   // we need to get the main thread's stack pointer
   // and subtract it with the top of the heap
   // ------+-------------------+   Last Address of RAM (INITIAL_SP)
   //       | Scheduler Stack   |
   //       +-------------------+
   //       | Main Thread Stack |
   //       |         |         |
   //       |         v         |
   //       +-------------------+ <- bottom_of_stack/__get_MSP()
   // RAM   |                   | 
   //       |  Available RAM    |  
   //       |                   |  
   //       +-------------------+ <- top_of_heap
   //       |         ^         |
   //       |         |         |
   //       |       Heap        |
   //       +-------------------+ <- __end__ / HEAP_START (linker defined var)
   //       | ZI                |
   //       +-------------------+
   //       | ZI: Shell Stack   |
   //       +-------------------+
   //       | ZI: Idle Stack    |
   //       +-------------------+
   //       | ZI: Timer Stack   |
   //       +-------------------+
   //       | RW                |  
   // ------+===================+  First Address of RAM
   //       |                   |
   // Flash |                   |
   //

   uint32_t bottom_of_stack = __get_MSP();
   char     * top_of_heap =  (char *) malloc(sizeof(char));
   uint32_t diff = bottom_of_stack - (uint32_t) top_of_heap;

   free((void *) top_of_heap);

   strm->printf("Available Memory : %d bytes\r\n",
        diff);
}

int main() {

    // Start the shell
    pc.printf("Starting debug shell ...\r\n");
    shell.addCommand("mem", cmd_mem);
    shell.start(osPriorityNormal, SHELL_STACK_SIZ, shellStack);

    while(1) {
        wait(0.2);
    }      
}


Revision:
0:dbde48ca1e30
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Shell.h	Tue Apr 28 16:56:21 2015 +0000
@@ -0,0 +1,46 @@
+#ifndef _SERIAL_SHELL_H_
+#define _SERIAL_SHELL_H_
+
+#include "mbed.h"
+#include "rtos.h"
+
+#include <string>
+#include <map>
+
+#define SHELL_MAX_LINE_LENGTH       64
+#define SHELL_MAX_ARGUMENTS         4
+
+typedef void (*shellcmd_t) (Stream *, int , char **);
+
+class Shell {
+    public:
+        Shell(Stream * channel);
+        virtual ~Shell() {}
+
+        void addCommand(std::string name, shellcmd_t func);
+        void start(osPriority priority = osPriorityNormal,
+                int stackSize = 1024, 
+                unsigned char *stack_pointer=NULL);
+
+    private:
+        static void threadHelper(const void * arg);
+
+        void shellMain();
+        void shellUsage(const char *p); 
+        bool shellGetLine(char *line, unsigned size);
+        void listCommands();
+        bool cmdExec(char * name, int argc, char *argv[]);
+
+        Stream * _chp;
+        Thread * _thread;
+
+        // UART/Serial buffers for
+        // parsing command line
+        char line[SHELL_MAX_LINE_LENGTH];
+        char *args[SHELL_MAX_ARGUMENTS + 1];
+
+        // commands
+        std::map<std::string, shellcmd_t> _commands; 
+};
+
+#endif