X-Git-Url: http://git.linex4red.de/pub/lufa.git/blobdiff_plain/67f390fe746ccc4d1dceac23f590eb1723cb7ff2..042d3288ad52301c5b0ef69eada03eee83497dda:/Demos/Device/LowLevel/CDC/CDC.c diff --git a/Demos/Device/LowLevel/CDC/CDC.c b/Demos/Device/LowLevel/CDC/CDC.c index d8263b11f..9ee744ea0 100644 --- a/Demos/Device/LowLevel/CDC/CDC.c +++ b/Demos/Device/LowLevel/CDC/CDC.c @@ -45,41 +45,51 @@ * It is possible to completely ignore these value or use other settings as the host is completely unaware of the physical * serial link characteristics and instead sends and receives data in endpoint streams. */ -CDC_Line_Coding_t LineCoding = { .BaudRateBPS = 9600, - .CharFormat = OneStopBit, - .ParityType = Parity_None, - .DataBits = 8 }; +CDC_Line_Coding_t LineEncoding = { .BaudRateBPS = 0, + .CharFormat = OneStopBit, + .ParityType = Parity_None, + .DataBits = 8 }; #if 0 /* NOTE: Here you can set up a standard stream using the created virtual serial port, so that the standard stream functions in * can be used on the virtual serial port (e.g. fprintf(&USBSerial, "Test"); to print a string). */ -static int CDC_putchar (char c, FILE *stream) -{ - if (!(USB_IsConnected)) - return -1; - +static int CDC_putchar(char c, FILE *stream) +{ Endpoint_SelectEndpoint(CDC_TX_EPNUM); - while (!(Endpoint_IsReadWriteAllowed())); + + if (!(LineEncoding.BaudRateBPS)) + return -1; + + while (!(Endpoint_IsReadWriteAllowed())) + { + if (USB_DeviceState != DEVICE_STATE_Configured) + return -1; + } + Endpoint_Write_Byte(c); Endpoint_ClearIN(); return 0; } -static int CDC_getchar (FILE *stream) +static int CDC_getchar(FILE *stream) { int c; - + + if (!(LineEncoding.BaudRateBPS)) + return -1; + Endpoint_SelectEndpoint(CDC_RX_EPNUM); for (;;) { - if (!(USB_IsConnected)) - return -1; - - while (!(Endpoint_IsReadWriteAllowed())); + while (!(Endpoint_IsReadWriteAllowed())) + { + if (USB_DeviceState != DEVICE_STATE_Configured) + return -1; + } if (!(Endpoint_BytesInEndpoint())) { @@ -133,7 +143,7 @@ void SetupHardware(void) /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and * starts the library USB task to begin the enumeration and USB management process. */ -void EVENT_USB_Connect(void) +void EVENT_USB_Device_Connect(void) { /* Indicate USB enumerating */ LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING); @@ -142,7 +152,7 @@ void EVENT_USB_Connect(void) /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via * the status LEDs and stops the USB management and CDC management tasks. */ -void EVENT_USB_Disconnect(void) +void EVENT_USB_Device_Disconnect(void) { /* Indicate USB not ready */ LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); @@ -151,7 +161,7 @@ void EVENT_USB_Disconnect(void) /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration * of the USB device after enumeration - the device endpoints are configured and the CDC management task started. */ -void EVENT_USB_ConfigurationChanged(void) +void EVENT_USB_Device_ConfigurationChanged(void) { /* Indicate USB connected and ready */ LEDs_SetAllLEDs(LEDMASK_USB_READY); @@ -177,16 +187,17 @@ void EVENT_USB_ConfigurationChanged(void) { LEDs_SetAllLEDs(LEDMASK_USB_ERROR); } + + /* Reset line encoding baud rate so that the host knows to send new values */ + LineEncoding.BaudRateBPS = 0; } -/** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific +/** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific * control requests that are not handled internally by the USB library (including the CDC control commands, * which are all issued via the control endpoint), so that they can be handled appropriately for the application. */ -void EVENT_USB_UnhandledControlPacket(void) +void EVENT_USB_Device_UnhandledControlRequest(void) { - uint8_t* LineCodingData = (uint8_t*)&LineCoding; - /* Process CDC specific control requests */ switch (USB_ControlRequest.bRequest) { @@ -197,7 +208,7 @@ void EVENT_USB_UnhandledControlPacket(void) Endpoint_ClearSETUP(); /* Write the line coding data to the control endpoint */ - Endpoint_Write_Control_Stream_LE(LineCodingData, sizeof(CDC_Line_Coding_t)); + Endpoint_Write_Control_Stream_LE(&LineEncoding, sizeof(CDC_Line_Coding_t)); /* Finalize the stream transfer to send the last packet or clear the host abort */ Endpoint_ClearOUT(); @@ -211,7 +222,7 @@ void EVENT_USB_UnhandledControlPacket(void) Endpoint_ClearSETUP(); /* Read the line coding data in from the host into the global struct */ - Endpoint_Read_Control_Stream_LE(LineCodingData, sizeof(CDC_Line_Coding_t)); + Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_Line_Coding_t)); /* Finalize the stream transfer to clear the last packet from the host */ Endpoint_ClearIN(); @@ -229,9 +240,7 @@ void EVENT_USB_UnhandledControlPacket(void) CONTROL_LINE_OUT_* masks to determine the RTS and DTR line states using the following code: */ - /* Acknowledge status stage */ - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); + Endpoint_ClearStatusStage(); } break; @@ -244,18 +253,17 @@ void CDC_Task(void) char* ReportString = NULL; uint8_t JoyStatus_LCL = Joystick_GetStatus(); static bool ActionSent = false; - - char* JoystickStrings[] = - { - "Joystick Up\r\n", - "Joystick Down\r\n", - "Joystick Left\r\n", - "Joystick Right\r\n", - "Joystick Pressed\r\n", - }; + char* JoystickStrings[] = + { + "Joystick Up\r\n", + "Joystick Down\r\n", + "Joystick Left\r\n", + "Joystick Right\r\n", + "Joystick Pressed\r\n", + }; /* Device must be connected and configured for the task to run */ - if (!(USB_IsConnected) || !(USB_ConfigurationNumber)) + if (USB_DeviceState != DEVICE_STATE_Configured) return; #if 0 @@ -298,7 +306,7 @@ void CDC_Task(void) { ActionSent = false; } - else if (ActionSent == false) + else if ((ActionSent == false) && LineEncoding.BaudRateBPS) { ActionSent = true; @@ -319,7 +327,11 @@ void CDC_Task(void) if (IsFull) { /* Wait until the endpoint is ready for another packet */ - while (!(Endpoint_IsINReady())); + while (!(Endpoint_IsINReady())) + { + if (USB_DeviceState == DEVICE_STATE_Unattached) + return; + } /* Send an empty packet to ensure that the host does not buffer data sent to it */ Endpoint_ClearIN();