Update copyrights for 2017.
[pub/lufa.git] / Bootloaders / CDC / BootloaderCDC.c
index 1f07653..aa17bc1 100644 (file)
@@ -1,13 +1,13 @@
 /*
              LUFA Library
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2014.
+     Copyright (C) Dean Camera, 2017.
 
   dean [at] fourwalledcubicle [dot] com
            www.lufa-lib.org
 */
 
 /*
 
   dean [at] fourwalledcubicle [dot] com
            www.lufa-lib.org
 */
 
 /*
-  Copyright 2014  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
 
   Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
@@ -103,8 +103,19 @@ void Application_Jump_Check(void)
                        if (!(MCUSR & (1 << EXTRF)) || (MagicBootKey == MAGIC_BOOT_KEY))
                          JumpToApplication = true;
 
                        if (!(MCUSR & (1 << EXTRF)) || (MagicBootKey == MAGIC_BOOT_KEY))
                          JumpToApplication = true;
 
+                       /* Clear reset source */
                        MCUSR &= ~(1 << EXTRF);
                }
                        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
 
        /* Don't run the user application if the reset vector is blank (no app loaded) */
        #endif
 
        /* Don't run the user application if the reset vector is blank (no app loaded) */
@@ -146,6 +157,9 @@ int main(void)
                USB_USBTask();
        }
 
                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();
 
        /* Disconnect from the host - USB interface will be reset later along with the AVR */
        USB_Detach();
 
@@ -346,7 +360,7 @@ static void ReadWriteMemoryBlock(const uint8_t Command)
                        else
                        {
                                /* Write the next EEPROM byte from the endpoint */
                        else
                        {
                                /* Write the next EEPROM byte from the endpoint */
-                               eeprom_write_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte());
+                               eeprom_update_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte());
 
                                /* Increment the address counter after use */
                                CurrAddress += 2;
 
                                /* Increment the address counter after use */
                                CurrAddress += 2;
@@ -600,7 +614,7 @@ static void CDC_Task(void)
        else if (Command == AVR109_COMMAND_WriteEEPROM)
        {
                /* Read the byte from the endpoint and write it to the EEPROM */
        else if (Command == AVR109_COMMAND_WriteEEPROM)
        {
                /* Read the byte from the endpoint and write it to the EEPROM */
-               eeprom_write_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte());
+               eeprom_update_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte());
 
                /* Increment the address after use */
                CurrAddress += 2;
 
                /* Increment the address after use */
                CurrAddress += 2;
@@ -657,4 +671,3 @@ static void CDC_Task(void)
        /* Acknowledge the command from the host */
        Endpoint_ClearOUT();
 }
        /* Acknowledge the command from the host */
        Endpoint_ClearOUT();
 }
-