Mikael Hakansson / Mbed 2 deprecated microServiceBus_bootloader

Dependencies:   mbed EthernetInterface mbed-rtos

Fork of FOTA_K64F by Erik -

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bootloader.cpp Source File

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        120
00006 #define TIMEOUT            10000000
00007 #define BUFFER_SIZE        16
00008 
00009 void setupserial();
00010 void write(char *value);
00011 
00012 __attribute__((section(".ARM.__at_0x79120"))) void bootloader(int size)
00013 {
00014     SysTick->CTRL = 0;
00015     __disable_irq();
00016 
00017     setupserial();
00018     write("\n\n\rBootloader\r\n");
00019     
00020     //Erase all sectors we use for the user program
00021     write("Erasing sectors!\r\n");
00022         
00023     for (int i = 0; i<NUM_SECTORS; i++) {
00024         write("*");
00025         erase_sector(SECTOR_SIZE * i);
00026     }
00027 
00028     write("\r\nDone erasing, reading file!\r\n");
00029 
00030     
00031     char buffer[BUFFER_SIZE];
00032     char *source = (char*)0x80000;
00033     
00034    write("Flashing device\r\n");
00035     
00036     //Data receive loop
00037     for(int count = 0; count<size; count+=BUFFER_SIZE) {
00038         for (int i = 0; i<BUFFER_SIZE; i++)
00039             buffer[i] = source[i+count];
00040         
00041         if (program_flash(count, buffer, BUFFER_SIZE) != 0) {
00042              write("Error!\r\n");   
00043              break;
00044         }
00045         //write(".");
00046                
00047         //Reset buffercount for next buffer
00048         
00049     }             
00050    
00051     write("\r\nDone programming!\r\n");
00052     NVIC_SystemReset();
00053     
00054     //Shouldn't arrive here
00055     while(1);
00056 }
00057 
00058 __attribute__((section(".ARM.__at_0x79120"))) static void setupserial(void) {
00059         //Setup USBTX/USBRX pins (PTB16/PTB17)
00060         SIM->SCGC5 |= 1 << SIM_SCGC5_PORTB_SHIFT;
00061         PORTB->PCR[16] = (PORTB->PCR[16] & 0x700) | (3 << 8);
00062         PORTB->PCR[17] = (PORTB->PCR[17] & 0x700) | (3 << 8);
00063 
00064         //Setup UART (ugly, copied resulting values from mbed serial setup)
00065         SIM->SCGC4 |= SIM_SCGC4_UART0_MASK;
00066 
00067         UART0->BDH = 3;
00068         UART0->BDL = 13;
00069         UART0->C4 = 8;
00070         UART0->C2 = 12;  //Enables UART
00071 
00072     }
00073 
00074 __attribute__((section(".ARM.__at_0x79120"))) static void write(char *value)
00075 {
00076         int i = 0;
00077         //Loop through string and send everything
00078         while(*(value+i) != '\0') {
00079             while(!(UART0->S1 & UART_S1_TDRE_MASK));
00080             UART0->D = *(value+i);
00081             i++;
00082         }
00083     }