Be doubly-certain that the incomming CDC class driver's endpoint/pipe is flushed...
[pub/USBasp.git] / Bootloaders / DFU / BootloaderDFU.c
index 704133f..157b78f 100644 (file)
@@ -1,21 +1,21 @@
 /*\r
              LUFA Library\r
 /*\r
              LUFA Library\r
-     Copyright (C) Dean Camera, 2009.\r
+     Copyright (C) Dean Camera, 2010.\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
-  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
-\r
-  Permission to use, copy, modify, and distribute this software\r
-  and its documentation for any purpose and without fee is hereby\r
-  granted, provided that the above copyright notice appear in all\r
-  copies and that both that the copyright notice and this\r
-  permission notice and warranty disclaimer appear in supporting\r
-  documentation, and that the name of the author not be used in\r
-  advertising or publicity pertaining to distribution of the\r
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, distribute, and sell this \r
+  software and its documentation for any purpose is hereby granted\r
+  without fee, provided that the above copyright notice appear in \r
+  all copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting \r
+  documentation, and that the name of the author not be used in \r
+  advertising or publicity pertaining to distribution of the \r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
  *\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
@@ -99,18 +92,36 @@ uint16_t StartAddr = 0x0000;
  */\r
 uint16_t EndAddr = 0x0000;\r
 \r
  */\r
 uint16_t EndAddr = 0x0000;\r
 \r
+\r
 /** Main program entry point. This routine configures the hardware required by the bootloader, then continuously \r
  *  runs the bootloader processing routine until instructed to soft-exit, or hard-reset via the watchdog to start\r
  *  the loaded application code.\r
  */\r
 /** Main program entry point. This routine configures the hardware required by the bootloader, then continuously \r
  *  runs the bootloader processing routine until instructed to soft-exit, or hard-reset via the watchdog to start\r
  *  the loaded application code.\r
  */\r
-int main (void)\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
 \r
 {\r
        /* Disable watchdog if enabled by bootloader/fuses */\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
@@ -118,59 +129,32 @@ 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
 }\r
 \r
-/** Event handler for the USB_Disconnect event. This indicates that the bootloader should exit and the user\r
- *  application started.\r
- */\r
-EVENT_HANDLER(USB_Disconnect)\r
-{\r
-       /* Upon disconnection, run user application */\r
-       RunBootloader = false;\r
-}\r
-\r
-/** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific\r
+/** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific\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
  *  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
-EVENT_HANDLER(USB_UnhandledControlPacket)\r
+void EVENT_USB_Device_UnhandledControlRequest(void)\r
 {\r
 {\r
-       /* Discard unused wIndex value */\r
-       Endpoint_Discard_Word();\r
-       \r
-       /* Discard unused wValue value */\r
-       Endpoint_Discard_Word();\r
-\r
        /* Get the size of the command and data from the wLength value */\r
        /* Get the size of the command and data from the wLength value */\r
-       SentCommand.DataSize = Endpoint_Read_Word_LE();\r
+       SentCommand.DataSize = USB_ControlRequest.wLength;\r
 \r
 \r
