Sample application using SerialShell, SDFileSystem and Ethernet support
Dependencies: EthernetInterface SDFileSystem2 SerialShell mbed-rtos mbed
Fork of FRDM_K64F-Ethernet by
main.cpp@1:10cb71a29f88, 2015-04-28 (annotated)
- Committer:
- vpcola
- Date:
- Tue Apr 28 17:01:47 2015 +0000
- Revision:
- 1:10cb71a29f88
- Parent:
- 0:bbc9cfdee3bc
A sample K64F solution with SerialShell, SDFileSystem and Ethernet support
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
issaiass | 0:bbc9cfdee3bc | 1 | #include "mbed.h" |
issaiass | 0:bbc9cfdee3bc | 2 | #include "EthernetInterface.h" |
vpcola | 1:10cb71a29f88 | 3 | #include "SDFileSystem.h" |
vpcola | 1:10cb71a29f88 | 4 | #include "Shell.h" |
issaiass | 0:bbc9cfdee3bc | 5 | |
issaiass | 0:bbc9cfdee3bc | 6 | #define MBED_DEV_IP "192.168.0.52" |
issaiass | 0:bbc9cfdee3bc | 7 | #define MBED_DEV_MASK "255.255.255.0" |
issaiass | 0:bbc9cfdee3bc | 8 | #define MBED_DEV_GW "0.0.0.0" |
issaiass | 0:bbc9cfdee3bc | 9 | #define ECHO_SERVER_PORT 5000 |
issaiass | 0:bbc9cfdee3bc | 10 | |
vpcola | 1:10cb71a29f88 | 11 | #define SD_MOSI PTE3 |
vpcola | 1:10cb71a29f88 | 12 | #define SD_MISO PTE1 |
vpcola | 1:10cb71a29f88 | 13 | #define SD_SCLK PTE2 |
vpcola | 1:10cb71a29f88 | 14 | #define SD_CS PTE4 |
vpcola | 1:10cb71a29f88 | 15 | #define SD_DETECT PTE6 |
vpcola | 1:10cb71a29f88 | 16 | |
vpcola | 1:10cb71a29f88 | 17 | Serial pc(PTC17,PTC16); |
vpcola | 1:10cb71a29f88 | 18 | SDFileSystem sd(SD_MOSI, SD_MISO, SD_SCLK, SD_CS, "sd"); |
vpcola | 1:10cb71a29f88 | 19 | |
vpcola | 1:10cb71a29f88 | 20 | #define SHELL_STACK_SIZ 1024 |
vpcola | 1:10cb71a29f88 | 21 | // Pre-allocate the shell's stack (on global mem) |
vpcola | 1:10cb71a29f88 | 22 | unsigned char shellStack[SHELL_STACK_SIZ]; |
vpcola | 1:10cb71a29f88 | 23 | Shell shell(&pc); |
vpcola | 1:10cb71a29f88 | 24 | |
vpcola | 1:10cb71a29f88 | 25 | /** |
vpcola | 1:10cb71a29f88 | 26 | * \brief Gets the amount of free memory |
vpcola | 1:10cb71a29f88 | 27 | * \param none |
vpcola | 1:10cb71a29f88 | 28 | * \return none |
vpcola | 1:10cb71a29f88 | 29 | **/ |
vpcola | 1:10cb71a29f88 | 30 | static void cmd_mem(Stream * chp, int argc, char * argv[]) |
vpcola | 1:10cb71a29f88 | 31 | { |
vpcola | 1:10cb71a29f88 | 32 | // In order to get free mem within RTOS |
vpcola | 1:10cb71a29f88 | 33 | // we need to get the main thread's stack pointer |
vpcola | 1:10cb71a29f88 | 34 | // and subtract it with the top of the heap |
vpcola | 1:10cb71a29f88 | 35 | // ------+-------------------+ Last Address of RAM (INITIAL_SP) |
vpcola | 1:10cb71a29f88 | 36 | // | Scheduler Stack | |
vpcola | 1:10cb71a29f88 | 37 | // +-------------------+ |
vpcola | 1:10cb71a29f88 | 38 | // | Main Thread Stack | |
vpcola | 1:10cb71a29f88 | 39 | // | | | |
vpcola | 1:10cb71a29f88 | 40 | // | v | |
vpcola | 1:10cb71a29f88 | 41 | // +-------------------+ <- bottom_of_stack/__get_MSP() |
vpcola | 1:10cb71a29f88 | 42 | // RAM | | |
vpcola | 1:10cb71a29f88 | 43 | // | Available RAM | |
vpcola | 1:10cb71a29f88 | 44 | // | | |
vpcola | 1:10cb71a29f88 | 45 | // +-------------------+ <- top_of_heap |
vpcola | 1:10cb71a29f88 | 46 | // | ^ | |
vpcola | 1:10cb71a29f88 | 47 | // | | | |
vpcola | 1:10cb71a29f88 | 48 | // | Heap | |
vpcola | 1:10cb71a29f88 | 49 | // +-------------------+ <- __end__ / HEAP_START (linker defined var) |
vpcola | 1:10cb71a29f88 | 50 | // | ZI | |
vpcola | 1:10cb71a29f88 | 51 | // +-------------------+ |
vpcola | 1:10cb71a29f88 | 52 | // | ZI: Shell Stack | |
vpcola | 1:10cb71a29f88 | 53 | // +-------------------+ |
vpcola | 1:10cb71a29f88 | 54 | // | ZI: Idle Stack | |
vpcola | 1:10cb71a29f88 | 55 | // +-------------------+ |
vpcola | 1:10cb71a29f88 | 56 | // | ZI: Timer Stack | |
vpcola | 1:10cb71a29f88 | 57 | // +-------------------+ |
vpcola | 1:10cb71a29f88 | 58 | // | RW | |
vpcola | 1:10cb71a29f88 | 59 | // ------+===================+ First Address of RAM |
vpcola | 1:10cb71a29f88 | 60 | // | | |
vpcola | 1:10cb71a29f88 | 61 | // Flash | | |
vpcola | 1:10cb71a29f88 | 62 | // |
vpcola | 1:10cb71a29f88 | 63 | |
vpcola | 1:10cb71a29f88 | 64 | uint32_t bottom_of_stack = __get_MSP(); |
vpcola | 1:10cb71a29f88 | 65 | char * top_of_heap = (char *) malloc(sizeof(char)); |
vpcola | 1:10cb71a29f88 | 66 | uint32_t diff = bottom_of_stack - (uint32_t) top_of_heap; |
vpcola | 1:10cb71a29f88 | 67 | |
vpcola | 1:10cb71a29f88 | 68 | free((void *) top_of_heap); |
vpcola | 1:10cb71a29f88 | 69 | |
vpcola | 1:10cb71a29f88 | 70 | chp->printf("Available Memory : %d bytes\r\n", |
vpcola | 1:10cb71a29f88 | 71 | diff); |
vpcola | 1:10cb71a29f88 | 72 | } |
vpcola | 1:10cb71a29f88 | 73 | |
vpcola | 1:10cb71a29f88 | 74 | /** |
vpcola | 1:10cb71a29f88 | 75 | * \brief List Directories and files |
vpcola | 1:10cb71a29f88 | 76 | * \param none |
vpcola | 1:10cb71a29f88 | 77 | * \return int |
vpcola | 1:10cb71a29f88 | 78 | **/ |
vpcola | 1:10cb71a29f88 | 79 | static void cmd_ls(Stream * chp, int argc, char * argv[]) |
vpcola | 1:10cb71a29f88 | 80 | { |
vpcola | 1:10cb71a29f88 | 81 | DIR * dp; |
vpcola | 1:10cb71a29f88 | 82 | struct dirent * dirp; |
vpcola | 1:10cb71a29f88 | 83 | FILINFO fileInfo; |
vpcola | 1:10cb71a29f88 | 84 | char dirroot[256]; |
vpcola | 1:10cb71a29f88 | 85 | |
vpcola | 1:10cb71a29f88 | 86 | if (argc >= 1) |
vpcola | 1:10cb71a29f88 | 87 | sprintf(dirroot, "/sd/%s", argv[0]); |
vpcola | 1:10cb71a29f88 | 88 | else |
vpcola | 1:10cb71a29f88 | 89 | sprintf(dirroot, "/sd"); |
vpcola | 1:10cb71a29f88 | 90 | |
vpcola | 1:10cb71a29f88 | 91 | chp->printf("Listing directory [%s]\r\n", dirroot); |
vpcola | 1:10cb71a29f88 | 92 | |
vpcola | 1:10cb71a29f88 | 93 | dp = opendir(dirroot); |
vpcola | 1:10cb71a29f88 | 94 | while((dirp = readdir(dp)) != NULL) |
vpcola | 1:10cb71a29f88 | 95 | { |
vpcola | 1:10cb71a29f88 | 96 | if (sd.stat(dirp->d_name, &fileInfo) == 0) |
vpcola | 1:10cb71a29f88 | 97 | { |
vpcola | 1:10cb71a29f88 | 98 | if (fileInfo.fattrib & AM_DIR ) |
vpcola | 1:10cb71a29f88 | 99 | chp->printf("<DIR>\t\t"); |
vpcola | 1:10cb71a29f88 | 100 | else |
vpcola | 1:10cb71a29f88 | 101 | chp->printf("%ld\t\t", fileInfo.fsize); |
vpcola | 1:10cb71a29f88 | 102 | } |
vpcola | 1:10cb71a29f88 | 103 | chp->printf("%s\r\n", dirp->d_name); |
vpcola | 1:10cb71a29f88 | 104 | } |
vpcola | 1:10cb71a29f88 | 105 | closedir(dp); |
vpcola | 1:10cb71a29f88 | 106 | } |
vpcola | 1:10cb71a29f88 | 107 | |
issaiass | 0:bbc9cfdee3bc | 108 | |
issaiass | 0:bbc9cfdee3bc | 109 | int main (void) { |
issaiass | 0:bbc9cfdee3bc | 110 | EthernetInterface eth; |
issaiass | 0:bbc9cfdee3bc | 111 | eth.init(MBED_DEV_IP, MBED_DEV_MASK, MBED_DEV_GW); //Assign a device ip, mask and gateway |
issaiass | 0:bbc9cfdee3bc | 112 | eth.connect(); |
issaiass | 0:bbc9cfdee3bc | 113 | printf("IP Address is %s\n", eth.getIPAddress()); |
issaiass | 0:bbc9cfdee3bc | 114 | |
issaiass | 0:bbc9cfdee3bc | 115 | TCPSocketServer server; |
issaiass | 0:bbc9cfdee3bc | 116 | server.bind(ECHO_SERVER_PORT); |
issaiass | 0:bbc9cfdee3bc | 117 | server.listen(); |
vpcola | 1:10cb71a29f88 | 118 | |
vpcola | 1:10cb71a29f88 | 119 | // Start the shell |
vpcola | 1:10cb71a29f88 | 120 | pc.printf("Starting debug shell ...\r\n"); |
vpcola | 1:10cb71a29f88 | 121 | shell.addCommand("ls", cmd_ls); |
vpcola | 1:10cb71a29f88 | 122 | shell.addCommand("mem", cmd_mem); |
vpcola | 1:10cb71a29f88 | 123 | // Start the thread statically (separate stack) |
vpcola | 1:10cb71a29f88 | 124 | shell.start(osPriorityNormal, SHELL_STACK_SIZ, shellStack); |
issaiass | 0:bbc9cfdee3bc | 125 | |
vpcola | 1:10cb71a29f88 | 126 | |
issaiass | 0:bbc9cfdee3bc | 127 | while (true) { |
issaiass | 0:bbc9cfdee3bc | 128 | printf("\nWait for new connection...\n"); |
issaiass | 0:bbc9cfdee3bc | 129 | TCPSocketConnection client; |
issaiass | 0:bbc9cfdee3bc | 130 | server.accept(client); |
issaiass | 0:bbc9cfdee3bc | 131 | client.set_blocking(false, 1500); // Timeout after (1.5)s |
issaiass | 0:bbc9cfdee3bc | 132 | |
issaiass | 0:bbc9cfdee3bc | 133 | printf("Connection from: %s\n", client.get_address()); |
issaiass | 0:bbc9cfdee3bc | 134 | char buffer[256]; |
issaiass | 0:bbc9cfdee3bc | 135 | while (true) { |
issaiass | 0:bbc9cfdee3bc | 136 | int n = client.receive(buffer, sizeof(buffer)); |
issaiass | 0:bbc9cfdee3bc | 137 | if (n <= 0) break; |
issaiass | 0:bbc9cfdee3bc | 138 | |
issaiass | 0:bbc9cfdee3bc | 139 | client.send_all(buffer, n); |
issaiass | 0:bbc9cfdee3bc | 140 | if (n <= 0) break; |
issaiass | 0:bbc9cfdee3bc | 141 | } |
issaiass | 0:bbc9cfdee3bc | 142 | |
issaiass | 0:bbc9cfdee3bc | 143 | client.close(); |
issaiass | 0:bbc9cfdee3bc | 144 | } |
issaiass | 0:bbc9cfdee3bc | 145 | } |