.

Dependencies:   mbed EthernetInterface mbed-rtos

Fork of Bootloader_K64F by Erik -

Revision:
7:4ab0430d06e3
Child:
8:00cefe0d59ed
diff -r 76c37fd3780a -r 4ab0430d06e3 Bootloader/bootloader.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Bootloader/bootloader.h	Sat Apr 23 15:10:04 2016 +0000
@@ -0,0 +1,87 @@
+#define START_ADDRESS   0x80000
+#define NUM_SECTORS     120
+#define SECTOR_SIZE     4096
+#define BUFFER_SIZE     16
+#define TIMEOUT            10000000
+
+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++;
+        }
+    }
+
+
+void write_flash(void) {
+    __disable_irq();
+    printf("Erasing flash!\r\n");
+    for (int i = 0; i < NUM_SECTORS; i++)
+        erase_sector_boot(START_ADDRESS + i * SECTOR_SIZE);
+    
+    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;
+
+            //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;
+            }
+        } else {
+            //No new data, increase timeout
+            timeout++;
+            
+            //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);
+
+    bootloader(count);
+    
+}
\ No newline at end of file