diff options
author | skullydazed <skullydazed@users.noreply.github.com> | 2019-07-07 10:59:58 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-07 10:59:58 -0700 |
commit | 04a7f66421ac141a8927491fa61e4f8e425accad (patch) | |
tree | 027bafce1cd12c31e84a758bea24d5fdb78226cb /Bootloaders | |
parent | d0fd80affba97b37eb00c650e87cb5982a4f260e (diff) | |
parent | 57edb9b0976fad82bfd0384b16c6c3269e0d19ce (diff) | |
download | lufa-04a7f66421ac141a8927491fa61e4f8e425accad.tar.gz lufa-04a7f66421ac141a8927491fa61e4f8e425accad.tar.bz2 lufa-04a7f66421ac141a8927491fa61e4f8e425accad.zip |
Merge pull request #2 from drashna/master
Update LUFA repo from upstream
Diffstat (limited to 'Bootloaders')
51 files changed, 364 insertions, 248 deletions
diff --git a/Bootloaders/CDC/BootloaderAPI.c b/Bootloaders/CDC/BootloaderAPI.c index 2be156808..21d13bbe3 100644 --- a/Bootloaders/CDC/BootloaderAPI.c +++ b/Bootloaders/CDC/BootloaderAPI.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -35,18 +35,39 @@ #include "BootloaderAPI.h" +static bool IsPageAddressValid(const uint32_t Address) +{ + /* Determine if the given page address is correctly aligned to the + start of a flash page. */ + bool PageAddressIsAligned = !(Address & (SPM_PAGESIZE - 1)); + + return (Address < BOOT_START_ADDR) && PageAddressIsAligned; +} + void BootloaderAPI_ErasePage(const uint32_t Address) { - boot_page_erase_safe(Address); - boot_spm_busy_wait(); - boot_rww_enable(); + if (! IsPageAddressValid(Address)) + return; + + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + boot_page_erase_safe(Address); + boot_spm_busy_wait(); + boot_rww_enable(); + } } void BootloaderAPI_WritePage(const uint32_t Address) { - boot_page_write_safe(Address); - boot_spm_busy_wait(); - boot_rww_enable(); + if (! IsPageAddressValid(Address)) + return; + + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + boot_page_write_safe(Address); + boot_spm_busy_wait(); + boot_rww_enable(); + } } void BootloaderAPI_FillWord(const uint32_t Address, const uint16_t Word) @@ -71,5 +92,8 @@ uint8_t BootloaderAPI_ReadLock(void) void BootloaderAPI_WriteLock(const uint8_t LockBits) { - boot_lock_bits_set_safe(LockBits); + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + boot_lock_bits_set_safe(LockBits); + } } diff --git a/Bootloaders/CDC/BootloaderAPI.h b/Bootloaders/CDC/BootloaderAPI.h index 5169bbc3c..d00645b5a 100644 --- a/Bootloaders/CDC/BootloaderAPI.h +++ b/Bootloaders/CDC/BootloaderAPI.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -39,6 +39,7 @@ /* Includes: */ #include <avr/io.h> #include <avr/boot.h> + #include <util/atomic.h> #include <stdbool.h> #include <LUFA/Common/Common.h> diff --git a/Bootloaders/CDC/BootloaderAPITable.S b/Bootloaders/CDC/BootloaderAPITable.S index 2c60f84e8..e07ab4c45 100644 --- a/Bootloaders/CDC/BootloaderAPITable.S +++ b/Bootloaders/CDC/BootloaderAPITable.S @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/CDC/BootloaderCDC.c b/Bootloaders/CDC/BootloaderCDC.c index aa17bc15b..8f844565f 100644 --- a/Bootloaders/CDC/BootloaderCDC.c +++ b/Bootloaders/CDC/BootloaderCDC.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -97,7 +97,7 @@ void Application_Jump_Check(void) JTAG_ENABLE(); #else /* Check if the device's BOOTRST fuse is set */ - if (boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS) & FUSE_BOOTRST) + if (!(BootloaderAPI_ReadFuse(GET_HIGH_FUSE_BITS) & ~FUSE_BOOTRST)) { /* If the reset source was not an external reset or the key is correct, clear it and jump to the application */ if (!(MCUSR & (1 << EXTRF)) || (MagicBootKey == MAGIC_BOOT_KEY)) @@ -297,9 +297,6 @@ static void ReadWriteMemoryBlock(const uint8_t Command) /* Check if command is to read a memory block */ if (Command == AVR109_COMMAND_BlockRead) { - /* Re-enable RWW section */ - boot_rww_enable(); - while (BlockSize--) { if (MemoryType == MEMORY_TYPE_FLASH) @@ -332,10 +329,7 @@ static void ReadWriteMemoryBlock(const uint8_t Command) uint32_t PageStartAddress = CurrAddress; if (MemoryType == MEMORY_TYPE_FLASH) - { - boot_page_erase(PageStartAddress); - boot_spm_busy_wait(); - } + BootloaderAPI_ErasePage(PageStartAddress); while (BlockSize--) { @@ -345,7 +339,7 @@ static void ReadWriteMemoryBlock(const uint8_t Command) if (HighByte) { /* Write the next FLASH word to the current FLASH page */ - boot_page_fill(CurrAddress, ((FetchNextCommandByte() << 8) | LowByte)); + BootloaderAPI_FillWord(CurrAddress, ((FetchNextCommandByte() << 8) | LowByte)); /* Increment the address counter after use */ CurrAddress += 2; @@ -371,10 +365,7 @@ static void ReadWriteMemoryBlock(const uint8_t Command) if (MemoryType == MEMORY_TYPE_FLASH) { /* Commit the flash page to memory */ - boot_page_write(PageStartAddress); - - /* Wait until write operation has completed */ - boot_spm_busy_wait(); + BootloaderAPI_WritePage(PageStartAddress); } /* Send response byte back to the host */ @@ -516,12 +507,7 @@ static void CDC_Task(void) { /* Clear the application section of flash */ for (uint32_t CurrFlashAddress = 0; CurrFlashAddress < (uint32_t)BOOT_START_ADDR; CurrFlashAddress += SPM_PAGESIZE) - { - boot_page_erase(CurrFlashAddress); - boot_spm_busy_wait(); - boot_page_write(CurrFlashAddress); - boot_spm_busy_wait(); - } + BootloaderAPI_ErasePage(CurrFlashAddress); /* Send confirmation byte back to the host */ WriteNextResponseByte('\r'); @@ -530,7 +516,7 @@ static void CDC_Task(void) else if (Command == AVR109_COMMAND_WriteLockbits) { /* Set the lock bits to those given by the host */ - boot_lock_bits_set(FetchNextCommandByte()); + BootloaderAPI_WriteLock(FetchNextCommandByte()); /* Send confirmation byte back to the host */ WriteNextResponseByte('\r'); @@ -538,19 +524,19 @@ static void CDC_Task(void) #endif else if (Command == AVR109_COMMAND_ReadLockbits) { - WriteNextResponseByte(boot_lock_fuse_bits_get(GET_LOCK_BITS)); + WriteNextResponseByte(BootloaderAPI_ReadLock()); } else if (Command == AVR109_COMMAND_ReadLowFuses) { - WriteNextResponseByte(boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS)); + WriteNextResponseByte(BootloaderAPI_ReadFuse(GET_LOW_FUSE_BITS)); } else if (Command == AVR109_COMMAND_ReadHighFuses) { - WriteNextResponseByte(boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS)); + WriteNextResponseByte(BootloaderAPI_ReadFuse(GET_HIGH_FUSE_BITS)); } else if (Command == AVR109_COMMAND_ReadExtendedFuses) { - WriteNextResponseByte(boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS)); + WriteNextResponseByte(BootloaderAPI_ReadFuse(GET_EXTENDED_FUSE_BITS)); } #if !defined(NO_BLOCK_SUPPORT) else if (Command == AVR109_COMMAND_GetBlockWriteSupport) @@ -571,7 +557,7 @@ static void CDC_Task(void) else if (Command == AVR109_COMMAND_FillFlashPageWordHigh) { /* Write the high byte to the current flash page */ - boot_page_fill(CurrAddress, FetchNextCommandByte()); + BootloaderAPI_FillWord(CurrAddress, FetchNextCommandByte()); /* Send confirmation byte back to the host */ WriteNextResponseByte('\r'); @@ -579,7 +565,7 @@ static void CDC_Task(void) else if (Command == AVR109_COMMAND_FillFlashPageWordLow) { /* Write the low byte to the current flash page */ - boot_page_fill(CurrAddress | 0x01, FetchNextCommandByte()); + BootloaderAPI_FillWord(CurrAddress | 0x01, FetchNextCommandByte()); /* Increment the address */ CurrAddress += 2; @@ -590,10 +576,7 @@ static void CDC_Task(void) else if (Command == AVR109_COMMAND_WriteFlashPage) { /* Commit the flash page to memory */ - boot_page_write(CurrAddress); - - /* Wait until write operation has completed */ - boot_spm_busy_wait(); + BootloaderAPI_WritePage(CurrAddress); /* Send confirmation byte back to the host */ WriteNextResponseByte('\r'); diff --git a/Bootloaders/CDC/BootloaderCDC.h b/Bootloaders/CDC/BootloaderCDC.h index b6543aa73..f86507f57 100644 --- a/Bootloaders/CDC/BootloaderCDC.h +++ b/Bootloaders/CDC/BootloaderCDC.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -43,6 +43,7 @@ #include <avr/eeprom.h> #include <avr/power.h> #include <avr/interrupt.h> + #include <util/delay.h> #include <stdbool.h> #include "Descriptors.h" diff --git a/Bootloaders/CDC/Config/AppConfig.h b/Bootloaders/CDC/Config/AppConfig.h index 22972b72f..057069314 100644 --- a/Bootloaders/CDC/Config/AppConfig.h +++ b/Bootloaders/CDC/Config/AppConfig.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/CDC/Config/LUFAConfig.h b/Bootloaders/CDC/Config/LUFAConfig.h index 5aa0e765b..33d50a786 100644 --- a/Bootloaders/CDC/Config/LUFAConfig.h +++ b/Bootloaders/CDC/Config/LUFAConfig.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/CDC/Descriptors.c b/Bootloaders/CDC/Descriptors.c index 627657037..6f0d01991 100644 --- a/Bootloaders/CDC/Descriptors.c +++ b/Bootloaders/CDC/Descriptors.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -104,7 +104,7 @@ const USB_Descriptor_Configuration_t ConfigurationDescriptor = .CDC_Functional_Header = { - .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = DTYPE_CSInterface}, + .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = CDC_DTYPE_CSInterface}, .Subtype = 0x00, .CDCSpecification = VERSION_BCD(1,1,0), @@ -112,7 +112,7 @@ const USB_Descriptor_Configuration_t ConfigurationDescriptor = .CDC_Functional_ACM = { - .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = DTYPE_CSInterface}, + .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = CDC_DTYPE_CSInterface}, .Subtype = 0x02, .Capabilities = 0x02, @@ -120,7 +120,7 @@ const USB_Descriptor_Configuration_t ConfigurationDescriptor = .CDC_Functional_Union = { - .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = DTYPE_CSInterface}, + .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = CDC_DTYPE_CSInterface}, .Subtype = 0x06, .MasterInterfaceNumber = INTERFACE_ID_CDC_CCI, @@ -184,7 +184,7 @@ const USB_Descriptor_String_t LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGU * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device * Descriptor. */ -const USB_Descriptor_String_t ManufacturerString = USB_STRING_DESCRIPTOR(L"Dean Camera"); +const USB_Descriptor_String_t ManufacturerString = USB_STRING_DESCRIPTOR(L"LUFA Library"); /** Product descriptor string. This is a Unicode string containing the product's details in human readable form, * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device diff --git a/Bootloaders/CDC/Descriptors.h b/Bootloaders/CDC/Descriptors.h index a6fbf5262..07497c8e5 100644 --- a/Bootloaders/CDC/Descriptors.h +++ b/Bootloaders/CDC/Descriptors.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/CDC/makefile b/Bootloaders/CDC/makefile index aa5a2117d..c618e9cca 100644 --- a/Bootloaders/CDC/makefile +++ b/Bootloaders/CDC/makefile @@ -1,6 +1,6 @@ # # LUFA Library -# Copyright (C) Dean Camera, 2017. +# Copyright (C) Dean Camera, 2019. # # dean [at] fourwalledcubicle [dot] com # www.lufa-lib.org diff --git a/Bootloaders/DFU/BootloaderAPI.c b/Bootloaders/DFU/BootloaderAPI.c index 491c506d0..21d13bbe3 100644 --- a/Bootloaders/DFU/BootloaderAPI.c +++ b/Bootloaders/DFU/BootloaderAPI.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -35,18 +35,39 @@ #include "BootloaderAPI.h" +static bool IsPageAddressValid(const uint32_t Address) +{ + /* Determine if the given page address is correctly aligned to the + start of a flash page. */ + bool PageAddressIsAligned = !(Address & (SPM_PAGESIZE - 1)); + + return (Address < BOOT_START_ADDR) && PageAddressIsAligned; +} + void BootloaderAPI_ErasePage(const uint32_t Address) { - boot_page_erase_safe(Address); - boot_spm_busy_wait(); - boot_rww_enable(); + if (! IsPageAddressValid(Address)) + return; + + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + boot_page_erase_safe(Address); + boot_spm_busy_wait(); + boot_rww_enable(); + } } void BootloaderAPI_WritePage(const uint32_t Address) { - boot_page_write_safe(Address); - boot_spm_busy_wait(); - boot_rww_enable(); + if (! IsPageAddressValid(Address)) + return; + + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + boot_page_write_safe(Address); + boot_spm_busy_wait(); + boot_rww_enable(); + } } void BootloaderAPI_FillWord(const uint32_t Address, const uint16_t Word) @@ -71,6 +92,8 @@ uint8_t BootloaderAPI_ReadLock(void) void BootloaderAPI_WriteLock(const uint8_t LockBits) { - boot_lock_bits_set_safe(LockBits); + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + boot_lock_bits_set_safe(LockBits); + } } - diff --git a/Bootloaders/DFU/BootloaderAPI.h b/Bootloaders/DFU/BootloaderAPI.h index 5169bbc3c..d00645b5a 100644 --- a/Bootloaders/DFU/BootloaderAPI.h +++ b/Bootloaders/DFU/BootloaderAPI.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -39,6 +39,7 @@ /* Includes: */ #include <avr/io.h> #include <avr/boot.h> + #include <util/atomic.h> #include <stdbool.h> #include <LUFA/Common/Common.h> diff --git a/Bootloaders/DFU/BootloaderAPITable.S b/Bootloaders/DFU/BootloaderAPITable.S index 95fd8e5c3..24208fb6c 100644 --- a/Bootloaders/DFU/BootloaderAPITable.S +++ b/Bootloaders/DFU/BootloaderAPITable.S @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/DFU/BootloaderDFU.c b/Bootloaders/DFU/BootloaderDFU.c index 928cf6fe3..883afef03 100644 --- a/Bootloaders/DFU/BootloaderDFU.c +++ b/Bootloaders/DFU/BootloaderDFU.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -133,7 +133,7 @@ void Application_Jump_Check(void) JTAG_ENABLE(); #else /* Check if the device's BOOTRST fuse is set */ - if (boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS) & FUSE_BOOTRST) + if (!(BootloaderAPI_ReadFuse(GET_HIGH_FUSE_BITS) & ~FUSE_BOOTRST)) { /* If the reset source was not an external reset or the key is correct, clear it and jump to the application */ //if (!(MCUSR & (1 << EXTRF)) || (MagicBootKey == MAGIC_BOOT_KEY)) @@ -196,7 +196,7 @@ int main(void) while (RunBootloader || WaitForExit) { USB_USBTask(); #if (BOARD == BOARD_QMK) - bool pressed = (PIN(QMK_ESC_ROW) & NUM(QMK_ESC_ROW)); + bool pressed = (PIN(QMK_ESC_INPUT) & NUM(QMK_ESC_INPUT)); if ((DFU_State == dfuIDLE) && (keypress > 5000) && pressed) { break; } @@ -209,6 +209,9 @@ int main(void) #endif } + /* Wait a short time to end all USB transactions and then disconnect */ + _delay_us(1000); + /* Reset configured hardware back to their original states for the user application */ ResetHardware(); @@ -231,12 +234,12 @@ static void SetupHardware(void) MCUCR = (1 << IVSEL); #if (BOARD == BOARD_QMK) - // column setup - DDR(QMK_ESC_COL) |= NUM(QMK_ESC_COL); - PORT(QMK_ESC_COL) |= NUM(QMK_ESC_COL); + // output setup + DDR(QMK_ESC_OUTPUT) |= NUM(QMK_ESC_OUTPUT); + PORT(QMK_ESC_OUTPUT) |= NUM(QMK_ESC_OUTPUT); // row setup - DDR(QMK_ESC_ROW) |= NUM(QMK_ESC_ROW); + DDR(QMK_ESC_INPUT) |= NUM(QMK_ESC_INPUT); #endif /* Initialize the USB and other board hardware drivers */ @@ -265,7 +268,7 @@ static void ResetHardware(void) MCUCR = 0; #if (BOARD == BOARD_QMK) - DDR(QMK_ESC_COL) = PORT(QMK_ESC_COL) = DDR(QMK_ESC_ROW) = PORT(QMK_ESC_ROW) = 0; + DDR(QMK_ESC_OUTPUT) = PORT(QMK_ESC_OUTPUT) = DDR(QMK_ESC_INPUT) = PORT(QMK_ESC_INPUT) = 0; #endif } @@ -383,7 +386,7 @@ void EVENT_USB_Device_ControlRequest(void) } /* Write the next word into the current flash page */ - boot_page_fill(CurrFlashAddress.Long, Endpoint_Read_16_LE()); + BootloaderAPI_FillWord(CurrFlashAddress.Long, Endpoint_Read_16_LE()); /* Adjust counters */ WordsInFlashPage += 1; @@ -393,8 +396,7 @@ void EVENT_USB_Device_ControlRequest(void) if ((WordsInFlashPage == (SPM_PAGESIZE >> 1)) || !(WordsRemaining)) { /* Commit the flash page to memory */ - boot_page_write(CurrFlashPageStartAddress); - boot_spm_busy_wait(); + BootloaderAPI_WritePage(CurrFlashPageStartAddress); /* Check if programming incomplete */ if (WordsRemaining) @@ -403,17 +405,13 @@ void EVENT_USB_Device_ControlRequest(void) WordsInFlashPage = 0; /* Erase next page's temp buffer */ - boot_page_erase(CurrFlashAddress.Long); - boot_spm_busy_wait(); + BootloaderAPI_ErasePage(CurrFlashAddress.Long); } } } /* Once programming complete, start address equals the end address */ StartAddr = EndAddr; - - /* Re-enable the RWW section of flash */ - boot_rww_enable(); } else // Write EEPROM { @@ -723,8 +721,7 @@ static void ProcessMemProgCommand(void) } CurrFlashAddress = {.Words = {StartAddr, Flash64KBPage}}; /* Erase the current page's temp buffer */ - boot_page_erase(CurrFlashAddress.Long); - boot_spm_busy_wait(); + BootloaderAPI_ErasePage(CurrFlashAddress.Long); } /* Set the state so that the next DNLOAD requests reads in the firmware */ @@ -821,21 +818,9 @@ static void ProcessWriteCommand(void) } else if (IS_TWOBYTE_COMMAND(SentCommand.Data, 0x00, 0xFF)) // Erase flash { - uint32_t CurrFlashAddress = 0; - /* Clear the application section of flash */ - while (CurrFlashAddress < (uint32_t)BOOT_START_ADDR) - { - boot_page_erase(CurrFlashAddress); - boot_spm_busy_wait(); - boot_page_write(CurrFlashAddress); - boot_spm_busy_wait(); - - CurrFlashAddress += SPM_PAGESIZE; - } - - /* Re-enable the RWW section of flash as writing to the flash locks it out */ - boot_rww_enable(); + for (uint32_t CurrFlashAddress = 0; CurrFlashAddress < (uint32_t)BOOT_START_ADDR; CurrFlashAddress += SPM_PAGESIZE) + BootloaderAPI_ErasePage(CurrFlashAddress); /* Memory has been erased, reset the security bit so that programming/reading is allowed */ IsSecure = false; diff --git a/Bootloaders/DFU/BootloaderDFU.h b/Bootloaders/DFU/BootloaderDFU.h index a97ba6c7e..bef122c72 100644 --- a/Bootloaders/DFU/BootloaderDFU.h +++ b/Bootloaders/DFU/BootloaderDFU.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/DFU/Config/AppConfig.h b/Bootloaders/DFU/Config/AppConfig.h index 3acf33c7e..db580c2b4 100644 --- a/Bootloaders/DFU/Config/AppConfig.h +++ b/Bootloaders/DFU/Config/AppConfig.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/DFU/Config/LUFAConfig.h b/Bootloaders/DFU/Config/LUFAConfig.h index 59ae519e4..cbe773b42 100644 --- a/Bootloaders/DFU/Config/LUFAConfig.h +++ b/Bootloaders/DFU/Config/LUFAConfig.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/DFU/Descriptors.c b/Bootloaders/DFU/Descriptors.c index 6b7b6d490..dcad11d6c 100644 --- a/Bootloaders/DFU/Descriptors.c +++ b/Bootloaders/DFU/Descriptors.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -36,6 +36,14 @@ */ #include "Descriptors.h" +#include "Keyboard.h" + + #ifndef MANUFACTURER + #define MANUFACTURER QMK +#endif +#ifndef PRODUCT + #define PRODUCT Keyboard +#endif /** Device descriptor structure. This descriptor, located in SRAM memory, describes the overall * device characteristics, including the supported USB version, control endpoint size and the @@ -125,13 +133,13 @@ const USB_Descriptor_String_t LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGU * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device * Descriptor. */ -const USB_Descriptor_String_t ManufacturerString = USB_STRING_DESCRIPTOR(L"QMK"); +const USB_Descriptor_String_t ManufacturerString = USB_STRING_DESCRIPTOR(LSTR(MANUFACTURER)); /** Product descriptor string. This is a Unicode string containing the product's details in human readable form, * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device * Descriptor. */ -const USB_Descriptor_String_t ProductString = USB_STRING_DESCRIPTOR(L"KB"); +const USB_Descriptor_String_t ProductString = USB_STRING_DESCRIPTOR(LSTR(PRODUCT)); /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors" * documentation) by the application code so that the address and size of a requested descriptor can be given @@ -182,4 +190,3 @@ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, *DescriptorAddress = Address; return Size; } - diff --git a/Bootloaders/DFU/Descriptors.h b/Bootloaders/DFU/Descriptors.h index 5487f88f3..40f604a1e 100644 --- a/Bootloaders/DFU/Descriptors.h +++ b/Bootloaders/DFU/Descriptors.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -189,6 +189,13 @@ const uint16_t wIndex, const void** const DescriptorAddress) ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(3); + + // convert to L string + #define LSTR(s) XLSTR(s) + #define XLSTR(s) L ## #s + // convert to string + #define STR(s) XSTR(s) + #define XSTR(s) #s -#endif +#endif diff --git a/Bootloaders/DFU/makefile b/Bootloaders/DFU/makefile index 0d2014015..3df99a57d 100644 --- a/Bootloaders/DFU/makefile +++ b/Bootloaders/DFU/makefile @@ -1,6 +1,6 @@ # # LUFA Library -# Copyright (C) Dean Camera, 2017. +# Copyright (C) Dean Camera, 2019. # # dean [at] fourwalledcubicle [dot] com # www.lufa-lib.org @@ -11,11 +11,11 @@ # Run "make help" for target help. -MCU = atmega32u4 -ARCH = AVR8 +MCU ?= atmega32u4 +ARCH ?= AVR8 BOARD = QMK -F_CPU = 16000000 -F_USB = $(F_CPU) +F_CPU ?= 16000000 +F_USB ?= $(F_CPU) OPTIMIZATION = s TARGET = BootloaderDFU SRC = $(TARGET).c Descriptors.c BootloaderAPI.c BootloaderAPITable.S $(LUFA_SRC_USB) @@ -26,8 +26,8 @@ LD_FLAGS = -Wl,--section-start=.text=$(BOOT_START_OFFSET) $(BOOT_API_LD_FLAG # Flash size and bootloader section sizes of the target, in KB. These must # match the target's total FLASH size and the bootloader size set in the # device's fuses. -FLASH_SIZE_KB = 32 -BOOT_SECTION_SIZE_KB = 4 +FLASH_SIZE_KB ?= 32 +BOOT_SECTION_SIZE_KB ?= 4 # Bootloader address calculation formulas # Do not modify these macros, but rather modify the dependent values above. diff --git a/Bootloaders/HID/BootloaderHID.c b/Bootloaders/HID/BootloaderHID.c index fa1dd5873..68a7cb5e5 100644 --- a/Bootloaders/HID/BootloaderHID.c +++ b/Bootloaders/HID/BootloaderHID.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -84,6 +84,9 @@ int main(void) while (RunBootloader) USB_USBTask(); + /* Wait a short time to end all USB transactions and then disconnect */ + _delay_us(1000); + /* Disconnect from the host - USB interface will be reset later along with the AVR */ USB_Detach(); @@ -152,6 +155,10 @@ void EVENT_USB_Device_ControlRequest(void) uint16_t PageAddress = Endpoint_Read_16_LE(); #endif + /* Determine if the given page address is correctly aligned to the + start of a flash page. */ + bool PageAddressIsAligned = !(PageAddress & (SPM_PAGESIZE - 1)); + /* Check if the command is a program page command, or a start application command */ #if (FLASHEND > 0xFFFF) if ((uint16_t)(PageAddress >> 8) == COMMAND_STARTAPPLICATION) @@ -161,11 +168,14 @@ void EVENT_USB_Device_ControlRequest(void) { RunBootloader = false; } - else if (PageAddress < BOOT_START_ADDR) + else if ((PageAddress < BOOT_START_ADDR) && PageAddressIsAligned) { /* Erase the given FLASH page, ready to be programmed */ - boot_page_erase(PageAddress); - boot_spm_busy_wait(); + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + boot_page_erase(PageAddress); + boot_spm_busy_wait(); + } /* Write each of the FLASH page's bytes in sequence */ for (uint8_t PageWord = 0; PageWord < (SPM_PAGESIZE / 2); PageWord++) @@ -182,8 +192,11 @@ void EVENT_USB_Device_ControlRequest(void) } /* Write the filled FLASH page to memory */ - boot_page_write(PageAddress); - boot_spm_busy_wait(); + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + boot_page_write(PageAddress); + boot_spm_busy_wait(); + } /* Re-enable RWW section */ boot_rww_enable(); diff --git a/Bootloaders/HID/BootloaderHID.h b/Bootloaders/HID/BootloaderHID.h index 62ee07de3..8e0c7685f 100644 --- a/Bootloaders/HID/BootloaderHID.h +++ b/Bootloaders/HID/BootloaderHID.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -42,6 +42,8 @@ #include <avr/boot.h> #include <avr/power.h> #include <avr/interrupt.h> + #include <util/atomic.h> + #include <util/delay.h> #include <stdbool.h> #include "Descriptors.h" diff --git a/Bootloaders/HID/Config/LUFAConfig.h b/Bootloaders/HID/Config/LUFAConfig.h index 5aa0e765b..33d50a786 100644 --- a/Bootloaders/HID/Config/LUFAConfig.h +++ b/Bootloaders/HID/Config/LUFAConfig.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/HID/Descriptors.c b/Bootloaders/HID/Descriptors.c index 854ae1b63..19d4ec47e 100644 --- a/Bootloaders/HID/Descriptors.c +++ b/Bootloaders/HID/Descriptors.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/HID/Descriptors.h b/Bootloaders/HID/Descriptors.h index 5516b1635..151920dc2 100644 --- a/Bootloaders/HID/Descriptors.h +++ b/Bootloaders/HID/Descriptors.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/HID/HostLoaderApp_Python/hid_bootloader_loader.py b/Bootloaders/HID/HostLoaderApp_Python/hid_bootloader_loader.py index cb824f582..4bbb52f70 100644 --- a/Bootloaders/HID/HostLoaderApp_Python/hid_bootloader_loader.py +++ b/Bootloaders/HID/HostLoaderApp_Python/hid_bootloader_loader.py @@ -1,6 +1,8 @@ +#!/usr/bin/env python + """ LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org @@ -15,12 +17,12 @@ Example: python hid_bootloader_loader.py at90usb1287 Mouse.hex - Requires the pywinusb (https://pypi.python.org/pypi/pywinusb/) and - IntelHex (https://pypi.python.org/pypi/IntelHex/) libraries. + Requires the hidapi (https://pypi.python.org/pypi/hidapi) and + IntelHex (https://pypi.python.org/pypi/IntelHex) libraries. """ import sys -from pywinusb import hid +import hid from intelhex import IntelHex @@ -40,26 +42,29 @@ device_info_map['at90usb82'] = {'page_size': 128, 'flash_kb': 8} def get_hid_device_handle(): - hid_device_filter = hid.HidDeviceFilter(vendor_id=0x03EB, - product_id=0x2067) + all_hid_devices = hid.enumerate() - valid_hid_devices = hid_device_filter.get_devices() + lufa_hid_devices = [d for d in all_hid_devices if d['vendor_id'] == 0x03EB and d['product_id'] == 0x2067] - if len(valid_hid_devices) is 0: + if len(lufa_hid_devices) is 0: return None - else: - return valid_hid_devices[0] + + device_handle = hid.device() + device_handle.open_path(lufa_hid_devices[0]['path']) + return device_handle def send_page_data(hid_device, address, data): # Bootloader page data should be the HID Report ID (always zero) followed # by the starting address to program, then one device's flash page worth # of data - output_report_data = [0] - output_report_data.extend([address & 0xFF, address >> 8]) - output_report_data.extend(data) + output_report_data = bytearray(65) + output_report_data[0] = 0 + output_report_data[1] = address & 0xFF + output_report_data[2] = address >> 8 + output_report_data[3 : ] = data - hid_device.send_output_report(output_report_data) + hid_device.write(output_report_data) def program_device(hex_data, device_info): @@ -70,8 +75,7 @@ def program_device(hex_data, device_info): sys.exit(1) try: - hid_device.open() - print("Connected to bootloader.") + print("Connected to bootloader.", flush=True) # Program in all data from the loaded HEX file, in a number of device # page sized chunks @@ -83,7 +87,7 @@ def program_device(hex_data, device_info): # address and convert it to a regular list of bytes page_data = [hex_data[i] for i in current_page_range] - print("Writing address 0x%04X-0x%04X" % (current_page_range[0], current_page_range[-1])) + print("Writing address 0x%04X-0x%04X" % (current_page_range[0], current_page_range[-1]), flush=True) # Devices with more than 64KB of flash should shift down the page # address so that it is 16-bit (page size is guaranteed to be @@ -95,7 +99,7 @@ def program_device(hex_data, device_info): # Once programming is complete, start the application via a dummy page # program to the page address 0xFFFF - print("Programming complete, starting application.") + print("Programming complete, starting application.", flush=True) send_page_data(hid_device, 0xFFFF, [0] * device_info['page_size']) finally: @@ -103,6 +107,11 @@ def program_device(hex_data, device_info): if __name__ == '__main__': + if len(sys.argv) != 3: + print("Usage:") + print("\t{} device file.hex".format(sys.argv[0])) + sys.exit(1) + # Load the specified HEX file try: hex_data = IntelHex(sys.argv[2]) diff --git a/Bootloaders/HID/makefile b/Bootloaders/HID/makefile index 12cfadb5d..238823fe3 100644 --- a/Bootloaders/HID/makefile +++ b/Bootloaders/HID/makefile @@ -1,6 +1,6 @@ # # LUFA Library -# Copyright (C) Dean Camera, 2017. +# Copyright (C) Dean Camera, 2019. # # dean [at] fourwalledcubicle [dot] com # www.lufa-lib.org diff --git a/Bootloaders/MassStorage/BootloaderAPI.c b/Bootloaders/MassStorage/BootloaderAPI.c index 491c506d0..8fbe44f01 100644 --- a/Bootloaders/MassStorage/BootloaderAPI.c +++ b/Bootloaders/MassStorage/BootloaderAPI.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -35,18 +35,44 @@ #include "BootloaderAPI.h" +bool IsPageAddressValid(const uint32_t Address) +{ + /* Determine if the given page address is correctly aligned to the + start of a flash page. + + Note that this is not static, as we need to force it into the + AUX_BOOT_SECTION on small flash devices to save space. + */ + + bool PageAddressIsAligned = !(Address & (SPM_PAGESIZE - 1)); + + return (Address < BOOT_START_ADDR) && PageAddressIsAligned; +} + void BootloaderAPI_ErasePage(const uint32_t Address) { - boot_page_erase_safe(Address); - boot_spm_busy_wait(); - boot_rww_enable(); + if (! IsPageAddressValid(Address)) + return; + + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + boot_page_erase_safe(Address); + boot_spm_busy_wait(); + boot_rww_enable(); + } } void BootloaderAPI_WritePage(const uint32_t Address) { - boot_page_write_safe(Address); - boot_spm_busy_wait(); - boot_rww_enable(); + if (! IsPageAddressValid(Address)) + return; + + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + boot_page_write_safe(Address); + boot_spm_busy_wait(); + boot_rww_enable(); + } } void BootloaderAPI_FillWord(const uint32_t Address, const uint16_t Word) @@ -71,6 +97,8 @@ uint8_t BootloaderAPI_ReadLock(void) void BootloaderAPI_WriteLock(const uint8_t LockBits) { - boot_lock_bits_set_safe(LockBits); + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + boot_lock_bits_set_safe(LockBits); + } } - diff --git a/Bootloaders/MassStorage/BootloaderAPI.h b/Bootloaders/MassStorage/BootloaderAPI.h index 4889b4c0d..87e16ffd7 100644 --- a/Bootloaders/MassStorage/BootloaderAPI.h +++ b/Bootloaders/MassStorage/BootloaderAPI.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -39,6 +39,7 @@ /* Includes: */ #include <avr/io.h> #include <avr/boot.h> + #include <util/atomic.h> #include <stdbool.h> #include <LUFA/Common/Common.h> @@ -51,6 +52,8 @@ #endif /* Function Prototypes: */ + bool IsPageAddressValid(const uint32_t Address) AUX_BOOT_SECTION ATTR_NO_INLINE; + void BootloaderAPI_ErasePage(const uint32_t Address); void BootloaderAPI_WritePage(const uint32_t Address); void BootloaderAPI_FillWord(const uint32_t Address, const uint16_t Word); diff --git a/Bootloaders/MassStorage/BootloaderAPITable.S b/Bootloaders/MassStorage/BootloaderAPITable.S index 44eb15ea9..cf700ab69 100644 --- a/Bootloaders/MassStorage/BootloaderAPITable.S +++ b/Bootloaders/MassStorage/BootloaderAPITable.S @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/MassStorage/BootloaderMassStorage.c b/Bootloaders/MassStorage/BootloaderMassStorage.c index 6c9697b66..e6157ef92 100644 --- a/Bootloaders/MassStorage/BootloaderMassStorage.c +++ b/Bootloaders/MassStorage/BootloaderMassStorage.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -114,7 +114,7 @@ void Application_Jump_Check(void) JTAG_ENABLE(); #else /* Check if the device's BOOTRST fuse is set */ - if (boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS) & FUSE_BOOTRST) + if (!(BootloaderAPI_ReadFuse(GET_HIGH_FUSE_BITS) & ~FUSE_BOOTRST)) { /* If the reset source was not an external reset or the key is correct, clear it and jump to the application */ if (!(MCUSR & (1 << EXTRF)) || (MagicBootKey == MAGIC_BOOT_KEY)) @@ -169,6 +169,9 @@ int main(void) USB_USBTask(); } + /* Wait a short time to end all USB transactions and then disconnect */ + _delay_us(1000); + /* Disconnect from the host - USB interface will be reset later along with the AVR */ USB_Detach(); diff --git a/Bootloaders/MassStorage/BootloaderMassStorage.h b/Bootloaders/MassStorage/BootloaderMassStorage.h index c9ddee4d7..c788d2921 100644 --- a/Bootloaders/MassStorage/BootloaderMassStorage.h +++ b/Bootloaders/MassStorage/BootloaderMassStorage.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -41,9 +41,11 @@ #include <avr/wdt.h> #include <avr/power.h> #include <avr/interrupt.h> + #include <util/delay.h> #include <string.h> #include "Descriptors.h" + #include "BootloaderAPI.h" #include "Config/AppConfig.h" #include "Lib/SCSI.h" diff --git a/Bootloaders/MassStorage/Config/AppConfig.h b/Bootloaders/MassStorage/Config/AppConfig.h index 92eb364dc..057a63b1d 100644 --- a/Bootloaders/MassStorage/Config/AppConfig.h +++ b/Bootloaders/MassStorage/Config/AppConfig.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/MassStorage/Config/LUFAConfig.h b/Bootloaders/MassStorage/Config/LUFAConfig.h index 735317867..d3576aef8 100644 --- a/Bootloaders/MassStorage/Config/LUFAConfig.h +++ b/Bootloaders/MassStorage/Config/LUFAConfig.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/MassStorage/Descriptors.c b/Bootloaders/MassStorage/Descriptors.c index e8bdbd4f5..bf50dba4b 100644 --- a/Bootloaders/MassStorage/Descriptors.c +++ b/Bootloaders/MassStorage/Descriptors.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/MassStorage/Descriptors.h b/Bootloaders/MassStorage/Descriptors.h index 506f41af8..e603b0ae0 100644 --- a/Bootloaders/MassStorage/Descriptors.h +++ b/Bootloaders/MassStorage/Descriptors.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/MassStorage/Lib/SCSI.c b/Bootloaders/MassStorage/Lib/SCSI.c index 3c14eb901..2109bd224 100644 --- a/Bootloaders/MassStorage/Lib/SCSI.c +++ b/Bootloaders/MassStorage/Lib/SCSI.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/MassStorage/Lib/SCSI.h b/Bootloaders/MassStorage/Lib/SCSI.h index 419559336..86e1d7d83 100644 --- a/Bootloaders/MassStorage/Lib/SCSI.h +++ b/Bootloaders/MassStorage/Lib/SCSI.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/MassStorage/Lib/VirtualFAT.c b/Bootloaders/MassStorage/Lib/VirtualFAT.c index ffd453128..c57b9b911 100644 --- a/Bootloaders/MassStorage/Lib/VirtualFAT.c +++ b/Bootloaders/MassStorage/Lib/VirtualFAT.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/MassStorage/Lib/VirtualFAT.h b/Bootloaders/MassStorage/Lib/VirtualFAT.h index ea80eae4d..07abb9eb2 100644 --- a/Bootloaders/MassStorage/Lib/VirtualFAT.h +++ b/Bootloaders/MassStorage/Lib/VirtualFAT.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/MassStorage/makefile b/Bootloaders/MassStorage/makefile index f3948e754..5ede0977e 100644 --- a/Bootloaders/MassStorage/makefile +++ b/Bootloaders/MassStorage/makefile @@ -1,6 +1,6 @@ # # LUFA Library -# Copyright (C) Dean Camera, 2017. +# Copyright (C) Dean Camera, 2019. # # dean [at] fourwalledcubicle [dot] com # www.lufa-lib.org diff --git a/Bootloaders/Printer/BootloaderAPI.c b/Bootloaders/Printer/BootloaderAPI.c index 2be156808..21d13bbe3 100644 --- a/Bootloaders/Printer/BootloaderAPI.c +++ b/Bootloaders/Printer/BootloaderAPI.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -35,18 +35,39 @@ #include "BootloaderAPI.h" +static bool IsPageAddressValid(const uint32_t Address) +{ + /* Determine if the given page address is correctly aligned to the + start of a flash page. */ + bool PageAddressIsAligned = !(Address & (SPM_PAGESIZE - 1)); + + return (Address < BOOT_START_ADDR) && PageAddressIsAligned; +} + void BootloaderAPI_ErasePage(const uint32_t Address) { - boot_page_erase_safe(Address); - boot_spm_busy_wait(); - boot_rww_enable(); + if (! IsPageAddressValid(Address)) + return; + + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + boot_page_erase_safe(Address); + boot_spm_busy_wait(); + boot_rww_enable(); + } } void BootloaderAPI_WritePage(const uint32_t Address) { - boot_page_write_safe(Address); - boot_spm_busy_wait(); - boot_rww_enable(); + if (! IsPageAddressValid(Address)) + return; + + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + boot_page_write_safe(Address); + boot_spm_busy_wait(); + boot_rww_enable(); + } } void BootloaderAPI_FillWord(const uint32_t Address, const uint16_t Word) @@ -71,5 +92,8 @@ uint8_t BootloaderAPI_ReadLock(void) void BootloaderAPI_WriteLock(const uint8_t LockBits) { - boot_lock_bits_set_safe(LockBits); + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { + boot_lock_bits_set_safe(LockBits); + } } diff --git a/Bootloaders/Printer/BootloaderAPI.h b/Bootloaders/Printer/BootloaderAPI.h index c2d9b4a18..844b59d06 100644 --- a/Bootloaders/Printer/BootloaderAPI.h +++ b/Bootloaders/Printer/BootloaderAPI.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -39,6 +39,7 @@ /* Includes: */ #include <avr/io.h> #include <avr/boot.h> + #include <util/atomic.h> #include <stdbool.h> #include <LUFA/Common/Common.h> diff --git a/Bootloaders/Printer/BootloaderAPITable.S b/Bootloaders/Printer/BootloaderAPITable.S index d8358c31c..58c1d51ff 100644 --- a/Bootloaders/Printer/BootloaderAPITable.S +++ b/Bootloaders/Printer/BootloaderAPITable.S @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/Printer/BootloaderPrinter.c b/Bootloaders/Printer/BootloaderPrinter.c index 9021f998f..deb3b96a8 100644 --- a/Bootloaders/Printer/BootloaderPrinter.c +++ b/Bootloaders/Printer/BootloaderPrinter.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -67,7 +67,7 @@ USB_ClassInfo_PRNT_Device_t TextOnly_Printer_Interface = /** Intel HEX parser state machine state information, to track the contents of * a HEX file streamed in as a sequence of arbitrary bytes. */ -struct +static struct { /** Current HEX parser state machine state. */ uint8_t ParserState; @@ -87,11 +87,11 @@ struct /** Checksum of the current record received so far. */ uint8_t Checksum; /** Starting address of the last addressed FLASH page. */ - uint32_t PageStartAddress; + flashaddr_t PageStartAddress; /** Current 32-bit byte extended base address in FLASH being targeted. */ - uint32_t CurrBaseAddress; + flashaddr_t CurrBaseAddress; /** Current 32-bit byte address in FLASH being targeted. */ - uint32_t CurrAddress; + flashaddr_t CurrAddress; } HEXParser; /** Indicates if there is data waiting to be written to a physical page of @@ -146,7 +146,7 @@ void Application_Jump_Check(void) JTAG_ENABLE(); #else /* Check if the device's BOOTRST fuse is set */ - if (boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS) & FUSE_BOOTRST) + if (!(BootloaderAPI_ReadFuse(GET_HIGH_FUSE_BITS) & ~FUSE_BOOTRST)) { /* If the reset source was not an external reset or the key is correct, clear it and jump to the application */ if (!(MCUSR & (1 << EXTRF)) || (MagicBootKey == MAGIC_BOOT_KEY)) @@ -221,8 +221,7 @@ static void FlushPageIfRequired(void) uint32_t NewPageStartAddress = (HEXParser.CurrAddress & ~(SPM_PAGESIZE - 1)); if (HEXParser.PageStartAddress != NewPageStartAddress) { - boot_page_write(HEXParser.PageStartAddress); - boot_spm_busy_wait(); + BootloaderAPI_WritePage(HEXParser.PageStartAddress); HEXParser.PageStartAddress = NewPageStartAddress; @@ -321,14 +320,13 @@ static void ParseIntelHEXByte(const char ReadCharacter) /* If we are writing to a new page, we need to erase it first */ if (!(PageDirty)) { - boot_page_erase(HEXParser.PageStartAddress); - boot_spm_busy_wait(); + BootloaderAPI_ErasePage(HEXParser.PageStartAddress); PageDirty = true; } /* Fill the FLASH memory buffer with the new word of data */ - boot_page_fill(HEXParser.CurrAddress, NewDataWord); + BootloaderAPI_FillWord(HEXParser.CurrAddress, NewDataWord); HEXParser.CurrAddress += 2; /* Flush the FLASH page to physical memory if we are crossing a page boundary */ @@ -403,6 +401,9 @@ int main(void) USB_USBTask(); } + /* Wait a short time to end all USB transactions and then disconnect */ + _delay_us(1000); + /* Disconnect from the host - USB interface will be reset later along with the AVR */ USB_Detach(); diff --git a/Bootloaders/Printer/BootloaderPrinter.h b/Bootloaders/Printer/BootloaderPrinter.h index 8bc1a6879..356dab7a2 100644 --- a/Bootloaders/Printer/BootloaderPrinter.h +++ b/Bootloaders/Printer/BootloaderPrinter.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -41,8 +41,10 @@ #include <avr/wdt.h> #include <avr/power.h> #include <avr/interrupt.h> + #include <util/delay.h> #include "Descriptors.h" + #include "BootloaderAPI.h" #include <LUFA/Drivers/Board/LEDs.h> #include <LUFA/Drivers/USB/USB.h> @@ -72,6 +74,13 @@ /** Magic bootloader key to unlock forced application start mode. */ #define MAGIC_BOOT_KEY 0xDC42 + /* Type Defines: */ + #if (FLASHEND > 0xFFFF) + typedef uint32_t flashaddr_t; + #else + typedef uint16_t flashaddr_t; + #endif + /* Enums: */ /** Intel HEX parser state machine states. */ enum HEX_Parser_States_t diff --git a/Bootloaders/Printer/Config/LUFAConfig.h b/Bootloaders/Printer/Config/LUFAConfig.h index 5aa0e765b..33d50a786 100644 --- a/Bootloaders/Printer/Config/LUFAConfig.h +++ b/Bootloaders/Printer/Config/LUFAConfig.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/Printer/Descriptors.c b/Bootloaders/Printer/Descriptors.c index 99625d605..7194e8bd3 100644 --- a/Bootloaders/Printer/Descriptors.c +++ b/Bootloaders/Printer/Descriptors.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -134,7 +134,7 @@ const USB_Descriptor_String_t LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGU * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device * Descriptor. */ -const USB_Descriptor_String_t ManufacturerString = USB_STRING_DESCRIPTOR(L"Dean Camera"); +const USB_Descriptor_String_t ManufacturerString = USB_STRING_DESCRIPTOR(L"LUFA Library"); /** Product descriptor string. This is a Unicode string containing the product's details in human readable form, * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device diff --git a/Bootloaders/Printer/Descriptors.h b/Bootloaders/Printer/Descriptors.h index adb0dddb6..49b95c1c4 100644 --- a/Bootloaders/Printer/Descriptors.h +++ b/Bootloaders/Printer/Descriptors.h @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2019. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2019 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted diff --git a/Bootloaders/Printer/makefile b/Bootloaders/Printer/makefile index 1de35bba0..9fa9e1d18 100644 --- a/Bootloaders/Printer/makefile +++ b/Bootloaders/Printer/makefile @@ -1,6 +1,6 @@ # # LUFA Library -# Copyright (C) Dean Camera, 2017. +# Copyright (C) Dean Camera, 2019. # # dean [at] fourwalledcubicle [dot] com # www.lufa-lib.org diff --git a/Bootloaders/makefile b/Bootloaders/makefile index e030ad44a..cf756f9a5 100644 --- a/Bootloaders/makefile +++ b/Bootloaders/makefile @@ -1,6 +1,6 @@ # # LUFA Library -# Copyright (C) Dean Camera, 2017. +# Copyright (C) Dean Camera, 2019. # # dean [at] fourwalledcubicle [dot] com # www.lufa-lib.org @@ -22,20 +22,9 @@ ifeq ($(MAKELEVEL), 10) $(error EMERGENCY ABORT: INFINITE RECURSION DETECTED) endif -# Need to special-case building without a per-project object directory -ifeq ($(OBJDIR),) - # If no target specified, force "clean all" and disallow parallel build - ifeq ($(MAKECMDGOALS),) - MAKECMDGOALS := clean all - .NOTPARALLEL: - endif - - # If one of the targets is to build, force "clean" beforehand and disallow parallel build - ifneq ($(findstring all, $(MAKECMDGOALS)),) - MAKECMDGOALS := clean $(MAKECMDGOALS) - .NOTPARALLEL: - endif -endif +# Build each directory sequentially, even if we are building using multiple +# cores within each project +.NOTPARALLEL: %: $(PROJECT_DIRECTORIES) @echo . > /dev/null |