From: Peter Henn Date: Wed, 29 Dec 2021 12:10:53 +0000 (+0000) Subject: Merge tag 'LUFA-170418' into ProMicro X-Git-Tag: DFU-Bootloader-ProMicro~9 X-Git-Url: http://git.linex4red.de/pub/lufa.git/commitdiff_plain/8e881ef48f0f49ed6176404597afea1fc7343165?ds=inline;hp=-c Merge tag 'LUFA-170418' into ProMicro - use merge strategy "theirs", means in principle rebase ProMicro branch to LUFA-170418 - remove the wrong FUSE_BOOTRST check in Application_Jump_Check which was added in the meantime --- 8e881ef48f0f49ed6176404597afea1fc7343165 diff --combined Bootloaders/DFU/BootloaderDFU.c index 1f86aeea1,8da725d24..8a3538e48 --- a/Bootloaders/DFU/BootloaderDFU.c +++ b/Bootloaders/DFU/BootloaderDFU.c @@@ -1,13 -1,13 +1,13 @@@ /* LUFA Library - Copyright (C) Dean Camera, 2013. + Copyright (C) Dean Camera, 2017. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2013 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2017 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 @@@ -108,7 -108,17 +108,17 @@@ void Application_Jump_Check(void { bool JumpToApplication = false; - #if ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1)) + #if (BOARD == BOARD_LEONARDO) + /* Enable pull-up on the IO13 pin so we can use it to select the mode */ + PORTC |= (1 << 7); + Delay_MS(10); + + /* If IO13 is not jumpered to ground, start the user application instead */ + JumpToApplication = ((PINC & (1 << 7)) != 0); + + /* Disable pull-up after the check has completed */ + PORTC &= ~(1 << 7); + #elif ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1)) /* Disable JTAG debugging */ JTAG_DISABLE(); @@@ -117,22 -127,42 +127,31 @@@ Delay_MS(10); /* If the TCK pin is not jumpered to ground, start the user application instead */ - JumpToApplication |= ((PINF & (1 << 4)) != 0); + JumpToApplication = ((PINF & (1 << 4)) != 0); /* Re-enable JTAG debugging */ 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 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)) - JumpToApplication = true; - - /* Clear reset source */ - MCUSR &= ~(1 << EXTRF); - } - else + { + /* If the reset source was the bootloader and the key is correct, clear it and jump to the application; + * this can happen in the HWBE fuse is set, and the HBE pin is low during the watchdog reset */ + if ((MCUSR & (1 << WDRF)) && (MagicBootKey == MAGIC_BOOT_KEY)) + JumpToApplication = true; + + /* Clear reset source */ + MCUSR &= ~(1 << WDRF); + } #endif - /* If the reset source was the bootloader and the key is correct, clear it and jump to the application */ - if ((MCUSR & (1 << WDRF)) && (MagicBootKey == MAGIC_BOOT_KEY)) - JumpToApplication |= true; + /* Don't run the user application if the reset vector is blank (no app loaded) */ + bool ApplicationValid = (pgm_read_word_near(0) != 0xFFFF); /* If a request has been made to jump to the user application, honor it */ - if (JumpToApplication) + if (JumpToApplication && ApplicationValid) { /* Turn off the watchdog */ - MCUSR &= ~(1< 5000) + { + break; + } + } + else + { + i = 0; + } + } /* Reset configured hardware back to their original states for the user application */ ResetHardware(); @@@ -215,11 -214,7 +234,11 @@@ static void SetupHardware(void /* Initialize the USB and other board hardware drivers */ USB_Init(); - LEDs_Init(); + //LEDs_Init(); + DDRB = 1; + PORTB = _BV(PB5); + DDRD = 0b00100000; + PORTD = 0; /* Bootloader active LED toggle timer initialization */ TIMSK1 = (1 << TOIE1); @@@ -231,12 -226,8 +250,12 @@@ static void ResetHardware(void { /* Shut down the USB and other board hardware drivers */ USB_Disable(); - LEDs_Disable(); + //LEDs_Disable(); + DDRB = 0; + PORTB = 0; + DDRD = 0; + PORTD = 0; - + /* Disable Bootloader active LED toggle timer */ TIMSK1 = 0; TCCR1B = 0; @@@ -249,10 -240,7 +268,10 @@@ /** 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); + //LEDs_ToggleLEDs(LEDS_LED1 | LEDS_LED2); + PORTB &= ~_BV(PB0); + _delay_ms(5); + PORTB |= _BV(PB0); } /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to @@@ -268,13 -256,8 +287,13 @@@ void EVENT_USB_Device_ControlRequest(vo return; } +stayinbootloader = true; + /* Activity - toggle indicator LEDs */ - LEDs_ToggleLEDs(LEDS_LED1 | LEDS_LED2); + //LEDs_ToggleLEDs(LEDS_LED1 | LEDS_LED2); + PORTB &= ~_BV(PB0); + _delay_ms(5); + PORTB |= _BV(PB0); /* Get the size of the command and data from the wLength value */ SentCommand.DataSize = USB_ControlRequest.wLength; @@@ -417,7 -400,7 +436,7 @@@ } /* Read the byte from the USB interface and write to to the EEPROM */ - eeprom_write_byte((uint8_t*)StartAddr, Endpoint_Read_8()); + eeprom_update_byte((uint8_t*)StartAddr, Endpoint_Read_8()); /* Adjust counters */ StartAddr++; @@@ -536,6 -519,12 +555,12 @@@ case DFU_REQ_GETSTATUS: Endpoint_ClearSETUP(); + while (!(Endpoint_IsINReady())) + { + if (USB_DeviceState == DEVICE_STATE_Unattached) + return; + } + /* Write 8-bit status value */ Endpoint_Write_8(DFU_Status); @@@ -564,6 -553,12 +589,12 @@@ case DFU_REQ_GETSTATE: Endpoint_ClearSETUP(); + while (!(Endpoint_IsINReady())) + { + if (USB_DeviceState == DEVICE_STATE_Unattached) + return; + } + /* Write the current device state to the endpoint */ Endpoint_Write_8(DFU_State); @@@ -786,8 -781,9 +817,9 @@@ static void ProcessWriteCommand(void } else // Start via jump { - /* Set the flag to terminate the bootloader at next opportunity */ - RunBootloader = false; + /* Set the flag to terminate the bootloader at next opportunity if a valid application has been loaded */ + if (pgm_read_word_near(0) == 0xFFFF) + RunBootloader = false; } } } @@@ -820,13 -816,44 +852,44 @@@ static void ProcessReadCommand(void) { const uint8_t BootloaderInfo[3] = {BOOTLOADER_VERSION, BOOTLOADER_ID_BYTE1, BOOTLOADER_ID_BYTE2}; - const uint8_t SignatureInfo[3] = {AVR_SIGNATURE_1, AVR_SIGNATURE_2, AVR_SIGNATURE_3}; + const uint8_t SignatureInfo[4] = {0x58, AVR_SIGNATURE_1, AVR_SIGNATURE_2, AVR_SIGNATURE_3}; - uint8_t DataIndexToRead = SentCommand.Data[1]; + uint8_t DataIndexToRead = SentCommand.Data[1]; + bool ReadAddressInvalid = false; - if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x00)) // Read bootloader info - ResponseByte = BootloaderInfo[DataIndexToRead]; + if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x00)) // Read bootloader info + { + if (DataIndexToRead < 3) + ResponseByte = BootloaderInfo[DataIndexToRead]; + else + ReadAddressInvalid = true; + } else if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01)) // Read signature byte - ResponseByte = SignatureInfo[DataIndexToRead - 0x30]; - } + { + switch (DataIndexToRead) + { + case 0x30: + ResponseByte = SignatureInfo[0]; + break; + case 0x31: + ResponseByte = SignatureInfo[1]; + break; + case 0x60: + ResponseByte = SignatureInfo[2]; + break; + case 0x61: + ResponseByte = SignatureInfo[3]; + break; + default: + ReadAddressInvalid = true; + break; + } + } + if (ReadAddressInvalid) + { + /* Set the state and status variables to indicate the error */ + DFU_State = dfuERROR; + DFU_Status = errADDRESS; + } + } diff --combined Bootloaders/DFU/BootloaderDFU.h index 11a7fd507,a97ba6c7e..ae4547589 --- a/Bootloaders/DFU/BootloaderDFU.h +++ b/Bootloaders/DFU/BootloaderDFU.h @@@ -1,13 -1,13 +1,13 @@@ /* LUFA Library - Copyright (C) Dean Camera, 2013. + Copyright (C) Dean Camera, 2017. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org */ /* - Copyright 2013 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2017 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 @@@ -52,7 -52,13 +52,13 @@@ #include "Config/AppConfig.h" #include - #include + //#include + #include + + /* Preprocessor Checks: */ + #if !defined(__OPTIMIZE_SIZE__) + #error This bootloader requires that it be optimized for size, not speed, to fit into the target device. Change optimization settings and try again. + #endif /* Macros: */ /** Major bootloader version number. */ @@@ -60,9 -66,9 +66,9 @@@ /** Minor bootloader version number. */ #define BOOTLOADER_VERSION_REV 0 - + /** Magic bootloader key to unlock forced application start mode. */ - #define MAGIC_BOOT_KEY 0xDC42 + #define MAGIC_BOOT_KEY 0xDC42 /** Complete bootloader version number expressed as a packed byte, constructed from the * two individual bootloader version macros. @@@ -164,7 -170,7 +170,7 @@@ dfuMANIFEST = 7, dfuMANIFEST_WAIT_RESET = 8, dfuUPLOAD_IDLE = 9, - dfuERROR = 10 + dfuERROR = 10 }; /** DFU command status error codes. Refer to the DFU class specification for information on each error code. */ @@@ -185,7 -191,7 +191,7 @@@ errUSBR = 12, errPOR = 13, errUNKNOWN = 14, - errSTALLEDPKT = 15 + errSTALLEDPKT = 15 }; /* Function Prototypes: */ @@@ -203,7 -209,7 +209,7 @@@ static void ProcessWriteCommand(void); static void ProcessReadCommand(void); #endif - + void Application_Jump_Check(void) ATTR_INIT_SECTION(3); #endif diff --combined Bootloaders/DFU/makefile index 60ad9d0e9,6e15cedfc..e4ff7a611 --- a/Bootloaders/DFU/makefile +++ b/Bootloaders/DFU/makefile @@@ -1,6 -1,6 +1,6 @@@ # # LUFA Library - # Copyright (C) Dean Camera, 2013. + # Copyright (C) Dean Camera, 2017. # # dean [at] fourwalledcubicle [dot] com # www.lufa-lib.org @@@ -11,9 -11,9 +11,9 @@@ # Run "make help" for target help. -MCU = at90usb1287 +MCU = atmega32u4 ARCH = AVR8 -BOARD = USBKEY +BOARD = USER F_CPU = 8000000 F_USB = $(F_CPU) OPTIMIZATION = s @@@ -26,30 -26,37 +26,37 @@@ LD_FLAGS = -Wl,--section-start=.tex # 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 = 128 -BOOT_SECTION_SIZE_KB = 8 +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. CALC_ADDRESS_IN_HEX = $(shell printf "0x%X" $$(( $(1) )) ) BOOT_START_OFFSET = $(call CALC_ADDRESS_IN_HEX, ($(FLASH_SIZE_KB) - $(BOOT_SECTION_SIZE_KB)) * 1024 ) - BOOT_SEC_OFFSET = $(call CALC_ADDRESS_IN_HEX, ($(FLASH_SIZE_KB) * 1024) - $(strip $(1)) ) + BOOT_SEC_OFFSET = $(call CALC_ADDRESS_IN_HEX, ($(FLASH_SIZE_KB) * 1024) - ($(strip $(1))) ) # Bootloader linker section flags for relocating the API table sections to # known FLASH addresses - these should not normally be user-edited. - BOOT_SECTION_LD_FLAG = -Wl,--section-start=.apitable_$(strip $(1))=$(call BOOT_SEC_OFFSET, $(3)) -Wl,--undefined=BootloaderAPI_$(strip $(2)) - BOOT_API_LD_FLAGS = $(call BOOT_SECTION_LD_FLAG, trampolines, Trampolines, 96) - BOOT_API_LD_FLAGS += $(call BOOT_SECTION_LD_FLAG, jumptable, JumpTable, 32) - BOOT_API_LD_FLAGS += $(call BOOT_SECTION_LD_FLAG, signatures, Signatures, 8) + BOOT_SECTION_LD_FLAG = -Wl,--section-start=$(strip $(1))=$(call BOOT_SEC_OFFSET, $(3)) -Wl,--undefined=$(strip $(2)) + BOOT_API_LD_FLAGS = $(call BOOT_SECTION_LD_FLAG, .apitable_trampolines, BootloaderAPI_Trampolines, 96) + BOOT_API_LD_FLAGS += $(call BOOT_SECTION_LD_FLAG, .apitable_jumptable, BootloaderAPI_JumpTable, 32) + BOOT_API_LD_FLAGS += $(call BOOT_SECTION_LD_FLAG, .apitable_signatures, BootloaderAPI_Signatures, 8) # Default target all: - # Include LUFA build script makefiles - include $(LUFA_PATH)/Build/lufa_core.mk - include $(LUFA_PATH)/Build/lufa_sources.mk - include $(LUFA_PATH)/Build/lufa_build.mk - include $(LUFA_PATH)/Build/lufa_cppcheck.mk - include $(LUFA_PATH)/Build/lufa_doxygen.mk - include $(LUFA_PATH)/Build/lufa_avrdude.mk - include $(LUFA_PATH)/Build/lufa_atprogram.mk + # Include LUFA-specific DMBS extension modules + DMBS_LUFA_PATH ?= $(LUFA_PATH)/Build/LUFA + include $(DMBS_LUFA_PATH)/lufa-sources.mk + include $(DMBS_LUFA_PATH)/lufa-gcc.mk + + # Include common DMBS build system modules + DMBS_PATH ?= $(LUFA_PATH)/Build/DMBS/DMBS + include $(DMBS_PATH)/core.mk + include $(DMBS_PATH)/cppcheck.mk + include $(DMBS_PATH)/doxygen.mk + include $(DMBS_PATH)/dfu.mk + include $(DMBS_PATH)/gcc.mk + include $(DMBS_PATH)/hid.mk + include $(DMBS_PATH)/avrdude.mk + include $(DMBS_PATH)/atprogram.mk diff --combined LUFA/StudioIntegration/lufa_toolchain.xml index 45a9ba902,66b416e28..dac65df7e --- a/LUFA/StudioIntegration/lufa_toolchain.xml +++ b/LUFA/StudioIntegration/lufa_toolchain.xml @@@ -1,6 -1,6 +1,6 @@@