Rename Drivers/AT90USBXXX to Drivers/Peripheral.
[pub/USBasp.git] / Bootloaders / DFU / BootloaderDFU.c
index a997b20..f358016 100644 (file)
  *\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
@@ -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
- *  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
@@ -109,8 +102,8 @@ int main (void)
        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
@@ -170,7 +163,7 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
        switch (bRequest)\r
        {\r
                case DFU_DNLOAD:\r
-                       Endpoint_ClearSetupReceived();\r
+                       Endpoint_ClearControlSETUP();\r
                        \r
                        /* Check if bootloader is waiting to terminate */\r
                        if (WaitForExit)\r
@@ -185,7 +178,7 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                        /* If the request has a data stage, load it into the command struct */\r
                        if (SentCommand.DataSize)\r
                        {\r
-                               while (!(Endpoint_IsSetupOUTReceived()));\r
+                               while (!(Endpoint_IsOUTReceived()));\r
 \r
                                /* First byte of the data stage is the DNLOAD request's command */\r
                                SentCommand.Command = Endpoint_Read_Byte();\r
@@ -242,8 +235,8 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                                                        /* Check if endpoint is empty - if so clear it and wait until ready for next packet */\r
                                                        if (!(Endpoint_BytesInEndpoint()))\r
                                                        {\r
-                                                               Endpoint_ClearSetupOUT();\r
-                                                               while (!(Endpoint_IsSetupOUTReceived()));\r
+                                                               Endpoint_ClearControlOUT();\r
+                                                               while (!(Endpoint_IsOUTReceived()));\r
                                                        }\r
 \r
                                                        /* Write the next word into the current flash page */\r
@@ -286,8 +279,8 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                                                        /* Check if endpoint is empty - if so clear it and wait until ready for next packet */\r
                                                        if (!(Endpoint_BytesInEndpoint()))\r
                                                        {\r
-                                                               Endpoint_ClearSetupOUT();\r
-                                                               while (!(Endpoint_IsSetupOUTReceived()));\r
+                                                               Endpoint_ClearControlOUT();\r
+                                                               while (!(Endpoint_IsOUTReceived()));\r
                                                        }\r
 \r
                                                        /* Read the byte from the USB interface and write to to the EEPROM */\r
@@ -303,16 +296,17 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                                }\r
                        }\r
 \r
-                       Endpoint_ClearSetupOUT();\r
+                       Endpoint_ClearControlOUT();\r
 \r
-                       /* Send ZLP to the host to acknowedge the request */\r
-                       Endpoint_ClearSetupIN();\r
+                       /* Acknowledge status stage */\r
+                       while (!(Endpoint_IsINReady()));\r
+                       Endpoint_ClearControlIN();\r
                                \r
                        break;\r
                case DFU_UPLOAD:\r
-                       Endpoint_ClearSetupReceived();\r
+                       Endpoint_ClearControlSETUP();\r
 \r
-                       while (!(Endpoint_IsSetupINReady()));\r
+                       while (!(Endpoint_IsINReady()));\r
 \r
                        if (DFU_State != dfuUPLOAD_IDLE)\r
                        {\r
@@ -349,8 +343,8 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                                                /* Check if endpoint is full - if so clear it and wait until ready for next packet */\r
                                                if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE)\r
                                                {\r
-                                                       Endpoint_ClearSetupIN();\r
-                                                       while (!(Endpoint_IsSetupINReady()));\r
+                                                       Endpoint_ClearControlIN();\r
+                                                       while (!(Endpoint_IsINReady()));\r
                                                }\r
 \r
                                                /* Read the flash word and send it via USB to the host */\r
@@ -374,8 +368,8 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                                                /* Check if endpoint is full - if so clear it and wait until ready for next packet */\r
                                                if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE)\r
                                                {\r
-                                                       Endpoint_ClearSetupIN();\r
-                                                       while (!(Endpoint_IsSetupINReady()));\r
+                                                       Endpoint_ClearControlIN();\r
+                                                       while (!(Endpoint_IsINReady()));\r
                                                }\r
 \r
                                                /* Read the EEPROM byte and send it via USB to the host */\r
@@ -390,15 +384,15 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                                DFU_State = dfuIDLE;\r
                        }\r
 \r
-                       Endpoint_ClearSetupIN();\r
+                       Endpoint_ClearControlIN();\r
 \r
-                       /* Send ZLP to the host to acknowedge the request */\r
-                       while (!(Endpoint_IsSetupOUTReceived()));\r
-                       Endpoint_ClearSetupOUT();\r
+                       /* Acknowledge status stage */\r
+                       while (!(Endpoint_IsOUTReceived()));\r
+                       Endpoint_ClearControlOUT();\r
 \r
                        break;\r
                case DFU_GETSTATUS:\r
