.

Dependencies:   mbed EthernetInterface mbed-rtos

Fork of Bootloader_K64F by Erik -

Committer:
Sissors
Date:
Sat Apr 23 18:24:07 2016 +0000
Revision:
8:00cefe0d59ed
Parent:
7:4ab0430d06e3
Basic functionality works over HTTP!

Who changed what in which revision?

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