diff options
Diffstat (limited to 'LUFA/Drivers/USB/Class/Host')
-rw-r--r-- | LUFA/Drivers/USB/Class/Host/CDC.c | 38 | ||||
-rw-r--r-- | LUFA/Drivers/USB/Class/Host/CDC.h | 13 |
2 files changed, 33 insertions, 18 deletions
diff --git a/LUFA/Drivers/USB/Class/Host/CDC.c b/LUFA/Drivers/USB/Class/Host/CDC.c index cf344beae2..5fb232c099 100644 --- a/LUFA/Drivers/USB/Class/Host/CDC.c +++ b/LUFA/Drivers/USB/Class/Host/CDC.c @@ -220,14 +220,19 @@ void CDC_Host_USBTask(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo) sizeof(CDCInterfaceInfo->State.ControlLineStates.DeviceToHost), NO_STREAM_CALLBACK); - } - - Pipe_ClearIN(); + Pipe_ClearIN(); - EVENT_CDC_Host_ControLineStateChanged(CDCInterfaceInfo); + EVENT_CDC_Host_ControLineStateChanged(CDCInterfaceInfo); + } + else + { + Pipe_ClearIN(); + } } Pipe_Freeze(); + + CDC_Host_Flush(CDCInterfaceInfo); } uint8_t CDC_Host_SetLineEncoding(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo) @@ -353,9 +358,9 @@ uint16_t CDC_Host_BytesReceived(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo } } -uint8_t CDC_Host_ReceiveByte(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo) +int16_t CDC_Host_ReceiveByte(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo) { - uint8_t ReceivedByte = 0; + uint8_t ReceivedByte = -1; if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive)) return 0; @@ -364,11 +369,14 @@ uint8_t CDC_Host_ReceiveByte(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo) Pipe_SetPipeToken(PIPE_TOKEN_IN); Pipe_Unfreeze(); - ReceivedByte = Pipe_Read_Byte(); - + if (!(Pipe_IsINReceived())) + return -1; + else if (Pipe_BytesInPipe()) + ReceivedByte = Pipe_Read_Byte(); + if (!(Pipe_BytesInPipe())) Pipe_ClearIN(); - + Pipe_Freeze(); return ReceivedByte; @@ -426,15 +434,19 @@ static int CDC_Host_putchar(char c, static int CDC_Host_getchar(FILE* Stream) { - if (!(CDC_Host_BytesReceived((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream)))) + int16_t ReceivedByte = CDC_Host_ReceiveByte((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream)); + + if (ReceivedByte < 0) return _FDEV_EOF; - return CDC_Host_ReceiveByte((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream)); + return ReceivedByte; } static int CDC_Host_getchar_Blocking(FILE* Stream) { - while (!(CDC_Host_BytesReceived((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream)))) + int16_t ReceivedByte; + + while ((ReceivedByte = CDC_Host_ReceiveByte((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream))) < 0) { if (USB_HostState == HOST_STATE_Unattached) return _FDEV_EOF; @@ -443,7 +455,7 @@ static int CDC_Host_getchar_Blocking(FILE* Stream) USB_USBTask(); } - return CDC_Host_ReceiveByte((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream)); + return ReceivedByte; } void CDC_Host_Event_Stub(void) diff --git a/LUFA/Drivers/USB/Class/Host/CDC.h b/LUFA/Drivers/USB/Class/Host/CDC.h index 640242819f..56bcf31477 100644 --- a/LUFA/Drivers/USB/Class/Host/CDC.h +++ b/LUFA/Drivers/USB/Class/Host/CDC.h @@ -236,7 +236,10 @@ uint8_t CDC_Host_SendByte(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo, const uint8_t Data) ATTR_NON_NULL_PTR_ARG(1); - /** Determines the number of bytes received by the CDC interface from the device, waiting to be read. + /** Determines the number of bytes received by the CDC interface from the device, waiting to be read. This indicates the number + * of bytes in the IN pipe bank only, and thus the number of calls to \ref CDC_Host_ReceiveByte() which are guaranteed to succeed + * immediately. If multiple bytes are to be received, they should be buffered by the user application, as the pipe bank will not be + * released back to the USB controller until all bytes are read. * * \pre This function must only be called when the Host state machine is in the HOST_STATE_Configured state or the * call will fail. @@ -248,17 +251,17 @@ uint16_t CDC_Host_BytesReceived(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1); /** Reads a byte of data from the device. If no data is waiting to be read of if a USB device is not connected, the function - * returns 0. The \ref CDC_Host_BytesReceived() function should be queried before data is received to ensure that no data - * underflow occurs. + * returns a negative value. The \ref CDC_Host_BytesReceived() function may be queried in advance to determine how many bytes + * are currently buffered in the CDC interface's data receive pipe. * * \pre This function must only be called when the Host state machine is in the HOST_STATE_Configured state or the * call will fail. * * \param[in,out] CDCInterfaceInfo Pointer to a structure containing a CDC Class host configuration and state. * - * \return Next received byte from the device, or 0 if no data received. + * \return Next received byte from the device, or a negative value if no data received. */ - uint8_t CDC_Host_ReceiveByte(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1); + int16_t CDC_Host_ReceiveByte(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1); /** Flushes any data waiting to be sent, ensuring that the send buffer is cleared. * |