X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/e071f3897a0946c6be1e1b5e1f78eda8dcbf6fc7..f9f1bcc25c17b031029200cd9648d76d4ee39dc4:/LUFA/Drivers/USB/Class/Device/CDC.c?ds=sidebyside diff --git a/LUFA/Drivers/USB/Class/Device/CDC.c b/LUFA/Drivers/USB/Class/Device/CDC.c index 237e1dd2f..31fd8f1b0 100644 --- a/LUFA/Drivers/USB/Class/Device/CDC.c +++ b/LUFA/Drivers/USB/Class/Device/CDC.c @@ -39,7 +39,7 @@ void CDC_Device_Event_Stub(void) } -void CDC_Device_ProcessControlPacket(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo) +void CDC_Device_ProcessControlRequest(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo) { if (!(Endpoint_IsSETUPReceived())) return; @@ -87,6 +87,8 @@ void CDC_Device_ProcessControlPacket(USB_ClassInfo_CDC_Device_t* CDCInterfaceInf bool CDC_Device_ConfigureEndpoints(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo) { + memset(&CDCInterfaceInfo->State, 0x00, sizeof(CDCInterfaceInfo->State)); + if (!(Endpoint_ConfigureEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber, EP_TYPE_BULK, ENDPOINT_DIR_IN, CDCInterfaceInfo->Config.DataINEndpointSize, ENDPOINT_BANK_SINGLE))) @@ -113,68 +115,79 @@ bool CDC_Device_ConfigureEndpoints(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo) void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo) { - if (USB_DeviceState != DEVICE_STATE_Configured) + if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) return; - Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber); - - if (!(Endpoint_BytesInEndpoint())) - return; - - if (!(Endpoint_IsReadWriteAllowed())) - { - Endpoint_ClearIN(); + CDC_Device_Flush(CDCInterfaceInfo); +} - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } - } +uint8_t CDC_Device_SendString(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, char* const Data, const uint16_t Length) +{ + if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) + return ENDPOINT_READYWAIT_NoError; - Endpoint_ClearIN(); + Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber); + return Endpoint_Write_Stream_LE(Data, Length, NO_STREAM_CALLBACK); } -void CDC_Device_SendString(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, char* const Data, const uint16_t Length) +uint8_t CDC_Device_SendByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, const uint8_t Data) { - if (USB_DeviceState != DEVICE_STATE_Configured) - return; + if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) + return ENDPOINT_READYWAIT_NoError; Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber); - Endpoint_Write_Stream_LE(Data, Length, NO_STREAM_CALLBACK); + + if (!(Endpoint_IsReadWriteAllowed())) + { + uint8_t ErrorCode; + + Endpoint_ClearIN(); + + if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError) + return ErrorCode; + } + + Endpoint_Write_Byte(Data); + return ENDPOINT_READYWAIT_NoError; } -void CDC_Device_SendByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, const uint8_t Data) +uint8_t CDC_Device_Flush(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) { - if (USB_DeviceState != DEVICE_STATE_Configured) - return; + if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) + return ENDPOINT_READYWAIT_NoError; Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber); - - if (!(Endpoint_IsReadWriteAllowed())) + + if (Endpoint_BytesInEndpoint()) { + uint8_t ErrorCode; + Endpoint_ClearIN(); - while (!(Endpoint_IsReadWriteAllowed())) - { - if (USB_DeviceState == DEVICE_STATE_Unattached) - return; - } + if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError) + return ErrorCode; } - - Endpoint_Write_Byte(Data); + + Endpoint_ClearIN(); + return Endpoint_WaitUntilReady(); } uint16_t CDC_Device_BytesReceived(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) { + if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) + return 0; + Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataOUTEndpointNumber); + if (Endpoint_IsOUTReceived() && !(Endpoint_BytesInEndpoint())) + Endpoint_ClearOUT(); + return Endpoint_BytesInEndpoint(); } uint8_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo) { - if (USB_DeviceState != DEVICE_STATE_Configured) + if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) return 0; Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataOUTEndpointNumber); @@ -189,7 +202,7 @@ uint8_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo) void CDC_Device_SendControlLineStateChange(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) { - if (USB_DeviceState != DEVICE_STATE_Configured) + if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) return; Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.NotificationEndpointNumber); @@ -200,11 +213,13 @@ void CDC_Device_SendControlLineStateChange(USB_ClassInfo_CDC_Device_t* const CDC .bRequest = NOTIF_SerialState, .wValue = 0, .wIndex = 0, - .wLength = sizeof(uint16_t), + .wLength = sizeof(CDCInterfaceInfo->State.ControlLineStates.DeviceToHost), }; Endpoint_Write_Stream_LE(&Notification, sizeof(Notification), NO_STREAM_CALLBACK); - Endpoint_Write_Stream_LE(&CDCInterfaceInfo->State.ControlLineStates.DeviceToHost, sizeof(uint8_t), NO_STREAM_CALLBACK); + Endpoint_Write_Stream_LE(&CDCInterfaceInfo->State.ControlLineStates.DeviceToHost, + sizeof(CDCInterfaceInfo->State.ControlLineStates.DeviceToHost), + NO_STREAM_CALLBACK); Endpoint_ClearIN(); }