.
Dependencies: mbed EthernetInterface mbed-rtos
Fork of Bootloader_K64F by
Bootloader/bootloader.h@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 | #define START_ADDRESS 0x80000 |
Sissors | 7:4ab0430d06e3 | 2 | #define NUM_SECTORS 120 |
Sissors | 7:4ab0430d06e3 | 3 | #define SECTOR_SIZE 4096 |
Sissors | 7:4ab0430d06e3 | 4 | #define BUFFER_SIZE 16 |
Sissors | 7:4ab0430d06e3 | 5 | #define TIMEOUT 10000000 |
Sissors | 7:4ab0430d06e3 | 6 | |
Sissors | 7:4ab0430d06e3 | 7 | int *(*program_flash_boot)(int, char*, unsigned int) = (int *(*)(int, char*, unsigned int))0x795C9; |
Sissors | 7:4ab0430d06e3 | 8 | int *(*erase_sector_boot)(int) = (int *(*)(int))0x79465; |
Sissors | 7:4ab0430d06e3 | 9 | void *(*bootloader)(int) = (void *(*)(int))0x79121; |
Sissors | 7:4ab0430d06e3 | 10 | |
Sissors | 7:4ab0430d06e3 | 11 | static void write(char *value) |
Sissors | 7:4ab0430d06e3 | 12 | { |
Sissors | 7:4ab0430d06e3 | 13 | int i = 0; |
Sissors | 7:4ab0430d06e3 | 14 | //Loop through string and send everything |
Sissors | 7:4ab0430d06e3 | 15 | while(*(value+i) != '\0') { |
Sissors | 7:4ab0430d06e3 | 16 | while(!(UART0->S1 & UART_S1_TDRE_MASK)); |
Sissors | 7:4ab0430d06e3 | 17 | UART0->D = *(value+i); |
Sissors | 7:4ab0430d06e3 | 18 | i++; |
Sissors | 7:4ab0430d06e3 | 19 | } |
Sissors | 7:4ab0430d06e3 | 20 | } |
Sissors | 7:4ab0430d06e3 | 21 | |
Sissors | 7:4ab0430d06e3 | 22 | |
Sissors | 7:4ab0430d06e3 | 23 | void write_flash(void) { |
Sissors | 7:4ab0430d06e3 | 24 | __disable_irq(); |
Sissors | 7:4ab0430d06e3 | 25 | printf("Erasing flash!\r\n"); |
Sissors | 7:4ab0430d06e3 | 26 | for (int i = 0; i < NUM_SECTORS; i++) |
Sissors | 7:4ab0430d06e3 | 27 | erase_sector_boot(START_ADDRESS + i * SECTOR_SIZE); |
Sissors | 7:4ab0430d06e3 | 28 | |
Sissors | 7:4ab0430d06e3 | 29 | char buffer[BUFFER_SIZE]; |
Sissors | 7:4ab0430d06e3 | 30 | uint32_t count = 0; |
Sissors | 7:4ab0430d06e3 | 31 | uint8_t buffercount = 0; |
Sissors | 7:4ab0430d06e3 | 32 | uint32_t timeout = 0; |
Sissors | 7:4ab0430d06e3 | 33 | |
Sissors | 7:4ab0430d06e3 | 34 | //Wait until data is sent |
Sissors | 7:4ab0430d06e3 | 35 | while(!(UART0->S1 & UART_S1_RDRF_MASK)); |
Sissors | 7:4ab0430d06e3 | 36 | |
Sissors | 7:4ab0430d06e3 | 37 | //Data receive loop |
Sissors | 7:4ab0430d06e3 | 38 | while(1) { |
Sissors | 7:4ab0430d06e3 | 39 | //Check if there is new data |
Sissors | 7:4ab0430d06e3 | 40 | if (UART0->S1 & UART_S1_RDRF_MASK) { |
Sissors | 7:4ab0430d06e3 | 41 | //Place data in buffer |
Sissors | 7:4ab0430d06e3 | 42 | buffer[buffercount] = UART0->D; |
Sissors | 7:4ab0430d06e3 | 43 | buffercount++; |
Sissors | 7:4ab0430d06e3 | 44 | |
Sissors | 7:4ab0430d06e3 | 45 | //Reset timeout |
Sissors | 7:4ab0430d06e3 | 46 | timeout = 0; |
Sissors | 7:4ab0430d06e3 | 47 | |
Sissors | 7:4ab0430d06e3 | 48 | //We write per BUFFER_SIZE chars |
Sissors | 7:4ab0430d06e3 | 49 | if (buffercount == BUFFER_SIZE) { |
Sissors | 7:4ab0430d06e3 | 50 | //NMI Handler is at bytes 8-9-10-11, we overwrite this to point to bootloader function |
Sissors | 7:4ab0430d06e3 | 51 | |
Sissors | 7:4ab0430d06e3 | 52 | //Program the buffer into the flash memory |
Sissors | 7:4ab0430d06e3 | 53 | if (program_flash_boot(count+START_ADDRESS, buffer, BUFFER_SIZE) != 0) { |
Sissors | 7:4ab0430d06e3 | 54 | write("Error!\r\n"); |
Sissors | 7:4ab0430d06e3 | 55 | break; |
Sissors | 7:4ab0430d06e3 | 56 | } |
Sissors | 7:4ab0430d06e3 | 57 | |
Sissors | 7:4ab0430d06e3 | 58 | //Reset buffercount for next buffer |
Sissors | 7:4ab0430d06e3 | 59 | write("#"); |
Sissors | 7:4ab0430d06e3 | 60 | buffercount = 0; |
Sissors | 7:4ab0430d06e3 | 61 | count += BUFFER_SIZE; |
Sissors | 7:4ab0430d06e3 | 62 | } |
Sissors | 7:4ab0430d06e3 | 63 | } else { |
Sissors | 7:4ab0430d06e3 | 64 | //No new data, increase timeout |
Sissors | 7:4ab0430d06e3 | 65 | timeout++; |
Sissors | 7:4ab0430d06e3 | 66 | |
Sissors | 7:4ab0430d06e3 | 67 | //We have received no new data for a while, assume we are done |
Sissors | 7:4ab0430d06e3 | 68 | if (timeout > TIMEOUT) { |
Sissors | 7:4ab0430d06e3 | 69 | //If there is data left in the buffer, program it |
Sissors | 7:4ab0430d06e3 | 70 | if (buffercount != 0) { |
Sissors | 7:4ab0430d06e3 | 71 | for (int i = buffercount; i<BUFFER_SIZE; i++) { |
Sissors | 7:4ab0430d06e3 | 72 | buffer[i] = 0xFF; |
Sissors | 7:4ab0430d06e3 | 73 | } |
Sissors | 7:4ab0430d06e3 | 74 | program_flash_boot(count+START_ADDRESS, buffer, BUFFER_SIZE); |
Sissors | 7:4ab0430d06e3 | 75 | count += BUFFER_SIZE; |
Sissors | 7:4ab0430d06e3 | 76 | } |
Sissors | 7:4ab0430d06e3 | 77 | break; //We should be done programming :D |
Sissors | 7:4ab0430d06e3 | 78 | } |
Sissors | 7:4ab0430d06e3 | 79 | } |
Sissors | 7:4ab0430d06e3 | 80 | } |
Sissors | 7:4ab0430d06e3 | 81 | |
Sissors | 7:4ab0430d06e3 | 82 | printf("Done writing\r\n"); |
Sissors | 7:4ab0430d06e3 | 83 | printf("Size = %d - %X\r\n", count, count); |
Sissors | 7:4ab0430d06e3 | 84 | |
Sissors | 7:4ab0430d06e3 | 85 | bootloader(count); |
Sissors | 7:4ab0430d06e3 | 86 | |
Sissors | 7:4ab0430d06e3 | 87 | } |