CDC Read&Write
CDC Read&Write
/* Function:
USB_DEVICE_CDC_RESULT USB_DEVICE_CDC_Read
(
USB_DEVICE_CDC_INDEX instance,
USB_CDC_DEVICE_TRANSFER_HANDLE * transferHandle,
void * data,
size_t size
);
Summary:
This function requests a data read from the USB Device CDC Function Driver
Layer.
Description:
This function requests a data read from the USB Device CDC Function Driver
Layer. The function places a requests with driver, the request will get
serviced as data is made available by the USB Host. A handle to the request
is returned in the transferHandle parameter. The termination of the request
is indicated by the USB_DEVICE_CDC_EVENT_READ_COMPLETE event. The amount of
data read and the transfer handle associated with the request is returned
along with the event in the pData parameter of the event handler. The
transfer handle expires when event handler for the
USB_DEVICE_CDC_EVENT_READ_COMPLETE exits. If the read request could not be
accepted, the function returns an error code and transferHandle will contain
the value USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID.
Remarks:
Refer to usb_device_cdc.h for usage information.
*/
USB_DEVICE_CDC_RESULT USB_DEVICE_CDC_Read
(
USB_DEVICE_CDC_INDEX iCDC ,
USB_DEVICE_CDC_TRANSFER_HANDLE * transferHandle ,
void * data , size_t size
)
{
unsigned int cnt;
unsigned int remainder;
USB_DEVICE_IRP * irp;
USB_DEVICE_CDC_ENDPOINT * endpoint;
USB_DEVICE_CDC_INSTANCE * thisCDCDevice;
OSAL_RESULT osalError;
USB_ERROR irpError;
OSAL_CRITSECT_DATA_TYPE IntState;
thisCDCDevice = &gUSBDeviceCDCInstance[iCDC];
endpoint = &thisCDCDevice->dataInterface.endpoint[USB_DEVICE_CDC_ENDPOINT_RX];
*transferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID;
/* Make sure that we are with in the queue size for this instance */
if(thisCDCDevice->currentQSizeRead >= thisCDCDevice->queueSizeRead)
{
SYS_ASSERT(false, "Read Queue is full");
return(USB_DEVICE_CDC_RESULT_ERROR_TRANSFER_QUEUE_FULL);
}
irp = &gUSBDeviceCDCIRP[cnt];
irp->data = data;
irp->size = size;
irp->userData = (uintptr_t) iCDC;
irp->callback = _USB_DEVICE_CDC_ReadIRPCallback;
*transferHandle = (USB_DEVICE_CDC_TRANSFER_HANDLE)irp;
irpError = USB_DEVICE_IRPSubmit(thisCDCDevice->deviceHandle,
endpoint->address, irp);
return((USB_DEVICE_CDC_RESULT)irpError);
}
}
// *****************************************************************************
/* Function:
USB_DEVICE_CDC_RESULT USB_DEVICE_CDC_Write
(
USB_DEVICE_CDC_INDEX instance,
USB_CDC_DEVICE_TRANSFER_HANDLE * transferHandle,
const void * data,
size_t size,
USB_DEVICE_CDC_TRANSFER_FLAGS flags
);
Summary:
This function requests a data write to the USB Device CDC Function Driver
Layer.
Description:
This function requests a data write to the USB Device CDC Function Driver
Layer. The function places a requests with driver, the request will get
serviced as data is requested by the USB Host. A handle to the request is
returned in the transferHandle parameter. The termination of the request is
indicated by the USB_DEVICE_CDC_EVENT_WRITE_COMPLETE event. The amount of
data written and the transfer handle associated with the request is returned
along with the event in writeCompleteData member of the pData parameter in
the event handler. The transfer handle expires when event handler for the
USB_DEVICE_CDC_EVENT_WRITE_COMPLETE exits. If the read request could not be
accepted, the function returns an error code and transferHandle will contain
the value USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID.
Remarks:
Refer to usb_device_cdc.h for usage information.
*/
USB_DEVICE_CDC_RESULT USB_DEVICE_CDC_Write
(
USB_DEVICE_CDC_INDEX iCDC ,
USB_DEVICE_CDC_TRANSFER_HANDLE * transferHandle ,
const void * data , size_t size ,
USB_DEVICE_CDC_TRANSFER_FLAGS flags
)
{
unsigned int cnt;
unsigned int remainder;
USB_DEVICE_IRP * irp;
USB_DEVICE_IRP_FLAG irpFlag = USB_DEVICE_IRP_FLAG_NONE;
USB_DEVICE_CDC_INSTANCE * thisCDCDevice;
USB_DEVICE_CDC_ENDPOINT * endpoint;
OSAL_RESULT osalError;
USB_ERROR irpError;
OSAL_CRITSECT_DATA_TYPE IntState;
* transferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID;
thisCDCDevice = &gUSBDeviceCDCInstance[iCDC];
endpoint = &thisCDCDevice->dataInterface.endpoint[USB_DEVICE_CDC_ENDPOINT_TX];
if(!(endpoint->isConfigured))
{
/* This means that the endpoint is not configured yet */
SYS_ASSERT(false, "Endpoint not configured");
return (USB_DEVICE_CDC_RESULT_ERROR_INSTANCE_NOT_CONFIGURED);
}
if(size == 0)
{
/* Size cannot be zero */
return (USB_DEVICE_CDC_RESULT_ERROR_TRANSFER_SIZE_INVALID);
}
return(USB_DEVICE_CDC_RESULT_ERROR_TRANSFER_SIZE_INVALID);
}
if(remainder != 0)
{
size -= remainder;
}
irpFlag = USB_DEVICE_IRP_FLAG_DATA_PENDING;
}
else if(flags & USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE)
{
irpFlag = USB_DEVICE_IRP_FLAG_DATA_COMPLETE;
}
irp = &gUSBDeviceCDCIRP[cnt];
irp->data = (void *)data;
irp->size = size;
*transferHandle = (USB_DEVICE_CDC_TRANSFER_HANDLE)irp;
irpError = USB_DEVICE_IRPSubmit(thisCDCDevice->deviceHandle,
endpoint->address, irp);
return((USB_DEVICE_CDC_RESULT)irpError);
}
}