Added new USB_DeviceState variable to keep track of the current Device mode USB state.
authorDean Camera <dean@fourwalledcubicle.com>
Tue, 21 Jul 2009 13:31:21 +0000 (13:31 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Tue, 21 Jul 2009 13:31:21 +0000 (13:31 +0000)
Added new Endpoint_ClearStatusStage() convenience function to assist with the status stages of control transfers.

Removed vague USB_IsConnected global - test USB_DeviceState or USB_HostState explicitly to gain previous functionality.

Removed USB_IsSuspended global - test USB_DeviceState against DEVICE_STATE_Suspended instead.

Fixed possible enumeration errors from spinloops which may fail to exit if the USB connection is severed before the exit condition becomes true.

58 files changed:
Bootloaders/CDC/BootloaderCDC.c
Bootloaders/DFU/BootloaderDFU.c
Bootloaders/TeensyHID/TeensyHID.c
Demos/Device/ClassDriver/MassStorage/Lib/DataflashManager.c
Demos/Device/ClassDriver/USBtoSerial/USBtoSerial.c
Demos/Device/Incomplete/Sideshow/Sideshow.c
Demos/Device/LowLevel/AudioInput/AudioInput.c
Demos/Device/LowLevel/AudioOutput/AudioOutput.c
Demos/Device/LowLevel/CDC/CDC.c
Demos/Device/LowLevel/DualCDC/DualCDC.c
Demos/Device/LowLevel/GenericHID/GenericHID.c
Demos/Device/LowLevel/Joystick/Joystick.c
Demos/Device/LowLevel/Keyboard/Keyboard.c
Demos/Device/LowLevel/KeyboardMouse/KeyboardMouse.c
Demos/Device/LowLevel/MIDI/MIDI.c
Demos/Device/LowLevel/MassStorage/Lib/DataflashManager.c
Demos/Device/LowLevel/MassStorage/MassStorage.c
Demos/Device/LowLevel/Mouse/Mouse.c
Demos/Device/LowLevel/RNDISEthernet/RNDISEthernet.c
Demos/Device/LowLevel/USBtoSerial/USBtoSerial.c
Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothHCICommands.c
Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c
Demos/Host/LowLevel/CDCHost/CDCHost.c
Demos/Host/LowLevel/GenericHIDHost/GenericHIDHost.c
Demos/Host/LowLevel/KeyboardHost/KeyboardHost.c
Demos/Host/LowLevel/KeyboardHostWithParser/KeyboardHostWithParser.c
Demos/Host/LowLevel/MassStorageHost/Lib/MassStoreCommands.c
Demos/Host/LowLevel/MassStorageHost/MassStorageHost.c
Demos/Host/LowLevel/MouseHost/MouseHost.c
Demos/Host/LowLevel/MouseHostWithParser/MouseHostWithParser.c
Demos/Host/LowLevel/PrinterHost/Lib/PrinterCommands.c
Demos/Host/LowLevel/PrinterHost/PrinterHost.c
Demos/Host/LowLevel/StillImageHost/Lib/StillImageCommands.c
Demos/Host/LowLevel/StillImageHost/StillImageHost.c
LUFA/Drivers/USB/Class/Device/Audio.c
LUFA/Drivers/USB/Class/Device/CDC.c
LUFA/Drivers/USB/Class/Device/HID.c
LUFA/Drivers/USB/Class/Device/MIDI.c
LUFA/Drivers/USB/Class/Device/MassStorage.c
LUFA/Drivers/USB/Class/Device/RNDIS.c
LUFA/Drivers/USB/HighLevel/Events.h
LUFA/Drivers/USB/HighLevel/USBInterrupt.c
LUFA/Drivers/USB/HighLevel/USBTask.c
LUFA/Drivers/USB/HighLevel/USBTask.h
LUFA/Drivers/USB/LowLevel/DevChapter9.c
LUFA/Drivers/USB/LowLevel/Device.h
LUFA/Drivers/USB/LowLevel/Endpoint.c
LUFA/Drivers/USB/LowLevel/Endpoint.h
LUFA/Drivers/USB/LowLevel/Host.c
LUFA/Drivers/USB/LowLevel/Host.h
LUFA/Drivers/USB/LowLevel/LowLevel.c
LUFA/Drivers/USB/LowLevel/Pipe.c
LUFA/Drivers/USB/LowLevel/Template/Template_Endpoint_Control_R.c
LUFA/Drivers/USB/LowLevel/Template/Template_Endpoint_Control_W.c
LUFA/ManPages/ChangeLog.txt
LUFA/ManPages/CompileTimeTokens.txt
LUFA/ManPages/MigrationInformation.txt
Projects/MissileLauncher/MissileLauncher.c

index d875b84..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
@@ -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
@@ -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
index 2086a91..f9c8195 100644 (file)
@@ -177,7 +177,11 @@ void EVENT_USB_UnhandledControlPacket(void)
                        /* 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_IsOUTReceived()));\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
@@ -235,7 +239,12 @@ void EVENT_USB_UnhandledControlPacket(void)
                                                        if (!(Endpoint_BytesInEndpoint()))\r
                                                        {\r
                                                                Endpoint_ClearOUT();\r
                                                        if (!(Endpoint_BytesInEndpoint()))\r
                                                        {\r
                                                                Endpoint_ClearOUT();\r
-                                                               while (!(Endpoint_IsOUTReceived()));\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
@@ -279,7 +288,12 @@ void EVENT_USB_UnhandledControlPacket(void)
                                                        if (!(Endpoint_BytesInEndpoint()))\r
                                                        {\r
                                                                Endpoint_ClearOUT();\r
                                                        if (!(Endpoint_BytesInEndpoint()))\r
                                                        {\r
                                                                Endpoint_ClearOUT();\r
-                                                               while (!(Endpoint_IsOUTReceived()));\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
@@ -297,16 +311,18 @@ void EVENT_USB_UnhandledControlPacket(void)
 \r
                        Endpoint_ClearOUT();\r
 \r
 \r
                        Endpoint_ClearOUT();\r
 \r
-                       /* Acknowledge status stage */\r
-                       while (!(Endpoint_IsINReady()));\r
-                       Endpoint_ClearIN();\r
-                               \r
+                       Endpoint_ClearStatusStage();\r
+\r
                        break;\r
                case DFU_UPLOAD:\r
                        Endpoint_ClearSETUP();\r
 \r
                        break;\r
                case DFU_UPLOAD:\r
                        Endpoint_ClearSETUP();\r
 \r
-                       while (!(Endpoint_IsINReady()));\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
@@ -343,7 +359,12 @@ void EVENT_USB_UnhandledControlPacket(void)
                                                if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE)\r
                                                {\r
                                                        Endpoint_ClearIN();\r
                                                if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE)\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
                                                /* 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
@@ -368,7 +389,12 @@ void EVENT_USB_UnhandledControlPacket(void)
                                                if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE)\r
                                                {\r
                                                        Endpoint_ClearIN();\r
                                                if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE)\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
                                                /* 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
@@ -385,10 +411,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
-\r
+                       Endpoint_ClearStatusStage();\r
                        break;\r
                case DFU_GETSTATUS:\r
                        Endpoint_ClearSETUP();\r
                        break;\r
                case DFU_GETSTATUS:\r
                        Endpoint_ClearSETUP();\r
@@ -408,10 +431,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
-       \r
+                       Endpoint_ClearStatusStage();\r
                        break;          \r
                case DFU_CLRSTATUS:\r
                        Endpoint_ClearSETUP();\r
                        break;          \r
                case DFU_CLRSTATUS:\r
                        Endpoint_ClearSETUP();\r
@@ -419,10 +439,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                        /* 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
-                       /* Acknowledge status stage */\r
-                       while (!(Endpoint_IsINReady()));\r
-                       Endpoint_ClearIN();\r
-                       \r
+                       Endpoint_ClearStatusStage();\r
                        break;\r
                case DFU_GETSTATE:\r
                        Endpoint_ClearSETUP();\r
                        break;\r
                case DFU_GETSTATE:\r
                        Endpoint_ClearSETUP();\r
@@ -432,21 +449,15 @@ 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
-\r
+                       Endpoint_ClearStatusStage();\r
                        break;\r
                case DFU_ABORT:\r
                        Endpoint_ClearSETUP();\r
                        \r
                        /* Reset the current state variable to the default idle state */\r
                        DFU_State = dfuIDLE;\r
                        break;\r
                case DFU_ABORT:\r
                        Endpoint_ClearSETUP();\r
                        \r
                        /* Reset the current state variable to the default idle state */\r
                        DFU_State = dfuIDLE;\r
-                       \r
-                       /* Acknowledge status stage */\r
-                       while (!(Endpoint_IsINReady()));\r
-                       Endpoint_ClearIN();\r
 \r
 \r
+                       Endpoint_ClearStatusStage();\r
                        break;\r
        }\r
 }\r
                        break;\r
        }\r
 }\r
@@ -465,7 +476,11 @@ static void DiscardFillerBytes(uint8_t NumberOfBytes)
                        Endpoint_ClearOUT();\r
 \r
                        /* Wait until next data packet received */\r
                        Endpoint_ClearOUT();\r
 \r
                        /* Wait until next data packet received */\r
