Fixed incorrect ADC driver init register manipulation (thanks to Tobias).
[pub/USBasp.git] / Bootloaders / DFU / BootloaderDFU.c
index 704133f..6bea9b2 100644 (file)
  *\r
  *  Main source file for the DFU class bootloader. This file contains the complete bootloader logic.\r
  */\r
  *\r
  *  Main source file for the DFU class bootloader. This file contains the complete bootloader logic.\r
  */\r
\r
-/** Configuration define. Define this token to true to case the bootloader to reject all memory commands\r
- *  until a memory erase has been performed. When used in conjunction with the lockbits of the AVR, this\r
- *  can protect the AVR's firmware from being dumped from a secured AVR. When false, memory operations are\r
- *  allowed at any time.\r
- */\r
-#define SECURE_MODE           false\r
 \r
 #define  INCLUDE_FROM_BOOTLOADER_C\r
 #include "BootloaderDFU.h"\r
 \r
 #define  INCLUDE_FROM_BOOTLOADER_C\r
 #include "BootloaderDFU.h"\r
@@ -57,7 +50,7 @@ bool RunBootloader = true;
 \r
 /** Flag to indicate if the bootloader is waiting to exit. When the host requests the bootloader to exit and\r
  *  jump to the application address it specifies, it sends two sequential commands which must be properly\r
 \r
 /** Flag to indicate if the bootloader is waiting to exit. When the host requests the bootloader to exit and\r
  *  jump to the application address it specifies, it sends two sequential commands which must be properly\r
- *  acknowedged. Upon reception of the first the RunBootloader flag is cleared and the WaitForExit flag is set,\r
+ *  acknowledged. Upon reception of the first the RunBootloader flag is cleared and the WaitForExit flag is set,\r
  *  causing the bootloader to wait for the final exit command before shutting down.\r
  */\r
 bool WaitForExit = false;\r
  *  causing the bootloader to wait for the final exit command before shutting down.\r
  */\r
 bool WaitForExit = false;\r
@@ -109,8 +102,8 @@ int main (void)
        MCUSR &= ~(1 << WDRF);\r
        wdt_disable();\r
 \r
        MCUSR &= ~(1 << WDRF);\r
        wdt_disable();\r
 \r
-       /* Disable Clock Division */\r
-       SetSystemClockPrescaler(0);\r
+       /* Disable clock division */\r
+       clock_prescale_set(clock_div_1);\r
        \r
        /* Relocate the interrupt vector table to the bootloader section */\r
        MCUCR = (1 << IVCE);\r
        \r
        /* Relocate the interrupt vector table to the bootloader section */\r
        MCUCR = (1 << IVCE);\r
@@ -305,7 +298,8 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
 \r
                        Endpoint_ClearSetupOUT();\r
 \r
 \r
                        Endpoint_ClearSetupOUT();\r
 \r
-                       /* Send ZLP to the host to acknowedge the request */\r
+                       /* Acknowledge status stage */\r
+                       while (!(Endpoint_IsSetupINReady()));\r
                        Endpoint_ClearSetupIN();\r
                                \r
                        break;\r
                        Endpoint_ClearSetupIN();\r
                                \r
                        break;\r
@@ -392,7 +386,7 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
 \r
                        Endpoint_ClearSetupIN();\r
 \r
 \r
                        Endpoint_ClearSetupIN();\r
 \r
-                       /* Send ZLP to the host to acknowedge the request */\r
+                       /* Acknowledge status stage */\r
                        while (!(Endpoint_IsSetupOUTReceived()));\r
                        Endpoint_ClearSetupOUT();\r
 \r
                        while (!(Endpoint_IsSetupOUTReceived()));\r
                        Endpoint_ClearSetupOUT();\r
 \r
@@ -415,6 +409,7 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
 \r
                        Endpoint_ClearSetupIN();\r
                        \r
 \r
                        Endpoint_ClearSetupIN();\r
                        \r
+                       /* Acknowledge status stage */\r
                        while (!(Endpoint_IsSetupOUTReceived()));\r
                        Endpoint_ClearSetupOUT();\r
        \r
                        while (!(Endpoint_IsSetupOUTReceived()));\r
                        Endpoint_ClearSetupOUT();\r
        \r
@@ -424,9 +419,11 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                        \r
                        /* Reset the status value variable to the default OK status */\r
                        DFU_Status = OK;\r
                        \r
                        /* Reset the status value variable to the default OK status */\r
                        DFU_Status = OK;\r
-                       \r
-                       Endpoint_ClearSetupIN();\r
 \r
 \r
+                       /* Acknowledge status stage */\r
+                       while (!(Endpoint_IsSetupINReady()));\r
+                       Endpoint_ClearSetupIN();\r
+                       \r
                        break;\r
                case DFU_GETSTATE:\r
                        Endpoint_ClearSetupReceived();\r
                        break;\r
                case DFU_GETSTATE:\r
                        Endpoint_ClearSetupReceived();\r
@@ -436,6 +433,7 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                \r
                        Endpoint_ClearSetupIN();\r
                        \r
                \r
                        Endpoint_ClearSetupIN();\r
                        \r
+                       /* Acknowledge status stage */\r
                        while (!(Endpoint_IsSetupOUTReceived()));\r
                        Endpoint_ClearSetupOUT();\r
 \r
                        while (!(Endpoint_IsSetupOUTReceived()));\r
                        Endpoint_ClearSetupOUT();\r
 \r
@@ -446,6 +444,8 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                        /* Reset the current state variable to the default idle state */\r
                        DFU_State = dfuIDLE;\r
                        \r
                        /* Reset the current state variable to the default idle state */\r
                        DFU_State = dfuIDLE;\r
                        \r
+                       /* Acknowledge status stage */\r
+                       while (!(Endpoint_IsSetupINReady()));\r
                        Endpoint_ClearSetupIN();\r
 \r
                        break;\r
                        Endpoint_ClearSetupIN();\r
 \r
                        break;\r