-                       Endpoint_ClearSetupReceived();\r
+                       Endpoint_ClearControlSETUP();\r
                        \r
                        /* Write 8-bit status value */\r
                        Endpoint_Write_Byte(DFU_Status);\r
@@ -413,40 +407,46 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                        /* Write 8-bit state string ID number */\r
                        Endpoint_Write_Byte(0);\r
 \r
-                       Endpoint_ClearSetupIN();\r
+                       Endpoint_ClearControlIN();\r
                        \r
-                       while (!(Endpoint_IsSetupOUTReceived()));\r
-                       Endpoint_ClearSetupOUT();\r
+                       /* Acknowledge status stage */\r
+                       while (!(Endpoint_IsOUTReceived()));\r
+                       Endpoint_ClearControlOUT();\r
        \r
                        break;          \r
                case DFU_CLRSTATUS:\r
-                       Endpoint_ClearSetupReceived();\r
+                       Endpoint_ClearControlSETUP();\r
                        \r
                        /* Reset the status value variable to the default OK status */\r
                        DFU_Status = OK;\r
-                       \r
-                       Endpoint_ClearSetupIN();\r
 \r
+                       /* Acknowledge status stage */\r
+                       while (!(Endpoint_IsINReady()));\r
+                       Endpoint_ClearControlIN();\r
+                       \r
                        break;\r
                case DFU_GETSTATE:\r
-                       Endpoint_ClearSetupReceived();\r
+                       Endpoint_ClearControlSETUP();\r
                        \r
                        /* Write the current device state to the endpoint */\r
                        Endpoint_Write_Byte(DFU_State);\r
                \r
-                       Endpoint_ClearSetupIN();\r
+                       Endpoint_ClearControlIN();\r
                        \r
-                       while (!(Endpoint_IsSetupOUTReceived()));\r
-                       Endpoint_ClearSetupOUT();\r
+                       /* Acknowledge status stage */\r
+                       while (!(Endpoint_IsOUTReceived()));\r
+                       Endpoint_ClearControlOUT();\r
 \r
                        break;\r
                case DFU_ABORT:\r
-                       Endpoint_ClearSetupReceived();\r
+                       Endpoint_ClearControlSETUP();\r
                        \r
                        /* Reset the current state variable to the default idle state */\r
                        DFU_State = dfuIDLE;\r
                        \r
-                       Endpoint_ClearSetupIN();\r
+                       /* Acknowledge status stage */\r
+                       while (!(Endpoint_IsINReady()));\r
+                       Endpoint_ClearControlIN();\r
 \r
                        break;\r
        }\r
@@ -463,10 +463,10 @@ static void DiscardFillerBytes(uint8_t NumberOfBytes)
        {\r
                if (!(Endpoint_BytesInEndpoint()))\r
                {\r
-                       Endpoint_ClearSetupOUT();\r
+                       Endpoint_ClearControlOUT();\r
 \r
                        /* Wait until next data packet received */\r
-                       while (!(Endpoint_IsSetupOUTReceived()));\r
+                       while (!(Endpoint_IsOUTReceived()));\r
                }\r
 \r
                Endpoint_Discard_Byte();                                                \r
@@ -539,7 +539,7 @@ static void LoadStartEndAddresses(void)
        EndAddr   = Address[1].Word;\r
 }\r
 \r
-/** Handler for a Memory Program command issued by the host. This routine handles the preperations needed\r
+/** Handler for a Memory Program command issued by the host. This routine handles the preparations needed\r
  *  to write subsequent data from the host into the specified memory.\r
  */\r
 static void ProcessMemProgCommand(void)\r
@@ -569,7 +569,7 @@ static void ProcessMemProgCommand(void)
        }\r
 }\r
 \r
-/** Handler for a Memory Read command issued by the host. This routine handles the preperations needed\r
+/** Handler for a Memory Read command issued by the host. This routine handles the preparations needed\r
  *  to read subsequent data from the specified memory out to the host, as well as implementing the memory\r
  *  blank check command.\r
  */\r
@@ -677,7 +677,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
-       const uint8_t SignatureInfo[3]  = {SIGNATURE_BYTE_1, SIGNATURE_BYTE_2, SIGNATURE_BYTE_3};\r
+       const uint8_t SignatureInfo[3]  = {SIGNATURE_0, SIGNATURE_1, SIGNATURE_2};\r
 \r
        uint8_t DataIndexToRead = SentCommand.Data[1];\r
 \r