.

Dependencies:   mbed EthernetInterface mbed-rtos

Fork of Bootloader_K64F by Erik -

Committer:
Sissors
Date:
Sun Apr 24 12:46:37 2016 +0000
Revision:
10:fb5121bcc468
I just committed already

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 10:fb5121bcc468 1 #include "mbed.h"
Sissors 10:fb5121bcc468 2 #include "FreescaleIAP.h"
Sissors 10:fb5121bcc468 3
Sissors 10:fb5121bcc468 4 //Could be nicer, but for now just erase all preceding sectors
Sissors 10:fb5121bcc468 5 #define NUM_SECTORS 120
Sissors 10:fb5121bcc468 6 #define TIMEOUT 10000000
Sissors 10:fb5121bcc468 7 #define BUFFER_SIZE 16
Sissors 10:fb5121bcc468 8
Sissors 10:fb5121bcc468 9 void setupserial();
Sissors 10:fb5121bcc468 10 void write(char *value);
Sissors 10:fb5121bcc468 11
Sissors 10:fb5121bcc468 12 __attribute__((section(".ARM.__at_0x79120"))) void bootloader(int size)
Sissors 10:fb5121bcc468 13 {
Sissors 10:fb5121bcc468 14 SysTick->CTRL = 0;
Sissors 10:fb5121bcc468 15 __disable_irq();
Sissors 10:fb5121bcc468 16
Sissors 10:fb5121bcc468 17 setupserial();
Sissors 10:fb5121bcc468 18 write("\n\n\rBootloader\r\n");
Sissors 10:fb5121bcc468 19
Sissors 10:fb5121bcc468 20 //Erase all sectors we use for the user program
Sissors 10:fb5121bcc468 21 write("Erasing sectors!\r\n");
Sissors 10:fb5121bcc468 22
Sissors 10:fb5121bcc468 23 for (int i = 0; i<NUM_SECTORS; i++) {
Sissors 10:fb5121bcc468 24 write("*");
Sissors 10:fb5121bcc468 25 erase_sector(SECTOR_SIZE * i);
Sissors 10:fb5121bcc468 26 }
Sissors 10:fb5121bcc468 27
Sissors 10:fb5121bcc468 28 write("Done erasing, reading file!\r\n");
Sissors 10:fb5121bcc468 29
Sissors 10:fb5121bcc468 30
Sissors 10:fb5121bcc468 31 char buffer[BUFFER_SIZE];
Sissors 10:fb5121bcc468 32 char *source = (char*)0x80000;
Sissors 10:fb5121bcc468 33
Sissors 10:fb5121bcc468 34 //Data receive loop
Sissors 10:fb5121bcc468 35 for(int count = 0; count<size; count+=BUFFER_SIZE) {
Sissors 10:fb5121bcc468 36 for (int i = 0; i<BUFFER_SIZE; i++)
Sissors 10:fb5121bcc468 37 buffer[i] = source[i+count];
Sissors 10:fb5121bcc468 38
Sissors 10:fb5121bcc468 39 if (program_flash(count, buffer, BUFFER_SIZE) != 0) {
Sissors 10:fb5121bcc468 40 write("Error!\r\n");
Sissors 10:fb5121bcc468 41 break;
Sissors 10:fb5121bcc468 42 }
Sissors 10:fb5121bcc468 43
Sissors 10:fb5121bcc468 44 //Reset buffercount for next buffer
Sissors 10:fb5121bcc468 45 write("%");
Sissors 10:fb5121bcc468 46 }
Sissors 10:fb5121bcc468 47
Sissors 10:fb5121bcc468 48 write("Done programming!\r\n");
Sissors 10:fb5121bcc468 49 NVIC_SystemReset();
Sissors 10:fb5121bcc468 50
Sissors 10:fb5121bcc468 51 //Shouldn't arrive here
Sissors 10:fb5121bcc468 52 while(1);
Sissors 10:fb5121bcc468 53 }
Sissors 10:fb5121bcc468 54
Sissors 10:fb5121bcc468 55 __attribute__((section(".ARM.__at_0x79120"))) static void setupserial(void) {
Sissors 10:fb5121bcc468 56 //Setup USBTX/USBRX pins (PTB16/PTB17)
Sissors 10:fb5121bcc468 57 SIM->SCGC5 |= 1 << SIM_SCGC5_PORTB_SHIFT;
Sissors 10:fb5121bcc468 58 PORTB->PCR[16] = (PORTB->PCR[16] & 0x700) | (3 << 8);
Sissors 10:fb5121bcc468 59 PORTB->PCR[17] = (PORTB->PCR[17] & 0x700) | (3 << 8);
Sissors 10:fb5121bcc468 60
Sissors 10:fb5121bcc468 61 //Setup UART (ugly, copied resulting values from mbed serial setup)
Sissors 10:fb5121bcc468 62 SIM->SCGC4 |= SIM_SCGC4_UART0_MASK;
Sissors 10:fb5121bcc468 63
Sissors 10:fb5121bcc468 64 UART0->BDH = 3;
Sissors 10:fb5121bcc468 65 UART0->BDL = 13;
Sissors 10:fb5121bcc468 66 UART0->C4 = 8;
Sissors 10:fb5121bcc468 67 UART0->C2 = 12; //Enables UART
Sissors 10:fb5121bcc468 68
Sissors 10:fb5121bcc468 69 }
Sissors 10:fb5121bcc468 70
Sissors 10:fb5121bcc468 71 __attribute__((section(".ARM.__at_0x79120"))) static void write(char *value)
Sissors 10:fb5121bcc468 72 {
Sissors 10:fb5121bcc468 73 int i = 0;
Sissors 10:fb5121bcc468 74 //Loop through string and send everything
Sissors 10:fb5121bcc468 75 while(*(value+i) != '\0') {
Sissors 10:fb5121bcc468 76 while(!(UART0->S1 & UART_S1_TDRE_MASK));
Sissors 10:fb5121bcc468 77 UART0->D = *(value+i);
Sissors 10:fb5121bcc468 78 i++;
Sissors 10:fb5121bcc468 79 }
Sissors 10:fb5121bcc468 80 }