-                       while (!(Endpoint_IsOUTReceived()));\r
+                       while (!(Endpoint_IsOUTReceived()))\r
+                       {                               \r
+                               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                 return;\r
+                       }\r
                }\r
                else\r
                {\r
                }\r
                else\r
                {\r
index 12d5c0d..eed7097 100644 (file)
@@ -119,7 +119,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                \r
                                /* Wait until the command (report) has been sent by the host */\r
                                while (!(Endpoint_IsOUTReceived()));\r
                                \r
                                /* Wait until the command (report) has been sent by the host */\r
                                while (!(Endpoint_IsOUTReceived()));\r
-\r
+                       \r
                                /* Read in the write destination address */\r
                                uint16_t PageAddress = Endpoint_Read_Word_LE();\r
                                \r
                                /* Read in the write destination address */\r
                                uint16_t PageAddress = Endpoint_Read_Word_LE();\r
                                \r
@@ -158,9 +158,7 @@ void EVENT_USB_UnhandledControlPacket(void)
 \r
                                Endpoint_ClearOUT();\r
 \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
index 1c584e7..5f12d02 100644 (file)
@@ -69,7 +69,11 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, co
        Dataflash_SendAddressBytes(0, CurrDFPageByte);\r
 \r
        /* Wait until endpoint is ready before continuing */\r
        Dataflash_SendAddressBytes(0, CurrDFPageByte);\r
 \r
        /* Wait until endpoint is ready before continuing */\r
-       while (!(Endpoint_IsReadWriteAllowed()));\r
+       while (!(Endpoint_IsReadWriteAllowed()))\r
+       {\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return;\r
+       }\r
 \r
        while (TotalBlocks)\r
        {\r
 \r
        while (TotalBlocks)\r
        {\r
@@ -85,7 +89,11 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, co
                                Endpoint_ClearOUT();\r
                                \r
                                /* Wait until the host has sent another packet */\r
                                Endpoint_ClearOUT();\r
                                \r
                                /* Wait until the host has sent another packet */\r
-                               while (!(Endpoint_IsReadWriteAllowed()));\r
+                               while (!(Endpoint_IsReadWriteAllowed()))\r
+                               {\r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
                        }\r
 \r
                        /* Check if end of dataflash page reached */\r
                        }\r
 \r
                        /* Check if end of dataflash page reached */\r
@@ -197,7 +205,11 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, con
        Dataflash_SendByte(0x00);\r
        \r
        /* Wait until endpoint is ready before continuing */\r
        Dataflash_SendByte(0x00);\r
        \r
        /* Wait until endpoint is ready before continuing */\r
-       while (!(Endpoint_IsReadWriteAllowed()));\r
+       while (!(Endpoint_IsReadWriteAllowed()))\r
+       {\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return;\r
+       }\r
        \r
        while (TotalBlocks)\r
        {\r
        \r
        while (TotalBlocks)\r
        {\r
@@ -213,7 +225,11 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, con
                                Endpoint_ClearIN();\r
                                \r
                                /* Wait until the endpoint is ready for more data */\r
                                Endpoint_ClearIN();\r
                                \r
                                /* Wait until the endpoint is ready for more data */\r
-                               while (!(Endpoint_IsReadWriteAllowed()));\r
+                               while (!(Endpoint_IsReadWriteAllowed()))\r
+                               {\r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
                        }\r
                        \r
                        /* Check if end of dataflash page reached */\r
                        }\r
                        \r
                        /* Check if end of dataflash page reached */\r
index ac63f0f..8953706 100644 (file)
@@ -152,7 +152,7 @@ void EVENT_USB_UnhandledControlPacket(void)
  */\r
 ISR(USART1_RX_vect, ISR_BLOCK)\r
 {\r
  */\r
 ISR(USART1_RX_vect, ISR_BLOCK)\r
 {\r
-       if (USB_IsConnected)\r
+       if (USB_DeviceState == DEVICE_STATE_Configured)\r
          Buffer_StoreElement(&Tx_Buffer, UDR1);\r
 }\r
 \r
          Buffer_StoreElement(&Tx_Buffer, UDR1);\r
 }\r
 \r
index cc8bd51..d67fae0 100644 (file)
@@ -142,7 +142,7 @@ void EVENT_USB_UnhandledControlPacket(void)
 void SideShow_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
 void SideShow_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        /* Select the SideShow data out endpoint */\r
          return;\r
 \r
        /* Select the SideShow data out endpoint */\r
index 5656b05..bf79016 100644 (file)
@@ -139,9 +139,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */\r
                                StreamingAudioInterfaceSelected = ((USB_ControlRequest.wValue) != 0);\r
                                \r
                                /* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */\r
                                StreamingAudioInterfaceSelected = ((USB_ControlRequest.wValue) != 0);\r
                                \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
 \r
                        break;\r
                        }\r
 \r
                        break;\r
@@ -152,7 +150,7 @@ void EVENT_USB_UnhandledControlPacket(void)
 void USB_Audio_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
 void USB_Audio_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        /* Check to see if the streaming interface is selected, if not the host is not receiving audio */\r
          return;\r
 \r
        /* Check to see if the streaming interface is selected, if not the host is not receiving audio */\r
index 6d22009..89c4446 100644 (file)
@@ -166,9 +166,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */\r
                                StreamingAudioInterfaceSelected = ((USB_ControlRequest.wValue) != 0);\r
                                \r
                                /* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */\r
                                StreamingAudioInterfaceSelected = ((USB_ControlRequest.wValue) != 0);\r
                                \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
 \r
                        break;\r
                        }\r
 \r
                        break;\r
@@ -181,7 +179,7 @@ void EVENT_USB_UnhandledControlPacket(void)
 void USB_Audio_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
 void USB_Audio_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        /* Check to see if the streaming interface is selected, if not the host is not receiving audio */\r
          return;\r
 \r
        /* Check to see if the streaming interface is selected, if not the host is not receiving audio */\r
index d8263b1..e63257d 100644 (file)
@@ -56,12 +56,15 @@ CDC_Line_Coding_t LineCoding = { .BaudRateBPS = 9600,
  */\r
        \r
 static int CDC_putchar (char c, FILE *stream)\r
  */\r
        \r
 static int CDC_putchar (char c, FILE *stream)\r
-{\r
-       if (!(USB_IsConnected))\r
-         return -1;\r
-         \r
+{        \r
        Endpoint_SelectEndpoint(CDC_TX_EPNUM);\r
        Endpoint_SelectEndpoint(CDC_TX_EPNUM);\r
-       while (!(Endpoint_IsReadWriteAllowed()));\r
+\r
+       while (!(Endpoint_IsReadWriteAllowed()))\r
+       {\r
+               if (USB_DeviceState != DEVICE_STATE_Configured)\r
+                 return -1;\r
+       }\r
+\r
        Endpoint_Write_Byte(c);\r
        Endpoint_ClearIN();\r
        \r
        Endpoint_Write_Byte(c);\r
        Endpoint_ClearIN();\r
        \r
@@ -76,10 +79,11 @@ static int CDC_getchar (FILE *stream)
        \r
        for (;;)\r
        {\r
        \r
        for (;;)\r
        {\r
-               if (!(USB_IsConnected))\r
-                 return -1;\r
-\r
-               while (!(Endpoint_IsReadWriteAllowed()));\r
+               while (!(Endpoint_IsReadWriteAllowed()))\r
+               {\r
+                       if (USB_DeviceState != DEVICE_STATE_Configured)\r
+                         return -1;\r
+               }\r
        \r
                if (!(Endpoint_BytesInEndpoint()))\r
                {\r
        \r
                if (!(Endpoint_BytesInEndpoint()))\r
                {\r
@@ -229,9 +233,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                                 CONTROL_LINE_OUT_* masks to determine the RTS and DTR line states using the following code:\r
                                */\r
                                \r
                                                 CONTROL_LINE_OUT_* masks to determine the RTS and DTR line states using the following code:\r
                                */\r
                                \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
        \r
                        break;\r
                        }\r
        \r
                        break;\r
@@ -244,18 +246,17 @@ void CDC_Task(void)
        char*       ReportString    = NULL;\r
        uint8_t     JoyStatus_LCL   = Joystick_GetStatus();\r
        static bool ActionSent      = false;\r
        char*       ReportString    = NULL;\r
        uint8_t     JoyStatus_LCL   = Joystick_GetStatus();\r
        static bool ActionSent      = false;\r
-\r
-       char* JoystickStrings[] =\r
-               {\r
-                       "Joystick Up\r\n",\r
-                       "Joystick Down\r\n",\r
-                       "Joystick Left\r\n",\r
-                       "Joystick Right\r\n",\r
-                       "Joystick Pressed\r\n",\r
-               };\r
+       char*       JoystickStrings[] =\r
+                                       {\r
+                                               "Joystick Up\r\n",\r
+                                               "Joystick Down\r\n",\r
+                                               "Joystick Left\r\n",\r
+                                               "Joystick Right\r\n",\r
+                                               "Joystick Pressed\r\n",\r
+                                       };\r
        \r
        /* Device must be connected and configured for the task to run */\r
        \r
        /* Device must be connected and configured for the task to run */\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
          \r
 #if 0\r
          return;\r
          \r
 #if 0\r
@@ -319,7 +320,11 @@ void CDC_Task(void)
                if (IsFull)\r
                {\r
                        /* Wait until the endpoint is ready for another packet */\r
                if (IsFull)\r
                {\r
                        /* Wait until the endpoint is ready for another packet */\r
-                       while (!(Endpoint_IsINReady()));\r
+                       while (!(Endpoint_IsINReady()))\r
+                       {\r
+                               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                 return;\r
+                       }\r
                        \r
                        /* Send an empty packet to ensure that the host does not buffer data sent to it */\r
                        Endpoint_ClearIN();\r
                        \r
                        /* Send an empty packet to ensure that the host does not buffer data sent to it */\r
                        Endpoint_ClearIN();\r
index 862945e..dd278d5 100644 (file)
@@ -211,9 +211,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Acknowledge the SETUP packet, ready for data transfer */\r
                                Endpoint_ClearSETUP();\r
                                \r
                                /* Acknowledge the SETUP packet, ready for data transfer */\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
@@ -228,20 +226,19 @@ void CDC1_Task(void)
        char*       ReportString    = NULL;\r
        uint8_t     JoyStatus_LCL   = Joystick_GetStatus();\r
        static bool ActionSent      = false;\r
        char*       ReportString    = NULL;\r
        uint8_t     JoyStatus_LCL   = Joystick_GetStatus();\r
        static bool ActionSent      = false;\r
-\r
+       char*       JoystickStrings[] =\r
+                                       {\r
+                                               "Joystick Up\r\n",\r
+                                               "Joystick Down\r\n",\r
+                                               "Joystick Left\r\n",\r
+                                               "Joystick Right\r\n",\r
+                                               "Joystick Pressed\r\n",\r
+                                       };\r
+       \r
        /* Device must be connected and configured for the task to run */\r
        /* Device must be connected and configured for the task to run */\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
          return;\r
 \r
-       char* JoystickStrings[] =\r
-               {\r
-                       "Joystick Up\r\n",\r
-                       "Joystick Down\r\n",\r
-                       "Joystick Left\r\n",\r
-                       "Joystick Right\r\n",\r
-                       "Joystick Pressed\r\n",\r
-               };\r
-\r
        /* Determine if a joystick action has occurred */\r
        if (JoyStatus_LCL & JOY_UP)\r
          ReportString = JoystickStrings[0];\r
        /* Determine if a joystick action has occurred */\r
        if (JoyStatus_LCL & JOY_UP)\r
          ReportString = JoystickStrings[0];\r
@@ -273,7 +270,11 @@ void CDC1_Task(void)
                Endpoint_ClearIN();\r
 \r
                /* Wait until the endpoint is ready for another packet */\r
                Endpoint_ClearIN();\r
 \r
                /* Wait until the endpoint is ready for another packet */\r
-               while (!(Endpoint_IsINReady()));\r
+               while (!(Endpoint_IsINReady()))\r
+               {\r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return;\r
+               }\r
                \r
                /* Send an empty packet to ensure that the host does not buffer data sent to it */\r
                Endpoint_ClearIN();\r
                \r
                /* Send an empty packet to ensure that the host does not buffer data sent to it */\r
                Endpoint_ClearIN();\r
@@ -293,7 +294,7 @@ void CDC1_Task(void)
 void CDC2_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
 void CDC2_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        /* Select the Serial Rx Endpoint */\r
          return;\r
 \r
        /* Select the Serial Rx Endpoint */\r
@@ -324,7 +325,11 @@ void CDC2_Task(void)
                Endpoint_ClearIN();\r
 \r
                /* Wait until the endpoint is ready for the next packet */\r
                Endpoint_ClearIN();\r
 \r
                /* Wait until the endpoint is ready for the next packet */\r
-               while (!(Endpoint_IsINReady()));\r
+               while (!(Endpoint_IsINReady()))\r
+               {\r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return;\r
+               }\r
 \r
                /* Send an empty packet to prevent host buffering */\r
                Endpoint_ClearIN();\r
 \r
                /* Send an empty packet to prevent host buffering */\r
                Endpoint_ClearIN();\r
index 5c6d1ee..34c991f 100644 (file)
@@ -148,7 +148,11 @@ void EVENT_USB_UnhandledControlPacket(void)
                                Endpoint_ClearSETUP();\r
                                \r
                                /* Wait until the generic report has been sent by the host */\r
                                Endpoint_ClearSETUP();\r
                                \r
                                /* Wait until the generic report has been sent by the host */\r
-                               while (!(Endpoint_IsOUTReceived()));\r
+                               while (!(Endpoint_IsOUTReceived()))\r
+                               {\r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
 \r
                                Endpoint_Read_Control_Stream_LE(&GenericData, sizeof(GenericData));\r
 \r
 \r
                                Endpoint_Read_Control_Stream_LE(&GenericData, sizeof(GenericData));\r
 \r
@@ -158,7 +162,11 @@ void EVENT_USB_UnhandledControlPacket(void)
                                Endpoint_ClearOUT();\r
 \r
                                /* Wait until the host is ready to receive the request confirmation */\r
                                Endpoint_ClearOUT();\r
 \r
                                /* Wait until the host is ready to receive the request confirmation */\r
-                               while (!(Endpoint_IsINReady()));\r
+                               while (!(Endpoint_IsINReady()))\r
+                               {\r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
                                \r
                                /* Handshake the request by sending an empty IN packet */\r
                                Endpoint_ClearIN();\r
                                \r
                                /* Handshake the request by sending an empty IN packet */\r
                                Endpoint_ClearIN();\r
@@ -203,7 +211,7 @@ void CreateGenericHIDReport(uint8_t* DataArray)
 void HID_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
 void HID_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);\r
          return;\r
 \r
        Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);\r
index e117475..db2415e 100644 (file)
@@ -182,7 +182,7 @@ bool GetNextReport(USB_JoystickReport_Data_t* ReportData)
 void HID_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
 void HID_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
   \r
        /* Select the Joystick Report Endpoint */\r
          return;\r
   \r
        /* Select the Joystick Report Endpoint */\r
index 9950484..e27b228 100644 (file)
@@ -172,7 +172,11 @@ void EVENT_USB_UnhandledControlPacket(void)
                                Endpoint_ClearSETUP();\r
                                \r
                                /* Wait until the LED report has been sent by the host */\r
                                Endpoint_ClearSETUP();\r
                                \r
                                /* Wait until the LED report has been sent by the host */\r
-                               while (!(Endpoint_IsOUTReceived()));\r
+                               while (!(Endpoint_IsOUTReceived()))\r
+                               {\r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
 \r
                                /* Read in the LED report from the host */\r
                                uint8_t LEDStatus = Endpoint_Read_Byte();\r
 \r
                                /* Read in the LED report from the host */\r
                                uint8_t LEDStatus = Endpoint_Read_Byte();\r
@@ -183,9 +187,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Clear the endpoint data */\r
                                Endpoint_ClearOUT();\r
 \r
                                /* Clear the endpoint data */\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
@@ -200,9 +202,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Send the flag to the host */\r
                                Endpoint_ClearIN();\r
 \r
                                /* Send the flag to the host */\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
@@ -214,9 +214,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Set or clear the flag depending on what the host indicates that the current Protocol should be */\r
                                UsingReportProtocol = (USB_ControlRequest.wValue != 0);\r
 \r
                                /* Set or clear the flag depending on what the host indicates that the current Protocol should be */\r
                                UsingReportProtocol = (USB_ControlRequest.wValue != 0);\r
 \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
                        \r
                        break;\r
                        }\r
                        \r
                        break;\r
@@ -228,9 +226,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Get idle period in MSB, IdleCount must be multiplied by 4 to get number of milliseconds */\r
                                IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);\r
                                \r
                                /* Get idle period in MSB, IdleCount must be multiplied by 4 to get number of milliseconds */\r
                                IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);\r
                                \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
                        \r
                        break;\r
                        }\r
                        \r
                        break;\r
@@ -245,9 +241,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Send the flag to the host */\r
                                Endpoint_ClearIN();\r
 \r
                                /* Send the flag to the host */\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
@@ -378,7 +372,7 @@ void ReceiveNextReport(void)
 void HID_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
 void HID_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
          \r
        /* Send the next keypress report to the host */\r
          return;\r
          \r
        /* Send the next keypress report to the host */\r
index c60f08f..7d486e1 100644 (file)
@@ -174,7 +174,11 @@ void EVENT_USB_UnhandledControlPacket(void)
                                Endpoint_ClearSETUP();\r
                                \r
                                /* Wait until the LED report has been sent by the host */\r
                                Endpoint_ClearSETUP();\r
                                \r
                                /* Wait until the LED report has been sent by the host */\r
-                               while (!(Endpoint_IsOUTReceived()));\r
+                               while (!(Endpoint_IsOUTReceived()))\r
+                               {\r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
 \r
                                /* Read in the LED report from the host */\r
                                uint8_t LEDStatus = Endpoint_Read_Byte();\r
 \r
                                /* Read in the LED report from the host */\r
                                uint8_t LEDStatus = Endpoint_Read_Byte();\r
@@ -195,9 +199,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Clear the endpoint data */\r
                                Endpoint_ClearOUT();\r
 \r
                                /* Clear the endpoint data */\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
@@ -213,7 +215,7 @@ void Keyboard_HID_Task(void)
        uint8_t JoyStatus_LCL = Joystick_GetStatus();\r
 \r
        /* Device must be connected and configured for the task to run */\r
        uint8_t JoyStatus_LCL = Joystick_GetStatus();\r
 \r
        /* Device must be connected and configured for the task to run */\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        /* Check if board button is not pressed, if so mouse mode enabled */\r
          return;\r
 \r
        /* Check if board button is not pressed, if so mouse mode enabled */\r
@@ -284,7 +286,7 @@ void Mouse_HID_Task(void)
        uint8_t JoyStatus_LCL = Joystick_GetStatus();\r
 \r
        /* Device must be connected and configured for the task to run */\r
        uint8_t JoyStatus_LCL = Joystick_GetStatus();\r
 \r
        /* Device must be connected and configured for the task to run */\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        /* Check if board button is pressed, if so mouse mode enabled */\r
          return;\r
 \r
        /* Check if board button is pressed, if so mouse mode enabled */\r
index 954cbcb..8aed527 100644 (file)
@@ -117,7 +117,7 @@ void MIDI_Task(void)
        static uint8_t PrevJoystickStatus;\r
 \r
        /* Device must be connected and configured for the task to run */\r
        static uint8_t PrevJoystickStatus;\r
 \r
        /* Device must be connected and configured for the task to run */\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPNUM);\r
          return;\r
 \r
        Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPNUM);\r
index fdc91dd..2bd03c9 100644 (file)
@@ -68,8 +68,12 @@ void DataflashManager_WriteBlocks(const uint32_t BlockAddress, uint16_t TotalBlo
        Dataflash_SendAddressBytes(0, CurrDFPageByte);\r
 \r
        /* Wait until endpoint is ready before continuing */\r
        Dataflash_SendAddressBytes(0, CurrDFPageByte);\r
 \r
        /* Wait until endpoint is ready before continuing */\r
-       while (!(Endpoint_IsReadWriteAllowed()));\r
-\r
+       while (!(Endpoint_IsReadWriteAllowed()))\r
+       {\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return;\r
+       }\r
+       \r
        while (TotalBlocks)\r
        {\r
                uint8_t BytesInBlockDiv16 = 0;\r
        while (TotalBlocks)\r
        {\r
                uint8_t BytesInBlockDiv16 = 0;\r
@@ -84,7 +88,11 @@ void DataflashManager_WriteBlocks(const uint32_t BlockAddress, uint16_t TotalBlo
                                Endpoint_ClearOUT();\r
                                \r
                                /* Wait until the host has sent another packet */\r
                                Endpoint_ClearOUT();\r
                                \r
                                /* Wait until the host has sent another packet */\r
-                               while (!(Endpoint_IsReadWriteAllowed()));\r
+                               while (!(Endpoint_IsReadWriteAllowed()))\r
+                               {\r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
                        }\r
 \r
                        /* Check if end of dataflash page reached */\r
                        }\r
 \r
                        /* Check if end of dataflash page reached */\r
@@ -195,7 +203,11 @@ void DataflashManager_ReadBlocks(const uint32_t BlockAddress, uint16_t TotalBloc
        Dataflash_SendByte(0x00);\r
        \r
        /* Wait until endpoint is ready before continuing */\r
        Dataflash_SendByte(0x00);\r
        \r
        /* Wait until endpoint is ready before continuing */\r
-       while (!(Endpoint_IsReadWriteAllowed()));\r
+       while (!(Endpoint_IsReadWriteAllowed()))\r
+       {\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return;\r
+       }\r
        \r
        while (TotalBlocks)\r
        {\r
        \r
        while (TotalBlocks)\r
        {\r
@@ -211,7 +223,11 @@ void DataflashManager_ReadBlocks(const uint32_t BlockAddress, uint16_t TotalBloc
                                Endpoint_ClearIN();\r
                                \r
                                /* Wait until the endpoint is ready for more data */\r
                                Endpoint_ClearIN();\r
                                \r
                                /* Wait until the endpoint is ready for more data */\r
-                               while (!(Endpoint_IsReadWriteAllowed()));\r
+                               while (!(Endpoint_IsReadWriteAllowed()))\r
+                               {\r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
                        }\r
                        \r
                        /* Check if end of dataflash page reached */\r
                        }\r
                        \r
                        /* Check if end of dataflash page reached */\r
index 2c80fc5..01d27f0 100644 (file)
@@ -142,9 +142,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Indicate that the current transfer should be aborted */\r
                                IsMassStoreReset = true;                        \r
 \r
                                /* Indicate that the current transfer should be aborted */\r
                                IsMassStoreReset = true;                        \r
 \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
 \r
                        break;\r
                        }\r
 \r
                        break;\r
@@ -158,9 +156,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
@@ -172,67 +168,67 @@ void EVENT_USB_UnhandledControlPacket(void)
  */\r
 void MassStorage_Task(void)\r
 {\r
  */\r
 void MassStorage_Task(void)\r
 {\r
-       /* Check if the USB System is connected to a Host */\r
-       if (USB_IsConnected)\r
+       /* Device must be connected and configured for the task to run */\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
+         return;\r
+         \r
+       /* Select the Data Out Endpoint */\r
+       Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM);\r
+       \r
+       /* Check to see if a command from the host has been issued */\r
+       if (Endpoint_IsReadWriteAllowed())\r
        {\r
        {\r
-               /* Select the Data Out Endpoint */\r
-               Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM);\r
-               \r
-               /* Check to see if a command from the host has been issued */\r
-               if (Endpoint_IsReadWriteAllowed())\r
-               {\r
-                       /* Indicate busy */\r
-                       LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
+               /* Indicate busy */\r
+               LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
 \r
 \r
-                       /* Process sent command block from the host */\r
-                       if (ReadInCommandBlock())\r
-                       {\r
-                               /* Check direction of command, select Data IN endpoint if data is from the device */\r
-                               if (CommandBlock.Flags & COMMAND_DIRECTION_DATA_IN)\r
-                                 Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM);\r
+               /* Process sent command block from the host */\r
+               if (ReadInCommandBlock())\r
+               {\r
+                       /* Check direction of command, select Data IN endpoint if data is from the device */\r
+                       if (CommandBlock.Flags & COMMAND_DIRECTION_DATA_IN)\r
+                         Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM);\r
 \r
 \r
-                               /* Decode the received SCSI command */\r
-                               SCSI_DecodeSCSICommand();\r
+                       /* Decode the received SCSI command */\r
+                       SCSI_DecodeSCSICommand();\r
 \r
 \r
-                               /* Load in the CBW tag into the CSW to link them together */\r
-                               CommandStatus.Tag = CommandBlock.Tag;\r
+                       /* Load in the CBW tag into the CSW to link them together */\r
+                       CommandStatus.Tag = CommandBlock.Tag;\r
 \r
 \r
-                               /* Load in the data residue counter into the CSW */\r
-                               CommandStatus.DataTransferResidue = CommandBlock.DataTransferLength;\r
+                       /* Load in the data residue counter into the CSW */\r
+                       CommandStatus.DataTransferResidue = CommandBlock.DataTransferLength;\r
 \r
 \r
-                               /* Stall the selected data pipe if command failed (if data is still to be transferred) */\r
-                               if ((CommandStatus.Status == Command_Fail) && (CommandStatus.DataTransferResidue))\r
-                                 Endpoint_StallTransaction();\r
+                       /* Stall the selected data pipe if command failed (if data is still to be transferred) */\r
+                       if ((CommandStatus.Status == Command_Fail) && (CommandStatus.DataTransferResidue))\r
+                         Endpoint_StallTransaction();\r
 \r
 \r
-                               /* Return command status block to the host */\r
-                               ReturnCommandStatus();\r
-                               \r
-                               /* Check if a Mass Storage Reset occurred */\r
-                               if (IsMassStoreReset)\r
-                               {\r
-                                       /* Reset the data endpoint banks */\r
-                                       Endpoint_ResetFIFO(MASS_STORAGE_OUT_EPNUM);\r
-                                       Endpoint_ResetFIFO(MASS_STORAGE_IN_EPNUM);\r
-                                       \r
-                                       Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM);\r
-                                       Endpoint_ClearStall();\r
-                                       Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM);\r
-                                       Endpoint_ClearStall();\r
-                               }\r
-\r
-                               /* Indicate ready */\r
-                               LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
-                       }\r
-                       else\r
+                       /* Return command status block to the host */\r
+                       ReturnCommandStatus();\r
+                       \r
+                       /* Check if a Mass Storage Reset occurred */\r
+                       if (IsMassStoreReset)\r
                        {\r
                        {\r
-                               /* Indicate error reading in the command block from the host */\r
-                               LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
+                               /* Reset the data endpoint banks */\r
+                               Endpoint_ResetFIFO(MASS_STORAGE_OUT_EPNUM);\r
+                               Endpoint_ResetFIFO(MASS_STORAGE_IN_EPNUM);\r
+                               \r
+                               Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM);\r
+                               Endpoint_ClearStall();\r
+                               Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM);\r
+                               Endpoint_ClearStall();\r
                        }\r
                        }\r
-               }\r
 \r
 \r
-               /* Clear the abort transfer flag */\r
-               IsMassStoreReset = false;\r
+                       /* Indicate ready */\r
+                       LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
+               }\r
+               else\r
+               {\r
+                       /* Indicate error reading in the command block from the host */\r
+                       LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
+               }\r
        }\r
        }\r
+\r
+       /* Clear the abort transfer flag */\r
+       IsMassStoreReset = false;\r
 }\r
 \r
 /** Function to read in a command block from the host, via the bulk data OUT endpoint. This function reads in the next command block\r
 }\r
 \r
 /** Function to read in a command block from the host, via the bulk data OUT endpoint. This function reads in the next command block\r
index febb364..d15f688 100644 (file)
@@ -172,9 +172,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Send the flag to the host */\r
                                Endpoint_ClearIN();\r
 \r
                                /* Send the flag to the host */\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
@@ -186,9 +184,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Set or clear the flag depending on what the host indicates that the current Protocol should be */\r
                                UsingReportProtocol = (USB_ControlRequest.wValue != 0);\r
                                \r
                                /* Set or clear the flag depending on what the host indicates that the current Protocol should be */\r
                                UsingReportProtocol = (USB_ControlRequest.wValue != 0);\r
                                \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
                        \r
                        break;\r
                        }\r
                        \r
                        break;\r
@@ -200,9 +196,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Get idle period in MSB, must multiply by 4 to get the duration in milliseconds */\r
                                IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);\r
                                \r
                                /* Get idle period in MSB, must multiply by 4 to get the duration in milliseconds */\r
                                IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);\r
                                \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
                        \r
                        break;\r
                        }\r
                        \r
                        break;\r
