Added new Benito project, a simple modified USB-to-Serial bridge for the programming...
[pub/USBasp.git] / Bootloaders / CDC / BootloaderCDC.c
index 92c2291..cbcfd7e 100644 (file)
@@ -165,9 +165,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                \r
                                Endpoint_ClearIN();\r
                                \r
                                \r
                                Endpoint_ClearIN();\r
                                \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsOUTReceived()));\r
-                               Endpoint_ClearOUT();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
                        \r
                        break;\r
                        }\r
                        \r
                        break;\r
@@ -176,16 +174,18 @@ void EVENT_USB_UnhandledControlPacket(void)
                        {\r
                                Endpoint_ClearSETUP();\r
 \r
                        {\r
                                Endpoint_ClearSETUP();\r
 \r
-                               while (!(Endpoint_IsOUTReceived()));\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
                                Endpoint_ClearOUT();\r
 \r
                                for (uint8_t i = 0; i < sizeof(LineCoding); i++)\r
                                  *(LineCodingData++) = Endpoint_Read_Byte();\r
 \r
                                Endpoint_ClearOUT();\r
 \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
        \r
                        break;\r
                        }\r
        \r
                        break;\r
@@ -194,9 +194,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                        {\r
                                Endpoint_ClearSETUP();\r
                                \r
                        {\r
                                Endpoint_ClearSETUP();\r
                                \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
        \r
                        break;\r
                        }\r
        \r
                        break;\r
@@ -206,7 +204,7 @@ void EVENT_USB_UnhandledControlPacket(void)
 /** 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
 static void ReadWriteMemoryBlock(const uint8_t Command)\r
 {\r
  */\r
 static void ReadWriteMemoryBlock(const uint8_t Command)\r
 {\r
@@ -333,7 +331,12 @@ static uint8_t FetchNextCommandByte(void)
        while (!(Endpoint_IsReadWriteAllowed()))\r
        {\r
                Endpoint_ClearOUT();\r
        while (!(Endpoint_IsReadWriteAllowed()))\r
        {\r
                Endpoint_ClearOUT();\r
-               while (!(Endpoint_IsOUTReceived()));\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
@@ -343,7 +346,7 @@ 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
  */\r
 static void WriteNextResponseByte(const uint8_t Response)\r
 {\r
@@ -354,7 +357,12 @@ static void WriteNextResponseByte(const uint8_t Response)
        if (!(Endpoint_IsReadWriteAllowed()))\r
        {\r
                Endpoint_ClearIN();\r
        if (!(Endpoint_IsReadWriteAllowed()))\r
        {\r
                Endpoint_ClearIN();\r
-               while (!(Endpoint_IsINReady()));\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
@@ -563,12 +571,21 @@ void CDC_Task(void)
                /* 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
                /* 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_IsINReady()));\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
                        Endpoint_ClearIN();\r
                }\r
 \r
                /* Wait until the data has been sent to the host */\r
-               while (!(Endpoint_IsINReady()));\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
                /* Select the OUT endpoint */\r
                Endpoint_SelectEndpoint(CDC_RX_EPNUM);\r