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

@ -117,13 +117,22 @@ void USB_Device_ProcessControlPacket(void)
static void USB_Device_SetAddress(void)
{
uint8_t DeviceAddress = (USB_ControlRequest.wValue & 0x7F);
Endpoint_ClearSETUP();
Endpoint_ClearIN();
while (!(Endpoint_IsINReady()));
while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
UDADDR = ((1 << ADDEN) | ((uint8_t)USB_ControlRequest.wValue & 0x7F));
UDADDR = ((1 << ADDEN) | DeviceAddress);
if (DeviceAddress)
USB_DeviceState = DEVICE_STATE_Addressed;
return;
}
@ -185,8 +194,17 @@ static void USB_Device_SetConfiguration(void)
Endpoint_ClearIN();
if (!(AlreadyConfigured) && USB_ConfigurationNumber)
EVENT_USB_DeviceEnumerationComplete();
if (USB_ConfigurationNumber)
{
USB_DeviceState = DEVICE_STATE_Configured;
if (!(AlreadyConfigured))
EVENT_USB_DeviceEnumerationComplete();
}
else
{
USB_DeviceState = DEVICE_STATE_Addressed;
}
EVENT_USB_ConfigurationChanged();
}
@ -199,7 +217,12 @@ void USB_Device_GetConfiguration(void)
Endpoint_ClearIN();
while (!(Endpoint_IsOUTReceived()));
while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
Endpoint_ClearOUT();
}
@ -332,7 +355,12 @@ static void USB_Device_GetStatus(void)
Endpoint_ClearIN();
while (!(Endpoint_IsOUTReceived()));
while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
Endpoint_ClearOUT();
}

View file