@@ -217,9 +211,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Send the flag to the host */\r
                                Endpoint_ClearIN();\r
 \r
                                /* Send the flag to the host */\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
@@ -314,7 +306,7 @@ void SendNextReport(void)
 void Mouse_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
 void Mouse_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
          \r
        /* Send the next mouse report to the host */\r
          return;\r
          \r
        /* Send the next mouse report to the host */\r
index 1c0e030..ca38c24 100644 (file)
@@ -285,7 +285,7 @@ void Ethernet_Task(void)
           Ethernet frame at a time, so the FrameInBuffer bool is used to indicate when the buffers contain data. */\r
 \r
        /* Device must be connected and configured for the task to run */\r
           Ethernet frame at a time, so the FrameInBuffer bool is used to indicate when the buffers contain data. */\r
 \r
        /* Device must be connected and configured for the task to run */\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        /* Check if a frame has been written to the IN frame buffer */\r
          return;\r
 \r
        /* Check if a frame has been written to the IN frame buffer */\r
index 9160dd1..af963b1 100644 (file)
@@ -192,9 +192,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                                 CONTROL_LINE_OUT_* masks to determine the RTS and DTR line states using the following code:\r
                                */\r
 \r
                                                 CONTROL_LINE_OUT_* masks to determine the RTS and DTR line states using the following code:\r
                                */\r
 \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
        \r
                        break;\r
                        }\r
        \r
                        break;\r
@@ -205,7 +203,7 @@ void EVENT_USB_UnhandledControlPacket(void)
 void CDC_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
 void CDC_Task(void)\r
 {\r
        /* Device must be connected and configured for the task to run */\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
          \r
 #if 0\r
          return;\r
          \r
 #if 0\r
@@ -264,7 +262,11 @@ void CDC_Task(void)
        if (Tx_Buffer.Elements)\r
        {\r
                /* Wait until Serial Tx Endpoint Ready for Read/Write */\r
        if (Tx_Buffer.Elements)\r
        {\r
                /* Wait until Serial Tx Endpoint Ready for Read/Write */\r
-               while (!(Endpoint_IsReadWriteAllowed()));\r
+               while (!(Endpoint_IsReadWriteAllowed()))\r
+               {\r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return;\r
+               }\r
                \r
                /* Write the bytes from the buffer to the endpoint while space is available */\r
                while (Tx_Buffer.Elements && Endpoint_IsReadWriteAllowed())\r
                \r
                /* Write the bytes from the buffer to the endpoint while space is available */\r
                while (Tx_Buffer.Elements && Endpoint_IsReadWriteAllowed())\r
@@ -284,8 +286,12 @@ void CDC_Task(void)
                if (IsFull && !(Tx_Buffer.Elements))\r
                {\r
                        /* Wait until Serial Tx Endpoint Ready for Read/Write */\r
                if (IsFull && !(Tx_Buffer.Elements))\r
                {\r
                        /* Wait until Serial Tx Endpoint Ready for Read/Write */\r
-                       while (!(Endpoint_IsReadWriteAllowed()));\r
-\r
+                       while (!(Endpoint_IsReadWriteAllowed()))\r
+                       {\r
+                               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                 return;\r
+                       }\r
+                               \r
                        /* Send an empty packet to terminate the transfer */\r
                        Endpoint_ClearIN();\r
                }\r
                        /* Send an empty packet to terminate the transfer */\r
                        Endpoint_ClearIN();\r
                }\r
@@ -298,11 +304,8 @@ void CDC_Task(void)
 ISR(USART1_RX_vect, ISR_BLOCK)\r
 {\r
        /* Only store received characters if the USB interface is connected */\r
 ISR(USART1_RX_vect, ISR_BLOCK)\r
 {\r
        /* Only store received characters if the USB interface is connected */\r
-       if (USB_IsConnected)\r
-       {\r
-               /* Character received, store it into the buffer */\r
-               Buffer_StoreElement(&Tx_Buffer, UDR1);\r
-       }\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
+         Buffer_StoreElement(&Tx_Buffer, UDR1);\r
 }\r
 \r
 /** Reconfigures the USART to match the current serial port settings issued by the host as closely as possible. */\r
 }\r
 \r
 /** Reconfigures the USART to match the current serial port settings issued by the host as closely as possible. */\r
index ddd159e..54291bf 100644 (file)
@@ -114,7 +114,12 @@ void Bluetooth_ProcessHCICommands(void)
 \r
                        do\r
                        {\r
 \r
                        do\r
                        {\r
-                               while (!(Bluetooth_GetNextHCIEventHeader()));\r
+                               while (!(Bluetooth_GetNextHCIEventHeader()))\r
+                               {\r
+                                       if (USB_HostState == HOST_STATE_Unattached)\r
+                                         return;\r
+                               }\r
+               \r
                                Bluetooth_DiscardRemainingHCIEventParameters();\r
                        } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);\r
 \r
                                Bluetooth_DiscardRemainingHCIEventParameters();\r
                        } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);\r
 \r
@@ -133,7 +138,12 @@ void Bluetooth_ProcessHCICommands(void)
 \r
                        do\r
                        {\r
 \r
                        do\r
                        {\r
-                               while (!(Bluetooth_GetNextHCIEventHeader()));\r
+                               while (!(Bluetooth_GetNextHCIEventHeader()))\r
+                               {\r
+                                       if (USB_HostState == HOST_STATE_Unattached)\r
+                                         return;\r
+                               }\r
+\r
                                Bluetooth_DiscardRemainingHCIEventParameters();\r
                        } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);\r
 \r
                                Bluetooth_DiscardRemainingHCIEventParameters();\r
                        } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);\r
 \r
@@ -155,7 +165,12 @@ void Bluetooth_ProcessHCICommands(void)
                                                                                            EventMask[3], EventMask[2], EventMask[1], EventMask[0]);\r
                        do\r
                        {\r
                                                                                            EventMask[3], EventMask[2], EventMask[1], EventMask[0]);\r
                        do\r
                        {\r
-                               while (!(Bluetooth_GetNextHCIEventHeader()));\r
+                               while (!(Bluetooth_GetNextHCIEventHeader()))\r
+                               {\r
+                                       if (USB_HostState == HOST_STATE_Unattached)\r
+                                         return;\r
+                               }\r
+\r
                                Bluetooth_DiscardRemainingHCIEventParameters();\r
                        } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);\r
                \r
                                Bluetooth_DiscardRemainingHCIEventParameters();\r
                        } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);\r
                \r
@@ -176,7 +191,12 @@ void Bluetooth_ProcessHCICommands(void)
                        \r
                        do\r
                        {\r
                        \r
                        do\r
                        {\r
-                               while (!(Bluetooth_GetNextHCIEventHeader()));\r
+                               while (!(Bluetooth_GetNextHCIEventHeader()))\r
+                               {\r
+                                       if (USB_HostState == HOST_STATE_Unattached)\r
+                                         return;\r
+                               }\r
+\r
                                Bluetooth_DiscardRemainingHCIEventParameters();\r
                        } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);\r
                        \r
                                Bluetooth_DiscardRemainingHCIEventParameters();\r
                        } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);\r
                        \r
@@ -195,7 +215,12 @@ void Bluetooth_ProcessHCICommands(void)
 \r
                        do\r
                        {\r
 \r
                        do\r
                        {\r
-                               while (!(Bluetooth_GetNextHCIEventHeader()));\r
+                               while (!(Bluetooth_GetNextHCIEventHeader()))\r
+                               {\r
+                                       if (USB_HostState == HOST_STATE_Unattached)\r
+                                         return;\r
+                               }\r
+\r
                                Bluetooth_DiscardRemainingHCIEventParameters();\r
                        } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);\r
 \r
                                Bluetooth_DiscardRemainingHCIEventParameters();\r
                        } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);\r
 \r
@@ -215,7 +240,12 @@ void Bluetooth_ProcessHCICommands(void)
 \r
                        do\r
                        {\r
 \r
                        do\r
                        {\r
-                               while (!(Bluetooth_GetNextHCIEventHeader()));\r
+                               while (!(Bluetooth_GetNextHCIEventHeader()))\r
+                               {\r
+                                       if (USB_HostState == HOST_STATE_Unattached)\r
+                                         return;\r
+                               }\r
+\r
                                Bluetooth_DiscardRemainingHCIEventParameters();\r
                        } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);\r
 \r
                                Bluetooth_DiscardRemainingHCIEventParameters();\r
                        } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);\r
 \r
@@ -366,7 +396,12 @@ void Bluetooth_ProcessHCICommands(void)
                \r
                        do\r
                        {\r
                \r
                        do\r
                        {\r
-                               while (!(Bluetooth_GetNextHCIEventHeader()));\r
+                               while (!(Bluetooth_GetNextHCIEventHeader()))\r
+                               {                               \r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
+\r
                                Bluetooth_DiscardRemainingHCIEventParameters();\r
                        } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);\r
 \r
                                Bluetooth_DiscardRemainingHCIEventParameters();\r
                        } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);\r
 \r
index e265910..628fa21 100644 (file)
@@ -41,7 +41,7 @@ Bluetooth_Device_t     Bluetooth_DeviceConfiguration ATTR_WEAK =
 \r
 void Bluetooth_Stack_Task(void)\r
 {\r
 \r
 void Bluetooth_Stack_Task(void)\r
 {\r
-       if (!(USB_IsConnected) || (USB_HostState != HOST_STATE_Ready))\r
+       if (USB_HostState != HOST_STATE_Configured)\r
          Bluetooth_HCIProcessingState = Bluetooth_Init;\r
                \r
        Bluetooth_ProcessHCICommands();\r
          Bluetooth_HCIProcessingState = Bluetooth_Init;\r
                \r
        Bluetooth_ProcessHCICommands();\r
index a3cde78..e20fa2c 100644 (file)
@@ -164,15 +164,12 @@ void CDC_Host_Task(void)
                                USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                                break;\r
                        }\r
                                USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                                break;\r
                        }\r
-                               \r
+\r
+                       puts_P(PSTR("CDC Device Enumerated.\r\n"));\r
+\r
                        USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                case HOST_STATE_Configured:\r
                        USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                case HOST_STATE_Configured:\r
-                       puts_P(PSTR("CDC Device Enumerated.\r\n"));\r
-                               \r
-                       USB_HostState = HOST_STATE_Ready;\r
-                       break;\r
-               case HOST_STATE_Ready:\r
                        /* Select and the data IN pipe */\r
                        Pipe_SelectPipe(CDC_DATAPIPE_IN);\r
                        Pipe_Unfreeze();\r
                        /* Select and the data IN pipe */\r
                        Pipe_SelectPipe(CDC_DATAPIPE_IN);\r
                        Pipe_Unfreeze();\r
index 3a055c8..d360643 100644 (file)
@@ -266,14 +266,11 @@ void HID_Host_Task(void)
                                break;\r
                        }\r
                        \r
                                break;\r
                        }\r
                        \r