-       switch (bRequest)\r
+       switch (USB_ControlRequest.bRequest)\r
        {\r
                case DFU_DNLOAD:\r
        {\r
                case DFU_DNLOAD:\r
-                       Endpoint_ClearSetupReceived();\r
+                       Endpoint_ClearSETUP();\r
                        \r
                        /* Check if bootloader is waiting to terminate */\r
                        if (WaitForExit)\r
                        \r
                        /* Check if bootloader is waiting to terminate */\r
                        if (WaitForExit)\r
@@ -185,7 +169,11 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                        /* If the request has a data stage, load it into the command struct */\r
                        if (SentCommand.DataSize)\r
                        {\r
                        /* 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
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
 \r
                                /* First byte of the data stage is the DNLOAD request's command */\r
                                SentCommand.Command = Endpoint_Read_Byte();\r
 \r
                                /* First byte of the data stage is the DNLOAD request's command */\r
                                SentCommand.Command = Endpoint_Read_Byte();\r
@@ -217,8 +205,8 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                                        /* Throw away the filler bytes before the start of the firmware */\r
                                        DiscardFillerBytes(DFU_FILLER_BYTES_SIZE);\r
 \r
                                        /* Throw away the filler bytes before the start of the firmware */\r
                                        DiscardFillerBytes(DFU_FILLER_BYTES_SIZE);\r
 \r
-                                       /* Throw away the page alignment filler bytes before the start of the firmware */\r
-                                       DiscardFillerBytes(StartAddr % SPM_PAGESIZE);\r
+                                       /* Throw away the packet alignment filler bytes before the start of the firmware */\r
+                                       DiscardFillerBytes(StartAddr % FIXED_CONTROL_ENDPOINT_SIZE);\r
                                        \r
                                        /* Calculate the number of bytes remaining to be written */\r
                                        uint16_t BytesRemaining = ((EndAddr - StartAddr) + 1);\r
                                        \r
                                        /* Calculate the number of bytes remaining to be written */\r
                                        uint16_t BytesRemaining = ((EndAddr - StartAddr) + 1);\r
@@ -232,7 +220,7 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                                                {\r
                                                        uint16_t Words[2];\r
                                                        uint32_t Long;\r
                                                {\r
                                                        uint16_t Words[2];\r
                                                        uint32_t Long;\r
-                                               } CurrFlashAddress                 = {Words: {StartAddr, Flash64KBPage}};\r
+                                               } CurrFlashAddress                 = {.Words = {StartAddr, Flash64KBPage}};\r
                                                \r
                                                uint32_t CurrFlashPageStartAddress = CurrFlashAddress.Long;\r
                                                uint8_t  WordsInFlashPage          = 0;\r
                                                \r
                                                uint32_t CurrFlashPageStartAddress = CurrFlashAddress.Long;\r
                                                uint8_t  WordsInFlashPage          = 0;\r
@@ -242,8 +230,13 @@ 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
                                                        /* 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_ClearOUT();\r
+\r
+                                                               while (!(Endpoint_IsOUTReceived()))\r
+                                                               {                               \r
+                                                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                                                         return;\r
+                                                               }\r
                                                        }\r
 \r
                                                        /* Write the next word into the current flash page */\r
                                                        }\r
 \r
                                                        /* Write the next word into the current flash page */\r
@@ -286,8 +279,13 @@ 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
                                                        /* 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_ClearOUT();\r
+\r
+                                                               while (!(Endpoint_IsOUTReceived()))\r
+                                                               {                               \r
+                                                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                                                         return;\r
+                                                               }\r
                                                        }\r
 \r
                                                        /* Read the byte from the USB interface and write to to the EEPROM */\r
                                                        }\r
 \r
                                                        /* Read the byte from the USB interface and write to to the EEPROM */\r
@@ -303,17 +301,20 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                                }\r
                        }\r
 \r
                                }\r
                        }\r
 \r
-                       Endpoint_ClearSetupOUT();\r
+                       Endpoint_ClearOUT();\r
+\r
+                       Endpoint_ClearStatusStage();\r
 \r
 \r
-                       /* Send ZLP to the host to acknowedge the request */\r
-                       Endpoint_ClearSetupIN();\r
-                               \r
                        break;\r
                case DFU_UPLOAD:\r
                        break;\r
                case DFU_UPLOAD:\r
-                       Endpoint_ClearSetupReceived();\r
-\r
-                       while (!(Endpoint_IsSetupINReady()));\r
+                       Endpoint_ClearSETUP();\r
 \r
 \r
+                       while (!(Endpoint_IsINReady()))\r
+                       {                               \r
+                               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                 return;\r
+                       }\r
+                                                       \r
                        if (DFU_State != dfuUPLOAD_IDLE)\r
                        {\r
                                if ((DFU_State == dfuERROR) && IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01))       // Blank Check\r
                        if (DFU_State != dfuUPLOAD_IDLE)\r
                        {\r
                                if ((DFU_State == dfuERROR) && IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01))       // Blank Check\r
@@ -342,19 +343,24 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                                        {\r
                                                uint16_t Words[2];\r
                                                uint32_t Long;\r
                                        {\r
                                                uint16_t Words[2];\r
                                                uint32_t Long;\r
-                                       } CurrFlashAddress = {Words: {StartAddr, Flash64KBPage}};\r
+                                       } CurrFlashAddress = {.Words = {StartAddr, Flash64KBPage}};\r
 \r
                                        while (WordsRemaining--)\r
                                        {\r
                                                /* 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
 \r
                                        while (WordsRemaining--)\r
                                        {\r
                                                /* 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_ClearIN();\r
+\r
+                                                       while (!(Endpoint_IsINReady()))\r
+                                                       {                               \r
+                                                               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                                                 return;\r
+                                                       }\r
                                                }\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
@@ -374,8 +380,13 @@ 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
                                                /* 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_ClearIN();\r
+                                                       \r
+                                                       while (!(Endpoint_IsINReady()))\r
+                                                       {                               \r
+                                                               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                                                 return;\r
+                                                       }\r
                                                }\r
 \r
                                                /* Read the EEPROM byte and send it via USB to the host */\r
                                                }\r
 \r
                                                /* Read the EEPROM byte and send it via USB to the host */\r
