initial check in based on SVN revision 575

This commit is contained in:
2025-05-14 12:57:39 -05:00
commit a3ef12e24a
217 changed files with 95547 additions and 0 deletions

View 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 */

View 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_ */

View File

@@ -0,0 +1,595 @@
/*
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
* Copyright 2016, 2019 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "usb_device_config.h"
#include "usb.h"
#include "usb_device.h"
#include "usb_device_ch9.h"
#include "usb_device_class.h"
#if ((defined(USB_DEVICE_CONFIG_NUM)) && (USB_DEVICE_CONFIG_NUM > 0U))
/* Include the class drivers according to the usb_device_config.h. */
#if ((defined(USB_DEVICE_CONFIG_HID)) && (USB_DEVICE_CONFIG_HID > 0U))
#include "usb_device_hid.h"
#endif
#if ((defined(USB_DEVICE_CONFIG_CDC_ACM)) && (USB_DEVICE_CONFIG_CDC_ACM > 0U))
#include "usb_device_cdc_acm.h"
#endif
#if ((defined(USB_DEVICE_CONFIG_MSC)) && (USB_DEVICE_CONFIG_MSC > 0U))
#include "usb_device_msc.h"
#endif
#if ((defined(USB_DEVICE_CONFIG_MTP)) && (USB_DEVICE_CONFIG_MTP > 0U))
#include "usb_device_mtp.h"
#endif
#if ((defined(USB_DEVICE_CONFIG_AUDIO)) && (USB_DEVICE_CONFIG_AUDIO > 0U))
#include "usb_device_audio.h"
#endif
#if ((defined(USB_DEVICE_CONFIG_PHDC)) && (USB_DEVICE_CONFIG_PHDC > 0U))
#include "usb_device_phdc.h"
#endif
#if ((defined(USB_DEVICE_CONFIG_VIDEO)) && (USB_DEVICE_CONFIG_VIDEO > 0U))
#include "usb_device_video.h"
#endif
#if ((defined(USB_DEVICE_CONFIG_PRINTER)) && (USB_DEVICE_CONFIG_PRINTER > 0U))
#include "usb_device_printer.h"
#endif
#if ((defined(USB_DEVICE_CONFIG_DFU)) && (USB_DEVICE_CONFIG_DFU > 0U))
#include "usb_device_dfu.h"
#endif
#if ((defined(USB_DEVICE_CONFIG_CCID)) && (USB_DEVICE_CONFIG_CCID > 0U))
#include "usb_device_ccid.h"
#endif
/*******************************************************************************
* Definitions
******************************************************************************/
/*******************************************************************************
* Prototypes
******************************************************************************/
static usb_status_t USB_DeviceClassAllocateHandle(uint8_t controllerId, usb_device_common_class_struct_t **handle);
static usb_status_t USB_DeviceClassFreeHandle(uint8_t controllerId);
static usb_status_t USB_DeviceClassGetHandleByControllerId(uint8_t controllerId,
usb_device_common_class_struct_t **handle);
static usb_status_t USB_DeviceClassGetHandleByDeviceHandle(usb_device_handle deviceHandle,
usb_device_common_class_struct_t **handle);
/*******************************************************************************
* Variables
******************************************************************************/
/* The device class driver list. */
static const usb_device_class_map_t s_UsbDeviceClassInterfaceMap[] = {
#if ((defined(USB_DEVICE_CONFIG_HID)) && (USB_DEVICE_CONFIG_HID > 0U))
{USB_DeviceHidInit, USB_DeviceHidDeinit, USB_DeviceHidEvent, kUSB_DeviceClassTypeHid},
#endif
#if ((defined(USB_DEVICE_CONFIG_CDC_ACM)) && (USB_DEVICE_CONFIG_CDC_ACM > 0U))
{USB_DeviceCdcAcmInit, USB_DeviceCdcAcmDeinit, USB_DeviceCdcAcmEvent, kUSB_DeviceClassTypeCdc},
#endif
#if ((defined(USB_DEVICE_CONFIG_MSC)) && (USB_DEVICE_CONFIG_MSC > 0U))
{USB_DeviceMscInit, USB_DeviceMscDeinit, USB_DeviceMscEvent, kUSB_DeviceClassTypeMsc},
#endif
#if ((defined(USB_DEVICE_CONFIG_MTP)) && (USB_DEVICE_CONFIG_MTP > 0U))
{USB_DeviceMtpInit, USB_DeviceMtpDeinit, USB_DeviceMtpEvent, kUSB_DeviceClassTypeMtp},
#endif
#if ((defined USB_DEVICE_CONFIG_AUDIO) && (USB_DEVICE_CONFIG_AUDIO > 0U))
{USB_DeviceAudioInit, USB_DeviceAudioDeinit, USB_DeviceAudioEvent, kUSB_DeviceClassTypeAudio},
#endif
#if ((defined USB_DEVICE_CONFIG_PHDC) && (USB_DEVICE_CONFIG_PHDC > 0U))
{USB_DevicePhdcInit, USB_DevicePhdcDeinit, USB_DevicePhdcEvent, kUSB_DeviceClassTypePhdc},
#endif
#if ((defined USB_DEVICE_CONFIG_VIDEO) && (USB_DEVICE_CONFIG_VIDEO > 0U))
{USB_DeviceVideoInit, USB_DeviceVideoDeinit, USB_DeviceVideoEvent, kUSB_DeviceClassTypeVideo},
#endif
#if ((defined USB_DEVICE_CONFIG_PRINTER) && (USB_DEVICE_CONFIG_PRINTER > 0U))
{USB_DevicePrinterInit, USB_DevicePrinterDeinit, USB_DevicePrinterEvent, kUSB_DeviceClassTypePrinter},
#endif
#if ((defined USB_DEVICE_CONFIG_DFU) && (USB_DEVICE_CONFIG_DFU > 0U))
{USB_DeviceDfuInit, USB_DeviceDfuDeinit, USB_DeviceDfuEvent, kUSB_DeviceClassTypeDfu},
#endif
#if ((defined USB_DEVICE_CONFIG_CCID) && (USB_DEVICE_CONFIG_CCID > 0U))
{USB_DeviceCcidInit, USB_DeviceCcidDeinit, USB_DeviceCcidEvent, kUSB_DeviceClassTypeCcid},
#endif
/* please make sure the following member is in the end of s_UsbDeviceClassInterfaceMap*/
{(usb_device_class_init_call_t)NULL, (usb_device_class_deinit_call_t)NULL, (usb_device_class_event_callback_t)NULL,
(usb_device_class_type_t)0},
};
USB_GLOBAL USB_RAM_ADDRESS_ALIGNMENT(USB_DATA_ALIGN_SIZE) static usb_device_common_class_struct_t
s_UsbDeviceCommonClassStruct[USB_DEVICE_CONFIG_NUM];
USB_GLOBAL USB_RAM_ADDRESS_ALIGNMENT(USB_DATA_ALIGN_SIZE) static uint8_t
s_UsbDeviceSetupBuffer[USB_DEVICE_CONFIG_NUM][USB_DATA_ALIGN_SIZE_MULTIPLE(USB_SETUP_PACKET_SIZE)];
/*******************************************************************************
* Code
******************************************************************************/
/*!
* @brief Allocate a device common class handle.
*
* This function allocates a a device common class handle.
*
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
* @param handle It is out parameter, is used to return pointer of the device common class handle to the
* caller.
*
* @retval kStatus_USB_Success Get a device handle successfully.
* @retval kStatus_USB_Busy Cannot allocate a common class handle.
* @retval kStatus_USB_Error The common class has been initialized.
*/
static usb_status_t USB_DeviceClassAllocateHandle(uint8_t controllerId, usb_device_common_class_struct_t **handle)
{
uint32_t count;
OSA_SR_ALLOC();
OSA_ENTER_CRITICAL();
/* Check the controller is initialized or not. */
for (count = 0U; count < USB_DEVICE_CONFIG_NUM; count++)
{
if ((NULL != s_UsbDeviceCommonClassStruct[count].handle) &&
(controllerId == s_UsbDeviceCommonClassStruct[count].controllerId))
{
OSA_EXIT_CRITICAL();
return kStatus_USB_Error;
}
}
/* Get a free common class handle. */
for (count = 0U; count < USB_DEVICE_CONFIG_NUM; count++)
{
if (NULL == s_UsbDeviceCommonClassStruct[count].handle)
{
s_UsbDeviceCommonClassStruct[count].controllerId = controllerId;
s_UsbDeviceCommonClassStruct[count].setupBuffer = s_UsbDeviceSetupBuffer[count];
*handle = &s_UsbDeviceCommonClassStruct[count];
OSA_EXIT_CRITICAL();
return kStatus_USB_Success;
}
}
OSA_EXIT_CRITICAL();
return kStatus_USB_Busy;
}
/*!
* @brief Free a device common class handle.
*
* This function frees a device common class handle.
*
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
*
* @retval kStatus_USB_Success Free device handle successfully.
* @retval kStatus_USB_InvalidParameter The common class can not be found.
*/
static usb_status_t USB_DeviceClassFreeHandle(uint8_t controllerId)
{
uint32_t count = 0U;
OSA_SR_ALLOC();
OSA_ENTER_CRITICAL();
for (; count < USB_DEVICE_CONFIG_NUM; count++)
{
if ((NULL != s_UsbDeviceCommonClassStruct[count].handle) &&
(controllerId == s_UsbDeviceCommonClassStruct[count].controllerId))
{
s_UsbDeviceCommonClassStruct[count].handle = NULL;
s_UsbDeviceCommonClassStruct[count].configList = (usb_device_class_config_list_struct_t *)NULL;
s_UsbDeviceCommonClassStruct[count].controllerId = 0U;
OSA_EXIT_CRITICAL();
return kStatus_USB_Success;
}
}
OSA_EXIT_CRITICAL();
return kStatus_USB_InvalidParameter;
}
/*!
* @brief Get the device common class handle according to the controller id.
*
* This function gets the device common class handle according to the controller id.
*
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
* @param handle It is out parameter, is used to return pointer of the device common class handle to the
* caller.
*
* @retval kStatus_USB_Success Free device handle successfully.
* @retval kStatus_USB_InvalidParameter The common class can not be found.
*/
static usb_status_t USB_DeviceClassGetHandleByControllerId(uint8_t controllerId,
usb_device_common_class_struct_t **handle)
{
uint32_t count = 0U;
OSA_SR_ALLOC();
OSA_ENTER_CRITICAL();
for (; count < USB_DEVICE_CONFIG_NUM; count++)
{
if ((NULL != s_UsbDeviceCommonClassStruct[count].handle) &&
(controllerId == s_UsbDeviceCommonClassStruct[count].controllerId))
{
*handle = &s_UsbDeviceCommonClassStruct[count];
OSA_EXIT_CRITICAL();
return kStatus_USB_Success;
}
}
OSA_EXIT_CRITICAL();
return kStatus_USB_InvalidParameter;
}
/*!
* @brief Get the device common class handle according to the device handle.
*
* This function gets the device common class handle according to the device handle.
*
* @param deviceHandle The device handle, got from the USB_DeviceInit.
* @param handle It is out parameter, is used to return pointer of the device common class handle to the
* caller.
*
* @retval kStatus_USB_Success Free device handle successfully.
* @retval kStatus_USB_InvalidParameter The common class can not be found.
*/
static usb_status_t USB_DeviceClassGetHandleByDeviceHandle(usb_device_handle deviceHandle,
usb_device_common_class_struct_t **handle)
{
uint32_t count = 0U;
OSA_SR_ALLOC();
OSA_ENTER_CRITICAL();
for (; count < USB_DEVICE_CONFIG_NUM; count++)
{
if (deviceHandle == s_UsbDeviceCommonClassStruct[count].handle)
{
*handle = &s_UsbDeviceCommonClassStruct[count];
OSA_EXIT_CRITICAL();
return kStatus_USB_Success;
}
}
OSA_EXIT_CRITICAL();
return kStatus_USB_InvalidParameter;
}
/*!
* @brief Get the device handle according to the controller id.
*
* This function gets the device handle according to the controller id.
*
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
* @param handle It is out parameter, is used to return pointer of the device handle to the caller.
*
* @retval kStatus_USB_Success Free device handle successfully.
* @retval kStatus_USB_InvalidParameter The device handle not be found.
*/
usb_status_t USB_DeviceClassGetDeviceHandle(uint8_t controllerId, usb_device_handle *handle)
{
uint32_t count = 0U;
OSA_SR_ALLOC();
OSA_ENTER_CRITICAL();
for (; count < USB_DEVICE_CONFIG_NUM; count++)
{
if ((NULL != s_UsbDeviceCommonClassStruct[count].handle) &&
(controllerId == s_UsbDeviceCommonClassStruct[count].controllerId))
{
*handle = s_UsbDeviceCommonClassStruct[count].handle;
OSA_EXIT_CRITICAL();
return kStatus_USB_Success;
}
}
OSA_EXIT_CRITICAL();
return kStatus_USB_InvalidParameter;
}
/*!
* @brief Handle the event passed to the class drivers.
*
* This function handles the event passed to the class drivers.
*
* @param handle The device handle, got from the USB_DeviceInit.
* @param event The event codes. Please refer to the enumeration usb_device_class_event_t.
* @param param The param type is determined by the event code.
*
* @return A USB error code or kStatus_USB_Success.
* @retval kStatus_USB_Success A valid request has been handled.
* @retval kStatus_USB_InvalidParameter The device handle not be found.
* @retval kStatus_USB_InvalidRequest The request is invalid, and the control pipe will be stalled by the caller.
*/
usb_status_t USB_DeviceClassEvent(usb_device_handle handle, usb_device_class_event_t event, void *param)
{
usb_device_common_class_struct_t *classHandle;
uint8_t mapIndex;
uint8_t classIndex;
usb_status_t errorReturn;
usb_status_t status = kStatus_USB_Error;
if (NULL == param)
{
return kStatus_USB_InvalidParameter;
}
/* Get the common class handle according to the device handle. */
errorReturn = USB_DeviceClassGetHandleByDeviceHandle(handle, &classHandle);
if (kStatus_USB_Success != errorReturn)
{
return kStatus_USB_InvalidParameter;
}
for (classIndex = 0U; classIndex < classHandle->configList->count; classIndex++)
{
for (mapIndex = 0U; mapIndex < (ARRAY_SIZE(s_UsbDeviceClassInterfaceMap) - 1U); mapIndex++)
{
if (s_UsbDeviceClassInterfaceMap[mapIndex].type ==
classHandle->configList->config[classIndex].classInfomation->type)
{
/* Call class event callback of supported class */
errorReturn = s_UsbDeviceClassInterfaceMap[mapIndex].classEventCallback(
(void *)classHandle->configList->config[classIndex].classHandle, event, param);
/* Return the error code kStatus_USB_InvalidRequest immediately, when a class returns
* kStatus_USB_InvalidRequest. */
if (kStatus_USB_InvalidRequest == errorReturn)
{
return kStatus_USB_InvalidRequest;
}
/* For composite device, it should return kStatus_USB_Success once a valid request has been handled */
if (kStatus_USB_Success == errorReturn)
{
status = kStatus_USB_Success;
}
break;
}
}
}
return status;
}
/*!
* @brief Handle the common class callback.
*
* This function handles the common class callback.
*
* @param handle The device handle, got from the USB_DeviceInit.
* @param event The event codes. Please refer to the enumeration usb_device_event_t.
* @param param The param type is determined by the event code.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceClassCallback(usb_device_handle handle, uint32_t event, void *param)
{
usb_device_common_class_struct_t *classHandle;
usb_status_t status;
/* Get the common class handle according to the device handle. */
status = USB_DeviceClassGetHandleByDeviceHandle(handle, &classHandle);
if (kStatus_USB_Success != status)
{
return status;
}
if ((uint32_t)kUSB_DeviceEventBusReset == event)
{
/* Initialize the control pipes */
(void)USB_DeviceControlPipeInit(handle, classHandle);
/* Notify the classes the USB bus reset signal detected. */
(void)USB_DeviceClassEvent(handle, kUSB_DeviceClassEventDeviceReset, classHandle);
}
/* Call the application device callback function. deviceCallback is from the second parameter of
USB_DeviceClassInit */
status = classHandle->configList->deviceCallback(handle, event, param);
return status;
}
/*!
* @brief Initialize the common class and the supported classes.
*
* This function is used to initialize the common class and the supported classes.
*
* @param[in] controllerId The controller id of the USB IP. Please refer to the enumeration #usb_controller_index_t.
* @param[in] configList The class configurations. The pointer must point to the global variable.
* Please refer to the structure #usb_device_class_config_list_struct_t.
* @param[out] handle It is out parameter, is used to return pointer of the device handle to the caller.
* The value of parameter is a pointer points the device handle, and this design is used to
* make simple device align with composite device. For composite device, there are many
* kinds of class handle, but there is only one device handle. So the handle points to
* a device instead of a class. And the class handle can be got from the
* #usb_device_class_config_struct_t::classHandle after the function successfully.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceClassInit(
uint8_t controllerId, /*!< [IN] Controller ID */
usb_device_class_config_list_struct_t *configList, /*!< [IN] Pointer to class configuration list */
usb_device_handle *handle /*!< [OUT] Pointer to the device handle */
)
{
usb_device_common_class_struct_t *classHandle;
usb_status_t error;
uint8_t mapIndex;
uint8_t classIndex;
if ((NULL == handle) || (NULL == configList) || ((usb_device_callback_t)NULL == configList->deviceCallback))
{
return kStatus_USB_InvalidParameter;
}
/* Allocate a common class driver handle. */
error = USB_DeviceClassAllocateHandle(controllerId, &classHandle);
if (kStatus_USB_Success != error)
{
return error;
}
/* Save the configuration list */
classHandle->configList = configList;
/* Initialize the device stack. */
error = USB_DeviceInit(controllerId, USB_DeviceClassCallback, &classHandle->handle);
if (kStatus_USB_Success != error)
{
(void)USB_DeviceDeinit(classHandle->handle);
(void)USB_DeviceClassFreeHandle(controllerId);
return error;
}
/* Initialize the all supported classes according to the configuration list. */
for (classIndex = 0U; classIndex < classHandle->configList->count; classIndex++)
{
for (mapIndex = 0U; mapIndex < (sizeof(s_UsbDeviceClassInterfaceMap) / sizeof(usb_device_class_map_t));
mapIndex++)
{
if (classHandle->configList->config[classIndex].classInfomation->type ==
s_UsbDeviceClassInterfaceMap[mapIndex].type)
{
(void)s_UsbDeviceClassInterfaceMap[mapIndex].classInit(
controllerId, &classHandle->configList->config[classIndex],
&classHandle->configList->config[classIndex].classHandle);
}
}
}
*handle = classHandle->handle;
return error;
}
/*!
* @brief De-initialize the common class and the supported classes.
*
* This function is used to de-initialize the common class and the supported classes.
*
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceClassDeinit(uint8_t controllerId /*!< [IN] Controller ID */
)
{
usb_device_common_class_struct_t *classHandle;
usb_status_t error;
uint8_t mapIndex;
uint8_t classIndex;
/* Get the common class handle according to the controller id. */
error = USB_DeviceClassGetHandleByControllerId(controllerId, &classHandle);
if (kStatus_USB_Success != error)
{
return error;
}
/* De-initialize the all supported classes according to the configuration list. */
for (classIndex = 0U; classIndex < classHandle->configList->count; classIndex++)
{
for (mapIndex = 0U; mapIndex < (sizeof(s_UsbDeviceClassInterfaceMap) / sizeof(usb_device_class_map_t));
mapIndex++)
{
if (classHandle->configList->config[classIndex].classInfomation->type ==
s_UsbDeviceClassInterfaceMap[mapIndex].type)
{
(void)s_UsbDeviceClassInterfaceMap[mapIndex].classDeinit(
classHandle->configList->config[classIndex].classHandle);
}
}
}
/* De-initialize the USB device stack. */
error = USB_DeviceDeinit(classHandle->handle);
if (kStatus_USB_Success == error)
{
/* Free the common class handle. */
(void)USB_DeviceClassFreeHandle(controllerId);
}
return error;
}
/*!
* @brief Get the USB bus speed.
*
* This function is used to get the USB bus speed.
*
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
* @param speed It is an OUT parameter, return current speed of the controller.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceClassGetSpeed(uint8_t controllerId, /*!< [IN] Controller ID */
uint8_t *speed /*!< [OUT] Current speed */
)
{
usb_device_common_class_struct_t *classHandle;
usb_status_t error;
/* Get the common class handle according to the controller id. */
error = USB_DeviceClassGetHandleByControllerId(controllerId, &classHandle);
if (kStatus_USB_Success != error)
{
return error;
}
/* Get the current speed. */
error = USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusSpeed, speed);
return error;
}
#if defined(USB_DEVICE_CONFIG_GET_SOF_COUNT) && (USB_DEVICE_CONFIG_GET_SOF_COUNT > 0U)
/*!
* @brief Get the USB SOF count.
*
* This function is used to get the USB SOF count.
*
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
* @param currentFrameCount It is an OUT parameter, return current sof count of the controller.
* HS: micro frame count, FS: frame count
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceClassGetCurrentFrameCount(uint8_t controllerId, /*!< [IN] Controller ID */
uint32_t *currentFrameCount /*!< [OUT] Current frame count */
)
{
usb_device_common_class_struct_t *classHandle;
usb_status_t error;
/* Get the common class handle according to the controller id. */
error = USB_DeviceClassGetHandleByControllerId(controllerId, &classHandle);
if (kStatus_USB_Success != error)
{
return error;
}
/* Get the current frame count. */
error = USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusGetCurrentFrameCount, currentFrameCount);
return error;
}
#endif /* USB_DEVICE_CONFIG_GET_SOF_COUNT */
#endif /* USB_DEVICE_CONFIG_NUM */