+                       puts_P(PSTR("HID Device Enumerated.\r\n"));\r
+\r
                        USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                case HOST_STATE_Configured:\r
                        USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                case HOST_STATE_Configured:\r
-                       puts_P(PSTR("HID Device Enumerated.\r\n"));\r
-                                       \r
-                       USB_HostState = HOST_STATE_Ready;\r
-                       break;\r
-               case HOST_STATE_Ready:\r
                        ReadNextReport();\r
 \r
                        break;\r
                        ReadNextReport();\r
 \r
                        break;\r
index c2d8dd4..273cd71 100644 (file)
@@ -230,9 +230,6 @@ void Keyboard_HID_Task(void)
                                break;\r
                        }\r
                                \r
                                break;\r
                        }\r
                                \r
-                       USB_HostState = HOST_STATE_Configured;\r
-                       break;\r
-               case HOST_STATE_Configured:\r
                        /* HID class request to set the keyboard protocol to the Boot Protocol */\r
                        USB_ControlRequest = (USB_Request_Header_t)\r
                                {\r
                        /* HID class request to set the keyboard protocol to the Boot Protocol */\r
                        USB_ControlRequest = (USB_Request_Header_t)\r
                                {\r
@@ -262,9 +259,9 @@ void Keyboard_HID_Task(void)
 \r
                        puts_P(PSTR("Keyboard Enumerated.\r\n"));\r
 \r
 \r
                        puts_P(PSTR("Keyboard Enumerated.\r\n"));\r
 \r
-                       USB_HostState = HOST_STATE_Ready;\r
+                       USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                        break;\r
-               case HOST_STATE_Ready:\r
+               case HOST_STATE_Configured:\r
                        /* If a report has been received, read and process it */\r
                        ReadNextReport();\r
 \r
                        /* If a report has been received, read and process it */\r
                        ReadNextReport();\r
 \r
index dc1707b..eee1755 100644 (file)
@@ -166,9 +166,6 @@ void Keyboard_HID_Task(void)
                                break;\r
                        }\r
                                \r
                                break;\r
                        }\r
                                \r
-                       USB_HostState = HOST_STATE_Configured;\r
-                       break;\r
-               case HOST_STATE_Configured:\r
                        puts_P(PSTR("Processing HID Report.\r\n"));\r
 \r
                        /* Get and process the device's first HID report descriptor */\r
                        puts_P(PSTR("Processing HID Report.\r\n"));\r
 \r
                        /* Get and process the device's first HID report descriptor */\r
@@ -187,9 +184,9 @@ void Keyboard_HID_Task(void)
 \r
                        puts_P(PSTR("Keyboard Enumerated.\r\n"));\r
 \r
 \r
                        puts_P(PSTR("Keyboard Enumerated.\r\n"));\r
 \r
-                       USB_HostState = HOST_STATE_Ready;\r
+                       USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                        break;\r
-               case HOST_STATE_Ready:\r
+               case HOST_STATE_Configured:\r
                        /* Select and unfreeze keyboard data pipe */\r
                        Pipe_SelectPipe(KEYBOARD_DATAPIPE);     \r
                        Pipe_Unfreeze();\r
                        /* Select and unfreeze keyboard data pipe */\r
                        Pipe_SelectPipe(KEYBOARD_DATAPIPE);     \r
                        Pipe_Unfreeze();\r
index 7ee7de1..d0518df 100644 (file)
@@ -154,7 +154,7 @@ static uint8_t MassStore_WaitForDataReceived(void)
                }\r
                  \r
                /* Check to see if the device was disconnected, if so exit function */\r
                }\r
                  \r
                /* Check to see if the device was disconnected, if so exit function */\r
-               if (!(USB_IsConnected))\r
+               if (USB_HostState == HOST_STATE_Unattached)\r
                  return PIPE_RWSTREAM_DeviceDisconnected;\r
        };\r
        \r
                  return PIPE_RWSTREAM_DeviceDisconnected;\r
        };\r
        \r
@@ -206,7 +206,11 @@ static uint8_t MassStore_SendReceiveData(void* BufferPtr)
                /* Acknowledge the packet */\r
                Pipe_ClearOUT();\r
                \r
                /* Acknowledge the packet */\r
                Pipe_ClearOUT();\r
                \r
-               while (!(Pipe_IsOUTReady()));\r
+               while (!(Pipe_IsOUTReady()))\r
+               {\r
+                       if (USB_HostState == HOST_STATE_Unattached)\r
+                         return PIPE_RWSTREAM_DeviceDisconnected;\r
+               }\r
        }\r
        \r
        /* Freeze used pipe after use */\r
        }\r
        \r
        /* Freeze used pipe after use */\r
index 1adec5b..6cfe669 100644 (file)
@@ -171,14 +171,11 @@ void MassStorage_Task(void)
                                break;\r
                        }\r
                                \r
                                break;\r
                        }\r
                                \r
+                       puts_P(PSTR("Mass Storage Disk Enumerated.\r\n"));\r
+\r
                        USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                case HOST_STATE_Configured:\r
                        USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                case HOST_STATE_Configured:\r
-                       puts_P(PSTR("Mass Storage Disk Enumerated.\r\n"));\r
-                               \r
-                       USB_HostState = HOST_STATE_Ready;\r
-                       break;\r
-               case HOST_STATE_Ready:\r
                        /* Indicate device busy via the status LEDs */\r
                        LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
                        \r
                        /* Indicate device busy via the status LEDs */\r
                        LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
                        \r
@@ -241,7 +238,11 @@ void MassStorage_Task(void)
                        {\r
                                Serial_TxByte('.');\r
                                \r
                        {\r
                                Serial_TxByte('.');\r
                                \r
-                               if ((ErrorCode = MassStore_TestUnitReady(0)) != 0)\r
+                               /* Abort if device removed */\r
+                               if (USB_HostState == HOST_STATE_Unattached)\r
+                                 break;\r
+\r
+                               if ((ErrorCode = MassStore_TestUnitReady(0)) != PIPE_RWSTREAM_NoError)\r
                                {\r
                                        ShowDiskReadError(PSTR("Test Unit Ready"), false, ErrorCode);\r
 \r
                                {\r
                                        ShowDiskReadError(PSTR("Test Unit Ready"), false, ErrorCode);\r
 \r
@@ -249,11 +250,7 @@ void MassStorage_Task(void)
                                        break;\r
                                }\r
                        }\r
                                        break;\r
                                }\r
                        }\r
-                       while ((SCSICommandStatus.Status != Command_Pass) && USB_IsConnected);\r
-                       \r
-                       /* Abort if device removed */\r
-                       if (!(USB_IsConnected))\r
-                         break;\r
+                       while (SCSICommandStatus.Status != Command_Pass);\r
 \r
                        puts_P(PSTR("\r\nRetrieving Capacity... "));\r
 \r
 \r
                        puts_P(PSTR("\r\nRetrieving Capacity... "));\r
 \r
@@ -320,7 +317,7 @@ void MassStorage_Task(void)
                        while (!(Buttons_GetStatus() & BUTTONS_BUTTON1))\r
                        {\r
                                /* Abort if device removed */\r
                        while (!(Buttons_GetStatus() & BUTTONS_BUTTON1))\r
                        {\r
                                /* Abort if device removed */\r
-                               if (!(USB_IsConnected))\r
+                               if (USB_HostState == HOST_STATE_Unattached)\r
                                  break;\r
                        }\r
                        \r
                                  break;\r
                        }\r
                        \r
@@ -346,7 +343,7 @@ void MassStorage_Task(void)
                                }\r
 \r
                                /* Abort if device removed */\r
                                }\r
 \r
                                /* Abort if device removed */\r
-                               if (!(USB_IsConnected))\r
+                               if (USB_HostState == HOST_STATE_Unattached)\r
                                  break;\r
                        }\r
                        \r
                                  break;\r
                        }\r
                        \r
index 5340aa1..b98f4bf 100644 (file)
@@ -226,9 +226,6 @@ void Mouse_HID_Task(void)
                                break;\r
                        }\r
                        \r
                                break;\r
                        }\r
                        \r
-                       USB_HostState = HOST_STATE_Configured;\r
-                       break;\r
-               case HOST_STATE_Configured:\r
                        /* HID class request to set the mouse protocol to the Boot Protocol */\r
                        USB_ControlRequest = (USB_Request_Header_t)\r
                                {\r
                        /* HID class request to set the mouse protocol to the Boot Protocol */\r
                        USB_ControlRequest = (USB_Request_Header_t)\r
                                {\r
@@ -257,10 +254,10 @@ void Mouse_HID_Task(void)
                        }\r
 \r
                        puts_P(PSTR("Mouse Enumerated.\r\n"));\r
                        }\r
 \r
                        puts_P(PSTR("Mouse Enumerated.\r\n"));\r
-                       \r
-                       USB_HostState = HOST_STATE_Ready;\r
+\r
+                       USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                        break;\r
-               case HOST_STATE_Ready:\r
+               case HOST_STATE_Configured:\r
                        /* If a report has been received, read and process it */\r
                        ReadNextReport();\r
 \r
                        /* If a report has been received, read and process it */\r
                        ReadNextReport();\r
 \r
index 42865e5..63fce0d 100644 (file)
@@ -166,9 +166,6 @@ void Mouse_HID_Task(void)
                                break;\r
                        }\r
                        \r
                                break;\r
                        }\r
                        \r
-                       USB_HostState = HOST_STATE_Configured;\r
-                       break;\r
-               case HOST_STATE_Configured:\r
                        puts_P(PSTR("Processing HID Report.\r\n"));\r
 \r
                        /* Get and process the device's first HID report descriptor */\r
                        puts_P(PSTR("Processing HID Report.\r\n"));\r
 \r
                        /* Get and process the device's first HID report descriptor */\r
@@ -186,10 +183,10 @@ void Mouse_HID_Task(void)
                        }\r
 \r
                        puts_P(PSTR("Mouse Enumerated.\r\n"));\r
                        }\r
 \r
                        puts_P(PSTR("Mouse Enumerated.\r\n"));\r
-                               \r
-                       USB_HostState = HOST_STATE_Ready;\r
+\r
+                       USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                        break;\r
-               case HOST_STATE_Ready:\r
+               case HOST_STATE_Configured:\r
                        /* Select and unfreeze mouse data pipe */\r
                        Pipe_SelectPipe(MOUSE_DATAPIPE);        \r
                        Pipe_Unfreeze();\r
                        /* Select and unfreeze mouse data pipe */\r
                        Pipe_SelectPipe(MOUSE_DATAPIPE);        \r
                        Pipe_Unfreeze();\r
index 4c88bb4..cb051cd 100644 (file)
@@ -54,7 +54,11 @@ uint8_t Printer_SendData(Printer_Data_t* PrinterCommands)
          return ErrorCode;\r
 \r
        Pipe_ClearOUT();\r
          return ErrorCode;\r
 \r
        Pipe_ClearOUT();\r
-       while (!(Pipe_IsOUTReady()));\r
+       while (!(Pipe_IsOUTReady()))\r
+       {\r
+               if (USB_HostState == HOST_STATE_Unattached)\r
+                 return PIPE_RWSTREAM_DeviceDisconnected;\r
+       }\r
        \r
        Pipe_Freeze();\r
 \r
        \r
        Pipe_Freeze();\r
 \r
index 3b145a6..bbda0c2 100644 (file)
@@ -195,9 +195,6 @@ void USB_Printer_Host(void)
                                }\r
                        }\r
                        \r
                                }\r
                        }\r
                        \r
-                       USB_HostState = HOST_STATE_Configured;\r
-                       break;\r
-               case HOST_STATE_Configured:\r
                        puts_P(PSTR("Retrieving Device ID...\r\n"));\r
                \r
                        char DeviceIDString[256];\r
                        puts_P(PSTR("Retrieving Device ID...\r\n"));\r
                \r
                        char DeviceIDString[256];\r
@@ -217,10 +214,10 @@ void USB_Printer_Host(void)
                        printf_P(PSTR("Printer Device ID: %s\r\n"), DeviceIDString);\r
 \r
                        puts_P(PSTR("Printer Enumerated.\r\n"));\r
                        printf_P(PSTR("Printer Device ID: %s\r\n"), DeviceIDString);\r
 \r
                        puts_P(PSTR("Printer Enumerated.\r\n"));\r
-                                       \r
-                       USB_HostState = HOST_STATE_Ready;\r
+\r
+                       USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                        break;\r
-               case HOST_STATE_Ready:\r
+               case HOST_STATE_Configured:\r
                        /* Indicate device busy via the status LEDs */\r
                        LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
                \r
                        /* Indicate device busy via the status LEDs */\r
                        LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
                \r
index e0d71a0..970df4f 100644 (file)
@@ -152,12 +152,9 @@ uint8_t SImage_RecieveBlockHeader(void)
                }\r
                  \r
                /* Check to see if the device was disconnected, if so exit function */\r
                }\r
                  \r
                /* Check to see if the device was disconnected, if so exit function */\r
-               if (!(USB_IsConnected))\r
-               {\r
-                       /* Return error code */\r
-                       return PIPE_RWSTREAM_DeviceDisconnected;\r
-               }\r
-       };\r
+               if (USB_HostState == HOST_STATE_Unattached)\r
+                 return PIPE_RWSTREAM_DeviceDisconnected;\r
+       }\r
        \r
        /* Freeze OUT pipe after use */\r
        Pipe_SelectPipe(SIMAGE_DATA_OUT_PIPE);\r
        \r
        /* Freeze OUT pipe after use */\r
        Pipe_SelectPipe(SIMAGE_DATA_OUT_PIPE);\r
index 5cfaf46..ca0771a 100644 (file)
@@ -166,14 +166,11 @@ void StillImage_Task(void)
                                break;\r
                        }\r
                                \r
                                break;\r
                        }\r
                                \r
+                       puts_P(PSTR("Still Image Device Enumerated.\r\n"));\r
+\r
                        USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                case HOST_STATE_Configured:\r
                        USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                case HOST_STATE_Configured:\r
-                       puts_P(PSTR("Still Image Device Enumerated.\r\n"));\r
-                               \r
-                       USB_HostState = HOST_STATE_Ready;\r
-                       break;\r
-               case HOST_STATE_Ready:\r
                        /* Indicate device busy via the status LEDs */\r
                        LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
                        \r
                        /* Indicate device busy via the status LEDs */\r
                        LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
                        \r
@@ -331,9 +328,7 @@ void StillImage_Task(void)
                        /* Indicate device no longer busy */\r
                        LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
                        \r
                        /* Indicate device no longer busy */\r
                        LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
                        \r
-                       /* Wait until USB device disconnected */\r
-                       while (USB_IsConnected);\r
-                       \r
+                       USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                        break;\r
        }\r
 }\r
                        break;\r
        }\r
 }\r
index d117bed..116921e 100644 (file)
@@ -50,8 +50,7 @@ void Audio_Device_ProcessControlPacket(USB_ClassInfo_Audio_Device_t* const Audio
                                \r
                                AudioInterfaceInfo->State.InterfaceEnabled = (USB_ControlRequest.wValue != 0);\r
                                  \r
                                \r
                                AudioInterfaceInfo->State.InterfaceEnabled = (USB_ControlRequest.wValue != 0);\r
                                  \r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
 \r
                        break;\r
                        }\r
 \r
                        break;\r
index 12b5593..237e1dd 100644 (file)
@@ -78,8 +78,7 @@ void CDC_Device_ProcessControlPacket(USB_ClassInfo_CDC_Device_t* CDCInterfaceInf
                                \r
                                EVENT_CDC_Device_ControLineStateChanged(CDCInterfaceInfo);\r
 \r
                                \r
                                EVENT_CDC_Device_ControLineStateChanged(CDCInterfaceInfo);\r
 \r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
        \r
                        break;\r
                        }\r
        \r
                        break;\r
@@ -114,7 +113,7 @@ bool CDC_Device_ConfigureEndpoints(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)
 \r
 void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)\r
 {\r
 \r
 void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)\r
 {\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);\r
          return;\r
 \r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);\r
@@ -125,7 +124,12 @@ void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)
        if (!(Endpoint_IsReadWriteAllowed()))\r
        {\r
                Endpoint_ClearIN();\r
        if (!(Endpoint_IsReadWriteAllowed()))\r
        {\r
                Endpoint_ClearIN();\r
-               while (!(Endpoint_IsReadWriteAllowed()));\r
+\r
+               while (!(Endpoint_IsReadWriteAllowed()))\r
+               {\r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return;\r
+               }\r
        }       \r
        \r
        Endpoint_ClearIN();\r
        }       \r
        \r
        Endpoint_ClearIN();\r
