.
Dependencies: mbed EthernetInterface mbed-rtos
Fork of Bootloader_K64F by
FreescaleIAP/bootloader.cpp@7:4ab0430d06e3, 2016-04-23 (annotated)
- Committer:
- Sissors
- Date:
- Sat Apr 23 15:10:04 2016 +0000
- Revision:
- 7:4ab0430d06e3
- Child:
- 8:00cefe0d59ed
Serial bootloader functional.
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:4ab0430d06e3 | 14 | __disable_irq(); |
Sissors | 7:4ab0430d06e3 | 15 | setupserial(); |
Sissors | 7:4ab0430d06e3 | 16 | write("\n\n\rBootloader\r\n"); |
Sissors | 7:4ab0430d06e3 | 17 | |
Sissors | 7:4ab0430d06e3 | 18 | //Erase all sectors we use for the user program |
Sissors | 7:4ab0430d06e3 | 19 | write("Erasing sectors!\r\n"); |
Sissors | 7:4ab0430d06e3 | 20 | for (int i = 0; i<NUM_SECTORS; i++) |
Sissors | 7:4ab0430d06e3 | 21 | erase_sector(SECTOR_SIZE * i); |
Sissors | 7:4ab0430d06e3 | 22 | |
Sissors | 7:4ab0430d06e3 | 23 | write("Done erasing, reading file!\r\n"); |
Sissors | 7:4ab0430d06e3 | 24 | |
Sissors | 7:4ab0430d06e3 | 25 | |
Sissors | 7:4ab0430d06e3 | 26 | char buffer[BUFFER_SIZE]; |
Sissors | 7:4ab0430d06e3 | 27 | char *source = (char*)0x80000; |
Sissors | 7:4ab0430d06e3 | 28 | |
Sissors | 7:4ab0430d06e3 | 29 | //Data receive loop |
Sissors | 7:4ab0430d06e3 | 30 | for(int count = 0; count<size; count+=BUFFER_SIZE) { |
Sissors | 7:4ab0430d06e3 | 31 | for (int i = 0; i<BUFFER_SIZE; i++) |
Sissors | 7:4ab0430d06e3 | 32 | buffer[i] = source[i+count]; |
Sissors | 7:4ab0430d06e3 | 33 | |
Sissors | 7:4ab0430d06e3 | 34 | if (program_flash(count, buffer, BUFFER_SIZE) != 0) { |
Sissors | 7:4ab0430d06e3 | 35 | write("Error!\r\n"); |
Sissors | 7:4ab0430d06e3 | 36 | break; |
Sissors | 7:4ab0430d06e3 | 37 | } |
Sissors | 7:4ab0430d06e3 | 38 | |
Sissors | 7:4ab0430d06e3 | 39 | //Reset buffercount for next buffer |
Sissors | 7:4ab0430d06e3 | 40 | write("%"); |
Sissors | 7:4ab0430d06e3 | 41 | } |
Sissors | 7:4ab0430d06e3 | 42 | |
Sissors | 7:4ab0430d06e3 | 43 | write("Done programming!\r\n"); |
Sissors | 7:4ab0430d06e3 | 44 | NVIC_SystemReset(); |
Sissors | 7:4ab0430d06e3 | 45 | |
Sissors | 7:4ab0430d06e3 | 46 | //Shouldn't arrive here |
Sissors | 7:4ab0430d06e3 | 47 | while(1); |
Sissors | 7:4ab0430d06e3 | 48 | } |
Sissors | 7:4ab0430d06e3 | 49 | |
Sissors | 7:4ab0430d06e3 | 50 | __attribute__((section(".ARM.__at_0x79120"))) static void setupserial(void) { |
Sissors | 7:4ab0430d06e3 | 51 | //Setup USBTX/USBRX pins (PTB16/PTB17) |
Sissors | 7:4ab0430d06e3 | 52 | SIM->SCGC5 |= 1 << SIM_SCGC5_PORTB_SHIFT; |
Sissors | 7:4ab0430d06e3 | 53 | PORTB->PCR[16] = (PORTB->PCR[16] & 0x700) | (3 << 8); |
Sissors | 7:4ab0430d06e3 | 54 | PORTB->PCR[17] = (PORTB->PCR[17] & 0x700) | (3 << 8); |
Sissors | 7:4ab0430d06e3 | 55 | |
Sissors | 7:4ab0430d06e3 | 56 | //Setup UART (ugly, copied resulting values from mbed serial setup) |
Sissors | 7:4ab0430d06e3 | 57 | SIM->SCGC4 |= SIM_SCGC4_UART0_MASK; |
Sissors | 7:4ab0430d06e3 | 58 | |
Sissors | 7:4ab0430d06e3 | 59 | UART0->BDH = 3; |
Sissors | 7:4ab0430d06e3 | 60 | UART0->BDL = 13; |
Sissors | 7:4ab0430d06e3 | 61 | UART0->C4 = 8; |
Sissors | 7:4ab0430d06e3 | 62 | UART0->C2 = 12; //Enables UART |
Sissors | 7:4ab0430d06e3 | 63 | |
Sissors | 7:4ab0430d06e3 | 64 | } |
Sissors | 7:4ab0430d06e3 | 65 | |
Sissors | 7:4ab0430d06e3 | 66 | __attribute__((section(".ARM.__at_0x79120"))) static void write(char *value) |
Sissors | 7:4ab0430d06e3 | 67 | { |
Sissors | 7:4ab0430d06e3 | 68 | int i = 0; |
Sissors | 7:4ab0430d06e3 | 69 | //Loop through string and send everything |
Sissors | 7:4ab0430d06e3 | 70 | while(*(value+i) != '\0') { |
Sissors | 7:4ab0430d06e3 | 71 | while(!(UART0->S1 & UART_S1_TDRE_MASK)); |
Sissors | 7:4ab0430d06e3 | 72 | UART0->D = *(value+i); |
Sissors | 7:4ab0430d06e3 | 73 | i++; |
Sissors | 7:4ab0430d06e3 | 74 | } |
Sissors | 7:4ab0430d06e3 | 75 | } |