diff --git a/Demos/Device/Incomplete/Sideshow/Sideshow.c b/Demos/Device/Incomplete/Sideshow/Sideshow.c index 41e36cc70e2f0b37182e13e6215f8546d9dd9ed9..cc8bd51c383333a0bb2833715db135b240629276 100644 --- a/Demos/Device/Incomplete/Sideshow/Sideshow.c +++ b/Demos/Device/Incomplete/Sideshow/Sideshow.c @@ -141,17 +141,17 @@ void EVENT_USB_UnhandledControlPacket(void) void SideShow_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_IsConnected) || !(USB_ConfigurationNumber)) + return; + + /* Select the SideShow data out endpoint */ + Endpoint_SelectEndpoint(SIDESHOW_OUT_EPNUM); + + /* Check to see if a new SideShow message has been received */ + if (Endpoint_IsReadWriteAllowed()) { - /* Select the SideShow data out endpoint */ - Endpoint_SelectEndpoint(SIDESHOW_OUT_EPNUM); - - /* Check to see if a new SideShow message has been received */ - if (Endpoint_IsReadWriteAllowed()) - { - /* Process the received SideShow message */ - Sideshow_ProcessCommandPacket(); - } + /* Process the received SideShow message */ + Sideshow_ProcessCommandPacket(); } } diff --git a/Demos/Device/LowLevel/AudioInput/AudioInput.c b/Demos/Device/LowLevel/AudioInput/AudioInput.c index 50e7f4df4548eb19a27d705d548d00345ca890e1..5656b05d1008242e19e497209c9b7296edaac41f 100644 --- a/Demos/Device/LowLevel/AudioInput/AudioInput.c +++ b/Demos/Device/LowLevel/AudioInput/AudioInput.c @@ -151,6 +151,10 @@ void EVENT_USB_UnhandledControlPacket(void) /** Task to manage the Audio interface, reading in ADC samples from the microphone, and them to the host. */ void USB_Audio_Task(void) { + /* Device must be connected and configured for the task to run */ + if (!(USB_IsConnected) || !(USB_ConfigurationNumber)) + return; + /* Check to see if the streaming interface is selected, if not the host is not receiving audio */ if (!(StreamingAudioInterfaceSelected)) return; diff --git a/Demos/Device/LowLevel/AudioOutput/AudioOutput.c b/Demos/Device/LowLevel/AudioOutput/AudioOutput.c index 9661f0342dfc36668244b71404de3993dad639ab..6d22009dcc358249c86c1f3e1026caa44871db55 100644 --- a/Demos/Device/LowLevel/AudioOutput/AudioOutput.c +++ b/Demos/Device/LowLevel/AudioOutput/AudioOutput.c @@ -180,6 +180,10 @@ 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)) + return; + /* Check to see if the streaming interface is selected, if not the host is not receiving audio */ if (!(StreamingAudioInterfaceSelected)) return; diff --git a/Demos/Device/LowLevel/CDC/CDC.c b/Demos/Device/LowLevel/CDC/CDC.c index ac0623a4de068f957ece54d585fcadb2500846f5..d8263b11f75a4162fc11e2425bb2b2cd7eb97d8b 100644 --- a/Demos/Device/LowLevel/CDC/CDC.c +++ b/Demos/Device/LowLevel/CDC/CDC.c @@ -254,6 +254,10 @@ void CDC_Task(void) "Joystick Pressed\r\n", }; + /* Device must be connected and configured for the task to run */ + if (!(USB_IsConnected) || !(USB_ConfigurationNumber)) + return; + #if 0 /* NOTE: Here you can use the notification endpoint to send back line state changes to the host, for the special RS-232 * handshake signal lines (and some error states), via the CONTROL_LINE_IN_* masks and the following code: diff --git a/Demos/Device/LowLevel/DualCDC/DualCDC.c b/Demos/Device/LowLevel/DualCDC/DualCDC.c index 1031068ad07cc101c8bebcfafcbbbff9b206c54b..862945e63187d5a76807e9ba27990a250502b5d9 100644 --- a/Demos/Device/LowLevel/DualCDC/DualCDC.c +++ b/Demos/Device/LowLevel/DualCDC/DualCDC.c @@ -229,6 +229,10 @@ void CDC1_Task(void) uint8_t JoyStatus_LCL = Joystick_GetStatus(); static bool ActionSent = false; + /* Device must be connected and configured for the task to run */ + if (!(USB_IsConnected) || !(USB_ConfigurationNumber)) + return; + char* JoystickStrings[] = { "Joystick Up\r\n", @@ -288,6 +292,10 @@ void CDC1_Task(void) */ void CDC2_Task(void) { + /* Device must be connected and configured for the task to run */ + if (!(USB_IsConnected) || !(USB_ConfigurationNumber)) + return; + /* Select the Serial Rx Endpoint */ Endpoint_SelectEndpoint(CDC2_RX_EPNUM); diff --git a/Demos/Device/LowLevel/GenericHID/GenericHID.c b/Demos/Device/LowLevel/GenericHID/GenericHID.c index f467494723698d628b688641c2a570df15708076..5c6d1ee4701ccb4ab015812cc755845706dc135f 100644 --- a/Demos/Device/LowLevel/GenericHID/GenericHID.c +++ b/Demos/Device/LowLevel/GenericHID/GenericHID.c @@ -202,47 +202,47 @@ void CreateGenericHIDReport(uint8_t* DataArray) void HID_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_IsConnected) || !(USB_ConfigurationNumber)) + return; + + Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM); + + /* Check to see if a packet has been sent from the host */ + if (Endpoint_IsOUTReceived()) { - Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM); - - /* Check to see if a packet has been sent from the host */ - if (Endpoint_IsOUTReceived()) + /* Check to see if the packet contains data */ + if (Endpoint_IsReadWriteAllowed()) { - /* Check to see if the packet contains data */ - if (Endpoint_IsReadWriteAllowed()) - { - /* Create a temporary buffer to hold the read in report from the host */ - uint8_t GenericData[GENERIC_REPORT_SIZE]; - - /* Read Generic Report Data */ - Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData)); - - /* Process Generic Report Data */ - ProcessGenericHIDReport(GenericData); - } + /* Create a temporary buffer to hold the read in report from the host */ + uint8_t GenericData[GENERIC_REPORT_SIZE]; + + /* Read Generic Report Data */ + Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData)); + + /* Process Generic Report Data */ + ProcessGenericHIDReport(GenericData); + } - /* Finalize the stream transfer to send the last packet */ - Endpoint_ClearOUT(); - } + /* Finalize the stream transfer to send the last packet */ + Endpoint_ClearOUT(); + } - Endpoint_SelectEndpoint(GENERIC_IN_EPNUM); + Endpoint_SelectEndpoint(GENERIC_IN_EPNUM); + + /* Check to see if the host is ready to accept another packet */ + if (Endpoint_IsINReady()) + { + /* Create a temporary buffer to hold the report to send to the host */ + uint8_t GenericData[GENERIC_REPORT_SIZE]; - /* Check to see if the host is ready to accept another packet */ - if (Endpoint_IsINReady()) - { - /* Create a temporary buffer to hold the report to send to the host */ - uint8_t GenericData[GENERIC_REPORT_SIZE]; - - /* Create Generic Report Data */ - CreateGenericHIDReport(GenericData); + /* Create Generic Report Data */ + CreateGenericHIDReport(GenericData); - /* Write Generic Report Data */ - Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData)); + /* Write Generic Report Data */ + Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData)); - /* Finalize the stream transfer to send the last packet */ - Endpoint_ClearIN(); - } + /* Finalize the stream transfer to send the last packet */ + Endpoint_ClearIN(); } } diff --git a/Demos/Device/LowLevel/Joystick/Joystick.c b/Demos/Device/LowLevel/Joystick/Joystick.c index 42ae62caac731b572105a93de54b23539205fc2c..e117475376235c2e8227babcd69b94d89a0d1054 100644 --- a/Demos/Device/LowLevel/Joystick/Joystick.c +++ b/Demos/Device/LowLevel/Joystick/Joystick.c @@ -181,28 +181,28 @@ bool GetNextReport(USB_JoystickReport_Data_t* ReportData) /** Function to manage HID report generation and transmission to the host. */ void HID_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_IsConnected) || !(USB_ConfigurationNumber)) + return; + + /* Select the Joystick Report Endpoint */ + Endpoint_SelectEndpoint(JOYSTICK_EPNUM); + + /* Check to see if the host is ready for another packet */ + if (Endpoint_IsINReady()) { - /* Select the Joystick Report Endpoint */ - Endpoint_SelectEndpoint(JOYSTICK_EPNUM); - - /* Check to see if the host is ready for another packet */ - if (Endpoint_IsINReady()) - { - USB_JoystickReport_Data_t JoystickReportData; - - /* Create the next HID report to send to the host */ - GetNextReport(&JoystickReportData); + USB_JoystickReport_Data_t JoystickReportData; - /* Write Joystick Report Data */ - Endpoint_Write_Stream_LE(&JoystickReportData, sizeof(JoystickReportData)); - - /* Finalize the stream transfer to send the last packet */ - Endpoint_ClearIN(); - - /* Clear the report data afterwards */ - memset(&JoystickReportData, 0, sizeof(JoystickReportData)); - } + /* Create the next HID report to send to the host */ + GetNextReport(&JoystickReportData); + + /* Write Joystick Report Data */ + Endpoint_Write_Stream_LE(&JoystickReportData, sizeof(JoystickReportData)); + + /* Finalize the stream transfer to send the last packet */ + Endpoint_ClearIN(); + + /* Clear the report data afterwards */ + memset(&JoystickReportData, 0, sizeof(JoystickReportData)); } } diff --git a/Demos/Device/LowLevel/Keyboard/Keyboard.c b/Demos/Device/LowLevel/Keyboard/Keyboard.c index 25271480891ff8adf800ae946c7836e2a31cb940..9950484bae970ea11094830a48248edae2fe8c3f 100644 --- a/Demos/Device/LowLevel/Keyboard/Keyboard.c +++ b/Demos/Device/LowLevel/Keyboard/Keyboard.c @@ -377,13 +377,13 @@ void ReceiveNextReport(void) /** Function to manage HID report generation and transmission to the host, when in report mode. */ void HID_Task(void) { - /* Check if the USB system is connected to a host */ - if (USB_IsConnected) - { - /* Send the next keypress report to the host */ - SendNextReport(); + /* Device must be connected and configured for the task to run */ + if (!(USB_IsConnected) || !(USB_ConfigurationNumber)) + return; + + /* Send the next keypress report to the host */ + SendNextReport(); - /* Process the LED report sent from the host */ - ReceiveNextReport(); - } + /* Process the LED report sent from the host */ + ReceiveNextReport(); } diff --git a/Demos/Device/LowLevel/KeyboardMouse/KeyboardMouse.c b/Demos/Device/LowLevel/KeyboardMouse/KeyboardMouse.c index ddfe05a4b4db64ae8c0c309ec5e8a611d034073f..c60f08f9c526916b1cc2f143c5d692ca4960a42e 100644 --- a/Demos/Device/LowLevel/KeyboardMouse/KeyboardMouse.c +++ b/Demos/Device/LowLevel/KeyboardMouse/KeyboardMouse.c @@ -212,6 +212,10 @@ 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)) + return; + /* Check if board button is not pressed, if so mouse mode enabled */ if (!(Buttons_GetStatus() & BUTTONS_BUTTON1)) { @@ -228,51 +232,47 @@ void Keyboard_HID_Task(void) if (JoyStatus_LCL & JOY_PRESS) KeyboardReportData.KeyCode[0] = 0x08; // E } - - /* Check if the USB system is connected to a host and report protocol mode is enabled */ - if (USB_IsConnected) + + /* Select the Keyboard Report Endpoint */ + Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM); + + /* Check if Keyboard Endpoint Ready for Read/Write */ + if (Endpoint_IsReadWriteAllowed()) { - /* Select the Keyboard Report Endpoint */ - Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM); - - /* Check if Keyboard Endpoint Ready for Read/Write */ - if (Endpoint_IsReadWriteAllowed()) - { - /* Write Keyboard Report Data */ - Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData)); - - /* Finalize the stream transfer to send the last packet */ - Endpoint_ClearIN(); - - /* Clear the report data afterwards */ - memset(&KeyboardReportData, 0, sizeof(KeyboardReportData)); - } - - /* Select the Keyboard LED Report Endpoint */ - Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM); - - /* Check if Keyboard LED Endpoint Ready for Read/Write */ - if (Endpoint_IsReadWriteAllowed()) - { - /* Read in the LED report from the host */ - uint8_t LEDStatus = Endpoint_Read_Byte(); - uint8_t LEDMask = LEDS_LED2; - - if (LEDStatus & 0x01) // NUM Lock - LEDMask |= LEDS_LED1; - - if (LEDStatus & 0x02) // CAPS Lock - LEDMask |= LEDS_LED3; + /* Write Keyboard Report Data */ + Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData)); + + /* Finalize the stream transfer to send the last packet */ + Endpoint_ClearIN(); + + /* Clear the report data afterwards */ + memset(&KeyboardReportData, 0, sizeof(KeyboardReportData)); + } + + /* Select the Keyboard LED Report Endpoint */ + Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM); + + /* Check if Keyboard LED Endpoint Ready for Read/Write */ + if (Endpoint_IsReadWriteAllowed()) + { + /* Read in the LED report from the host */ + uint8_t LEDStatus = Endpoint_Read_Byte(); + uint8_t LEDMask = LEDS_LED2; + + if (LEDStatus & 0x01) // NUM Lock + LEDMask |= LEDS_LED1; + + if (LEDStatus & 0x02) // CAPS Lock + LEDMask |= LEDS_LED3; - if (LEDStatus & 0x04) // SCROLL Lock - LEDMask |= LEDS_LED4; + if (LEDStatus & 0x04) // SCROLL Lock + LEDMask |= LEDS_LED4; - /* Set the status LEDs to the current Keyboard LED status */ - LEDs_SetAllLEDs(LEDMask); + /* Set the status LEDs to the current Keyboard LED status */ + LEDs_SetAllLEDs(LEDMask); - /* Handshake the OUT Endpoint - clear endpoint and ready for next report */ - Endpoint_ClearOUT(); - } + /* Handshake the OUT Endpoint - clear endpoint and ready for next report */ + Endpoint_ClearOUT(); } } @@ -283,6 +283,10 @@ 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)) + return; + /* Check if board button is pressed, if so mouse mode enabled */ if (Buttons_GetStatus() & BUTTONS_BUTTON1) { @@ -300,23 +304,19 @@ void Mouse_HID_Task(void) MouseReportData.Button = (1 << 0); } - /* Check if the USB system is connected to a host and report protocol mode is enabled */ - if (USB_IsConnected) - { - /* Select the Mouse Report Endpoint */ - Endpoint_SelectEndpoint(MOUSE_IN_EPNUM); + /* Select the Mouse Report Endpoint */ + Endpoint_SelectEndpoint(MOUSE_IN_EPNUM); - /* Check if Mouse Endpoint Ready for Read/Write */ - if (Endpoint_IsReadWriteAllowed()) - { - /* Write Mouse Report Data */ - Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData)); + /* Check if Mouse Endpoint Ready for Read/Write */ + if (Endpoint_IsReadWriteAllowed()) + { + /* Write Mouse Report Data */ + Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData)); - /* Finalize the stream transfer to send the last packet */ - Endpoint_ClearIN(); + /* Finalize the stream transfer to send the last packet */ + Endpoint_ClearIN(); - /* Clear the report data afterwards */ - memset(&MouseReportData, 0, sizeof(MouseReportData)); - } + /* Clear the report data afterwards */ + memset(&MouseReportData, 0, sizeof(MouseReportData)); } } diff --git a/Demos/Device/LowLevel/MIDI/MIDI.c b/Demos/Device/LowLevel/MIDI/MIDI.c index 84d3778545195756c221a845b6c67deb2af0841d..954cbcbf640d01fc37fad6862dec9bf7cd58d528 100644 --- a/Demos/Device/LowLevel/MIDI/MIDI.c +++ b/Demos/Device/LowLevel/MIDI/MIDI.c @@ -116,16 +116,17 @@ void MIDI_Task(void) { static uint8_t PrevJoystickStatus; - /* Select the MIDI IN stream */ + /* Device must be connected and configured for the task to run */ + if (!(USB_IsConnected) || !(USB_ConfigurationNumber)) + return; + Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPNUM); - /* Check if endpoint is ready to be written to */ if (Endpoint_IsINReady()) { uint8_t MIDICommand = 0; uint8_t MIDIPitch; - /* Get current joystick mask, XOR with previous to detect joystick changes */ uint8_t JoystickStatus = Joystick_GetStatus(); uint8_t JoystickChanges = (JoystickStatus ^ PrevJoystickStatus); diff --git a/Demos/Device/LowLevel/Mouse/Mouse.c b/Demos/Device/LowLevel/Mouse/Mouse.c index 60eb7fad7b4ac86a841f2908e7b96cc51c8381db..febb36450c2a6b596d12843d32ec04359a1c7e36 100644 --- a/Demos/Device/LowLevel/Mouse/Mouse.c +++ b/Demos/Device/LowLevel/Mouse/Mouse.c @@ -313,10 +313,10 @@ void SendNextReport(void) /** Task to manage HID report generation and transmission to the host, when in report mode. */ void Mouse_Task(void) { - /* Check if the USB system is connected to a host */ - if (USB_IsConnected) - { - /* Send the next mouse report to the host */ - SendNextReport(); - } + /* Device must be connected and configured for the task to run */ + if (!(USB_IsConnected) || !(USB_ConfigurationNumber)) + return; + + /* Send the next mouse report to the host */ + SendNextReport(); } diff --git a/Demos/Device/LowLevel/RNDISEthernet/RNDISEthernet.c b/Demos/Device/LowLevel/RNDISEthernet/RNDISEthernet.c index d77d25b6b60cd071f9df90932fa2e279954498bd..1c0e0304cc8cda0b412edf03d71858bde74d0416 100644 --- a/Demos/Device/LowLevel/RNDISEthernet/RNDISEthernet.c +++ b/Demos/Device/LowLevel/RNDISEthernet/RNDISEthernet.c @@ -284,6 +284,10 @@ void Ethernet_Task(void) outgoing frames should be loaded into the FrameOUT structure. Both structures can only hold a single 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)) + return; + /* Check if a frame has been written to the IN frame buffer */ if (FrameIN.FrameInBuffer) { diff --git a/Demos/Device/LowLevel/USBtoSerial/USBtoSerial.c b/Demos/Device/LowLevel/USBtoSerial/USBtoSerial.c index c48c33c08335ac90eecf0974787747d950db79bf..9160dd10733b351f492eb8701a93ecfa0344ade9 100644 --- a/Demos/Device/LowLevel/USBtoSerial/USBtoSerial.c +++ b/Demos/Device/LowLevel/USBtoSerial/USBtoSerial.c @@ -204,89 +204,90 @@ void EVENT_USB_UnhandledControlPacket(void) /** Task to manage CDC data transmission and reception to and from the host, from and to the physical USART. */ void CDC_Task(void) { - if (USB_IsConnected) - { + /* Device must be connected and configured for the task to run */ + if (!(USB_IsConnected) || !(USB_ConfigurationNumber)) + return; + #if 0 - /* NOTE: Here you can use the notification endpoint to send back line state changes to the host, for the special RS-232 - handshake signal lines (and some error states), via the CONTROL_LINE_IN_* masks and the following code: - */ + /* NOTE: Here you can use the notification endpoint to send back line state changes to the host, for the special RS-232 + handshake signal lines (and some error states), via the CONTROL_LINE_IN_* masks and the following code: + */ - USB_Notification_Header_t Notification = (USB_Notification_Header_t) - { - .NotificationType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE), - .Notification = NOTIF_SerialState, - .wValue = 0, - .wIndex = 0, - .wLength = sizeof(uint16_t), - }; - - uint16_t LineStateMask; - - // Set LineStateMask here to a mask of CONTROL_LINE_IN_* masks to set the input handshake line states to send to the host + USB_Notification_Header_t Notification = (USB_Notification_Header_t) + { + .NotificationType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE), + .Notification = NOTIF_SerialState, + .wValue = 0, + .wIndex = 0, + .wLength = sizeof(uint16_t), + }; - Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPNUM); - Endpoint_Write_Stream_LE(&Notification, sizeof(Notification)); - Endpoint_Write_Stream_LE(&LineStateMask, sizeof(LineStateMask)); - Endpoint_ClearIN(); + uint16_t LineStateMask; + + // Set LineStateMask here to a mask of CONTROL_LINE_IN_* masks to set the input handshake line states to send to the host + + Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPNUM); + Endpoint_Write_Stream_LE(&Notification, sizeof(Notification)); + Endpoint_Write_Stream_LE(&LineStateMask, sizeof(LineStateMask)); + Endpoint_ClearIN(); #endif - /* Select the Serial Rx Endpoint */ - Endpoint_SelectEndpoint(CDC_RX_EPNUM); - - /* Check to see if a packet has been received from the host */ - if (Endpoint_IsOUTReceived()) + /* Select the Serial Rx Endpoint */ + Endpoint_SelectEndpoint(CDC_RX_EPNUM); + + /* Check to see if a packet has been received from the host */ + if (Endpoint_IsOUTReceived()) + { + /* Read the bytes in from the endpoint into the buffer while space is available */ + while (Endpoint_BytesInEndpoint() && (Rx_Buffer.Elements != BUFF_STATICSIZE)) { - /* Read the bytes in from the endpoint into the buffer while space is available */ - while (Endpoint_BytesInEndpoint() && (Rx_Buffer.Elements != BUFF_STATICSIZE)) - { - /* Store each character from the endpoint */ - Buffer_StoreElement(&Rx_Buffer, Endpoint_Read_Byte()); - } - - /* Check to see if all bytes in the current packet have been read */ - if (!(Endpoint_BytesInEndpoint())) - { - /* Clear the endpoint buffer */ - Endpoint_ClearOUT(); - } + /* Store each character from the endpoint */ + Buffer_StoreElement(&Rx_Buffer, Endpoint_Read_Byte()); } - /* Check if Rx buffer contains data - if so, send it */ - if (Rx_Buffer.Elements) - Serial_TxByte(Buffer_GetElement(&Rx_Buffer)); + /* Check to see if all bytes in the current packet have been read */ + if (!(Endpoint_BytesInEndpoint())) + { + /* Clear the endpoint buffer */ + Endpoint_ClearOUT(); + } + } + + /* Check if Rx buffer contains data - if so, send it */ + if (Rx_Buffer.Elements) + Serial_TxByte(Buffer_GetElement(&Rx_Buffer)); + + /* Select the Serial Tx Endpoint */ + Endpoint_SelectEndpoint(CDC_TX_EPNUM); - /* Select the Serial Tx Endpoint */ - Endpoint_SelectEndpoint(CDC_TX_EPNUM); + /* Check if the Tx buffer contains anything to be sent to the host */ + if (Tx_Buffer.Elements) + { + /* Wait until Serial Tx Endpoint Ready for Read/Write */ + while (!(Endpoint_IsReadWriteAllowed())); + + /* Write the bytes from the buffer to the endpoint while space is available */ + while (Tx_Buffer.Elements && Endpoint_IsReadWriteAllowed()) + { + /* Write each byte retreived from the buffer to the endpoint */ + Endpoint_Write_Byte(Buffer_GetElement(&Tx_Buffer)); + } + + /* Remember if the packet to send completely fills the endpoint */ + bool IsFull = (Endpoint_BytesInEndpoint() == CDC_TXRX_EPSIZE); + + /* Send the data */ + Endpoint_ClearIN(); - /* Check if the Tx buffer contains anything to be sent to the host */ - if (Tx_Buffer.Elements) + /* If no more data to send and the last packet filled the endpoint, send an empty packet to release + * the buffer on the receiver (otherwise all data will be cached until a non-full packet is received) */ + if (IsFull && !(Tx_Buffer.Elements)) { /* Wait until Serial Tx Endpoint Ready for Read/Write */ while (!(Endpoint_IsReadWriteAllowed())); - - /* Write the bytes from the buffer to the endpoint while space is available */ - while (Tx_Buffer.Elements && Endpoint_IsReadWriteAllowed()) - { - /* Write each byte retreived from the buffer to the endpoint */ - Endpoint_Write_Byte(Buffer_GetElement(&Tx_Buffer)); - } - - /* Remember if the packet to send completely fills the endpoint */ - bool IsFull = (Endpoint_BytesInEndpoint() == CDC_TXRX_EPSIZE); - - /* Send the data */ - Endpoint_ClearIN(); - /* If no more data to send and the last packet filled the endpoint, send an empty packet to release - * the buffer on the receiver (otherwise all data will be cached until a non-full packet is received) */ - if (IsFull && !(Tx_Buffer.Elements)) - { - /* Wait until Serial Tx Endpoint Ready for Read/Write */ - while (!(Endpoint_IsReadWriteAllowed())); - - /* Send an empty packet to terminate the transfer */ - Endpoint_ClearIN(); - } + /* Send an empty packet to terminate the transfer */ + Endpoint_ClearIN(); } } } diff --git a/Demos/Host/ClassDriver/CDCHost/CDCHost.c b/Demos/Host/ClassDriver/CDCHost/CDCHost.c index 1edb352f52d74cc764f5a64503c5ea3f2cd8c8f3..0c331bcde8c02cc3df832e13d3ba72561dbdf97f 100644 --- a/Demos/Host/ClassDriver/CDCHost/CDCHost.c +++ b/Demos/Host/ClassDriver/CDCHost/CDCHost.c @@ -62,7 +62,7 @@ int main(void) { SetupHardware(); - puts_P(PSTR(ESC_FG_CYAN "CDC Host Demo running.\r\n")); + puts_P(PSTR(ESC_FG_CYAN "CDC Host Demo running.\r\n" ESC_FG_WHITE)); LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); @@ -72,8 +72,11 @@ int main(void) { case HOST_STATE_Addressed: if (!(CDC_Host_ConfigurePipes(&VirtualSerial_CDC_Interface))) - LEDs_SetAllLEDs(LEDMASK_USB_ERROR); - + { + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + USB_HostState = HOST_STATE_WaitForDeviceRemoval; + } + USB_HostState = HOST_STATE_Configured; break; case HOST_STATE_Configured: @@ -135,8 +138,8 @@ void EVENT_USB_HostError(const uint8_t ErrorCode) { USB_ShutDown(); - puts_P(PSTR(ESC_FG_RED "Host Mode Error\r\n" ESC_FG_WHITE)); - printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode); + puts_P(PSTR(ESC_FG_RED "Host Mode Error\r\n")); + printf_P(PSTR(" -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode); LEDs_SetAllLEDs(LEDMASK_USB_ERROR); for(;;); diff --git a/Demos/Host/LowLevel/PrinterHost/PrinterHost.c b/Demos/Host/LowLevel/PrinterHost/PrinterHost.c index 5623dc7e7ea1753558fd13f304da523d36cfb26e..3b145a63dac0185fdffa4ab9452550534fe5d488 100644 --- a/Demos/Host/LowLevel/PrinterHost/PrinterHost.c +++ b/Demos/Host/LowLevel/PrinterHost/PrinterHost.c @@ -226,7 +226,7 @@ void USB_Printer_Host(void) Printer_Data_t TestPageData = { - "\033%-12345X\033E LUFA PCL Test Page \033E\033%-12345X", + "\033%-12345X\033E" "LUFA PCL Test Page" "\033E\033%-12345X", (sizeof(TestPageData.Data) - 1) }; diff --git a/LUFA/Drivers/USB/Class/Device/CDC.c b/LUFA/Drivers/USB/Class/Device/CDC.c index 07c05141bd715c902eff63822e7340a89b937fef..12b55938c427034a158a209f1e97b7a167e8a1e0 100644 --- a/LUFA/Drivers/USB/Class/Device/CDC.c +++ b/LUFA/Drivers/USB/Class/Device/CDC.c @@ -114,7 +114,7 @@ bool CDC_Device_ConfigureEndpoints(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo) void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo) { - if (!(USB_IsConnected)) + if (!(USB_IsConnected) || !(USB_ConfigurationNumber)) return; Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber); diff --git a/LUFA/Drivers/USB/Class/Device/HID.c b/LUFA/Drivers/USB/Class/Device/HID.c index 2ac0dd3548d333fe5f71dcdab465e5b19cbbaee1..4a6295ab0c4f3205e9b31a731ec6e57a548650fb 100644 --- a/LUFA/Drivers/USB/Class/Device/HID.c +++ b/LUFA/Drivers/USB/Class/Device/HID.c @@ -152,7 +152,7 @@ bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* const HIDInterfac void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo) { - if (!(USB_IsConnected)) + if (!(USB_IsConnected) || !(USB_ConfigurationNumber)) return; Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber); diff --git a/LUFA/Drivers/USB/Class/Device/MassStorage.c b/LUFA/Drivers/USB/Class/Device/MassStorage.c index 9d73989b70affa9ef9a4db190d6ced6ff5c3b5e5..ceb4b9647d3b734e8a262a81ccfa13c4d0d18c43 100644 --- a/LUFA/Drivers/USB/Class/Device/MassStorage.c +++ b/LUFA/Drivers/USB/Class/Device/MassStorage.c @@ -96,7 +96,7 @@ bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t* const MSInterfaceIn void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo) { - if (!(USB_IsConnected)) + if (!(USB_IsConnected) || !(USB_ConfigurationNumber)) return; Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber); diff --git a/LUFA/Drivers/USB/Class/Device/RNDIS.c b/LUFA/Drivers/USB/Class/Device/RNDIS.c index 1b2da6ff3d7758b1f473b3844c18c8cea568c748..0d35ee6b400464bcebe423cd3a9a19757f240d81 100644 --- a/LUFA/Drivers/USB/Class/Device/RNDIS.c +++ b/LUFA/Drivers/USB/Class/Device/RNDIS.c @@ -138,7 +138,7 @@ bool RNDIS_Device_ConfigureEndpoints(USB_ClassInfo_RNDIS_Device_t* const RNDISIn void RNDIS_Device_USBTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo) { - if (!(USB_IsConnected)) + if (!(USB_IsConnected) || !(USB_ConfigurationNumber)) return; RNDIS_Message_Header_t* MessageHeader = (RNDIS_Message_Header_t*)&RNDISInterfaceInfo->State.RNDISMessageBuffer;