Be doubly-certain that the incomming CDC class driver's endpoint/pipe is flushed...
[pub/USBasp.git] / Bootloaders / CDC / BootloaderCDC.c
index 654eabd..482859b 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
 #define  INCLUDE_FROM_BOOTLOADERCDC_C\r
 #include "BootloaderCDC.h"\r
 \r
 #define  INCLUDE_FROM_BOOTLOADERCDC_C\r
 #include "BootloaderCDC.h"\r
 \r
-/* Globals: */\r
 /** Line coding options for the virtual serial port. Although the virtual serial port data is never\r
  *  sent through a physical serial port, the line encoding data must still be read and preserved from\r
  *  the host, or the host will detect a problem and fail to open the port. This structure contains the\r
  *  current encoding options, including baud rate, character format, parity mode and total number of \r
  *  bits in each data chunk.\r
  */\r
 /** Line coding options for the virtual serial port. Although the virtual serial port data is never\r
  *  sent through a physical serial port, the line encoding data must still be read and preserved from\r
  *  the host, or the host will detect a problem and fail to open the port. This structure contains the\r
  *  current encoding options, including baud rate, character format, parity mode and total number of \r
  *  bits in each data chunk.\r
  */\r
-CDC_Line_Coding_t LineCoding = { BaudRateBPS: 9600,\r
-                                 CharFormat:  OneStopBit,\r
-                                 ParityType:  Parity_None,\r
-                                 DataBits:    8            };\r
+CDC_Line_Coding_t LineCoding = { .BaudRateBPS = 9600,\r
+                                 .CharFormat  = OneStopBit,\r
+                                 .ParityType  = Parity_None,\r
+                                 .DataBits    = 8            };\r
 \r
 /** Current address counter. This stores the current address of the FLASH or EEPROM as set by the host,\r
  *  and is used when reading or writing to the AVRs memory (either FLASH or EEPROM depending on the issued\r
  *  command.)\r
  */\r
 \r
 /** Current address counter. This stores the current address of the FLASH or EEPROM as set by the host,\r
  *  and is used when reading or writing to the AVRs memory (either FLASH or EEPROM depending on the issued\r
  *  command.)\r
  */\r
-uint16_t CurrAddress;\r
+uint32_t CurrAddress;\r
 \r
 /** Flag to indicate if the bootloader should be running, or should exit and allow the application code to run\r
  *  via a soft reset. When cleared, the bootloader will abort, the USB interface will shut down and the application\r
 \r
 /** Flag to indicate if the bootloader should be running, or should exit and allow the application code to run\r
  *  via a soft reset. When cleared, the bootloader will abort, the USB interface will shut down and the application\r
@@ -67,12 +66,32 @@ bool RunBootloader = true;
  */\r
 int main(void)\r
 {\r
  */\r
 int main(void)\r
 {\r
+       /* Setup hardware required for the bootloader */\r
+       SetupHardware();\r
+\r
+       while (RunBootloader)\r
+       {\r
+               CDC_Task();\r
+               USB_USBTask();\r
+       }\r
+       \r
+       /* Reset all configured hardware to their default states for the user app */\r
+       ResetHardware();\r
+\r
+       /* Start the user application */\r
+       AppPtr_t AppStartPtr = (AppPtr_t)0x0000;\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
        /* 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
@@ -80,18 +99,11 @@ int main(void)
        \r
        /* Initialize USB Subsystem */\r
        USB_Init();\r
        \r
        /* Initialize USB Subsystem */\r
        USB_Init();\r
+}\r
 \r
 \r
-       while (RunBootloader)\r
-       {\r
-               USB_USBTask();\r
-               CDC_Task();\r
-       }\r
-       \r
-       Endpoint_SelectEndpoint(CDC_TX_EPNUM);\r
-\r
-       /* Wait until any pending transmissions have completed before shutting down */\r
-       while (!(Endpoint_ReadWriteAllowed()));\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
        /* Shut down the USB subsystem */\r
        USB_ShutDown();\r
        \r
@@ -99,36 +111,14 @@ int main(void)
        MCUCR = (1 << IVCE);\r
        MCUCR = 0;\r
 \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
        /* Re-enable RWW section */\r
        boot_rww_enable();\r
        /* Re-enable RWW section */\r
        boot_rww_enable();\r
-\r
-       /* Start the user application */\r
-       AppPtr_t AppStartPtr = (AppPtr_t)0x0000;\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
-EVENT_HANDLER(USB_Disconnect)\r
-{\r
-       /* Upon disconnection, run user application */\r
-       RunBootloader = false;\r
 }\r
 \r
 /** Event handler for the USB_ConfigurationChanged event. This configures the device's endpoints ready\r
  *  to relay data to and from the attached USB host.\r
  */\r
 }\r
 \r
 /** Event handler for the USB_ConfigurationChanged event. This configures the device's endpoints ready\r
  *  to relay data to and from the attached USB host.\r
  */\r