@@ -390,15 +401,12 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                                DFU_State = dfuIDLE;\r
                        }\r
 \r
                                DFU_State = dfuIDLE;\r
                        }\r
 \r
-                       Endpoint_ClearSetupIN();\r
-\r
-                       /* Send ZLP to the host to acknowedge the request */\r
-                       while (!(Endpoint_IsSetupOUTReceived()));\r
-                       Endpoint_ClearSetupOUT();\r
+                       Endpoint_ClearIN();\r
 \r
 \r
+                       Endpoint_ClearStatusStage();\r
                        break;\r
                case DFU_GETSTATUS:\r
                        break;\r
                case DFU_GETSTATUS:\r
-                       Endpoint_ClearSetupReceived();\r
+                       Endpoint_ClearSETUP();\r
                        \r
                        /* Write 8-bit status value */\r
                        Endpoint_Write_Byte(DFU_Status);\r
                        \r
                        /* Write 8-bit status value */\r
                        Endpoint_Write_Byte(DFU_Status);\r
@@ -413,41 +421,35 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
                        /* Write 8-bit state string ID number */\r
                        Endpoint_Write_Byte(0);\r
 \r
                        /* Write 8-bit state string ID number */\r
                        Endpoint_Write_Byte(0);\r
 \r
-                       Endpoint_ClearSetupIN();\r
+                       Endpoint_ClearIN();\r
                        \r
                        \r
-                       while (!(Endpoint_IsSetupOUTReceived()));\r
-                       Endpoint_ClearSetupOUT();\r
-       \r
+                       Endpoint_ClearStatusStage();\r
                        break;          \r
                case DFU_CLRSTATUS:\r
                        break;          \r
                case DFU_CLRSTATUS:\r
-                       Endpoint_ClearSetupReceived();\r
+                       Endpoint_ClearSETUP();\r
                        \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
+                       Endpoint_ClearStatusStage();\r
                        break;\r
                case DFU_GETSTATE:\r
                        break;\r
                case DFU_GETSTATE:\r
-                       Endpoint_ClearSetupReceived();\r
+                       Endpoint_ClearSETUP();\r
                        \r
                        /* Write the current device state to the endpoint */\r
                        Endpoint_Write_Byte(DFU_State);\r
                \r
                        \r
                        /* Write the current device state to the endpoint */\r
                        Endpoint_Write_Byte(DFU_State);\r
                \r
-                       Endpoint_ClearSetupIN();\r
+                       Endpoint_ClearIN();\r
                        \r
                        \r
-                       while (!(Endpoint_IsSetupOUTReceived()));\r
-                       Endpoint_ClearSetupOUT();\r
-\r
+                       Endpoint_ClearStatusStage();\r
                        break;\r
                case DFU_ABORT:\r
                        break;\r
                case DFU_ABORT:\r
