This program hasn't been modified yet it breaks my FRDMK64 board

Dependencies:   mbed

Revision:
1:03c972a91ee2
Parent:
0:b963b1cb4a1a
--- a/bootloader.cpp	Thu Dec 15 08:37:10 2016 +0000
+++ b/bootloader.cpp	Fri Jan 06 11:11:18 2017 +0000
@@ -2,40 +2,46 @@
 #include "FreescaleIAP.h"
 
 //Could be nicer, but for now just erase all preceding sectors
-#define NUM_SECTORS        15
+#define NUM_SECTORS        57
 #define TIMEOUT            10000000
 #define BUFFER_SIZE        16
 
 void setupserial();
 void write(char *value);
 
-__attribute__((section(".ARM.__at_0x10000"))) void bootloader(void)
+__attribute__((section(".ARM.__at_0x50000"))) void bootloader(void)
 {
+    SIM->SCGC6 |= SIM_SCGC6_FTF_MASK;                               //enable the Flash module clock
+
     setupserial();
     write("\n\n\rBootloader\r\n");
     write("Continue? (y/n)");
-    
+
     //Wait until data arrived, if it is 'y', continue
     while(!(UART0->S1 & UART_S1_RDRF_MASK));
-    if (UART0->D != 'y')
+    if (UART0->D != 'y') {
+       // NVIC_SystemReset();
         return;
-    
+    }
+
     //Erase all sectors we use for the user program
     write("Erasing sectors!\r\n");
-    for (int i = 0; i<NUM_SECTORS; i++)
+    for (int i = 0; i<NUM_SECTORS; i++) {
         erase_sector(SECTOR_SIZE * i);
+        write("erased sector\r\n");
+    }
 
     write("Done erasing, send file!\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
@@ -43,7 +49,7 @@
             //Place data in buffer
             buffer[buffercount] = UART0->D;
             buffercount++;
-            
+
             //Reset timeout
             timeout = 0;
 
@@ -56,13 +62,13 @@
                     buffer[10] = 0x01;
                     buffer[11] = 0x00;
                 }
-                
+
                 //Program the buffer into the flash memory
                 if (program_flash(count, buffer, BUFFER_SIZE) != 0) {
-                    write("Error!\r\n");   
+                    write("Error!\r\n");
                     break;
                 }
-                
+
                 //Reset buffercount for next buffer
                 write("#");
                 buffercount = 0;
@@ -71,7 +77,7 @@
         } 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
@@ -87,34 +93,36 @@
     }
     write("Done programming!\r\n");
     NVIC_SystemReset();
-    
+
     //Shouldn't arrive here
     while(1);
 }
 
-__attribute__((section(".ARM.__at_0x10080"))) static void setupserial(void) {
-        //Setup USBTX/USBRX pins (PTB16/PTB17)
-        SIM->SCGC5 |= 1 << SIM_SCGC5_PORTB_SHIFT;
-        PORTB->PCR[16] = (PORTB->PCR[16] & 0x700) | (3 << 8);
-        PORTB->PCR[17] = (PORTB->PCR[17] & 0x700) | (3 << 8);
+__attribute__((section(".ARM.__at_0x50080"))) static void setupserial(void)
+{
+    //Setup USBTX/USBRX pins (PTB16/PTB17)
+    SIM->SCGC5 |= 1 << SIM_SCGC5_PORTB_SHIFT;
+    PORTB->PCR[16] = (PORTB->PCR[16] & 0x700) | (3 << 8);
+    PORTB->PCR[17] = (PORTB->PCR[17] & 0x700) | (3 << 8);
 
-        //Setup UART (ugly, copied resulting values from mbed serial setup)
-        SIM->SCGC4 |= SIM_SCGC4_UART0_MASK;
+    //Setup UART (ugly, copied resulting values from mbed serial setup)
+    SIM->SCGC4 |= SIM_SCGC4_UART0_MASK;
 
-        UART0->BDH = 3;
-        UART0->BDL = 13;
-        UART0->C4 = 8;
-        UART0->C2 = 12;  //Enables UART
+    UART0->BDH = 3;
+    UART0->BDL = 13;
+    UART0->C4 = 8;
+    UART0->C2 = 12;  //Enables UART
 
-    }
+}
 
-__attribute__((section(".ARM.__at_0x100A0"))) static void write(char *value)
+__attribute__((section(".ARM.__at_0x500A0"))) 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++;
     }
+}
+