Add demos/bootloaders/projects overview to the manual.
[pub/USBasp.git] / Bootloaders / DFU / BootloaderDFU.c
index 10a3016..ca0387b 100644 (file)
@@ -98,6 +98,23 @@ uint16_t EndAddr = 0x0000;
  */\r
 int main (void)\r
 {\r
  */\r
 int main (void)\r
 {\r
+       /* Configure hardware required by the bootloader */\r
+       SetupHardware();\r
+\r
+       /* Run the USB management task while the bootloader is supposed to be running */\r
+       while (RunBootloader || WaitForExit)\r
+         USB_USBTask();\r
+       \r
+       /* Reset configured hardware back to their original states for the user application */\r
+       ResetHardware();\r
+       \r
+       /* Start the user application */\r
+       AppStartPtr();\r
+}\r
+\r
+/** Configures all hardware required for the bootloader. */\r
+void SetupHardware(void)\r
+{\r
        /* Disable watchdog if enabled by bootloader/fuses */\r
        MCUSR &= ~(1 << WDRF);\r
        wdt_disable();\r
        /* Disable watchdog if enabled by bootloader/fuses */\r
        MCUSR &= ~(1 << WDRF);\r
        wdt_disable();\r
@@ -111,35 +128,23 @@ int main (void)
 \r
        /* Initialize the USB subsystem */\r
        USB_Init();\r
 \r
        /* Initialize the USB subsystem */\r
        USB_Init();\r
+}\r
 \r
 \r
-       /* Run the USB management task while the bootloader is supposed to be running */\r
-       while (RunBootloader || WaitForExit)\r
-         USB_USBTask();\r
-       \r
+/** Resets all configured hardware required for the bootloader back to their original states. */\r
+void ResetHardware(void)\r
+{\r
        /* Shut down the USB subsystem */\r
        USB_ShutDown();\r
        \r
        /* Relocate the interrupt vector table back to the application section */\r
        MCUCR = (1 << IVCE);\r
        MCUCR = 0;\r
        /* Shut down the USB subsystem */\r
        USB_ShutDown();\r
        \r
        /* Relocate the interrupt vector table back to the application section */\r
        MCUCR = (1 << IVCE);\r
        MCUCR = 0;\r
-\r
-       /* Reset any used hardware ports back to their defaults */\r
-       PORTD = 0;\r
-       DDRD  = 0;\r
-       \r
-       #if defined(PORTE)\r
-       PORTE = 0;\r
-       DDRE  = 0;\r
-       #endif\r
-       \r
-       /* Start the user application */\r
-       AppStartPtr();\r
 }\r
 \r
 /** Event handler for the USB_Disconnect event. This indicates that the bootloader should exit and the user\r
  *  application started.\r
  */\r
 }\r
 \r
 /** Event handler for the USB_Disconnect event. This indicates that the bootloader should exit and the user\r
  *  application started.\r
  */\r
-void EventHandler_USB_Disconnect(void)\r
+void EVENT_USB_Disconnect(void)\r
 {\r
        /* Upon disconnection, run user application */\r
        RunBootloader = false;\r
 {\r
        /* Upon disconnection, run user application */\r
        RunBootloader = false;\r
@@ -149,7 +154,7 @@ void EventHandler_USB_Disconnect(void)
  *  control requests that are not handled internally by the USB library (including the DFU commands, which are\r
  *  all issued via the control endpoint), so that they can be handled appropriately for the application.\r
  */\r
  *  control requests that are not handled internally by the USB library (including the DFU commands, which are\r
  *  all issued via the control endpoint), so that they can be handled appropriately for the application.\r
  */\r
-void EventHandler_USB_UnhandledControlPacket(void)\r
+void EVENT_USB_UnhandledControlPacket(void)\r
 {\r
        /* Get the size of the command and data from the wLength value */\r
        SentCommand.DataSize = USB_ControlRequest.wLength;\r
 {\r
        /* Get the size of the command and data from the wLength value */\r
        SentCommand.DataSize = USB_ControlRequest.wLength;\r
@@ -342,7 +347,7 @@ void EventHandler_USB_UnhandledControlPacket(void)
                                                }\r
 \r
                                                /* Read the flash word and send it via USB to the host */\r
                                                }\r
 \r
                                                /* Read the flash word and send it via USB to the host */\r
-                                               #if defined(RAMPZ)\r
+                                               #if (FLASHEND > 0xFFFF)\r
                                                        Endpoint_Write_Word_LE(pgm_read_word_far(CurrFlashAddress.Long));\r
                                                #else\r
                                                        Endpoint_Write_Word_LE(pgm_read_word(CurrFlashAddress.Long));                                                   \r
                                                        Endpoint_Write_Word_LE(pgm_read_word_far(CurrFlashAddress.Long));\r
                                                #else\r
                                                        Endpoint_Write_Word_LE(pgm_read_word(CurrFlashAddress.Long));                                                   \r
@@ -462,8 +467,10 @@ static void DiscardFillerBytes(uint8_t NumberOfBytes)
                        /* Wait until next data packet received */\r
                        while (!(Endpoint_IsOUTReceived()));\r
                }\r
                        /* Wait until next data packet received */\r
                        while (!(Endpoint_IsOUTReceived()));\r
                }\r
-\r
-               Endpoint_Discard_Byte();                                                \r
+               else\r
+               {\r
+                       Endpoint_Discard_Byte();\r
+               }\r
        }\r
 }\r
 \r
        }\r
 }\r
 \r
@@ -585,7 +592,7 @@ static void ProcessMemReadCommand(void)
                while (CurrFlashAddress < BOOT_START_ADDR)\r
                {\r
                        /* Check if the current byte is not blank */\r
                while (CurrFlashAddress < BOOT_START_ADDR)\r
                {\r
                        /* Check if the current byte is not blank */\r
-                       #if defined(RAMPZ)\r
+                       #if (FLASHEND > 0xFFFF)\r
                        if (pgm_read_byte_far(CurrFlashAddress) != 0xFF)\r
                        #else\r
                        if (pgm_read_byte(CurrFlashAddress) != 0xFF)\r
                        if (pgm_read_byte_far(CurrFlashAddress) != 0xFF)\r
                        #else\r
                        if (pgm_read_byte(CurrFlashAddress) != 0xFF)\r
@@ -671,7 +678,7 @@ static void ProcessWriteCommand(void)
 static void ProcessReadCommand(void)\r
 {\r
        const uint8_t BootloaderInfo[3] = {BOOTLOADER_VERSION, BOOTLOADER_ID_BYTE1, BOOTLOADER_ID_BYTE2};\r
 static void ProcessReadCommand(void)\r
 {\r
        const uint8_t BootloaderInfo[3] = {BOOTLOADER_VERSION, BOOTLOADER_ID_BYTE1, BOOTLOADER_ID_BYTE2};\r
-       const uint8_t SignatureInfo[3]  = {SIGNATURE_0, SIGNATURE_1, SIGNATURE_2};\r
+       const uint8_t SignatureInfo[3]  = {AVR_SIGNATURE_1,    AVR_SIGNATURE_2,     AVR_SIGNATURE_3};\r
 \r
        uint8_t DataIndexToRead = SentCommand.Data[1];\r
 \r
 \r
        uint8_t DataIndexToRead = SentCommand.Data[1];\r
 \r