Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed EthernetInterface mbed-rtos
Fork of FOTA_K64F by
bootloader.h
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "rtos.h" 00004 #include <string> 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 // Retrieving binary from server 00020 /*************************************************************/ 00021 void write_flash(void) 00022 { 00023 00024 printf("Erasing flash!\r\n"); 00025 for (int i = 0; i < NUM_SECTORS; i++) 00026 erase_sector_boot(START_ADDRESS + i * SECTOR_SIZE); 00027 00028 printf("Connecting ethernet\r\n"); 00029 EthernetInterface eth; 00030 //eth.init(); //Use DHCP 00031 if(eth.connect() != 0){ 00032 printf("Unable to connect"); 00033 } 00034 00035 printf("IP Address is %s\r\n", eth.getIPAddress()); 00036 00037 00038 TCPSocketConnection sock; 00039 00040 //----------------YOUR SERVER DETAILS-----------------------// 00041 //sock.connect("192.168.1.64", 80); 00042 sock.connect("microservicebus-northeurope-stage.azurewebsites.net",80); 00043 /* 00044 std::string str; 00045 str = "GET /api/mbed?id="; 00046 str.append(eth.getMACAddress()); 00047 00048 size_t start_pos = 0; 00049 std::string from(":"); 00050 std::string to(""); 00051 00052 while((start_pos = str.find(from, start_pos)) != std::string::npos) { 00053 str.replace(start_pos, from.length(), to); 00054 start_pos += to.length(); // Handles case where 'to' is a substring of 'from' 00055 } 00056 00057 str.append( " HTTP/1.1\r\nHost: 192.168.1.64\r\n\r\n" ); 00058 00059 char *http_cmd = &str[0u]; 00060 */ 00061 char http_cmd[] = "GET /api/mbed?id=36:41:55:13:08:16 HTTP/1.1\r\nHost: 192.168.1.64\r\n\r\n"; 00062 //----------------YOUR SERVER DETAILS-----------------------// 00063 00064 printf("Registing at "); 00065 printf(http_cmd); 00066 printf("\r\n"); 00067 00068 sock.send_all(http_cmd, sizeof(http_cmd)-1); 00069 00070 char buffer[BUFFER_SIZE]; 00071 int received; 00072 int binsize; 00073 int bufcount; 00074 int remaining; 00075 int count = 0; 00076 00077 //Receive first packet 00078 received = sock.receive(buffer, sizeof(buffer)); 00079 if (received <= 0) { 00080 printf("No data received from server\r\n"); 00081 while(1); 00082 } 00083 00084 //Search for "Content-Length", if not available, receive more until buffer is full 00085 while(strstr(buffer, "Content-Length: ") == 0) { 00086 if (received == sizeof(buffer)) { 00087 printf("Could not determine size of bin file\r\n"); 00088 while(1); 00089 } else { 00090 received += sock.receive(buffer + received, sizeof(buffer) - received); 00091 } 00092 } 00093 //Determine size of the file 00094 char *temp = strstr(buffer, "Content-Length: ") + 16; //'16' is size of "Content-Length: " 00095 printf(buffer); 00096 sscanf(temp, "%d", &binsize); 00097 printf("Size of the binary = %d bytes\r\n", binsize); 00098 00099 //Search for "\r\n\r\n" (beginning of bin file), if not available, receive more until buffer is full 00100 while(strstr(buffer, "\r\n\r\n") == 0) { 00101 if (received == sizeof(buffer)) { 00102 printf("Could not find start of bin file\r\n"); 00103 while(1); 00104 } else { 00105 received += sock.receive(buffer+received, sizeof(buffer) - received); 00106 } 00107 } 00108 //Get pointer to begin of the file in the buffer 00109 temp = strstr(buffer, "\r\n\r\n") + 4; //'16' is size of "\r\n\r\n" 00110 00111 //See how much of the bin file we already received, and move this to the start of the buffer 00112 bufcount = received - ((uint32_t)temp - (uint32_t)buffer); 00113 memmove(buffer, temp, bufcount); 00114 printf("Received %d bytes\r\n", bufcount); 00115 00116 //Start receiving the remaining bin file 00117 remaining = binsize - bufcount; 00118 00119 while (remaining > 0) { 00120 //Completely fill the buffer each time so we can easily write it to flash 00121 while (bufcount < sizeof(buffer)) { 00122 //Try to receive remainder of the buffer 00123 received = sock.receive(&buffer[bufcount], sizeof(buffer)-bufcount); 00124 //printf("Received %d\r\n", received); 00125 printf("."); 00126 if (received <= 0) { 00127 printf("Error, should not happen\r\n"); 00128 while(1); 00129 } 00130 00131 //Track how much we received and how much is left 00132 bufcount += received; 00133 remaining -= received; 00134 if (remaining == 0) { 00135 if (program_flash_boot(count+START_ADDRESS, buffer, sizeof(buffer)) != 0) { 00136 printf("Error @ 0x%X!\r\n", count); 00137 while(1); 00138 } 00139 count += sizeof(buffer); 00140 break; 00141 } 00142 } 00143 //Buffer is full, program it and increase the counter (the counter is a bit redundant, we could get it from the other variables) 00144 if (program_flash_boot(count+START_ADDRESS, buffer, sizeof(buffer)) != 0) { 00145 printf("Error @ 0x%X!\r\n", count); 00146 while(1); 00147 } 00148 count += sizeof(buffer); 00149 bufcount = 0; 00150 } 00151 printf("Done\r\n"); 00152 sock.close(); 00153 00154 eth.disconnect(); 00155 00156 bootloader(count); 00157 }
Generated on Tue Jul 12 2022 21:33:01 by
1.7.2
