Changed over manual loops waiting for endpoints to be ready to use the library Endpoint_WaitUntilReady() function for robustness. Fixes issues with terminated transfers on the host locking up USB devices.

pull/1469/head
Dean Camera 16 years ago
parent 4b35dd1670
commit a9d5e129b7

@ -69,11 +69,8 @@ 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())) if (Endpoint_WaitUntilReady())
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return; return;
}
while (TotalBlocks) while (TotalBlocks)
{ {
@ -89,12 +86,9 @@ 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())) if (Endpoint_WaitUntilReady())
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return; return;
} }
}
/* Check if end of dataflash page reached */ /* Check if end of dataflash page reached */
if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4)) if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
@ -205,11 +199,8 @@ 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())) if (Endpoint_WaitUntilReady())
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return; return;
}
while (TotalBlocks) while (TotalBlocks)
{ {
@ -225,12 +216,9 @@ 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())) if (Endpoint_WaitUntilReady())
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return; return;
} }
}
/* Check if end of dataflash page reached */ /* Check if end of dataflash page reached */
if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4)) if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))

@ -70,7 +70,7 @@
#define LEDMASK_USB_BUSY (LEDS_LED2) #define LEDMASK_USB_BUSY (LEDS_LED2)
/** Total number of logical drives within the device - must be non-zero. */ /** Total number of logical drives within the device - must be non-zero. */
#define TOTAL_LUNS 2 #define TOTAL_LUNS 1
/** Blocks in each LUN, calculated from the total capacity divided by the total number of Logical Units in the device. */ /** Blocks in each LUN, calculated from the total capacity divided by the total number of Logical Units in the device. */
#define LUN_MEDIA_BLOCKS (VIRTUAL_MEMORY_BLOCKS / TOTAL_LUNS) #define LUN_MEDIA_BLOCKS (VIRTUAL_MEMORY_BLOCKS / TOTAL_LUNS)

@ -62,11 +62,8 @@ static int CDC_putchar(char c, FILE *stream)
if (!(LineEncoding.BaudRateBPS)) if (!(LineEncoding.BaudRateBPS))
return -1; return -1;
while (!(Endpoint_IsReadWriteAllowed())) if (Endpoint_WaitUntilReady())
{
if (USB_DeviceState != DEVICE_STATE_Configured)
return -1; return -1;
}
Endpoint_Write_Byte(c); Endpoint_Write_Byte(c);
Endpoint_ClearIN(); Endpoint_ClearIN();
@ -85,11 +82,8 @@ static int CDC_getchar(FILE *stream)
for (;;) for (;;)
{ {
while (!(Endpoint_IsReadWriteAllowed())) if (Endpoint_WaitUntilReady())
{
if (USB_DeviceState != DEVICE_STATE_Configured)
return -1; return -1;
}
if (!(Endpoint_BytesInEndpoint())) if (!(Endpoint_BytesInEndpoint()))
{ {
@ -327,11 +321,7 @@ void CDC_Task(void)
if (IsFull) if (IsFull)
{ {
/* Wait until the endpoint is ready for another packet */ /* Wait until the endpoint is ready for another packet */
while (!(Endpoint_IsINReady())) Endpoint_WaitUntilReady();
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
/* Send an empty packet to ensure that the host does not buffer data sent to it */ /* Send an empty packet to ensure that the host does not buffer data sent to it */
Endpoint_ClearIN(); Endpoint_ClearIN();

@ -274,11 +274,7 @@ void CDC1_Task(void)
Endpoint_ClearIN(); Endpoint_ClearIN();
/* Wait until the endpoint is ready for another packet */ /* Wait until the endpoint is ready for another packet */
while (!(Endpoint_IsINReady())) Endpoint_WaitUntilReady();
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
/* Send an empty packet to ensure that the host does not buffer data sent to it */ /* Send an empty packet to ensure that the host does not buffer data sent to it */
Endpoint_ClearIN(); Endpoint_ClearIN();
@ -329,11 +325,7 @@ void CDC2_Task(void)
Endpoint_ClearIN(); Endpoint_ClearIN();
/* Wait until the endpoint is ready for the next packet */ /* Wait until the endpoint is ready for the next packet */
while (!(Endpoint_IsINReady())) Endpoint_WaitUntilReady();
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
/* Send an empty packet to prevent host buffering */ /* Send an empty packet to prevent host buffering */
Endpoint_ClearIN(); Endpoint_ClearIN();

@ -68,11 +68,8 @@ void DataflashManager_WriteBlocks(const uint32_t BlockAddress, uint16_t TotalBlo
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())) if (Endpoint_WaitUntilReady())
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return; return;
}
while (TotalBlocks) while (TotalBlocks)
{ {
@ -88,12 +85,9 @@ void DataflashManager_WriteBlocks(const uint32_t BlockAddress, uint16_t TotalBlo
Endpoint_ClearOUT(); Endpoint_ClearOUT();
/* Wait until the host has sent another packet */ /* Wait until the host has sent another packet */
while (!(Endpoint_IsReadWriteAllowed())) if (Endpoint_WaitUntilReady())
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return; return;
} }
}
/* Check if end of dataflash page reached */ /* Check if end of dataflash page reached */
if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4)) if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
@ -203,11 +197,8 @@ void DataflashManager_ReadBlocks(const uint32_t BlockAddress, uint16_t TotalBloc
Dataflash_SendByte(0x00); Dataflash_SendByte(0x00);
/* Wait until endpoint is ready before continuing */ /* Wait until endpoint is ready before continuing */
while (!(Endpoint_IsReadWriteAllowed())) if (Endpoint_WaitUntilReady())
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return; return;
}
while (TotalBlocks) while (TotalBlocks)
{ {
@ -223,12 +214,9 @@ void DataflashManager_ReadBlocks(const uint32_t BlockAddress, uint16_t TotalBloc
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())) if (Endpoint_WaitUntilReady())
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return; return;
} }
}
/* Check if end of dataflash page reached */ /* Check if end of dataflash page reached */
if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4)) if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))

