From 110c4034433fa5ea87e54b5b4f7139d2e539f4c3 Mon Sep 17 00:00:00 2001 From: logicog Date: Mon, 29 Sep 2025 15:10:42 +0200 Subject: [PATCH] Adding firmware update at beginning of boot if a new image is found in second half of flash --- rtlplayground.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/rtlplayground.c b/rtlplayground.c index 16816d3..fe06a3d 100644 --- a/rtlplayground.c +++ b/rtlplayground.c @@ -16,6 +16,9 @@ #include "uip/uip.h" #include "uip/uip_arp.h" +// Upload Firmware to 1M +#define FIRMWARE_UPLOAD_START 0x100000 + #define SYS_TICK_HZ 100 #define SERIAL_BAUD_RATE 115200 @@ -1694,6 +1697,35 @@ void bootloader(void) print_string(" Flash controller\n"); flash_init(0); + // Check update in progress and move blocks + flash_read_bulk(flash_buf, FIRMWARE_UPLOAD_START, 0x100); + if (flash_buf[0] == 0x00 && flash_buf[1] == 0x40) { + print_string("Update in progress, moving firmware to start of FLASH!\n"); + + __xdata uint32_t dest = 0x0; + __xdata uint32_t source = FIRMWARE_UPLOAD_START; + // A 512kByte = 4MBit Flash has 128*8=1024 512k blocks, we copy only 120 + for (__xdata uint16_t i=0; i < 960; i++) { + print_string("Writing block: "); + print_short(dest); + flash_read_bulk(flash_buf, source, 0x200); + write_char('\n'); + if (!(i & 0x7)) + flash_sector_erase(dest); + flash_write_bytes(dest, flash_buf, 0x200); + dest += 0x200; + source += 0x200; + } + print_string("Deleting uploaded flash image\n"); + dest = FIRMWARE_UPLOAD_START; + for (register uint8_t i=0; i < 120; i++) { + flash_sector_erase(dest); + dest += 0x1000; + } + print_string("Resetting now"); + reset_chip(); + } + // Reset NIC reg_bit_set(0x24, 2); do {