X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/ce3ea6fb2513d3aa14c354e2ed78c7eb4a38ee0e..cf610c5c91c3eee02dec7ca848acc8da831ad98a:/Demos/Device/LowLevel/CDC/CDC.c diff --git a/Demos/Device/LowLevel/CDC/CDC.c b/Demos/Device/LowLevel/CDC/CDC.c index 8a992c767..444dc019d 100644 --- a/Demos/Device/LowLevel/CDC/CDC.c +++ b/Demos/Device/LowLevel/CDC/CDC.c @@ -36,7 +36,6 @@ #include "CDC.h" -/* Globals: */ /** Contains the current baud rate and other settings of the virtual serial port. While this demo does not use * the physical USART and thus does not use these settings, they must still be retained and returned to the host * upon request or the host will assume the device is non-functional. @@ -45,10 +44,11 @@ * 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 @@ -59,11 +59,11 @@ static int CDC_putchar(char c, FILE *stream) { Endpoint_SelectEndpoint(CDC_TX_EPNUM); - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState != DEVICE_STATE_Configured) - return -1; - } + if (!(LineEncoding.BaudRateBPS)) + return -1; + + if (Endpoint_WaitUntilReady()) + return -1; Endpoint_Write_Byte(c); Endpoint_ClearIN(); @@ -74,16 +74,16 @@ static int CDC_putchar(char c, FILE *stream) static int CDC_getchar(FILE *stream) { int c; - + + if (!(LineEncoding.BaudRateBPS)) + return -1; + Endpoint_SelectEndpoint(CDC_RX_EPNUM); for (;;) { - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState != DEVICE_STATE_Configured) - return -1; - } + if (Endpoint_WaitUntilReady()) + return -1; if (!(Endpoint_BytesInEndpoint())) { @@ -137,7 +137,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); @@ -146,7 +146,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); @@ -155,7 +155,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); @@ -181,16 +181,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) { @@ -201,7 +202,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(); @@ -215,7 +216,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(); @@ -246,14 +247,6 @@ 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", - }; /* Device must be connected and configured for the task to run */ if (USB_DeviceState != DEVICE_STATE_Configured) @@ -284,22 +277,20 @@ void CDC_Task(void) /* Determine if a joystick action has occurred */ if (JoyStatus_LCL & JOY_UP) - ReportString = JoystickStrings[0]; + ReportString = "Joystick Up\r\n"; else if (JoyStatus_LCL & JOY_DOWN) - ReportString = JoystickStrings[1]; + ReportString = "Joystick Down\r\n"; else if (JoyStatus_LCL & JOY_LEFT) - ReportString = JoystickStrings[2]; + ReportString = "Joystick Left\r\n"; else if (JoyStatus_LCL & JOY_RIGHT) - ReportString = JoystickStrings[3]; + ReportString = "Joystick Right\r\n"; else if (JoyStatus_LCL & JOY_PRESS) - ReportString = JoystickStrings[4]; + ReportString = "Joystick Pressed\r\n"; + else + ActionSent = false; /* Flag management - Only allow one string to be sent per action */ - if (ReportString == NULL) - { - ActionSent = false; - } - else if (ActionSent == false) + if ((ReportString != NULL) && (ActionSent == false) && LineEncoding.BaudRateBPS) { ActionSent = true; @@ -320,11 +311,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();