@ -64,7 +64,7 @@
/** Total number of Logical Units (drives) in the device. The total device capacity is shared equally between /** Total number of Logical Units (drives) in the device. The total device capacity is shared equally between
* each drive - this can be set to any positive non-zero amount. * each drive - this can be set to any positive non-zero amount.
*/ */
#define TOTAL_LUNS 2 #define TOTAL_LUNS 1
/** Blocks in each LUN, calculated from the total capacity divided by the total number of Logical Units in the device. */ /** Blocks in each LUN, calculated from the total capacity divided by the total number of Logical Units in the device. */
#define LUN_MEDIA_BLOCKS (VIRTUAL_MEMORY_BLOCKS / TOTAL_LUNS) #define LUN_MEDIA_BLOCKS (VIRTUAL_MEMORY_BLOCKS / TOTAL_LUNS)

@ -263,11 +263,7 @@ void CDC_Task(void)
if ((Tx_Buffer.Elements) && LineEncoding.BaudRateBPS) if ((Tx_Buffer.Elements) && LineEncoding.BaudRateBPS)
{ {
/* Wait until Serial Tx Endpoint Ready for Read/Write */ /* Wait until Serial Tx Endpoint Ready for Read/Write */
while (!(Endpoint_IsReadWriteAllowed())) Endpoint_WaitUntilReady();
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
/* Write the bytes from the buffer to the endpoint while space is available */ /* Write the bytes from the buffer to the endpoint while space is available */
while (Tx_Buffer.Elements && Endpoint_IsReadWriteAllowed()) while (Tx_Buffer.Elements && Endpoint_IsReadWriteAllowed())
@ -287,11 +283,7 @@ void CDC_Task(void)
if (IsFull && !(Tx_Buffer.Elements)) if (IsFull && !(Tx_Buffer.Elements))
{ {
/* Wait until Serial Tx Endpoint Ready for Read/Write */ /* Wait until Serial Tx Endpoint Ready for Read/Write */
while (!(Endpoint_IsReadWriteAllowed())) Endpoint_WaitUntilReady();
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
/* Send an empty packet to terminate the transfer */ /* Send an empty packet to terminate the transfer */
Endpoint_ClearIN(); Endpoint_ClearIN();

@ -126,12 +126,7 @@ void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)
if (!(Endpoint_IsReadWriteAllowed())) if (!(Endpoint_IsReadWriteAllowed()))
{ {
Endpoint_ClearIN(); Endpoint_ClearIN();
Endpoint_WaitUntilReady();
while (!(Endpoint_IsReadWriteAllowed()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
} }
Endpoint_ClearIN(); Endpoint_ClearIN();
@ -156,12 +151,7 @@ void CDC_Device_SendByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, con
if (!(Endpoint_IsReadWriteAllowed())) if (!(Endpoint_IsReadWriteAllowed()))
{ {
Endpoint_ClearIN(); Endpoint_ClearIN();
Endpoint_WaitUntilReady();
while (!(Endpoint_IsReadWriteAllowed()))
{
if (USB_DeviceState == DEVICE_STATE_Unattached)
return;
}
} }
Endpoint_Write_Byte(Data); Endpoint_Write_Byte(Data);

@ -223,7 +223,7 @@ ISR(USB_GEN_vect, ISR_BLOCK)
} }
#if defined(INTERRUPT_CONTROL_ENDPOINT) #if defined(INTERRUPT_CONTROL_ENDPOINT)
ISR(USB_COM_vect, ISR_NOBLOCK) ISR(USB_COM_vect, ISR_BLOCK)
{ {
uint8_t PrevSelectedEndpoint = Endpoint_GetCurrentEndpoint(); uint8_t PrevSelectedEndpoint = Endpoint_GetCurrentEndpoint();

Loading…
Cancel
Save