\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
- 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
- UDADDR = ((1 << ADDEN) | ((uint8_t)USB_ControlRequest.wValue & 0x7F));\r
+ if (DeviceAddress)\r
+ USB_DeviceState = DEVICE_STATE_Addressed;\r
\r
return;\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
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
\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