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
bootloader.h@0:e542df4c4901, 2016-08-25 (annotated)
- Committer:
- wmmihaa
- Date:
- Thu Aug 25 13:53:00 2016 +0000
- Revision:
- 0:e542df4c4901
Created
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wmmihaa | 0:e542df4c4901 | 1 | #include "mbed.h" |
wmmihaa | 0:e542df4c4901 | 2 | #include "EthernetInterface.h" |
wmmihaa | 0:e542df4c4901 | 3 | #include "rtos.h" |
wmmihaa | 0:e542df4c4901 | 4 | |
wmmihaa | 0:e542df4c4901 | 5 | #define START_ADDRESS 0x80000 |
wmmihaa | 0:e542df4c4901 | 6 | #define NUM_SECTORS 120 |
wmmihaa | 0:e542df4c4901 | 7 | #define SECTOR_SIZE 4096 |
wmmihaa | 0:e542df4c4901 | 8 | #define BUFFER_SIZE 1024 |
wmmihaa | 0:e542df4c4901 | 9 | |
wmmihaa | 0:e542df4c4901 | 10 | |
wmmihaa | 0:e542df4c4901 | 11 | /*************************************************************/ |
wmmihaa | 0:e542df4c4901 | 12 | // Bootloader functions |
wmmihaa | 0:e542df4c4901 | 13 | /*************************************************************/ |
wmmihaa | 0:e542df4c4901 | 14 | int *(*program_flash_boot)(int, char*, unsigned int) = (int *(*)(int, char*, unsigned int))0x795C9; |
wmmihaa | 0:e542df4c4901 | 15 | int *(*erase_sector_boot)(int) = (int *(*)(int))0x79465; |
wmmihaa | 0:e542df4c4901 | 16 | void *(*bootloader)(int) = (void *(*)(int))0x79121; |
wmmihaa | 0:e542df4c4901 | 17 | |
wmmihaa | 0:e542df4c4901 | 18 | |
wmmihaa | 0:e542df4c4901 | 19 | /*************************************************************/ |
wmmihaa | 0:e542df4c4901 | 20 | // Retrieving binary from server |
wmmihaa | 0:e542df4c4901 | 21 | /*************************************************************/ |
wmmihaa | 0:e542df4c4901 | 22 | void write_flash(void) |
wmmihaa | 0:e542df4c4901 | 23 | { |
wmmihaa | 0:e542df4c4901 | 24 | |
wmmihaa | 0:e542df4c4901 | 25 | printf("Erasing flash!\r\n"); |
wmmihaa | 0:e542df4c4901 | 26 | for (int i = 0; i < NUM_SECTORS; i++) |
wmmihaa | 0:e542df4c4901 | 27 | erase_sector_boot(START_ADDRESS + i * SECTOR_SIZE); |
wmmihaa | 0:e542df4c4901 | 28 | |
wmmihaa | 0:e542df4c4901 | 29 | printf("Connecting ethernet\r\n"); |
wmmihaa | 0:e542df4c4901 | 30 | EthernetInterface eth; |
wmmihaa | 0:e542df4c4901 | 31 | //eth.init(); //Use DHCP |
wmmihaa | 0:e542df4c4901 | 32 | if(eth.connect() != 0){ |
wmmihaa | 0:e542df4c4901 | 33 | printf("Unable to connect"); |
wmmihaa | 0:e542df4c4901 | 34 | } |
wmmihaa | 0:e542df4c4901 | 35 | |
wmmihaa | 0:e542df4c4901 | 36 | printf("IP Address is %s\r\n", eth.getIPAddress()); |
wmmihaa | 0:e542df4c4901 | 37 | |
wmmihaa | 0:e542df4c4901 | 38 | |
wmmihaa | 0:e542df4c4901 | 39 | TCPSocketConnection sock; |
wmmihaa | 0:e542df4c4901 | 40 | |
wmmihaa | 0:e542df4c4901 | 41 | //----------------YOUR SERVER DETAILS-----------------------// |
wmmihaa | 0:e542df4c4901 | 42 | sock.connect("192.168.1.64", 80); |
wmmihaa | 0:e542df4c4901 | 43 | char http_cmd[] = "GET /api/mbed HTTP/1.1\r\nHost: 192.168.1.64\r\n\r\n"; |
wmmihaa | 0:e542df4c4901 | 44 | //----------------YOUR SERVER DETAILS-----------------------// |
wmmihaa | 0:e542df4c4901 | 45 | |
wmmihaa | 0:e542df4c4901 | 46 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
wmmihaa | 0:e542df4c4901 | 47 | |
wmmihaa | 0:e542df4c4901 | 48 | char buffer[BUFFER_SIZE]; |
wmmihaa | 0:e542df4c4901 | 49 | int received; |
wmmihaa | 0:e542df4c4901 | 50 | int binsize; |
wmmihaa | 0:e542df4c4901 | 51 | int bufcount; |
wmmihaa | 0:e542df4c4901 | 52 | int remaining; |
wmmihaa | 0:e542df4c4901 | 53 | int count = 0; |
wmmihaa | 0:e542df4c4901 | 54 | |
wmmihaa | 0:e542df4c4901 | 55 | //Receive first packet |
wmmihaa | 0:e542df4c4901 | 56 | received = sock.receive(buffer, sizeof(buffer)); |
wmmihaa | 0:e542df4c4901 | 57 | if (received <= 0) { |
wmmihaa | 0:e542df4c4901 | 58 | printf("No data received from server\r\n"); |
wmmihaa | 0:e542df4c4901 | 59 | while(1); |
wmmihaa | 0:e542df4c4901 | 60 | } |
wmmihaa | 0:e542df4c4901 | 61 | |
wmmihaa | 0:e542df4c4901 | 62 | //Search for "Content-Length", if not available, receive more until buffer is full |
wmmihaa | 0:e542df4c4901 | 63 | while(strstr(buffer, "Content-Length: ") == 0) { |
wmmihaa | 0:e542df4c4901 | 64 | if (received == sizeof(buffer)) { |
wmmihaa | 0:e542df4c4901 | 65 | printf("Could not determine size of bin file\r\n"); |
wmmihaa | 0:e542df4c4901 | 66 | while(1); |
wmmihaa | 0:e542df4c4901 | 67 | } else { |
wmmihaa | 0:e542df4c4901 | 68 | received += sock.receive(buffer + received, sizeof(buffer) - received); |
wmmihaa | 0:e542df4c4901 | 69 | } |
wmmihaa | 0:e542df4c4901 | 70 | } |
wmmihaa | 0:e542df4c4901 | 71 | //Determine size of the file |
wmmihaa | 0:e542df4c4901 | 72 | char *temp = strstr(buffer, "Content-Length: ") + 16; //'16' is size of "Content-Length: " |
wmmihaa | 0:e542df4c4901 | 73 | printf(buffer); |
wmmihaa | 0:e542df4c4901 | 74 | sscanf(temp, "%d", &binsize); |
wmmihaa | 0:e542df4c4901 | 75 | printf("Size of the binary = %d bytes\r\n", binsize); |
wmmihaa | 0:e542df4c4901 | 76 | |
wmmihaa | 0:e542df4c4901 | 77 | //Search for "\r\n\r\n" (beginning of bin file), if not available, receive more until buffer is full |
wmmihaa | 0:e542df4c4901 | 78 | while(strstr(buffer, "\r\n\r\n") == 0) { |
wmmihaa | 0:e542df4c4901 | 79 | if (received == sizeof(buffer)) { |
wmmihaa | 0:e542df4c4901 | 80 | printf("Could not find start of bin file\r\n"); |
wmmihaa | 0:e542df4c4901 | 81 | while(1); |
wmmihaa | 0:e542df4c4901 | 82 | } else { |
wmmihaa | 0:e542df4c4901 | 83 | received += sock.receive(buffer+received, sizeof(buffer) - received); |
wmmihaa | 0:e542df4c4901 | 84 | } |
wmmihaa | 0:e542df4c4901 | 85 | } |
wmmihaa | 0:e542df4c4901 | 86 | //Get pointer to begin of the file in the buffer |
wmmihaa | 0:e542df4c4901 | 87 | temp = strstr(buffer, "\r\n\r\n") + 4; //'16' is size of "\r\n\r\n" |
wmmihaa | 0:e542df4c4901 | 88 | |
wmmihaa | 0:e542df4c4901 | 89 | //See how much of the bin file we already received, and move this to the start of the buffer |
wmmihaa | 0:e542df4c4901 | 90 | bufcount = received - ((uint32_t)temp - (uint32_t)buffer); |
wmmihaa | 0:e542df4c4901 | 91 | memmove(buffer, temp, bufcount); |
wmmihaa | 0:e542df4c4901 | 92 | printf("Received %d bytes\r\n", bufcount); |
wmmihaa | 0:e542df4c4901 | 93 | |
wmmihaa | 0:e542df4c4901 | 94 | //Start receiving the remaining bin file |
wmmihaa | 0:e542df4c4901 | 95 | remaining = binsize - bufcount; |
wmmihaa | 0:e542df4c4901 | 96 | |
wmmihaa | 0:e542df4c4901 | 97 | while (remaining > 0) { |
wmmihaa | 0:e542df4c4901 | 98 | //Completely fill the buffer each time so we can easily write it to flash |
wmmihaa | 0:e542df4c4901 | 99 | while (bufcount < sizeof(buffer)) { |
wmmihaa | 0:e542df4c4901 | 100 | //Try to receive remainder of the buffer |
wmmihaa | 0:e542df4c4901 | 101 | received = sock.receive(&buffer[bufcount], sizeof(buffer)-bufcount); |
wmmihaa | 0:e542df4c4901 | 102 | //printf("Received %d\r\n", received); |
wmmihaa | 0:e542df4c4901 | 103 | printf("."); |
wmmihaa | 0:e542df4c4901 | 104 | if (received <= 0) { |
wmmihaa | 0:e542df4c4901 | 105 | printf("Error, should not happen\r\n"); |
wmmihaa | 0:e542df4c4901 | 106 | while(1); |
wmmihaa | 0:e542df4c4901 | 107 | } |
wmmihaa | 0:e542df4c4901 | 108 | |
wmmihaa | 0:e542df4c4901 | 109 | //Track how much we received and how much is left |
wmmihaa | 0:e542df4c4901 | 110 | bufcount += received; |
wmmihaa | 0:e542df4c4901 | 111 | remaining -= received; |
wmmihaa | 0:e542df4c4901 | 112 | if (remaining == 0) { |
wmmihaa | 0:e542df4c4901 | 113 | if (program_flash_boot(count+START_ADDRESS, buffer, sizeof(buffer)) != 0) { |
wmmihaa | 0:e542df4c4901 | 114 | printf("Error @ 0x%X!\r\n", count); |
wmmihaa | 0:e542df4c4901 | 115 | while(1); |
wmmihaa | 0:e542df4c4901 | 116 | } |
wmmihaa | 0:e542df4c4901 | 117 | count += sizeof(buffer); |
wmmihaa | 0:e542df4c4901 | 118 | break; |
wmmihaa | 0:e542df4c4901 | 119 | } |
wmmihaa | 0:e542df4c4901 | 120 | } |
wmmihaa | 0:e542df4c4901 | 121 | //Buffer is full, program it and increase the counter (the counter is a bit redundant, we could get it from the other variables) |
wmmihaa | 0:e542df4c4901 | 122 | if (program_flash_boot(count+START_ADDRESS, buffer, sizeof(buffer)) != 0) { |
wmmihaa | 0:e542df4c4901 | 123 | printf("Error @ 0x%X!\r\n", count); |
wmmihaa | 0:e542df4c4901 | 124 | while(1); |
wmmihaa | 0:e542df4c4901 | 125 | } |
wmmihaa | 0:e542df4c4901 | 126 | count += sizeof(buffer); |
wmmihaa | 0:e542df4c4901 | 127 | bufcount = 0; |
wmmihaa | 0:e542df4c4901 | 128 | } |
wmmihaa | 0:e542df4c4901 | 129 | printf("Done\r\n"); |
wmmihaa | 0:e542df4c4901 | 130 | sock.close(); |
wmmihaa | 0:e542df4c4901 | 131 | |
wmmihaa | 0:e542df4c4901 | 132 | eth.disconnect(); |
wmmihaa | 0:e542df4c4901 | 133 | |
wmmihaa | 0:e542df4c4901 | 134 | bootloader(count); |
wmmihaa | 0:e542df4c4901 | 135 | |
wmmihaa | 0:e542df4c4901 | 136 | } |