Ryan Stephen
/
frdmk64_bootloader
This program hasn't been modified yet it breaks my FRDMK64 board
Embed:
(wiki syntax)
Show/hide line numbers
bootloader.cpp
00001 #include "mbed.h" 00002 #include "FreescaleIAP.h" 00003 00004 //Could be nicer, but for now just erase all preceding sectors 00005 #define NUM_SECTORS 57 00006 #define TIMEOUT 10000000 00007 #define BUFFER_SIZE 16 00008 00009 void setupserial(); 00010 void write(char *value); 00011 00012 __attribute__((section(".ARM.__at_0x50000"))) void bootloader(void) 00013 { 00014 SIM->SCGC6 |= SIM_SCGC6_FTF_MASK; //enable the Flash module clock 00015 00016 setupserial(); 00017 write("\n\n\rBootloader\r\n"); 00018 write("Continue? (y/n)"); 00019 00020 //Wait until data arrived, if it is 'y', continue 00021 while(!(UART0->S1 & UART_S1_RDRF_MASK)); 00022 if (UART0->D != 'y') { 00023 // NVIC_SystemReset(); 00024 return; 00025 } 00026 00027 //Erase all sectors we use for the user program 00028 write("Erasing sectors!\r\n"); 00029 for (int i = 0; i<NUM_SECTORS; i++) { 00030 erase_sector(SECTOR_SIZE * i); 00031 write("erased sector\r\n"); 00032 } 00033 00034 write("Done erasing, send file!\r\n"); 00035 00036 00037 char buffer[BUFFER_SIZE]; 00038 uint32_t count = 0; 00039 uint8_t buffercount = 0; 00040 uint32_t timeout = 0; 00041 00042 //Wait until data is sent 00043 while(!(UART0->S1 & UART_S1_RDRF_MASK)); 00044 00045 //Data receive loop 00046 while(1) { 00047 //Check if there is new data 00048 if (UART0->S1 & UART_S1_RDRF_MASK) { 00049 //Place data in buffer 00050 buffer[buffercount] = UART0->D; 00051 buffercount++; 00052 00053 //Reset timeout 00054 timeout = 0; 00055 00056 //We write per BUFFER_SIZE chars 00057 if (buffercount == BUFFER_SIZE) { 00058 //NMI Handler is at bytes 8-9-10-11, we overwrite this to point to bootloader function 00059 if (count == 0) { 00060 buffer[8] = 0x01; 00061 buffer[9] = 0x00; 00062 buffer[10] = 0x01; 00063 buffer[11] = 0x00; 00064 } 00065 00066 //Program the buffer into the flash memory 00067 if (program_flash(count, buffer, BUFFER_SIZE) != 0) { 00068 write("Error!\r\n"); 00069 break; 00070 } 00071 00072 //Reset buffercount for next buffer 00073 write("#"); 00074 buffercount = 0; 00075 count += BUFFER_SIZE; 00076 } 00077 } else { 00078 //No new data, increase timeout 00079 timeout++; 00080 00081 //We have received no new data for a while, assume we are done 00082 if (timeout > TIMEOUT) { 00083 //If there is data left in the buffer, program it 00084 if (buffercount != 0) { 00085 for (int i = buffercount; i<BUFFER_SIZE; i++) { 00086 buffer[i] = 0xFF; 00087 } 00088 program_flash(count, buffer, BUFFER_SIZE); 00089 } 00090 break; //We should be done programming :D 00091 } 00092 } 00093 } 00094 write("Done programming!\r\n"); 00095 NVIC_SystemReset(); 00096 00097 //Shouldn't arrive here 00098 while(1); 00099 } 00100 00101 __attribute__((section(".ARM.__at_0x50080"))) static void setupserial(void) 00102 { 00103 //Setup USBTX/USBRX pins (PTB16/PTB17) 00104 SIM->SCGC5 |= 1 << SIM_SCGC5_PORTB_SHIFT; 00105 PORTB->PCR[16] = (PORTB->PCR[16] & 0x700) | (3 << 8); 00106 PORTB->PCR[17] = (PORTB->PCR[17] & 0x700) | (3 << 8); 00107 00108 //Setup UART (ugly, copied resulting values from mbed serial setup) 00109 SIM->SCGC4 |= SIM_SCGC4_UART0_MASK; 00110 00111 UART0->BDH = 3; 00112 UART0->BDL = 13; 00113 UART0->C4 = 8; 00114 UART0->C2 = 12; //Enables UART 00115 00116 } 00117 00118 __attribute__((section(".ARM.__at_0x500A0"))) static void write(char *value) 00119 { 00120 int i = 0; 00121 //Loop through string and send everything 00122 while(*(value+i) != '\0') { 00123 while(!(UART0->S1 & UART_S1_TDRE_MASK)); 00124 UART0->D = *(value+i); 00125 i++; 00126 } 00127 } 00128
Generated on Wed Jul 13 2022 17:12:28 by 1.7.2