X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/63e80bea0827d28a5a64bd359b0e702babfc1be3..57071dea2222c74cfcb28d8ed9c6a9ab61bcb631:/Bootloaders/Printer/BootloaderPrinter.c diff --git a/Bootloaders/Printer/BootloaderPrinter.c b/Bootloaders/Printer/BootloaderPrinter.c index ef7f03ffc..5d4a89bd3 100644 --- a/Bootloaders/Printer/BootloaderPrinter.c +++ b/Bootloaders/Printer/BootloaderPrinter.c @@ -59,6 +59,8 @@ struct uint8_t Checksum; /** Starting address of the last addressed FLASH page. */ uint32_t PageStartAddress; + /** Current 32-bit byte extended base address in FLASH being targeted. */ + uint32_t CurrBaseAddress; /** Current 32-bit byte address in FLASH being targeted. */ uint32_t CurrAddress; } HEXParser = @@ -117,7 +119,7 @@ static void ParseIntelHEXByte(const char ReadCharacter) if ((HEXParser.ParserState == HEX_PARSE_STATE_WAIT_LINE) || (ReadCharacter == ':')) { HEXParser.Checksum = 0; - HEXParser.CurrAddress &= ~0xFFFF; + HEXParser.CurrAddress = HEXParser.CurrBaseAddress; HEXParser.ParserState = HEX_PARSE_STATE_WAIT_LINE; HEXParser.ReadMSB = false; @@ -154,12 +156,12 @@ static void ParseIntelHEXByte(const char ReadCharacter) break; case HEX_PARSE_STATE_ADDRESS_HIGH: - HEXParser.CurrAddress |= ((uint16_t)HEXParser.Data << 8); + HEXParser.CurrAddress += ((uint16_t)HEXParser.Data << 8); HEXParser.ParserState = HEX_PARSE_STATE_ADDRESS_LOW; break; case HEX_PARSE_STATE_ADDRESS_LOW: - HEXParser.CurrAddress |= HEXParser.Data; + HEXParser.CurrAddress += HEXParser.Data; HEXParser.ParserState = HEX_PARSE_STATE_RECORD_TYPE; break; @@ -172,6 +174,14 @@ static void ParseIntelHEXByte(const char ReadCharacter) /* Track the number of read data bytes in the record */ HEXParser.DataRem--; + /* Protect the bootloader against being written to */ + if (HEXParser.CurrAddress >= BOOT_START_ADDR) + { + HEXParser.ParserState = HEX_PARSE_STATE_WAIT_LINE; + PageDirty = false; + return; + } + /* Wait for a machine word (two bytes) of data to be read */ if (HEXParser.DataRem & 0x01) { @@ -179,6 +189,9 @@ static void ParseIntelHEXByte(const char ReadCharacter) break; } + /* Convert the last two received data bytes into a 16-bit word */ + uint16_t NewDataWord = ((uint16_t)HEXParser.Data << 8) | HEXParser.PrevData; + switch (HEXParser.RecordType) { case HEX_RECORD_TYPE_Data: @@ -194,7 +207,7 @@ static void ParseIntelHEXByte(const char ReadCharacter) } /* Fill the FLASH memory buffer with the new word of data */ - boot_page_fill(HEXParser.CurrAddress, ((uint16_t)HEXParser.Data << 8) | HEXParser.PrevData); + boot_page_fill(HEXParser.CurrAddress, NewDataWord); HEXParser.CurrAddress += 2; /* Flush the FLASH page to physical memory if we are crossing a page boundary */ @@ -210,9 +223,14 @@ static void ParseIntelHEXByte(const char ReadCharacter) } break; + case HEX_RECORD_TYPE_ExtendedSegmentAddress: + /* Extended address data - store the upper 12-bits of the new address */ + HEXParser.CurrBaseAddress = ((uint32_t)NewDataWord << 4); + break; + case HEX_RECORD_TYPE_ExtendedLinearAddress: /* Extended address data - store the upper 16-bits of the new address */ - HEXParser.CurrAddress |= (uint32_t)HEXParser.Data << (HEXParser.DataRem ? 24 : 16); + HEXParser.CurrBaseAddress = ((uint32_t)NewDataWord << 16); break; } @@ -297,6 +315,16 @@ void SetupHardware(void) /* Hardware Initialization */ LEDs_Init(); USB_Init(); + + /* Bootloader active LED toggle timer initialization */ + TIMSK1 = (1 << TOIE1); + TCCR1B = ((1 << CS11) | (1 << CS10)); +} + +/** ISR to periodically toggle the LEDs on the board to indicate that the bootloader is active. */ +ISR(TIMER1_OVF_vect, ISR_BLOCK) +{ + LEDs_ToggleLEDs(LEDS_LED1 | LEDS_LED2); } /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs. */