View File

@@ -0,0 +1,439 @@
/*
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
* Copyright 2016 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef __USB_DEVICE_CLASS_H__
#define __USB_DEVICE_CLASS_H__
/*!
* @addtogroup usb_device_class_driver
* @{
*/
/*******************************************************************************
* Definitions
******************************************************************************/
/*! @brief Macro to define class handle */
typedef void *class_handle_t;
/*! @brief Available class types. */
typedef enum _usb_usb_device_class_type
{
kUSB_DeviceClassTypeHid = 1U,
kUSB_DeviceClassTypeCdc,
kUSB_DeviceClassTypeMsc,
kUSB_DeviceClassTypeMtp,
kUSB_DeviceClassTypeAudio,
kUSB_DeviceClassTypePhdc,
kUSB_DeviceClassTypeVideo,
kUSB_DeviceClassTypePrinter,
kUSB_DeviceClassTypeDfu,
kUSB_DeviceClassTypeCcid,
} usb_device_class_type_t;
/*! @brief Available common class events. */
typedef enum _usb_device_class_event
{
kUSB_DeviceClassEventClassRequest = 1U,
kUSB_DeviceClassEventDeviceReset,
kUSB_DeviceClassEventSetConfiguration,
kUSB_DeviceClassEventSetInterface,
kUSB_DeviceClassEventSetEndpointHalt,
kUSB_DeviceClassEventClearEndpointHalt,
} usb_device_class_event_t;
/*!
* @brief Obtains the endpoint data structure.
*
* Define the endpoint data structure.
*
*/
typedef struct _usb_device_endpoint_struct
{
uint8_t endpointAddress; /*!< Endpoint address*/
uint8_t transferType; /*!< Endpoint transfer type*/
uint16_t maxPacketSize; /*!< Endpoint maximum packet size */
uint8_t interval; /*!< Endpoint interval*/
} usb_device_endpoint_struct_t;
/*!
* @brief Obtains the endpoint group.
*
* Structure representing endpoints and the number of endpoints that the user wants.
*
*/
typedef struct _usb_device_endpoint_list
{
uint8_t count; /*!< How many endpoints in current interface*/
usb_device_endpoint_struct_t *endpoint; /*!< Endpoint structure list*/
} usb_device_endpoint_list_t;
/*!
* @brief Obtains the interface list data structure.
*
* Structure representing an interface.
*
*/
typedef struct _usb_device_interface_struct
{
uint8_t alternateSetting; /*!< Alternate setting number*/
usb_device_endpoint_list_t endpointList; /*!< Endpoints of the interface*/
void *classSpecific; /*!< Class specific structure handle*/
} usb_device_interface_struct_t;
/*!
* @brief Obtains the interface data structure.
*
* Structure representing interface.
*
*/
typedef struct _usb_device_interfaces_struct
{
uint8_t classCode; /*!< Class code of the interface*/
uint8_t subclassCode; /*!< Subclass code of the interface*/
uint8_t protocolCode; /*!< Protocol code of the interface*/
uint8_t interfaceNumber; /*!< Interface number*/
usb_device_interface_struct_t *interface; /*!< Interface structure list*/
uint8_t count; /*!< Number of interfaces in the current interface*/
} usb_device_interfaces_struct_t;
/*!
* @brief Obtains the interface group.
*
* Structure representing how many interfaces in one class type.
*
*/
typedef struct _usb_device_interface_list
{
uint8_t count; /*!< Number of interfaces of the class*/
usb_device_interfaces_struct_t *interfaces; /*!< All interfaces*/
} usb_device_interface_list_t;
/*!
* @brief Obtains the class data structure.
*
* Structure representing how many configurations in one class type.
*
*/
typedef struct _usb_device_class_struct
{
usb_device_interface_list_t *interfaceList; /*!< Interfaces of the class*/
usb_device_class_type_t type; /*!< Class type*/
uint8_t configurations; /*!< Number of configurations of the class*/
} usb_device_class_struct_t;
/*callback function pointer structure for application to provide class parameters*/
typedef usb_status_t (*usb_device_class_callback_t)(class_handle_t classHandle,
uint32_t callbackEvent,
void *eventParam);
/*!
* @brief Obtains the device class information structure.
*
* Structure representing the device class information. This structure only can be stored in RAM space.
*
*/
typedef struct _usb_device_class_config_struct
{
usb_device_class_callback_t classCallback; /*!< Class callback function to handle the device status-related event
for the specified type of class*/
class_handle_t classHandle; /*!< The class handle of the class, filled by the common driver.*/
usb_device_class_struct_t *classInfomation; /*!< Detailed information of the class*/
} usb_device_class_config_struct_t;
/*!
* @brief Obtains the device class configuration structure.
*
* Structure representing the device class configuration information.
*
*/
typedef struct _usb_device_class_config_list_struct
{
usb_device_class_config_struct_t *config; /*!< Array of class configuration structures */
usb_device_callback_t deviceCallback; /*!< Device callback function */
uint8_t count; /*!< Number of class supported */
} usb_device_class_config_list_struct_t;
/*!
* @brief Obtains the control request structure.
*
* This structure is used to pass the control request information.
* The structure is used in following two cases.
* 1. Case one, the host wants to send data to the device in the control data stage: @n
* a. If a setup packet is received, the structure is used to pass the setup packet data and wants to get the
* buffer to receive data sent from the host.
* The field isSetup is 1.
* The length is the requested buffer length.
* The buffer is filled by the class or application by using the valid buffer address.
* The setup is the setup packet address.
* b. If the data received is sent by the host, the structure is used to pass the data buffer address and the
* data
* length sent by the host.
* In this way, the field isSetup is 0.
* The buffer is the address of the data sent from the host.
* The length is the received data length.
* The setup is the setup packet address. @n
* 2. Case two, the host wants to get data from the device in control data stage: @n
* If the setup packet is received, the structure is used to pass the setup packet data and wants to get the
* data buffer address to send data to the host.
* The field isSetup is 1.
* The length is the requested data length.
* The buffer is filled by the class or application by using the valid buffer address.
* The setup is the setup packet address.
*
*/
typedef struct _usb_device_control_request_struct
{
usb_setup_struct_t *setup; /*!< The pointer of the setup packet data. */
uint8_t *buffer; /*!< Pass the buffer address. */
uint32_t length; /*!< Pass the buffer length or requested length. */
uint8_t isSetup; /*!< Indicates whether a setup packet is received. */
} usb_device_control_request_struct_t;
/*! @brief Obtains the control get descriptor request common structure. */
typedef struct _usb_device_get_descriptor_common_struct
{
uint8_t *buffer; /*!< Pass the buffer address. */
uint32_t length; /*!< Pass the buffer length. */
} usb_device_get_descriptor_common_struct_t;
/*! @brief Obtains the control get device descriptor request structure. */
typedef struct _usb_device_get_device_descriptor_struct
{
uint8_t *buffer; /*!< Pass the buffer address. */
uint32_t length; /*!< Pass the buffer length. */
} usb_device_get_device_descriptor_struct_t;
/*! @brief Obtains the control get device qualifier descriptor request structure. */
typedef struct _usb_device_get_device_qualifier_descriptor_struct
{
uint8_t *buffer; /*!< Pass the buffer address. */
uint32_t length; /*!< Pass the buffer length. */
} usb_device_get_device_qualifier_descriptor_struct_t;
/*! @brief Obtains the control get configuration descriptor request structure. */
typedef struct _usb_device_get_configuration_descriptor_struct
{
uint8_t *buffer; /*!< Pass the buffer address. */
uint32_t length; /*!< Pass the buffer length. */
uint8_t configuration; /*!< The configuration number. */
} usb_device_get_configuration_descriptor_struct_t;
/*! @brief Obtains the control get bos descriptor request structure. */
typedef struct _usb_device_get_bos_descriptor_struct
{
uint8_t *buffer; /*!< Pass the buffer address. */
uint32_t length; /*!< Pass the buffer length. */
} usb_device_get_bos_descriptor_struct_t;
/*! @brief Obtains the control get string descriptor request structure. */
typedef struct _usb_device_get_string_descriptor_struct
{
uint8_t *buffer; /*!< Pass the buffer address. */
uint32_t length; /*!< Pass the buffer length. */
uint16_t languageId; /*!< Language ID. */
uint8_t stringIndex; /*!< String index. */
} usb_device_get_string_descriptor_struct_t;
/*! @brief Obtains the control get HID descriptor request structure. */
typedef struct _usb_device_get_hid_descriptor_struct
{
uint8_t *buffer; /*!< Pass the buffer address. */
uint32_t length; /*!< Pass the buffer length. */
uint8_t interfaceNumber; /*!< The interface number. */
} usb_device_get_hid_descriptor_struct_t;
/*! @brief Obtains the control get HID report descriptor request structure. */
typedef struct _usb_device_get_hid_report_descriptor_struct
{
uint8_t *buffer; /*!< Pass the buffer address. */
uint32_t length; /*!< Pass the buffer length. */
uint8_t interfaceNumber; /*!< The interface number. */
} usb_device_get_hid_report_descriptor_struct_t;
/*! @brief Obtains the control get HID physical descriptor request structure. */
typedef struct _usb_device_get_hid_physical_descriptor_struct
{
uint8_t *buffer; /*!< Pass the buffer address. */
uint32_t length; /*!< Pass the buffer length. */
uint8_t index; /*!< Physical index */
uint8_t interfaceNumber; /*!< The interface number. */
} usb_device_get_hid_physical_descriptor_struct_t;
/*! @brief Obtains the control get descriptor request common union. */
typedef union _usb_device_get_descriptor_common_union
{
usb_device_get_descriptor_common_struct_t commonDescriptor; /*!< Common structure. */
usb_device_get_device_descriptor_struct_t deviceDescriptor; /*!< The structure to get device descriptor. */
usb_device_get_device_qualifier_descriptor_struct_t
deviceQualifierDescriptor; /*!< The structure to get device qualifier descriptor. */
usb_device_get_configuration_descriptor_struct_t
configurationDescriptor; /*!< The structure to get configuration descriptor. */
usb_device_get_string_descriptor_struct_t stringDescriptor; /*!< The structure to get string descriptor. */
usb_device_get_hid_descriptor_struct_t hidDescriptor; /*!< The structure to get HID descriptor. */
usb_device_get_hid_report_descriptor_struct_t
hidReportDescriptor; /*!< The structure to get HID report descriptor. */
usb_device_get_hid_physical_descriptor_struct_t
hidPhysicalDescriptor; /*!< The structure to get HID physical descriptor. */
} usb_device_get_descriptor_common_union_t;
/*! @brief Define function type for class device instance initialization */
typedef usb_status_t (*usb_device_class_init_call_t)(uint8_t controllerId,
usb_device_class_config_struct_t *classConfig,
class_handle_t *classHandle);
/*! @brief Define function type for class device instance deinitialization, internal */
typedef usb_status_t (*usb_device_class_deinit_call_t)(class_handle_t handle);
/*! @brief Define function type for class device instance Event change */
typedef usb_status_t (*usb_device_class_event_callback_t)(void *classHandle, uint32_t event, void *param);
/*! @brief Define class driver interface structure. */
typedef struct _usb_device_class_map
{
usb_device_class_init_call_t classInit; /*!< Class driver initialization- entry of the class driver */
usb_device_class_deinit_call_t classDeinit; /*!< Class driver de-initialization*/
usb_device_class_event_callback_t classEventCallback; /*!< Class driver event callback*/
usb_device_class_type_t type; /*!< Class type*/
} usb_device_class_map_t;
/*! @brief Structure holding common class state information */
typedef struct _usb_device_common_class_struct
{
usb_device_handle handle; /*!< USB device handle*/
usb_device_class_config_list_struct_t *configList; /*!< USB device configure list*/
uint8_t *setupBuffer; /*!< Setup packet data buffer*/
uint16_t standardTranscationBuffer; /*!<
* This variable is used in:
* get status request
* get configuration request
* get interface request
* set interface request
* get sync frame request
*/
uint8_t controllerId; /*!< Controller ID*/
} usb_device_common_class_struct_t;
/*******************************************************************************
* API
******************************************************************************/
#if defined(__cplusplus)
extern "C" {
#endif
/*!
* @brief Initializes the common class and the supported classes.
*
* This function is used to initialize the common class and the supported classes.
*
* @param[in] controllerId The controller ID of the USB IP. See the enumeration #usb_controller_index_t.
* @param[in] configList The class configurations. The pointer must point to the global variable.
* See the structure #usb_device_class_config_list_struct_t.
* @param[out] handle A parameter used to return pointer of the device handle to the caller.
* The value of the parameter is a pointer to the device handle. This design is used to
* make a simple device align with the composite device. For the composite device, there are
* many
* kinds of class handles. However, there is only one device handle. Therefore, the handle
* points to
* a device instead of a class. The class handle can be received from the
* #usb_device_class_config_struct_t::classHandle after the function successfully.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceClassInit(uint8_t controllerId,
usb_device_class_config_list_struct_t *configList,
usb_device_handle *handle);
/*!
* @brief Deinitializes the common class and the supported classes.
*
* This function is used to deinitialize the common class and the supported classes.
*
* @param[in] controllerId The controller ID of the USB IP. See the enumeration #usb_controller_index_t.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceClassDeinit(uint8_t controllerId);
/*!
* @brief Gets the USB bus speed.
*
* This function is used to get the USB bus speed.
*
* @param[in] controllerId The controller ID of the USB IP. See the enumeration #usb_controller_index_t.
* @param[out] speed It is an OUT parameter, which returns the current speed of the controller.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceClassGetSpeed(uint8_t controllerId, uint8_t *speed);
/*!
* @brief Handles the event passed to the class drivers.
*
* This function handles the event passed to the class drivers.
*
* @param[in] handle The device handle received from the #USB_DeviceInit.
* @param[in] event The event codes. See the enumeration #usb_device_class_event_t.
* @param[in,out] param The parameter type is determined by the event code.
*
* @return A USB error code or kStatus_USB_Success.
* @retval kStatus_USB_Success A valid request has been handled.
* @retval kStatus_USB_InvalidParameter The device handle not be found.
* @retval kStatus_USB_InvalidRequest The request is invalid, and the control pipe is stalled by the caller.
*/
usb_status_t USB_DeviceClassEvent(usb_device_handle handle, usb_device_class_event_t event, void *param);
/*!
* @brief Handles the common class callback.
*
* This function handles the common class callback.
*
* @param[in] handle The device handle received from the #USB_DeviceInit.
* @param[in] event The event codes. See the enumeration #usb_device_event_t.
* @param[in,out] param The parameter type is determined by the event code.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceClassCallback(usb_device_handle handle, uint32_t event, void *param);
/*!
* @brief Gets the device handle according to the controller ID.
*
* This function gets the device handle according to the controller ID.
*
* @param[in] controllerId The controller ID of the USB IP. See the enumeration #usb_controller_index_t.
* @param[out] handle An out parameter used to return the pointer of the device handle to the caller.
*
* @retval kStatus_USB_Success Get device handle successfully.
* @retval kStatus_USB_InvalidParameter The device handle can't be found.
*/
usb_status_t USB_DeviceClassGetDeviceHandle(uint8_t controllerId, usb_device_handle *handle);
#if defined(USB_DEVICE_CONFIG_GET_SOF_COUNT) && (USB_DEVICE_CONFIG_GET_SOF_COUNT > 0U)
/*!
* @brief Get the USB SOF count.
*
* This function is used to get the USB SOF count.
*
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
* @param currentFrameCount It is an OUT parameter, return current sof count of the controller.
* HS: micro frame count, FS: frame count
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceClassGetCurrentFrameCount(uint8_t controllerId, /*!< [IN] Controller ID */
uint32_t *currentFrameCount /*!< [OUT] Current frame count */
);
#endif
#if defined(__cplusplus)
}
#endif
/*! @}*/
#endif /* __USB_DEVICE_CLASS_H__ */

