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
commit e071f3897a
58 changed files with 663 additions and 460 deletions

View file

@ -165,9 +165,7 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearIN();
/* Acknowledge status stage */
while (!(Endpoint_IsOUTReceived()));
Endpoint_ClearOUT();
Endpoint_ClearStatusStage();
}
break;
@ -176,16 +174,18 @@ void EVENT_USB_UnhandledControlPacket(void)
{
Endpoint_ClearSETUP();
while (!(Endpoint_IsOUTReceived()));
while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
for (uint8_t i = 0; i < sizeof(LineCoding); i++)
*(LineCodingData++) = Endpoint_Read_Byte();
Endpoint_ClearOUT();
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
@ -194,9 +194,7 @@ void EVENT_USB_UnhandledControlPacket(void)
{
Endpoint_ClearSETUP();
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
@ -333,7 +331,12 @@ static uint8_t FetchNextCommandByte(void)
while (!(Endpoint_IsReadWriteAllowed()))
{
Endpoint_ClearOUT();
while (!(Endpoint_IsOUTReceived()));
while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return 0;
}
}
/* Fetch the next byte from the OUT endpoint */
@ -354,7 +357,12 @@ static void WriteNextResponseByte(const uint8_t Response)
if (!(Endpoint_IsReadWriteAllowed()))
{
Endpoint_ClearIN();
while (!(Endpoint_IsINReady()));
while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
}
/* Write the next byte to the OUT endpoint */
@ -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 (IsEndpointFull)
{
while (!(Endpoint_IsINReady()));
while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
Endpoint_ClearIN();
}
/* 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 */
Endpoint_SelectEndpoint(CDC_RX_EPNUM);

View file

@ -177,7 +177,11 @@ void EVENT_USB_UnhandledControlPacket(void)
/* If the request has a data stage, load it into the command struct */
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 */
SentCommand.Command = Endpoint_Read_Byte();
@ -235,7 +239,12 @@ void EVENT_USB_UnhandledControlPacket(void)
if (!(Endpoint_BytesInEndpoint()))
{
Endpoint_ClearOUT();
while (!(Endpoint_IsOUTReceived()));
while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
}
/* Write the next word into the current flash page */
@ -279,7 +288,12 @@ void EVENT_USB_UnhandledControlPacket(void)
if (!(Endpoint_BytesInEndpoint()))
{
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 */
@ -297,16 +311,18 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearOUT();
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
break;
case DFU_UPLOAD:
Endpoint_ClearSETUP();
while (!(Endpoint_IsINReady()));
while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
if (DFU_State != dfuUPLOAD_IDLE)
{
if ((DFU_State == dfuERROR) && IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01)) // Blank Check
@ -343,7 +359,12 @@ void EVENT_USB_UnhandledControlPacket(void)
if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE)
{
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 */
@ -368,7 +389,12 @@ void EVENT_USB_UnhandledControlPacket(void)
if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE)
{
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 */
@ -385,10 +411,7 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearIN();
/* Acknowledge status stage */
while (!(Endpoint_IsOUTReceived()));
Endpoint_ClearOUT();
Endpoint_ClearStatusStage();
break;
case DFU_GETSTATUS:
Endpoint_ClearSETUP();
@ -408,10 +431,7 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearIN();
/* Acknowledge status stage */
while (!(Endpoint_IsOUTReceived()));
Endpoint_ClearOUT();
Endpoint_ClearStatusStage();
break;
case DFU_CLRSTATUS:
Endpoint_ClearSETUP();
@ -419,10 +439,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Reset the status value variable to the default OK status */
DFU_Status = OK;
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
break;
case DFU_GETSTATE:
Endpoint_ClearSETUP();
@ -432,21 +449,15 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearIN();
/* Acknowledge status stage */
while (!(Endpoint_IsOUTReceived()));
Endpoint_ClearOUT();
Endpoint_ClearStatusStage();
break;
case DFU_ABORT:
Endpoint_ClearSETUP();
/* Reset the current state variable to the default idle state */
DFU_State = dfuIDLE;
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
break;
}
}
@ -465,7 +476,11 @@ static void DiscardFillerBytes(uint8_t NumberOfBytes)
Endpoint_ClearOUT();
/* Wait until next data packet received */
while (!(Endpoint_IsOUTReceived()));
while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
}
else
{

View file

@ -119,7 +119,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Wait until the command (report) has been sent by the host */
while (!(Endpoint_IsOUTReceived()));
/* Read in the write destination address */
uint16_t PageAddress = Endpoint_Read_Word_LE();
@ -158,9 +158,7 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearOUT();
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;

View file

@ -69,7 +69,11 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, co
Dataflash_SendAddressBytes(0, CurrDFPageByte);
/* Wait until endpoint is ready before continuing */
while (!(Endpoint_IsReadWriteAllowed()));
while (!(Endpoint_IsReadWriteAllowed()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
while (TotalBlocks)
{
@ -85,7 +89,11 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, co
Endpoint_ClearOUT();
/* 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 */
@ -197,7 +205,11 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, con
Dataflash_SendByte(0x00);
/* Wait until endpoint is ready before continuing */
while (!(Endpoint_IsReadWriteAllowed()));
while (!(Endpoint_IsReadWriteAllowed()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
while (TotalBlocks)
{
@ -213,7 +225,11 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, con
Endpoint_ClearIN();
/* 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 */

View file

@ -152,7 +152,7 @@ void EVENT_USB_UnhandledControlPacket(void)
*/
ISR(USART1_RX_vect, ISR_BLOCK)
{
if (USB_IsConnected)
if (USB_DeviceState == DEVICE_STATE_Configured)
Buffer_StoreElement(&Tx_Buffer, UDR1);
}

View file

@ -142,7 +142,7 @@ void EVENT_USB_UnhandledControlPacket(void)
void SideShow_Task(void)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
/* Select the SideShow data out endpoint */

View file

@ -139,9 +139,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */
StreamingAudioInterfaceSelected = ((USB_ControlRequest.wValue) != 0);
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
@ -152,7 +150,7 @@ void EVENT_USB_UnhandledControlPacket(void)
void USB_Audio_Task(void)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
/* Check to see if the streaming interface is selected, if not the host is not receiving audio */

View file

@ -166,9 +166,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Check if the host is enabling the audio interface (setting AlternateSetting to 1) */
StreamingAudioInterfaceSelected = ((USB_ControlRequest.wValue) != 0);
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
@ -181,7 +179,7 @@ void EVENT_USB_UnhandledControlPacket(void)
void USB_Audio_Task(void)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
/* Check to see if the streaming interface is selected, if not the host is not receiving audio */

View file

@ -56,12 +56,15 @@ CDC_Line_Coding_t LineCoding = { .BaudRateBPS = 9600,
*/
static int CDC_putchar (char c, FILE *stream)
{
if (!(USB_IsConnected))
return -1;
{
Endpoint_SelectEndpoint(CDC_TX_EPNUM);
while (!(Endpoint_IsReadWriteAllowed()));
while (!(Endpoint_IsReadWriteAllowed()))
{
if (USB_DeviceState != DEVICE_STATE_Configured)
return -1;
}
Endpoint_Write_Byte(c);
Endpoint_ClearIN();
@ -76,10 +79,11 @@ static int CDC_getchar (FILE *stream)
for (;;)
{
if (!(USB_IsConnected))
return -1;
while (!(Endpoint_IsReadWriteAllowed()));
while (!(Endpoint_IsReadWriteAllowed()))
{
if (USB_DeviceState != DEVICE_STATE_Configured)
return -1;
}
if (!(Endpoint_BytesInEndpoint()))
{
@ -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:
*/
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
@ -244,18 +246,17 @@ void CDC_Task(void)
char* ReportString = NULL;
uint8_t JoyStatus_LCL = Joystick_GetStatus();
static bool ActionSent = false;
char* JoystickStrings[] =
{
"Joystick Up\r\n",
"Joystick Down\r\n",
"Joystick Left\r\n",
"Joystick Right\r\n",
"Joystick Pressed\r\n",
};
char* JoystickStrings[] =
{
"Joystick Up\r\n",
"Joystick Down\r\n",
"Joystick Left\r\n",
"Joystick Right\r\n",
"Joystick Pressed\r\n",
};
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
#if 0
@ -319,7 +320,11 @@ void CDC_Task(void)
if (IsFull)
{
/* Wait until the endpoint is ready for another packet */
while (!(Endpoint_IsINReady()));
while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
/* Send an empty packet to ensure that the host does not buffer data sent to it */
Endpoint_ClearIN();

View file

@ -211,9 +211,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Acknowledge the SETUP packet, ready for data transfer */
Endpoint_ClearSETUP();
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
@ -228,20 +226,19 @@ void CDC1_Task(void)
char* ReportString = NULL;
uint8_t JoyStatus_LCL = Joystick_GetStatus();
static bool ActionSent = false;
char* JoystickStrings[] =
{
"Joystick Up\r\n",
"Joystick Down\r\n",
"Joystick Left\r\n",
"Joystick Right\r\n",
"Joystick Pressed\r\n",
};
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
char* JoystickStrings[] =
{
"Joystick Up\r\n",
"Joystick Down\r\n",
"Joystick Left\r\n",
"Joystick Right\r\n",
"Joystick Pressed\r\n",
};
/* Determine if a joystick action has occurred */
if (JoyStatus_LCL & JOY_UP)
ReportString = JoystickStrings[0];
@ -273,7 +270,11 @@ void CDC1_Task(void)
Endpoint_ClearIN();
/* Wait until the endpoint is ready for another packet */
while (!(Endpoint_IsINReady()));
while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
/* Send an empty packet to ensure that the host does not buffer data sent to it */
Endpoint_ClearIN();
@ -293,7 +294,7 @@ void CDC1_Task(void)
void CDC2_Task(void)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
/* Select the Serial Rx Endpoint */
@ -324,7 +325,11 @@ void CDC2_Task(void)
Endpoint_ClearIN();
/* Wait until the endpoint is ready for the next packet */
while (!(Endpoint_IsINReady()));
while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
/* Send an empty packet to prevent host buffering */
Endpoint_ClearIN();

View file

@ -148,7 +148,11 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearSETUP();
/* Wait until the generic report has been sent by the host */
while (!(Endpoint_IsOUTReceived()));
while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
Endpoint_Read_Control_Stream_LE(&GenericData, sizeof(GenericData));
@ -158,7 +162,11 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearOUT();
/* Wait until the host is ready to receive the request confirmation */
while (!(Endpoint_IsINReady()));
while (!(Endpoint_IsINReady()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
/* Handshake the request by sending an empty IN packet */
Endpoint_ClearIN();
@ -203,7 +211,7 @@ void CreateGenericHIDReport(uint8_t* DataArray)
void HID_Task(void)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);

View file

@ -182,7 +182,7 @@ bool GetNextReport(USB_JoystickReport_Data_t* ReportData)
void HID_Task(void)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
/* Select the Joystick Report Endpoint */

View file

@ -172,7 +172,11 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearSETUP();
/* Wait until the LED report has been sent by the host */
while (!(Endpoint_IsOUTReceived()));
while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
/* Read in the LED report from the host */
uint8_t LEDStatus = Endpoint_Read_Byte();
@ -183,9 +187,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Clear the endpoint data */
Endpoint_ClearOUT();
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
@ -200,9 +202,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Send the flag to the host */
Endpoint_ClearIN();
/* Acknowledge status stage */
while (!(Endpoint_IsOUTReceived()));
Endpoint_ClearOUT();
Endpoint_ClearStatusStage();
}
break;
@ -214,9 +214,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Set or clear the flag depending on what the host indicates that the current Protocol should be */
UsingReportProtocol = (USB_ControlRequest.wValue != 0);
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
@ -228,9 +226,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Get idle period in MSB, IdleCount must be multiplied by 4 to get number of milliseconds */
IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
@ -245,9 +241,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Send the flag to the host */
Endpoint_ClearIN();
/* Acknowledge status stage */
while (!(Endpoint_IsOUTReceived()));
Endpoint_ClearOUT();
Endpoint_ClearStatusStage();
}
break;
@ -378,7 +372,7 @@ void ReceiveNextReport(void)
void HID_Task(void)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
/* Send the next keypress report to the host */

View file

@ -174,7 +174,11 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearSETUP();
/* Wait until the LED report has been sent by the host */
while (!(Endpoint_IsOUTReceived()));
while (!(Endpoint_IsOUTReceived()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
/* Read in the LED report from the host */
uint8_t LEDStatus = Endpoint_Read_Byte();
@ -195,9 +199,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Clear the endpoint data */
Endpoint_ClearOUT();
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
@ -213,7 +215,7 @@ void Keyboard_HID_Task(void)
uint8_t JoyStatus_LCL = Joystick_GetStatus();
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
/* Check if board button is not pressed, if so mouse mode enabled */
@ -284,7 +286,7 @@ void Mouse_HID_Task(void)
uint8_t JoyStatus_LCL = Joystick_GetStatus();
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
/* Check if board button is pressed, if so mouse mode enabled */

View file

@ -117,7 +117,7 @@ void MIDI_Task(void)
static uint8_t PrevJoystickStatus;
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPNUM);

View file

@ -68,8 +68,12 @@ void DataflashManager_WriteBlocks(const uint32_t BlockAddress, uint16_t TotalBlo
Dataflash_SendAddressBytes(0, CurrDFPageByte);
/* Wait until endpoint is ready before continuing */
while (!(Endpoint_IsReadWriteAllowed()));
while (!(Endpoint_IsReadWriteAllowed()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
while (TotalBlocks)
{
uint8_t BytesInBlockDiv16 = 0;
@ -84,7 +88,11 @@ void DataflashManager_WriteBlocks(const uint32_t BlockAddress, uint16_t TotalBlo
Endpoint_ClearOUT();
/* 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 */
@ -195,7 +203,11 @@ void DataflashManager_ReadBlocks(const uint32_t BlockAddress, uint16_t TotalBloc
Dataflash_SendByte(0x00);
/* Wait until endpoint is ready before continuing */
while (!(Endpoint_IsReadWriteAllowed()));
while (!(Endpoint_IsReadWriteAllowed()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
while (TotalBlocks)
{
@ -211,7 +223,11 @@ void DataflashManager_ReadBlocks(const uint32_t BlockAddress, uint16_t TotalBloc
Endpoint_ClearIN();
/* 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 */

View file

@ -142,9 +142,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Indicate that the current transfer should be aborted */
IsMassStoreReset = true;
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
@ -158,9 +156,7 @@ void EVENT_USB_UnhandledControlPacket(void)
Endpoint_ClearIN();
/* Acknowledge status stage */
while (!(Endpoint_IsOUTReceived()));
Endpoint_ClearOUT();
Endpoint_ClearStatusStage();
}
break;
@ -172,67 +168,67 @@ void EVENT_USB_UnhandledControlPacket(void)
*/
void MassStorage_Task(void)
{
/* Check if the USB System is connected to a Host */
if (USB_IsConnected)
/* Device must be connected and configured for the task to run */
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
/* Select the Data Out Endpoint */
Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM);
/* Check to see if a command from the host has been issued */
if (Endpoint_IsReadWriteAllowed())
{
/* Select the Data Out Endpoint */
Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM);
/* Check to see if a command from the host has been issued */
if (Endpoint_IsReadWriteAllowed())
/* Indicate busy */
LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
/* Process sent command block from the host */
if (ReadInCommandBlock())
{
/* Indicate busy */
LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
/* Check direction of command, select Data IN endpoint if data is from the device */
if (CommandBlock.Flags & COMMAND_DIRECTION_DATA_IN)
Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM);
/* Process sent command block from the host */
if (ReadInCommandBlock())
/* Decode the received SCSI command */
SCSI_DecodeSCSICommand();
/* Load in the CBW tag into the CSW to link them together */
CommandStatus.Tag = CommandBlock.Tag;
/* Load in the data residue counter into the CSW */
CommandStatus.DataTransferResidue = CommandBlock.DataTransferLength;
/* Stall the selected data pipe if command failed (if data is still to be transferred) */
if ((CommandStatus.Status == Command_Fail) && (CommandStatus.DataTransferResidue))
Endpoint_StallTransaction();
/* Return command status block to the host */
ReturnCommandStatus();
/* Check if a Mass Storage Reset occurred */
if (IsMassStoreReset)
{
/* Check direction of command, select Data IN endpoint if data is from the device */
if (CommandBlock.Flags & COMMAND_DIRECTION_DATA_IN)
Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM);
/* Decode the received SCSI command */
SCSI_DecodeSCSICommand();
/* Load in the CBW tag into the CSW to link them together */
CommandStatus.Tag = CommandBlock.Tag;
/* Load in the data residue counter into the CSW */
CommandStatus.DataTransferResidue = CommandBlock.DataTransferLength;
/* Stall the selected data pipe if command failed (if data is still to be transferred) */
if ((CommandStatus.Status == Command_Fail) && (CommandStatus.DataTransferResidue))
Endpoint_StallTransaction();
/* Return command status block to the host */
ReturnCommandStatus();
/* Reset the data endpoint banks */
Endpoint_ResetFIFO(MASS_STORAGE_OUT_EPNUM);
Endpoint_ResetFIFO(MASS_STORAGE_IN_EPNUM);
/* Check if a Mass Storage Reset occurred */
if (IsMassStoreReset)
{
/* Reset the data endpoint banks */
Endpoint_ResetFIFO(MASS_STORAGE_OUT_EPNUM);
Endpoint_ResetFIFO(MASS_STORAGE_IN_EPNUM);
Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM);
Endpoint_ClearStall();
Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM);
Endpoint_ClearStall();
}
Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM);
Endpoint_ClearStall();
Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM);
Endpoint_ClearStall();
}
/* Indicate ready */
LEDs_SetAllLEDs(LEDMASK_USB_READY);
}
else
{
/* Indicate error reading in the command block from the host */
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
}
/* Indicate ready */
LEDs_SetAllLEDs(LEDMASK_USB_READY);
}
else
{
/* Indicate error reading in the command block from the host */
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
}
/* Clear the abort transfer flag */
IsMassStoreReset = false;
}
/* Clear the abort transfer flag */
IsMassStoreReset = false;
}
/** Function to read in a command block from the host, via the bulk data OUT endpoint. This function reads in the next command block

View file

@ -172,9 +172,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Send the flag to the host */
Endpoint_ClearIN();
/* Acknowledge status stage */
while (!(Endpoint_IsOUTReceived()));
Endpoint_ClearOUT();
Endpoint_ClearStatusStage();
}
break;
@ -186,9 +184,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Set or clear the flag depending on what the host indicates that the current Protocol should be */
UsingReportProtocol = (USB_ControlRequest.wValue != 0);
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
@ -200,9 +196,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Get idle period in MSB, must multiply by 4 to get the duration in milliseconds */
IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
@ -217,9 +211,7 @@ void EVENT_USB_UnhandledControlPacket(void)
/* Send the flag to the host */
Endpoint_ClearIN();
/* Acknowledge status stage */
while (!(Endpoint_IsOUTReceived()));
Endpoint_ClearOUT();
Endpoint_ClearStatusStage();
}
break;
@ -314,7 +306,7 @@ void SendNextReport(void)
void Mouse_Task(void)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
/* Send the next mouse report to the host */

View file

@ -285,7 +285,7 @@ void Ethernet_Task(void)
Ethernet frame at a time, so the FrameInBuffer bool is used to indicate when the buffers contain data. */
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
/* Check if a frame has been written to the IN frame buffer */

View file

@ -192,9 +192,7 @@ void EVENT_USB_UnhandledControlPacket(void)
CONTROL_LINE_OUT_* masks to determine the RTS and DTR line states using the following code:
*/
/* Acknowledge status stage */
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
Endpoint_ClearStatusStage();
}
break;
@ -205,7 +203,7 @@ void EVENT_USB_UnhandledControlPacket(void)
void CDC_Task(void)
{
/* Device must be connected and configured for the task to run */
if (!(USB_IsConnected) || !(USB_ConfigurationNumber))
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
#if 0
@ -264,7 +262,11 @@ void CDC_Task(void)
if (Tx_Buffer.Elements)
{
/* Wait until Serial Tx Endpoint Ready for Read/Write */
while (!(Endpoint_IsReadWriteAllowed()));
while (!(Endpoint_IsReadWriteAllowed()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
/* Write the bytes from the buffer to the endpoint while space is available */
while (Tx_Buffer.Elements && Endpoint_IsReadWriteAllowed())
@ -284,8 +286,12 @@ void CDC_Task(void)
if (IsFull && !(Tx_Buffer.Elements))
{
/* Wait until Serial Tx Endpoint Ready for Read/Write */
while (!(Endpoint_IsReadWriteAllowed()));
while (!(Endpoint_IsReadWriteAllowed()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
/* Send an empty packet to terminate the transfer */
Endpoint_ClearIN();
}
@ -298,11 +304,8 @@ void CDC_Task(void)
ISR(USART1_RX_vect, ISR_BLOCK)
{
/* Only store received characters if the USB interface is connected */
if (USB_IsConnected)
{
/* Character received, store it into the buffer */
Buffer_StoreElement(&Tx_Buffer, UDR1);
}
if (USB_DeviceState != DEVICE_STATE_Configured)
Buffer_StoreElement(&Tx_Buffer, UDR1);
}
/** Reconfigures the USART to match the current serial port settings issued by the host as closely as possible. */

Some files were not shown because too many files have changed in this diff Show more