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