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); } }
Shell.h@2:3db531afe4fc, 2015-04-29 (annotated)
- Committer:
- vpcola
- Date:
- Wed Apr 29 06:13:20 2015 +0000
- Revision:
- 2:3db531afe4fc
- Parent:
- 0:dbde48ca1e30
Removed unused headers;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vpcola | 0:dbde48ca1e30 | 1 | #ifndef _SERIAL_SHELL_H_ |
vpcola | 0:dbde48ca1e30 | 2 | #define _SERIAL_SHELL_H_ |
vpcola | 0:dbde48ca1e30 | 3 | |
vpcola | 0:dbde48ca1e30 | 4 | #include "mbed.h" |
vpcola | 0:dbde48ca1e30 | 5 | #include "rtos.h" |
vpcola | 0:dbde48ca1e30 | 6 | |
vpcola | 0:dbde48ca1e30 | 7 | #include <string> |
vpcola | 0:dbde48ca1e30 | 8 | #include <map> |
vpcola | 0:dbde48ca1e30 | 9 | |
vpcola | 0:dbde48ca1e30 | 10 | #define SHELL_MAX_LINE_LENGTH 64 |
vpcola | 0:dbde48ca1e30 | 11 | #define SHELL_MAX_ARGUMENTS 4 |
vpcola | 0:dbde48ca1e30 | 12 | |
vpcola | 0:dbde48ca1e30 | 13 | typedef void (*shellcmd_t) (Stream *, int , char **); |
vpcola | 0:dbde48ca1e30 | 14 | |
vpcola | 0:dbde48ca1e30 | 15 | class Shell { |
vpcola | 0:dbde48ca1e30 | 16 | public: |
vpcola | 0:dbde48ca1e30 | 17 | Shell(Stream * channel); |
vpcola | 0:dbde48ca1e30 | 18 | virtual ~Shell() {} |
vpcola | 0:dbde48ca1e30 | 19 | |
vpcola | 0:dbde48ca1e30 | 20 | void addCommand(std::string name, shellcmd_t func); |
vpcola | 0:dbde48ca1e30 | 21 | void start(osPriority priority = osPriorityNormal, |
vpcola | 0:dbde48ca1e30 | 22 | int stackSize = 1024, |
vpcola | 0:dbde48ca1e30 | 23 | unsigned char *stack_pointer=NULL); |
vpcola | 0:dbde48ca1e30 | 24 | |
vpcola | 0:dbde48ca1e30 | 25 | private: |
vpcola | 0:dbde48ca1e30 | 26 | static void threadHelper(const void * arg); |
vpcola | 0:dbde48ca1e30 | 27 | |
vpcola | 0:dbde48ca1e30 | 28 | void shellMain(); |
vpcola | 0:dbde48ca1e30 | 29 | void shellUsage(const char *p); |
vpcola | 0:dbde48ca1e30 | 30 | bool shellGetLine(char *line, unsigned size); |
vpcola | 0:dbde48ca1e30 | 31 | void listCommands(); |
vpcola | 0:dbde48ca1e30 | 32 | bool cmdExec(char * name, int argc, char *argv[]); |
vpcola | 0:dbde48ca1e30 | 33 | |
vpcola | 0:dbde48ca1e30 | 34 | Stream * _chp; |
vpcola | 0:dbde48ca1e30 | 35 | Thread * _thread; |
vpcola | 0:dbde48ca1e30 | 36 | |
vpcola | 0:dbde48ca1e30 | 37 | // UART/Serial buffers for |
vpcola | 0:dbde48ca1e30 | 38 | // parsing command line |
vpcola | 0:dbde48ca1e30 | 39 | char line[SHELL_MAX_LINE_LENGTH]; |
vpcola | 0:dbde48ca1e30 | 40 | char *args[SHELL_MAX_ARGUMENTS + 1]; |
vpcola | 0:dbde48ca1e30 | 41 | |
vpcola | 0:dbde48ca1e30 | 42 | // commands |
vpcola | 0:dbde48ca1e30 | 43 | std::map<std::string, shellcmd_t> _commands; |
vpcola | 0:dbde48ca1e30 | 44 | }; |
vpcola | 0:dbde48ca1e30 | 45 | |
vpcola | 0:dbde48ca1e30 | 46 | #endif |