@ -118,7 +118,36 @@
#define USB_Device_IsUSBSuspended() ((UDINT & (1 << SUSPI)) ? true : false)
#endif
/* Type Defines: */
enum USB_Device_States_t
{
DEVICE_STATE_Unattached = 0, /**< Internally implemented by the library. This state indicates
* that the device is not currently connected to a host.
*/
DEVICE_STATE_Powered = 1, /**< Internally implemented by the library. This state indicates
* that the device is connected to a host, but enumeration has not
* yet begun.
*/
DEVICE_STATE_Default = 2, /**< Internally implemented by the library. This state indicates
* that the device's USB bus has been reset by the host and it is
* now waiting for the host to begin the enumeration process.
*/
DEVICE_STATE_Addressed = 3, /**< Internally implemented by the library. This state indicates
* that the device has been addressed by the USB Host, but is not
* yet configured.
*/
DEVICE_STATE_Configured = 4, /**< May be implemented by the user project. This state indicates
* that the device has been enumerated by the host and is ready
* for USB communications to begin.
*/
DEVICE_STATE_Suspended = 5, /**< May be implemented by the user project. This state indicates
* that the USB bus has been suspended by the host, and the device
* should power down to a minimal power level until the bus is
* resumed.
*/
};
/* Function Prototypes: */
/** Function to retrieve a given descriptor's size and memory location from the given descriptor type value,
* index and language ID. This function MUST be overridden in the user application (added with full, identical

View file

@ -71,6 +71,30 @@ void Endpoint_ClearEndpoints(void)
}
}
void Endpoint_ClearStatusStage(void)
{
if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
{
while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
Endpoint_ClearOUT();
}
else
{
while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
Endpoint_ClearIN();
}
}
#if !defined(CONTROL_ONLY_DEVICE)
uint8_t Endpoint_WaitUntilReady(void)
{
@ -93,7 +117,7 @@ uint8_t Endpoint_WaitUntilReady(void)
return ENDPOINT_READYWAIT_NoError;
}
if (!(USB_IsConnected))
if (USB_DeviceState == DEVICE_STATE_Unattached)
return ENDPOINT_READYWAIT_DeviceDisconnected;
else if (Endpoint_IsStalled())
return ENDPOINT_READYWAIT_EndpointStalled;

View file

@ -431,14 +431,14 @@
ENDPOINT_RWSTREAM_EndpointStalled = 1, /**< The endpoint was stalled during the stream
* transfer by the host or device.
*/
ENDPOINT_RWSTREAM_DeviceDisconnected = 1, /**< Device was disconnected from the host during
ENDPOINT_RWSTREAM_DeviceDisconnected = 2, /**< Device was disconnected from the host during
* the transfer.
*/
ENDPOINT_RWSTREAM_Timeout = 2, /**< The host failed to accept or send the next packet
ENDPOINT_RWSTREAM_Timeout = 3, /**< The host failed to accept or send the next packet
* within the software timeout period set by the
* \ref USB_STREAM_TIMEOUT_MS macro.
*/
ENDPOINT_RWSTREAM_CallbackAborted = 3, /**< Indicates that the stream's callback function
ENDPOINT_RWSTREAM_CallbackAborted = 4, /**< Indicates that the stream's callback function
* aborted the transfer early.
*/
};
@ -451,6 +451,9 @@
{
ENDPOINT_RWCSTREAM_NoError = 0, /**< Command completed successfully, no error. */
ENDPOINT_RWCSTREAM_HostAborted = 1, /**< The aborted the transfer prematurely. */
ENDPOINT_RWCSTREAM_DeviceDisconnected = 2, /**< Device was disconnected from the host during
* the transfer.
*/
};
/* Inline Functions: */
@ -726,6 +729,12 @@
* \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
*/
uint8_t Endpoint_WaitUntilReady(void);
/** Completes the status stage of a control transfer on a CONTROL type endpoint automatically,
* with respect to the data direction. This is a convenience function which can be used to
* simplify user control request handling.
*/
void Endpoint_ClearStatusStage(void);
/** Reads and discards the given number of bytes from the endpoint from the given buffer,
* discarding fully read packets from the host as needed. The last packet is not automatically
@ -922,6 +931,9 @@
* in both failure and success states; the user is responsible for manually clearing the setup OUT to
* finalize the transfer via the \ref Endpoint_ClearOUT() macro.
*
* \note This function automatically clears the control transfer's status stage. Do not manually attempt
* to clear the status stage when using this routine in a control transaction.
*
* \note This routine should only be used on CONTROL type endpoints.
*
* \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
@ -937,6 +949,9 @@
uint8_t Endpoint_Write_Control_Stream_LE(void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
/** EEPROM buffer source version of Endpoint_Write_Control_Stream_LE.
*
* \note This function automatically clears the control transfer's status stage. Do not manually attempt
* to clear the status stage when using this routine in a control transaction.
*
* \note This routine should only be used on CONTROL type endpoints.
*
@ -953,6 +968,9 @@
uint8_t Endpoint_Write_Control_EStream_LE(void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
/** FLASH buffer source version of \ref Endpoint_Write_Control_Stream_LE.
*
* \note This function automatically clears the control transfer's status stage. Do not manually attempt
* to clear the status stage when using this routine in a control transaction.
*
* \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
*
@ -975,6 +993,9 @@
* in both failure and success states; the user is responsible for manually clearing the setup OUT to
* finalize the transfer via the \ref Endpoint_ClearOUT() macro.
*
* \note This function automatically clears the control transfer's status stage. Do not manually attempt
* to clear the status stage when using this routine in a control transaction.
*
* \note This routine should only be used on CONTROL type endpoints.
*
* \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
@ -990,6 +1011,9 @@
uint8_t Endpoint_Write_Control_Stream_BE(void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
/** EEPROM buffer source version of \ref Endpoint_Write_Control_Stream_BE.
*
* \note This function automatically clears the control transfer's status stage. Do not manually attempt
* to clear the status stage when using this routine in a control transaction.
*
* \note This routine should only be used on CONTROL type endpoints.
*
@ -1006,6 +1030,9 @@
uint8_t Endpoint_Write_Control_EStream_BE(void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
/** FLASH buffer source version of \ref Endpoint_Write_Control_Stream_BE.
*
* \note This function automatically clears the control transfer's status stage. Do not manually attempt
* to clear the status stage when using this routine in a control transaction.
*
* \note The FLASH data must be located in the first 64KB of FLASH for this function to work correctly.
*
@ -1028,6 +1055,9 @@
* automatically sent after success or failure states; the user is responsible for manually sending the
* setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro.
*
* \note This function automatically clears the control transfer's status stage. Do not manually attempt
* to clear the status stage when using this routine in a control transaction.
*
* \note This routine should only be used on CONTROL type endpoints.
*
* \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
@ -1043,6 +1073,9 @@
uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
/** EEPROM buffer source version of \ref Endpoint_Read_Control_Stream_LE.
*
* \note This function automatically clears the control transfer's status stage. Do not manually attempt
* to clear the status stage when using this routine in a control transaction.
*
* \note This routine should only be used on CONTROL type endpoints.
*
@ -1063,6 +1096,9 @@
* automatically sent after success or failure states; the user is responsible for manually sending the
* setup IN to finalize the transfer via the \ref Endpoint_ClearIN() macro.
*
* \note This function automatically clears the control transfer's status stage. Do not manually attempt
* to clear the status stage when using this routine in a control transaction.
*
* \note This routine should only be used on CONTROL type endpoints.
*
* \warning Unlike the standard stream read/write commands, the control stream commands cannot be chained
@ -1078,6 +1114,9 @@
uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
/** EEPROM buffer source version of \ref Endpoint_Read_Control_Stream_BE.
*
* \note This function automatically clears the control transfer's status stage. Do not manually attempt
* to clear the status stage when using this routine in a control transaction.
*
* \note This routine should only be used on CONTROL type endpoints.
*

View file

@ -60,12 +60,12 @@ void USB_Host_ProcessNextHostState(void)
}
break;
case HOST_STATE_Attached:
case HOST_STATE_Powered:
WaitMSRemaining = HOST_DEVICE_SETTLE_DELAY_MS;
USB_HostState = HOST_STATE_Attached_WaitForDeviceSettle;
USB_HostState = HOST_STATE_Powered_WaitForDeviceSettle;
break;
case HOST_STATE_Attached_WaitForDeviceSettle:
case HOST_STATE_Powered_WaitForDeviceSettle:
#if HOST_DEVICE_SETTLE_DELAY_MS > 0
_delay_ms(1);
@ -77,14 +77,14 @@ void USB_Host_ProcessNextHostState(void)
USB_Host_VBUS_Auto_Enable();
USB_Host_VBUS_Auto_On();
USB_HostState = HOST_STATE_Attached_WaitForConnect;
USB_HostState = HOST_STATE_Powered_WaitForConnect;
}
#else
USB_HostState = HOST_STATE_Attached_WaitForConnect;
USB_HostState = HOST_STATE_Powered_WaitForConnect;
#endif
break;
case HOST_STATE_Attached_WaitForConnect:
case HOST_STATE_Powered_WaitForConnect:
if (USB_INT_HasOccurred(USB_INT_DCONNI))
{
USB_INT_Clear(USB_INT_DCONNI);
@ -93,22 +93,21 @@ void USB_Host_ProcessNextHostState(void)
USB_INT_Clear(USB_INT_VBERRI);
USB_INT_Enable(USB_INT_VBERRI);
USB_IsConnected = true;
EVENT_USB_Connect();
USB_Host_ResumeBus();
Pipe_ClearPipes();
HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Attached_DoReset);
HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Powered_DoReset);
}
break;
case HOST_STATE_Attached_DoReset:
case HOST_STATE_Powered_DoReset:
USB_Host_ResetDevice();
HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Powered);
HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Powered_ConfigPipe);
break;
case HOST_STATE_Powered:
case HOST_STATE_Powered_ConfigPipe:
Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL,
PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,
PIPE_CONTROLPIPE_DEFAULT_SIZE, PIPE_BANK_SINGLE);
@ -199,9 +198,7 @@ void USB_Host_ProcessNextHostState(void)
USB_Host_VBUS_Auto_Off();
EVENT_USB_DeviceUnattached();
if (USB_IsConnected)
EVENT_USB_Disconnect();
EVENT_USB_Disconnect();
USB_ResetInterface();
}
@ -222,7 +219,7 @@ uint8_t USB_Host_WaitMS(uint8_t MS)
MS--;
}
if ((USB_IsConnected == false) || (USB_CurrentMode == USB_MODE_DEVICE))
if ((USB_HostState == HOST_STATE_Unattached) || (USB_CurrentMode == USB_MODE_DEVICE))
{
ErrorCode = HOST_WAITERROR_DeviceDisconnect;

View file

@ -241,32 +241,32 @@
*
* \note Do not manually change to this state in the user code.
*/
HOST_STATE_Attached = 3, /**< Internally implemented by the library. This state indicates
HOST_STATE_Powered = 3, /**< Internally implemented by the library. This state indicates
* that a device has been attached, and the library's internals
* are being configured to begin the enumeration process.
*
* \note Do not manually change to this state in the user code.
*/
HOST_STATE_Attached_WaitForDeviceSettle = 4, /**< Internally implemented by the library. This state indicates
HOST_STATE_Powered_WaitForDeviceSettle = 4, /**< Internally implemented by the library. This state indicates
* that the stack is waiting for the initial settling period to
* elapse before beginning the enumeration process.
*
* \note Do not manually change to this state in the user code.
*/
HOST_STATE_Attached_WaitForConnect = 5, /**< Internally implemented by the library. This state indicates
HOST_STATE_Powered_WaitForConnect = 5, /**< Internally implemented by the library. This state indicates
* that the stack is waiting for a connection event from the USB
* controller to indicate a valid USB device has been attached to
* the bus and is ready to be enumerated.
*
* \note Do not manually change to this state in the user code.
*/
HOST_STATE_Attached_DoReset = 6, /**< Internally implemented by the library. This state indicates
HOST_STATE_Powered_DoReset = 6, /**< Internally implemented by the library. This state indicates
* that a valid USB device has been attached, and that it is
* will now be reset to ensure it is ready for enumeration.
*
* \note Do not manually change to this state in the user code.
*/
HOST_STATE_Powered = 7, /**< Internally implemented by the library. This state indicates
HOST_STATE_Powered_ConfigPipe = 7, /**< Internally implemented by the library. This state indicates
* that the attached device is currently powered and reset, and
* that the control pipe is now being configured by the stack.
*
@ -301,20 +301,12 @@
* retrieval and processing of the device descriptor) should also
* be placed in this state.
*/
HOST_STATE_Configured = 12, /**< May be implemented by the user project. This state should
* implement any extra device configuration (such as the setting of
* class-specific parameters) before normal communication is begun
* in the HOST_STATE_Ready state.
*/
HOST_STATE_Ready = 13, /**< May be implemented by the user project. This state should
* contain the main communications with the attached device. From this
* this state the host state machine should be changed to either
* HOST_STATE_Suspended (after the bus is manually suspended using the
* USB_Host_SuspendBus() macro) or HOST_STATE_WaitForDeviceRemoval as
* needed.
HOST_STATE_Configured = 12, /**< May be implemented by the user project. This state should implement the
* actual work performed on the attached device and changed to the
* HOST_STATE_Suspended or HOST_STATE_WaitForDeviceRemoval states as needed.
*/
HOST_STATE_Suspended = 15, /**< May be implemented by the user project. This state should be maintained
* while the bus is suspended, and changed to either the HOST_STATE_Ready
* while the bus is suspended, and changed to either the HOST_STATE_Configured
* (after resuming the bus with the USB_Host_ResumeBus() macro) or the
* HOST_STATE_WaitForDeviceRemoval states as needed.
*/