View File

@@ -0,0 +1,658 @@
/*
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
* Copyright 2016 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef __USB_DEVICE_H__
#define __USB_DEVICE_H__
/*!
* @addtogroup usb_device_driver
* @{
*/
/*******************************************************************************
* Definitions
******************************************************************************/
/*! @brief Defines Get/Set status Types */
typedef enum _usb_device_status
{
kUSB_DeviceStatusTestMode = 1U, /*!< Test mode */
kUSB_DeviceStatusSpeed, /*!< Current speed */
kUSB_DeviceStatusOtg, /*!< OTG status */
kUSB_DeviceStatusDevice, /*!< Device status */
kUSB_DeviceStatusEndpoint, /*!< Endpoint state usb_device_endpoint_status_t */
kUSB_DeviceStatusDeviceState, /*!< Device state */
kUSB_DeviceStatusAddress, /*!< Device address */
kUSB_DeviceStatusSynchFrame, /*!< Current frame */
kUSB_DeviceStatusBus, /*!< Bus status */
kUSB_DeviceStatusBusSuspend, /*!< Bus suspend */
kUSB_DeviceStatusBusSleep, /*!< Bus suspend */
kUSB_DeviceStatusBusResume, /*!< Bus resume */
kUSB_DeviceStatusRemoteWakeup, /*!< Remote wakeup state */
kUSB_DeviceStatusBusSleepResume, /*!< Bus resume */
#if defined(USB_DEVICE_CONFIG_GET_SOF_COUNT) && (USB_DEVICE_CONFIG_GET_SOF_COUNT > 0U)
kUSB_DeviceStatusGetCurrentFrameCount, /*!< Get current frame count */
#endif
} usb_device_status_t;
/*! @brief Defines USB 2.0 device state */
typedef enum _usb_device_state
{
kUSB_DeviceStateConfigured = 0U, /*!< Device state, Configured*/
kUSB_DeviceStateAddress, /*!< Device state, Address*/
kUSB_DeviceStateDefault, /*!< Device state, Default*/
kUSB_DeviceStateAddressing, /*!< Device state, Address setting*/
kUSB_DeviceStateTestMode, /*!< Device state, Test mode*/
} usb_device_state_t;
/*! @brief Defines endpoint state */
typedef enum _usb_endpoint_status
{
kUSB_DeviceEndpointStateIdle = 0U, /*!< Endpoint state, idle*/
kUSB_DeviceEndpointStateStalled, /*!< Endpoint state, stalled*/
} usb_device_endpoint_status_t;
/*! @brief Control endpoint index */
#define USB_CONTROL_ENDPOINT (0U)
/*! @brief Control endpoint maxPacketSize */
#define USB_CONTROL_MAX_PACKET_SIZE (64U)
#if (USB_DEVICE_CONFIG_EHCI && (USB_CONTROL_MAX_PACKET_SIZE != (64U)))
#error For high speed, USB_CONTROL_MAX_PACKET_SIZE must be 64!!!
#endif
/*! @brief The setup packet size of USB control transfer. */
#define USB_SETUP_PACKET_SIZE (8U)
/*! @brief USB endpoint mask */
#define USB_ENDPOINT_NUMBER_MASK (0x0FU)
/*! @brief uninitialized value */
#define USB_UNINITIALIZED_VAL_32 (0xFFFFFFFFU)
/*! @brief the endpoint callback length of cancelled transfer */
#define USB_CANCELLED_TRANSFER_LENGTH (0xFFFFFFFFU)
/*! @brief invalid tranfer buffer addresss */
#define USB_INVALID_TRANSFER_BUFFER (0xFFFFFFFEU)
#if defined(USB_DEVICE_CONFIG_GET_SOF_COUNT) && (USB_DEVICE_CONFIG_GET_SOF_COUNT > 0U)
/* USB device IP3511 max frame count */
#define USB_DEVICE_IP3511_MAX_FRAME_COUNT (0x000007FFU)
/* USB device EHCI max frame count */
#define USB_DEVICE_EHCI_MAX_FRAME_COUNT (0x00003FFFU)
/* USB device EHCI max frame count */
#define USB_DEVICE_KHCI_MAX_FRAME_COUNT (0x000007FFU)
/*! @brief usb device controller max frame count */
#if ((defined(USB_DEVICE_CONFIG_KHCI)) && (USB_DEVICE_CONFIG_KHCI > 0U))
#define USB_DEVICE_MAX_FRAME_COUNT (USB_DEVICE_KHCI_MAX_FRAME_COUNT)
#elif (((defined(USB_DEVICE_CONFIG_LPCIP3511FS)) && (USB_DEVICE_CONFIG_LPCIP3511FS > 0U)) || \
((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U)))
#define USB_DEVICE_MAX_FRAME_COUNT (USB_DEVICE_IP3511_MAX_FRAME_COUNT)
#elif ((defined(USB_DEVICE_CONFIG_EHCI)) && (USB_DEVICE_CONFIG_EHCI > 0U))
#define USB_DEVICE_MAX_FRAME_COUNT (USB_DEVICE_EHCI_MAX_FRAME_COUNT)
#endif
#endif
/*! @brief Available common EVENT types in device callback */
typedef enum _usb_device_event
{
kUSB_DeviceEventBusReset = 1U, /*!< USB bus reset signal detected */
kUSB_DeviceEventSuspend, /*!< USB bus suspend signal detected */
kUSB_DeviceEventResume, /*!< USB bus resume signal detected. The resume signal is driven by itself or a host */
kUSB_DeviceEventSleeped, /*!< USB bus LPM suspend signal detected */
kUSB_DeviceEventLPMResume, /*!< USB bus LPM resume signal detected. The resume signal is driven by itself or a host
*/
kUSB_DeviceEventError, /*!< An error is happened in the bus. */
kUSB_DeviceEventDetach, /*!< USB device is disconnected from a host. */
kUSB_DeviceEventAttach, /*!< USB device is connected to a host. */
kUSB_DeviceEventSetConfiguration, /*!< Set configuration. */
kUSB_DeviceEventSetInterface, /*!< Set interface. */
kUSB_DeviceEventGetDeviceDescriptor, /*!< Get device descriptor. */
kUSB_DeviceEventGetConfigurationDescriptor, /*!< Get configuration descriptor. */
kUSB_DeviceEventGetStringDescriptor, /*!< Get string descriptor. */
kUSB_DeviceEventGetHidDescriptor, /*!< Get HID descriptor. */
kUSB_DeviceEventGetHidReportDescriptor, /*!< Get HID report descriptor. */
kUSB_DeviceEventGetHidPhysicalDescriptor, /*!< Get HID physical descriptor. */
kUSB_DeviceEventGetBOSDescriptor, /*!< Get configuration descriptor. */
kUSB_DeviceEventGetDeviceQualifierDescriptor, /*!< Get device qualifier descriptor. */
kUSB_DeviceEventVendorRequest, /*!< Vendor request. */
kUSB_DeviceEventSetRemoteWakeup, /*!< Enable or disable remote wakeup function. */
kUSB_DeviceEventGetConfiguration, /*!< Get current configuration index */
kUSB_DeviceEventGetInterface, /*!< Get current interface alternate setting value */
kUSB_DeviceEventSetBHNPEnable,
#if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
kUSB_DeviceEventDcdDetectionfinished, /*!< The DCD detection finished */
#endif
} usb_device_event_t;
/*! @brief Endpoint callback message structure */
typedef struct _usb_device_endpoint_callback_message_struct
{
uint8_t *buffer; /*!< Transferred buffer */
uint32_t length; /*!< Transferred data length */
uint8_t isSetup; /*!< Is in a setup phase */
} usb_device_endpoint_callback_message_struct_t;
/*!
* @brief Endpoint callback function typedef.
*
* This callback function is used to notify the upper layer what the transfer result is.
* This callback pointer is passed when a specified endpoint is initialized by calling API #USB_DeviceInitEndpoint.
*
* @param handle The device handle. It equals to the value returned from #USB_DeviceInit.
* @param message The result of a transfer, which includes transfer buffer, transfer length, and whether is in a
* setup phase.
* phase for control pipe.
* @param callbackParam The parameter for this callback. It is same with
* usb_device_endpoint_callback_struct_t::callbackParam.
*
* @return A USB error code or kStatus_USB_Success.
*/
typedef usb_status_t (*usb_device_endpoint_callback_t)(usb_device_handle handle,
usb_device_endpoint_callback_message_struct_t *message,
void *callbackParam);
/*!
* @brief Device callback function typedef.
*
* This callback function is used to notify the upper layer that the device status has changed.
* This callback pointer is passed by calling API #USB_DeviceInit.
*
* @param handle The device handle. It equals the value returned from #USB_DeviceInit.
* @param callbackEvent The callback event type. See enumeration #usb_device_event_t.
* @param eventParam The event parameter for this callback. The parameter type is determined by the callback event.
*
* @return A USB error code or kStatus_USB_Success.
*/
typedef usb_status_t (*usb_device_callback_t)(usb_device_handle handle, uint32_t callbackEvent, void *eventParam);
/*! @brief Endpoint callback structure */
typedef struct _usb_device_endpoint_callback_struct
{
usb_device_endpoint_callback_t callbackFn; /*!< Endpoint callback function*/
void *callbackParam; /*!< Parameter for callback function*/
uint8_t isBusy;
} usb_device_endpoint_callback_struct_t;
/*! @brief Endpoint initialization structure */
typedef struct _usb_device_endpoint_init_struct
{
uint16_t maxPacketSize; /*!< Endpoint maximum packet size */
uint8_t endpointAddress; /*!< Endpoint address*/
uint8_t transferType; /*!< Endpoint transfer type*/
uint8_t zlt; /*!< ZLT flag*/
uint8_t interval; /*!< Endpoint interval*/
} usb_device_endpoint_init_struct_t;
/*! @brief Endpoint status structure */
typedef struct _usb_device_endpoint_status_struct
{
uint8_t endpointAddress; /*!< Endpoint address */
uint16_t endpointStatus; /*!< Endpoint status : idle or stalled */
} usb_device_endpoint_status_struct_t;
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus*/
/*!
* @name USB device APIs
* @{
*/
/*******************************************************************************
* API
******************************************************************************/
/*!
* @brief Initializes the USB device stack.
*
* This function initializes the USB device module specified by the controllerId.
*
* @param[in] controllerId The controller ID of the USB IP. See the enumeration #usb_controller_index_t.
* @param[in] deviceCallback Function pointer of the device callback.
* @param[out] handle It is an out parameter used to return the pointer of the device handle to the caller.
*
* @retval kStatus_USB_Success The device is initialized successfully.
* @retval kStatus_USB_InvalidHandle The handle is a NULL pointer.
* @retval kStatus_USB_Busy Cannot allocate a device handle.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller according to the controller id.
* @retval kStatus_USB_InvalidControllerInterface The controller driver interfaces is invalid. There is an empty
* interface entity.
* @retval kStatus_USB_Error The macro USB_DEVICE_CONFIG_ENDPOINTS is more than the IP's endpoint number.
* Or, the device has been initialized.
* Or, the mutex or message queue is created failed.
*/
extern usb_status_t USB_DeviceInit(uint8_t controllerId,
usb_device_callback_t deviceCallback,
usb_device_handle *handle);
/*!
* @brief Enables the device functionality.
*
* The function enables the device functionality, so that the device can be recognized by the host when the device
* detects that it has been connected to a host.
*
* @param[in] handle The device handle got from #USB_DeviceInit.
*
* @retval kStatus_USB_Success The device is run successfully.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
* @retval kStatus_USB_InvalidHandle The device handle is a NULL pointer. Or the controller handle is invalid.
*
*/
extern usb_status_t USB_DeviceRun(usb_device_handle handle);
/*!
* @brief Disables the device functionality.
*
* The function disables the device functionality. After this function called, even if the device is detached to the
* host,
* it can't work.
*
* @param[in] handle The device handle received from #USB_DeviceInit.
*
* @retval kStatus_USB_Success The device is stopped successfully.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
* @retval kStatus_USB_InvalidHandle The device handle is a NULL pointer or the controller handle is invalid.
*/
extern usb_status_t USB_DeviceStop(usb_device_handle handle);
/*!
* @brief De-initializes the device controller.
*
* The function de-initializes the device controller specified by the handle.
*
* @param[in] handle The device handle got from #USB_DeviceInit.
*
* @retval kStatus_USB_Success The device is stopped successfully.
* @retval kStatus_USB_InvalidHandle The device handle is a NULL pointer or the controller handle is invalid.
*/
extern usb_status_t USB_DeviceDeinit(usb_device_handle handle);
/*!
* @brief Sends data through a specified endpoint.
*
* The function is used to send data through a specified endpoint.
*
* @param[in] handle The device handle got from #USB_DeviceInit.
* @param[in] endpointAddress Endpoint index.
* @param[in] buffer The memory address to hold the data need to be sent. The function is not reentrant.
* @param[in] length The data length need to be sent.
*
* @retval kStatus_USB_Success The send request is sent successfully.
* @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
* @retval kStatus_USB_Busy Cannot allocate DTDS for current transfer in EHCI driver.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
* @retval kStatus_USB_Error The device is doing reset.
*
* @note The return value indicates whether the sending request is successful or not. The transfer done is notified by
* the
* corresponding callback function.
* Currently, only one transfer request can be supported for one specific endpoint.
* If there is a specific requirement to support multiple transfer requests for one specific endpoint, the application
* should implement a queue on the application level.
* The subsequent transfer can begin only when the previous transfer is done (get notification through the endpoint
* callback).
*/
extern usb_status_t USB_DeviceSendRequest(usb_device_handle handle,
uint8_t endpointAddress,
uint8_t *buffer,
uint32_t length);
/*!
* @brief Receives data through a specified endpoint.
*
* The function is used to receive data through a specified endpoint. The function is not reentrant.
*
* @param[in] handle The device handle got from #USB_DeviceInit.
* @param[in] endpointAddress Endpoint index.
* @param[in] buffer The memory address to save the received data.
* @param[in] length The data length want to be received.
*
* @retval kStatus_USB_Success The receive request is sent successfully.
* @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
* @retval kStatus_USB_Busy Cannot allocate DTDS for current transfer in EHCI driver.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
* @retval kStatus_USB_Error The device is doing reset.
*
* @note The return value indicates whether the receiving request is successful or not. The transfer done is notified by
* the
* corresponding callback function.
* Currently, only one transfer request can be supported for one specific endpoint.
* If there is a specific requirement to support multiple transfer requests for one specific endpoint, the application
* should implement a queue on the application level.
* The subsequent transfer can begin only when the previous transfer is done (get notification through the endpoint
* callback).
*/
extern usb_status_t USB_DeviceRecvRequest(usb_device_handle handle,
uint8_t endpointAddress,
uint8_t *buffer,
uint32_t length);
/*!
* @brief Cancels the pending transfer in a specified endpoint.
*
* The function is used to cancel the pending transfer in a specified endpoint.
*
* @param[in] handle The device handle got from #USB_DeviceInit.
* @param[in] endpointAddress Endpoint address, bit7 is the direction of endpoint, 1U - IN, and 0U - OUT.
*
* @retval kStatus_USB_Success The transfer is cancelled.
* @retval kStatus_USB_InvalidHandle The handle is a NULL pointer or the controller handle is invalid.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
*/
extern usb_status_t USB_DeviceCancel(usb_device_handle handle, uint8_t endpointAddress);
/*!
* @brief Initializes a specified endpoint.
*
* The function is used to initialize a specified endpoint. The corresponding endpoint callback is also initialized.
*
* @param[in] handle The device handle received from #USB_DeviceInit.
* @param[in] epInit Endpoint initialization structure. See the structure usb_device_endpoint_init_struct_t.
* @param[in] epCallback Endpoint callback structure. See the structure
* usb_device_endpoint_callback_struct_t.
*
* @retval kStatus_USB_Success The endpoint is initialized successfully.
* @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
* @retval kStatus_USB_InvalidParameter The epInit or epCallback is NULL pointer. Or the endpoint number is
* more than USB_DEVICE_CONFIG_ENDPOINTS.
* @retval kStatus_USB_Busy The endpoint is busy in EHCI driver.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
*/
extern usb_status_t USB_DeviceInitEndpoint(usb_device_handle handle,
usb_device_endpoint_init_struct_t *epInit,
usb_device_endpoint_callback_struct_t *epCallback);
/*!
* @brief Deinitializes a specified endpoint.
*
* The function is used to deinitializes a specified endpoint.
*
* @param[in] handle The device handle got from #USB_DeviceInit.
* @param[in] endpointAddress Endpoint address, bit7 is the direction of endpoint, 1U - IN, and 0U - OUT.
*
* @retval kStatus_USB_Success The endpoint is de-initialized successfully.
* @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
* @retval kStatus_USB_InvalidParameter The endpoint number is more than USB_DEVICE_CONFIG_ENDPOINTS.
* @retval kStatus_USB_Busy The endpoint is busy in EHCI driver.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
*/
extern usb_status_t USB_DeviceDeinitEndpoint(usb_device_handle handle, uint8_t endpointAddress);
/*!
* @brief Stalls a specified endpoint.
*
* The function is used to stall a specified endpoint.
*
* @param[in] handle The device handle received from #USB_DeviceInit.
* @param[in] endpointAddress Endpoint address, bit7 is the direction of endpoint, 1U - IN, and 0U - OUT.
*
* @retval kStatus_USB_Success The endpoint is stalled successfully.
* @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
* @retval kStatus_USB_InvalidParameter The endpoint number is more than USB_DEVICE_CONFIG_ENDPOINTS.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
*/
extern usb_status_t USB_DeviceStallEndpoint(usb_device_handle handle, uint8_t endpointAddress);
/*!
* @brief Un-stall a specified endpoint.
*
* The function is used to unstall a specified endpoint.
*
* @param[in] handle The device handle received from #USB_DeviceInit.
* @param[in] endpointAddress Endpoint address, bit7 is the direction of endpoint, 1U - IN, and 0U - OUT.
*
* @retval kStatus_USB_Success The endpoint is un-stalled successfully.
* @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
* @retval kStatus_USB_InvalidParameter The endpoint number is more than USB_DEVICE_CONFIG_ENDPOINTS.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
*/
extern usb_status_t USB_DeviceUnstallEndpoint(usb_device_handle handle, uint8_t endpointAddress);
/*!
* @brief Gets the status of the selected item.
*
* The function is used to get the status of the selected item.
*
* @param[in] handle The device handle got from #USB_DeviceInit.
* @param[in] type The selected item. See the structure #usb_device_status_t.
* @param[out] param The parameter type is determined by the selected item.
*
* @retval kStatus_USB_Success Get status successfully.
* @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
* @retval kStatus_USB_InvalidParameter The parameter is NULL pointer.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
* @retval kStatus_USB_Error Unsupported type.
*/
extern usb_status_t USB_DeviceGetStatus(usb_device_handle handle, usb_device_status_t type, void *param);
/*!
* @brief Sets the status of the selected item.
*
* The function is used to set the status of the selected item.
*
* @param[in] handle The device handle got from #USB_DeviceInit.
* @param[in] type The selected item. See the structure #usb_device_status_t.
* @param[in] param The parameter type is determined by the selected item.
*
* @retval kStatus_USB_Success Set status successfully.
* @retval kStatus_USB_InvalidHandle The handle is a NULL pointer. Or the controller handle is invalid.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
* @retval kStatus_USB_Error Unsupported type or the parameter is NULL pointer.
*/
extern usb_status_t USB_DeviceSetStatus(usb_device_handle handle, usb_device_status_t type, void *param);
#if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
/*!
* @brief Enable the device dcd module.
*
* The function enable the device dcd module.
*
* @param[in] handle The device handle got from #USB_DeviceInit.
*
* @retval kStatus_USB_Success The device could run.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
* @retval kStatus_USB_InvalidHandle The device handle is a NULL pointer. Or the controller handle is invalid.
*
*/
extern usb_status_t USB_DeviceDcdEnable(usb_device_handle handle);
/*!
* @brief Disable the device dcd module.
*
* The function disable the device dcd module.
*
* @param[in] handle The device handle got from #USB_DeviceInit.
*
* @retval kStatus_USB_Success The dcd is reset and stopped.
* @retval kStatus_USB_ControllerNotFound Cannot find the controller.
* @retval kStatus_USB_InvalidHandle The device handle is a NULL pointer or the controller handle is invalid.
*
*/
extern usb_status_t USB_DeviceDcdDisable(usb_device_handle handle);
#endif
#if ((defined(USB_DEVICE_CONFIG_USE_TASK)) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
/*!
* @brief Device task function.
*
* The function is used to handle the controller message.
* This function should not be called in the application directly.
*
* @param[in] deviceHandle The device handle got from #USB_DeviceInit.
*/
extern void USB_DeviceTaskFunction(void *deviceHandle);
#endif
#if ((defined(USB_DEVICE_CONFIG_KHCI)) && (USB_DEVICE_CONFIG_KHCI > 0U))
#if ((defined(USB_DEVICE_CONFIG_USE_TASK)) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
/*!
* @brief Device KHCI task function.
*
* The function is used to handle the KHCI controller message.
* In the bare metal environment, this function should be called periodically in the main function.
* In the RTOS environment, this function should be used as a function entry to create a task.
*
* @param[in] deviceHandle The device handle got from #USB_DeviceInit.
*/
#define USB_DeviceKhciTaskFunction(deviceHandle) USB_DeviceTaskFunction(deviceHandle)
#endif
#endif
#if ((defined(USB_DEVICE_CONFIG_EHCI)) && (USB_DEVICE_CONFIG_EHCI > 0U))
#if ((defined(USB_DEVICE_CONFIG_USE_TASK)) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
/*!
* @brief Device EHCI task function.
*
* The function is used to handle the EHCI controller message.
* In the bare metal environment, this function should be called periodically in the main function.
* In the RTOS environment, this function should be used as a function entry to create a task.
*
* @param[in] deviceHandle The device handle got from #USB_DeviceInit.
*/
#define USB_DeviceEhciTaskFunction(deviceHandle) USB_DeviceTaskFunction(deviceHandle)
#endif
#if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
#if (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
/*!
* @brief Device ehci DCD ISR function.
*
* The function is the ehci DCD interrupt service routine.
*
* @param[in] deviceHandle The device handle got from #USB_DeviceInit.
*/
extern void USB_DeviceEhciIsrHSDCDFunction(void *deviceHandle);
#endif
#endif
#endif
#if (((defined(USB_DEVICE_CONFIG_LPCIP3511FS)) && (USB_DEVICE_CONFIG_LPCIP3511FS > 0U)) || \
((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U)))
#if ((defined(USB_DEVICE_CONFIG_USE_TASK)) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
/*!
* @brief Device LPC ip3511 controller task function.
*
* The function is used to handle the LPC ip3511 controller message.
* In the bare metal environment, this function should be called periodically in the main function.
* In the RTOS environment, this function should be used as a function entry to create a task.
*
* @param[in] deviceHandle The device handle got from #USB_DeviceInit.
*/
#define USB_DeviceLpcIp3511TaskFunction(deviceHandle) USB_DeviceTaskFunction(deviceHandle)
#endif
#if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
#if (defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
/*!
* @brief Device IP3511 DCD ISR function.
*
* The function is the IP3511 DCD interrupt service routine.
*
* @param[in] deviceHandle The device handle got from #USB_DeviceInit.
*/
extern void USB_DeviceLpcIp3511IsrDCDFunction(void *deviceHandle);
#endif
#endif
#endif
#if ((defined(USB_DEVICE_CONFIG_KHCI)) && (USB_DEVICE_CONFIG_KHCI > 0U))
/*!
* @brief Device KHCI ISR function.
*
* The function is the KHCI interrupt service routine.
*
* @param[in] deviceHandle The device handle got from #USB_DeviceInit.
*/
extern void USB_DeviceKhciIsrFunction(void *deviceHandle);
#if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
#if (defined(FSL_FEATURE_SOC_USBDCD_COUNT) && (FSL_FEATURE_SOC_USBDCD_COUNT > 0U))
#if 0U /* it is not implemented yet */
/*!
* @brief Device KHCI DCD ISR function.
*
* The function is the KHCI DCD interrupt service routine.
*
* @param[in] deviceHandle The device handle got from #USB_DeviceInit.
*/
extern void USB_DeviceDcdIsrFunction(void *deviceHandle);
#endif
#endif
#endif
#endif
#if ((defined(USB_DEVICE_CONFIG_EHCI)) && (USB_DEVICE_CONFIG_EHCI > 0U))
/*!
* @brief Device EHCI ISR function.
*
* The function is the EHCI interrupt service routine.
*
* @param[in] deviceHandle The device handle got from #USB_DeviceInit.
*/
extern void USB_DeviceEhciIsrFunction(void *deviceHandle);
#endif
#if (((defined(USB_DEVICE_CONFIG_LPCIP3511FS)) && (USB_DEVICE_CONFIG_LPCIP3511FS > 0U)) || \
((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U)))
/*!
* @brief Device LPC USB ISR function.
*
* The function is the LPC USB interrupt service routine.
*
* @param[in] deviceHandle The device handle got from #USB_DeviceInit.
*/
extern void USB_DeviceLpcIp3511IsrFunction(void *deviceHandle);
#endif
#if (((defined(USB_DEVICE_CONFIG_DWC3)) && (USB_DEVICE_CONFIG_DWC3 > 0U)) || \
((defined(USB_DEVICE_CONFIG_DWC3)) && (USB_DEVICE_CONFIG_DWC3 > 0U)))
/*!
* @brief Device USB DWC3 ISR function.
*
* The function is the USB interrupt service routine.
*
* @param[in] deviceHandle The device handle got from #USB_DeviceInit.
*/
extern void USB_DeviceDwc3IsrFunction(void *deviceHandle);
#endif
/*!
* @brief Gets the device stack version function.
*
* The function is used to get the device stack version.
*
* @param[out] version The version structure pointer to keep the device stack version.
*
*/
extern void USB_DeviceGetVersion(uint32_t *version);
#if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U)) || \
(((defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U)) && \
(defined(FSL_FEATURE_SOC_USB_ANALOG_COUNT) && (FSL_FEATURE_SOC_USB_ANALOG_COUNT > 0U))))
/*!
* @brief Update the hardware tick.
*
* The function is used to update the hardware tick.
*
* @param[in] handle The device handle got from #USB_DeviceInit.
* @param[in] tick Current hardware tick(uint is ms).
*
*/
extern usb_status_t USB_DeviceUpdateHwTick(usb_device_handle handle, uint64_t tick);
#endif
/*! @}*/
#if defined(__cplusplus)
}
#endif /* __cplusplus*/
/*! @}*/
#endif /* __USB_DEVICE_H__ */

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,286 @@
/*
* Copyright (c) 2016, Freescale Semiconductor, Inc.
* Copyright 2016 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef __USB_DEVICE_LPC3511IP_H__
#define __USB_DEVICE_LPC3511IP_H__
#include "fsl_device_registers.h"
/*!
* @addtogroup usb_device_controller_lpcip3511_driver
* @{
*/
/*******************************************************************************
* Definitions
******************************************************************************/
/* For bulk out endpoint in high speed mode, use long length data transfer to decrease the Ping packet count to increase bulk bandwidth */
/* The bigger this macro's value is, the higher bandwidth bulk out endpoint has. However, you need to set a reasonable value for this macro based on USB RAM size of Soc. If this macro's value is too big, link may be failed. */
/* Note that please set this value as integral multiple of 512U */
#if (((defined(USB_DEVICE_CONFIG_MSC)) && (USB_DEVICE_CONFIG_MSC > 0U)) && ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U)))
#define USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX (0U)
#endif
/*! @brief The reserved buffer size, the buffer is for the memory copy if the application transfer buffer is
((not 64 bytes alignment) || (not in the same 64K ram) || (HS && OUT && not multiple of 4)) */
#if ((defined(USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX)) && (USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX > 0U))
/* if use USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX (>0U), need to increase the reserved buffer size */
#define USB_DEVICE_IP3511_ENDPOINT_RESERVED_BUFFER_SIZE ((5U + ((USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX - 512U) / 512U)) * 1024U)
#else
#define USB_DEVICE_IP3511_ENDPOINT_RESERVED_BUFFER_SIZE (5U * 1024U)
#endif
/*! @brief Use one bit to represent one reserved 64 bytes to allocate the buffer by uint of 64 bytes. */
#define USB_DEVICE_IP3511_BITS_FOR_RESERVED_BUFFER ((USB_DEVICE_IP3511_ENDPOINT_RESERVED_BUFFER_SIZE + 63U) / 64U)
/*! @brief How many IPs support the reserved buffer */
#define USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY (USB_DEVICE_CONFIG_LPCIP3511FS + USB_DEVICE_CONFIG_LPCIP3511HS)
/*! @brief Prime all the double endpoint buffer at the same time, if the transfer length is larger than max packet size.
*/
#define USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE (1U)
#if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
#define USB_LPC3511IP_Type USBHSD_Type
#define USB_DEVICE_IP3511_ENDPOINTS_NUM FSL_FEATURE_USBHSD_EP_NUM
#else
#define USB_LPC3511IP_Type USB_Type
#define USB_DEVICE_IP3511_ENDPOINTS_NUM FSL_FEATURE_USB_EP_NUM
#endif
/* for out endpoint,only use buffer toggle, disable prime double buffer at the same time*/
/*host send data less than maxpacket size and in endpoint prime length more more than maxpacketsize, there will be state
* mismtach*/
#if USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE
#define USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER (1U)
#else
#define USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER (0U)
#endif
#define USB_DEVICE_IP3511HS_LPM_ADPPROBE_ATTACH_DEBOUNCE_COUNT (3)
/* if FSL_FEATURE_USBHSD_HAS_EXIT_HS_ISSUE is true:
* Enable this macro to exit HS mode automatically if the user case is:
* host and device keep cable connected, and host turn off vbus to simulate detachment.
* If user disconnects the cable, there is no issue and don't need enable this macro.
* There is one delay in the isr if enable this macro.
*/
#define USB_DEVICE_IP3511HS_FORCE_EXIT_HS_MODE_ENABLE (0u)
/*! @brief Endpoint state structure */
typedef struct _usb_device_lpc3511ip_endpoint_state_struct
{
uint8_t *transferBuffer; /*!< Address of buffer containing the data to be transmitted */
uint32_t transferLength; /*!< Length of data to transmit. */
uint32_t transferDone; /*!< The data length has been transferred*/
uint32_t transferPrimedLength; /*!< it may larger than transferLength, because the primed length may larger than the
transaction length. */
uint8_t *epPacketBuffer; /*!< The max packet buffer for copying*/
union
{
uint32_t state; /*!< The state of the endpoint */
struct
{
uint32_t maxPacketSize : 11U; /*!< The maximum packet size of the endpoint */
uint32_t stalled : 1U; /*!< The endpoint is stalled or not */
uint32_t transferring : 1U; /*!< The endpoint is transferring */
uint32_t zlt : 1U; /*!< zlt flag */
uint32_t stallPrimed : 1U;
uint32_t epPacketCopyed : 1U; /*!< whether use the copy buffer */
uint32_t epControlDefault : 5u; /*!< The EP command/status 26~30 bits */
uint32_t doubleBufferBusy : 2U; /*!< How many buffers are primed, for control endpoint it is not used */
uint32_t producerOdd : 1U; /*!< When priming one transaction, prime to this endpoint buffer */
uint32_t consumerOdd : 1U; /*!< When transaction is done, read result from this endpoint buffer */
uint32_t endpointType : 2U;
uint32_t reserved1 : 5U;
} stateBitField;
} stateUnion;
union
{
uint16_t epBufferStatus;
/* If double buff is disable, only epBufferStatusUnion[0] is used;
For control endpoint, only epBufferStatusUnion[0] is used. */
struct
{
uint16_t transactionLength : 15U;
uint16_t epPacketCopyed : 1U;
} epBufferStatusField;
} epBufferStatusUnion[2];
} usb_device_lpc3511ip_endpoint_state_struct_t;
/*! @brief LPC USB controller (IP3511) state structure */
typedef struct _usb_device_lpc3511ip_state_struct
{
/*!< control data buffer, must align with 64 */
uint8_t *controlData;
/*!< 8 bytes' setup data, must align with 64 */
uint8_t *setupData;
/*!< 4 bytes for zero length transaction, must align with 64 */
uint8_t *zeroTransactionData;
/* Endpoint state structures */
usb_device_lpc3511ip_endpoint_state_struct_t endpointState[(USB_DEVICE_IP3511_ENDPOINTS_NUM * 2)];
usb_device_handle deviceHandle; /*!< (4 bytes) Device handle used to identify the device object belongs to */
USB_LPC3511IP_Type *registerBase; /*!< (4 bytes) ip base address */
volatile uint32_t *epCommandStatusList; /* endpoint list */
#if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U)) && \
(defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
void *dcdHandle; /*!< Dcd handle used to identify the device object belongs to */
#endif
uint8_t controllerId; /*!< Controller ID */
uint8_t isResetting; /*!< Is doing device reset or not */
uint8_t deviceSpeed; /*!< some controller support the HS */
#if ((defined(USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY > 0U))
uint8_t *epReservedBuffer;
uint8_t epReservedBufferBits[(USB_DEVICE_IP3511_BITS_FOR_RESERVED_BUFFER + 7) / 8];
#endif
#if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
uint8_t controllerSpeed;
#endif
#if (defined(USB_DEVICE_CONFIG_DETACH_ENABLE) && (USB_DEVICE_CONFIG_DETACH_ENABLE))
uint8_t deviceState; /*!< Is device attached,1 attached,0 detached */
#endif
#if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
#if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
uint8_t lpmRemoteWakeUp;
#endif
#endif
#if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
#if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
uint8_t hsInterruptIssue;
#endif
#endif
} usb_device_lpc3511ip_state_struct_t;
/*!
* @name USB device controller (IP3511) functions
* @{
*/
/*******************************************************************************
* API
******************************************************************************/
#if defined(__cplusplus)
extern "C" {
#endif
/*!
* @brief Initializes the USB device controller instance.
*
* This function initializes the USB device controller module specified by the controllerId.
*
* @param[in] controllerId The controller ID of the USB IP. See the enumeration type usb_controller_index_t.
* @param[in] handle Pointer of the device handle used to identify the device object belongs to.
* @param[out] controllerHandle An out parameter used to return the pointer of the device controller handle to the
* caller.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceLpc3511IpInit(uint8_t controllerId,
usb_device_handle handle,
usb_device_controller_handle *controllerHandle);
/*!
* @brief Deinitializes the USB device controller instance.
*
* This function deinitializes the USB device controller module.
*
* @param[in] controllerHandle Pointer of the device controller handle.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceLpc3511IpDeinit(usb_device_controller_handle controllerHandle);
/*!
* @brief Sends data through a specified endpoint.
*
* This function sends data through a specified endpoint.
*
* @param[in] controllerHandle Pointer of the device controller handle.
* @param[in] endpointAddress Endpoint index.
* @param[in] buffer The memory address to hold the data need to be sent.
* @param[in] length The data length need to be sent.
*
* @return A USB error code or kStatus_USB_Success.
*
* @note The return value indicates whether the sending request is successful or not. The transfer completion is
* notified by the
* corresponding callback function.
* Currently, only one transfer request can be supported for a specific endpoint.
* If there is a specific requirement to support multiple transfer requests for a specific endpoint, the application
* should implement a queue in the application level.
* The subsequent transfer can begin only when the previous transfer is done (a notification is obtained through the
* endpoint
* callback).
*/
usb_status_t USB_DeviceLpc3511IpSend(usb_device_controller_handle controllerHandle,
uint8_t endpointAddress,
uint8_t *buffer,
uint32_t length);
/*!
* @brief Receives data through a specified endpoint.
*
* This function receives data through a specified endpoint.
*
* @param[in] controllerHandle Pointer of the device controller handle.
* @param[in] endpointAddress Endpoint index.
* @param[in] buffer The memory address to save the received data.
* @param[in] length The data length to be received.
*
* @return A USB error code or kStatus_USB_Success.
*
* @note The return value indicates whether the receiving request is successful or not. The transfer completion is
* notified by the
* corresponding callback function.
* Currently, only one transfer request can be supported for a specific endpoint.
* If there is a specific requirement to support multiple transfer requests for a specific endpoint, the application
* should implement a queue in the application level.
* The subsequent transfer can begin only when the previous transfer is done (a notification is obtained through the
* endpoint
* callback).
*/
usb_status_t USB_DeviceLpc3511IpRecv(usb_device_controller_handle controllerHandle,
uint8_t endpointAddress,
uint8_t *buffer,
uint32_t length);
/*!
* @brief Cancels the pending transfer in a specified endpoint.
*
* The function is used to cancel the pending transfer in a specified endpoint.
*
* @param[in] controllerHandle ointer of the device controller handle.
* @param[in] ep Endpoint address, bit7 is the direction of endpoint, 1U - IN, abd 0U - OUT.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceLpc3511IpCancel(usb_device_controller_handle controllerHandle, uint8_t ep);
/*!
* @brief Controls the status of the selected item.
*
* The function is used to control the status of the selected item.
*
* @param[in] controllerHandle Pointer of the device controller handle.
* @param[in] type The selected item. Please refer to enumeration type usb_device_control_type_t.
* @param[in,out] param The parameter type is determined by the selected item.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceLpc3511IpControl(usb_device_controller_handle controllerHandle,
usb_device_control_type_t type,
void *param);
/*! @} */
#if defined(__cplusplus)
}
#endif
/*! @} */
#endif /* __USB_DEVICE_LPC3511IP_H__ */

View File

@@ -0,0 +1,964 @@
/*
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
* Copyright 2016 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "usb_device_config.h"
#include "usb.h"
#include "usb_device.h"
#include "usb_device_dci.h"
#include "usb_device_class.h"
#include "usb_device_ch9.h"
#if ((defined(USB_DEVICE_CONFIG_NUM)) && (USB_DEVICE_CONFIG_NUM > 0U))
/*******************************************************************************
* Definitions
******************************************************************************/
/*!
* @brief Standard request callback function typedef.
*
* This function is used to handle the standard request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @return A USB error code or kStatus_USB_Success.
*/
typedef usb_status_t (*usb_standard_request_callback_t)(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
/*******************************************************************************
* Prototypes
******************************************************************************/
static usb_status_t USB_DeviceCh9GetStatus(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
static usb_status_t USB_DeviceCh9SetClearFeature(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
static usb_status_t USB_DeviceCh9SetAddress(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
static usb_status_t USB_DeviceCh9GetDescriptor(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
static usb_status_t USB_DeviceCh9GetConfiguration(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
static usb_status_t USB_DeviceCh9SetConfiguration(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
static usb_status_t USB_DeviceCh9GetInterface(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
static usb_status_t USB_DeviceCh9SetInterface(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
static usb_status_t USB_DeviceCh9SynchFrame(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
/*******************************************************************************
* Variables
******************************************************************************/
/* The function list to handle the standard request. */
static const usb_standard_request_callback_t s_UsbDeviceStandardRequest[] = {
USB_DeviceCh9GetStatus,
USB_DeviceCh9SetClearFeature,
(usb_standard_request_callback_t)NULL,
USB_DeviceCh9SetClearFeature,
(usb_standard_request_callback_t)NULL,
USB_DeviceCh9SetAddress,
USB_DeviceCh9GetDescriptor,
(usb_standard_request_callback_t)NULL,
USB_DeviceCh9GetConfiguration,
USB_DeviceCh9SetConfiguration,
USB_DeviceCh9GetInterface,
USB_DeviceCh9SetInterface,
USB_DeviceCh9SynchFrame,
};
/*******************************************************************************
* Code
******************************************************************************/
/*!
* @brief Handle get status request.
*
* This function is used to handle get status request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
* or, the request is unsupported.
*/
static usb_status_t USB_DeviceCh9GetStatus(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
usb_status_t error = kStatus_USB_InvalidRequest;
uint8_t state = 0U;
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (((uint8_t)kUSB_DeviceStateAddress != state) && ((uint8_t)kUSB_DeviceStateConfigured != state))
{
return error;
}
if ((setup->bmRequestType & USB_REQUEST_TYPE_RECIPIENT_MASK) == USB_REQUEST_TYPE_RECIPIENT_DEVICE)
{
#if (defined(USB_DEVICE_CONFIG_OTG) && (USB_DEVICE_CONFIG_OTG))
if (setup->wIndex == USB_REQUEST_STANDARD_GET_STATUS_OTG_STATUS_SELECTOR)
{
error =
USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusOtg, &classHandle->standardTranscationBuffer);
classHandle->standardTranscationBuffer = USB_SHORT_TO_LITTLE_ENDIAN(classHandle->standardTranscationBuffer);
/* The device status length must be USB_DEVICE_STATUS_SIZE. */
*length = 1;
}
else /* Get the device status */
{
#endif
error = USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDevice,
&classHandle->standardTranscationBuffer);
classHandle->standardTranscationBuffer =
classHandle->standardTranscationBuffer & USB_GET_STATUS_DEVICE_MASK;
classHandle->standardTranscationBuffer = USB_SHORT_TO_LITTLE_ENDIAN(classHandle->standardTranscationBuffer);
/* The device status length must be USB_DEVICE_STATUS_SIZE. */
*length = USB_DEVICE_STATUS_SIZE;
#if (defined(USB_DEVICE_CONFIG_OTG) && (USB_DEVICE_CONFIG_OTG))
}
#endif
}
else if ((setup->bmRequestType & USB_REQUEST_TYPE_RECIPIENT_MASK) == USB_REQUEST_TYPE_RECIPIENT_INTERFACE)
{
/* Get the interface status */
error = kStatus_USB_Success;
classHandle->standardTranscationBuffer = 0U;
/* The interface status length must be USB_INTERFACE_STATUS_SIZE. */
*length = USB_INTERFACE_STATUS_SIZE;
}
else if ((setup->bmRequestType & USB_REQUEST_TYPE_RECIPIENT_MASK) == USB_REQUEST_TYPE_RECIPIENT_ENDPOINT)
{
/* Get the endpoint status */
usb_device_endpoint_status_struct_t endpointStatus;
endpointStatus.endpointAddress = (uint8_t)setup->wIndex;
endpointStatus.endpointStatus = (uint16_t)kUSB_DeviceEndpointStateIdle;
error = USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusEndpoint, &endpointStatus);
classHandle->standardTranscationBuffer = endpointStatus.endpointStatus & USB_GET_STATUS_ENDPOINT_MASK;
classHandle->standardTranscationBuffer = USB_SHORT_TO_LITTLE_ENDIAN(classHandle->standardTranscationBuffer);
/* The endpoint status length must be USB_INTERFACE_STATUS_SIZE. */
*length = USB_ENDPOINT_STATUS_SIZE;
}
else
{
/*no action*/
}
*buffer = (uint8_t *)&classHandle->standardTranscationBuffer;
return error;
}
/*!
* @brief Handle set or clear device feature request.
*
* This function is used to handle set or clear device feature request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
* or, the request is unsupported.
*/
static usb_status_t USB_DeviceCh9SetClearFeature(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
usb_status_t error = kStatus_USB_InvalidRequest;
uint8_t state = 0U;
uint8_t isSet = 0U;
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (((uint8_t)kUSB_DeviceStateAddress != state) && ((uint8_t)kUSB_DeviceStateConfigured != state))
{
return error;
}
/* Identify the request is set or clear the feature. */
if (USB_REQUEST_STANDARD_SET_FEATURE == setup->bRequest)
{
isSet = 1U;
}
if ((setup->bmRequestType & USB_REQUEST_TYPE_RECIPIENT_MASK) == USB_REQUEST_TYPE_RECIPIENT_DEVICE)
{
/* Set or Clear the device feature. */
if (USB_REQUEST_STANDARD_FEATURE_SELECTOR_DEVICE_REMOTE_WAKEUP == setup->wValue)
{
#if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusRemoteWakeup, &isSet);
#endif
/* Set or Clear the device remote wakeup feature. */
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventSetRemoteWakeup, &isSet);
}
#if ((defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)) || \
(defined(USB_DEVICE_CONFIG_LPCIP3511HS) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))) && \
(defined(USB_DEVICE_CONFIG_USB20_TEST_MODE) && (USB_DEVICE_CONFIG_USB20_TEST_MODE > 0U))
else if (USB_REQUEST_STANDARD_FEATURE_SELECTOR_DEVICE_TEST_MODE == setup->wValue)
{
state = kUSB_DeviceStateTestMode;
error = USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
}
#endif
#if (defined(USB_DEVICE_CONFIG_OTG) && (USB_DEVICE_CONFIG_OTG))
else if (USB_REQUEST_STANDARD_FEATURE_SELECTOR_B_HNP_ENABLE == setup->wValue)
{
error = USB_DeviceClassCallback(classHandle->handle, kUSB_DeviceEventSetBHNPEnable, &isSet);
}
#endif
else
{
}
}
else if ((setup->bmRequestType & USB_REQUEST_TYPE_RECIPIENT_MASK) == USB_REQUEST_TYPE_RECIPIENT_ENDPOINT)
{
/* Set or Clear the endpoint feature. */
if (USB_REQUEST_STANDARD_FEATURE_SELECTOR_ENDPOINT_HALT == setup->wValue)
{
if (USB_CONTROL_ENDPOINT == (setup->wIndex & USB_ENDPOINT_NUMBER_MASK))
{
/* Set or Clear the control endpoint status(halt or not). */
if (0U != isSet)
{
(void)USB_DeviceStallEndpoint(classHandle->handle, (uint8_t)setup->wIndex);
}
else
{
(void)USB_DeviceUnstallEndpoint(classHandle->handle, (uint8_t)setup->wIndex);
}
}
/* Set or Clear the endpoint status feature. */
if (0U != isSet)
{
error = USB_DeviceClassEvent(classHandle->handle, kUSB_DeviceClassEventSetEndpointHalt, &setup->wIndex);
}
else
{
error =
USB_DeviceClassEvent(classHandle->handle, kUSB_DeviceClassEventClearEndpointHalt, &setup->wIndex);
}
}
else
{
/*no action*/
}
}
else
{
/*no action*/
}
return error;
}
/*!
* @brief Handle set address request.
*
* This function is used to handle set address request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state.
*/
static usb_status_t USB_DeviceCh9SetAddress(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
usb_status_t error = kStatus_USB_InvalidRequest;
uint8_t state = 0U;
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (((uint8_t)kUSB_DeviceStateAddressing != state) && ((uint8_t)kUSB_DeviceStateAddress != state) &&
((uint8_t)kUSB_DeviceStateDefault != state) && ((uint8_t)kUSB_DeviceStateConfigured != state))
{
return error;
}
if ((uint8_t)kUSB_DeviceStateAddressing != state)
{
/* If the device address is not setting, pass the address and the device state will change to
* kUSB_DeviceStateAddressing internally. */
state = (uint8_t)(setup->wValue & 0xFFU);
error = USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusAddress, &state);
}
else
{
/* If the device address is setting, set device address and the address will be write into the controller
* internally. */
error = USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusAddress, NULL);
/* And then change the device state to kUSB_DeviceStateAddress. */
if (kStatus_USB_Success == error)
{
state = (uint8_t)kUSB_DeviceStateAddress;
error = USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
}
}
return error;
}
/*!
* @brief Handle get descriptor request.
*
* This function is used to handle get descriptor request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
* or, the request is unsupported.
*/
static usb_status_t USB_DeviceCh9GetDescriptor(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
usb_device_get_descriptor_common_union_t commonDescriptor;
usb_status_t error = kStatus_USB_InvalidRequest;
uint8_t state = 0U;
uint8_t descriptorType = (uint8_t)((setup->wValue & 0xFF00U) >> 8U);
uint8_t descriptorIndex = (uint8_t)((setup->wValue & 0x00FFU));
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (((uint8_t)kUSB_DeviceStateAddress != state) && ((uint8_t)kUSB_DeviceStateConfigured != state) &&
((uint8_t)kUSB_DeviceStateDefault != state))
{
return error;
}
commonDescriptor.commonDescriptor.length = setup->wLength;
if (USB_DESCRIPTOR_TYPE_DEVICE == descriptorType)
{
/* Get the device descriptor */
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetDeviceDescriptor,
&commonDescriptor.deviceDescriptor);
}
else if (USB_DESCRIPTOR_TYPE_CONFIGURE == descriptorType)
{
/* Get the configuration descriptor */
commonDescriptor.configurationDescriptor.configuration = descriptorIndex;
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetConfigurationDescriptor,
&commonDescriptor.configurationDescriptor);
}
else if (USB_DESCRIPTOR_TYPE_STRING == descriptorType)
{
/* Get the string descriptor */
commonDescriptor.stringDescriptor.stringIndex = descriptorIndex;
commonDescriptor.stringDescriptor.languageId = setup->wIndex;
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetStringDescriptor,
&commonDescriptor.stringDescriptor);
}
#if (defined(USB_DEVICE_CONFIG_HID) && (USB_DEVICE_CONFIG_HID > 0U))
else if (USB_DESCRIPTOR_TYPE_HID == descriptorType)
{
/* Get the hid descriptor */
commonDescriptor.hidDescriptor.interfaceNumber = (uint8_t)setup->wIndex;
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetHidDescriptor,
&commonDescriptor.hidDescriptor);
}
else if (USB_DESCRIPTOR_TYPE_HID_REPORT == descriptorType)
{
/* Get the hid report descriptor */
commonDescriptor.hidReportDescriptor.interfaceNumber = (uint8_t)setup->wIndex;
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetHidReportDescriptor,
&commonDescriptor.hidReportDescriptor);
}
else if (USB_DESCRIPTOR_TYPE_HID_PHYSICAL == descriptorType)
{
/* Get the hid physical descriptor */
commonDescriptor.hidPhysicalDescriptor.index = descriptorIndex;
commonDescriptor.hidPhysicalDescriptor.interfaceNumber = (uint8_t)setup->wIndex;
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetHidPhysicalDescriptor,
&commonDescriptor.hidPhysicalDescriptor);
}
#endif
#if (defined(USB_DEVICE_CONFIG_CV_TEST) && (USB_DEVICE_CONFIG_CV_TEST > 0U))
else if (USB_DESCRIPTOR_TYPE_DEVICE_QUALITIER == descriptorType)
{
/* Get the device descriptor */
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetDeviceQualifierDescriptor,
&commonDescriptor.deviceDescriptor);
}
#endif
#if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
else if (USB_DESCRIPTOR_TYPE_BOS == descriptorType)
{
/* Get the configuration descriptor */
commonDescriptor.configurationDescriptor.configuration = descriptorIndex;
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetBOSDescriptor,
&commonDescriptor.configurationDescriptor);
}
#endif
else
{
}
*buffer = commonDescriptor.commonDescriptor.buffer;
*length = commonDescriptor.commonDescriptor.length;
return error;
}
/*!
* @brief Handle get current configuration request.
*
* This function is used to handle get current configuration request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
* or, the request is unsupported.
*/
static usb_status_t USB_DeviceCh9GetConfiguration(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
uint8_t state = 0U;
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (((uint8_t)kUSB_DeviceStateAddress != state) && (((uint8_t)kUSB_DeviceStateConfigured != state)))
{
return kStatus_USB_InvalidRequest;
}
*length = USB_CONFIGURE_SIZE;
*buffer = (uint8_t *)&classHandle->standardTranscationBuffer;
return USB_DeviceClassCallback(classHandle->handle, (uint8_t)kUSB_DeviceEventGetConfiguration,
&classHandle->standardTranscationBuffer);
}
/*!
* @brief Handle set current configuration request.
*
* This function is used to handle set current configuration request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
* or, the request is unsupported.
*/
static usb_status_t USB_DeviceCh9SetConfiguration(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
uint8_t state = 0U;
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (((uint8_t)kUSB_DeviceStateAddress != state) && ((uint8_t)kUSB_DeviceStateConfigured != state))
{
return kStatus_USB_InvalidRequest;
}
/* The device state is changed to kUSB_DeviceStateConfigured */
state = (uint8_t)kUSB_DeviceStateConfigured;
(void)USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (0U == setup->wValue)
{
/* If the new configuration is zero, the device state is changed to kUSB_DeviceStateAddress */
state = (uint8_t)kUSB_DeviceStateAddress;
(void)USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
}
/* Notify the class layer the configuration is changed */
(void)USB_DeviceClassEvent(classHandle->handle, kUSB_DeviceClassEventSetConfiguration, &setup->wValue);
/* Notify the application the configuration is changed */
return USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventSetConfiguration, &setup->wValue);
}
/*!
* @brief Handle get the alternate setting of a interface request.
*
* This function is used to handle get the alternate setting of a interface request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
* or, the request is unsupported.
*/
static usb_status_t USB_DeviceCh9GetInterface(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
usb_status_t error = kStatus_USB_InvalidRequest;
uint8_t state = 0U;
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (state != (uint8_t)kUSB_DeviceStateConfigured)
{
return error;
}
*length = USB_INTERFACE_SIZE;
*buffer = (uint8_t *)&classHandle->standardTranscationBuffer;
classHandle->standardTranscationBuffer = (uint16_t)(((uint32_t)setup->wIndex & 0xFFU) << 8U);
/* The Bit[15~8] is used to save the interface index, and the alternate setting will be saved in Bit[7~0] by
* application. */
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetInterface,
&classHandle->standardTranscationBuffer);
classHandle->standardTranscationBuffer = USB_SHORT_TO_LITTLE_ENDIAN(classHandle->standardTranscationBuffer);
return error;
}
/*!
* @brief Handle set the alternate setting of a interface request.
*
* This function is used to handle set the alternate setting of a interface request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
* or, the request is unsupported.
*/
static usb_status_t USB_DeviceCh9SetInterface(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
uint8_t state = 0U;
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (state != (uint8_t)kUSB_DeviceStateConfigured)
{
return kStatus_USB_InvalidRequest;
}
classHandle->standardTranscationBuffer = ((setup->wIndex & 0xFFU) << 8U) | (setup->wValue & 0xFFU);
/* Notify the class driver the alternate setting of the interface is changed. */
/* The Bit[15~8] is used to save the interface index, and the alternate setting is saved in Bit[7~0]. */
(void)USB_DeviceClassEvent(classHandle->handle, kUSB_DeviceClassEventSetInterface,
&classHandle->standardTranscationBuffer);
/* Notify the application the alternate setting of the interface is changed. */
/* The Bit[15~8] is used to save the interface index, and the alternate setting will is saved in Bit[7~0]. */
return USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventSetInterface,
&classHandle->standardTranscationBuffer);
}
/*!
* @brief Handle get sync frame request.
*
* This function is used to handle get sync frame request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
* or, the request is unsupported.
*/
static usb_status_t USB_DeviceCh9SynchFrame(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
usb_status_t error = kStatus_USB_InvalidRequest;
uint8_t state = 0U;
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (state != (uint8_t)kUSB_DeviceStateConfigured)
{
return error;
}
classHandle->standardTranscationBuffer = USB_SHORT_FROM_LITTLE_ENDIAN(setup->wIndex);
/* Get the sync frame value */
error =
USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusSynchFrame, &classHandle->standardTranscationBuffer);
*buffer = (uint8_t *)&classHandle->standardTranscationBuffer;
*length = sizeof(classHandle->standardTranscationBuffer);
return error;
}
/*!
* @brief Send the response to the host.
*
* This function is used to send the response to the host.
*
* There are two cases this function will be called.
* Case one when a setup packet is received in control endpoint callback function:
* 1. If there is not data phase in the setup transfer, the function will prime an IN transfer with the data
* length is zero for status phase.
* 2. If there is an IN data phase, the function will prime an OUT transfer with the actual length to need to
* send for data phase. And then prime an IN transfer with the data length is zero for status phase.
* 3. If there is an OUT data phase, the function will prime an IN transfer with the actual length to want to
* receive for data phase.
*
* Case two when is not a setup packet received in control endpoint callback function:
* 1. The function will prime an IN transfer with data length is zero for status phase.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param error The error code returned from the standard request function.
* @param stage The stage of the control transfer.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @return A USB error code or kStatus_USB_Success.
*/
static usb_status_t USB_DeviceControlCallbackFeedback(usb_device_handle handle,
usb_setup_struct_t *setup,
usb_status_t error,
usb_device_control_read_write_sequence_t stage,
uint8_t **buffer,
uint32_t *length)
{
usb_status_t status;
uint8_t direction = USB_IN;
if (kStatus_USB_InvalidRequest == error)
{
/* Stall the control pipe when the request is unsupported. */
if ((!((setup->bmRequestType & USB_REQUEST_TYPE_TYPE_MASK) == USB_REQUEST_TYPE_TYPE_STANDARD)) &&
((setup->bmRequestType & USB_REQUEST_TYPE_DIR_MASK) == USB_REQUEST_TYPE_DIR_OUT) &&
(0U != setup->wLength) && (kUSB_DeviceControlPipeSetupStage == stage))
{
direction = USB_OUT;
}
status = USB_DeviceStallEndpoint(
handle,
(USB_CONTROL_ENDPOINT) | (uint8_t)((uint32_t)direction << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT));
}
else
{
if (*length > setup->wLength)
{
*length = setup->wLength;
}
status = USB_DeviceSendRequest(handle, (USB_CONTROL_ENDPOINT), *buffer, *length);
if ((kStatus_USB_Success == status) &&
(USB_REQUEST_TYPE_DIR_IN == (setup->bmRequestType & USB_REQUEST_TYPE_DIR_MASK)))
{
status = USB_DeviceRecvRequest(handle, (USB_CONTROL_ENDPOINT), (uint8_t *)NULL, 0U);
}
}
return status;
}
/*!
* @brief Control endpoint callback function.
*
* This callback function is used to notify uplayer the transfser result of a transfer.
* This callback pointer is passed when a specified endpoint initialized by calling API USB_DeviceInitEndpoint.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param message The result of a transfer, includes transfer buffer, transfer length and whether is in setup
* phase for control pipe.
* @param callbackParam The parameter for this callback. It is same with
* usb_device_endpoint_callback_struct_t::callbackParam.
*
* @return A USB error code or kStatus_USB_Success.
*/
static usb_status_t USB_DeviceControlCallback(usb_device_handle handle,
usb_device_endpoint_callback_message_struct_t *message,
void *callbackParam)
{
usb_setup_struct_t *deviceSetup, *setup;
usb_device_common_class_struct_t *classHandle;
uint8_t *buffer = (uint8_t *)NULL;
uint32_t length = 0U;
void *temp;
usb_status_t status = kStatus_USB_InvalidRequest;
uint8_t state = 0U;
/* endpoint callback length is USB_CANCELLED_TRANSFER_LENGTH (0xFFFFFFFFU) when transfer is canceled */
if ((USB_CANCELLED_TRANSFER_LENGTH == message->length) || (NULL == callbackParam))
{
return status;
}
classHandle = (usb_device_common_class_struct_t *)callbackParam;
temp = (void *)&classHandle->setupBuffer[0];
deviceSetup = (usb_setup_struct_t *)temp;
(void)USB_DeviceGetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
if (0U != message->isSetup)
{
if ((USB_SETUP_PACKET_SIZE != message->length) || (NULL == message->buffer))
{
/* If a invalid setup is received, the control pipes should be de-init and init again.
* Due to the IP can not meet this require, it is reserved for feature.
*/
/*
USB_DeviceDeinitEndpoint(handle,
USB_CONTROL_ENDPOINT | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT));
USB_DeviceDeinitEndpoint(handle,
USB_CONTROL_ENDPOINT | (USB_OUT << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT));
USB_DeviceControlPipeInit(handle, callbackParam);
*/
return status;
}
/* Receive a setup request */
temp = (void *)(message->buffer);
setup = (usb_setup_struct_t *)temp;
/* Copy the setup packet to the application buffer */
deviceSetup->wValue = USB_SHORT_FROM_LITTLE_ENDIAN(setup->wValue);
deviceSetup->wIndex = USB_SHORT_FROM_LITTLE_ENDIAN(setup->wIndex);
deviceSetup->wLength = USB_SHORT_FROM_LITTLE_ENDIAN(setup->wLength);
deviceSetup->bRequest = setup->bRequest;
deviceSetup->bmRequestType = setup->bmRequestType;
if ((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_MASK) == USB_REQUEST_TYPE_TYPE_STANDARD)
{
/* Handle the standard request, only handle the request in request array. */
if (deviceSetup->bRequest < (sizeof(s_UsbDeviceStandardRequest) / 4U))
{
if (s_UsbDeviceStandardRequest[deviceSetup->bRequest] != (usb_standard_request_callback_t)NULL)
{
status =
s_UsbDeviceStandardRequest[deviceSetup->bRequest](classHandle, deviceSetup, &buffer, &length);
}
}
}
else
{
if ((0U != deviceSetup->wLength) &&
((deviceSetup->bmRequestType & USB_REQUEST_TYPE_DIR_MASK) == USB_REQUEST_TYPE_DIR_OUT))
{
/* Class or vendor request with the OUT data phase. */
if ((0U != deviceSetup->wLength) &&
((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_CLASS) == USB_REQUEST_TYPE_TYPE_CLASS))
{
/* Get data buffer to receive the data from the host. */
usb_device_control_request_struct_t controlRequest;
controlRequest.buffer = (uint8_t *)NULL;
controlRequest.isSetup = 1U;
controlRequest.setup = deviceSetup;
controlRequest.length = deviceSetup->wLength;
status = USB_DeviceClassEvent(handle, kUSB_DeviceClassEventClassRequest, &controlRequest);
length = controlRequest.length;
buffer = controlRequest.buffer;
}
else if ((0U != deviceSetup->wLength) &&
((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_VENDOR) == USB_REQUEST_TYPE_TYPE_VENDOR))
{
/* Get data buffer to receive the data from the host. */
usb_device_control_request_struct_t controlRequest;
controlRequest.buffer = (uint8_t *)NULL;
controlRequest.isSetup = 1U;
controlRequest.setup = deviceSetup;
controlRequest.length = deviceSetup->wLength;
status = USB_DeviceClassCallback(handle, (uint32_t)kUSB_DeviceEventVendorRequest, &controlRequest);
length = controlRequest.length;
buffer = controlRequest.buffer;
}
else
{
/*no action*/
}
if (kStatus_USB_Success == status)
{
/* Prime an OUT transfer */
status = USB_DeviceRecvRequest(handle, USB_CONTROL_ENDPOINT, buffer, deviceSetup->wLength);
return status;
}
}
else
{
/* Class or vendor request with the IN data phase. */
if (((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_CLASS) == USB_REQUEST_TYPE_TYPE_CLASS))
{
/* Get data buffer to response the host. */
usb_device_control_request_struct_t controlRequest;
controlRequest.buffer = (uint8_t *)NULL;
controlRequest.isSetup = 1U;
controlRequest.setup = deviceSetup;
controlRequest.length = deviceSetup->wLength;
status = USB_DeviceClassEvent(handle, kUSB_DeviceClassEventClassRequest, &controlRequest);
length = controlRequest.length;
buffer = controlRequest.buffer;
}
else if (((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_VENDOR) == USB_REQUEST_TYPE_TYPE_VENDOR))
{
/* Get data buffer to response the host. */
usb_device_control_request_struct_t controlRequest;
controlRequest.buffer = (uint8_t *)NULL;
controlRequest.isSetup = 1U;
controlRequest.setup = deviceSetup;
controlRequest.length = deviceSetup->wLength;
status = USB_DeviceClassCallback(handle, (uint32_t)kUSB_DeviceEventVendorRequest, &controlRequest);
length = controlRequest.length;
buffer = controlRequest.buffer;
}
else
{
/*no action*/
}
}
}
/* Send the response to the host. */
status = USB_DeviceControlCallbackFeedback(handle, deviceSetup, status, kUSB_DeviceControlPipeSetupStage,
&buffer, &length);
}
else if ((uint8_t)kUSB_DeviceStateAddressing == state)
{
/* Set the device address to controller. */
status = s_UsbDeviceStandardRequest[deviceSetup->bRequest](classHandle, deviceSetup, &buffer, &length);
}
#if ((defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)) || \
(defined(USB_DEVICE_CONFIG_LPCIP3511HS) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))) && \
(defined(USB_DEVICE_CONFIG_USB20_TEST_MODE) && (USB_DEVICE_CONFIG_USB20_TEST_MODE > 0U))
else if ((uint8_t)kUSB_DeviceStateTestMode == state)
{
uint8_t portTestControl = (uint8_t)(deviceSetup->wIndex >> 8);
/* Set the controller.into test mode. */
status = USB_DeviceSetStatus(handle, kUSB_DeviceStatusTestMode, &portTestControl);
}
#endif
else if ((0U != message->length) && (0U != deviceSetup->wLength) &&
((deviceSetup->bmRequestType & USB_REQUEST_TYPE_DIR_MASK) == USB_REQUEST_TYPE_DIR_OUT))
{
if (((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_CLASS) == USB_REQUEST_TYPE_TYPE_CLASS))
{
/* Data received in OUT phase, and notify the class driver. */
usb_device_control_request_struct_t controlRequest;
controlRequest.buffer = message->buffer;
controlRequest.isSetup = 0U;
controlRequest.setup = deviceSetup;
controlRequest.length = message->length;
status = USB_DeviceClassEvent(handle, kUSB_DeviceClassEventClassRequest, &controlRequest);
}
else if (((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_VENDOR) == USB_REQUEST_TYPE_TYPE_VENDOR))
{
/* Data received in OUT phase, and notify the application. */
usb_device_control_request_struct_t controlRequest;
controlRequest.buffer = message->buffer;
controlRequest.isSetup = 0U;
controlRequest.setup = deviceSetup;
controlRequest.length = message->length;
status = USB_DeviceClassCallback(handle, (uint32_t)kUSB_DeviceEventVendorRequest, &controlRequest);
}
else
{
/*no action*/
}
/* Send the response to the host. */
status = USB_DeviceControlCallbackFeedback(handle, deviceSetup, status, kUSB_DeviceControlPipeDataStage,
&buffer, &length);
}
else
{
/*no action*/
}
return status;
}
/*!
* @brief Control endpoint initialization function.
*
* This callback function is used to initialize the control pipes.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param param The up layer handle.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceControlPipeInit(usb_device_handle handle, void *param)
{
usb_device_endpoint_init_struct_t epInitStruct;
usb_device_endpoint_callback_struct_t epCallback;
usb_status_t status;
epCallback.callbackFn = USB_DeviceControlCallback;
epCallback.callbackParam = param;
epInitStruct.zlt = 1U;
epInitStruct.transferType = USB_ENDPOINT_CONTROL;
epInitStruct.interval = 0;
epInitStruct.endpointAddress = USB_CONTROL_ENDPOINT | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT);
epInitStruct.maxPacketSize = USB_CONTROL_MAX_PACKET_SIZE;
/* Initialize the control IN pipe */
status = USB_DeviceInitEndpoint(handle, &epInitStruct, &epCallback);
if (kStatus_USB_Success != status)
{
return status;
}
epInitStruct.endpointAddress = USB_CONTROL_ENDPOINT | (USB_OUT << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT);
/* Initialize the control OUT pipe */
status = USB_DeviceInitEndpoint(handle, &epInitStruct, &epCallback);
if (kStatus_USB_Success != status)
{
(void)USB_DeviceDeinitEndpoint(
handle, USB_CONTROL_ENDPOINT | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT));
return status;
}
return kStatus_USB_Success;
}
#endif /* USB_DEVICE_CONFIG_NUM */

