X-Git-Url: http://git.linex4red.de/pub/lufa.git/blobdiff_plain/ed031c1df2f5b053b9cd9f48c63e66a42b7c049e..619b0b7b6b44e4422ea9aeb0cde41343bb5dda70:/Demos/Device/USBtoSerial/USBtoSerial.c?ds=inline diff --git a/Demos/Device/USBtoSerial/USBtoSerial.c b/Demos/Device/USBtoSerial/USBtoSerial.c index c7c9e4b04..1d238aa11 100644 --- a/Demos/Device/USBtoSerial/USBtoSerial.c +++ b/Demos/Device/USBtoSerial/USBtoSerial.c @@ -30,12 +30,6 @@ #include "USBtoSerial.h" -/* Project Tags, for reading out using the ButtLoad project */ -BUTTLOADTAG(ProjName, "LUFA USB RS232 App"); -BUTTLOADTAG(BuildTime, __TIME__); -BUTTLOADTAG(BuildDate, __DATE__); -BUTTLOADTAG(LUFAVersion, "LUFA V" LUFA_VERSION_STRING); - /* Scheduler Task List */ TASK_LIST { @@ -79,7 +73,7 @@ int main(void) LEDs_Init(); ReconfigureUSART(); - /* Ringbuffer Initialization */ + /* Ring buffer Initialization */ Buffer_Initialize(&Rx_Buffer); Buffer_Initialize(&Tx_Buffer); @@ -165,13 +159,13 @@ EVENT_HANDLER(USB_UnhandledControlPacket) if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) { /* Acknowledge the SETUP packet, ready for data transfer */ - Endpoint_ClearSetupReceived(); + Endpoint_ClearControlSETUP(); /* Write the line coding data to the control endpoint */ Endpoint_Write_Control_Stream_LE(LineCodingData, sizeof(LineCoding)); /* Finalize the stream transfer to send the last packet or clear the host abort */ - Endpoint_ClearSetupOUT(); + Endpoint_ClearControlOUT(); } break; @@ -179,13 +173,13 @@ EVENT_HANDLER(USB_UnhandledControlPacket) if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) { /* Acknowledge the SETUP packet, ready for data transfer */ - Endpoint_ClearSetupReceived(); + Endpoint_ClearControlSETUP(); /* Read the line coding data in from the host into the global struct */ Endpoint_Read_Control_Stream_LE(LineCodingData, sizeof(LineCoding)); /* Finalize the stream transfer to clear the last packet from the host */ - Endpoint_ClearSetupIN(); + Endpoint_ClearControlIN(); /* Reconfigure the USART with the new settings */ ReconfigureUSART(); @@ -207,11 +201,11 @@ EVENT_HANDLER(USB_UnhandledControlPacket) #endif /* Acknowledge the SETUP packet, ready for data transfer */ - Endpoint_ClearSetupReceived(); + Endpoint_ClearControlSETUP(); /* Acknowledge status stage */ - while (!(Endpoint_IsSetupINReady())); - Endpoint_ClearSetupIN(); + while (!(Endpoint_IsINReady())); + Endpoint_ClearControlIN(); } break; @@ -244,26 +238,27 @@ TASK(CDC_Task) Endpoint_SelectEndpoint(CDC_NOTIFICATION_EPNUM); Endpoint_Write_Stream_LE(&Notification, sizeof(Notification)); Endpoint_Write_Stream_LE(&LineStateMask, sizeof(LineStateMask)); - Endpoint_ClearCurrentBank(); + Endpoint_ClearIN(); #endif /* Select the Serial Rx Endpoint */ Endpoint_SelectEndpoint(CDC_RX_EPNUM); - if (Endpoint_ReadWriteAllowed()) + if (Endpoint_IsOUTReceived()) { - /* Read the received data endpoint into the transmission buffer */ - while (Endpoint_BytesInEndpoint()) + /* Read the bytes in from the endpoint into the buffer while space is available */ + while (Endpoint_BytesInEndpoint() && (BUFF_STATICSIZE - Rx_Buffer.Elements)) { - /* Wait until the buffer has space for a new character */ - while (!((BUFF_STATICSIZE - Rx_Buffer.Elements))); - /* Store each character from the endpoint */ Buffer_StoreElement(&Rx_Buffer, Endpoint_Read_Byte()); } - /* Clear the endpoint buffer */ - Endpoint_ClearCurrentBank(); + /* 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 */ @@ -284,26 +279,30 @@ TASK(CDC_Task) if (Tx_Buffer.Elements) { /* Wait until Serial Tx Endpoint Ready for Read/Write */ - while (!(Endpoint_ReadWriteAllowed())); - - /* Check before sending the data if the endpoint is completely full */ - bool IsFull = (Endpoint_BytesInEndpoint() == CDC_TXRX_EPSIZE); + while (!(Endpoint_IsReadWriteAllowed())); - /* Write the transmission buffer contents to the received data endpoint */ + /* Write the bytes from the buffer to the endpoint while space is available */ while (Tx_Buffer.Elements && (Endpoint_BytesInEndpoint() < CDC_TXRX_EPSIZE)) - Endpoint_Write_Byte(Buffer_GetElement(&Tx_Buffer)); + { + /* 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_ClearCurrentBank(); + Endpoint_ClearIN(); - /* If a full endpoint was sent, we need to send an empty packet afterwards to terminate the transfer */ - if (IsFull) + /* 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_ReadWriteAllowed())); + while (!(Endpoint_IsReadWriteAllowed())); /* Send an empty packet to terminate the transfer */ - Endpoint_ClearCurrentBank(); + Endpoint_ClearIN(); } } }