@@ -133,7 +137,7 @@ void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)
 \r
 void CDC_Device_SendString(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, char* const Data, const uint16_t Length)\r
 {\r
 \r
 void CDC_Device_SendString(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, char* const Data, const uint16_t Length)\r
 {\r
-       if (!(USB_IsConnected))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);\r
          return;\r
 \r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);\r
@@ -142,7 +146,7 @@ void CDC_Device_SendString(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, c
 \r
 void CDC_Device_SendByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, const uint8_t Data)\r
 {\r
 \r
 void CDC_Device_SendByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, const uint8_t Data)\r
 {\r
-       if (!(USB_IsConnected))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);\r
          return;\r
 \r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);\r
@@ -150,7 +154,12 @@ void CDC_Device_SendByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, con
        if (!(Endpoint_IsReadWriteAllowed()))\r
        {\r
                Endpoint_ClearIN();\r
        if (!(Endpoint_IsReadWriteAllowed()))\r
        {\r
                Endpoint_ClearIN();\r
-               while (!(Endpoint_IsReadWriteAllowed()));\r
+               \r
+               while (!(Endpoint_IsReadWriteAllowed()))\r
+               {\r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return;               \r
+               }\r
        }\r
 \r
        Endpoint_Write_Byte(Data);      \r
        }\r
 \r
        Endpoint_Write_Byte(Data);      \r
@@ -165,7 +174,7 @@ uint16_t CDC_Device_BytesReceived(USB_ClassInfo_CDC_Device_t* const CDCInterface
 \r
 uint8_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)\r
 {\r
 \r
 uint8_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)\r
 {\r
-       if (!(USB_IsConnected))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return 0;\r
 \r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataOUTEndpointNumber);\r
          return 0;\r
 \r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataOUTEndpointNumber);\r
@@ -180,7 +189,7 @@ uint8_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)
 \r
 void CDC_Device_SendControlLineStateChange(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)\r
 {\r
 \r
 void CDC_Device_SendControlLineStateChange(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)\r
 {\r
-       if (!(USB_IsConnected))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.NotificationEndpointNumber);\r
          return;\r
 \r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.NotificationEndpointNumber);\r
index 4a6295a..f292c63 100644 (file)
@@ -88,8 +88,7 @@ void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t* const HIDInterf
                                Endpoint_Write_Byte(HIDInterfaceInfo->State.UsingReportProtocol);\r
                                Endpoint_ClearIN();\r
 \r
                                Endpoint_Write_Byte(HIDInterfaceInfo->State.UsingReportProtocol);\r
                                Endpoint_ClearIN();\r
 \r
-                               while (!(Endpoint_IsOUTReceived()));\r
-                               Endpoint_ClearOUT();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
                        \r
                        break;\r
                        }\r
                        \r
                        break;\r
@@ -100,8 +99,7 @@ void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t* const HIDInterf
 \r
                                HIDInterfaceInfo->State.UsingReportProtocol = (USB_ControlRequest.wValue != 0x0000);\r
                                \r
 \r
                                HIDInterfaceInfo->State.UsingReportProtocol = (USB_ControlRequest.wValue != 0x0000);\r
                                \r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
                        \r
                        break;\r
                        }\r
                        \r
                        break;\r
@@ -115,8 +113,7 @@ void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t* const HIDInterf
                                        \r
                                        HIDInterfaceInfo->State.IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);\r
                                        \r
                                        \r
                                        HIDInterfaceInfo->State.IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);\r
                                        \r
-                                       while (!(Endpoint_IsINReady()));\r
-                                       Endpoint_ClearIN();\r
+                                       Endpoint_ClearStatusStage();\r
                                }\r
                        }\r
                        \r
                                }\r
                        }\r
                        \r
@@ -129,8 +126,7 @@ void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t* const HIDInterf
                                Endpoint_Write_Byte(HIDInterfaceInfo->State.IdleCount >> 2);\r
                                Endpoint_ClearIN();\r
 \r
                                Endpoint_Write_Byte(HIDInterfaceInfo->State.IdleCount >> 2);\r
                                Endpoint_ClearIN();\r
 \r
-                               while (!(Endpoint_IsOUTReceived()));\r
-                               Endpoint_ClearOUT();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
 \r
                        break;\r
                        }\r
 \r
                        break;\r
@@ -152,7 +148,7 @@ bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* const HIDInterfac
                \r
 void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)\r
 {\r
                \r
 void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)\r
 {\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber);\r
          return;\r
 \r
        Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber);\r
index da33bdb..3643275 100644 (file)
@@ -70,7 +70,7 @@ void MIDI_Device_USBTask(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
 \r
 void MIDI_Device_SendEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo, MIDI_EventPacket_t* const Event)\r
 {\r
 \r
 void MIDI_Device_SendEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo, MIDI_EventPacket_t* const Event)\r
 {\r
-       if (!(USB_IsConnected))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
        \r
        Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber);\r
          return;\r
        \r
        Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber);\r
@@ -84,7 +84,7 @@ void MIDI_Device_SendEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfac
 \r
 bool MIDI_Device_ReceiveEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo, MIDI_EventPacket_t* const Event)\r
 {\r
 \r
 bool MIDI_Device_ReceiveEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo, MIDI_EventPacket_t* const Event)\r
 {\r
-       if (!(USB_IsConnected))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return false;\r
        \r
        Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataOUTEndpointNumber);\r
          return false;\r
        \r
        Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataOUTEndpointNumber);\r
index f7f3fd9..9f8688c 100644 (file)
@@ -53,8 +53,7 @@ void MS_Device_ProcessControlPacket(USB_ClassInfo_MS_Device_t* const MSInterface
 \r
                                MSInterfaceInfo->State.IsMassStoreReset = true;                 \r
 \r
 \r
                                MSInterfaceInfo->State.IsMassStoreReset = true;                 \r
 \r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
 \r
                        break;\r
                        }\r
 \r
                        break;\r
@@ -63,12 +62,10 @@ void MS_Device_ProcessControlPacket(USB_ClassInfo_MS_Device_t* const MSInterface
                        {\r
                                Endpoint_ClearSETUP();\r
 \r
                        {\r
                                Endpoint_ClearSETUP();\r
 \r
-                               Endpoint_Write_Byte(MSInterfaceInfo->Config.TotalLUNs - 1);\r
-                               \r
+                               Endpoint_Write_Byte(MSInterfaceInfo->Config.TotalLUNs - 1);                             \r
                                Endpoint_ClearIN();\r
                                \r
                                Endpoint_ClearIN();\r
                                \r
-                               while (!(Endpoint_IsOUTReceived()));\r
-                               Endpoint_ClearOUT();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
                        \r
                        break;\r
                        }\r
                        \r
                        break;\r
@@ -96,7 +93,7 @@ bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t* const MSInterfaceIn
 \r
 void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)\r
 {\r
 \r
 void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)\r
 {\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);\r
          return;\r
 \r
        Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);\r
index 0d35ee6..37771c6 100644 (file)
@@ -138,7 +138,7 @@ bool RNDIS_Device_ConfigureEndpoints(USB_ClassInfo_RNDIS_Device_t* const RNDISIn
 \r
 void RNDIS_Device_USBTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo)\r
 {\r
 \r
 void RNDIS_Device_USBTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo)\r
 {\r
-       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
 \r
        RNDIS_Message_Header_t* MessageHeader = (RNDIS_Message_Header_t*)&RNDISInterfaceInfo->State.RNDISMessageBuffer;\r
          return;\r
 \r
        RNDIS_Message_Header_t* MessageHeader = (RNDIS_Message_Header_t*)&RNDISInterfaceInfo->State.RNDISMessageBuffer;\r
index 2df0dda..4042d46 100644 (file)
@@ -98,7 +98,7 @@
                         *        which is not always accurate (host may suspend the bus while still connected). If the actual connection state\r
                         *        needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by\r
                         *        passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection\r
                         *        which is not always accurate (host may suspend the bus while still connected). If the actual connection state\r
                         *        needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by\r
                         *        passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection\r
-                        *        and disconnection events may be manually fired, and the \ref USB_IsConnected global changed manually.\r
+                        *        and disconnection events may be manually fired, and the \ref USB_DeviceState global changed manually.\r
                         *\r
                         *  \see USBTask.h for more information on the USB management task and reducing CPU usage.\r
                         */\r
                         *\r
                         *  \see USBTask.h for more information on the USB management task and reducing CPU usage.\r
                         */\r
                         *        which is not always accurate (host may suspend the bus while still connected). If the actual connection state\r
                         *        needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by\r
                         *        passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection\r
                         *        which is not always accurate (host may suspend the bus while still connected). If the actual connection state\r
                         *        needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by\r
                         *        passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection\r
-                        *        and disconnection events may be manually fired, and the \ref USB_IsConnected global changed manually.\r
+                        *        and disconnection events may be manually fired, and the \ref USB_DeviceState global changed manually.\r
                         *\r
                         *  \see USBTask.h for more information on the USB management task and reducing CPU usage.\r
                         */\r
                         *\r
                         *  \see USBTask.h for more information on the USB management task and reducing CPU usage.\r
                         */\r
index c240bef..5edefa9 100644 (file)
@@ -78,19 +78,17 @@ ISR(USB_GEN_vect, ISR_BLOCK)
                {\r
                        EVENT_USB_VBUSConnect();\r
                        \r
                {\r
                        EVENT_USB_VBUSConnect();\r
                        \r
-                       if (USB_IsConnected)\r
+                       if (USB_DeviceState != DEVICE_STATE_Unattached)\r
                          EVENT_USB_Disconnect();\r
                                \r
                        USB_ResetInterface();\r
                          EVENT_USB_Disconnect();\r
                                \r
                        USB_ResetInterface();\r
-                               \r
-                       USB_IsConnected = true;\r
 \r
 \r
+                       USB_DeviceState = DEVICE_STATE_Powered;\r
                        EVENT_USB_Connect();\r
                }\r
                else\r
                {\r
                        EVENT_USB_Connect();\r
                }\r
                else\r
                {\r
-                       USB_IsConnected = false;\r
-\r
+                       USB_DeviceState = DEVICE_STATE_Unattached;\r
                        EVENT_USB_Disconnect();\r
                \r
                        USB_Detach();\r
                        EVENT_USB_Disconnect();\r
                \r
                        USB_Detach();\r
@@ -117,16 +115,12 @@ ISR(USB_GEN_vect, ISR_BLOCK)
                if (!(USB_Options & USB_OPT_MANUAL_PLL))\r
                  USB_PLL_Off();\r
 \r
                if (!(USB_Options & USB_OPT_MANUAL_PLL))\r
                  USB_PLL_Off();\r
 \r
-               USB_IsSuspended = true;\r
-\r
-               EVENT_USB_Suspend();\r
-\r
                #if defined(USB_SERIES_2_AVR) && !defined(NO_LIMITED_CONTROLLER_CONNECT)\r
                #if defined(USB_SERIES_2_AVR) && !defined(NO_LIMITED_CONTROLLER_CONNECT)\r
-               if (USB_IsConnected)\r
-               {\r
-                       USB_IsConnected = false;\r
-                       EVENT_USB_Disconnect();\r
-               }\r
+               USB_DeviceState = DEVICE_STATE_Unattached;\r
+               EVENT_USB_Disconnect();\r
+               #else\r
+               USB_DeviceState = DEVICE_STATE_Suspended;\r
+               EVENT_USB_Suspend();\r
                #endif\r
        }\r
 \r
                #endif\r
        }\r
 \r
@@ -146,22 +140,19 @@ ISR(USB_GEN_vect, ISR_BLOCK)
                USB_INT_Enable(USB_INT_SUSPEND);\r
                \r
                #if defined(USB_SERIES_2_AVR) && !defined(NO_LIMITED_CONTROLLER_CONNECT)\r
                USB_INT_Enable(USB_INT_SUSPEND);\r
                \r
                #if defined(USB_SERIES_2_AVR) && !defined(NO_LIMITED_CONTROLLER_CONNECT)\r
-               if (!(USB_IsConnected))\r
-               {\r
-                       USB_IsConnected = true;\r
-                       EVENT_USB_Connect();\r
-               }\r
+               USB_DeviceState = DEVICE_STATE_Powered;\r
+               EVENT_USB_Connect();\r
+               #else\r
+               USB_DeviceState = DEVICE_STATE_Configured;\r
+               EVENT_USB_WakeUp();             \r
                #endif\r
                #endif\r
-\r
-               USB_IsSuspended = false;\r
-\r
-               EVENT_USB_WakeUp();\r
        }\r
    \r
        if (USB_INT_HasOccurred(USB_INT_EORSTI) && USB_INT_IsEnabled(USB_INT_EORSTI))\r
        {\r
                USB_INT_Clear(USB_INT_EORSTI);\r
 \r
        }\r
    \r
        if (USB_INT_HasOccurred(USB_INT_EORSTI) && USB_INT_IsEnabled(USB_INT_EORSTI))\r
        {\r
                USB_INT_Clear(USB_INT_EORSTI);\r
 \r
+               USB_DeviceState         = DEVICE_STATE_Default;\r
                USB_ConfigurationNumber = 0;\r
 \r
                USB_INT_Clear(USB_INT_SUSPEND);\r
                USB_ConfigurationNumber = 0;\r
 \r
                USB_INT_Clear(USB_INT_SUSPEND);\r
@@ -217,7 +208,7 @@ ISR(USB_GEN_vect, ISR_BLOCK)
 \r
                USB_INT_Enable(USB_INT_DDISCI);\r
                \r
 \r
                USB_INT_Enable(USB_INT_DDISCI);\r
                \r
-               USB_HostState = HOST_STATE_Attached;\r
+               USB_HostState = HOST_STATE_Powered;\r
        }\r
 \r
        if (USB_INT_HasOccurred(USB_INT_BCERRI) && USB_INT_IsEnabled(USB_INT_BCERRI))\r
        }\r
 \r
        if (USB_INT_HasOccurred(USB_INT_BCERRI) && USB_INT_IsEnabled(USB_INT_BCERRI))\r
@@ -227,7 +218,7 @@ ISR(USB_GEN_vect, ISR_BLOCK)
                EVENT_USB_DeviceEnumerationFailed(HOST_ENUMERROR_NoDeviceDetected, 0);\r
                EVENT_USB_DeviceUnattached();\r
                \r
                EVENT_USB_DeviceEnumerationFailed(HOST_ENUMERROR_NoDeviceDetected, 0);\r
                EVENT_USB_DeviceUnattached();\r
                \r
-               if (USB_IsConnected)\r
+               if (USB_HostState != HOST_STATE_Unattached)\r
                  EVENT_USB_Disconnect();\r
 \r
                USB_ResetInterface();\r
                  EVENT_USB_Disconnect();\r
 \r
                USB_ResetInterface();\r
