From c76ecfb6151c615cc35774c404b956a16c14085c Mon Sep 17 00:00:00 2001 From: cpldcpu Date: Mon, 30 Dec 2013 13:20:35 +0100 Subject: commandline: do not write to empty pages --- commandline/Makefile | 2 +- commandline/builds/Windows/micronucleus.exe | Bin 26126 -> 26126 bytes commandline/library/micronucleus_lib.c | 39 ++++++++++++++++++---------- commandline/micronucleus.c | 5 ++-- commandline/micronucleus.exe | Bin 26126 -> 0 bytes 5 files changed, 29 insertions(+), 17 deletions(-) delete mode 100644 commandline/micronucleus.exe (limited to 'commandline') diff --git a/commandline/Makefile b/commandline/Makefile index 028f7aa..5d7a410 100644 --- a/commandline/Makefile +++ b/commandline/Makefile @@ -41,7 +41,7 @@ $(LWLIBS): $(EXAMPLES): $(addsuffix .o, $(LWLIBS)) @echo Building command line tool: $@... $(CC) $(CFLAGS) -o $@$(EXE_SUFFIX) $@.c $^ $(LIBS) - + clean: rm -f $(EXAMPLES)$(EXE_SUFFIX) *.o *.exe diff --git a/commandline/builds/Windows/micronucleus.exe b/commandline/builds/Windows/micronucleus.exe index 4118d0c..e94113b 100755 Binary files a/commandline/builds/Windows/micronucleus.exe and b/commandline/builds/Windows/micronucleus.exe differ diff --git a/commandline/library/micronucleus_lib.c b/commandline/library/micronucleus_lib.c index b9bceb2..ddcefdd 100644 --- a/commandline/library/micronucleus_lib.c +++ b/commandline/library/micronucleus_lib.c @@ -121,38 +121,49 @@ int micronucleus_writeFlash(micronucleus* deviceHandle, unsigned int program_siz unsigned int address; // overall flash memory address unsigned int page_address; // address within this page when copying buffer unsigned int res; - + unsigned int pagecontainsdata; + for (address = 0; address < deviceHandle->flash_size; address += deviceHandle->page_size) { // work around a bug in older bootloader versions if (deviceHandle->version.major == 1 && deviceHandle->version.minor <= 2 && address / deviceHandle->page_size == deviceHandle->pages - 1) { page_length = deviceHandle->flash_size % deviceHandle->page_size; } + + pagecontainsdata=0; // copy in bytes from user program for (page_address = 0; page_address < page_length; page_address += 1) { if (address + page_address > program_size) { page_buffer[page_address] = 0xFF; // pad out remainder with unprogrammed bytes } else { + pagecontainsdata=1; // page contains data and needs to be written page_buffer[page_address] = program[address + page_address]; // load from user program } } + // always write last page so bootloader can insert the tiny vector table + if ( address >= deviceHandle->flash_size - deviceHandle->page_size ) + pagecontainsdata = 1; + // ask microcontroller to write this page's data - res = usb_control_msg(deviceHandle->device, - USB_ENDPOINT_OUT| USB_TYPE_VENDOR | USB_RECIP_DEVICE, - 1, - page_length, address, - page_buffer, page_length, - MICRONUCLEUS_USB_TIMEOUT); - - // call progress update callback if that's a thing - if (prog) prog(((float) address) / ((float) deviceHandle->flash_size)); - - // give microcontroller enough time to write this page and come back online - delay(deviceHandle->write_sleep); + if (pagecontainsdata) { + res = usb_control_msg(deviceHandle->device, + USB_ENDPOINT_OUT| USB_TYPE_VENDOR | USB_RECIP_DEVICE, + 1, + page_length, address, + page_buffer, page_length, + MICRONUCLEUS_USB_TIMEOUT); + + if (res != page_length) return -1; + + // give microcontroller enough time to write this page and come back online + delay(deviceHandle->write_sleep); + } + + // call progress update callback if that's a thing + if (prog) prog(((float) address) / ((float) deviceHandle->flash_size)); - if (res != page_length) return -1; } // call progress update callback with completion status diff --git a/commandline/micronucleus.c b/commandline/micronucleus.c index c6d122a..b7d49ab 100644 --- a/commandline/micronucleus.c +++ b/commandline/micronucleus.c @@ -185,9 +185,10 @@ int main(int argc, char **argv) { // return EXIT_FAILURE; // } - printf("> Available space for user application: %d bytes\n", my_device->flash_size); + printf("> Device has firmware version %d.%d\n",my_device->version.major,my_device->version.minor); + printf("> Available space for user applications: %d bytes\n", my_device->flash_size); printf("> Suggested sleep time between sending pages: %ums\n", my_device->write_sleep); - printf("> Whole page count: %d\n", my_device->pages); + printf("> Whole page count: %d page size: %d\n", my_device->pages,my_device->page_size); printf("> Erase function sleep duration: %dms\n", my_device->erase_sleep); int startAddress = 1, endAddress = 0; diff --git a/commandline/micronucleus.exe b/commandline/micronucleus.exe deleted file mode 100644 index c6252cb..0000000 Binary files a/commandline/micronucleus.exe and /dev/null differ -- cgit v1.2.3