Sample application using SerialShell, SDFileSystem and Ethernet support

Dependencies:   EthernetInterface SDFileSystem2 SerialShell mbed-rtos mbed

Fork of FRDM_K64F-Ethernet by Rangel Alvarado

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "SDFileSystem.h"
00004 #include "Shell.h"
00005  
00006 #define MBED_DEV_IP       "192.168.0.52"
00007 #define MBED_DEV_MASK     "255.255.255.0"
00008 #define MBED_DEV_GW       "0.0.0.0"
00009 #define ECHO_SERVER_PORT   5000
00010 
00011 #define SD_MOSI PTE3
00012 #define SD_MISO PTE1
00013 #define SD_SCLK PTE2
00014 #define SD_CS   PTE4
00015 #define SD_DETECT PTE6
00016 
00017 Serial pc(PTC17,PTC16);
00018 SDFileSystem sd(SD_MOSI, SD_MISO, SD_SCLK, SD_CS, "sd");
00019 
00020 #define SHELL_STACK_SIZ 1024
00021 // Pre-allocate the shell's stack (on global mem)
00022 unsigned char shellStack[SHELL_STACK_SIZ];
00023 Shell shell(&pc);
00024 
00025 /**
00026  *  \brief Gets the amount of free memory
00027  *  \param none
00028  *  \return none
00029  **/
00030 static void cmd_mem(Stream * chp, int argc, char * argv[])
00031 {
00032    // In order to get free mem within RTOS
00033    // we need to get the main thread's stack pointer
00034    // and subtract it with the top of the heap
00035    // ------+-------------------+   Last Address of RAM (INITIAL_SP)
00036    //       | Scheduler Stack   |
00037    //       +-------------------+
00038    //       | Main Thread Stack |
00039    //       |         |         |
00040    //       |         v         |
00041    //       +-------------------+ <- bottom_of_stack/__get_MSP()
00042    // RAM   |                   | 
00043    //       |  Available RAM    |  
00044    //       |                   |  
00045    //       +-------------------+ <- top_of_heap
00046    //       |         ^         |
00047    //       |         |         |
00048    //       |       Heap        |
00049    //       +-------------------+ <- __end__ / HEAP_START (linker defined var)
00050    //       | ZI                |
00051    //       +-------------------+
00052    //       | ZI: Shell Stack   |
00053    //       +-------------------+
00054    //       | ZI: Idle Stack    |
00055    //       +-------------------+
00056    //       | ZI: Timer Stack   |
00057    //       +-------------------+
00058    //       | RW                |  
00059    // ------+===================+  First Address of RAM
00060    //       |                   |
00061    // Flash |                   |
00062    //
00063 
00064    uint32_t bottom_of_stack = __get_MSP();
00065    char     * top_of_heap =  (char *) malloc(sizeof(char));
00066    uint32_t diff = bottom_of_stack - (uint32_t) top_of_heap;
00067 
00068    free((void *) top_of_heap);
00069 
00070    chp->printf("Available Memory : %d bytes\r\n",
00071         diff);
00072 }
00073 
00074 /**
00075  *  \brief List Directories and files 
00076  *  \param none
00077  *  \return int
00078  **/
00079 static void cmd_ls(Stream * chp, int argc, char * argv[])
00080 {
00081    DIR * dp;
00082    struct dirent * dirp;
00083    FILINFO fileInfo;
00084    char dirroot[256];
00085    
00086    if (argc >= 1)
00087        sprintf(dirroot, "/sd/%s", argv[0]);
00088    else
00089        sprintf(dirroot, "/sd");
00090    
00091    chp->printf("Listing directory [%s]\r\n", dirroot);
00092    
00093    dp = opendir(dirroot);           
00094    while((dirp = readdir(dp)) != NULL)
00095    {
00096        if (sd.stat(dirp->d_name, &fileInfo) == 0)
00097        {
00098            if (fileInfo.fattrib & AM_DIR )
00099                    chp->printf("<DIR>\t\t");
00100            else
00101                    chp->printf("%ld\t\t", fileInfo.fsize);
00102        }
00103        chp->printf("%s\r\n", dirp->d_name);
00104    }
00105    closedir(dp);
00106 }
00107 
00108  
00109 int main (void) {
00110     EthernetInterface eth;
00111     eth.init(MBED_DEV_IP, MBED_DEV_MASK, MBED_DEV_GW); //Assign a device ip, mask and gateway
00112     eth.connect();
00113     printf("IP Address is %s\n", eth.getIPAddress());
00114     
00115     TCPSocketServer server;
00116     server.bind(ECHO_SERVER_PORT);
00117     server.listen();
00118 
00119     // Start the shell
00120     pc.printf("Starting debug shell ...\r\n");
00121     shell.addCommand("ls", cmd_ls);
00122     shell.addCommand("mem", cmd_mem);
00123     // Start the thread statically (separate stack)
00124     shell.start(osPriorityNormal, SHELL_STACK_SIZ, shellStack);
00125     
00126         
00127     while (true) {
00128         printf("\nWait for new connection...\n");
00129         TCPSocketConnection client;
00130         server.accept(client);
00131         client.set_blocking(false, 1500); // Timeout after (1.5)s
00132         
00133         printf("Connection from: %s\n", client.get_address());
00134         char buffer[256];
00135         while (true) {
00136             int n = client.receive(buffer, sizeof(buffer));
00137             if (n <= 0) break;
00138             
00139             client.send_all(buffer, n);
00140             if (n <= 0) break;
00141         }
00142         
00143         client.close();
00144     }
00145 }