View File

@@ -0,0 +1,79 @@
/*
* Copyright (c) 2015, Freescale Semiconductor, Inc.
* Copyright 2016 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef __USB_DEVICE_CH9_H__
#define __USB_DEVICE_CH9_H__
/*******************************************************************************
* Definitions
******************************************************************************/
/*!
* @addtogroup usb_device_ch9
* @{
*/
/*! @brief Defines USB device status size when the host request to get device status */
#define USB_DEVICE_STATUS_SIZE (0x02U)
/*! @brief Defines USB device interface status size when the host request to get interface status */
#define USB_INTERFACE_STATUS_SIZE (0x02U)
/*! @brief Defines USB device endpoint status size when the host request to get endpoint status */
#define USB_ENDPOINT_STATUS_SIZE (0x02U)
/*! @brief Defines USB device configuration size when the host request to get current configuration */
#define USB_CONFIGURE_SIZE (0X01U)
/*! @brief Defines USB device interface alternate setting size when the host request to get interface alternate setting
*/
#define USB_INTERFACE_SIZE (0X01U)
/*! @brief Defines USB device status mask */
#define USB_GET_STATUS_DEVICE_MASK (0x03U)
/*! @brief Defines USB device interface status mask */
#define USB_GET_STATUS_INTERFACE_MASK (0x03U)
/*! @brief Defines USB device endpoint status mask */
#define USB_GET_STATUS_ENDPOINT_MASK (0x03U)
/*! @brief Control read and write sequence */
typedef enum _usb_device_control_read_write_sequence
{
kUSB_DeviceControlPipeSetupStage = 0U, /*!< Setup stage */
kUSB_DeviceControlPipeDataStage, /*!< Data stage */
kUSB_DeviceControlPipeStatusStage, /*!< status stage */
} usb_device_control_read_write_sequence_t;
#if defined(__cplusplus)
extern "C" {
#endif
/*******************************************************************************
* API
******************************************************************************/
/*!
* @brief Initializes the control pipes.
*
* The function is used to initialize the control pipes. This function should be called when event
* kUSB_DeviceEventBusReset is received.
*
* @param[in] handle The device handle.
* @param[in] param The event parameter.
*
* @return A USB error code or kStatus_USB_Success.
*/
extern usb_status_t USB_DeviceControlPipeInit(usb_device_handle handle, void *param);
#if defined(__cplusplus)
}
#endif
/*! @}*/
#endif /* __USB_DEVICE_CH9_H__ */

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,170 @@
/*
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
* Copyright 2016 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef __USB_DEVICE_DCI_H__
#define __USB_DEVICE_DCI_H__
/*!
* @addtogroup usb_device_controller_driver
* @{
*/
/*******************************************************************************
* Definitions
******************************************************************************/
/*! @brief Macro to define controller handle */
#define usb_device_controller_handle usb_device_handle
#define USB_DEVICE_MESSAGES_SIZE \
(sizeof(uint32_t) * (1U + (sizeof(usb_device_callback_message_struct_t) - 1U) / sizeof(uint32_t)))
/*! @brief Available notify types for device notification */
typedef enum _usb_device_notification
{
kUSB_DeviceNotifyBusReset = 0x10U, /*!< Reset signal detected */
kUSB_DeviceNotifySuspend, /*!< Suspend signal detected */
kUSB_DeviceNotifyResume, /*!< Resume signal detected */
kUSB_DeviceNotifyLPMSleep, /*!< LPM signal detected */
kUSB_DeviceNotifyLPMResume, /*!< Resume signal detected */
kUSB_DeviceNotifyError, /*!< Errors happened in bus */
kUSB_DeviceNotifyDetach, /*!< Device disconnected from a host */
kUSB_DeviceNotifyAttach, /*!< Device connected to a host */
#if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
kUSB_DeviceNotifyDcdDetectFinished, /*!< Device charger detection finished */
#endif
} usb_device_notification_t;
/*! @brief Device notification message structure */
typedef struct _usb_device_callback_message_struct
{
uint8_t *buffer; /*!< Transferred buffer */
uint32_t length; /*!< Transferred data length */
uint8_t code; /*!< Notification code */
uint8_t isSetup; /*!< Is in a setup phase */
} usb_device_callback_message_struct_t;
/*! @brief Control type for controller */
typedef enum _usb_device_control_type
{
kUSB_DeviceControlRun = 0U, /*!< Enable the device functionality */
kUSB_DeviceControlStop, /*!< Disable the device functionality */
kUSB_DeviceControlEndpointInit, /*!< Initialize a specified endpoint */
kUSB_DeviceControlEndpointDeinit, /*!< De-initialize a specified endpoint */
kUSB_DeviceControlEndpointStall, /*!< Stall a specified endpoint */
kUSB_DeviceControlEndpointUnstall, /*!< Un-stall a specified endpoint */
kUSB_DeviceControlGetDeviceStatus, /*!< Get device status */
kUSB_DeviceControlGetEndpointStatus, /*!< Get endpoint status */
kUSB_DeviceControlSetDeviceAddress, /*!< Set device address */
kUSB_DeviceControlGetSynchFrame, /*!< Get current frame */
kUSB_DeviceControlResume, /*!< Drive controller to generate a resume signal in USB bus */
kUSB_DeviceControlSleepResume, /*!< Drive controller to generate a LPM resume signal in USB bus */
kUSB_DeviceControlSuspend, /*!< Drive controller to enter into suspend mode */
kUSB_DeviceControlSleep, /*!< Drive controller to enter into sleep mode */
kUSB_DeviceControlSetDefaultStatus, /*!< Set controller to default status */
kUSB_DeviceControlGetSpeed, /*!< Get current speed */
kUSB_DeviceControlGetOtgStatus, /*!< Get OTG status */
kUSB_DeviceControlSetOtgStatus, /*!< Set OTG status */
kUSB_DeviceControlSetTestMode, /*!< Drive xCHI into test mode */
kUSB_DeviceControlGetRemoteWakeUp, /*!< Get flag of LPM Remote Wake-up Enabled by USB host. */
#if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
kUSB_DeviceControlDcdDisable, /*!< disable dcd module function. */
kUSB_DeviceControlDcdEnable, /*!< enable dcd module function. */
#endif
kUSB_DeviceControlPreSetDeviceAddress, /*!< Pre set device address */
kUSB_DeviceControlUpdateHwTick, /*!< update hardware tick */
#if defined(USB_DEVICE_CONFIG_GET_SOF_COUNT) && (USB_DEVICE_CONFIG_GET_SOF_COUNT > 0U)
kUSB_DeviceControlGetCurrentFrameCount, /*!< Get current frame count */
#endif
} usb_device_control_type_t;
/*! @brief USB device controller initialization function typedef */
typedef usb_status_t (*usb_device_controller_init_t)(uint8_t controllerId,
usb_device_handle handle,
usb_device_controller_handle *controllerHandle);
/*! @brief USB device controller de-initialization function typedef */
typedef usb_status_t (*usb_device_controller_deinit_t)(usb_device_controller_handle controllerHandle);
/*! @brief USB device controller send data function typedef */
typedef usb_status_t (*usb_device_controller_send_t)(usb_device_controller_handle controllerHandle,
uint8_t endpointAddress,
uint8_t *buffer,
uint32_t length);
/*! @brief USB device controller receive data function typedef */
typedef usb_status_t (*usb_device_controller_recv_t)(usb_device_controller_handle controllerHandle,
uint8_t endpointAddress,
uint8_t *buffer,
uint32_t length);
/*! @brief USB device controller cancel transfer function in a specified endpoint typedef */
typedef usb_status_t (*usb_device_controller_cancel_t)(usb_device_controller_handle controllerHandle,
uint8_t endpointAddress);
/*! @brief USB device controller control function typedef */
typedef usb_status_t (*usb_device_controller_control_t)(usb_device_controller_handle controllerHandle,
usb_device_control_type_t command,
void *param);
/*! @brief USB device controller interface structure */
typedef struct _usb_device_controller_interface_struct
{
usb_device_controller_init_t deviceInit; /*!< Controller initialization */
usb_device_controller_deinit_t deviceDeinit; /*!< Controller de-initialization */
usb_device_controller_send_t deviceSend; /*!< Controller send data */
usb_device_controller_recv_t deviceRecv; /*!< Controller receive data */
usb_device_controller_cancel_t deviceCancel; /*!< Controller cancel transfer */
usb_device_controller_control_t deviceControl; /*!< Controller control */
} usb_device_controller_interface_struct_t;
/*! @brief USB device status structure */
typedef struct _usb_device_struct
{
#if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U)) || \
(defined(FSL_FEATURE_SOC_USB_ANALOG_COUNT) && (FSL_FEATURE_SOC_USB_ANALOG_COUNT > 0U))
volatile uint64_t hwTick; /*!< Current hw tick(ms)*/
#endif
usb_device_controller_handle controllerHandle; /*!< Controller handle */
const usb_device_controller_interface_struct_t *controllerInterface; /*!< Controller interface handle */
#if USB_DEVICE_CONFIG_USE_TASK
OSA_MSGQ_HANDLE_DEFINE(notificationQueueBuffer,
USB_DEVICE_CONFIG_MAX_MESSAGES,
USB_DEVICE_MESSAGES_SIZE); /*!< Message queue buffer*/
osa_msgq_handle_t notificationQueue; /*!< Message queue*/
#endif
usb_device_callback_t deviceCallback; /*!< Device callback function pointer */
usb_device_endpoint_callback_struct_t
epCallback[USB_DEVICE_CONFIG_ENDPOINTS << 1U]; /*!< Endpoint callback function structure */
uint8_t deviceAddress; /*!< Current device address */
uint8_t controllerId; /*!< Controller ID */
uint8_t state; /*!< Current device state */
#if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
uint8_t remotewakeup; /*!< Remote wakeup is enabled or not */
#endif
uint8_t isResetting; /*!< Is doing device reset or not */
#if (defined(USB_DEVICE_CONFIG_USE_TASK) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
uint8_t epCallbackDirectly; /*!< Whether call ep callback directly when the task is enabled */
#endif
} usb_device_struct_t;
/*******************************************************************************
* API
******************************************************************************/
/*!
* @brief Notify the device that the controller status changed.
*
* This function is used to notify the device that the controller status changed.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param message The device callback message handle.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceNotificationTrigger(void *handle, void *msg);
/*! @}*/
#endif /* __USB_DEVICE_DCI_H__ */