-EVENT_HANDLER(USB_ConfigurationChanged)\r
+void EVENT_USB_Device_ConfigurationChanged(void)\r
 {\r
        /* Setup CDC Notification, Rx and Tx Endpoints */\r
        Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT,\r
 {\r
        /* Setup CDC Notification, Rx and Tx Endpoints */\r
        Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT,\r
@@ -144,58 +134,57 @@ EVENT_HANDLER(USB_ConfigurationChanged)
                                   ENDPOINT_BANK_SINGLE);\r
 }\r
 \r
                                   ENDPOINT_BANK_SINGLE);\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, so that they can be handled appropriately\r
  *  for the application.\r
  */\r
  *  control requests that are not handled internally by the USB library, so that they can be handled appropriately\r
  *  for the application.\r
  */\r
-EVENT_HANDLER(USB_UnhandledControlPacket)\r
+void EVENT_USB_Device_UnhandledControlRequest(void)\r
 {\r
        uint8_t* LineCodingData = (uint8_t*)&LineCoding;\r
 \r
 {\r
        uint8_t* LineCodingData = (uint8_t*)&LineCoding;\r
 \r
-       Endpoint_Discard_Word();\r
-\r
        /* Process CDC specific control requests */\r
        /* Process CDC specific control requests */\r
-       switch (bRequest)\r
+       switch (USB_ControlRequest.bRequest)\r
        {\r
                case REQ_GetLineEncoding:\r
        {\r
                case REQ_GetLineEncoding:\r
-                       if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
+                       if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {\r
                        {\r
-                               Endpoint_ClearSetupReceived();\r
+                               Endpoint_ClearSETUP();\r
 \r
                                for (uint8_t i = 0; i < sizeof(LineCoding); i++)\r
                                  Endpoint_Write_Byte(*(LineCodingData++));     \r
                                \r
 \r
                                for (uint8_t i = 0; i < sizeof(LineCoding); i++)\r
                                  Endpoint_Write_Byte(*(LineCodingData++));     \r
                                \r
-                               Endpoint_ClearSetupIN();\r
+                               Endpoint_ClearIN();\r
                                \r
                                \r
-                               while (!(Endpoint_IsSetupOUTReceived()));\r
-                               Endpoint_ClearSetupOUT();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
                        \r
                        break;\r
                case REQ_SetLineEncoding:\r
                        }\r
                        \r
                        break;\r
                case REQ_SetLineEncoding:\r
-                       if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
+                       if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {\r
                        {\r
-                               Endpoint_ClearSetupReceived();\r
-\r
-                               while (!(Endpoint_IsSetupOUTReceived()));\r
+                               Endpoint_ClearSETUP();\r
 \r
 \r
+                               while (!(Endpoint_IsOUTReceived()))\r
+                               {                               \r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
+                       \r
                                for (uint8_t i = 0; i < sizeof(LineCoding); i++)\r
                                  *(LineCodingData++) = Endpoint_Read_Byte();\r
 \r
                                for (uint8_t i = 0; i < sizeof(LineCoding); i++)\r
                                  *(LineCodingData++) = Endpoint_Read_Byte();\r
 \r
-                               Endpoint_ClearSetupOUT();\r
+                               Endpoint_ClearOUT();\r
 \r
 \r
-                               while (!(Endpoint_IsSetupINReady()));\r
-                               Endpoint_ClearSetupIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
        \r
                        break;\r
                case REQ_SetControlLineState:\r
                        }\r
        \r
                        break;\r
                case REQ_SetControlLineState:\r
-                       if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
+                       if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {\r
                        {\r
-                               Endpoint_ClearSetupReceived();\r
+                               Endpoint_ClearSETUP();\r
                                \r
                                \r
-                               while (!(Endpoint_IsSetupINReady()));\r
-                               Endpoint_ClearSetupIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
        \r
                        break;\r
                        }\r
        \r
                        break;\r
@@ -205,9 +194,9 @@ EVENT_HANDLER(USB_UnhandledControlPacket)
 /** Reads or writes a block of EEPROM or FLASH memory to or from the appropriate CDC data endpoint, depending\r
  *  on the AVR910 protocol command issued.\r
  *\r
 /** Reads or writes a block of EEPROM or FLASH memory to or from the appropriate CDC data endpoint, depending\r
  *  on the AVR910 protocol command issued.\r
  *\r
- *  \param Command  Single character AVR910 protocol command indicating what memory operation to perform\r
+ *  \param[in] Command  Single character AVR910 protocol command indicating what memory operation to perform\r
  */\r
  */\r
-static void ProgramReadWriteMemoryBlock(const uint8_t Command)\r
+static void ReadWriteMemoryBlock(const uint8_t Command)\r
 {\r
        uint16_t BlockSize;\r
        char     MemoryType;\r
 {\r
        uint16_t BlockSize;\r
        char     MemoryType;\r
@@ -220,101 +209,101 @@ static void ProgramReadWriteMemoryBlock(const uint8_t Command)
        \r
        MemoryType =  FetchNextCommandByte();\r
 \r
        \r
        MemoryType =  FetchNextCommandByte();\r
 \r
-       if ((MemoryType == 'E') || (MemoryType == 'F'))\r
+       if ((MemoryType != 'E') && (MemoryType != 'F'))\r
        {\r
        {\r
-               /* Check if command is to read memory */\r
-               if (Command == 'g')\r
-               {\r
-                       /* Re-enable RWW section */\r
-                       boot_rww_enable();\r
+               /* Send error byte back to the host */\r
+               WriteNextResponseByte('?');\r
+               \r
+               return;\r
+       }\r
 \r
 \r
-                       while (BlockSize--)\r
-                       {\r
-                               if (MemoryType == 'E')\r
-                               {\r
-                                       /* Read the next EEPROM byte into the endpoint */\r
-                                       WriteNextResponseByte(eeprom_read_byte((uint8_t*)CurrAddress));\r
+       /* Check if command is to read memory */\r
+       if (Command == 'g')\r
+       {\r
+               /* Re-enable RWW section */\r
+               boot_rww_enable();\r
 \r
 \r
-                                       /* Increment the address counter after use */\r
-                                       CurrAddress++;\r
-                               }\r
-                               else\r
-                               {\r
-                                       /* Read the next FLASH byte from the current FLASH page */\r
-                                       #if defined(RAMPZ)\r
-                                       WriteNextResponseByte(pgm_read_byte_far(((uint32_t)CurrAddress << 1) + HighByte));\r
-                                       #else\r
-                                       WriteNextResponseByte(pgm_read_byte((CurrAddress << 1) + HighByte));                                    \r
-                                       #endif\r
-                                       \r
-                                       /* If both bytes in current word have been read, increment the address counter */\r
-                                       if (HighByte)\r
-                                         CurrAddress++;\r
-                                       \r
-                                       HighByte ^= 1;\r
-                               }\r
-                       }\r
-               }\r
-               else\r
+               while (BlockSize--)\r
                {\r
                {\r
-                       uint32_t PageStartAddress = ((uint32_t)CurrAddress << 1);\r
-       \r
                        if (MemoryType == 'F')\r
                        {\r
                        if (MemoryType == 'F')\r
                        {\r
-                               boot_page_erase(PageStartAddress);\r
-                               boot_spm_busy_wait();\r
+                               /* Read the next FLASH byte from the current FLASH page */\r
+                               #if (FLASHEND > 0xFFFF)\r
+                               WriteNextResponseByte(pgm_read_byte_far(CurrAddress | HighByte));\r
+                               #else\r
+                               WriteNextResponseByte(pgm_read_byte(CurrAddress | HighByte));                                   \r
+                               #endif\r
+                               \r
+                               /* If both bytes in current word have been read, increment the address counter */\r
+                               if (HighByte)\r
+                                 CurrAddress += 2;\r
+                               \r
+                               HighByte = !HighByte;\r
                        }\r
                        }\r
-                       \r
-                       while (BlockSize--)\r
+                       else\r
                        {\r
                        {\r
-                               if (MemoryType == 'E')\r
+                               /* Read the next EEPROM byte into the endpoint */\r
+                               WriteNextResponseByte(eeprom_read_byte((uint8_t*)(uint16_t)(CurrAddress >> 1)));\r
+\r
+                               /* Increment the address counter after use */\r
+                               CurrAddress += 2;\r
+                       }                       \r
+               }\r
+       }\r
+       else\r
+       {\r
+               uint32_t PageStartAddress = CurrAddress;\r
+\r
+               if (MemoryType == 'F')\r
+               {\r
+                       boot_page_erase(PageStartAddress);\r
+                       boot_spm_busy_wait();\r
+               }\r
+               \r
+               while (BlockSize--)\r
+               {\r
+                       if (MemoryType == 'F')\r
+                       {       \r
+                               /* If both bytes in current word have been written, increment the address counter */\r
+                               if (HighByte)\r
                                {\r
                                {\r
-                                       /* Write the next EEPROM byte from the endpoint */\r
-                                       eeprom_write_byte((uint8_t*)CurrAddress, FetchNextCommandByte());                                       \r
+                                       /* Write the next FLASH word to the current FLASH page */\r
+                                       boot_page_fill(CurrAddress, ((FetchNextCommandByte() << 8) | LowByte));\r
 \r
                                        /* Increment the address counter after use */\r
 \r
                                        /* Increment the address counter after use */\r
-                                       CurrAddress++;\r
+                                       CurrAddress += 2;\r
+\r
+                                       HighByte = false;\r
                                }\r
                                else\r
                                }\r
                                else\r
-                               {       \r
-                                       /* If both bytes in current word have been written, increment the address counter */\r
-                                       if (HighByte)\r
-                                       {\r
-                                               /* Write the next FLASH word to the current FLASH page */\r
-                                               boot_page_fill(((uint32_t)CurrAddress << 1), ((FetchNextCommandByte() << 8) | LowByte));\r
-\r
-                                               HighByte = false;\r
-                                               \r
-                                               /* Increment the address counter after use */\r
-                                               CurrAddress++;\r
-                                       }\r
-                                       else\r
-                                       {\r
-                                               LowByte = FetchNextCommandByte();\r
-                                       \r
-                                               HighByte = true;\r
-                                       }\r
+                               {\r
+                                       LowByte = FetchNextCommandByte();\r
+                               \r
+                                       HighByte = true;\r
                                }\r
                        }\r
                                }\r
                        }\r
-\r
-                       /* If in FLASH programming mode, commit the page after writing */\r
-                       if (MemoryType == 'F')\r
+                       else\r
                        {\r
                        {\r
-                               /* Commit the flash page to memory */\r
-                               boot_page_write(PageStartAddress);\r
-                               \r
-                               /* Wait until write operation has completed */\r
-                               boot_spm_busy_wait();\r
+                               /* Write the next EEPROM byte from the endpoint */\r
+                               eeprom_write_byte((uint8_t*)(uint16_t)(CurrAddress >> 1), FetchNextCommandByte());                                      \r
+\r
+                               /* Increment the address counter after use */\r
+                               CurrAddress += 2;\r
                        }\r
                        }\r
-               \r
-                       /* Send response byte back to the host */\r
-                       WriteNextResponseByte('\r');            \r
                }\r
                }\r
-       }\r
-       else\r
-       {\r
-               /* Send error byte back to the host */\r
-               WriteNextResponseByte('?');\r
+\r
+               /* If in FLASH programming mode, commit the page after writing */\r
+               if (MemoryType == 'F')\r
+               {\r
+                       /* Commit the flash page to memory */\r
+                       boot_page_write(PageStartAddress);\r
+                       \r
+                       /* Wait until write operation has completed */\r
+                       boot_spm_busy_wait();\r
+               }\r
+       \r
+               /* Send response byte back to the host */\r
+               WriteNextResponseByte('\r');            \r
        }\r
 }\r
 \r
        }\r
 }\r
 \r
@@ -329,10 +318,15 @@ static uint8_t FetchNextCommandByte(void)
        Endpoint_SelectEndpoint(CDC_RX_EPNUM);\r
        \r
        /* If OUT endpoint empty, clear it and wait for the next packet from the host */\r
        Endpoint_SelectEndpoint(CDC_RX_EPNUM);\r
        \r
        /* If OUT endpoint empty, clear it and wait for the next packet from the host */\r
-       if (!(Endpoint_ReadWriteAllowed()))\r
+       while (!(Endpoint_IsReadWriteAllowed()))\r
        {\r
        {\r
-               Endpoint_ClearCurrentBank();\r
-               while (!(Endpoint_ReadWriteAllowed()));\r
+               Endpoint_ClearOUT();\r
+\r
+               while (!(Endpoint_IsOUTReceived()))\r
+               {\r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return 0;\r
+               }\r
        }\r
        \r
        /* Fetch the next byte from the OUT endpoint */\r
        }\r
        \r
        /* Fetch the next byte from the OUT endpoint */\r
@@ -342,18 +336,23 @@ static uint8_t FetchNextCommandByte(void)
 /** Writes the next response byte to the CDC data IN endpoint, and sends the endpoint back if needed to free up the\r
  *  bank when full ready for the next byte in the packet to the host.\r
  *\r
 /** Writes the next response byte to the CDC data IN endpoint, and sends the endpoint back if needed to free up the\r
  *  bank when full ready for the next byte in the packet to the host.\r
  *\r
- *  \param Response  Next response byte to send to the host\r
+ *  \param[in] Response  Next response byte to send to the host\r
  */\r
 static void WriteNextResponseByte(const uint8_t Response)\r
 {\r
        /* Select the IN endpoint so that the next data byte can be written */\r
        Endpoint_SelectEndpoint(CDC_TX_EPNUM);\r
        \r
  */\r
 static void WriteNextResponseByte(const uint8_t Response)\r
 {\r
        /* Select the IN endpoint so that the next data byte can be written */\r
        Endpoint_SelectEndpoint(CDC_TX_EPNUM);\r
        \r
-       /* If OUT endpoint empty, clear it and wait for the next packet from the host */\r
-       if (!(Endpoint_ReadWriteAllowed()))\r
+       /* If IN endpoint full, clear it and wait until ready for the next packet to the host */\r
+       if (!(Endpoint_IsReadWriteAllowed()))\r
        {\r
        {\r
-               Endpoint_ClearCurrentBank();\r
-               while (!(Endpoint_ReadWriteAllowed()));\r
+               Endpoint_ClearIN();\r
+               \r
+               while (!(Endpoint_IsINReady()))\r
+               {                               \r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return;\r
+               }\r
        }\r
        \r
        /* Write the next byte to the OUT endpoint */\r
        }\r
        \r
        /* Write the next byte to the OUT endpoint */\r
@@ -363,13 +362,13 @@ static void WriteNextResponseByte(const uint8_t Response)
 /** Task to read in AVR910 commands from the CDC data OUT endpoint, process them, perform the required actions\r
  *  and send the appropriate response back to the host.\r
  */\r
 /** Task to read in AVR910 commands from the CDC data OUT endpoint, process them, perform the required actions\r
  *  and send the appropriate response back to the host.\r
  */\r
-TASK(CDC_Task)\r
+void CDC_Task(void)\r
 {\r
        /* Select the OUT endpoint */\r
        Endpoint_SelectEndpoint(CDC_RX_EPNUM);\r
        \r
        /* Check if endpoint has a command in it sent from the host */\r
 {\r
        /* Select the OUT endpoint */\r
        Endpoint_SelectEndpoint(CDC_RX_EPNUM);\r
        \r
        /* Check if endpoint has a command in it sent from the host */\r
-       if (Endpoint_ReadWriteAllowed())\r
+       if (Endpoint_IsOUTReceived())\r
        {\r
                /* Read in the bootloader command (first byte sent from host) */\r
                uint8_t Command = FetchNextCommandByte();\r
        {\r
                /* Read in the bootloader command (first byte sent from host) */\r
                uint8_t Command = FetchNextCommandByte();\r
@@ -399,8 +398,8 @@ TASK(CDC_Task)
                else if (Command == 'A')\r
                {\r
                        /* Set the current address to that given by the host */\r
                else if (Command == 'A')\r
                {\r
                        /* Set the current address to that given by the host */\r
-                       CurrAddress  = (FetchNextCommandByte() << 8);\r
-                       CurrAddress |=  FetchNextCommandByte();\r
+                       CurrAddress   = (FetchNextCommandByte() << 9);\r
+                       CurrAddress  |= (FetchNextCommandByte() << 1);\r
 \r
                        /* Send confirmation byte back to the host */\r
                        WriteNextResponseByte('\r');\r
 \r
                        /* Send confirmation byte back to the host */\r
                        WriteNextResponseByte('\r');\r
@@ -423,9 +422,9 @@ TASK(CDC_Task)
                }\r
                else if (Command == 's')\r
                {\r
                }\r
                else if (Command == 's')\r
                {\r
-                       WriteNextResponseByte(boot_signature_byte_get(4));\r
-                       WriteNextResponseByte(boot_signature_byte_get(2));\r
-                       WriteNextResponseByte(boot_signature_byte_get(0));              \r
+                       WriteNextResponseByte(AVR_SIGNATURE_3);         \r
+                       WriteNextResponseByte(AVR_SIGNATURE_2);\r
+                       WriteNextResponseByte(AVR_SIGNATURE_1);\r
                }\r
                else if (Command == 'b')\r
                {\r
                }\r
                else if (Command == 'b')\r
                {\r
@@ -475,24 +474,29 @@ TASK(CDC_Task)
                {\r
                        WriteNextResponseByte(boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS));         \r
                }\r
                {\r
                        WriteNextResponseByte(boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS));         \r
                }\r
-               else if ((Command == 'C') || (Command == 'c'))\r
-               {\r
-                       if (Command == 'c')\r
-                       {\r
-                               /* Increment the address if the second byte is being written */\r
-                               CurrAddress++;\r
-                       }\r
-                       \r
+               else if (Command == 'C')\r
+               {                       \r
                        /* Write the high byte to the current flash page */\r
                        /* Write the high byte to the current flash page */\r
-                       boot_page_fill(((uint32_t)CurrAddress << 1), FetchNextCommandByte());\r
+                       boot_page_fill(CurrAddress, FetchNextCommandByte());\r
+\r
+                       /* Send confirmation byte back to the host */\r
+                       WriteNextResponseByte('\r');            \r
+               }\r
+               else if (Command == 'c')\r
+               {                       \r
+                       /* Write the low byte to the current flash page */\r
+                       boot_page_fill(CurrAddress | 1, FetchNextCommandByte());\r
                        \r
                        \r
+                       /* Increment the address */\r
+                       CurrAddress += 2;\r
+\r
                        /* Send confirmation byte back to the host */\r
                        WriteNextResponseByte('\r');            \r
                }\r
                else if (Command == 'm')\r
                {\r
                        /* Commit the flash page to memory */\r
                        /* Send confirmation byte back to the host */\r
                        WriteNextResponseByte('\r');            \r
                }\r
                else if (Command == 'm')\r
                {\r
                        /* Commit the flash page to memory */\r
-                       boot_page_write((uint32_t)CurrAddress << 1);\r
+                       boot_page_write(CurrAddress);\r
                        \r
                        /* Wait until write operation has completed */\r
                        boot_spm_busy_wait();\r
                        \r
                        /* Wait until write operation has completed */\r
                        boot_spm_busy_wait();\r
@@ -502,15 +506,15 @@ TASK(CDC_Task)
                }\r
                else if ((Command == 'B') || (Command == 'g'))\r
                {\r
                }\r
                else if ((Command == 'B') || (Command == 'g'))\r
                {\r
-                       /* Delegate the block write/read to a seperate function for clarity */\r
-                       ProgramReadWriteMemoryBlock(Command);\r
+                       /* Delegate the block write/read to a separate function for clarity */\r
+                       ReadWriteMemoryBlock(Command);\r
                }\r
                else if (Command == 'R')\r
                {\r
                }\r
                else if (Command == 'R')\r
                {\r
-                       #if defined(RAMPZ)\r
-                       uint16_t ProgramWord = pgm_read_word_far(((uint32_t)CurrAddress << 1));\r
+                       #if (FLASHEND > 0xFFFF)\r
+                       uint16_t ProgramWord = pgm_read_word_far(CurrAddress);\r
                        #else\r
                        #else\r
-                       uint16_t ProgramWord = pgm_read_word(CurrAddress << 1);                 \r
+                       uint16_t ProgramWord = pgm_read_word(CurrAddress);                      \r
                        #endif\r
                        \r
                        WriteNextResponseByte(ProgramWord >> 8);\r
                        #endif\r
                        \r
                        WriteNextResponseByte(ProgramWord >> 8);\r
@@ -519,10 +523,10 @@ TASK(CDC_Task)
                else if (Command == 'D')\r
                {\r
                        /* Read the byte from the endpoint and write it to the EEPROM */\r
                else if (Command == 'D')\r
                {\r
                        /* Read the byte from the endpoint and write it to the EEPROM */\r
-                       eeprom_write_byte((uint8_t*)CurrAddress, FetchNextCommandByte());\r
+                       eeprom_write_byte((uint8_t*)((uint16_t)(CurrAddress >> 1)), FetchNextCommandByte());\r
                        \r
                        /* Increment the address after use */                   \r
                        \r
                        /* Increment the address after use */                   \r
-                       CurrAddress++;\r
+                       CurrAddress += 2;\r
        \r
                        /* Send confirmation byte back to the host */\r
                        WriteNextResponseByte('\r');            \r
        \r
                        /* Send confirmation byte back to the host */\r
                        WriteNextResponseByte('\r');            \r
@@ -530,10 +534,10 @@ TASK(CDC_Task)
                else if (Command == 'd')\r
                {\r
                        /* Read the EEPROM byte and write it to the endpoint */\r
                else if (Command == 'd')\r
                {\r
                        /* Read the EEPROM byte and write it to the endpoint */\r
-                       WriteNextResponseByte(eeprom_read_byte((uint8_t*)CurrAddress));\r
+                       WriteNextResponseByte(eeprom_read_byte((uint8_t*)((uint16_t)(CurrAddress >> 1))));\r
 \r
                        /* Increment the address after use */\r
 \r
                        /* Increment the address after use */\r
-                       CurrAddress++;\r
+                       CurrAddress += 2;\r
                }\r
                else if (Command == 27)\r
                {\r
                }\r
                else if (Command == 27)\r
                {\r
@@ -549,22 +553,34 @@ TASK(CDC_Task)
                Endpoint_SelectEndpoint(CDC_TX_EPNUM);\r
 \r
                /* Remember if the endpoint is completely full before clearing it */\r
                Endpoint_SelectEndpoint(CDC_TX_EPNUM);\r
 \r
                /* Remember if the endpoint is completely full before clearing it */\r
-               bool IsEndpointFull = !(Endpoint_ReadWriteAllowed());\r
+               bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed());\r
 \r
                /* Send the endpoint data to the host */\r
 \r
                /* Send the endpoint data to the host */\r
-               Endpoint_ClearCurrentBank();\r
+               Endpoint_ClearIN();\r
                \r
                /* If a full endpoint's worth of data was sent, we need to send an empty packet afterwards to signal end of transfer */\r
                if (IsEndpointFull)\r
                {\r
                \r
                /* If a full endpoint's worth of data was sent, we need to send an empty packet afterwards to signal end of transfer */\r
                if (IsEndpointFull)\r
                {\r
-                       while (!(Endpoint_ReadWriteAllowed()));\r
-                       Endpoint_ClearCurrentBank();\r
+                       while (!(Endpoint_IsINReady()))\r
+                       {                               \r
+                               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                 return;\r
+                       }\r
+\r
+                       Endpoint_ClearIN();\r
+               }\r
+\r
+               /* Wait until the data has been sent to the host */\r
+               while (!(Endpoint_IsINReady()))\r
+               {                               \r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return;\r
                }\r
                \r
                /* Select the OUT endpoint */\r
                Endpoint_SelectEndpoint(CDC_RX_EPNUM);\r
 \r
                /* Acknowledge the command from the host */\r
                }\r
                \r
                /* Select the OUT endpoint */\r
                Endpoint_SelectEndpoint(CDC_RX_EPNUM);\r
 \r
                /* Acknowledge the command from the host */\r
-               Endpoint_ClearCurrentBank();\r
+               Endpoint_ClearOUT();\r
        }\r
 }\r
        }\r
 }\r