diff options
author | Dean Camera <dean@fourwalledcubicle.com> | 2010-07-30 03:52:11 +0000 |
---|---|---|
committer | Dean Camera <dean@fourwalledcubicle.com> | 2010-07-30 03:52:11 +0000 |
commit | 24e621a8d8424721ed21ce329d2a28dbf8c35343 (patch) | |
tree | a014f41c82dfbb50706e1a911a5da2414b9766fd /LUFA/Drivers/USB/Class/Device | |
parent | 7c8d16fd7adae430f94004048a7702b7338f5eb1 (diff) | |
download | lufa-24e621a8d8424721ed21ce329d2a28dbf8c35343.tar.gz lufa-24e621a8d8424721ed21ce329d2a28dbf8c35343.tar.bz2 lufa-24e621a8d8424721ed21ce329d2a28dbf8c35343.zip |
Altered the CDC Deivice and Host Class drivers' receive byte routines, so that no data is indicated by a negative return value.
Added auto flushing of OUT data to the CDC Host Class driver's USBTask function to automatically flush the send pipe buffer.
Diffstat (limited to 'LUFA/Drivers/USB/Class/Device')
-rw-r--r-- | LUFA/Drivers/USB/Class/Device/CDC.c | 32 | ||||
-rw-r--r-- | LUFA/Drivers/USB/Class/Device/CDC.h | 14 |
2 files changed, 27 insertions, 19 deletions
diff --git a/LUFA/Drivers/USB/Class/Device/CDC.c b/LUFA/Drivers/USB/Class/Device/CDC.c index 453d21d84..026ac269a 100644 --- a/LUFA/Drivers/USB/Class/Device/CDC.c +++ b/LUFA/Drivers/USB/Class/Device/CDC.c @@ -128,11 +128,6 @@ void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) { if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) return; - - Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataOUTEndpointNumber); - - if (Endpoint_IsOUTReceived() && !(Endpoint_BytesInEndpoint())) - Endpoint_ClearOUT(); CDC_Device_Flush(CDCInterfaceInfo); } @@ -222,19 +217,24 @@ uint16_t CDC_Device_BytesReceived(USB_ClassInfo_CDC_Device_t* const CDCInterface } } -uint8_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) +int16_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) { + uint8_t ReceivedByte = -1; + if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) return 0; Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataOUTEndpointNumber); - uint8_t DataByte = Endpoint_Read_Byte(); + if (!(Endpoint_IsOUTReceived())) + return -1; + else if (Endpoint_BytesInEndpoint()) + ReceivedByte = Endpoint_Read_Byte(); if (!(Endpoint_BytesInEndpoint())) Endpoint_ClearOUT(); - - return DataByte; + + return ReceivedByte; } void CDC_Device_SendControlLineStateChange(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) @@ -282,24 +282,28 @@ static int CDC_Device_putchar(char c, static int CDC_Device_getchar(FILE* Stream) { - if (!(CDC_Device_BytesReceived((USB_ClassInfo_CDC_Device_t*)fdev_get_udata(Stream)))) + int16_t ReceivedByte = CDC_Device_ReceiveByte((USB_ClassInfo_CDC_Device_t*)fdev_get_udata(Stream)); + + if (ReceivedByte < 0) return _FDEV_EOF; - return CDC_Device_ReceiveByte((USB_ClassInfo_CDC_Device_t*)fdev_get_udata(Stream)); + return ReceivedByte; } static int CDC_Device_getchar_Blocking(FILE* Stream) { - while (!(CDC_Device_BytesReceived((USB_ClassInfo_CDC_Device_t*)fdev_get_udata(Stream)))) + int16_t ReceivedByte; + + while ((ReceivedByte = CDC_Device_ReceiveByte((USB_ClassInfo_CDC_Device_t*)fdev_get_udata(Stream))) < 0) { if (USB_DeviceState == DEVICE_STATE_Unattached) return _FDEV_EOF; CDC_Device_USBTask((USB_ClassInfo_CDC_Device_t*)fdev_get_udata(Stream)); - USB_USBTask(); + USB_USBTask(); } - return CDC_Device_ReceiveByte((USB_ClassInfo_CDC_Device_t*)fdev_get_udata(Stream)); + return ReceivedByte; } #endif diff --git a/LUFA/Drivers/USB/Class/Device/CDC.h b/LUFA/Drivers/USB/Class/Device/CDC.h index 9e92d5f6c..3ada8f7af 100644 --- a/LUFA/Drivers/USB/Class/Device/CDC.h +++ b/LUFA/Drivers/USB/Class/Device/CDC.h @@ -233,7 +233,10 @@ uint8_t CDC_Device_SendByte(USB_ClassInfo_CDC_Device_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 host, waiting to be read. + /** Determines the number of bytes received by the CDC interface from the host, waiting to be read. This indicates the number + * of bytes in the OUT endpoint bank only, and thus the number of calls to \ref CDC_Device_ReceiveByte() which are guaranteed to + * succeed immediately. If multiple bytes are to be received, they should be buffered by the user application, as the endpoint + * bank will not be released back to the USB controller until all bytes are read. * * \pre This function must only be called when the Device state machine is in the DEVICE_STATE_Configured state or * the call will fail. @@ -245,17 +248,18 @@ uint16_t CDC_Device_BytesReceived(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1); /** Reads a byte of data from the host. If no data is waiting to be read of if a USB host is not connected, the function - * returns 0. The \ref CDC_Device_BytesReceived() function should be queried before data is received to ensure that no data - * underflow occurs. + * returns a negative value. The \ref CDC_Device_BytesReceived() function may be queried in advance to determine how many + * bytes are currently buffered in the CDC interface's data receive endpoint bank, and thus how many repeated calls to this + * function which are guaranteed to succeed. * * \pre This function must only be called when the Device state machine is in the DEVICE_STATE_Configured state or * the call will fail. * * \param[in,out] CDCInterfaceInfo Pointer to a structure containing a CDC Class configuration and state. * - * \return Next received byte from the host, or 0 if no data received. + * \return Next received byte from the host, or a negative value if no data received. */ - uint8_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1); + int16_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1); /** Flushes any data waiting to be sent, ensuring that the send buffer is cleared. * |