@@ -239,13 +230,16 @@ ISR(USB_GEN_vect, ISR_BLOCK)
        {               \r
                USB_INT_Clear(USB_INT_IDTI);\r
 \r
        {               \r
                USB_INT_Clear(USB_INT_IDTI);\r
 \r
-               if (USB_IsConnected)\r
-               {\r
-                       if (USB_CurrentMode == USB_MODE_HOST)\r
-                         EVENT_USB_DeviceUnattached();\r
+               if (USB_DeviceState != DEVICE_STATE_Unattached)\r
+                 EVENT_USB_Disconnect();\r
 \r
 \r
+               if (USB_HostState != HOST_STATE_Unattached)\r
+               {\r
                        EVENT_USB_Disconnect();\r
                        EVENT_USB_Disconnect();\r
+                       EVENT_USB_DeviceUnattached();\r
                }\r
                }\r
+       \r
+               EVENT_USB_Disconnect();\r
 \r
                EVENT_USB_UIDChange();\r
                \r
 \r
                EVENT_USB_UIDChange();\r
                \r
index 894171f..500a697 100644 (file)
@@ -33,8 +33,6 @@
 #define  INCLUDE_FROM_USBTASK_C\r
 #include "USBTask.h"\r
 \r
 #define  INCLUDE_FROM_USBTASK_C\r
 #include "USBTask.h"\r
 \r
-volatile bool        USB_IsSuspended;\r
-volatile bool        USB_IsConnected;\r
 volatile bool        USB_IsInitialized;\r
 USB_Request_Header_t USB_ControlRequest;\r
 \r
 volatile bool        USB_IsInitialized;\r
 USB_Request_Header_t USB_ControlRequest;\r
 \r
@@ -42,6 +40,10 @@ USB_Request_Header_t USB_ControlRequest;
 volatile uint8_t     USB_HostState;\r
 #endif\r
 \r
 volatile uint8_t     USB_HostState;\r
 #endif\r
 \r
+#if defined(USB_CAN_BE_DEVICE)\r
+volatile uint8_t     USB_DeviceState;\r
+#endif\r
+\r
 void USB_USBTask(void)\r
 {\r
        #if defined(USB_HOST_ONLY)\r
 void USB_USBTask(void)\r
 {\r
        #if defined(USB_HOST_ONLY)\r
@@ -59,7 +61,7 @@ void USB_USBTask(void)
 #if defined(USB_CAN_BE_DEVICE)\r
 static void USB_DeviceTask(void)\r
 {\r
 #if defined(USB_CAN_BE_DEVICE)\r
 static void USB_DeviceTask(void)\r
 {\r
-       if (USB_IsConnected)\r
+       if (USB_DeviceState != DEVICE_STATE_Unattached)\r
        {\r
                uint8_t PrevEndpoint = Endpoint_GetCurrentEndpoint();\r
        \r
        {\r
                uint8_t PrevEndpoint = Endpoint_GetCurrentEndpoint();\r
        \r
index 2a9bf19..12acb11 100644 (file)
 \r
        /* Public Interface - May be used in end-application: */\r
                /* Global Variables: */\r
 \r
        /* Public Interface - May be used in end-application: */\r
                /* Global Variables: */\r
-                       /** Indicates if the USB interface is currently connected to a host if in device mode, or to a\r
-                        *  device while running in host mode.\r
-                        *\r
-                        *  \note This variable should be treated as read-only in the user application, and never manually\r
-                        *        changed in value.\r
-                        *\r
-                        *  \note For the smaller USB AVRs (AT90USBXX2) with limited USB controllers, VBUS is not available to the USB controller.\r
-                        *        this means that the current connection state is derived from the bus suspension and wake up events by default,\r
-                        *        which is not always accurate (host may suspend the bus while still connected). If the actual connection state\r
-                        *        needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by\r
-                        *        passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection\r
-                        *        and disconnection events may be manually fired, and the \ref USB_IsConnected global changed manually.\r
-                        *\r
-                        *  \ingroup Group_USBManagement\r
-                        */\r
-                       extern volatile bool USB_IsConnected;\r
-\r
                        /** Indicates if the USB interface is currently initialized but not necessarily connected to a host\r
                         *  or device (i.e. if \ref USB_Init() has been run). If this is false, all other library globals are invalid.\r
                         *\r
                        /** Indicates if the USB interface is currently initialized but not necessarily connected to a host\r
                         *  or device (i.e. if \ref USB_Init() has been run). If this is false, all other library globals are invalid.\r
                         *\r
                         */\r
                         extern USB_Request_Header_t USB_ControlRequest;\r
                        \r
                         */\r
                         extern USB_Request_Header_t USB_ControlRequest;\r
                        \r
-                       #if defined(USB_CAN_BE_DEVICE) || defined(__DOXYGEN__)\r
-                       /** Indicates if the USB interface is currently suspended by the host when in device mode. When suspended,\r
-                        *  the device should consume minimal power, and cannot communicate to the host. If Remote Wakeup is\r
-                        *  supported by the device and \ref USB_RemoteWakeupEnabled is true, suspension can be terminated by the device\r
-                        *  by issuing a Remote Wakeup request.\r
-                        *\r
-                        *  \note This global is only present if the user application can be a USB device.\r
-                        *\r
-                        *  \note This variable should be treated as read-only in the user application, and never manually\r
-                        *        changed in value.\r
-                        *\r
-                        *  \ingroup Group_Device\r
-                        */\r
-                       extern volatile bool USB_IsSuspended;\r
-                       #endif\r
-\r
                        #if defined(USB_CAN_BE_HOST) || defined(__DOXYGEN__)\r
                        /** Indicates the current host state machine state. When in host mode, this indicates the state\r
                        #if defined(USB_CAN_BE_HOST) || defined(__DOXYGEN__)\r
                        /** Indicates the current host state machine state. When in host mode, this indicates the state\r
-                        *  via one of the values of the \ref USB_Host_States_t enum values in Host.h.\r
+                        *  via one of the values of the \ref USB_Host_States_t enum values.\r
                         *\r
                         *  This value may be altered by the user application to implement the \ref HOST_STATE_Addressed,\r
                         *\r
                         *  This value may be altered by the user application to implement the \ref HOST_STATE_Addressed,\r
-                        *  \ref HOST_STATE_Configured, \ref HOST_STATE_Ready and \ref HOST_STATE_Suspended states which\r
-                        *  are not implemented by the library.\r
+                        *  \ref HOST_STATE_Configured and \ref HOST_STATE_Suspended states which are not implemented by\r
+                        *  the library.\r
                         *\r
                         *  \note This global is only present if the user application can be a USB host.\r
                         *\r
                         *\r
                         *  \note This global is only present if the user application can be a USB host.\r
                         *\r
-                        *  \see \ref USB_Host_States_t for a list of possible host states\r
+                        *  \see \ref USB_Host_States_t for a list of possible device states\r
                         *\r
                         *  \ingroup Group_Host\r
                         */\r
                        extern volatile uint8_t USB_HostState;\r
                        #endif\r
 \r
                         *\r
                         *  \ingroup Group_Host\r
                         */\r
                        extern volatile uint8_t USB_HostState;\r
                        #endif\r
 \r
+                       #if defined(USB_CAN_BE_DEVICE) || defined(__DOXYGEN__)\r
+                       /** Indicates the current device state machine state. When in device mode, this indicates the state\r
+                        *  via one of the values of the \ref USB_Device_States_t enum values.\r
+                        *\r
+                        *  This value should not be altered by the user application as it is handled automatically by the\r
+                        *  library. The only exception to this rule is if the NO_LIMITED_CONTROLLER_CONNECT token is used\r
+                        *  (see \ref EVENT_USB_Connect() and \ref EVENT_USB_Disconnect() events).\r
+                        *\r
+                        *  \note This global is only present if the user application can be a USB device.\r
+                        *\r
+                        *  \note This variable should be treated as read-only in the user application, and never manually\r
+                        *        changed in value except in the circumstances outlined above.\r
+                        *\r
+                        *  \see \ref USB_Device_States_t for a list of possible device states\r
+                        *\r
+                        *  \ingroup Group_Device\r
+                        */\r
+                       extern volatile uint8_t USB_DeviceState;\r
+                       #endif\r
+\r
                /* Function Prototypes: */\r
                        /** This is the main USB management task. The USB driver requires that this task be executed\r
                         *  continuously when the USB system is active (device attached in host mode, or attached to a host\r
                /* Function Prototypes: */\r
                        /** This is the main USB management task. The USB driver requires that this task be executed\r
                         *  continuously when the USB system is active (device attached in host mode, or attached to a host\r
index 10dce72..8de4bd6 100644 (file)
@@ -117,13 +117,22 @@ void USB_Device_ProcessControlPacket(void)
 \r
 static void USB_Device_SetAddress(void)\r
 {\r
 \r
 static void USB_Device_SetAddress(void)\r
 {\r
+       uint8_t DeviceAddress = (USB_ControlRequest.wValue & 0x7F);\r
+\r
        Endpoint_ClearSETUP();\r
        \r
        Endpoint_ClearIN();\r
        \r
        Endpoint_ClearSETUP();\r
        \r
        Endpoint_ClearIN();\r
        \r
-       while (!(Endpoint_IsINReady()));\r
+       while (!(Endpoint_IsINReady()))\r
+       {\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return;\r
+       }\r
+\r
+       UDADDR = ((1 << ADDEN) | DeviceAddress);\r
 \r
 \r
-       UDADDR = ((1 << ADDEN) | ((uint8_t)USB_ControlRequest.wValue & 0x7F));\r
+       if (DeviceAddress)\r
+         USB_DeviceState = DEVICE_STATE_Addressed;\r
 \r
        return;\r
 }\r
 \r
        return;\r
 }\r
@@ -185,8 +194,17 @@ static void USB_Device_SetConfiguration(void)
 \r
        Endpoint_ClearIN();\r
 \r
 \r
        Endpoint_ClearIN();\r
 \r
-       if (!(AlreadyConfigured) && USB_ConfigurationNumber)\r
-         EVENT_USB_DeviceEnumerationComplete();\r
+       if (USB_ConfigurationNumber)\r
+       {\r
+               USB_DeviceState = DEVICE_STATE_Configured;\r
+\r
+               if (!(AlreadyConfigured))\r
+                 EVENT_USB_DeviceEnumerationComplete();\r
+       }\r
+       else\r
+       {\r
+               USB_DeviceState = DEVICE_STATE_Addressed;\r
+       }\r
 \r
        EVENT_USB_ConfigurationChanged();\r
 }\r
 \r
        EVENT_USB_ConfigurationChanged();\r
 }\r
@@ -199,7 +217,12 @@ void USB_Device_GetConfiguration(void)
        \r
        Endpoint_ClearIN();\r
 \r
        \r
        Endpoint_ClearIN();\r
 \r
-       while (!(Endpoint_IsOUTReceived()));\r
+       while (!(Endpoint_IsOUTReceived()))\r
+       {\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return;       \r
+       }\r
+\r
        Endpoint_ClearOUT();\r
 }\r
 \r
        Endpoint_ClearOUT();\r
 }\r
 \r
@@ -332,7 +355,12 @@ static void USB_Device_GetStatus(void)
 \r
        Endpoint_ClearIN();\r
        \r
 \r
        Endpoint_ClearIN();\r
        \r
-       while (!(Endpoint_IsOUTReceived()));\r
+       while (!(Endpoint_IsOUTReceived()))\r
+       {\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return;       \r
+       }\r
+       \r
        Endpoint_ClearOUT();\r
 }\r
 \r
        Endpoint_ClearOUT();\r
 }\r
 \r
index 975d09a..e162a59 100644 (file)
 \r
                                #define USB_Device_IsUSBSuspended()           ((UDINT &  (1 << SUSPI)) ? true : false)\r
                        #endif\r
 \r
                                #define USB_Device_IsUSBSuspended()           ((UDINT &  (1 << SUSPI)) ? true : false)\r
                        #endif\r
-\r
+                       \r
+               /* Type Defines: */\r
+                       enum USB_Device_States_t\r
+                       {\r
+                               DEVICE_STATE_Unattached                   = 0, /**< Internally implemented by the library. This state indicates\r
+                                                                               *   that the device is not currently connected to a host.\r
+                                                                               */\r
+                               DEVICE_STATE_Powered                      = 1, /**< Internally implemented by the library. This state indicates\r
+                                                                               *   that the device is connected to a host, but enumeration has not\r
+                                                                               *   yet begun.\r
+                                                                               */\r
+                               DEVICE_STATE_Default                      = 2, /**< Internally implemented by the library. This state indicates\r
+                                                                               *   that the device's USB bus has been reset by the host and it is\r
+                                                                               *   now waiting for the host to begin the enumeration process.\r
+                                                                               */\r
+                               DEVICE_STATE_Addressed                    = 3, /**< Internally implemented by the library. This state indicates\r
+                                                                               *   that the device has been addressed by the USB Host, but is not\r
+                                                                               *   yet configured.\r
+                                                                               */\r
+                               DEVICE_STATE_Configured                   = 4, /**< May be implemented by the user project. This state indicates\r
+                                                                               *   that the device has been enumerated by the host and is ready\r
+                                                                               *   for USB communications to begin.\r
+                                                                               */\r
+                               DEVICE_STATE_Suspended                    = 5, /**< May be implemented by the user project. This state indicates\r
+                                                                               *   that the USB bus has been suspended by the host, and the device\r
+                                                                               *   should power down to a minimal power level until the bus is\r
+                                                                               *   resumed.\r
+                                                                               */\r
+                       };\r
+                       \r
                /* Function Prototypes: */\r
                        /** Function to retrieve a given descriptor's size and memory location from the given descriptor type value,\r
                         *  index and language ID. This function MUST be overridden in the user application (added with full, identical  \r
                /* Function Prototypes: */\r
                        /** Function to retrieve a given descriptor's size and memory location from the given descriptor type value,\r
                         *  index and language ID. This function MUST be overridden in the user application (added with full, identical  \r
index f9db9cd..e4d0140 100644 (file)
@@ -71,6 +71,30 @@ void Endpoint_ClearEndpoints(void)
        }\r
 }\r
 \r
        }\r
 }\r
 \r
+void Endpoint_ClearStatusStage(void)\r
+{\r
+       if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)\r
+       {\r
+               while (!(Endpoint_IsOUTReceived()))\r
+               {                               \r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return;\r
+               }\r
+\r
+               Endpoint_ClearOUT();\r
+       }\r
+       else\r
+       {\r
+               while (!(Endpoint_IsINReady()))\r
+               {\r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return;\r
+               }\r
+               \r
+               Endpoint_ClearIN();\r
+       }\r
+}\r
+\r
 #if !defined(CONTROL_ONLY_DEVICE)\r
 uint8_t Endpoint_WaitUntilReady(void)\r
 {\r
 #if !defined(CONTROL_ONLY_DEVICE)\r
 uint8_t Endpoint_WaitUntilReady(void)\r
 {\r
@@ -93,7 +117,7 @@ uint8_t Endpoint_WaitUntilReady(void)
                          return ENDPOINT_READYWAIT_NoError;            \r
                }\r
                \r
                          return ENDPOINT_READYWAIT_NoError;            \r
                }\r
                \r
-               if (!(USB_IsConnected))\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
                  return ENDPOINT_READYWAIT_DeviceDisconnected;\r
                else if (Endpoint_IsStalled())\r
                  return ENDPOINT_READYWAIT_EndpointStalled;\r
                  return ENDPOINT_READYWAIT_DeviceDisconnected;\r
                else if (Endpoint_IsStalled())\r
                  return ENDPOINT_READYWAIT_EndpointStalled;\r
index 2caea83..a9ec122 100644 (file)
                                ENDPOINT_RWSTREAM_EndpointStalled    = 1, /**< The endpoint was stalled during the stream\r
                                                                           *   transfer by the host or device.\r
                                                                           */\r
                                ENDPOINT_RWSTREAM_EndpointStalled    = 1, /**< The endpoint was stalled during the stream\r
                                                                           *   transfer by the host or device.\r
                                                                           */\r
-                               ENDPOINT_RWSTREAM_DeviceDisconnected = 1, /**< Device was disconnected from the host during\r
+                               ENDPOINT_RWSTREAM_DeviceDisconnected = 2, /**< Device was disconnected from the host during\r
                                                                           *   the transfer.\r
                                                                           */\r
                                                                           *   the transfer.\r
                                                                           */\r
-                               ENDPOINT_RWSTREAM_Timeout            = 2, /**< The host failed to accept or send the next packet\r
+                               ENDPOINT_RWSTREAM_Timeout            = 3, /**< The host failed to accept or send the next packet\r
                                                                           *   within the software timeout period set by the\r
                                                                           *   \ref USB_STREAM_TIMEOUT_MS macro.\r
                                                                           */\r
                                                                           *   within the software timeout period set by the\r
                                                                           *   \ref USB_STREAM_TIMEOUT_MS macro.\r
                                                                           */\r
-                               ENDPOINT_RWSTREAM_CallbackAborted    = 3, /**< Indicates that the stream's callback function\r
+                               ENDPOINT_RWSTREAM_CallbackAborted    = 4, /**< Indicates that the stream's callback function\r
                                                                       *   aborted the transfer early.\r
                                                                           */\r
                        };\r
                                                                       *   aborted the transfer early.\r
                                                                           */\r
                        };\r
                        {\r
                                ENDPOINT_RWCSTREAM_NoError            = 0, /**< Command completed successfully, no error. */\r
                                ENDPOINT_RWCSTREAM_HostAborted        = 1, /**< The aborted the transfer prematurely. */\r
                        {\r
                                ENDPOINT_RWCSTREAM_NoError            = 0, /**< Command completed successfully, no error. */\r
                                ENDPOINT_RWCSTREAM_HostAborted        = 1, /**< The aborted the transfer prematurely. */\r
+                               ENDPOINT_RWCSTREAM_DeviceDisconnected = 2, /**< Device was disconnected from the host during\r
+                                                                           *   the transfer.\r
+                                                                           */\r
                        };\r
 \r
                /* Inline Functions: */\r
                        };\r
 \r
                /* Inline Functions: */\r
                         *  \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.\r
                         */\r
                        uint8_t Endpoint_WaitUntilReady(void);\r
                         *  \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.\r
                         */\r
                        uint8_t Endpoint_WaitUntilReady(void);\r
+                       \r
+                       /** Completes the status stage of a control transfer on a CONTROL type endpoint automatically,\r
+                        *  with respect to the data direction. This is a convenience function which can be used to\r
+                        *  simplify user control request handling.\r
+                        */\r
+                       void Endpoint_ClearStatusStage(void);\r
 \r
                        /** Reads and discards the given number of bytes from the endpoint from the given buffer,\r
                         *  discarding fully read packets from the host as needed. The last packet is not automatically\r
 \r
                        /** Reads and discards the given number of bytes from the endpoint from the given buffer,\r
                         *  discarding fully read packets from the host as needed. The last packet is not automatically\r
                         *  in both failure and success states; the user is responsible for manually clearing the setup OUT to\r
                         *  finalize the transfer via the \ref Endpoint_ClearOUT() macro.\r
                         *\r
                         *  in both failure and success states; the user is responsible for manually clearing the setup OUT to\r
                         *  finalize the transfer via the \ref Endpoint_ClearOUT() macro.\r
                         *\r
+                        *  \note This function automatically clears the control transfer's status stage. Do not manually attempt\r
+                        *        to clear the status stage when using this routine in a control transaction.\r
+                        *\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *\r
                         *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *\r
                         *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained\r
 \r
                        /** EEPROM buffer source version of Endpoint_Write_Control_Stream_LE.\r
                         *\r
 \r
                        /** EEPROM buffer source version of Endpoint_Write_Control_Stream_LE.\r
                         *\r
+                        *  \note This function automatically clears the control transfer's status stage. Do not manually attempt\r
+                        *        to clear the status stage when using this routine in a control transaction.\r
+                        *\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *\r
                         *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *\r
                         *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained\r
 \r
                        /** FLASH buffer source version of \ref Endpoint_Write_Control_Stream_LE.\r
                         *\r
 \r
                        /** FLASH buffer source version of \ref Endpoint_Write_Control_Stream_LE.\r
                         *\r
+                        *  \note This function automatically clears the control transfer's status stage. Do not manually attempt\r
+                        *        to clear the status stage when using this routine in a control transaction.\r
+                        *\r
                         *  \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.\r
                         *\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *  \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.\r
                         *\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *  in both failure and success states; the user is responsible for manually clearing the setup OUT to\r
                         *  finalize the transfer via the \ref Endpoint_ClearOUT() macro.\r
                         *\r
                         *  in both failure and success states; the user is responsible for manually clearing the setup OUT to\r
                         *  finalize the transfer via the \ref Endpoint_ClearOUT() macro.\r
                         *\r
+                        *  \note This function automatically clears the control transfer's status stage. Do not manually attempt\r
+                        *        to clear the status stage when using this routine in a control transaction.\r
+                        *\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *\r
                         *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *\r
                         *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained\r
 \r
                        /** EEPROM buffer source version of \ref Endpoint_Write_Control_Stream_BE.\r
                         *\r
 \r
                        /** EEPROM buffer source version of \ref Endpoint_Write_Control_Stream_BE.\r
                         *\r
+                        *  \note This function automatically clears the control transfer's status stage. Do not manually attempt\r
+                        *        to clear the status stage when using this routine in a control transaction.\r
+                        *\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *\r
                         *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *\r
                         *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained\r
 \r
                        /** FLASH buffer source version of \ref Endpoint_Write_Control_Stream_BE.\r
                         *\r
 \r
                        /** FLASH buffer source version of \ref Endpoint_Write_Control_Stream_BE.\r
                         *\r
+                        *  \note This function automatically clears the control transfer's status stage. Do not manually attempt\r
+                        *        to clear the status stage when using this routine in a control transaction.\r
+                        *\r
                         *  \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.\r
                         *\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *  \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.\r
                         *\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *  automatically sent after success or failure states; the user is responsible for manually sending the\r
                         *  setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro.\r
                         *\r
                         *  automatically sent after success or failure states; the user is responsible for manually sending the\r
                         *  setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro.\r
                         *\r
+                        *  \note This function automatically clears the control transfer's status stage. Do not manually attempt\r
+                        *        to clear the status stage when using this routine in a control transaction.\r
+                        *\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *\r
                         *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *\r
                         *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained\r
 \r
                        /** EEPROM buffer source version of \ref Endpoint_Read_Control_Stream_LE.\r
                         *\r
 \r
                        /** EEPROM buffer source version of \ref Endpoint_Read_Control_Stream_LE.\r
                         *\r
+                        *  \note This function automatically clears the control transfer's status stage. Do not manually attempt\r
+                        *        to clear the status stage when using this routine in a control transaction.\r
+                        *\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *\r
                         *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *\r
                         *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained\r
                         *  automatically sent after success or failure states; the user is responsible for manually sending the\r
                         *  setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro.\r
                         *\r
                         *  automatically sent after success or failure states; the user is responsible for manually sending the\r
                         *  setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro.\r
                         *\r
+                        *  \note This function automatically clears the control transfer's status stage. Do not manually attempt\r
+                        *        to clear the status stage when using this routine in a control transaction.\r
+                        *\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *\r
                         *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *\r
                         *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained\r
                        \r
                        /** EEPROM buffer source version of \ref Endpoint_Read_Control_Stream_BE.\r
                         *\r
                        \r
                        /** EEPROM buffer source version of \ref Endpoint_Read_Control_Stream_BE.\r
                         *\r
+                        *  \note This function automatically clears the control transfer's status stage. Do not manually attempt\r
+                        *        to clear the status stage when using this routine in a control transaction.\r
+                        *\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *\r
                         *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained\r
                         *  \note This routine should only be used on CONTROL type endpoints.\r
                         *\r
                         *  \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained\r
index efe4bae..ae7525b 100644 (file)
@@ -60,12 +60,12 @@ void USB_Host_ProcessNextHostState(void)
                        }\r
                \r
                        break;\r
                        }\r
                \r
                        break;\r
-               case HOST_STATE_Attached:\r
+               case HOST_STATE_Powered:\r
                        WaitMSRemaining = HOST_DEVICE_SETTLE_DELAY_MS;\r
                \r
                        WaitMSRemaining = HOST_DEVICE_SETTLE_DELAY_MS;\r
                \r
-                       USB_HostState = HOST_STATE_Attached_WaitForDeviceSettle;\r
+                       USB_HostState = HOST_STATE_Powered_WaitForDeviceSettle;\r
                        break;\r
                        break;\r
-               case HOST_STATE_Attached_WaitForDeviceSettle:\r
+               case HOST_STATE_Powered_WaitForDeviceSettle:\r
                        #if HOST_DEVICE_SETTLE_DELAY_MS > 0\r
                        _delay_ms(1);\r
 \r
                        #if HOST_DEVICE_SETTLE_DELAY_MS > 0\r
                        _delay_ms(1);\r
 \r
@@ -77,14 +77,14 @@ void USB_Host_ProcessNextHostState(void)
                                USB_Host_VBUS_Auto_Enable();\r
                                USB_Host_VBUS_Auto_On();\r
                                \r
                                USB_Host_VBUS_Auto_Enable();\r
                                USB_Host_VBUS_Auto_On();\r
                                \r
-                               USB_HostState = HOST_STATE_Attached_WaitForConnect;\r
+                               USB_HostState = HOST_STATE_Powered_WaitForConnect;\r
                        }\r
                        #else\r
                        }\r
                        #else\r
-                       USB_HostState = HOST_STATE_Attached_WaitForConnect;                     \r
+                       USB_HostState = HOST_STATE_Powered_WaitForConnect;                      \r
                        #endif\r
                        \r
                        break;\r
                        #endif\r
                        \r
                        break;\r
-               case HOST_STATE_Attached_WaitForConnect:                \r
+               case HOST_STATE_Powered_WaitForConnect:         \r
                        if (USB_INT_HasOccurred(USB_INT_DCONNI))\r
                        {       \r
                                USB_INT_Clear(USB_INT_DCONNI);\r
                        if (USB_INT_HasOccurred(USB_INT_DCONNI))\r
                        {       \r
                                USB_INT_Clear(USB_INT_DCONNI);\r
@@ -93,22 +93,21 @@ void USB_Host_ProcessNextHostState(void)
                                USB_INT_Clear(USB_INT_VBERRI);\r
                                USB_INT_Enable(USB_INT_VBERRI);\r
 \r
                                USB_INT_Clear(USB_INT_VBERRI);\r
                                USB_INT_Enable(USB_INT_VBERRI);\r
 \r
-                               USB_IsConnected = true;\r
                                EVENT_USB_Connect();\r
                                        \r
                                USB_Host_ResumeBus();\r
                                Pipe_ClearPipes();\r
                                \r
                                EVENT_USB_Connect();\r
                                        \r
                                USB_Host_ResumeBus();\r
                                Pipe_ClearPipes();\r
                                \r
-                               HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Attached_DoReset);\r
+                               HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Powered_DoReset);\r
                        }\r
 \r
                        break;\r
                        }\r
 \r
                        break;\r
-               case HOST_STATE_Attached_DoReset:\r
+               case HOST_STATE_Powered_DoReset:\r
                        USB_Host_ResetDevice();\r
 \r
                        USB_Host_ResetDevice();\r
 \r
-                       HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Powered);\r
+                       HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Powered_ConfigPipe);\r
                        break;\r
                        break;\r
-               case HOST_STATE_Powered:\r
+               case HOST_STATE_Powered_ConfigPipe:\r
                        Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL,\r
                                                           PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,\r
                                                           PIPE_CONTROLPIPE_DEFAULT_SIZE, PIPE_BANK_SINGLE);            \r
                        Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL,\r
                                                           PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,\r
                                                           PIPE_CONTROLPIPE_DEFAULT_SIZE, PIPE_BANK_SINGLE);            \r
@@ -199,9 +198,7 @@ void USB_Host_ProcessNextHostState(void)
                USB_Host_VBUS_Auto_Off();\r
 \r
                EVENT_USB_DeviceUnattached();\r
                USB_Host_VBUS_Auto_Off();\r
 \r
                EVENT_USB_DeviceUnattached();\r
-               \r
-               if (USB_IsConnected)\r
-                 EVENT_USB_Disconnect();\r
+               EVENT_USB_Disconnect();\r
 \r
                USB_ResetInterface();\r
        }\r
 \r
                USB_ResetInterface();\r
        }\r
@@ -222,7 +219,7 @@ uint8_t USB_Host_WaitMS(uint8_t MS)
                        MS--;\r
                }\r
                                        \r
                        MS--;\r
                }\r
                                        \r
-               if ((USB_IsConnected == false) || (USB_CurrentMode == USB_MODE_DEVICE))\r
+               if ((USB_HostState == HOST_STATE_Unattached) || (USB_CurrentMode == USB_MODE_DEVICE))\r
                {\r
                        ErrorCode = HOST_WAITERROR_DeviceDisconnect;\r
                        \r
                {\r
                        ErrorCode = HOST_WAITERROR_DeviceDisconnect;\r
                        \r
index 88634fd..a602cee 100644 (file)
                                                                               *   \r
                                                                               *   \note Do not manually change to this state in the user code.\r
                                                                               */\r
                                                                               *   \r
                                                                               *   \note Do not manually change to this state in the user code.\r
                                                                               */\r
-                               HOST_STATE_Attached                     = 3,  /**< Internally implemented by the library. This state indicates\r
+                               HOST_STATE_Powered                      = 3,  /**< Internally implemented by the library. This state indicates\r
                                                                               *   that a device has been attached, and the library's internals\r
                                                                               *   are being configured to begin the enumeration process.\r
                                                                               *   \r
                                                                               *   \note Do not manually change to this state in the user code.\r
                                                                               */\r
                                                                               *   that a device has been attached, and the library's internals\r
                                                                               *   are being configured to begin the enumeration process.\r
                                                                               *   \r
                                                                               *   \note Do not manually change to this state in the user code.\r
                                                                               */\r
-                               HOST_STATE_Attached_WaitForDeviceSettle = 4,  /**< Internally implemented by the library. This state indicates\r
+                               HOST_STATE_Powered_WaitForDeviceSettle = 4,   /**< Internally implemented by the library. This state indicates\r
                                                                               *   that the stack is waiting for the initial settling period to\r
                                                                               *   elapse before beginning the enumeration process.\r
                                                                               *   \r
                                                                               *   \note Do not manually change to this state in the user code.\r
                                                                               */\r
                                                                               *   that the stack is waiting for the initial settling period to\r
                                                                               *   elapse before beginning the enumeration process.\r
                                                                               *   \r
                                                                               *   \note Do not manually change to this state in the user code.\r
                                                                               */\r
-                               HOST_STATE_Attached_WaitForConnect      = 5,  /**< Internally implemented by the library. This state indicates\r
+                               HOST_STATE_Powered_WaitForConnect      = 5,   /**< Internally implemented by the library. This state indicates\r
                                                                               *   that the stack is waiting for a connection event from the USB\r
                                                                               *   controller to indicate a valid USB device has been attached to\r
                                                                               *   the bus and is ready to be enumerated.\r
                                                                               *   \r
                                                                               *   \note Do not manually change to this state in the user code.\r
                                                                               */\r
                                                                               *   that the stack is waiting for a connection event from the USB\r
                                                                               *   controller to indicate a valid USB device has been attached to\r
                                                                               *   the bus and is ready to be enumerated.\r
                                                                               *   \r
                                                                               *   \note Do not manually change to this state in the user code.\r
                                                                               */\r
-                               HOST_STATE_Attached_DoReset             = 6,  /**< Internally implemented by the library. This state indicates\r
+                               HOST_STATE_Powered_DoReset             = 6,   /**< Internally implemented by the library. This state indicates\r
                                                                               *   that a valid USB device has been attached, and that it is\r
                                                                               *   will now be reset to ensure it is ready for enumeration.\r
                                                                               *   \r
                                                                               *   \note Do not manually change to this state in the user code.\r
                                                                               */\r
                                                                               *   that a valid USB device has been attached, and that it is\r
                                                                               *   will now be reset to ensure it is ready for enumeration.\r
                                                                               *   \r
                                                                               *   \note Do not manually change to this state in the user code.\r
                                                                               */\r
-                               HOST_STATE_Powered                      = 7,  /**< Internally implemented by the library. This state indicates\r
+                               HOST_STATE_Powered_ConfigPipe           = 7,  /**< Internally implemented by the library. This state indicates\r
                                                                               *   that the attached device is currently powered and reset, and\r
                                                                               *   that the control pipe is now being configured by the stack.\r
                                                                               *   \r
                                                                               *   that the attached device is currently powered and reset, and\r
                                                                               *   that the control pipe is now being configured by the stack.\r
                                                                               *   \r
                                                                               *   retrieval and processing of the device descriptor) should also\r
                                                                               *   be placed in this state.\r
                                                                               */\r
                                                                               *   retrieval and processing of the device descriptor) should also\r
                                                                               *   be placed in this state.\r
                                                                               */\r
-                               HOST_STATE_Configured                   = 12, /**< May be implemented by the user project. This state should\r
-                                                                              *   implement any extra device configuration (such as the setting of\r
-                                                                              *   class-specific parameters) before normal communication is begun\r
-                                                                              *   in the HOST_STATE_Ready state.\r
-                                                                              */\r
-                               HOST_STATE_Ready                        = 13, /**< May be implemented by the user project. This state should\r
-                                                                              *   contain the main communications with the attached device. From this\r
-                                                                              *   this state the host state machine should be changed to either\r
-                                                                              *   HOST_STATE_Suspended (after the bus is manually suspended using the\r
-                                                                              *   USB_Host_SuspendBus() macro) or HOST_STATE_WaitForDeviceRemoval as\r
-                                                                              *   needed.\r
+                               HOST_STATE_Configured                   = 12, /**< May be implemented by the user project. This state should implement the\r
+                                                                              *   actual work performed on the attached device and changed to the\r
+                                                                              *   HOST_STATE_Suspended or HOST_STATE_WaitForDeviceRemoval states as needed.\r
                                                                               */\r
                                HOST_STATE_Suspended                    = 15, /**< May be implemented by the user project. This state should be maintained\r
                                                                               */\r
                                HOST_STATE_Suspended                    = 15, /**< May be implemented by the user project. This state should be maintained\r
-                                                                              *   while the bus is suspended, and changed to either the HOST_STATE_Ready\r
+                                                                              *   while the bus is suspended, and changed to either the HOST_STATE_Configured\r
                                                                               *   (after resuming the bus with the USB_Host_ResumeBus() macro) or the\r
                                                                               *   HOST_STATE_WaitForDeviceRemoval states as needed.\r
                                                                               */\r
                                                                               *   (after resuming the bus with the USB_Host_ResumeBus() macro) or the\r
                                                                               *   HOST_STATE_WaitForDeviceRemoval states as needed.\r
                                                                               */\r
index 4fcf4fc..cd7fa89 100644 (file)
@@ -110,15 +110,21 @@ void USB_Init(
 \r
 void USB_ShutDown(void)\r
 {\r
 \r
 void USB_ShutDown(void)\r
 {\r
-       if (USB_IsConnected)\r
+       #if defined(USB_CAN_BE_DEVICE)\r
+       if (USB_DeviceState != DEVICE_STATE_Unattached)\r
+         EVENT_USB_Disconnect();\r
+       #endif\r
+       \r
+       #if defined(USB_CAN_BE_HOST)\r
+       if (USB_HostState != HOST_STATE_Unattached)\r
          EVENT_USB_Disconnect();\r
          EVENT_USB_Disconnect();\r
+       #endif\r
 \r
        USB_Detach();\r
 \r
        USB_INT_DisableAllInterrupts();\r
        USB_INT_ClearAllInterrupts();\r
 \r
 \r
        USB_Detach();\r
 \r
        USB_INT_DisableAllInterrupts();\r
        USB_INT_ClearAllInterrupts();\r
 \r
-       USB_IsConnected   = false;\r
        USB_IsInitialized = false;\r
 \r
        #if defined(USB_CAN_BE_HOST)\r
        USB_IsInitialized = false;\r
 \r
        #if defined(USB_CAN_BE_HOST)\r
@@ -126,7 +132,10 @@ void USB_ShutDown(void)
        #endif\r
 \r
        #if defined(USB_CAN_BE_DEVICE)\r
        #endif\r
 \r
        #if defined(USB_CAN_BE_DEVICE)\r
-       USB_ConfigurationNumber = 0;\r
+       USB_DeviceState = DEVICE_STATE_Unattached;\r
+       USB_ConfigurationNumber  = 0;\r
+       USB_RemoteWakeupEnabled  = false;\r
+       USB_CurrentlySelfPowered = false;\r
        #endif\r
 \r
        #if defined(CAN_BE_BOTH)\r
        #endif\r
 \r
        #if defined(CAN_BE_BOTH)\r
@@ -149,16 +158,14 @@ void USB_ResetInterface(void)
 {\r
        USB_INT_DisableAllInterrupts();\r
        USB_INT_ClearAllInterrupts();\r
 {\r
        USB_INT_DisableAllInterrupts();\r
        USB_INT_ClearAllInterrupts();\r
-\r
-       USB_IsConnected = false;\r
-\r
+       \r
        #if defined(USB_CAN_BE_HOST)\r
        #if defined(USB_CAN_BE_HOST)\r
-       USB_HostState = HOST_STATE_Unattached;\r
+       USB_HostState   = HOST_STATE_Unattached;\r
        #endif\r
        #endif\r
-\r
+       \r
        #if defined(USB_CAN_BE_DEVICE)\r
        #if defined(USB_CAN_BE_DEVICE)\r
+       USB_DeviceState = DEVICE_STATE_Unattached;\r
        USB_ConfigurationNumber  = 0;\r
        USB_ConfigurationNumber  = 0;\r
-       USB_IsSuspended          = false;\r
        USB_RemoteWakeupEnabled  = false;\r
        USB_CurrentlySelfPowered = false;\r
        #endif\r
        USB_RemoteWakeupEnabled  = false;\r
        USB_CurrentlySelfPowered = false;\r
        #endif\r
index 3fb30a2..7d3e876 100644 (file)
@@ -93,7 +93,7 @@ uint8_t Pipe_WaitUntilReady(void)
 \r
                if (Pipe_IsStalled())\r
                  return PIPE_READYWAIT_PipeStalled;\r
 \r
                if (Pipe_IsStalled())\r
                  return PIPE_READYWAIT_PipeStalled;\r
-               else if (!(USB_IsConnected))\r
+               else if (USB_HostState == HOST_STATE_Unattached)\r
                  return PIPE_READYWAIT_DeviceDisconnected;\r
                          \r
                if (USB_INT_HasOccurred(USB_INT_HSOFI))\r
                  return PIPE_READYWAIT_DeviceDisconnected;\r
                          \r
                if (USB_INT_HasOccurred(USB_INT_HSOFI))\r
index d66edaf..b85705c 100644 (file)
@@ -14,9 +14,16 @@ uint8_t TEMPLATE_FUNC_NAME (void* Buffer, uint16_t Length)
                        \r
                        Endpoint_ClearOUT();\r
                }\r
                        \r
                        Endpoint_ClearOUT();\r
                }\r
+\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return ENDPOINT_RWCSTREAM_DeviceDisconnected;\r
        }\r
        \r
        }\r
        \r
-       while (!(Endpoint_IsINReady()));\r
+       while (!(Endpoint_IsINReady()))\r
+       {\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return ENDPOINT_RWCSTREAM_DeviceDisconnected;\r
+       }\r
        \r
        return ENDPOINT_RWCSTREAM_NoError;\r
 }\r
        \r
        return ENDPOINT_RWCSTREAM_NoError;\r
 }\r
index 2796c64..43a9f3d 100644 (file)
@@ -8,7 +8,11 @@ uint8_t TEMPLATE_FUNC_NAME (void* Buffer, uint16_t Length)
        \r
        while (Length && !(Endpoint_IsOUTReceived()))\r
        {\r
        \r
        while (Length && !(Endpoint_IsOUTReceived()))\r
        {\r
-               while (!(Endpoint_IsINReady()));\r
+               while (!(Endpoint_IsINReady()))\r
+               {\r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return ENDPOINT_RWCSTREAM_DeviceDisconnected;\r
+               }\r
                \r
                while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))\r
                {\r
                \r
                while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))\r
                {\r
@@ -25,11 +29,20 @@ uint8_t TEMPLATE_FUNC_NAME (void* Buffer, uint16_t Length)
        \r
        if (LastPacketFull)\r
        {\r
        \r
        if (LastPacketFull)\r
        {\r
-               while (!(Endpoint_IsINReady()));\r
+               while (!(Endpoint_IsINReady()))\r
+               {\r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return ENDPOINT_RWCSTREAM_DeviceDisconnected;         \r
+               }\r
+               \r
                Endpoint_ClearIN();\r
        }\r
        \r
                Endpoint_ClearIN();\r
        }\r
        \r
-       while (!(Endpoint_IsOUTReceived()));\r
+       while (!(Endpoint_IsOUTReceived()))\r
+       {\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return ENDPOINT_RWCSTREAM_DeviceDisconnected; \r
+       }\r
 \r
        return ENDPOINT_RWCSTREAM_NoError;\r
 }\r
 \r
        return ENDPOINT_RWCSTREAM_NoError;\r
 }\r
index 0c51940..b2500b0 100644 (file)
@@ -29,6 +29,8 @@
   *  - Added new USE_FLASH_DESCRIPTORS and TOTAL_NUM_CONFIGURATIONS compile time options\r
   *  - Added support for the new ATMEGA32U2, ATMEGA16U2 and ATMEGA8U2 AVR models\r
   *  - Added new PrinterHost demo (thanks to John Andrews)\r
   *  - Added new USE_FLASH_DESCRIPTORS and TOTAL_NUM_CONFIGURATIONS compile time options\r
   *  - Added support for the new ATMEGA32U2, ATMEGA16U2 and ATMEGA8U2 AVR models\r
   *  - Added new PrinterHost demo (thanks to John Andrews)\r
+  *  - Added new USB_DeviceState variable to keep track of the current Device mode USB state\r
+  *  - Added new Endpoint_ClearStatusStage() convenience function to assist with the status stages of control transfers\r
   *  \r
   *  <b>Changed:</b>\r
   *  - Deprecated psuedo-scheduler and removed dynamic memory allocator from the library (first no longer needed and second unused)\r
   *  \r
   *  <b>Changed:</b>\r
   *  - Deprecated psuedo-scheduler and removed dynamic memory allocator from the library (first no longer needed and second unused)\r
@@ -47,6 +49,8 @@
   *    descriptor is located can be specified. This means that descriptors can now be located in multiple memory spaces within a device.\r
   *  - Host mode demos now use sane terminal escape codes, so that text is always readable and events/program output is visually distinguished\r
   *    from oneanother using foreground colours\r
   *    descriptor is located can be specified. This means that descriptors can now be located in multiple memory spaces within a device.\r
   *  - Host mode demos now use sane terminal escape codes, so that text is always readable and events/program output is visually distinguished\r
   *    from oneanother using foreground colours\r
+  *  - Removed vague USB_IsConnected global - test USB_DeviceState or USB_HostState explicitly to gain previous functionality\r
+  *  - Removed USB_IsSuspended global - test USB_DeviceState against DEVICE_STATE_Suspended instead\r
   *\r
   *  <b>Fixed:</b>\r
   *  - Changed bootloaders to use FLASHEND rather than the existence of RAMPZ to determine if far FLASH pointers are needed to fix\r
   *\r
   *  <b>Fixed:</b>\r
   *  - Changed bootloaders to use FLASHEND rather than the existence of RAMPZ to determine if far FLASH pointers are needed to fix\r
@@ -91,6 +95,8 @@
   *    LUFA/Drivers/USB/Class/ directory to LUFA/Drivers/USB/HighLevel/ in preperation for the new USB class APIs\r
   *  - Moved out each demos' functionality library files (e.g. Ring Buffer library) to /Lib directories for a better directory structure\r
   *  - Removed Tx interrupt from the USBtoSerial demo; now sends characters via polling to ensure more time for the Rx interrupt\r
   *    LUFA/Drivers/USB/Class/ directory to LUFA/Drivers/USB/HighLevel/ in preperation for the new USB class APIs\r
   *  - Moved out each demos' functionality library files (e.g. Ring Buffer library) to /Lib directories for a better directory structure\r
   *  - Removed Tx interrupt from the USBtoSerial demo; now sends characters via polling to ensure more time for the Rx interrupt\r
+  *  - Fixed possible enumeration errors from spinloops which may fail to exit if the USB connection is severed before the exit condition\r
+  *    becomes true\r
   *\r
   *\r
   *  \section Sec_ChangeLog090510 Version 090510\r
   *\r
   *\r
   *  \section Sec_ChangeLog090510 Version 090510\r
index 2d9ec7a..f11dff2 100644 (file)
  *  On the smaller USB AVRs, the USB controller lacks VBUS events to determine the physical connection state of the USB bus to a host. In lieu of\r
  *  VBUS events, the library attempts to determine the connection state via the bus suspension and wake up events instead. This however may be\r
  *  slightly inaccurate due to the possibility of the host suspending the bus while the device is still connected. If accurate connection status is\r
  *  On the smaller USB AVRs, the USB controller lacks VBUS events to determine the physical connection state of the USB bus to a host. In lieu of\r
  *  VBUS events, the library attempts to determine the connection state via the bus suspension and wake up events instead. This however may be\r
  *  slightly inaccurate due to the possibility of the host suspending the bus while the device is still connected. If accurate connection status is\r
- *  required, the VBUS line of the USB connector should be routed to an AVR pin to detect its level, so that the USB_IsConnected global\r
+ *  required, the VBUS line of the USB connector should be routed to an AVR pin to detect its level, so that the USB_DeviceState global\r
  *  can be accurately set and the USB_Connect and USB_Disconnect events manually raised by the RAISE_EVENT macro. When defined, this token disables\r
  *  the library's auto-detection of the connection state by the aforementioned suspension and wake up events.\r
  *\r
  *  can be accurately set and the USB_Connect and USB_Disconnect events manually raised by the RAISE_EVENT macro. When defined, this token disables\r
  *  the library's auto-detection of the connection state by the aforementioned suspension and wake up events.\r
  *\r
index 945406d..5ea8d9c 100644 (file)
  *    - The "Dynamic Memory Block Allocator" has been removed, as it was unused in (and unrelated to) the LUFA library and never\r
  *      used in user applications. The library is available from the author's website for those wishing to still use it in their\r
  *      applications.\r
  *    - The "Dynamic Memory Block Allocator" has been removed, as it was unused in (and unrelated to) the LUFA library and never\r
  *      used in user applications. The library is available from the author's website for those wishing to still use it in their\r
  *      applications.\r
+ *    - The USB_IsConnected global has been removed, as it is too vague for general use. Test \ref USB_DeviceState or \ref USB_HostState\r
+ *      explicitly to ensure the device is in the desired state instead.\r
  *\r
  *  <b>Host Mode</b>\r
  *    - The HIDParser.c module has moved from LUFA/Drivers/USB/Class/ to LUFA/Drivers/USB/Class/Host/.\r
  *\r
  *  <b>Host Mode</b>\r
  *    - The HIDParser.c module has moved from LUFA/Drivers/USB/Class/ to LUFA/Drivers/USB/Class/Host/.\r
- *    - The USB_GetDeviceConfigDescriptor() function now requires the desired configuration index within the device as its first\r
+ *    - The \ref USB_GetDeviceConfigDescriptor() function now requires the desired configuration index within the device as its first\r
  *      parameter, to add support for multi-configuration devices. Existing code should use a configuration index of 1 to indicate the\r
  *      first configuration descriptor within the device.\r
  *      parameter, to add support for multi-configuration devices. Existing code should use a configuration index of 1 to indicate the\r
  *      first configuration descriptor within the device.\r
+ *    - The non-standard "Ready" host state has been removed. Existing \ref HOST_STATE_Configured code should be moved to the end of\r
+ *      the existing \ref HOST_STATE_Addressed state, and the existing HOST_STATE_Ready state code should be moved to the \ref HOST_STATE_Configured\r
+ *      state.\r
  *\r
  *  <b>Device Mode</b>\r
  *    - The \ref CALLBACK_USB_GetDescriptor() function now takes an extra parameter to specify the descriptor's memory space so that\r
  *      descriptors in mixed memory spaces can be used. The previous functionality can be returned by defining the USE_FLASH_DESCRIPTORS\r
  *      token in the project makefile to fix all descriptors into FLASH space and remove the extra function parameter.\r
  *\r
  *  <b>Device Mode</b>\r
  *    - The \ref CALLBACK_USB_GetDescriptor() function now takes an extra parameter to specify the descriptor's memory space so that\r
  *      descriptors in mixed memory spaces can be used. The previous functionality can be returned by defining the USE_FLASH_DESCRIPTORS\r
  *      token in the project makefile to fix all descriptors into FLASH space and remove the extra function parameter.\r
+ *    - Removed USB_IsSuspended - test \ref USB_DeviceState against \ref DEVICE_STATE_Suspended instead.\r
  *\r
  *\r
  * \section Sec_Migration090605 Migrating from 090510 to 090605\r
  *\r
  *\r
  * \section Sec_Migration090605 Migrating from 090510 to 090605\r
index 88902cf..1ae3704 100644 (file)
@@ -337,9 +337,6 @@ void HID_Host_Task(void)
                        USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                case HOST_STATE_Configured:\r
                        USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                case HOST_STATE_Configured:\r
-                       USB_HostState = HOST_STATE_Ready;\r
-                       break;\r
-               case HOST_STATE_Ready:\r
                        DiscardNextReport();\r
 \r
                        break;\r
                        DiscardNextReport();\r
 \r
                        break;\r