From e071f3897a0946c6be1e1b5e1f78eda8dcbf6fc7 Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Tue, 21 Jul 2009 13:31:21 +0000 Subject: Added new USB_DeviceState variable to keep track of the current Device mode USB state. 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. --- .../BluetoothHost/Lib/BluetoothHCICommands.c | 49 ++++++++++++++++++---- .../Incomplete/BluetoothHost/Lib/BluetoothStack.c | 2 +- Demos/Host/LowLevel/CDCHost/CDCHost.c | 9 ++-- .../Host/LowLevel/GenericHIDHost/GenericHIDHost.c | 7 +--- Demos/Host/LowLevel/KeyboardHost/KeyboardHost.c | 7 +--- .../KeyboardHostWithParser.c | 7 +--- .../MassStorageHost/Lib/MassStoreCommands.c | 8 +++- .../LowLevel/MassStorageHost/MassStorageHost.c | 23 +++++----- Demos/Host/LowLevel/MouseHost/MouseHost.c | 9 ++-- .../MouseHostWithParser/MouseHostWithParser.c | 9 ++-- .../LowLevel/PrinterHost/Lib/PrinterCommands.c | 6 ++- Demos/Host/LowLevel/PrinterHost/PrinterHost.c | 9 ++-- .../StillImageHost/Lib/StillImageCommands.c | 9 ++-- .../Host/LowLevel/StillImageHost/StillImageHost.c | 11 ++--- 14 files changed, 88 insertions(+), 77 deletions(-) (limited to 'Demos/Host') diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothHCICommands.c b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothHCICommands.c index ddd159ef7..54291bf75 100644 --- a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothHCICommands.c +++ b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothHCICommands.c @@ -114,7 +114,12 @@ void Bluetooth_ProcessHCICommands(void) do { - while (!(Bluetooth_GetNextHCIEventHeader())); + while (!(Bluetooth_GetNextHCIEventHeader())) + { + if (USB_HostState == HOST_STATE_Unattached) + return; + } + Bluetooth_DiscardRemainingHCIEventParameters(); } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE); @@ -133,7 +138,12 @@ void Bluetooth_ProcessHCICommands(void) do { - while (!(Bluetooth_GetNextHCIEventHeader())); + while (!(Bluetooth_GetNextHCIEventHeader())) + { + if (USB_HostState == HOST_STATE_Unattached) + return; + } + Bluetooth_DiscardRemainingHCIEventParameters(); } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE); @@ -155,7 +165,12 @@ void Bluetooth_ProcessHCICommands(void) EventMask[3], EventMask[2], EventMask[1], EventMask[0]); do { - while (!(Bluetooth_GetNextHCIEventHeader())); + while (!(Bluetooth_GetNextHCIEventHeader())) + { + if (USB_HostState == HOST_STATE_Unattached) + return; + } + Bluetooth_DiscardRemainingHCIEventParameters(); } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE); @@ -176,7 +191,12 @@ void Bluetooth_ProcessHCICommands(void) do { - while (!(Bluetooth_GetNextHCIEventHeader())); + while (!(Bluetooth_GetNextHCIEventHeader())) + { + if (USB_HostState == HOST_STATE_Unattached) + return; + } + Bluetooth_DiscardRemainingHCIEventParameters(); } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE); @@ -195,7 +215,12 @@ void Bluetooth_ProcessHCICommands(void) do { - while (!(Bluetooth_GetNextHCIEventHeader())); + while (!(Bluetooth_GetNextHCIEventHeader())) + { + if (USB_HostState == HOST_STATE_Unattached) + return; + } + Bluetooth_DiscardRemainingHCIEventParameters(); } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE); @@ -215,7 +240,12 @@ void Bluetooth_ProcessHCICommands(void) do { - while (!(Bluetooth_GetNextHCIEventHeader())); + while (!(Bluetooth_GetNextHCIEventHeader())) + { + if (USB_HostState == HOST_STATE_Unattached) + return; + } + Bluetooth_DiscardRemainingHCIEventParameters(); } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE); @@ -366,7 +396,12 @@ void Bluetooth_ProcessHCICommands(void) do { - while (!(Bluetooth_GetNextHCIEventHeader())); + while (!(Bluetooth_GetNextHCIEventHeader())) + { + if (USB_DeviceState == DEVICE_STATE_Unattached) + return; + } + Bluetooth_DiscardRemainingHCIEventParameters(); } while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE); diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c index e2659102e..628fa217c 100644 --- a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c +++ b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c @@ -41,7 +41,7 @@ Bluetooth_Device_t Bluetooth_DeviceConfiguration ATTR_WEAK = void Bluetooth_Stack_Task(void) { - if (!(USB_IsConnected) || (USB_HostState != HOST_STATE_Ready)) + if (USB_HostState != HOST_STATE_Configured) Bluetooth_HCIProcessingState = Bluetooth_Init; Bluetooth_ProcessHCICommands(); diff --git a/Demos/Host/LowLevel/CDCHost/CDCHost.c b/Demos/Host/LowLevel/CDCHost/CDCHost.c index a3cde7826..e20fa2cfc 100644 --- a/Demos/Host/LowLevel/CDCHost/CDCHost.c +++ b/Demos/Host/LowLevel/CDCHost/CDCHost.c @@ -164,15 +164,12 @@ void CDC_Host_Task(void) USB_HostState = HOST_STATE_WaitForDeviceRemoval; break; } - + + puts_P(PSTR("CDC Device Enumerated.\r\n")); + USB_HostState = HOST_STATE_Configured; break; case HOST_STATE_Configured: - puts_P(PSTR("CDC Device Enumerated.\r\n")); - - USB_HostState = HOST_STATE_Ready; - break; - case HOST_STATE_Ready: /* Select and the data IN pipe */ Pipe_SelectPipe(CDC_DATAPIPE_IN); Pipe_Unfreeze(); diff --git a/Demos/Host/LowLevel/GenericHIDHost/GenericHIDHost.c b/Demos/Host/LowLevel/GenericHIDHost/GenericHIDHost.c index 3a055c820..d36064333 100644 --- a/Demos/Host/LowLevel/GenericHIDHost/GenericHIDHost.c +++ b/Demos/Host/LowLevel/GenericHIDHost/GenericHIDHost.c @@ -266,14 +266,11 @@ void HID_Host_Task(void) break; } + puts_P(PSTR("HID Device Enumerated.\r\n")); + USB_HostState = HOST_STATE_Configured; break; case HOST_STATE_Configured: - puts_P(PSTR("HID Device Enumerated.\r\n")); - - USB_HostState = HOST_STATE_Ready; - break; - case HOST_STATE_Ready: ReadNextReport(); break; diff --git a/Demos/Host/LowLevel/KeyboardHost/KeyboardHost.c b/Demos/Host/LowLevel/KeyboardHost/KeyboardHost.c index c2d8dd4a6..273cd7175 100644 --- a/Demos/Host/LowLevel/KeyboardHost/KeyboardHost.c +++ b/Demos/Host/LowLevel/KeyboardHost/KeyboardHost.c @@ -230,9 +230,6 @@ void Keyboard_HID_Task(void) break; } - USB_HostState = HOST_STATE_Configured; - break; - case HOST_STATE_Configured: /* HID class request to set the keyboard protocol to the Boot Protocol */ USB_ControlRequest = (USB_Request_Header_t) { @@ -262,9 +259,9 @@ void Keyboard_HID_Task(void) puts_P(PSTR("Keyboard Enumerated.\r\n")); - USB_HostState = HOST_STATE_Ready; + USB_HostState = HOST_STATE_Configured; break; - case HOST_STATE_Ready: + case HOST_STATE_Configured: /* If a report has been received, read and process it */ ReadNextReport(); diff --git a/Demos/Host/LowLevel/KeyboardHostWithParser/KeyboardHostWithParser.c b/Demos/Host/LowLevel/KeyboardHostWithParser/KeyboardHostWithParser.c index dc1707bd0..eee1755bf 100644 --- a/Demos/Host/LowLevel/KeyboardHostWithParser/KeyboardHostWithParser.c +++ b/Demos/Host/LowLevel/KeyboardHostWithParser/KeyboardHostWithParser.c @@ -166,9 +166,6 @@ void Keyboard_HID_Task(void) break; } - USB_HostState = HOST_STATE_Configured; - break; - case HOST_STATE_Configured: puts_P(PSTR("Processing HID Report.\r\n")); /* Get and process the device's first HID report descriptor */ @@ -187,9 +184,9 @@ void Keyboard_HID_Task(void) puts_P(PSTR("Keyboard Enumerated.\r\n")); - USB_HostState = HOST_STATE_Ready; + USB_HostState = HOST_STATE_Configured; break; - case HOST_STATE_Ready: + case HOST_STATE_Configured: /* Select and unfreeze keyboard data pipe */ Pipe_SelectPipe(KEYBOARD_DATAPIPE); Pipe_Unfreeze(); diff --git a/Demos/Host/LowLevel/MassStorageHost/Lib/MassStoreCommands.c b/Demos/Host/LowLevel/MassStorageHost/Lib/MassStoreCommands.c index 7ee7de138..d0518df1f 100644 --- a/Demos/Host/LowLevel/MassStorageHost/Lib/MassStoreCommands.c +++ b/Demos/Host/LowLevel/MassStorageHost/Lib/MassStoreCommands.c @@ -154,7 +154,7 @@ static uint8_t MassStore_WaitForDataReceived(void) } /* Check to see if the device was disconnected, if so exit function */ - if (!(USB_IsConnected)) + if (USB_HostState == HOST_STATE_Unattached) return PIPE_RWSTREAM_DeviceDisconnected; }; @@ -206,7 +206,11 @@ static uint8_t MassStore_SendReceiveData(void* BufferPtr) /* Acknowledge the packet */ Pipe_ClearOUT(); - while (!(Pipe_IsOUTReady())); + while (!(Pipe_IsOUTReady())) + { + if (USB_HostState == HOST_STATE_Unattached) + return PIPE_RWSTREAM_DeviceDisconnected; + } } /* Freeze used pipe after use */ diff --git a/Demos/Host/LowLevel/MassStorageHost/MassStorageHost.c b/Demos/Host/LowLevel/MassStorageHost/MassStorageHost.c index 1adec5bcf..6cfe669f6 100644 --- a/Demos/Host/LowLevel/MassStorageHost/MassStorageHost.c +++ b/Demos/Host/LowLevel/MassStorageHost/MassStorageHost.c @@ -171,14 +171,11 @@ void MassStorage_Task(void) break; } + puts_P(PSTR("Mass Storage Disk Enumerated.\r\n")); + USB_HostState = HOST_STATE_Configured; break; case HOST_STATE_Configured: - puts_P(PSTR("Mass Storage Disk Enumerated.\r\n")); - - USB_HostState = HOST_STATE_Ready; - break; - case HOST_STATE_Ready: /* Indicate device busy via the status LEDs */ LEDs_SetAllLEDs(LEDMASK_USB_BUSY); @@ -241,7 +238,11 @@ void MassStorage_Task(void) { Serial_TxByte('.'); - if ((ErrorCode = MassStore_TestUnitReady(0)) != 0) + /* Abort if device removed */ + if (USB_HostState == HOST_STATE_Unattached) + break; + + if ((ErrorCode = MassStore_TestUnitReady(0)) != PIPE_RWSTREAM_NoError) { ShowDiskReadError(PSTR("Test Unit Ready"), false, ErrorCode); @@ -249,11 +250,7 @@ void MassStorage_Task(void) break; } } - while ((SCSICommandStatus.Status != Command_Pass) && USB_IsConnected); - - /* Abort if device removed */ - if (!(USB_IsConnected)) - break; + while (SCSICommandStatus.Status != Command_Pass); puts_P(PSTR("\r\nRetrieving Capacity... ")); @@ -320,7 +317,7 @@ void MassStorage_Task(void) while (!(Buttons_GetStatus() & BUTTONS_BUTTON1)) { /* Abort if device removed */ - if (!(USB_IsConnected)) + if (USB_HostState == HOST_STATE_Unattached) break; } @@ -346,7 +343,7 @@ void MassStorage_Task(void) } /* Abort if device removed */ - if (!(USB_IsConnected)) + if (USB_HostState == HOST_STATE_Unattached) break; } diff --git a/Demos/Host/LowLevel/MouseHost/MouseHost.c b/Demos/Host/LowLevel/MouseHost/MouseHost.c index 5340aa11f..b98f4bf96 100644 --- a/Demos/Host/LowLevel/MouseHost/MouseHost.c +++ b/Demos/Host/LowLevel/MouseHost/MouseHost.c @@ -226,9 +226,6 @@ void Mouse_HID_Task(void) break; } - USB_HostState = HOST_STATE_Configured; - break; - case HOST_STATE_Configured: /* HID class request to set the mouse protocol to the Boot Protocol */ USB_ControlRequest = (USB_Request_Header_t) { @@ -257,10 +254,10 @@ void Mouse_HID_Task(void) } puts_P(PSTR("Mouse Enumerated.\r\n")); - - USB_HostState = HOST_STATE_Ready; + + USB_HostState = HOST_STATE_Configured; break; - case HOST_STATE_Ready: + case HOST_STATE_Configured: /* If a report has been received, read and process it */ ReadNextReport(); diff --git a/Demos/Host/LowLevel/MouseHostWithParser/MouseHostWithParser.c b/Demos/Host/LowLevel/MouseHostWithParser/MouseHostWithParser.c index 42865e515..63fce0dcb 100644 --- a/Demos/Host/LowLevel/MouseHostWithParser/MouseHostWithParser.c +++ b/Demos/Host/LowLevel/MouseHostWithParser/MouseHostWithParser.c @@ -166,9 +166,6 @@ void Mouse_HID_Task(void) break; } - USB_HostState = HOST_STATE_Configured; - break; - case HOST_STATE_Configured: puts_P(PSTR("Processing HID Report.\r\n")); /* Get and process the device's first HID report descriptor */ @@ -186,10 +183,10 @@ void Mouse_HID_Task(void) } puts_P(PSTR("Mouse Enumerated.\r\n")); - - USB_HostState = HOST_STATE_Ready; + + USB_HostState = HOST_STATE_Configured; break; - case HOST_STATE_Ready: + case HOST_STATE_Configured: /* Select and unfreeze mouse data pipe */ Pipe_SelectPipe(MOUSE_DATAPIPE); Pipe_Unfreeze(); diff --git a/Demos/Host/LowLevel/PrinterHost/Lib/PrinterCommands.c b/Demos/Host/LowLevel/PrinterHost/Lib/PrinterCommands.c index 4c88bb4f2..cb051cdf1 100644 --- a/Demos/Host/LowLevel/PrinterHost/Lib/PrinterCommands.c +++ b/Demos/Host/LowLevel/PrinterHost/Lib/PrinterCommands.c @@ -54,7 +54,11 @@ uint8_t Printer_SendData(Printer_Data_t* PrinterCommands) return ErrorCode; Pipe_ClearOUT(); - while (!(Pipe_IsOUTReady())); + while (!(Pipe_IsOUTReady())) + { + if (USB_HostState == HOST_STATE_Unattached) + return PIPE_RWSTREAM_DeviceDisconnected; + } Pipe_Freeze(); diff --git a/Demos/Host/LowLevel/PrinterHost/PrinterHost.c b/Demos/Host/LowLevel/PrinterHost/PrinterHost.c index 3b145a63d..bbda0c24b 100644 --- a/Demos/Host/LowLevel/PrinterHost/PrinterHost.c +++ b/Demos/Host/LowLevel/PrinterHost/PrinterHost.c @@ -195,9 +195,6 @@ void USB_Printer_Host(void) } } - USB_HostState = HOST_STATE_Configured; - break; - case HOST_STATE_Configured: puts_P(PSTR("Retrieving Device ID...\r\n")); char DeviceIDString[256]; @@ -217,10 +214,10 @@ void USB_Printer_Host(void) printf_P(PSTR("Printer Device ID: %s\r\n"), DeviceIDString); puts_P(PSTR("Printer Enumerated.\r\n")); - - USB_HostState = HOST_STATE_Ready; + + USB_HostState = HOST_STATE_Configured; break; - case HOST_STATE_Ready: + case HOST_STATE_Configured: /* Indicate device busy via the status LEDs */ LEDs_SetAllLEDs(LEDMASK_USB_BUSY); diff --git a/Demos/Host/LowLevel/StillImageHost/Lib/StillImageCommands.c b/Demos/Host/LowLevel/StillImageHost/Lib/StillImageCommands.c index e0d71a059..970df4f87 100644 --- a/Demos/Host/LowLevel/StillImageHost/Lib/StillImageCommands.c +++ b/Demos/Host/LowLevel/StillImageHost/Lib/StillImageCommands.c @@ -152,12 +152,9 @@ uint8_t SImage_RecieveBlockHeader(void) } /* Check to see if the device was disconnected, if so exit function */ - if (!(USB_IsConnected)) - { - /* Return error code */ - return PIPE_RWSTREAM_DeviceDisconnected; - } - }; + if (USB_HostState == HOST_STATE_Unattached) + return PIPE_RWSTREAM_DeviceDisconnected; + } /* Freeze OUT pipe after use */ Pipe_SelectPipe(SIMAGE_DATA_OUT_PIPE); diff --git a/Demos/Host/LowLevel/StillImageHost/StillImageHost.c b/Demos/Host/LowLevel/StillImageHost/StillImageHost.c index 5cfaf46be..ca0771a94 100644 --- a/Demos/Host/LowLevel/StillImageHost/StillImageHost.c +++ b/Demos/Host/LowLevel/StillImageHost/StillImageHost.c @@ -166,14 +166,11 @@ void StillImage_Task(void) break; } + puts_P(PSTR("Still Image Device Enumerated.\r\n")); + USB_HostState = HOST_STATE_Configured; break; case HOST_STATE_Configured: - puts_P(PSTR("Still Image Device Enumerated.\r\n")); - - USB_HostState = HOST_STATE_Ready; - break; - case HOST_STATE_Ready: /* Indicate device busy via the status LEDs */ LEDs_SetAllLEDs(LEDMASK_USB_BUSY); @@ -331,9 +328,7 @@ void StillImage_Task(void) /* Indicate device no longer busy */ LEDs_SetAllLEDs(LEDMASK_USB_READY); - /* Wait until USB device disconnected */ - while (USB_IsConnected); - + USB_HostState = HOST_STATE_WaitForDeviceRemoval; break; } } -- cgit v1.2.3