![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
TEN MBED OS Course shell utility
Revision 0:1e8461adc480, committed 2017-01-29
- Comitter:
- uLipe
- Date:
- Sun Jan 29 20:57:17 2017 +0000
- Commit message:
- first;
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.gitignore Sun Jan 29 20:57:17 2017 +0000 @@ -0,0 +1,4 @@ +.build +.mbed +projectfiles +*.py*
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Sun Jan 29 20:57:17 2017 +0000 @@ -0,0 +1,89 @@ +# Getting started with Blinky on mbed OS + +This is a very simple guide, reviewing the steps required to get Blinky working on an mbed OS platform. + +Please install [mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli). + +## Get the example application! + +From the command line, import the example: + +``` +mbed import mbed-os-example-blinky +cd mbed-os-example-blinky +``` + +### Now compile + +Invoke `mbed compile` specifying the name of your platform and your favorite toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the ARM Compiler 5: + +``` +mbed compile -m K64F -t ARM +``` + +Your PC may take a few minutes to compile your code. At the end you should get the following result: + +``` +[snip] ++----------------------------+-------+-------+------+ +| Module | .text | .data | .bss | ++----------------------------+-------+-------+------+ +| Misc | 13939 | 24 | 1372 | +| core/hal | 16993 | 96 | 296 | +| core/rtos | 7384 | 92 | 4204 | +| features/FEATURE_IPV4 | 80 | 0 | 176 | +| frameworks/greentea-client | 1830 | 60 | 44 | +| frameworks/utest | 2392 | 512 | 292 | +| Subtotals | 42618 | 784 | 6384 | ++----------------------------+-------+-------+------+ +Allocated Heap: unknown +Allocated Stack: unknown +Total Static RAM memory (data + bss): 7168 bytes +Total RAM memory (data + bss + heap + stack): 7168 bytes +Total Flash memory (text + data + misc): 43402 bytes +Image: .\.build\K64F\ARM\mbed-os-example-blinky.bin +``` + +### Program your board + +1. Connect your mbed device to the computer over USB. +1. Copy the binary file to the mbed device . +1. Press the reset button to start the program. + +You should see the LED of your platform turning on and off. + +Congratulations if you managed to complete this test! + +## Export the project to Keil MDK and debug your application + +From the command line, run the following command: + +``` +mbed export -m K64F -i uvision +``` + +To debug the application: + +1. Start uVision. +1. Import the uVision project generated earlier. +1. Compile your application and generate an `.axf` file. +1. Make sure uVision is configured to debug over CMSIS-DAP (From the Project menu > Options for Target '...' > Debug tab > Use CMSIS-DAP Debugger). +1. Set breakpoints and start a debug session. + +![Image of uVision](img/uvision.png) + +## Troubleshooting + +1. Make sure `mbed-cli` is working correctly and its version is greater than `0.8.9` + + ``` + mbed --version + ``` + + If not, you can update it easily: + + ``` + pip install mbed-cli --upgrade + ``` + +2. If using Keil MDK, make sure you have a license installed. [MDK-Lite](http://www.keil.com/arm/mdk.asp) has a 32KB restriction on code size.
Binary file img/uvision.png has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Jan 29 20:57:17 2017 +0000 @@ -0,0 +1,45 @@ +/** + * @brief our first hello world threaded program with MBED OS + */ +#include "mbed.h" +#include "rtos.h" +#include "shell.h" + + +/** + * @brief create shell command handler + */ +static int shell_callback(char *cmd, int argc, char **argv, void *outchar) +{ + Serial *s = (Serial *)outchar; + int ret = 0; + if(strcmp("Hello", cmd) == 0){ + s->printf("## welcome to simple shell app, please add your custom commands ##\n\r"); + }else { + ret = -1; + } + return(ret); +} + +/** + * @brief create shell command handler + */ +static void shell_usage(void *outchar) +{ + Serial *s = (Serial *)outchar; + s->printf("## Hello - prints a hello world message ## \n\r"); +} + + + +/** + * @brief main application loop + */ +int main(void) +{ + /* starts the shell task and applications task*/ + shell_set_command_handler(shell_callback); + shell_set_cmd_list(shell_usage); + shell_start(); + return 0; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Sun Jan 29 20:57:17 2017 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#34c1facf42a174f47fdf9002cd8c6bf10ac41744
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/shell.cpp Sun Jan 29 20:57:17 2017 +0000 @@ -0,0 +1,185 @@ +/** + * @brief our first hello world threaded program with MBED OS + */ +#include "mbed.h" +#include "rtos.h" +#include "shell.h" + +/* declare a thread reserved only for shell command handler */ +const size_t shell_stk_size = 8192; +uint8_t shell_stk[shell_stk_size]; +Thread shell_thread(osPriorityNormal, shell_stk_size, &shell_stk[0]); + +/* reserve the debbuger uart to shell interface */ +Serial pc_serial(SHELL_TXD,SHELL_RXD); + +static shell_callback_t shell_handler = NULL; +static shell_usage_t shell_usage; +static int argc = 0; +static char *argv[16]; + +/** + * @brief show help menu + */ +static void print_usage(void) +{ + pc_serial.printf("## use with the syntax below: \n\r"); + pc_serial.printf("## command <arg1> <arg2> ... <arg16> \n\r"); + pc_serial.printf("## Available commands: \n\r"); + if(shell_usage != NULL) { + shell_usage((void *)&pc_serial); + } +} + + +/** + * @brief parse the command received via comport + */ +static void shell_parser (char *cmd, int size) +{ + int cmd_ptr = 0; + int arg_ptr = 0; + int cmd_size = 0; + char command_buffer[256]; + + + /* copy to the root command */ + memset(&command_buffer, 0, sizeof(command_buffer)); + + /* find the root command terminator (space) */ + while(cmd_ptr < size) { + if(cmd[cmd_ptr] == ' ') break; + cmd_ptr++; + } + cmd_size = size - cmd_ptr; + + + /* extract command arguments */ + strncpy(&command_buffer[0], &cmd[cmd_ptr + 1], (size - cmd_ptr)); + + /* terminates the root command */ + cmd[cmd_ptr] = 0; + arg_ptr = 0; + + //pc_serial.printf("## command: %s \n\r", cmd); + //pc_serial.printf("## arguments: %s \n\r", command_buffer); + + + /* extract the further arguments */ + while(arg_ptr < (cmd_size)) { + + argc++; + *(argv + (argc- 1)) = &command_buffer[arg_ptr]; + + /* find terminator */ + while(command_buffer[arg_ptr] != ' ') { + arg_ptr++; + } + + /* adds to argument list */ + command_buffer[arg_ptr] = 0; + arg_ptr++; + // pc_serial.printf("## argument no: %d : %s \n\r", argc, argv[argc-1]); + } + + + + /* finds and execute the command table */ + if(shell_handler != NULL){ + + int ret = shell_handler(cmd, argc, argv, (void *)&pc_serial); + if(ret < 0) { + print_usage(); + } + }else { + print_usage(); + } +} + + + + +/** + * @brief shell commands processing thread + */ +static void shell_task(void) +{ + char serial_buffer[1024] = {0}; + int read_ptr = 0; + + /* setup the serial as 115200 bps */ + pc_serial.baud(115200); + + /* prints a welcome message */ + + pc_serial.printf("******************************************************************\n\r"); + pc_serial.printf("*** Welcome to MbedOS Simple Shell application ****\n\r"); + pc_serial.printf("*** Type some commands or just Enter key to see the available ****\n\r"); + pc_serial.printf("******************************************************************\n\r"); + pc_serial.printf(">>"); + + for(;;Thread::wait(50)) { + /* check if we have character available */ + if(pc_serial.readable()) { + bool new_cmd = false; + + /* get the incoming character */ + char c = pc_serial.getc(); + + if( (c == '\n') || (c == '\r')) { + /* handle enter key */ + new_cmd = true; + pc_serial.printf("\n\r"); + + }else if( (c == 0x7F) || (c == 0x08)){ + /* handle backspace and del keys */ + pc_serial.printf("\033[1D"); + pc_serial.putc(' '); + pc_serial.printf("\033[1D"); + + read_ptr--; + if(read_ptr < 0) read_ptr = 1023; + serial_buffer[read_ptr] = ' '; + + + } else { + /* loopback the pressed key */ + pc_serial.putc(c); + + /* store the incoming character on command circular buffer */ + serial_buffer[read_ptr] = c; + read_ptr = (read_ptr + 1) % 1024; + } + + + + if(new_cmd != false) { + /* command arrived, has other characters? */ + if(read_ptr != 0) { + shell_parser(&serial_buffer[0], read_ptr); + } else { + print_usage(); + } + /* reset the buffer command */ + memset(&serial_buffer, 0, sizeof(serial_buffer)); + read_ptr = 0; + pc_serial.printf(">>"); + } + + } + } +} + +void shell_set_command_handler(shell_callback_t sh) +{ + shell_handler = sh; +} +void shell_set_cmd_list(shell_usage_t sh) +{ + shell_usage = sh; +} +void shell_start(void) +{ + shell_thread.start(shell_task); +} + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/shell.h Sun Jan 29 20:57:17 2017 +0000 @@ -0,0 +1,32 @@ +/** + * @brief simple shel utility + */ +#ifndef __SHELL_H +#define __SHELL_H + +#define SHELL_TXD USBTX +#define SHELL_RXD USBRX + + +/* define the shell command handler callback */ +typedef int (*shell_callback_t) (char *cmd, int argc, char **argv, void *outchar); +typedef void (*shell_usage_t) (void *outchar); + +/** + * @brief set custom shell command handler + */ +void shell_set_command_handler(shell_callback_t sh); + + +/** + * @brief set the command list + */ +void shell_set_cmd_list(shell_usage_t sh); + + +/** + * @brief start the shell character capturer + */ +void shell_start(void); + +#endif \ No newline at end of file