microServiceBus.com is an integration platform for IoT and enterprise applications. This platform lets you expose microservices from small devices and large systems using a hosting infrastructure. These host can run on both Linux and Windows using components built either natively or using node.js.

Dependencies:   C12832 EthernetInterface MbedJSONValue WebSocketClient mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bootloader.h Source File

bootloader.h

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "rtos.h"
00004 
00005 #define START_ADDRESS   0x80000
00006 #define NUM_SECTORS     120
00007 #define SECTOR_SIZE     4096
00008 #define BUFFER_SIZE     1024
00009 
00010 
00011 /*************************************************************/
00012 //               Bootloader functions
00013 /*************************************************************/
00014 int *(*program_flash_boot)(int, char*, unsigned int) = (int *(*)(int, char*, unsigned int))0x795C9;
00015 int *(*erase_sector_boot)(int) = (int *(*)(int))0x79465;
00016 void *(*bootloader)(int) = (void *(*)(int))0x79121;
00017 
00018 
00019 /*************************************************************/
00020 //           Retrieving binary from server
00021 /*************************************************************/
00022 void write_flash(void)
00023 {
00024 
00025     printf("Erasing flash!\r\n");
00026     for (int i = 0; i < NUM_SECTORS; i++)
00027         erase_sector_boot(START_ADDRESS + i * SECTOR_SIZE);
00028 
00029     printf("Connecting ethernet\r\n");
00030     EthernetInterface eth;
00031     //eth.init(); //Use DHCP
00032     if(eth.connect() != 0){
00033         printf("Unable to connect");
00034     }
00035     
00036     printf("IP Address is %s\r\n", eth.getIPAddress());
00037     
00038     
00039     TCPSocketConnection sock;
00040     
00041     //----------------YOUR SERVER DETAILS-----------------------//
00042     sock.connect("192.168.1.64", 80);
00043     char http_cmd[] = "GET /api/mbed HTTP/1.1\r\nHost: 192.168.1.64\r\n\r\n";
00044     //----------------YOUR SERVER DETAILS-----------------------//
00045     
00046     sock.send_all(http_cmd, sizeof(http_cmd)-1);
00047     
00048     char buffer[BUFFER_SIZE];
00049     int received;
00050     int binsize;
00051     int bufcount;
00052     int remaining;
00053     int count = 0;
00054     
00055     //Receive first packet
00056     received = sock.receive(buffer, sizeof(buffer));
00057     if (received <= 0) {
00058         printf("No data received from server\r\n");
00059         while(1);
00060     }
00061     
00062     //Search for "Content-Length", if not available, receive more until buffer is full    
00063     while(strstr(buffer, "Content-Length: ") == 0) {
00064         if (received == sizeof(buffer)) {
00065             printf("Could not determine size of bin file\r\n");
00066             while(1);
00067         } else {
00068             received += sock.receive(buffer + received, sizeof(buffer) - received);   
00069         }
00070     }
00071     //Determine size of the file
00072     char *temp = strstr(buffer, "Content-Length: ") + 16;   //'16' is size of "Content-Length: "
00073     printf(buffer);
00074     sscanf(temp, "%d", &binsize); 
00075     printf("Size of the binary = %d bytes\r\n", binsize);   
00076 
00077     //Search for "\r\n\r\n" (beginning of bin file), if not available, receive more until buffer is full    
00078     while(strstr(buffer, "\r\n\r\n") == 0) {
00079         if (received == sizeof(buffer)) {
00080             printf("Could not find start of bin file\r\n");
00081             while(1);
00082         } else {
00083             received += sock.receive(buffer+received, sizeof(buffer) - received);   
00084         }
00085     }
00086     //Get pointer to begin of the file in the buffer
00087     temp = strstr(buffer, "\r\n\r\n") + 4;   //'16' is size of "\r\n\r\n"
00088     
00089     //See how much of the bin file we already received, and move this to the start of the buffer
00090     bufcount = received - ((uint32_t)temp - (uint32_t)buffer);
00091     memmove(buffer, temp, bufcount);
00092     printf("Received %d bytes\r\n", bufcount);
00093     
00094     //Start receiving the remaining bin file
00095     remaining = binsize - bufcount;
00096         
00097     while (remaining > 0) {
00098         //Completely fill the buffer each time so we can easily write it to flash
00099         while (bufcount < sizeof(buffer)) {
00100             //Try to receive remainder of the buffer
00101             received = sock.receive(&buffer[bufcount], sizeof(buffer)-bufcount);
00102             //printf("Received %d\r\n", received);
00103             printf(".");
00104             if (received <= 0) {
00105                 printf("Error, should not happen\r\n");
00106                 while(1);
00107             }
00108             
00109             //Track how much we received and how much is left
00110             bufcount += received;
00111             remaining -= received;
00112             if (remaining == 0) {
00113                 if (program_flash_boot(count+START_ADDRESS, buffer, sizeof(buffer)) != 0) {
00114                     printf("Error @ 0x%X!\r\n", count);
00115                     while(1);
00116                 }
00117                 count += sizeof(buffer);
00118                 break;
00119             }
00120         }
00121         //Buffer is full, program it and increase the counter (the counter is a bit redundant, we could get it from the other variables)
00122         if (program_flash_boot(count+START_ADDRESS, buffer, sizeof(buffer)) != 0) {
00123             printf("Error @ 0x%X!\r\n", count);
00124             while(1);
00125         }
00126         count += sizeof(buffer);
00127         bufcount = 0;
00128     }
00129     printf("Done\r\n");
00130     sock.close();
00131     
00132     eth.disconnect();
00133 
00134     bootloader(count);
00135 
00136 }