Add demos/bootloaders/projects overview to the manual.
[pub/USBasp.git] / Bootloaders / DFU / BootloaderDFU.c
index 1850803..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,29 +128,17 @@ 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
 }\r
 \r
 /** Event handler for the USB_Disconnect event. This indicates that the bootloader should exit and the user\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
@@ -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