From a9d5e129b76449c73a853af450d7d353512cd3a0 Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Wed, 5 Aug 2009 09:37:45 +0000 Subject: [PATCH] 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. --- .../MassStorage/Lib/DataflashManager.c | 28 ++++++------------- .../ClassDriver/MassStorage/MassStorage.h | 2 +- Demos/Device/LowLevel/CDC/CDC.c | 20 ++++--------- Demos/Device/LowLevel/DualCDC/DualCDC.c | 12 ++------ .../MassStorage/Lib/DataflashManager.c | 28 ++++++------------- .../Device/LowLevel/MassStorage/MassStorage.h | 2 +- .../Device/LowLevel/USBtoSerial/USBtoSerial.c | 12 ++------ LUFA/Drivers/USB/Class/Device/CDC.c | 14 ++-------- LUFA/Drivers/USB/HighLevel/USBInterrupt.c | 4 +-- 9 files changed, 31 insertions(+), 91 deletions(-) diff --git a/Demos/Device/ClassDriver/MassStorage/Lib/DataflashManager.c b/Demos/Device/ClassDriver/MassStorage/Lib/DataflashManager.c index 5f12d02b43..3858d38732 100644 --- a/Demos/Device/ClassDriver/MassStorage/Lib/DataflashManager.c +++ b/Demos/Device/ClassDriver/MassStorage/Lib/DataflashManager.c @@ -69,11 +69,8 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, co Dataflash_SendAddressBytes(0, CurrDFPageByte); /* Wait until endpoint is ready before continuing */ - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + if (Endpoint_WaitUntilReady()) + return; while (TotalBlocks) { @@ -89,11 +86,8 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, co Endpoint_ClearOUT(); /* Wait until the host has sent another packet */ - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + if (Endpoint_WaitUntilReady()) + return; } /* Check if end of dataflash page reached */ @@ -205,11 +199,8 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, con Dataflash_SendByte(0x00); /* Wait until endpoint is ready before continuing */ - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + if (Endpoint_WaitUntilReady()) + return; while (TotalBlocks) { @@ -225,11 +216,8 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, con Endpoint_ClearIN(); /* Wait until the endpoint is ready for more data */ - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + if (Endpoint_WaitUntilReady()) + return; } /* Check if end of dataflash page reached */ diff --git a/Demos/Device/ClassDriver/MassStorage/MassStorage.h b/Demos/Device/ClassDriver/MassStorage/MassStorage.h index e6b2bf99f4..930e8f4cb0 100644 --- a/Demos/Device/ClassDriver/MassStorage/MassStorage.h +++ b/Demos/Device/ClassDriver/MassStorage/MassStorage.h @@ -70,7 +70,7 @@ #define LEDMASK_USB_BUSY (LEDS_LED2) /** 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. */ #define LUN_MEDIA_BLOCKS (VIRTUAL_MEMORY_BLOCKS / TOTAL_LUNS) diff --git a/Demos/Device/LowLevel/CDC/CDC.c b/Demos/Device/LowLevel/CDC/CDC.c index 9ee744ea04..12cd8c40f9 100644 --- a/Demos/Device/LowLevel/CDC/CDC.c +++ b/Demos/Device/LowLevel/CDC/CDC.c @@ -62,11 +62,8 @@ static int CDC_putchar(char c, FILE *stream) if (!(LineEncoding.BaudRateBPS)) return -1; - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState != DEVICE_STATE_Configured) - return -1; - } + if (Endpoint_WaitUntilReady()) + return -1; Endpoint_Write_Byte(c); Endpoint_ClearIN(); @@ -85,11 +82,8 @@ static int CDC_getchar(FILE *stream) for (;;) { - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState != DEVICE_STATE_Configured) - return -1; - } + if (Endpoint_WaitUntilReady()) + return -1; if (!(Endpoint_BytesInEndpoint())) { @@ -327,11 +321,7 @@ void CDC_Task(void) if (IsFull) { /* Wait until the endpoint is ready for another packet */ - while (!(Endpoint_IsINReady())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + Endpoint_WaitUntilReady(); /* Send an empty packet to ensure that the host does not buffer data sent to it */ Endpoint_ClearIN(); diff --git a/Demos/Device/LowLevel/DualCDC/DualCDC.c b/Demos/Device/LowLevel/DualCDC/DualCDC.c index c49a1ad85b..7a84c8dcda 100644 --- a/Demos/Device/LowLevel/DualCDC/DualCDC.c +++ b/Demos/Device/LowLevel/DualCDC/DualCDC.c @@ -274,11 +274,7 @@ void CDC1_Task(void) Endpoint_ClearIN(); /* Wait until the endpoint is ready for another packet */ - while (!(Endpoint_IsINReady())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + Endpoint_WaitUntilReady(); /* Send an empty packet to ensure that the host does not buffer data sent to it */ Endpoint_ClearIN(); @@ -329,11 +325,7 @@ void CDC2_Task(void) Endpoint_ClearIN(); /* Wait until the endpoint is ready for the next packet */ - while (!(Endpoint_IsINReady())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + Endpoint_WaitUntilReady(); /* Send an empty packet to prevent host buffering */ Endpoint_ClearIN(); diff --git a/Demos/Device/LowLevel/MassStorage/Lib/DataflashManager.c b/Demos/Device/LowLevel/MassStorage/Lib/DataflashManager.c index 2bd03c98d9..9cd0279d62 100644 --- a/Demos/Device/LowLevel/MassStorage/Lib/DataflashManager.c +++ b/Demos/Device/LowLevel/MassStorage/Lib/DataflashManager.c @@ -68,11 +68,8 @@ void DataflashManager_WriteBlocks(const uint32_t BlockAddress, uint16_t TotalBlo Dataflash_SendAddressBytes(0, CurrDFPageByte); /* Wait until endpoint is ready before continuing */ - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + if (Endpoint_WaitUntilReady()) + return; while (TotalBlocks) { @@ -88,11 +85,8 @@ void DataflashManager_WriteBlocks(const uint32_t BlockAddress, uint16_t TotalBlo Endpoint_ClearOUT(); /* Wait until the host has sent another packet */ - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + if (Endpoint_WaitUntilReady()) + return; } /* Check if end of dataflash page reached */ @@ -203,11 +197,8 @@ void DataflashManager_ReadBlocks(const uint32_t BlockAddress, uint16_t TotalBloc Dataflash_SendByte(0x00); /* Wait until endpoint is ready before continuing */ - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + if (Endpoint_WaitUntilReady()) + return; while (TotalBlocks) { @@ -223,11 +214,8 @@ void DataflashManager_ReadBlocks(const uint32_t BlockAddress, uint16_t TotalBloc Endpoint_ClearIN(); /* Wait until the endpoint is ready for more data */ - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + if (Endpoint_WaitUntilReady()) + return; } /* Check if end of dataflash page reached */ diff --git a/Demos/Device/LowLevel/MassStorage/MassStorage.h b/Demos/Device/LowLevel/MassStorage/MassStorage.h index 868daf09a6..65407c8c14 100644 --- a/Demos/Device/LowLevel/MassStorage/MassStorage.h +++ b/Demos/Device/LowLevel/MassStorage/MassStorage.h @@ -64,7 +64,7 @@ /** 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. */ - #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. */ #define LUN_MEDIA_BLOCKS (VIRTUAL_MEMORY_BLOCKS / TOTAL_LUNS) diff --git a/Demos/Device/LowLevel/USBtoSerial/USBtoSerial.c b/Demos/Device/LowLevel/USBtoSerial/USBtoSerial.c index bb1b4da2d3..736c0af10b 100644 --- a/Demos/Device/LowLevel/USBtoSerial/USBtoSerial.c +++ b/Demos/Device/LowLevel/USBtoSerial/USBtoSerial.c @@ -263,11 +263,7 @@ void CDC_Task(void) if ((Tx_Buffer.Elements) && LineEncoding.BaudRateBPS) { /* Wait until Serial Tx Endpoint Ready for Read/Write */ - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + Endpoint_WaitUntilReady(); /* Write the bytes from the buffer to the endpoint while space is available */ while (Tx_Buffer.Elements && Endpoint_IsReadWriteAllowed()) @@ -287,11 +283,7 @@ void CDC_Task(void) if (IsFull && !(Tx_Buffer.Elements)) { /* Wait until Serial Tx Endpoint Ready for Read/Write */ - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + Endpoint_WaitUntilReady(); /* Send an empty packet to terminate the transfer */ Endpoint_ClearIN(); diff --git a/LUFA/Drivers/USB/Class/Device/CDC.c b/LUFA/Drivers/USB/Class/Device/CDC.c index 9da0ed1ebf..0d6b378503 100644 --- a/LUFA/Drivers/USB/Class/Device/CDC.c +++ b/LUFA/Drivers/USB/Class/Device/CDC.c @@ -126,12 +126,7 @@ void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo) if (!(Endpoint_IsReadWriteAllowed())) { Endpoint_ClearIN(); - - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + Endpoint_WaitUntilReady(); } Endpoint_ClearIN(); @@ -156,12 +151,7 @@ void CDC_Device_SendByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, con if (!(Endpoint_IsReadWriteAllowed())) { Endpoint_ClearIN(); - - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + Endpoint_WaitUntilReady(); } Endpoint_Write_Byte(Data); diff --git a/LUFA/Drivers/USB/HighLevel/USBInterrupt.c b/LUFA/Drivers/USB/HighLevel/USBInterrupt.c index a039dffb62..00fe7d1dcb 100644 --- a/LUFA/Drivers/USB/HighLevel/USBInterrupt.c +++ b/LUFA/Drivers/USB/HighLevel/USBInterrupt.c @@ -223,7 +223,7 @@ ISR(USB_GEN_vect, ISR_BLOCK) } #if defined(INTERRUPT_CONTROL_ENDPOINT) -ISR(USB_COM_vect, ISR_NOBLOCK) +ISR(USB_COM_vect, ISR_BLOCK) { uint8_t PrevSelectedEndpoint = Endpoint_GetCurrentEndpoint(); @@ -231,6 +231,6 @@ ISR(USB_COM_vect, ISR_NOBLOCK) USB_INT_Clear(USB_INT_ENDPOINT_SETUP); - Endpoint_SelectEndpoint(PrevSelectedEndpoint); + Endpoint_SelectEndpoint(PrevSelectedEndpoint); } #endif