.

Dependencies:   mbed EthernetInterface mbed-rtos

Fork of Bootloader_K64F by Erik -

Revision:
8:00cefe0d59ed
Parent:
7:4ab0430d06e3
Child:
9:56ed3a56ecc3
--- a/Bootloader/bootloader.h	Sat Apr 23 15:10:04 2016 +0000
+++ b/Bootloader/bootloader.h	Sat Apr 23 18:24:07 2016 +0000
@@ -1,87 +1,106 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "rtos.h"
+
 #define START_ADDRESS   0x80000
 #define NUM_SECTORS     120
 #define SECTOR_SIZE     4096
-#define BUFFER_SIZE     16
-#define TIMEOUT            10000000
+#define BUFFER_SIZE     1024
 
+
+//Bootloader functions
 int *(*program_flash_boot)(int, char*, unsigned int) = (int *(*)(int, char*, unsigned int))0x795C9;
 int *(*erase_sector_boot)(int) = (int *(*)(int))0x79465;
 void *(*bootloader)(int) = (void *(*)(int))0x79121;
 
+
 static void write(char *value)
 {
-        int i = 0;
-        //Loop through string and send everything
-        while(*(value+i) != '\0') {
-            while(!(UART0->S1 & UART_S1_TDRE_MASK));
-            UART0->D = *(value+i);
-            i++;
-        }
+    int i = 0;
+    //Loop through string and send everything
+    while(*(value+i) != '\0') {
+        while(!(UART0->S1 & UART_S1_TDRE_MASK));
+        UART0->D = *(value+i);
+        i++;
     }
-
+}
 
-void write_flash(void) {
-    __disable_irq();
+//Getting the new binary
+void write_flash(void)
+{
+
     printf("Erasing flash!\r\n");
     for (int i = 0; i < NUM_SECTORS; i++)
         erase_sector_boot(START_ADDRESS + i * SECTOR_SIZE);
-    
+
+    printf("Connecting ethernet\r\n");
+    EthernetInterface eth;
+    eth.init(); //Use DHCP
+    eth.connect();
+    printf("IP Address is %s\r\n", eth.getIPAddress());
+
+    TCPSocketConnection sock;
+    sock.connect("192.168.0.18", 80);
+    printf("Connected to mbed\r\n");
+    char http_cmd[] = "HEAD /loader_test.bin HTTP/1.1\r\nHost: 192.168.0.18 80\r\n\r\n";
+    sock.send_all(http_cmd, sizeof(http_cmd)-1);
+    printf("Send head request\r\n");
+
     char buffer[BUFFER_SIZE];
-    uint32_t count = 0;
-    uint8_t buffercount = 0;
-    uint32_t timeout = 0;
-    
-    //Wait until data is sent
-    while(!(UART0->S1 & UART_S1_RDRF_MASK));
-    
-    //Data receive loop
-    while(1) {
-        //Check if there is new data
-        if (UART0->S1 & UART_S1_RDRF_MASK) {
-            //Place data in buffer
-            buffer[buffercount] = UART0->D;
-            buffercount++;
-            
-            //Reset timeout
-            timeout = 0;
+    printf("Receiving head (hihi)\r\n");
+    int headlength = sock.receive(buffer, sizeof(buffer));
+    if (headlength == BUFFER_SIZE) {
+        printf("Head exceeded expected maximum, loading will fail\r\n");
+        while(1);
+    }
+    sock.close();
+    wait(2);
+    sock.connect("192.168.0.18", 80);
+
+    char http_cmd2[] = "GET /loader_test.bin HTTP/1.1\r\nHost: 192.168.0.18 80\r\n\r\n";
+
+    sock.send_all(http_cmd2, sizeof(http_cmd2)-1);
+    printf("Send get request\r\n");
+    if (sock.receive(buffer, headlength) != headlength) {
+        printf("No response\r\n");
+        while(1);
+    }
 
-            //We write per BUFFER_SIZE chars
-            if (buffercount == BUFFER_SIZE) {
-                //NMI Handler is at bytes 8-9-10-11, we overwrite this to point to bootloader function
-                
-                //Program the buffer into the flash memory
-                if (program_flash_boot(count+START_ADDRESS, buffer, BUFFER_SIZE) != 0) {
-                    write("Error!\r\n");   
-                    break;
-                }
-                
-                //Reset buffercount for next buffer
-                write("#");
-                buffercount = 0;
-                count += BUFFER_SIZE;
+    int count;
+    int ret;
+    int received = 0;
+    while (true) {
+        //printf("Receiving\r\n");
+        ret = sock.receive(&buffer[received], 16-received);
+        received = received + ret;
+        if (ret <= 0) {
+            if (received != 0) {
+                program_flash_boot(count+START_ADDRESS, buffer, 16);
             }
-        } else {
-            //No new data, increase timeout
-            timeout++;
+            break;
+        }
+        
+        printf("%d\r\n", ret);
+        if (received == 16) {
+            __disable_irq();
+            if (program_flash_boot(count+START_ADDRESS, buffer, 16) != 0) {
+                write("Error!\r\n");
+                while(1);
+            }
+            __enable_irq();
+            count += received;
+            received = 0;
             
-            //We have received no new data for a while, assume we are done
-            if (timeout > TIMEOUT) {
-                //If there is data left in the buffer, program it
-                if (buffercount != 0) {
-                    for (int i = buffercount; i<BUFFER_SIZE; i++) {
-                        buffer[i] = 0xFF;
-                    }
-                    program_flash_boot(count+START_ADDRESS, buffer, BUFFER_SIZE);
-                    count += BUFFER_SIZE;
-                }
-                break;          //We should be done programming :D
-            }
         }
+
+
     }
-    
-    printf("Done writing\r\n");
-    printf("Size = %d - %X\r\n", count, count);
+
+    sock.close();
+
+    eth.disconnect();
+    printf("Done\r\n");
 
     bootloader(count);
-    
+
 }
\ No newline at end of file