.

Dependencies:   mbed EthernetInterface mbed-rtos

Fork of Bootloader_K64F by Erik -

Committer:
Sissors
Date:
Sat Apr 23 18:24:07 2016 +0000
Revision:
8:00cefe0d59ed
Parent:
7:4ab0430d06e3
Child:
9:56ed3a56ecc3
Basic functionality works over HTTP!

Who changed what in which revision?

UserRevisionLine numberNew 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 8:00cefe0d59ed 11 //Bootloader functions
Sissors 7:4ab0430d06e3 12 int *(*program_flash_boot)(int, char*, unsigned int) = (int *(*)(int, char*, unsigned int))0x795C9;
Sissors 7:4ab0430d06e3 13 int *(*erase_sector_boot)(int) = (int *(*)(int))0x79465;
Sissors 7:4ab0430d06e3 14 void *(*bootloader)(int) = (void *(*)(int))0x79121;
Sissors 7:4ab0430d06e3 15
Sissors 8:00cefe0d59ed 16
Sissors 7:4ab0430d06e3 17 static void write(char *value)
Sissors 7:4ab0430d06e3 18 {
Sissors 8:00cefe0d59ed 19 int i = 0;
Sissors 8:00cefe0d59ed 20 //Loop through string and send everything
Sissors 8:00cefe0d59ed 21 while(*(value+i) != '\0') {
Sissors 8:00cefe0d59ed 22 while(!(UART0->S1 & UART_S1_TDRE_MASK));
Sissors 8:00cefe0d59ed 23 UART0->D = *(value+i);
Sissors 8:00cefe0d59ed 24 i++;
Sissors 7:4ab0430d06e3 25 }
Sissors 8:00cefe0d59ed 26 }
Sissors 7:4ab0430d06e3 27
Sissors 8:00cefe0d59ed 28 //Getting the new binary
Sissors 8:00cefe0d59ed 29 void write_flash(void)
Sissors 8:00cefe0d59ed 30 {
Sissors 8:00cefe0d59ed 31
Sissors 7:4ab0430d06e3 32 printf("Erasing flash!\r\n");
Sissors 7:4ab0430d06e3 33 for (int i = 0; i < NUM_SECTORS; i++)
Sissors 7:4ab0430d06e3 34 erase_sector_boot(START_ADDRESS + i * SECTOR_SIZE);
Sissors 8:00cefe0d59ed 35
Sissors 8:00cefe0d59ed 36 printf("Connecting ethernet\r\n");
Sissors 8:00cefe0d59ed 37 EthernetInterface eth;
Sissors 8:00cefe0d59ed 38 eth.init(); //Use DHCP
Sissors 8:00cefe0d59ed 39 eth.connect();
Sissors 8:00cefe0d59ed 40 printf("IP Address is %s\r\n", eth.getIPAddress());
Sissors 8:00cefe0d59ed 41
Sissors 8:00cefe0d59ed 42 TCPSocketConnection sock;
Sissors 8:00cefe0d59ed 43 sock.connect("192.168.0.18", 80);
Sissors 8:00cefe0d59ed 44 printf("Connected to mbed\r\n");
Sissors 8:00cefe0d59ed 45 char http_cmd[] = "HEAD /loader_test.bin HTTP/1.1\r\nHost: 192.168.0.18 80\r\n\r\n";
Sissors 8:00cefe0d59ed 46 sock.send_all(http_cmd, sizeof(http_cmd)-1);
Sissors 8:00cefe0d59ed 47 printf("Send head request\r\n");
Sissors 8:00cefe0d59ed 48
Sissors 7:4ab0430d06e3 49 char buffer[BUFFER_SIZE];
Sissors 8:00cefe0d59ed 50 printf("Receiving head (hihi)\r\n");
Sissors 8:00cefe0d59ed 51 int headlength = sock.receive(buffer, sizeof(buffer));
Sissors 8:00cefe0d59ed 52 if (headlength == BUFFER_SIZE) {
Sissors 8:00cefe0d59ed 53 printf("Head exceeded expected maximum, loading will fail\r\n");
Sissors 8:00cefe0d59ed 54 while(1);
Sissors 8:00cefe0d59ed 55 }
Sissors 8:00cefe0d59ed 56 sock.close();
Sissors 8:00cefe0d59ed 57 wait(2);
Sissors 8:00cefe0d59ed 58 sock.connect("192.168.0.18", 80);
Sissors 8:00cefe0d59ed 59
Sissors 8:00cefe0d59ed 60 char http_cmd2[] = "GET /loader_test.bin HTTP/1.1\r\nHost: 192.168.0.18 80\r\n\r\n";
Sissors 8:00cefe0d59ed 61
Sissors 8:00cefe0d59ed 62 sock.send_all(http_cmd2, sizeof(http_cmd2)-1);
Sissors 8:00cefe0d59ed 63 printf("Send get request\r\n");
Sissors 8:00cefe0d59ed 64 if (sock.receive(buffer, headlength) != headlength) {
Sissors 8:00cefe0d59ed 65 printf("No response\r\n");
Sissors 8:00cefe0d59ed 66 while(1);
Sissors 8:00cefe0d59ed 67 }
Sissors 7:4ab0430d06e3 68
Sissors 8:00cefe0d59ed 69 int count;
Sissors 8:00cefe0d59ed 70 int ret;
Sissors 8:00cefe0d59ed 71 int received = 0;
Sissors 8:00cefe0d59ed 72 while (true) {
Sissors 8:00cefe0d59ed 73 //printf("Receiving\r\n");
Sissors 8:00cefe0d59ed 74 ret = sock.receive(&buffer[received], 16-received);
Sissors 8:00cefe0d59ed 75 received = received + ret;
Sissors 8:00cefe0d59ed 76 if (ret <= 0) {
Sissors 8:00cefe0d59ed 77 if (received != 0) {
Sissors 8:00cefe0d59ed 78 program_flash_boot(count+START_ADDRESS, buffer, 16);
Sissors 7:4ab0430d06e3 79 }
Sissors 8:00cefe0d59ed 80 break;
Sissors 8:00cefe0d59ed 81 }
Sissors 8:00cefe0d59ed 82
Sissors 8:00cefe0d59ed 83 printf("%d\r\n", ret);
Sissors 8:00cefe0d59ed 84 if (received == 16) {
Sissors 8:00cefe0d59ed 85 __disable_irq();
Sissors 8:00cefe0d59ed 86 if (program_flash_boot(count+START_ADDRESS, buffer, 16) != 0) {
Sissors 8:00cefe0d59ed 87 write("Error!\r\n");
Sissors 8:00cefe0d59ed 88 while(1);
Sissors 8:00cefe0d59ed 89 }
Sissors 8:00cefe0d59ed 90 __enable_irq();
Sissors 8:00cefe0d59ed 91 count += received;
Sissors 8:00cefe0d59ed 92 received = 0;
Sissors 7:4ab0430d06e3 93
Sissors 7:4ab0430d06e3 94 }
Sissors 8:00cefe0d59ed 95
Sissors 8:00cefe0d59ed 96
Sissors 7:4ab0430d06e3 97 }
Sissors 8:00cefe0d59ed 98
Sissors 8:00cefe0d59ed 99 sock.close();
Sissors 8:00cefe0d59ed 100
Sissors 8:00cefe0d59ed 101 eth.disconnect();
Sissors 8:00cefe0d59ed 102 printf("Done\r\n");
Sissors 7:4ab0430d06e3 103
Sissors 7:4ab0430d06e3 104 bootloader(count);
Sissors 8:00cefe0d59ed 105
Sissors 7:4ab0430d06e3 106 }