View file

@ -110,15 +110,21 @@ void USB_Init(
void USB_ShutDown(void)
{
if (USB_IsConnected)
#if defined(USB_CAN_BE_DEVICE)
if (USB_DeviceState != DEVICE_STATE_Unattached)
EVENT_USB_Disconnect();
#endif
#if defined(USB_CAN_BE_HOST)
if (USB_HostState != HOST_STATE_Unattached)
EVENT_USB_Disconnect();
#endif
USB_Detach();
USB_INT_DisableAllInterrupts();
USB_INT_ClearAllInterrupts();
USB_IsConnected = false;
USB_IsInitialized = false;
#if defined(USB_CAN_BE_HOST)
@ -126,7 +132,10 @@ void USB_ShutDown(void)
#endif
#if defined(USB_CAN_BE_DEVICE)
USB_ConfigurationNumber = 0;
USB_DeviceState = DEVICE_STATE_Unattached;
USB_ConfigurationNumber = 0;
USB_RemoteWakeupEnabled = false;
USB_CurrentlySelfPowered = false;
#endif
#if defined(CAN_BE_BOTH)
@ -149,16 +158,14 @@ void USB_ResetInterface(void)
{
USB_INT_DisableAllInterrupts();
USB_INT_ClearAllInterrupts();
USB_IsConnected = false;
#if defined(USB_CAN_BE_HOST)
USB_HostState = HOST_STATE_Unattached;
USB_HostState = HOST_STATE_Unattached;
#endif
#if defined(USB_CAN_BE_DEVICE)
USB_DeviceState = DEVICE_STATE_Unattached;
USB_ConfigurationNumber = 0;
USB_IsSuspended = false;
USB_RemoteWakeupEnabled = false;
USB_CurrentlySelfPowered = false;
#endif

View file

@ -93,7 +93,7 @@ uint8_t Pipe_WaitUntilReady(void)
if (Pipe_IsStalled())
return PIPE_READYWAIT_PipeStalled;
else if (!(USB_IsConnected))
else if (USB_HostState == HOST_STATE_Unattached)
return PIPE_READYWAIT_DeviceDisconnected;
if (USB_INT_HasOccurred(USB_INT_HSOFI))

View file

@ -14,9 +14,16 @@ uint8_t TEMPLATE_FUNC_NAME (void* Buffer, uint16_t Length)
Endpoint_ClearOUT();
}
if (USB_DeviceState == DEVICE_STATE_Unattached)
return ENDPOINT_RWCSTREAM_DeviceDisconnected;
}
while (!(Endpoint_IsINReady()));
while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return ENDPOINT_RWCSTREAM_DeviceDisconnected;
}
return ENDPOINT_RWCSTREAM_NoError;
}

View file

@ -8,7 +8,11 @@ uint8_t TEMPLATE_FUNC_NAME (void* Buffer, uint16_t Length)
while (Length && !(Endpoint_IsOUTReceived()))
{
while (!(Endpoint_IsINReady()));
while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return ENDPOINT_RWCSTREAM_DeviceDisconnected;
}
while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))
{
@ -25,11 +29,20 @@ uint8_t TEMPLATE_FUNC_NAME (void* Buffer, uint16_t Length)
if (LastPacketFull)
{
while (!(Endpoint_IsINReady()));
while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return ENDPOINT_RWCSTREAM_DeviceDisconnected;
}
Endpoint_ClearIN();
}
while (!(Endpoint_IsOUTReceived()));
while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return ENDPOINT_RWCSTREAM_DeviceDisconnected;
}
return ENDPOINT_RWCSTREAM_NoError;
}