From e071f3897a0946c6be1e1b5e1f78eda8dcbf6fc7 Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Tue, 21 Jul 2009 13:31:21 +0000 Subject: 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. --- Bootloaders/CDC/BootloaderCDC.c | 47 +++++++++++++++-------- Bootloaders/DFU/BootloaderDFU.c | 79 +++++++++++++++++++++++---------------- Bootloaders/TeensyHID/TeensyHID.c | 6 +-- 3 files changed, 81 insertions(+), 51 deletions(-) (limited to 'Bootloaders') diff --git a/Bootloaders/CDC/BootloaderCDC.c b/Bootloaders/CDC/BootloaderCDC.c index d875b842d..cbcfd7e6c 100644 --- a/Bootloaders/CDC/BootloaderCDC.c +++ b/Bootloaders/CDC/BootloaderCDC.c @@ -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); diff --git a/Bootloaders/DFU/BootloaderDFU.c b/Bootloaders/DFU/BootloaderDFU.c index 2086a91d2..f9c8195f3 100644 --- a/Bootloaders/DFU/BootloaderDFU.c +++ b/Bootloaders/DFU/BootloaderDFU.c @@ -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 { diff --git a/Bootloaders/TeensyHID/TeensyHID.c b/Bootloaders/TeensyHID/TeensyHID.c index 12d5c0de8..eed709783 100644 --- a/Bootloaders/TeensyHID/TeensyHID.c +++ b/Bootloaders/TeensyHID/TeensyHID.c @@ -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; -- cgit v1.2.3