From a4d0b9a64071148572343f2af11bcfac0fc4c27f Mon Sep 17 00:00:00 2001 From: Jenna Fox Date: Sun, 23 Sep 2012 16:30:13 +1000 Subject: quick once over leaveBootloader and writeWordToPageBuffer --- firmware/main.c | 97 +++++++++++++-------------------------------------------- 1 file changed, 21 insertions(+), 76 deletions(-) diff --git a/firmware/main.c b/firmware/main.c index d28b743..f5a57ce 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -63,7 +63,7 @@ typedef union longConverter{ // outstanding events for the mainloop to deal with static uchar events = 0; -#define EVENT_ERASE_APPLICATION 1 +#define EVENT_PAGE_NEEDS_ERASE 1 #define EVENT_WRITE_PAGE 2 #define EVENT_EXIT_BOOTLOADER 4 @@ -112,7 +112,7 @@ static const uchar currentRequest = 0; #define fireEvent(event) events |= (event) #define isEvent(event) (events & (event)) -#define clearEvents() events = 0 +#define clearEvents() events = 0 /* ------------------------------------------------------------------------ */ @@ -140,42 +140,33 @@ static void writeFlashPage(void) { ); \ })) -static void writeWordToPageBuffer(uint16_t data) -{ +static void writeWordToPageBuffer(uint16_t data) { // first two interrupt vectors get replaced with a jump to the bootloader vector table - if(currentAddress == (RESET_VECTOR_OFFSET * 2) || currentAddress == (USBPLUS_VECTOR_OFFSET * 2)) + if (currentAddress == (RESET_VECTOR_OFFSET * 2) || currentAddress == (USBPLUS_VECTOR_OFFSET * 2)) { data = 0xC000 + (BOOTLOADER_ADDRESS/2) - 1; + } - // write 2's complement of checksum -#ifdef APPCHECKSUM - if(currentAddress == BOOTLOADER_ADDRESS - APPCHECKSUM_POSITION) - data = (uint8_t)(~checksum + 1); -#endif - - if(currentAddress == BOOTLOADER_ADDRESS - TINYVECTOR_RESET_OFFSET) + if (currentAddress == BOOTLOADER_ADDRESS - TINYVECTOR_RESET_OFFSET) { data = vectorTemp[0] + ((FLASHEND + 1) - BOOTLOADER_ADDRESS)/2 + 2 + RESET_VECTOR_OFFSET; - - if(currentAddress == BOOTLOADER_ADDRESS - TINYVECTOR_USBPLUS_OFFSET) + } + + if (currentAddress == BOOTLOADER_ADDRESS - TINYVECTOR_USBPLUS_OFFSET) { data = vectorTemp[1] + ((FLASHEND + 1) - BOOTLOADER_ADDRESS)/2 + 1 + USBPLUS_VECTOR_OFFSET; - -#ifdef APPCHECKSUM - // only calculate checksum when we are writing new data to flash (ignore when writing vectors after bootloader programming) - checksum += (uint8_t)(data/256) + (uint8_t)(data); -#endif - + } + // clear page buffer as a precaution before filling the buffer on the first page // TODO: maybe clear on the first byte of every page? - if(currentAddress == 0x0000) - __boot_page_fill_clear(); + if (currentAddress == 0x0000) __boot_page_fill_clear(); cli(); boot_page_fill(currentAddress, data); sei(); - + // only need to erase if there is data already in the page that doesn't match what we're programming - if(pgm_read_word(currentAddress) != data && pgm_read_word(currentAddress) != 0xFFFF) - needToErase = 1; - + if (pgm_read_word(currentAddress) != data && pgm_read_word(currentAddress) != 0xFFFF) { + fireEvent(EVENT_PAGE_NEEDS_ERASE); + } + currentAddress += 2; } @@ -212,41 +203,14 @@ static void eraseApplication(void) # endif #endif -/* ------------------------------------------------------------------------ */ -#ifdef APPCHECKSUM -// sum all bytes from 0x0000 through the previously saved checksum, should equal 0 for valid app -static inline int testForValidApplication(void) -{ - uint16_t i; - uint8_t checksum = 0; - for(i=0; i