-                       Endpoint_ClearSetupReceived();\r
+                       Endpoint_ClearSETUP();\r
                        \r
                        /* 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
-                       Endpoint_ClearSetupIN();\r
 \r
 \r
+                       Endpoint_ClearStatusStage();\r
                        break;\r
        }\r
 }\r
                        break;\r
        }\r
 }\r
@@ -455,7 +457,7 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
 /** Routine to discard the specified number of bytes from the control endpoint stream. This is used to\r
  *  discard unused bytes in the stream from the host, including the memory program block suffix.\r
  *\r
 /** Routine to discard the specified number of bytes from the control endpoint stream. This is used to\r
  *  discard unused bytes in the stream from the host, including the memory program block suffix.\r
  *\r
- *  \param NumberOfBytes  Number of bytes to discard from the host from the control endpoint\r
+ *  \param[in] NumberOfBytes  Number of bytes to discard from the host from the control endpoint\r
  */\r
 static void DiscardFillerBytes(uint8_t NumberOfBytes)\r
 {\r
  */\r
 static void DiscardFillerBytes(uint8_t NumberOfBytes)\r
 {\r
@@ -463,13 +465,19 @@ static void DiscardFillerBytes(uint8_t NumberOfBytes)
        {\r
                if (!(Endpoint_BytesInEndpoint()))\r
                {\r
        {\r
                if (!(Endpoint_BytesInEndpoint()))\r
                {\r
-                       Endpoint_ClearSetupOUT();\r
+                       Endpoint_ClearOUT();\r
 \r
                        /* Wait until next data packet received */\r
 \r
                        /* Wait until next data packet received */\r
-                       while (!(Endpoint_IsSetupOUTReceived()));\r
+                       while (!(Endpoint_IsOUTReceived()))\r
+                       {                               \r
+                               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                 return;\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       Endpoint_Discard_Byte();\r
                }\r
                }\r
-\r
-               Endpoint_Discard_Byte();                                                \r
        }\r
 }\r
 \r
        }\r
 }\r
 \r
@@ -531,15 +539,15 @@ static void LoadStartEndAddresses(void)
        {\r
                uint8_t  Bytes[2];\r
                uint16_t Word;\r
        {\r
                uint8_t  Bytes[2];\r
                uint16_t Word;\r
-       } Address[2] = {{Bytes: {SentCommand.Data[2], SentCommand.Data[1]}},\r
-                       {Bytes: {SentCommand.Data[4], SentCommand.Data[3]}}};\r
+       } Address[2] = {{.Bytes = {SentCommand.Data[2], SentCommand.Data[1]}},\r
+                       {.Bytes = {SentCommand.Data[4], SentCommand.Data[3]}}};\r
                \r
        /* Load in the start and ending read addresses from the sent data packet */\r
        StartAddr = Address[0].Word;\r
        EndAddr   = Address[1].Word;\r
 }\r
 \r
                \r
        /* Load in the start and ending read addresses from the sent data packet */\r
        StartAddr = Address[0].Word;\r
        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
  *  to write subsequent data from the host into the specified memory.\r
  */\r
 static void ProcessMemProgCommand(void)\r
@@ -557,7 +565,7 @@ static void ProcessMemProgCommand(void)
                        {\r
                                uint16_t Words[2];\r
                                uint32_t Long;\r
                        {\r
                                uint16_t Words[2];\r
                                uint32_t Long;\r
-                       } CurrFlashAddress = {Words: {StartAddr, Flash64KBPage}};\r
+                       } CurrFlashAddress = {.Words = {StartAddr, Flash64KBPage}};\r
                        \r
                        /* Erase the current page's temp buffer */\r
                        boot_page_erase(CurrFlashAddress.Long);\r
                        \r
                        /* Erase the current page's temp buffer */\r
                        boot_page_erase(CurrFlashAddress.Long);\r
@@ -569,7 +577,7 @@ static void ProcessMemProgCommand(void)
        }\r
 }\r
 \r
        }\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
  *  to read subsequent data from the specified memory out to the host, as well as implementing the memory\r
  *  blank check command.\r
  */\r
@@ -591,7 +599,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
@@ -639,7 +647,7 @@ static void ProcessWriteCommand(void)
                                {\r
                                        uint8_t  Bytes[2];\r
                                        AppPtr_t FuncPtr;\r
                                {\r
                                        uint8_t  Bytes[2];\r
                                        AppPtr_t FuncPtr;\r
-                               } Address = {Bytes: {SentCommand.Data[4], SentCommand.Data[3]}};\r
+                               } Address = {.Bytes = {SentCommand.Data[4], SentCommand.Data[3]}};\r
 \r
                                AppStartPtr = Address.FuncPtr;\r
                                \r
 \r
                                AppStartPtr = Address.FuncPtr;\r
                                \r
@@ -677,16 +685,12 @@ 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
        if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x00))                         // Read bootloader info\r
 \r
        uint8_t DataIndexToRead = SentCommand.Data[1];\r
 \r
        if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x00))                         // Read bootloader info\r
-       {\r
-               ResponseByte = BootloaderInfo[DataIndexToRead];\r
-       }\r
+         ResponseByte = BootloaderInfo[DataIndexToRead];\r
        else if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01))                    // Read signature byte\r
        else if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01))                    // Read signature byte\r
-       {\r
-               ResponseByte = SignatureInfo[DataIndexToRead - 0x30];\r
-       }\r
+         ResponseByte = SignatureInfo[DataIndexToRead - 0x30];\r
 }\r
 }\r