initial check in based on SVN revision 575
This commit is contained in:
878
usb/device/class/cdc/usb_device_cdc_acm.c
Normal file
878
usb/device/class/cdc/usb_device_cdc_acm.c
Normal file
@@ -0,0 +1,878 @@
|
||||
/*
|
||||
* Copyright (c) 2015, Freescale Semiconductor, Inc.
|
||||
* Copyright 2016, 2019 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "usb_device_config.h"
|
||||
#include "usb.h"
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usb_device_class.h"
|
||||
|
||||
#if USB_DEVICE_CONFIG_CDC_ACM
|
||||
#include "usb_device_cdc_acm.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
#define USB_CDC_ACM_ENTER_CRITICAL() \
|
||||
OSA_SR_ALLOC(); \
|
||||
OSA_ENTER_CRITICAL()
|
||||
|
||||
#define USB_CDC_ACM_EXIT_CRITICAL() OSA_EXIT_CRITICAL()
|
||||
|
||||
/*******************************************************************************
|
||||
* Variables
|
||||
******************************************************************************/
|
||||
/* CDC ACM device instance */
|
||||
|
||||
USB_GLOBAL USB_RAM_ADDRESS_ALIGNMENT(USB_DATA_ALIGN_SIZE) static usb_device_cdc_acm_struct_t
|
||||
g_cdcAcmHandle[USB_DEVICE_CONFIG_CDC_ACM];
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
******************************************************************************/
|
||||
|
||||
/*!
|
||||
* @brief Allocates the CDC ACM device handle.
|
||||
*
|
||||
* This function allocates the CDC ACM device handle.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCdcAcmAllocateHandle(usb_device_cdc_acm_struct_t **handle)
|
||||
{
|
||||
uint32_t count;
|
||||
for (count = 0; count < (uint32_t)USB_DEVICE_CONFIG_CDC_ACM; count++)
|
||||
{
|
||||
if (NULL == g_cdcAcmHandle[count].handle)
|
||||
{
|
||||
*handle = &g_cdcAcmHandle[count];
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
}
|
||||
|
||||
return kStatus_USB_Busy;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Frees the CDC ACM device handle.
|
||||
*
|
||||
* This function frees the CDC ACM device handle.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCdcAcmFreeHandle(usb_device_cdc_acm_struct_t *handle)
|
||||
{
|
||||
handle->handle = NULL;
|
||||
handle->configStruct = NULL;
|
||||
handle->configuration = 0;
|
||||
handle->alternate = 0;
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Responds to the interrupt in endpoint event.
|
||||
*
|
||||
* This function responds to the interrupt in endpoint event.
|
||||
*
|
||||
* @param handle The device handle of the CDC ACM device.
|
||||
* @param message The pointer to the message of the endpoint callback.
|
||||
* @param callbackParam The pointer to the parameter of the callback.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCdcAcmInterruptIn(usb_device_handle handle,
|
||||
usb_device_endpoint_callback_message_struct_t *message,
|
||||
void *callbackParam)
|
||||
{
|
||||
usb_device_cdc_acm_struct_t *cdcAcmHandle;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
cdcAcmHandle = (usb_device_cdc_acm_struct_t *)callbackParam;
|
||||
if (NULL == cdcAcmHandle)
|
||||
{
|
||||
return kStatus_USB_InvalidHandle;
|
||||
}
|
||||
|
||||
cdcAcmHandle->interruptIn.isBusy = 0U;
|
||||
|
||||
if ((NULL != cdcAcmHandle->configStruct) && (NULL != cdcAcmHandle->configStruct->classCallback))
|
||||
{
|
||||
/*classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback((class_handle_t)cdcAcmHandle,
|
||||
kUSB_DeviceCdcEventSerialStateNotif, message);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Responds to the bulk in endpoint event.
|
||||
*
|
||||
* This function responds to the bulk in endpoint event.
|
||||
*
|
||||
* @param handle The device handle of the CDC ACM device.
|
||||
* @param message The pointer to the message of the endpoint callback.
|
||||
* @param callbackParam The pointer to the parameter of the callback.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCdcAcmBulkIn(usb_device_handle handle,
|
||||
usb_device_endpoint_callback_message_struct_t *message,
|
||||
void *callbackParam)
|
||||
{
|
||||
usb_device_cdc_acm_struct_t *cdcAcmHandle;
|
||||
usb_status_t status = kStatus_USB_Error;
|
||||
cdcAcmHandle = (usb_device_cdc_acm_struct_t *)callbackParam;
|
||||
|
||||
if (NULL == cdcAcmHandle)
|
||||
{
|
||||
return kStatus_USB_InvalidHandle;
|
||||
}
|
||||
|
||||
cdcAcmHandle->bulkIn.isBusy = 0;
|
||||
|
||||
if ((NULL != cdcAcmHandle->configStruct) && (NULL != cdcAcmHandle->configStruct->classCallback))
|
||||
{
|
||||
/*classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
status = cdcAcmHandle->configStruct->classCallback((class_handle_t)cdcAcmHandle,
|
||||
kUSB_DeviceCdcEventSendResponse, message);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Responds to the bulk out endpoint event.
|
||||
*
|
||||
* This function responds to the bulk out endpoint event.
|
||||
*
|
||||
* @param handle The device handle of the CDC ACM device.
|
||||
* @param message The pointer to the message of the endpoint callback.
|
||||
* @param callbackParam The pointer to the parameter of the callback.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCdcAcmBulkOut(usb_device_handle handle,
|
||||
usb_device_endpoint_callback_message_struct_t *message,
|
||||
void *callbackParam)
|
||||
{
|
||||
usb_device_cdc_acm_struct_t *cdcAcmHandle;
|
||||
usb_status_t status = kStatus_USB_Error;
|
||||
cdcAcmHandle = (usb_device_cdc_acm_struct_t *)callbackParam;
|
||||
|
||||
if (NULL == cdcAcmHandle)
|
||||
{
|
||||
return kStatus_USB_InvalidHandle;
|
||||
}
|
||||
|
||||
cdcAcmHandle->bulkOut.isBusy = 0U;
|
||||
|
||||
if ((NULL != cdcAcmHandle->configStruct) && (NULL != cdcAcmHandle->configStruct->classCallback))
|
||||
{
|
||||
/*classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
status = cdcAcmHandle->configStruct->classCallback((class_handle_t)cdcAcmHandle,
|
||||
kUSB_DeviceCdcEventRecvResponse, message);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Initializes the endpoints in CDC ACM class.
|
||||
*
|
||||
* This function initializes the endpoints in CDC ACM class.
|
||||
*
|
||||
* @param cdcAcmHandle The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCdcAcmEndpointsInit(usb_device_cdc_acm_struct_t *cdcAcmHandle)
|
||||
{
|
||||
usb_device_interface_list_t *interfaceList;
|
||||
usb_device_interface_struct_t *interface = NULL;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
uint32_t count;
|
||||
uint32_t index;
|
||||
|
||||
if (NULL == cdcAcmHandle)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
/* return error when configuration is invalid (0 or more than the configuration number) */
|
||||
if ((cdcAcmHandle->configuration == 0U) ||
|
||||
(cdcAcmHandle->configuration > cdcAcmHandle->configStruct->classInfomation->configurations))
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
interfaceList = &cdcAcmHandle->configStruct->classInfomation->interfaceList[cdcAcmHandle->configuration - 1U];
|
||||
|
||||
for (count = 0; count < interfaceList->count; count++)
|
||||
{
|
||||
if (USB_DEVICE_CONFIG_CDC_COMM_CLASS_CODE == interfaceList->interfaces[count].classCode)
|
||||
{
|
||||
for (index = 0; index < interfaceList->interfaces[count].count; index++)
|
||||
{
|
||||
if (interfaceList->interfaces[count].interface[index].alternateSetting == cdcAcmHandle->alternate)
|
||||
{
|
||||
interface = &interfaceList->interfaces[count].interface[index];
|
||||
break;
|
||||
}
|
||||
}
|
||||
cdcAcmHandle->interfaceNumber = interfaceList->interfaces[count].interfaceNumber;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (NULL == interface)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
cdcAcmHandle->commInterfaceHandle = interface;
|
||||
for (count = 0; count < interface->endpointList.count; count++)
|
||||
{
|
||||
usb_device_endpoint_init_struct_t epInitStruct;
|
||||
usb_device_endpoint_callback_struct_t epCallback;
|
||||
epInitStruct.zlt = 0;
|
||||
epInitStruct.interval = interface->endpointList.endpoint[count].interval;
|
||||
epInitStruct.endpointAddress = interface->endpointList.endpoint[count].endpointAddress;
|
||||
epInitStruct.maxPacketSize = interface->endpointList.endpoint[count].maxPacketSize;
|
||||
epInitStruct.transferType = interface->endpointList.endpoint[count].transferType;
|
||||
|
||||
if ((USB_IN == ((epInitStruct.endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT)) &&
|
||||
(USB_ENDPOINT_INTERRUPT == epInitStruct.transferType))
|
||||
{
|
||||
cdcAcmHandle->interruptIn.ep = (epInitStruct.endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_NUMBER_MASK);
|
||||
cdcAcmHandle->interruptIn.isBusy = 0;
|
||||
cdcAcmHandle->interruptIn.pipeDataBuffer = (uint8_t *)USB_INVALID_TRANSFER_BUFFER;
|
||||
cdcAcmHandle->interruptIn.pipeStall = 0U;
|
||||
cdcAcmHandle->interruptIn.pipeDataLen = 0U;
|
||||
epCallback.callbackFn = USB_DeviceCdcAcmInterruptIn;
|
||||
}
|
||||
|
||||
epCallback.callbackParam = cdcAcmHandle;
|
||||
|
||||
error = USB_DeviceInitEndpoint(cdcAcmHandle->handle, &epInitStruct, &epCallback);
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
for (count = 0; count < interfaceList->count; count++)
|
||||
{
|
||||
if (USB_DEVICE_CONFIG_CDC_DATA_CLASS_CODE == interfaceList->interfaces[count].classCode)
|
||||
{
|
||||
for (index = 0; index < interfaceList->interfaces[count].count; index++)
|
||||
{
|
||||
if (interfaceList->interfaces[count].interface[index].alternateSetting == cdcAcmHandle->alternate)
|
||||
{
|
||||
interface = &interfaceList->interfaces[count].interface[index];
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cdcAcmHandle->dataInterfaceHandle = interface;
|
||||
|
||||
for (count = 0; count < interface->endpointList.count; count++)
|
||||
{
|
||||
usb_device_endpoint_init_struct_t epInitStruct;
|
||||
usb_device_endpoint_callback_struct_t epCallback;
|
||||
epInitStruct.zlt = 0;
|
||||
epInitStruct.interval = interface->endpointList.endpoint[count].interval;
|
||||
epInitStruct.endpointAddress = interface->endpointList.endpoint[count].endpointAddress;
|
||||
epInitStruct.maxPacketSize = interface->endpointList.endpoint[count].maxPacketSize;
|
||||
epInitStruct.transferType = interface->endpointList.endpoint[count].transferType;
|
||||
|
||||
if ((USB_IN == ((epInitStruct.endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT)) &&
|
||||
(USB_ENDPOINT_BULK == epInitStruct.transferType))
|
||||
{
|
||||
cdcAcmHandle->bulkIn.ep = (epInitStruct.endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_NUMBER_MASK);
|
||||
cdcAcmHandle->bulkIn.isBusy = 0;
|
||||
cdcAcmHandle->bulkIn.pipeDataBuffer = (uint8_t *)USB_INVALID_TRANSFER_BUFFER;
|
||||
cdcAcmHandle->bulkIn.pipeStall = 0U;
|
||||
cdcAcmHandle->bulkIn.pipeDataLen = 0U;
|
||||
epCallback.callbackFn = USB_DeviceCdcAcmBulkIn;
|
||||
}
|
||||
else if ((USB_OUT == ((epInitStruct.endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT)) &&
|
||||
(USB_ENDPOINT_BULK == epInitStruct.transferType))
|
||||
{
|
||||
cdcAcmHandle->bulkOut.ep = (epInitStruct.endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_NUMBER_MASK);
|
||||
cdcAcmHandle->bulkOut.isBusy = 0;
|
||||
cdcAcmHandle->bulkOut.pipeDataBuffer = (uint8_t *)USB_INVALID_TRANSFER_BUFFER;
|
||||
cdcAcmHandle->bulkOut.pipeStall = 0U;
|
||||
cdcAcmHandle->bulkOut.pipeDataLen = 0U;
|
||||
epCallback.callbackFn = USB_DeviceCdcAcmBulkOut;
|
||||
}
|
||||
else
|
||||
{
|
||||
/*no action*/
|
||||
}
|
||||
epCallback.callbackParam = cdcAcmHandle;
|
||||
|
||||
error = USB_DeviceInitEndpoint(cdcAcmHandle->handle, &epInitStruct, &epCallback);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief De-initializes the endpoints in CDC ACM class.
|
||||
*
|
||||
* This function de-initializes the endpoints in CDC ACM class.
|
||||
*
|
||||
* @param cdcAcmHandle The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCdcAcmEndpointsDeinit(usb_device_cdc_acm_struct_t *cdcAcmHandle)
|
||||
{
|
||||
usb_status_t status = kStatus_USB_Error;
|
||||
uint32_t count;
|
||||
|
||||
if ((NULL == cdcAcmHandle->commInterfaceHandle) || (NULL == cdcAcmHandle->dataInterfaceHandle))
|
||||
{
|
||||
return status;
|
||||
}
|
||||
for (count = 0; count < cdcAcmHandle->commInterfaceHandle->endpointList.count; count++)
|
||||
{
|
||||
status = USB_DeviceDeinitEndpoint(
|
||||
cdcAcmHandle->handle, cdcAcmHandle->commInterfaceHandle->endpointList.endpoint[count].endpointAddress);
|
||||
}
|
||||
for (count = 0; count < cdcAcmHandle->dataInterfaceHandle->endpointList.count; count++)
|
||||
{
|
||||
status = USB_DeviceDeinitEndpoint(
|
||||
cdcAcmHandle->handle, cdcAcmHandle->dataInterfaceHandle->endpointList.endpoint[count].endpointAddress);
|
||||
}
|
||||
cdcAcmHandle->commInterfaceHandle = NULL;
|
||||
cdcAcmHandle->dataInterfaceHandle = NULL;
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Handles the CDC ACM class event.
|
||||
*
|
||||
* This function responses to various events including the common device events and the class specific events.
|
||||
* For class specific events, it calls the class callback defined in the application to deal with the class specific
|
||||
* event.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @param event The event type.
|
||||
* @param param The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceCdcAcmEvent(void *handle, uint32_t event, void *param)
|
||||
{
|
||||
usb_device_cdc_acm_struct_t *cdcAcmHandle;
|
||||
usb_device_cdc_acm_request_param_struct_t reqParam;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
uint32_t count;
|
||||
uint16_t interfaceAlternate;
|
||||
uint8_t *temp8;
|
||||
uint8_t alternate;
|
||||
usb_device_class_event_t eventCode = (usb_device_class_event_t)event;
|
||||
if ((NULL == param) || (NULL == handle))
|
||||
{
|
||||
return kStatus_USB_InvalidHandle;
|
||||
}
|
||||
|
||||
cdcAcmHandle = (usb_device_cdc_acm_struct_t *)handle;
|
||||
|
||||
switch (eventCode)
|
||||
{
|
||||
case kUSB_DeviceClassEventDeviceReset:
|
||||
/* Bus reset, clear the configuration. */
|
||||
cdcAcmHandle->configuration = 0;
|
||||
break;
|
||||
case kUSB_DeviceClassEventSetConfiguration:
|
||||
temp8 = ((uint8_t *)param);
|
||||
if (NULL == cdcAcmHandle->configStruct)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (*temp8 == cdcAcmHandle->configuration)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
error = USB_DeviceCdcAcmEndpointsDeinit(cdcAcmHandle);
|
||||
cdcAcmHandle->configuration = *temp8;
|
||||
cdcAcmHandle->alternate = 0U;
|
||||
error = USB_DeviceCdcAcmEndpointsInit(cdcAcmHandle);
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
(void)usb_echo("kUSB_DeviceClassEventSetConfiguration, USB_DeviceInitEndpoint fail\r\n");
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
case kUSB_DeviceClassEventSetInterface:
|
||||
if (NULL == cdcAcmHandle->configStruct)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
interfaceAlternate = *((uint16_t *)param);
|
||||
alternate = (uint8_t)(interfaceAlternate & 0xFFU);
|
||||
|
||||
if (cdcAcmHandle->interfaceNumber != ((uint8_t)(interfaceAlternate >> 8U)))
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (alternate == cdcAcmHandle->alternate)
|
||||
{
|
||||
break;
|
||||
}
|
||||
error = USB_DeviceCdcAcmEndpointsDeinit(cdcAcmHandle);
|
||||
cdcAcmHandle->alternate = alternate;
|
||||
error = USB_DeviceCdcAcmEndpointsInit(cdcAcmHandle);
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
(void)usb_echo("kUSB_DeviceClassEventSetInterface, USB_DeviceInitEndpoint fail\r\n");
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
case kUSB_DeviceClassEventSetEndpointHalt:
|
||||
if ((NULL == cdcAcmHandle->configStruct) || (NULL == cdcAcmHandle->commInterfaceHandle) ||
|
||||
(NULL == cdcAcmHandle->dataInterfaceHandle))
|
||||
{
|
||||
break;
|
||||
}
|
||||
temp8 = ((uint8_t *)param);
|
||||
for (count = 0; count < cdcAcmHandle->commInterfaceHandle->endpointList.count; count++)
|
||||
{
|
||||
if (*temp8 == cdcAcmHandle->commInterfaceHandle->endpointList.endpoint[count].endpointAddress)
|
||||
{
|
||||
cdcAcmHandle->interruptIn.pipeStall = 1U;
|
||||
error = USB_DeviceStallEndpoint(cdcAcmHandle->handle, *temp8);
|
||||
}
|
||||
}
|
||||
for (count = 0; count < cdcAcmHandle->dataInterfaceHandle->endpointList.count; count++)
|
||||
{
|
||||
if (*temp8 == cdcAcmHandle->dataInterfaceHandle->endpointList.endpoint[count].endpointAddress)
|
||||
{
|
||||
if (USB_IN == (((*temp8) & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT))
|
||||
{
|
||||
cdcAcmHandle->bulkIn.pipeStall = 1U;
|
||||
}
|
||||
else
|
||||
{
|
||||
cdcAcmHandle->bulkOut.pipeStall = 1U;
|
||||
}
|
||||
error = USB_DeviceStallEndpoint(cdcAcmHandle->handle, *temp8);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case kUSB_DeviceClassEventClearEndpointHalt:
|
||||
if ((NULL == cdcAcmHandle->configStruct) || (NULL == cdcAcmHandle->commInterfaceHandle) ||
|
||||
(NULL == cdcAcmHandle->dataInterfaceHandle))
|
||||
{
|
||||
break;
|
||||
}
|
||||
temp8 = ((uint8_t *)param);
|
||||
for (count = 0; count < cdcAcmHandle->commInterfaceHandle->endpointList.count; count++)
|
||||
{
|
||||
if (*temp8 == cdcAcmHandle->commInterfaceHandle->endpointList.endpoint[count].endpointAddress)
|
||||
{
|
||||
error = USB_DeviceUnstallEndpoint(cdcAcmHandle->handle, *temp8);
|
||||
if (USB_IN == (((*temp8) & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT))
|
||||
{
|
||||
if (0U != cdcAcmHandle->interruptIn.pipeStall)
|
||||
{
|
||||
cdcAcmHandle->interruptIn.pipeStall = 0U;
|
||||
if ((uint8_t *)USB_INVALID_TRANSFER_BUFFER != cdcAcmHandle->interruptIn.pipeDataBuffer)
|
||||
{
|
||||
error = USB_DeviceSendRequest(cdcAcmHandle->handle, (cdcAcmHandle->interruptIn.ep),
|
||||
cdcAcmHandle->interruptIn.pipeDataBuffer,
|
||||
cdcAcmHandle->interruptIn.pipeDataLen);
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
usb_device_endpoint_callback_message_struct_t endpointCallbackMessage;
|
||||
endpointCallbackMessage.buffer = cdcAcmHandle->interruptIn.pipeDataBuffer;
|
||||
endpointCallbackMessage.length = cdcAcmHandle->interruptIn.pipeDataLen;
|
||||
endpointCallbackMessage.isSetup = 0U;
|
||||
(void)USB_DeviceCdcAcmBulkIn(cdcAcmHandle->handle, (void *)&endpointCallbackMessage,
|
||||
handle);
|
||||
}
|
||||
cdcAcmHandle->interruptIn.pipeDataBuffer = (uint8_t *)USB_INVALID_TRANSFER_BUFFER;
|
||||
cdcAcmHandle->interruptIn.pipeDataLen = 0U;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (count = 0; count < cdcAcmHandle->dataInterfaceHandle->endpointList.count; count++)
|
||||
{
|
||||
if (*temp8 == cdcAcmHandle->dataInterfaceHandle->endpointList.endpoint[count].endpointAddress)
|
||||
{
|
||||
error = USB_DeviceUnstallEndpoint(cdcAcmHandle->handle, *temp8);
|
||||
if (USB_IN == (((*temp8) & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT))
|
||||
{
|
||||
if (0U != cdcAcmHandle->bulkIn.pipeStall)
|
||||
{
|
||||
cdcAcmHandle->bulkIn.pipeStall = 0U;
|
||||
if ((uint8_t *)USB_INVALID_TRANSFER_BUFFER != cdcAcmHandle->bulkIn.pipeDataBuffer)
|
||||
{
|
||||
error = USB_DeviceSendRequest(cdcAcmHandle->handle, (cdcAcmHandle->bulkIn.ep),
|
||||
cdcAcmHandle->bulkIn.pipeDataBuffer,
|
||||
cdcAcmHandle->bulkIn.pipeDataLen);
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
usb_device_endpoint_callback_message_struct_t endpointCallbackMessage;
|
||||
endpointCallbackMessage.buffer = cdcAcmHandle->bulkIn.pipeDataBuffer;
|
||||
endpointCallbackMessage.length = cdcAcmHandle->bulkIn.pipeDataLen;
|
||||
endpointCallbackMessage.isSetup = 0U;
|
||||
(void)USB_DeviceCdcAcmBulkIn(cdcAcmHandle->handle, (void *)&endpointCallbackMessage,
|
||||
handle);
|
||||
}
|
||||
cdcAcmHandle->bulkIn.pipeDataBuffer = (uint8_t *)USB_INVALID_TRANSFER_BUFFER;
|
||||
cdcAcmHandle->bulkIn.pipeDataLen = 0U;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (0U != cdcAcmHandle->bulkOut.pipeStall)
|
||||
{
|
||||
cdcAcmHandle->bulkOut.pipeStall = 0U;
|
||||
if ((uint8_t *)USB_INVALID_TRANSFER_BUFFER != cdcAcmHandle->bulkOut.pipeDataBuffer)
|
||||
{
|
||||
error = USB_DeviceRecvRequest(cdcAcmHandle->handle, (cdcAcmHandle->bulkOut.ep),
|
||||
cdcAcmHandle->bulkOut.pipeDataBuffer,
|
||||
cdcAcmHandle->bulkOut.pipeDataLen);
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
usb_device_endpoint_callback_message_struct_t endpointCallbackMessage;
|
||||
endpointCallbackMessage.buffer = cdcAcmHandle->bulkOut.pipeDataBuffer;
|
||||
endpointCallbackMessage.length = cdcAcmHandle->bulkOut.pipeDataLen;
|
||||
endpointCallbackMessage.isSetup = 0U;
|
||||
(void)USB_DeviceCdcAcmBulkOut(cdcAcmHandle->handle,
|
||||
(void *)&endpointCallbackMessage, handle);
|
||||
}
|
||||
cdcAcmHandle->bulkOut.pipeDataBuffer = (uint8_t *)USB_INVALID_TRANSFER_BUFFER;
|
||||
cdcAcmHandle->bulkOut.pipeDataLen = 0U;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case kUSB_DeviceClassEventClassRequest:
|
||||
|
||||
{
|
||||
usb_device_control_request_struct_t *controlRequest = (usb_device_control_request_struct_t *)param;
|
||||
|
||||
if ((controlRequest->setup->wIndex & 0xFFU) != cdcAcmHandle->interfaceNumber)
|
||||
{
|
||||
break;
|
||||
}
|
||||
/* Standard CDC request */
|
||||
if (USB_REQUEST_TYPE_TYPE_CLASS == (controlRequest->setup->bmRequestType & USB_REQUEST_TYPE_TYPE_MASK))
|
||||
{
|
||||
reqParam.buffer = &(controlRequest->buffer);
|
||||
reqParam.length = &(controlRequest->length);
|
||||
reqParam.interfaceIndex = controlRequest->setup->wIndex;
|
||||
reqParam.setupValue = controlRequest->setup->wValue;
|
||||
reqParam.isSetup = controlRequest->isSetup;
|
||||
switch (controlRequest->setup->bRequest)
|
||||
{
|
||||
case USB_DEVICE_CDC_REQUEST_SEND_ENCAPSULATED_COMMAND:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback(
|
||||
(class_handle_t)cdcAcmHandle, kUSB_DeviceCdcEventSendEncapsulatedCommand, &reqParam);
|
||||
break;
|
||||
case USB_DEVICE_CDC_REQUEST_GET_ENCAPSULATED_RESPONSE:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback(
|
||||
(class_handle_t)cdcAcmHandle, kUSB_DeviceCdcEventGetEncapsulatedResponse, &reqParam);
|
||||
break;
|
||||
case USB_DEVICE_CDC_REQUEST_SET_COMM_FEATURE:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback((class_handle_t)cdcAcmHandle,
|
||||
kUSB_DeviceCdcEventSetCommFeature, &reqParam);
|
||||
break;
|
||||
case USB_DEVICE_CDC_REQUEST_GET_COMM_FEATURE:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback((class_handle_t)cdcAcmHandle,
|
||||
kUSB_DeviceCdcEventGetCommFeature, &reqParam);
|
||||
break;
|
||||
case USB_DEVICE_CDC_REQUEST_CLEAR_COMM_FEATURE:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback(
|
||||
(class_handle_t)cdcAcmHandle, kUSB_DeviceCdcEventClearCommFeature, &reqParam);
|
||||
break;
|
||||
case USB_DEVICE_CDC_REQUEST_GET_LINE_CODING:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback((class_handle_t)cdcAcmHandle,
|
||||
kUSB_DeviceCdcEventGetLineCoding, &reqParam);
|
||||
break;
|
||||
case USB_DEVICE_CDC_REQUEST_SET_LINE_CODING:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback((class_handle_t)cdcAcmHandle,
|
||||
kUSB_DeviceCdcEventSetLineCoding, &reqParam);
|
||||
break;
|
||||
case USB_DEVICE_CDC_REQUEST_SET_CONTROL_LINE_STATE:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback(
|
||||
(class_handle_t)cdcAcmHandle, kUSB_DeviceCdcEventSetControlLineState, &reqParam);
|
||||
break;
|
||||
case USB_DEVICE_CDC_REQUEST_SEND_BREAK:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback((class_handle_t)cdcAcmHandle,
|
||||
kUSB_DeviceCdcEventSendBreak, &reqParam);
|
||||
break;
|
||||
default:
|
||||
error = kStatus_USB_InvalidRequest;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
/*no action*/
|
||||
break;
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Initializes the USB CDC ACM class.
|
||||
*
|
||||
* This function obtains a usb device handle according to the controller id, initializes the CDC ACM class
|
||||
* with the class configure parameters and creates the mutex for each pipe.
|
||||
*
|
||||
* @param controllerId The id of the controller. The value can be choosen from kUSB_ControllerKhci0,
|
||||
* kUSB_ControllerKhci1, kUSB_ControllerEhci0 or kUSB_ControllerEhci1.
|
||||
* @param config The user configuration structure of type usb_device_class_config_struct_t. The user
|
||||
* populates the members of this structure and passes the pointer of this structure
|
||||
* into this function.
|
||||
* @param handle It is out parameter. The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceCdcAcmInit(uint8_t controllerId,
|
||||
usb_device_class_config_struct_t *config,
|
||||
class_handle_t *handle)
|
||||
{
|
||||
usb_device_cdc_acm_struct_t *cdcAcmHandle;
|
||||
usb_status_t error;
|
||||
|
||||
error = USB_DeviceCdcAcmAllocateHandle(&cdcAcmHandle);
|
||||
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
error = USB_DeviceClassGetDeviceHandle(controllerId, &cdcAcmHandle->handle);
|
||||
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
if (NULL == cdcAcmHandle->handle)
|
||||
{
|
||||
return kStatus_USB_InvalidHandle;
|
||||
}
|
||||
cdcAcmHandle->configStruct = config;
|
||||
cdcAcmHandle->configuration = 0;
|
||||
cdcAcmHandle->alternate = 0xFF;
|
||||
|
||||
cdcAcmHandle->bulkIn.mutex = (osa_mutex_handle_t)&cdcAcmHandle->bulkIn.mutexBuffer[0];
|
||||
if (KOSA_StatusSuccess != OSA_MutexCreate((cdcAcmHandle->bulkIn.mutex)))
|
||||
{
|
||||
#ifdef DEBUG
|
||||
(void)usb_echo("mutex create error!");
|
||||
#endif
|
||||
}
|
||||
cdcAcmHandle->bulkOut.mutex = (osa_mutex_handle_t)&cdcAcmHandle->bulkOut.mutexBuffer[0];
|
||||
if (KOSA_StatusSuccess != OSA_MutexCreate((cdcAcmHandle->bulkOut.mutex)))
|
||||
{
|
||||
#ifdef DEBUG
|
||||
(void)usb_echo("mutex create error!");
|
||||
#endif
|
||||
}
|
||||
cdcAcmHandle->interruptIn.mutex = (osa_mutex_handle_t)&cdcAcmHandle->interruptIn.mutexBuffer[0];
|
||||
if (KOSA_StatusSuccess != OSA_MutexCreate((cdcAcmHandle->interruptIn.mutex)))
|
||||
{
|
||||
#ifdef DEBUG
|
||||
(void)usb_echo("mutex create error!");
|
||||
#endif
|
||||
}
|
||||
*handle = (class_handle_t)cdcAcmHandle;
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief De-Initializes the USB CDC ACM class.
|
||||
*
|
||||
* This function destroys the mutex for each pipe, deinit each endpoint of the CDC ACM class and free
|
||||
* the CDC ACM class handle.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceCdcAcmDeinit(class_handle_t handle)
|
||||
{
|
||||
usb_device_cdc_acm_struct_t *cdcAcmHandle;
|
||||
usb_status_t error;
|
||||
|
||||
cdcAcmHandle = (usb_device_cdc_acm_struct_t *)handle;
|
||||
|
||||
if (NULL == cdcAcmHandle)
|
||||
{
|
||||
return kStatus_USB_InvalidHandle;
|
||||
}
|
||||
if (KOSA_StatusSuccess != OSA_MutexDestroy((cdcAcmHandle->bulkIn.mutex)))
|
||||
{
|
||||
#ifdef DEBUG
|
||||
(void)usb_echo("mutex destroy error!");
|
||||
#endif
|
||||
}
|
||||
if (KOSA_StatusSuccess != OSA_MutexDestroy((cdcAcmHandle->bulkOut.mutex)))
|
||||
{
|
||||
#ifdef DEBUG
|
||||
(void)usb_echo("mutex destroy error!");
|
||||
#endif
|
||||
}
|
||||
if (KOSA_StatusSuccess != OSA_MutexDestroy((cdcAcmHandle->interruptIn.mutex)))
|
||||
{
|
||||
#ifdef DEBUG
|
||||
(void)usb_echo("mutex destroy error!");
|
||||
#endif
|
||||
}
|
||||
error = USB_DeviceCdcAcmEndpointsDeinit(cdcAcmHandle);
|
||||
(void)USB_DeviceCdcAcmFreeHandle(cdcAcmHandle);
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Prime the endpoint to send packet to host.
|
||||
*
|
||||
* This function checks whether the endpoint is sending packet, then it primes the endpoint
|
||||
* with the buffer address and the buffer length if the pipe is not busy. Otherwise, it ignores this transfer by
|
||||
* returning an error code.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @param ep The endpoint number of the transfer.
|
||||
* @param buffer The pointer to the buffer to be transferred.
|
||||
* @param length The length of the buffer to be transferred.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceCdcAcmSend(class_handle_t handle, uint8_t ep, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
usb_device_cdc_acm_struct_t *cdcAcmHandle;
|
||||
usb_status_t status = kStatus_USB_Error;
|
||||
usb_device_cdc_acm_pipe_t *cdcAcmPipe = NULL;
|
||||
|
||||
if (NULL == handle)
|
||||
{
|
||||
return kStatus_USB_InvalidHandle;
|
||||
}
|
||||
cdcAcmHandle = (usb_device_cdc_acm_struct_t *)handle;
|
||||
|
||||
if (cdcAcmHandle->bulkIn.ep == ep)
|
||||
{
|
||||
cdcAcmPipe = &(cdcAcmHandle->bulkIn);
|
||||
}
|
||||
else if (cdcAcmHandle->interruptIn.ep == ep)
|
||||
{
|
||||
cdcAcmPipe = &(cdcAcmHandle->interruptIn);
|
||||
}
|
||||
else
|
||||
{
|
||||
/*no action*/
|
||||
}
|
||||
|
||||
if (NULL != cdcAcmPipe)
|
||||
{
|
||||
if (1U == cdcAcmPipe->isBusy)
|
||||
{
|
||||
return kStatus_USB_Busy;
|
||||
}
|
||||
cdcAcmPipe->isBusy = 1U;
|
||||
|
||||
if (0u != cdcAcmPipe->pipeStall)
|
||||
{
|
||||
cdcAcmPipe->pipeDataBuffer = buffer;
|
||||
cdcAcmPipe->pipeDataLen = length;
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
|
||||
status = USB_DeviceSendRequest(cdcAcmHandle->handle, ep, buffer, length);
|
||||
if (kStatus_USB_Success != status)
|
||||
{
|
||||
cdcAcmPipe->isBusy = 0U;
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Prime the endpoint to receive packet from host.
|
||||
*
|
||||
* This function checks whether the endpoint is receiving packet, then it primes the endpoint
|
||||
* with the buffer address and the buffer length if the pipe is not busy. Otherwise, it ignores this transfer by
|
||||
* returning an error code.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @param ep The endpoint number of the transfer.
|
||||
* @param buffer The pointer to the buffer to be transferred.
|
||||
* @param length The length of the buffer to be transferred.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceCdcAcmRecv(class_handle_t handle, uint8_t ep, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
usb_device_cdc_acm_struct_t *cdcAcmHandle;
|
||||
usb_status_t status;
|
||||
if (NULL == handle)
|
||||
{
|
||||
return kStatus_USB_InvalidHandle;
|
||||
}
|
||||
cdcAcmHandle = (usb_device_cdc_acm_struct_t *)handle;
|
||||
|
||||
if (1U == cdcAcmHandle->bulkOut.isBusy)
|
||||
{
|
||||
return kStatus_USB_Busy;
|
||||
}
|
||||
cdcAcmHandle->bulkOut.isBusy = 1U;
|
||||
|
||||
if (0U != cdcAcmHandle->bulkOut.pipeStall)
|
||||
{
|
||||
cdcAcmHandle->bulkOut.pipeDataBuffer = buffer;
|
||||
cdcAcmHandle->bulkOut.pipeDataLen = length;
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
|
||||
status = USB_DeviceRecvRequest(cdcAcmHandle->handle, ep, buffer, length);
|
||||
if (kStatus_USB_Success != status)
|
||||
{
|
||||
cdcAcmHandle->bulkOut.isBusy = 0U;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
#endif /* USB_DEVICE_CONFIG_CDC_ACM */
|
||||
270
usb/device/class/cdc/usb_device_cdc_acm.h
Normal file
270
usb/device/class/cdc/usb_device_cdc_acm.h
Normal file
@@ -0,0 +1,270 @@
|
||||
/*
|
||||
* Copyright (c) 2015, Freescale Semiconductor, Inc.
|
||||
* Copyright 2016,2019 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#ifndef _USB_DEVICE_CDC_ACM_H_
|
||||
#define _USB_DEVICE_CDC_ACM_H_
|
||||
|
||||
/*!
|
||||
* @addtogroup cdc_acm
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
#define USB_DEVICE_CONFIG_CDC_ACM_MAX_INSTANCE (1U) /*!< The maximum number of CDC device instance. */
|
||||
#define USB_DEVICE_CONFIG_CDC_COMM_CLASS_CODE (0x02U) /*!< The CDC communication class code. */
|
||||
#define USB_DEVICE_CONFIG_CDC_DATA_CLASS_CODE (0x0AU) /*!< The CDC data class code. */
|
||||
|
||||
#define USB_DEVICE_CDC_REQUEST_SEND_ENCAPSULATED_COMMAND \
|
||||
(0x00) /*!< The CDC class request code for SEND_ENCAPSULATED_COMMAND. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_ENCAPSULATED_RESPONSE \
|
||||
(0x01) /*!< The CDC class request code for GET_ENCAPSULATED_RESPONSE. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_COMM_FEATURE (0x02) /*!< The CDC class request code for SET_COMM_FEATURE. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_COMM_FEATURE (0x03) /*!< The CDC class request code for GET_COMM_FEATURE. */
|
||||
#define USB_DEVICE_CDC_REQUEST_CLEAR_COMM_FEATURE (0x04) /*!< The CDC class request code for CLEAR_COMM_FEATURE. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_AUX_LINE_STATE (0x10) /*!< The CDC class request code for SET_AUX_LINE_STATE. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_HOOK_STATE (0x11) /*!< The CDC class request code for SET_HOOK_STATE. */
|
||||
#define USB_DEVICE_CDC_REQUEST_PULSE_SETUP (0x12) /*!< The CDC class request code for PULSE_SETUP. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SEND_PULSE (0x13) /*!< The CDC class request code for SEND_PULSE. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_PULSE_TIME (0x14) /*!< The CDC class request code for SET_PULSE_TIME. */
|
||||
#define USB_DEVICE_CDC_REQUEST_RING_AUX_JACK (0x15) /*!< The CDC class request code for RING_AUX_JACK. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_LINE_CODING (0x20) /*!< The CDC class request code for SET_LINE_CODING. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_LINE_CODING (0x21) /*!< The CDC class request code for GET_LINE_CODING. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_CONTROL_LINE_STATE \
|
||||
(0x22) /*!< The CDC class request code for SET_CONTROL_LINE_STATE. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SEND_BREAK (0x23) /*!< The CDC class request code for SEND_BREAK. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_RINGER_PARAMS (0x30) /*!< The CDC class request code for SET_RINGER_PARAMS. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_RINGER_PARAMS (0x31) /*!< The CDC class request code for GET_RINGER_PARAMS. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_OPERATION_PARAM (0x32) /*!< The CDC class request code for SET_OPERATION_PARAM. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_OPERATION_PARAM (0x33) /*!< The CDC class request code for GET_OPERATION_PARAM. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_LINE_PARAMS (0x34) /*!< The CDC class request code for SET_LINE_PARAMS. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_LINE_PARAMS (0x35) /*!< The CDC class request code for GET_LINE_PARAMS. */
|
||||
#define USB_DEVICE_CDC_REQUEST_DIAL_DIGITS (0x36) /*!< The CDC class request code for DIAL_DIGITS. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_UNIT_PARAMETER (0x37) /*!< The CDC class request code for SET_UNIT_PARAMETER. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_UNIT_PARAMETER (0x38) /*!< The CDC class request code for GET_UNIT_PARAMETER. */
|
||||
#define USB_DEVICE_CDC_REQUEST_CLEAR_UNIT_PARAMETER \
|
||||
(0x39) /*!< The CDC class request code for CLEAR_UNIT_PARAMETER. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_ETHERNET_MULTICAST_FILTERS \
|
||||
(0x40) /*!< The CDC class request code for SET_ETHERNET_MULTICAST_FILTERS. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_ETHERNET_POW_PATTER_FILTER \
|
||||
(0x41) /*!< The CDC class request code for SET_ETHERNET_POW_PATTER_FILTER. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_ETHERNET_POW_PATTER_FILTER \
|
||||
(0x42) /*!< The CDC class request code for GET_ETHERNET_POW_PATTER_FILTER. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_ETHERNET_PACKET_FILTER \
|
||||
(0x43) /*!< The CDC class request code for SET_ETHERNET_PACKET_FILTER. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_ETHERNET_STATISTIC \
|
||||
(0x44) /*!< The CDC class request code for GET_ETHERNET_STATISTIC. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_ATM_DATA_FORMAT (0x50) /*!< The CDC class request code for SET_ATM_DATA_FORMAT. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_ATM_DEVICE_STATISTICS \
|
||||
(0x51) /*!< The CDC class request code for GET_ATM_DEVICE_STATISTICS. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_ATM_DEFAULT_VC (0x52) /*!< The CDC class request code for SET_ATM_DEFAULT_VC. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_ATM_VC_STATISTICS \
|
||||
(0x53) /*!< The CDC class request code for GET_ATM_VC_STATISTICS. */
|
||||
#define USB_DEVICE_CDC_REQUEST_MDLM_SPECIFIC_REQUESTS_MASK \
|
||||
(0x7F) /*!< The CDC class request code for MDLM_SPECIFIC_REQUESTS_MASK. */
|
||||
|
||||
#define USB_DEVICE_CDC_NOTIF_NETWORK_CONNECTION (0x00) /*!< The CDC class notify code for NETWORK_CONNECTION. */
|
||||
#define USB_DEVICE_CDC_NOTIF_RESPONSE_AVAIL (0x01) /*!< The CDC class notify code for RESPONSE_AVAIL. */
|
||||
#define USB_DEVICE_CDC_NOTIF_AUX_JACK_HOOK_STATE (0x08) /*!< The CDC class notify code for AUX_JACK_HOOK_STATE. */
|
||||
#define USB_DEVICE_CDC_NOTIF_RING_DETECT (0x09) /*!< The CDC class notify code for RING_DETECT. */
|
||||
#define USB_DEVICE_CDC_NOTIF_SERIAL_STATE (0x20) /*!< The CDC class notify code for SERIAL_STATE. */
|
||||
#define USB_DEVICE_CDC_NOTIF_CALL_STATE_CHANGE (0x28) /*!< The CDC class notify code for CALL_STATE_CHANGE. */
|
||||
#define USB_DEVICE_CDC_NOTIF_LINE_STATE_CHANGE (0x29) /*!< The CDC class notify code for LINE_STATE_CHANGE. */
|
||||
#define USB_DEVICE_CDC_NOTIF_CONNECTION_SPEED_CHANGE \
|
||||
(0x2A) /*!< The CDC class notify code for CONNECTION_SPEED_CHANGE. */
|
||||
|
||||
#define USB_DEVICE_CDC_FEATURE_ABSTRACT_STATE (0x01) /*!< The CDC class feature select code for ABSTRACT_STATE. */
|
||||
#define USB_DEVICE_CDC_FEATURE_COUNTRY_SETTING (0x02) /*!< The CDC class feature select code for COUNTRY_SETTING. */
|
||||
|
||||
#define USB_DEVICE_CDC_CONTROL_SIG_BITMAP_CARRIER_ACTIVATION \
|
||||
(0x02) /*!< The CDC class control signal bitmap value for CARRIER_ACTIVATION. */
|
||||
#define USB_DEVICE_CDC_CONTROL_SIG_BITMAP_DTE_PRESENCE \
|
||||
(0x01) /*!< The CDC class control signal bitmap value for DTE_PRESENCE. */
|
||||
#define USB_DEVICE_CDC_UART_STATE_RX_CARRIER (0x01) /*!< The UART state bitmap value of RX_CARRIER. */
|
||||
#define USB_DEVICE_CDC_UART_STATE_TX_CARRIER (0x02) /*!< The UART state bitmap value of TX_CARRIER. */
|
||||
#define USB_DEVICE_CDC_UART_STATE_BREAK (0x04) /*!< The UART state bitmap value of BREAK. */
|
||||
#define USB_DEVICE_CDC_UART_STATE_RING_SIGNAL (0x08) /*!< The UART state bitmap value of RING_SIGNAL. */
|
||||
#define USB_DEVICE_CDC_UART_STATE_FRAMING (0x10) /*!< The UART state bitmap value of FRAMING. */
|
||||
#define USB_DEVICE_CDC_UART_STATE_PARITY (0x20) /*!< The UART state bitmap value of PARITY. */
|
||||
#define USB_DEVICE_CDC_UART_STATE_OVERRUN (0x40) /*!< The UART state bitmap value of OVERRUN. */
|
||||
|
||||
/*! @brief Definition of CDC class event. */
|
||||
typedef enum _usb_device_cdc_acm_event
|
||||
{
|
||||
kUSB_DeviceCdcEventSendResponse = 0x01, /*!< This event indicates the bulk send transfer is complete or cancelled etc. */
|
||||
kUSB_DeviceCdcEventRecvResponse, /*!< This event indicates the bulk receive transfer is complete or cancelled etc.. */
|
||||
kUSB_DeviceCdcEventSerialStateNotif, /*!< This event indicates the serial state has been sent to the host. */
|
||||
kUSB_DeviceCdcEventSendEncapsulatedCommand, /*!< This event indicates the device received the
|
||||
SEND_ENCAPSULATED_COMMAND request. */
|
||||
kUSB_DeviceCdcEventGetEncapsulatedResponse, /*!< This event indicates the device received the
|
||||
GET_ENCAPSULATED_RESPONSE request. */
|
||||
kUSB_DeviceCdcEventSetCommFeature, /*!< This event indicates the device received the SET_COMM_FEATURE request. */
|
||||
kUSB_DeviceCdcEventGetCommFeature, /*!< This event indicates the device received the GET_COMM_FEATURE request. */
|
||||
kUSB_DeviceCdcEventClearCommFeature, /*!< This event indicates the device received the CLEAR_COMM_FEATURE request.
|
||||
*/
|
||||
kUSB_DeviceCdcEventGetLineCoding, /*!< This event indicates the device received the GET_LINE_CODING request. */
|
||||
kUSB_DeviceCdcEventSetLineCoding, /*!< This event indicates the device received the SET_LINE_CODING request. */
|
||||
kUSB_DeviceCdcEventSetControlLineState, /*!< This event indicates the device received the SET_CONTRL_LINE_STATE
|
||||
request. */
|
||||
kUSB_DeviceCdcEventSendBreak /*!< This event indicates the device received the SEND_BREAK request. */
|
||||
} usb_device_cdc_acm_event_t;
|
||||
|
||||
/*! @brief Definition of parameters for CDC ACM request. */
|
||||
typedef struct _usb_device_cdc_acm_request_param_struct
|
||||
{
|
||||
uint8_t **buffer; /*!< The pointer to the address of the buffer for CDC class request. */
|
||||
uint32_t *length; /*!< The pointer to the length of the buffer for CDC class request. */
|
||||
uint16_t interfaceIndex; /*!< The interface index of the setup packet. */
|
||||
uint16_t setupValue; /*!< The wValue field of the setup packet. */
|
||||
uint8_t isSetup; /*!< The flag indicates if it is a setup packet, 1: yes, 0: no. */
|
||||
} usb_device_cdc_acm_request_param_struct_t;
|
||||
|
||||
/*! @brief Definition of pipe structure. */
|
||||
typedef struct _usb_device_cdc_acm_pipe
|
||||
{
|
||||
osa_mutex_handle_t mutex; /*!< The mutex of the pipe. */
|
||||
uint32_t mutexBuffer[(OSA_MUTEX_HANDLE_SIZE + 3)/4];
|
||||
uint8_t *pipeDataBuffer; /*!< pipe data buffer backup when stall */
|
||||
uint32_t pipeDataLen; /*!< pipe data length backup when stall */
|
||||
uint8_t pipeStall; /*!< pipe is stall */
|
||||
uint8_t ep; /*!< The endpoint number of the pipe. */
|
||||
uint8_t isBusy; /*!< 1: The pipe is transferring packet, 0: The pipe is idle. */
|
||||
} usb_device_cdc_acm_pipe_t;
|
||||
|
||||
/*! @brief Definition of structure for CDC ACM device. */
|
||||
typedef struct _usb_device_cdc_acm_struct
|
||||
{
|
||||
usb_device_handle handle; /*!< The handle of the USB device. */
|
||||
usb_device_class_config_struct_t *configStruct; /*!< The class configure structure. */
|
||||
usb_device_interface_struct_t *commInterfaceHandle; /*!< The CDC communication interface handle. */
|
||||
usb_device_interface_struct_t *dataInterfaceHandle; /*!< The CDC data interface handle. */
|
||||
usb_device_cdc_acm_pipe_t bulkIn; /*!< The bulk in pipe for sending packet to host. */
|
||||
usb_device_cdc_acm_pipe_t bulkOut; /*!< The bulk out pipe for receiving packet from host. */
|
||||
usb_device_cdc_acm_pipe_t interruptIn; /*!< The interrupt in pipe for notifying the device state to host. */
|
||||
uint8_t configuration; /*!< The current configuration value. */
|
||||
uint8_t interfaceNumber; /*!< The current interface number. */
|
||||
uint8_t alternate; /*!< The alternate setting value of the interface. */
|
||||
uint8_t hasSentState; /*!< 1: The device has primed the state in interrupt pipe, 0: Not primed the state. */
|
||||
} usb_device_cdc_acm_struct_t;
|
||||
|
||||
/*******************************************************************************
|
||||
* API
|
||||
******************************************************************************/
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* @name USB CDC ACM Class Driver
|
||||
* @{
|
||||
*/
|
||||
/*!
|
||||
* @brief Initializes the USB CDC ACM class.
|
||||
*
|
||||
* This function obtains a USB device handle according to the controller ID, initializes the CDC ACM class
|
||||
* with the class configure parameters and creates the mutex for each pipe.
|
||||
*
|
||||
* @param controllerId The ID of the controller. The value can be chosen from the kUSB_ControllerKhci0,
|
||||
* kUSB_ControllerKhci1, kUSB_ControllerEhci0, or kUSB_ControllerEhci1.
|
||||
* @param config The user configuration structure of type usb_device_class_config_struct_t. The user
|
||||
* populates the members of this structure and passes the pointer of this structure
|
||||
* into this function.
|
||||
* @param handle It is out parameter. The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
* @retval kStatus_USB_Success The CDC ACM class is initialized successfully.
|
||||
* @retval kStatus_USB_Busy No CDC ACM device handle available for allocation.
|
||||
* @retval kStatus_USB_InvalidHandle The CDC ACM device handle allocation failure.
|
||||
* @retval kStatus_USB_InvalidParameter The USB device handle allocation failure.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceCdcAcmInit(uint8_t controllerId,
|
||||
usb_device_class_config_struct_t *config,
|
||||
class_handle_t *handle);
|
||||
/*!
|
||||
* @brief Deinitializes the USB CDC ACM class.
|
||||
*
|
||||
* This function destroys the mutex for each pipe, deinitializes each endpoint of the CDC ACM class and frees
|
||||
* the CDC ACM class handle.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
* @retval kStatus_USB_Success The CDC ACM class is de-initialized successfully.
|
||||
* @retval kStatus_USB_Error The endpoint deinitialization failure.
|
||||
* @retval kStatus_USB_InvalidHandle The CDC ACM device handle or the CDC ACM class handle is invalid.
|
||||
* @retval kStatus_USB_InvalidParameter The endpoint number of the CDC ACM class handle is invalid.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceCdcAcmDeinit(class_handle_t handle);
|
||||
/*!
|
||||
* @brief Handles the CDC ACM class event.
|
||||
*
|
||||
* This function responds to various events including the common device events and the class-specific events.
|
||||
* For class-specific events, it calls the class callback defined in the application to deal with the class-specific
|
||||
* event.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @param event The event type.
|
||||
* @param param The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
* @retval kStatus_USB_Success The CDC ACM class is de-initialized successfully.
|
||||
* @retval kStatus_USB_Error The configure structure of the CDC ACM class handle is invalid.
|
||||
* @retval kStatus_USB_InvalidHandle The CDC ACM device handle or the CDC ACM class handle is invalid.
|
||||
* @retval kStatus_USB_InvalidParameter The endpoint number of the CDC ACM class handle is invalid.
|
||||
* @retval Others The error code returned by class callback in application.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceCdcAcmEvent(void *handle, uint32_t event, void *param);
|
||||
|
||||
/*!
|
||||
* @brief Primes the endpoint to send packet to host.
|
||||
*
|
||||
* This function checks whether the endpoint is sending packet, then it primes the endpoint
|
||||
* with the buffer address and the buffer length if the pipe is not busy. Otherwise, it ignores this transfer by
|
||||
* returning an error code.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @param ep The endpoint number of the transfer.
|
||||
* @param buffer The pointer to the buffer to be transferred.
|
||||
* @param length The length of the buffer to be transferred.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
* @retval kStatus_USB_Success Prime to send packet successfully.
|
||||
* @retval kStatus_USB_Busy The endpoint is busy in transferring.
|
||||
* @retval kStatus_USB_InvalidHandle The CDC ACM device handle or the CDC ACM class handle is invalid.
|
||||
* @retval kStatus_USB_ControllerNotFound The controller interface is invalid.
|
||||
*
|
||||
* @note The function can only be called in the same context.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceCdcAcmSend(class_handle_t handle, uint8_t ep, uint8_t *buffer, uint32_t length);
|
||||
/*!
|
||||
* @brief Primes the endpoint to receive packet from host.
|
||||
*
|
||||
* This function checks whether the endpoint is receiving packet, then it primes the endpoint
|
||||
* with the buffer address and the buffer length if the pipe is not busy. Otherwise, it ignores this transfer by
|
||||
* returning an error code.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @param ep The endpoint number of the transfer.
|
||||
* @param buffer The pointer to the buffer to be transferred.
|
||||
* @param length The length of the buffer to be transferred.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
* @retval kStatus_USB_Success Prime to receive packet successfully.
|
||||
* @retval kStatus_USB_Busy The endpoint is busy in transferring.
|
||||
* @retval kStatus_USB_InvalidHandle The CDC ACM device handle or the CDC ACM class handle is invalid.
|
||||
* @retval kStatus_USB_ControllerNotFound The controller interface is invalid.
|
||||
*
|
||||
* @note The function can only be called in the same context.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceCdcAcmRecv(class_handle_t handle, uint8_t ep, uint8_t *buffer, uint32_t length);
|
||||
|
||||
/*! @}*/
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
/*! @}*/
|
||||
|
||||
#endif /* _USB_DEVICE_CDC_ACM_H_ */
|
||||
Reference in New Issue
Block a user