X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/071e02c6b6b4837fa9cf0b6d4c749994e02638d7..5a4def747897c1c6ffbe465506d846c7c686d3e9:/Demos/Device/LowLevel/DualVirtualSerial/DualVirtualSerial.c diff --git a/Demos/Device/LowLevel/DualVirtualSerial/DualVirtualSerial.c b/Demos/Device/LowLevel/DualVirtualSerial/DualVirtualSerial.c index 29fd57f29..45159a8f8 100644 --- a/Demos/Device/LowLevel/DualVirtualSerial/DualVirtualSerial.c +++ b/Demos/Device/LowLevel/DualVirtualSerial/DualVirtualSerial.c @@ -1,7 +1,7 @@ /* LUFA Library Copyright (C) Dean Camera, 2010. - + dean [at] fourwalledcubicle [dot] com www.fourwalledcubicle.com */ @@ -9,13 +9,13 @@ /* Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) - Permission to use, copy, modify, distribute, and sell this + Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted - without fee, provided that the above copyright notice appear in + without fee, provided that the above copyright notice appear in all copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the software without specific, written prior permission. The author disclaim all warranties with regard to this @@ -33,7 +33,7 @@ * Main source file for the DualVirtualSerial demo. This file contains the main tasks of the demo and * is responsible for the initial application hardware configuration. */ - + #include "DualVirtualSerial.h" /** Contains the current baud rate and other settings of the first virtual serial port. While this demo does not use @@ -69,7 +69,7 @@ CDC_Line_Coding_t LineEncoding2 = { .BaudRateBPS = 0, int main(void) { SetupHardware(); - + LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); sei(); @@ -119,57 +119,31 @@ void EVENT_USB_Device_Disconnect(void) * of the USB device after enumeration - the device endpoints are configured and the CDC management tasks are started. */ void EVENT_USB_Device_ConfigurationChanged(void) -{ - /* Indicate USB connected and ready */ - LEDs_SetAllLEDs(LEDMASK_USB_READY); - - /* Setup CDC Notification, Rx and Tx Endpoints for the first CDC */ - if (!(Endpoint_ConfigureEndpoint(CDC1_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT, - ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE, - ENDPOINT_BANK_SINGLE))) - { - LEDs_SetAllLEDs(LEDMASK_USB_ERROR); - } - - if (!(Endpoint_ConfigureEndpoint(CDC1_TX_EPNUM, EP_TYPE_BULK, - ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE, - ENDPOINT_BANK_SINGLE))) - { - LEDs_SetAllLEDs(LEDMASK_USB_ERROR); - } - - if (!(Endpoint_ConfigureEndpoint(CDC1_RX_EPNUM, EP_TYPE_BULK, - ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE, - ENDPOINT_BANK_SINGLE))) - { - LEDs_SetAllLEDs(LEDMASK_USB_ERROR); - } - - /* Setup CDC Notification, Rx and Tx Endpoints for the second CDC */ - if (!(Endpoint_ConfigureEndpoint(CDC2_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT, - ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE, - ENDPOINT_BANK_SINGLE))) - { - LEDs_SetAllLEDs(LEDMASK_USB_ERROR); - } - - if (!(Endpoint_ConfigureEndpoint(CDC2_TX_EPNUM, EP_TYPE_BULK, - ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE, - ENDPOINT_BANK_SINGLE))) - { - LEDs_SetAllLEDs(LEDMASK_USB_ERROR); - } - - if (!(Endpoint_ConfigureEndpoint(CDC2_RX_EPNUM, EP_TYPE_BULK, - ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE, - ENDPOINT_BANK_SINGLE))) - { - LEDs_SetAllLEDs(LEDMASK_USB_ERROR); - } - +{ + bool ConfigSuccess = true; + + /* Setup first CDC Interface's Endpoints */ + ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC1_TX_EPNUM, EP_TYPE_BULK, ENDPOINT_DIR_IN, + CDC_TXRX_EPSIZE, ENDPOINT_BANK_SINGLE); + ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC1_RX_EPNUM, EP_TYPE_BULK, ENDPOINT_DIR_OUT, + CDC_TXRX_EPSIZE, ENDPOINT_BANK_SINGLE); + ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC1_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, + CDC_NOTIFICATION_EPSIZE, ENDPOINT_BANK_SINGLE); + + /* Setup second CDC Interface's Endpoints */ + ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC2_TX_EPNUM, EP_TYPE_BULK, ENDPOINT_DIR_IN, + CDC_TXRX_EPSIZE, ENDPOINT_BANK_SINGLE); + ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC2_RX_EPNUM, EP_TYPE_BULK, ENDPOINT_DIR_OUT, + CDC_TXRX_EPSIZE, ENDPOINT_BANK_SINGLE); + ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC2_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN, + CDC_NOTIFICATION_EPSIZE, ENDPOINT_BANK_SINGLE); + /* Reset line encoding baud rates so that the host knows to send new values */ LineEncoding1.BaudRateBPS = 0; LineEncoding2.BaudRateBPS = 0; + + /* Indicate endpoint configuration success or failure */ + LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR); } /** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific @@ -179,48 +153,40 @@ void EVENT_USB_Device_ConfigurationChanged(void) void EVENT_USB_Device_UnhandledControlRequest(void) { /* Determine which interface's Line Coding data is being set from the wIndex parameter */ - uint8_t* LineEncodingData = (USB_ControlRequest.wIndex == 0) ? (uint8_t*)&LineEncoding1 : (uint8_t*)&LineEncoding2; + void* LineEncodingData = (USB_ControlRequest.wIndex == 0) ? &LineEncoding1 : &LineEncoding2; /* Process CDC specific control requests */ switch (USB_ControlRequest.bRequest) { case REQ_GetLineEncoding: if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) - { - /* Acknowledge the SETUP packet, ready for data transfer */ + { Endpoint_ClearSETUP(); /* Write the line coding data to the control endpoint */ Endpoint_Write_Control_Stream_LE(LineEncodingData, sizeof(CDC_Line_Coding_t)); - - /* Finalize the stream transfer to send the last packet or clear the host abort */ Endpoint_ClearOUT(); } - + break; case REQ_SetLineEncoding: if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) { - /* Acknowledge the SETUP packet, ready for data transfer */ Endpoint_ClearSETUP(); /* Read the line coding data in from the host into the global struct */ Endpoint_Read_Control_Stream_LE(LineEncodingData, sizeof(CDC_Line_Coding_t)); - - /* Finalize the stream transfer to clear the last packet from the host */ Endpoint_ClearIN(); } - + break; case REQ_SetControlLineState: if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) { - /* Acknowledge the SETUP packet, ready for data transfer */ Endpoint_ClearSETUP(); - Endpoint_ClearStatusStage(); } - + break; } } @@ -233,7 +199,7 @@ void CDC1_Task(void) char* ReportString = NULL; uint8_t JoyStatus_LCL = Joystick_GetStatus(); static bool ActionSent = false; - + /* Device must be connected and configured for the task to run */ if (USB_DeviceState != DEVICE_STATE_Configured) return; @@ -256,26 +222,26 @@ void CDC1_Task(void) if ((ReportString != NULL) && (ActionSent == false) && LineEncoding1.BaudRateBPS) { ActionSent = true; - + /* Select the Serial Tx Endpoint */ Endpoint_SelectEndpoint(CDC1_TX_EPNUM); /* Write the String to the Endpoint */ Endpoint_Write_Stream_LE(ReportString, strlen(ReportString)); - + /* Finalize the stream transfer to send the last packet */ Endpoint_ClearIN(); /* Wait until the endpoint is ready for another packet */ Endpoint_WaitUntilReady(); - + /* Send an empty packet to ensure that the host does not buffer data sent to it */ Endpoint_ClearIN(); } /* Select the Serial Rx Endpoint */ Endpoint_SelectEndpoint(CDC1_RX_EPNUM); - + /* Throw away any received data from the host */ if (Endpoint_IsOUTReceived()) Endpoint_ClearOUT(); @@ -292,16 +258,16 @@ void CDC2_Task(void) /* Select the Serial Rx Endpoint */ Endpoint_SelectEndpoint(CDC2_RX_EPNUM); - + /* Check to see if any data has been received */ if (Endpoint_IsOUTReceived()) { /* Create a temp buffer big enough to hold the incoming endpoint packet */ uint8_t Buffer[Endpoint_BytesInEndpoint()]; - + /* Remember how large the incoming packet is */ uint16_t DataLength = Endpoint_BytesInEndpoint(); - + /* Read in the incoming packet into the buffer */ Endpoint_Read_Stream_LE(&Buffer, DataLength); @@ -310,7 +276,7 @@ void CDC2_Task(void) /* Select the Serial Tx Endpoint */ Endpoint_SelectEndpoint(CDC2_TX_EPNUM); - + /* Write the received data to the endpoint */ Endpoint_Write_Stream_LE(&Buffer, DataLength); @@ -324,3 +290,4 @@ void CDC2_Task(void) Endpoint_ClearIN(); } } +