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.
This commit is contained in:
Dean Camera 2009-07-21 13:31:21 +00:00
parent 44179abcf8
commit e071f3897a
58 changed files with 666 additions and 463 deletions

View file

@ -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();

View file

@ -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;

View file

@ -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();

View file

@ -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();

View file

@ -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 */

View file

@ -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;
}

View file

@ -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();

View file

@ -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();

View file

@ -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();

View file

@ -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);

View file

@ -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);

View file

@ -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;
}
}