X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/9798440ca4f694e7cd8312a51b82e59589f1ebeb..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 0dc830b5c..31fd8f1b0 100644 --- a/LUFA/Drivers/USB/Class/Device/CDC.c +++ b/LUFA/Drivers/USB/Class/Device/CDC.c @@ -28,7 +28,10 @@ this software. */ -#define INCLUDE_FROM_CDC_CLASS_C +#include "../../HighLevel/USBMode.h" +#if defined(USB_CAN_BE_DEVICE) + +#define INCLUDE_FROM_CDC_CLASS_DEVICE_C #include "CDC.h" void CDC_Device_Event_Stub(void) @@ -36,12 +39,12 @@ void CDC_Device_Event_Stub(void) } -void CDC_Device_ProcessControlPacket(USB_ClassInfo_CDC_t* CDCInterfaceInfo) +void CDC_Device_ProcessControlRequest(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo) { if (!(Endpoint_IsSETUPReceived())) return; - if (USB_ControlRequest.wIndex != CDCInterfaceInfo->ControlInterfaceNumber) + if (USB_ControlRequest.wIndex != CDCInterfaceInfo->Config.ControlInterfaceNumber) return; switch (USB_ControlRequest.bRequest) @@ -50,7 +53,7 @@ void CDC_Device_ProcessControlPacket(USB_ClassInfo_CDC_t* CDCInterfaceInfo) if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) { Endpoint_ClearSETUP(); - Endpoint_Write_Control_Stream_LE(&CDCInterfaceInfo->LineEncoding, sizeof(CDCInterfaceInfo->LineEncoding)); + Endpoint_Write_Control_Stream_LE(&CDCInterfaceInfo->State.LineEncoding, sizeof(CDCInterfaceInfo->State.LineEncoding)); Endpoint_ClearOUT(); } @@ -59,7 +62,7 @@ void CDC_Device_ProcessControlPacket(USB_ClassInfo_CDC_t* CDCInterfaceInfo) if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) { Endpoint_ClearSETUP(); - Endpoint_Read_Control_Stream_LE(&CDCInterfaceInfo->LineEncoding, sizeof(CDCInterfaceInfo->LineEncoding)); + Endpoint_Read_Control_Stream_LE(&CDCInterfaceInfo->State.LineEncoding, sizeof(CDCInterfaceInfo->State.LineEncoding)); Endpoint_ClearIN(); EVENT_CDC_Device_LineEncodingChanged(CDCInterfaceInfo); @@ -71,36 +74,37 @@ void CDC_Device_ProcessControlPacket(USB_ClassInfo_CDC_t* CDCInterfaceInfo) { Endpoint_ClearSETUP(); - CDCInterfaceInfo->ControlLineState = USB_ControlRequest.wValue; + CDCInterfaceInfo->State.ControlLineStates.HostToDevice = USB_ControlRequest.wValue; EVENT_CDC_Device_ControLineStateChanged(CDCInterfaceInfo); - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); + Endpoint_ClearStatusStage(); } break; } } -bool CDC_Device_ConfigureEndpoints(USB_ClassInfo_CDC_t* CDCInterfaceInfo) +bool CDC_Device_ConfigureEndpoints(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo) { - if (!(Endpoint_ConfigureEndpoint(CDCInterfaceInfo->DataINEndpointNumber, EP_TYPE_BULK, - ENDPOINT_DIR_IN, CDCInterfaceInfo->DataINEndpointSize, + 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))) { return false; } - if (!(Endpoint_ConfigureEndpoint(CDCInterfaceInfo->DataOUTEndpointNumber, EP_TYPE_BULK, - ENDPOINT_DIR_OUT, CDCInterfaceInfo->DataOUTEndpointSize, + if (!(Endpoint_ConfigureEndpoint(CDCInterfaceInfo->Config.DataOUTEndpointNumber, EP_TYPE_BULK, + ENDPOINT_DIR_OUT, CDCInterfaceInfo->Config.DataOUTEndpointSize, ENDPOINT_BANK_SINGLE))) { return false; } - if (!(Endpoint_ConfigureEndpoint(CDCInterfaceInfo->NotificationEndpointNumber, EP_TYPE_INTERRUPT, - ENDPOINT_DIR_IN, CDCInterfaceInfo->NotificationEndpointSize, + if (!(Endpoint_ConfigureEndpoint(CDCInterfaceInfo->Config.NotificationEndpointNumber, EP_TYPE_INTERRUPT, + ENDPOINT_DIR_IN, CDCInterfaceInfo->Config.NotificationEndpointSize, ENDPOINT_BANK_SINGLE))) { return false; @@ -109,63 +113,84 @@ bool CDC_Device_ConfigureEndpoints(USB_ClassInfo_CDC_t* CDCInterfaceInfo) return true; } -void CDC_Device_USBTask(USB_ClassInfo_CDC_t* CDCInterfaceInfo) +void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo) { - if (!(USB_IsConnected)) + if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) return; - Endpoint_SelectEndpoint(CDCInterfaceInfo->DataINEndpointNumber); - - if (!(Endpoint_BytesInEndpoint())) - return; - - if (!(Endpoint_IsReadWriteAllowed())) - { - Endpoint_ClearIN(); - while (!(Endpoint_IsReadWriteAllowed())); - } - - Endpoint_ClearIN(); + CDC_Device_Flush(CDCInterfaceInfo); } -void CDC_Device_SendString(USB_ClassInfo_CDC_t* CDCInterfaceInfo, char* Data, uint16_t Length) +uint8_t CDC_Device_SendString(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, char* const Data, const uint16_t Length) { - if (!(USB_IsConnected)) - return; - - Endpoint_SelectEndpoint(CDCInterfaceInfo->DataINEndpointNumber); - Endpoint_Write_Stream_LE(Data, Length, NO_STREAM_CALLBACK); + if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) + return ENDPOINT_READYWAIT_NoError; + + Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber); + return Endpoint_Write_Stream_LE(Data, Length, NO_STREAM_CALLBACK); } -void CDC_Device_SendByte(USB_ClassInfo_CDC_t* CDCInterfaceInfo, uint8_t Data) +uint8_t CDC_Device_SendByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, const uint8_t Data) { - if (!(USB_IsConnected)) - return; + if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) + return ENDPOINT_READYWAIT_NoError; - Endpoint_SelectEndpoint(CDCInterfaceInfo->DataINEndpointNumber); + Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber); if (!(Endpoint_IsReadWriteAllowed())) { + uint8_t ErrorCode; + Endpoint_ClearIN(); - while (!(Endpoint_IsReadWriteAllowed())); + + if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError) + return ErrorCode; } - Endpoint_Write_Byte(Data); + Endpoint_Write_Byte(Data); + return ENDPOINT_READYWAIT_NoError; } -uint16_t CDC_Device_BytesReceived(USB_ClassInfo_CDC_t* CDCInterfaceInfo) +uint8_t CDC_Device_Flush(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) { - Endpoint_SelectEndpoint(CDCInterfaceInfo->DataOUTEndpointNumber); + if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) + return ENDPOINT_READYWAIT_NoError; + + Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber); + + if (Endpoint_BytesInEndpoint()) + { + uint8_t ErrorCode; + + Endpoint_ClearIN(); + + if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError) + return ErrorCode; + } + + 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_t* CDCInterfaceInfo) +uint8_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo) { - if (!(USB_IsConnected)) + if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) return 0; - Endpoint_SelectEndpoint(CDCInterfaceInfo->DataOUTEndpointNumber); + Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataOUTEndpointNumber); uint8_t DataByte = Endpoint_Read_Byte(); @@ -175,12 +200,12 @@ uint8_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_t* CDCInterfaceInfo) return DataByte; } -void CDC_Device_SendControlLineStateChange(USB_ClassInfo_CDC_t* CDCInterfaceInfo, uint16_t LineStateMask) +void CDC_Device_SendControlLineStateChange(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) { - if (!(USB_IsConnected)) + if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS)) return; - Endpoint_SelectEndpoint(CDCInterfaceInfo->NotificationEndpointNumber); + Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.NotificationEndpointNumber); USB_Request_Header_t Notification = (USB_Request_Header_t) { @@ -188,10 +213,14 @@ void CDC_Device_SendControlLineStateChange(USB_ClassInfo_CDC_t* CDCInterfaceInfo .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(&LineStateMask, sizeof(LineStateMask), NO_STREAM_CALLBACK); + Endpoint_Write_Stream_LE(&CDCInterfaceInfo->State.ControlLineStates.DeviceToHost, + sizeof(CDCInterfaceInfo->State.ControlLineStates.DeviceToHost), + NO_STREAM_CALLBACK); Endpoint_ClearIN(); } + +#endif