Commit e071f389 authored by Dean Camera's avatar Dean Camera
Browse files

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.
parent 44179abc
...@@ -165,9 +165,7 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -165,9 +165,7 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearIN(); Endpoint_ClearIN();
/* Acknowledge status stage */ Endpoint_ClearStatusStage();
while (!(Endpoint_IsOUTReceived()));
Endpoint_ClearOUT();
} }
break; break;
...@@ -176,16 +174,18 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -176,16 +174,18 @@ void EVENT_USB_UnhandledControlPacket(void)
{ {
Endpoint_ClearSETUP(); Endpoint_ClearSETUP();
while (!(Endpoint_IsOUTReceived())); while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
for (uint8_t i = 0; i < sizeof(LineCoding); i++) for (uint8_t i = 0; i < sizeof(LineCoding); i++)
*(LineCodingData++) = Endpoint_Read_Byte(); *(LineCodingData++) = Endpoint_Read_Byte();
Endpoint_ClearOUT(); Endpoint_ClearOUT();
/* Acknowledge status stage */ Endpoint_ClearStatusStage();
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
} }
break; break;
...@@ -194,9 +194,7 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -194,9 +194,7 @@ void EVENT_USB_UnhandledControlPacket(void)
{ {
Endpoint_ClearSETUP(); Endpoint_ClearSETUP();
/* Acknowledge status stage */ Endpoint_ClearStatusStage();
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
} }
break; break;
...@@ -333,7 +331,12 @@ static uint8_t FetchNextCommandByte(void) ...@@ -333,7 +331,12 @@ static uint8_t FetchNextCommandByte(void)
while (!(Endpoint_IsReadWriteAllowed())) while (!(Endpoint_IsReadWriteAllowed()))
{ {
Endpoint_ClearOUT(); Endpoint_ClearOUT();
while (!(Endpoint_IsOUTReceived()));
while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return 0;
}
} }
/* Fetch the next byte from the OUT endpoint */ /* Fetch the next byte from the OUT endpoint */
...@@ -354,7 +357,12 @@ static void WriteNextResponseByte(const uint8_t Response) ...@@ -354,7 +357,12 @@ static void WriteNextResponseByte(const uint8_t Response)
if (!(Endpoint_IsReadWriteAllowed())) if (!(Endpoint_IsReadWriteAllowed()))
{ {
Endpoint_ClearIN(); Endpoint_ClearIN();
while (!(Endpoint_IsINReady()));
while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
} }
/* Write the next byte to the OUT endpoint */ /* Write the next byte to the OUT endpoint */
...@@ -563,12 +571,21 @@ void CDC_Task(void) ...@@ -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 */ /* If a full endpoint's worth of data was sent, we need to send an empty packet afterwards to signal end of transfer */
if (IsEndpointFull) if (IsEndpointFull)
{ {
while (!(Endpoint_IsINReady())); while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
Endpoint_ClearIN(); Endpoint_ClearIN();
} }
/* Wait until the data has been sent to the host */ /* Wait until the data has been sent to the host */
while (!(Endpoint_IsINReady())); while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
/* Select the OUT endpoint */ /* Select the OUT endpoint */
Endpoint_SelectEndpoint(CDC_RX_EPNUM); Endpoint_SelectEndpoint(CDC_RX_EPNUM);
......
...@@ -177,7 +177,11 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -177,7 +177,11 @@ void EVENT_USB_UnhandledControlPacket(void)
/* If the request has a data stage, load it into the command struct */ /* If the request has a data stage, load it into the command struct */
if (SentCommand.DataSize) if (SentCommand.DataSize)
{ {
while (!(Endpoint_IsOUTReceived())); while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
/* First byte of the data stage is the DNLOAD request's command */ /* First byte of the data stage is the DNLOAD request's command */
SentCommand.Command = Endpoint_Read_Byte(); SentCommand.Command = Endpoint_Read_Byte();
...@@ -235,7 +239,12 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -235,7 +239,12 @@ void EVENT_USB_UnhandledControlPacket(void)
if (!(Endpoint_BytesInEndpoint())) if (!(Endpoint_BytesInEndpoint()))
{ {
Endpoint_ClearOUT(); Endpoint_ClearOUT();
while (!(Endpoint_IsOUTReceived()));
while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
} }
/* Write the next word into the current flash page */ /* Write the next word into the current flash page */
...@@ -279,7 +288,12 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -279,7 +288,12 @@ void EVENT_USB_UnhandledControlPacket(void)
if (!(Endpoint_BytesInEndpoint())) if (!(Endpoint_BytesInEndpoint()))
{ {
Endpoint_ClearOUT(); Endpoint_ClearOUT();
while (!(Endpoint_IsOUTReceived()));
while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
} }
/* Read the byte from the USB interface and write to to the EEPROM */ /* Read the byte from the USB interface and write to to the EEPROM */
...@@ -297,16 +311,18 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -297,16 +311,18 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearOUT(); Endpoint_ClearOUT();
/* Acknowledge status stage */ Endpoint_ClearStatusStage();
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
break; break;
case DFU_UPLOAD: case DFU_UPLOAD:
Endpoint_ClearSETUP(); Endpoint_ClearSETUP();
while (!(Endpoint_IsINReady())); while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
if (DFU_State != dfuUPLOAD_IDLE) if (DFU_State != dfuUPLOAD_IDLE)
{ {
if ((DFU_State == dfuERROR) && IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01)) // Blank Check if ((DFU_State == dfuERROR) && IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01)) // Blank Check
...@@ -343,7 +359,12 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -343,7 +359,12 @@ void EVENT_USB_UnhandledControlPacket(void)
if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE) if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE)
{ {
Endpoint_ClearIN(); Endpoint_ClearIN();
while (!(Endpoint_IsINReady()));
while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
} }
/* Read the flash word and send it via USB to the host */ /* Read the flash word and send it via USB to the host */
...@@ -368,7 +389,12 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -368,7 +389,12 @@ void EVENT_USB_UnhandledControlPacket(void)
if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE) if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE)
{ {
Endpoint_ClearIN(); Endpoint_ClearIN();
while (!(Endpoint_IsINReady()));
while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
} }
/* Read the EEPROM byte and send it via USB to the host */ /* Read the EEPROM byte and send it via USB to the host */
...@@ -385,10 +411,7 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -385,10 +411,7 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearIN(); Endpoint_ClearIN();
/* Acknowledge status stage */ Endpoint_ClearStatusStage();
while (!(Endpoint_IsOUTReceived()));
Endpoint_ClearOUT();
break; break;
case DFU_GETSTATUS: case DFU_GETSTATUS:
Endpoint_ClearSETUP(); Endpoint_ClearSETUP();
...@@ -408,10 +431,7 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -408,10 +431,7 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearIN(); Endpoint_ClearIN();
/* Acknowledge status stage */ Endpoint_ClearStatusStage();
while (!(Endpoint_IsOUTReceived()));
Endpoint_ClearOUT();
break; break;
case DFU_CLRSTATUS: case DFU_CLRSTATUS:
Endpoint_ClearSETUP(); Endpoint_ClearSETUP();
...@@ -419,10 +439,7 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -419,10 +439,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Reset the status value variable to the default OK status */ /* Reset the status value variable to the default OK status */
DFU_Status = OK; DFU_Status = OK;
/* Acknowledge status stage */ Endpoint_ClearStatusStage();
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
break; break;
case DFU_GETSTATE: case DFU_GETSTATE:
Endpoint_ClearSETUP(); Endpoint_ClearSETUP();
...@@ -432,21 +449,15 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -432,21 +449,15 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearIN(); Endpoint_ClearIN();
/* Acknowledge status stage */ Endpoint_ClearStatusStage();
while (!(Endpoint_IsOUTReceived()));
Endpoint_ClearOUT();
break; break;
case DFU_ABORT: case DFU_ABORT:
Endpoint_ClearSETUP(); Endpoint_ClearSETUP();
/* Reset the current state variable to the default idle state */ /* Reset the current state variable to the default idle state */
DFU_State = dfuIDLE; DFU_State = dfuIDLE;
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
break; break;
} }
} }
...@@ -465,7 +476,11 @@ static void DiscardFillerBytes(uint8_t NumberOfBytes) ...@@ -465,7 +476,11 @@ static void DiscardFillerBytes(uint8_t NumberOfBytes)
Endpoint_ClearOUT(); Endpoint_ClearOUT();
/* Wait until next data packet received */ /* Wait until next data packet received */
while (!(Endpoint_IsOUTReceived())); while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
} }
else else
{ {
......
...@@ -119,7 +119,7 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -119,7 +119,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Wait until the command (report) has been sent by the host */ /* Wait until the command (report) has been sent by the host */
while (!(Endpoint_IsOUTReceived())); while (!(Endpoint_IsOUTReceived()));
/* Read in the write destination address */ /* Read in the write destination address */
uint16_t PageAddress = Endpoint_Read_Word_LE(); uint16_t PageAddress = Endpoint_Read_Word_LE();
...@@ -158,9 +158,7 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -158,9 +158,7 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearOUT(); Endpoint_ClearOUT();
/* Acknowledge status stage */ Endpoint_ClearStatusStage();
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
} }
break; break;
......
...@@ -69,7 +69,11 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, co ...@@ -69,7 +69,11 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, co
Dataflash_SendAddressBytes(0, CurrDFPageByte); Dataflash_SendAddressBytes(0, CurrDFPageByte);
/* Wait until endpoint is ready before continuing */ /* Wait until endpoint is ready before continuing */
while (!(Endpoint_IsReadWriteAllowed())); while (!(Endpoint_IsReadWriteAllowed()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
while (TotalBlocks) while (TotalBlocks)
{ {
...@@ -85,7 +89,11 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, co ...@@ -85,7 +89,11 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, co
Endpoint_ClearOUT(); Endpoint_ClearOUT();
/* Wait until the host has sent another packet */ /* Wait until the host has sent another packet */
while (!(Endpoint_IsReadWriteAllowed())); while (!(Endpoint_IsReadWriteAllowed()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
} }
/* Check if end of dataflash page reached */ /* Check if end of dataflash page reached */
...@@ -197,7 +205,11 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, con ...@@ -197,7 +205,11 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, con
Dataflash_SendByte(0x00); Dataflash_SendByte(0x00);
/* Wait until endpoint is ready before continuing */ /* Wait until endpoint is ready before continuing */
while (!(Endpoint_IsReadWriteAllowed())); while (!(Endpoint_IsReadWriteAllowed()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
while (TotalBlocks) while (TotalBlocks)
{ {
...@@ -213,7 +225,11 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, con ...@@ -213,7 +225,11 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, con
Endpoint_ClearIN(); Endpoint_ClearIN();
/* Wait until the endpoint is ready for more data */ /* Wait until the endpoint is ready for more data */
while (!(Endpoint_IsReadWriteAllowed())); while (!(Endpoint_IsReadWriteAllowed()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
} }
/* Check if end of dataflash page reached */ /* Check if end of dataflash page reached */
......
...@@ -152,7 +152,7 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -152,7 +152,7 @@ void EVENT_USB_UnhandledControlPacket(void)
*/ */
ISR(USART1_RX_vect, ISR_BLOCK) ISR(USART1_RX_vect, ISR_BLOCK)
{ {
if (USB_IsConnected) if (USB_DeviceState == DEVICE_STATE_Configured)
Buffer_StoreElement(&Tx_Buffer, UDR1); Buffer_StoreElement(&Tx_Buffer, UDR1);
} }
......
...@@ -142,7 +142,7 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -142,7 +142,7 @@ void EVENT_USB_UnhandledControlPacket(void)
void SideShow_Task(void) void SideShow_Task(void)
{ {
/* Device must be connected and configured for the task to run */ /* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber)) if (USB_DeviceState != DEVICE_STATE_Configured)
return; return;
/* Select the SideShow data out endpoint */ /* Select the SideShow data out endpoint */
......
...@@ -139,9 +139,7 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -139,9 +139,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */ /* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */
StreamingAudioInterfaceSelected = ((USB_ControlRequest.wValue) != 0); StreamingAudioInterfaceSelected = ((USB_ControlRequest.wValue) != 0);
/* Acknowledge status stage */ Endpoint_ClearStatusStage();
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
} }
break; break;
...@@ -152,7 +150,7 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -152,7 +150,7 @@ void EVENT_USB_UnhandledControlPacket(void)
void USB_Audio_Task(void) void USB_Audio_Task(void)
{ {
/* Device must be connected and configured for the task to run */ /* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber)) if (USB_DeviceState != DEVICE_STATE_Configured)
return; return;
/* Check to see if the streaming interface is selected, if not the host is not receiving audio */ /* Check to see if the streaming interface is selected, if not the host is not receiving audio */
......
...@@ -166,9 +166,7 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -166,9 +166,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */ /* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */
StreamingAudioInterfaceSelected = ((USB_ControlRequest.wValue) != 0); StreamingAudioInterfaceSelected = ((USB_ControlRequest.wValue) != 0);
/* Acknowledge status stage */ Endpoint_ClearStatusStage();
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
} }
break; break;
...@@ -181,7 +179,7 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -181,7 +179,7 @@ void EVENT_USB_UnhandledControlPacket(void)
void USB_Audio_Task(void) void USB_Audio_Task(void)
{ {
/* Device must be connected and configured for the task to run */ /* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber)) if (USB_DeviceState != DEVICE_STATE_Configured)
return; return;
/* Check to see if the streaming interface is selected, if not the host is not receiving audio */ /* Check to see if the streaming interface is selected, if not the host is not receiving audio */
......
...@@ -56,12 +56,15 @@ CDC_Line_Coding_t LineCoding = { .BaudRateBPS = 9600, ...@@ -56,12 +56,15 @@ CDC_Line_Coding_t LineCoding = { .BaudRateBPS = 9600,
*/ */
static int CDC_putchar (char c, FILE *stream) static int CDC_putchar (char c, FILE *stream)
{ {
if (!(USB_IsConnected))
return -1;
Endpoint_SelectEndpoint(CDC_TX_EPNUM); Endpoint_SelectEndpoint(CDC_TX_EPNUM);
while (!(Endpoint_IsReadWriteAllowed()));
while (!(Endpoint_IsReadWriteAllowed()))
{
if (USB_DeviceState != DEVICE_STATE_Configured)
return -1;
}
Endpoint_Write_Byte(c); Endpoint_Write_Byte(c);
Endpoint_ClearIN(); Endpoint_ClearIN();
...@@ -76,10 +79,11 @@ static int CDC_getchar (FILE *stream) ...@@ -76,10 +79,11 @@ static int CDC_getchar (FILE *stream)
for (;;) for (;;)
{ {
if (!(USB_IsConnected)) while (!(Endpoint_IsReadWriteAllowed()))
return -1; {
if (USB_DeviceState != DEVICE_STATE_Configured)
while (!(Endpoint_IsReadWriteAllowed())); return -1;
}
if (!(Endpoint_BytesInEndpoint())) if (!(Endpoint_BytesInEndpoint()))
{ {
...@@ -229,9 +233,7 @@ void EVENT_USB_UnhandledControlPacket(void) ...@@ -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: CONTROL_LINE_OUT_* masks to determine the RTS and DTR line states using the following code:
*/ */
/* Acknowledge status stage */ Endpoint_ClearStatusStage();
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
} }
break; break;
...@@ -244,18 +246,17 @@ void CDC_Task(void) ...@@ -244,18 +246,17 @@ void CDC_Task(void)
char* ReportString = NULL; char* ReportString = NULL;
uint8_t JoyStatus_LCL = Joystick_GetStatus(); uint8_t JoyStatus_LCL = Joystick_GetStatus();
static bool ActionSent = false; static bool ActionSent = false;
char* JoystickStrings[] =
char* JoystickStrings[] = {
{ "Joystick Up\r\n",
"Joystick Up\r\n", "Joystick Down\r\n",
"Joystick Down\r\n", "Joystick Left\r\n",
"Joystick Left\r\n", "Joystick Right\r\n",
"Joystick Right\r\n", "Joystick Pressed\r\n",
"Joystick Pressed\r\n", };
};