v1
This commit is contained in:
@@ -0,0 +1,766 @@
|
||||
/*!
|
||||
* \file usbd_cdc.c
|
||||
*
|
||||
* \brief This file provides the high layer firmware functions to manage the
|
||||
* following functionalities of the USB CDC Class:
|
||||
* - Initialization and Configuration of high and low layer
|
||||
* - Enumeration as CDC Device (and enumeration for each implemented
|
||||
* memory interface)
|
||||
* - OUT/IN data transfer
|
||||
* - Command IN transfer (class requests management)
|
||||
* - Error management
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author MCD Application Team
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* @verbatim
|
||||
*
|
||||
* ===================================================================
|
||||
* CDC Class Driver Description
|
||||
* ===================================================================
|
||||
* This driver manages the "Universal Serial Bus Class Definitions for
|
||||
* Communications Devices Revision 1.2 November 16, 2007" and the sub-protocol
|
||||
* specification of "Universal Serial Bus Communications Class Subclass
|
||||
* Specification for PSTN Devices Revision 1.2 February 9, 2007" This driver
|
||||
* implements the following aspects of the specification:
|
||||
* - Device descriptor management
|
||||
* - Configuration descriptor management
|
||||
* - Enumeration as CDC device with 2 data endpoints (IN and OUT)
|
||||
* and 1 command endpoint (IN)
|
||||
* - Requests management (as described in section 6.2 in
|
||||
* specification)
|
||||
* - Abstract Control Model compliant
|
||||
* - Union Functional collection (using 1 IN endpoint for control)
|
||||
* - Data interface class
|
||||
*
|
||||
* These aspects may be enriched or modified for a specific user
|
||||
* application.
|
||||
*
|
||||
* This driver doesn't implement the following aspects of the
|
||||
* specification (but it is possible to manage these features with some
|
||||
* modifications on this driver):
|
||||
* - Any class-specific aspect relative to communication classes
|
||||
* should be managed by user application.
|
||||
* - All communication classes other than PSTN are not managed
|
||||
*
|
||||
* @endverbatim
|
||||
*
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_cdc.h"
|
||||
#include "usbd_ctlreq.h"
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
Func Prototype
|
||||
------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_CDC_Private_FunctionPrototypes
|
||||
* @{
|
||||
*/
|
||||
|
||||
static uint8_t USBD_CDC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
|
||||
static uint8_t USBD_CDC_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
|
||||
static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev,
|
||||
USBD_SetupReqTypedef *req);
|
||||
static uint8_t USBD_CDC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum);
|
||||
static uint8_t USBD_CDC_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum);
|
||||
static uint8_t USBD_CDC_EP0_RxReady(USBD_HandleTypeDef *pdev);
|
||||
|
||||
static uint8_t *USBD_CDC_GetFSCfgDesc(uint16_t *length);
|
||||
static uint8_t *USBD_CDC_GetHSCfgDesc(uint16_t *length);
|
||||
static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc(uint16_t *length);
|
||||
static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc(uint16_t *length);
|
||||
uint8_t *USBD_CDC_GetDeviceQualifierDescriptor(uint16_t *length);
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Local Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/* USB Standard Device Descriptor */
|
||||
__ALIGN_BEGIN static uint8_t
|
||||
USBD_CDC_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = {
|
||||
USB_LEN_DEV_QUALIFIER_DESC,
|
||||
USB_DESC_TYPE_DEVICE_QUALIFIER,
|
||||
0x00,
|
||||
0x02,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x40,
|
||||
0x01,
|
||||
0x00,
|
||||
};
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CDC_Private_Variables
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* CDC interface class callbacks structure */
|
||||
USBD_ClassTypeDef USBD_CDC = {
|
||||
USBD_CDC_Init,
|
||||
USBD_CDC_DeInit,
|
||||
USBD_CDC_Setup,
|
||||
NULL, /* EP0_TxSent */
|
||||
USBD_CDC_EP0_RxReady,
|
||||
USBD_CDC_DataIn,
|
||||
USBD_CDC_DataOut,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
USBD_CDC_GetHSCfgDesc,
|
||||
USBD_CDC_GetFSCfgDesc,
|
||||
USBD_CDC_GetOtherSpeedCfgDesc,
|
||||
USBD_CDC_GetDeviceQualifierDescriptor,
|
||||
};
|
||||
|
||||
/* USB CDC device Configuration Descriptor */
|
||||
__ALIGN_BEGIN static uint8_t
|
||||
USBD_CDC_CfgDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = {
|
||||
/* Configuration Descriptor */
|
||||
0x09, /* bLength: Configuration Descriptor size */
|
||||
USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
|
||||
USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength */
|
||||
0x00, 0x02, /* bNumInterfaces: 2 interfaces */
|
||||
0x01, /* bConfigurationValue: Configuration value */
|
||||
0x00, /* iConfiguration: Index of string descriptor
|
||||
describing the configuration */
|
||||
#if (USBD_SELF_POWERED == 1U)
|
||||
0xC0, /* bmAttributes: Bus Powered according to user configuration */
|
||||
#else
|
||||
0x80, /* bmAttributes: Bus Powered according to user configuration */
|
||||
#endif /* USBD_SELF_POWERED */
|
||||
USBD_MAX_POWER, /* MaxPower (mA) */
|
||||
|
||||
/* Interface Descriptor */
|
||||
0x09, /* bLength: Interface Descriptor size */
|
||||
USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */
|
||||
/* Interface descriptor type */
|
||||
0x00, /* bInterfaceNumber: Number of Interface */
|
||||
0x00, /* bAlternateSetting: Alternate setting */
|
||||
0x01, /* bNumEndpoints: One endpoint used */
|
||||
0x02, /* bInterfaceClass: Communication Interface Class */
|
||||
0x02, /* bInterfaceSubClass: Abstract Control Model */
|
||||
0x01, /* bInterfaceProtocol: Common AT commands */
|
||||
0x00, /* iInterface */
|
||||
|
||||
/* Header Functional Descriptor */
|
||||
0x05, /* bLength: Endpoint Descriptor size */
|
||||
0x24, /* bDescriptorType: CS_INTERFACE */
|
||||
0x00, /* bDescriptorSubtype: Header Func Desc */
|
||||
0x10, /* bcdCDC: spec release number */
|
||||
0x01,
|
||||
|
||||
/* Call Management Functional Descriptor */
|
||||
0x05, /* bFunctionLength */
|
||||
0x24, /* bDescriptorType: CS_INTERFACE */
|
||||
0x01, /* bDescriptorSubtype: Call Management Func Desc */
|
||||
0x00, /* bmCapabilities: D0+D1 */
|
||||
0x01, /* bDataInterface */
|
||||
|
||||
/* ACM Functional Descriptor */
|
||||
0x04, /* bFunctionLength */
|
||||
0x24, /* bDescriptorType: CS_INTERFACE */
|
||||
0x02, /* bDescriptorSubtype: Abstract Control Management desc */
|
||||
0x02, /* bmCapabilities */
|
||||
|
||||
/* Union Functional Descriptor */
|
||||
0x05, /* bFunctionLength */
|
||||
0x24, /* bDescriptorType: CS_INTERFACE */
|
||||
0x06, /* bDescriptorSubtype: Union func desc */
|
||||
0x00, /* bMasterInterface: Communication class interface */
|
||||
0x01, /* bSlaveInterface0: Data Class Interface */
|
||||
|
||||
/* Endpoint 2 Descriptor */
|
||||
0x07, /* bLength: Endpoint Descriptor size */
|
||||
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
|
||||
CDC_CMD_EP, /* bEndpointAddress */
|
||||
0x03, /* bmAttributes: Interrupt */
|
||||
LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize */
|
||||
HIBYTE(CDC_CMD_PACKET_SIZE), CDC_FS_BINTERVAL, /* bInterval */
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
||||
/* Data class interface descriptor */
|
||||
0x09, /* bLength: Endpoint Descriptor size */
|
||||
USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */
|
||||
0x01, /* bInterfaceNumber: Number of Interface */
|
||||
0x00, /* bAlternateSetting: Alternate setting */
|
||||
0x02, /* bNumEndpoints: Two endpoints used */
|
||||
0x0A, /* bInterfaceClass: CDC */
|
||||
0x00, /* bInterfaceSubClass */
|
||||
0x00, /* bInterfaceProtocol */
|
||||
0x00, /* iInterface */
|
||||
|
||||
/* Endpoint OUT Descriptor */
|
||||
0x07, /* bLength: Endpoint Descriptor size */
|
||||
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
|
||||
CDC_OUT_EP, /* bEndpointAddress */
|
||||
0x02, /* bmAttributes: Bulk */
|
||||
LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize */
|
||||
HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), CDC_FS_BINTERVAL, /* bInterval */
|
||||
|
||||
/* Endpoint IN Descriptor */
|
||||
0x07, /* bLength: Endpoint Descriptor size */
|
||||
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
|
||||
CDC_IN_EP, /* bEndpointAddress */
|
||||
0x02, /* bmAttributes: Bulk */
|
||||
LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize */
|
||||
HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), CDC_FS_BINTERVAL, /* bInterval */
|
||||
};
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
extern USBD_HandleTypeDef hUsbDeviceFS;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Local Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
static uint8_t CDCInEpAdd = CDC_IN_EP;
|
||||
static uint8_t CDCOutEpAdd = CDC_OUT_EP;
|
||||
static uint8_t CDCCmdEpAdd = CDC_CMD_EP;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief USBD_CDC_Init
|
||||
* Initialize the CDC interface
|
||||
* @param pdev: device instance
|
||||
* @param cfgidx: Configuration index
|
||||
* @retval status
|
||||
*/
|
||||
static uint8_t USBD_CDC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
|
||||
{
|
||||
UNUSED(cfgidx);
|
||||
USBD_CDC_HandleTypeDef *hcdc;
|
||||
|
||||
hcdc =
|
||||
(USBD_CDC_HandleTypeDef *)USBD_malloc(sizeof(USBD_CDC_HandleTypeDef));
|
||||
|
||||
if (hcdc == NULL) {
|
||||
pdev->pClassDataCmsit[pdev->classId] = NULL;
|
||||
return (uint8_t)USBD_EMEM;
|
||||
}
|
||||
|
||||
(void)USBD_memset(hcdc, 0, sizeof(USBD_CDC_HandleTypeDef));
|
||||
|
||||
pdev->pClassDataCmsit[pdev->classId] = (void *)hcdc;
|
||||
pdev->pClassData = pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
/* Open EP IN */
|
||||
(void)USBD_LL_OpenEP(pdev, CDCInEpAdd, USBD_EP_TYPE_BULK,
|
||||
CDC_DATA_FS_IN_PACKET_SIZE);
|
||||
|
||||
pdev->ep_in[CDCInEpAdd & 0xFU].is_used = 1U;
|
||||
|
||||
/* Open EP OUT */
|
||||
(void)USBD_LL_OpenEP(pdev, CDCOutEpAdd, USBD_EP_TYPE_BULK,
|
||||
CDC_DATA_FS_OUT_PACKET_SIZE);
|
||||
|
||||
pdev->ep_out[CDCOutEpAdd & 0xFU].is_used = 1U;
|
||||
|
||||
/* Set bInterval for CMD Endpoint */
|
||||
pdev->ep_in[CDCCmdEpAdd & 0xFU].bInterval = CDC_FS_BINTERVAL;
|
||||
|
||||
/* Open Command IN EP */
|
||||
(void)USBD_LL_OpenEP(pdev, CDCCmdEpAdd, USBD_EP_TYPE_INTR,
|
||||
CDC_CMD_PACKET_SIZE);
|
||||
pdev->ep_in[CDCCmdEpAdd & 0xFU].is_used = 1U;
|
||||
|
||||
hcdc->RxBuffer = NULL;
|
||||
|
||||
/* Init physical Interface components */
|
||||
((USBD_CDC_ItfTypeDef *)pdev->pUserData[pdev->classId])->Init();
|
||||
|
||||
/* Init Xfer states */
|
||||
hcdc->TxState = 0U;
|
||||
hcdc->RxState = 0U;
|
||||
|
||||
if (hcdc->RxBuffer == NULL) {
|
||||
return (uint8_t)USBD_EMEM;
|
||||
}
|
||||
|
||||
/* Prepare Out endpoint to receive next packet */
|
||||
(void)USBD_LL_PrepareReceive(pdev, CDCOutEpAdd, hcdc->RxBuffer,
|
||||
CDC_DATA_FS_OUT_PACKET_SIZE);
|
||||
|
||||
return (uint8_t)USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CDC_Init
|
||||
* DeInitialize the CDC layer
|
||||
* @param pdev: device instance
|
||||
* @param cfgidx: Configuration index
|
||||
* @retval status
|
||||
*/
|
||||
static uint8_t USBD_CDC_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
|
||||
{
|
||||
UNUSED(cfgidx);
|
||||
|
||||
/* Close EP IN */
|
||||
(void)USBD_LL_CloseEP(pdev, CDCInEpAdd);
|
||||
pdev->ep_in[CDCInEpAdd & 0xFU].is_used = 0U;
|
||||
|
||||
/* Close EP OUT */
|
||||
(void)USBD_LL_CloseEP(pdev, CDCOutEpAdd);
|
||||
pdev->ep_out[CDCOutEpAdd & 0xFU].is_used = 0U;
|
||||
|
||||
/* Close Command IN EP */
|
||||
(void)USBD_LL_CloseEP(pdev, CDCCmdEpAdd);
|
||||
pdev->ep_in[CDCCmdEpAdd & 0xFU].is_used = 0U;
|
||||
pdev->ep_in[CDCCmdEpAdd & 0xFU].bInterval = 0U;
|
||||
|
||||
/* DeInit physical Interface components */
|
||||
if (pdev->pClassDataCmsit[pdev->classId] != NULL) {
|
||||
((USBD_CDC_ItfTypeDef *)pdev->pUserData[pdev->classId])->DeInit();
|
||||
(void)USBD_free(pdev->pClassDataCmsit[pdev->classId]);
|
||||
pdev->pClassDataCmsit[pdev->classId] = NULL;
|
||||
pdev->pClassData = NULL;
|
||||
}
|
||||
|
||||
return (uint8_t)USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CDC_Setup
|
||||
* Handle the CDC specific requests
|
||||
* @param pdev: instance
|
||||
* @param req: usb requests
|
||||
* @retval status
|
||||
*/
|
||||
static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev,
|
||||
USBD_SetupReqTypedef *req)
|
||||
{
|
||||
USBD_CDC_HandleTypeDef *hcdc =
|
||||
(USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
uint16_t len;
|
||||
uint8_t ifalt = 0U;
|
||||
uint16_t status_info = 0U;
|
||||
USBD_StatusTypeDef ret = USBD_OK;
|
||||
|
||||
if (hcdc == NULL) {
|
||||
return (uint8_t)USBD_FAIL;
|
||||
}
|
||||
|
||||
switch (req->bmRequest & USB_REQ_TYPE_MASK) {
|
||||
case USB_REQ_TYPE_CLASS:
|
||||
if (req->wLength != 0U) {
|
||||
if ((req->bmRequest & 0x80U) != 0U) {
|
||||
((USBD_CDC_ItfTypeDef *)pdev->pUserData[pdev->classId])
|
||||
->Control(req->bRequest, (uint8_t *)hcdc->data,
|
||||
req->wLength);
|
||||
|
||||
len = MIN(CDC_REQ_MAX_DATA_SIZE, req->wLength);
|
||||
(void)USBD_CtlSendData(pdev, (uint8_t *)hcdc->data, len);
|
||||
} else {
|
||||
hcdc->CmdOpCode = req->bRequest;
|
||||
hcdc->CmdLength = (uint8_t)MIN(req->wLength, USB_MAX_EP0_SIZE);
|
||||
|
||||
(void)USBD_CtlPrepareRx(pdev, (uint8_t *)hcdc->data,
|
||||
hcdc->CmdLength);
|
||||
}
|
||||
} else {
|
||||
((USBD_CDC_ItfTypeDef *)pdev->pUserData[pdev->classId])
|
||||
->Control(req->bRequest, (uint8_t *)req, 0U);
|
||||
}
|
||||
break;
|
||||
|
||||
case USB_REQ_TYPE_STANDARD:
|
||||
switch (req->bRequest) {
|
||||
case USB_REQ_GET_STATUS:
|
||||
if (pdev->dev_state == USBD_STATE_CONFIGURED) {
|
||||
(void)USBD_CtlSendData(pdev, (uint8_t *)&status_info, 2U);
|
||||
} else {
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
}
|
||||
break;
|
||||
|
||||
case USB_REQ_GET_INTERFACE:
|
||||
if (pdev->dev_state == USBD_STATE_CONFIGURED) {
|
||||
(void)USBD_CtlSendData(pdev, &ifalt, 1U);
|
||||
} else {
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
}
|
||||
break;
|
||||
|
||||
case USB_REQ_SET_INTERFACE:
|
||||
if (pdev->dev_state != USBD_STATE_CONFIGURED) {
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
}
|
||||
break;
|
||||
|
||||
case USB_REQ_CLEAR_FEATURE:
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
break;
|
||||
}
|
||||
|
||||
return (uint8_t)ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CDC_DataIn
|
||||
* Data sent on non-control IN endpoint
|
||||
* @param pdev: device instance
|
||||
* @param epnum: endpoint number
|
||||
* @retval status
|
||||
*/
|
||||
static uint8_t USBD_CDC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
|
||||
{
|
||||
USBD_CDC_HandleTypeDef *hcdc;
|
||||
PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef *)pdev->pData;
|
||||
|
||||
if (pdev->pClassDataCmsit[pdev->classId] == NULL) {
|
||||
return (uint8_t)USBD_FAIL;
|
||||
}
|
||||
|
||||
hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
if ((pdev->ep_in[epnum & 0xFU].total_length > 0U) &&
|
||||
((pdev->ep_in[epnum & 0xFU].total_length %
|
||||
hpcd->IN_ep[epnum & 0xFU].maxpacket) == 0U)) {
|
||||
/* Update the packet total length */
|
||||
pdev->ep_in[epnum & 0xFU].total_length = 0U;
|
||||
|
||||
/* Send ZLP */
|
||||
(void)USBD_LL_Transmit(pdev, epnum, NULL, 0U);
|
||||
} else {
|
||||
hcdc->TxState = 0U;
|
||||
|
||||
if (((USBD_CDC_ItfTypeDef *)pdev->pUserData[pdev->classId])
|
||||
->TransmitCplt != NULL) {
|
||||
((USBD_CDC_ItfTypeDef *)pdev->pUserData[pdev->classId])
|
||||
->TransmitCplt(hcdc->TxBuffer, &hcdc->TxLength, epnum);
|
||||
}
|
||||
}
|
||||
|
||||
return (uint8_t)USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CDC_DataOut
|
||||
* Data received on non-control Out endpoint
|
||||
* @param pdev: device instance
|
||||
* @param epnum: endpoint number
|
||||
* @retval status
|
||||
*/
|
||||
static uint8_t USBD_CDC_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum)
|
||||
{
|
||||
USBD_CDC_HandleTypeDef *hcdc =
|
||||
(USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
if (pdev->pClassDataCmsit[pdev->classId] == NULL) {
|
||||
return (uint8_t)USBD_FAIL;
|
||||
}
|
||||
|
||||
/* Get the received data length */
|
||||
hcdc->RxLength = USBD_LL_GetRxDataSize(pdev, epnum);
|
||||
|
||||
/* USB data will be immediately processed, this allow next USB traffic being
|
||||
NAKed till the end of the application Xfer */
|
||||
|
||||
((USBD_CDC_ItfTypeDef *)pdev->pUserData[pdev->classId])
|
||||
->Receive(hcdc->RxBuffer, &hcdc->RxLength);
|
||||
|
||||
USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &hcdc->RxBuffer[0]);
|
||||
|
||||
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
|
||||
|
||||
return (uint8_t)USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CDC_EP0_RxReady
|
||||
* Handle EP0 Rx Ready event
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
static uint8_t USBD_CDC_EP0_RxReady(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
USBD_CDC_HandleTypeDef *hcdc =
|
||||
(USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
if (hcdc == NULL) {
|
||||
return (uint8_t)USBD_FAIL;
|
||||
}
|
||||
|
||||
if ((pdev->pUserData[pdev->classId] != NULL) &&
|
||||
(hcdc->CmdOpCode != 0xFFU)) {
|
||||
((USBD_CDC_ItfTypeDef *)pdev->pUserData[pdev->classId])
|
||||
->Control(hcdc->CmdOpCode, (uint8_t *)hcdc->data,
|
||||
(uint16_t)hcdc->CmdLength);
|
||||
hcdc->CmdOpCode = 0xFFU;
|
||||
}
|
||||
|
||||
return (uint8_t)USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CDC_GetFSCfgDesc
|
||||
* Return configuration descriptor
|
||||
* @param length : pointer data length
|
||||
* @retval pointer to descriptor buffer
|
||||
*/
|
||||
static uint8_t *USBD_CDC_GetFSCfgDesc(uint16_t *length)
|
||||
{
|
||||
USBD_EpDescTypeDef *pEpCmdDesc =
|
||||
USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_CMD_EP);
|
||||
USBD_EpDescTypeDef *pEpOutDesc =
|
||||
USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_OUT_EP);
|
||||
USBD_EpDescTypeDef *pEpInDesc = USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_IN_EP);
|
||||
|
||||
if (pEpCmdDesc != NULL) {
|
||||
pEpCmdDesc->bInterval = CDC_FS_BINTERVAL;
|
||||
}
|
||||
|
||||
if (pEpOutDesc != NULL) {
|
||||
pEpOutDesc->wMaxPacketSize = CDC_DATA_FS_MAX_PACKET_SIZE;
|
||||
}
|
||||
|
||||
if (pEpInDesc != NULL) {
|
||||
pEpInDesc->wMaxPacketSize = CDC_DATA_FS_MAX_PACKET_SIZE;
|
||||
}
|
||||
|
||||
*length = (uint16_t)sizeof(USBD_CDC_CfgDesc);
|
||||
return USBD_CDC_CfgDesc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CDC_GetHSCfgDesc
|
||||
* Return configuration descriptor
|
||||
* @param length : pointer data length
|
||||
* @retval pointer to descriptor buffer
|
||||
*/
|
||||
static uint8_t *USBD_CDC_GetHSCfgDesc(uint16_t *length)
|
||||
{
|
||||
// USBD_EpDescTypeDef *pEpCmdDesc = USBD_GetEpDesc(USBD_CDC_CfgDesc,
|
||||
// CDC_CMD_EP); USBD_EpDescTypeDef *pEpOutDesc =
|
||||
// USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_OUT_EP); USBD_EpDescTypeDef
|
||||
// *pEpInDesc = USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_IN_EP);
|
||||
|
||||
// if (pEpCmdDesc != NULL)
|
||||
// {
|
||||
// pEpCmdDesc->bInterval = CDC_HS_BINTERVAL;
|
||||
// }
|
||||
|
||||
// if (pEpOutDesc != NULL)
|
||||
// {
|
||||
// pEpOutDesc->wMaxPacketSize = CDC_DATA_HS_MAX_PACKET_SIZE;
|
||||
// }
|
||||
|
||||
// if (pEpInDesc != NULL)
|
||||
// {
|
||||
// pEpInDesc->wMaxPacketSize = CDC_DATA_HS_MAX_PACKET_SIZE;
|
||||
// }
|
||||
|
||||
// *length = (uint16_t)sizeof(USBD_CDC_CfgDesc);
|
||||
return USBD_CDC_CfgDesc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CDC_GetOtherSpeedCfgDesc
|
||||
* Return configuration descriptor
|
||||
* @param length : pointer data length
|
||||
* @retval pointer to descriptor buffer
|
||||
*/
|
||||
static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc(uint16_t *length)
|
||||
{
|
||||
USBD_EpDescTypeDef *pEpCmdDesc =
|
||||
USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_CMD_EP);
|
||||
USBD_EpDescTypeDef *pEpOutDesc =
|
||||
USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_OUT_EP);
|
||||
USBD_EpDescTypeDef *pEpInDesc = USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_IN_EP);
|
||||
|
||||
if (pEpCmdDesc != NULL) {
|
||||
pEpCmdDesc->bInterval = CDC_FS_BINTERVAL;
|
||||
}
|
||||
|
||||
if (pEpOutDesc != NULL) {
|
||||
pEpOutDesc->wMaxPacketSize = CDC_DATA_FS_MAX_PACKET_SIZE;
|
||||
}
|
||||
|
||||
if (pEpInDesc != NULL) {
|
||||
pEpInDesc->wMaxPacketSize = CDC_DATA_FS_MAX_PACKET_SIZE;
|
||||
}
|
||||
|
||||
*length = (uint16_t)sizeof(USBD_CDC_CfgDesc);
|
||||
return USBD_CDC_CfgDesc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CDC_GetDeviceQualifierDescriptor
|
||||
* return Device Qualifier descriptor
|
||||
* @param length : pointer data length
|
||||
* @retval pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t *USBD_CDC_GetDeviceQualifierDescriptor(uint16_t *length)
|
||||
{
|
||||
*length = (uint16_t)sizeof(USBD_CDC_DeviceQualifierDesc);
|
||||
|
||||
return USBD_CDC_DeviceQualifierDesc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CDC_RegisterInterface
|
||||
* @param pdev: device instance
|
||||
* @param fops: CD Interface callback
|
||||
* @retval status
|
||||
*/
|
||||
uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev,
|
||||
USBD_CDC_ItfTypeDef *fops)
|
||||
{
|
||||
if (fops == NULL) {
|
||||
return (uint8_t)USBD_FAIL;
|
||||
}
|
||||
|
||||
pdev->pUserData[pdev->classId] = fops;
|
||||
|
||||
return (uint8_t)USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CDC_SetTxBuffer
|
||||
* @param pdev: device instance
|
||||
* @param pbuff: Tx Buffer
|
||||
* @param length: Tx Buffer length
|
||||
* @retval status
|
||||
*/
|
||||
uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff,
|
||||
uint32_t length)
|
||||
{
|
||||
USBD_CDC_HandleTypeDef *hcdc =
|
||||
(USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
if (hcdc == NULL) {
|
||||
return (uint8_t)USBD_FAIL;
|
||||
}
|
||||
|
||||
hcdc->TxBuffer = pbuff;
|
||||
hcdc->TxLength = length;
|
||||
|
||||
return (uint8_t)USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CDC_SetRxBuffer
|
||||
* @param pdev: device instance
|
||||
* @param pbuff: Rx Buffer
|
||||
* @retval status
|
||||
*/
|
||||
uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff)
|
||||
{
|
||||
USBD_CDC_HandleTypeDef *hcdc =
|
||||
(USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
if (hcdc == NULL) {
|
||||
return (uint8_t)USBD_FAIL;
|
||||
}
|
||||
|
||||
hcdc->RxBuffer = pbuff;
|
||||
|
||||
return (uint8_t)USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CDC_TransmitPacket
|
||||
* Transmit packet on IN endpoint
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
extern volatile uint32_t epnum_change;
|
||||
uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, uint8_t epnum)
|
||||
{
|
||||
USBD_CDC_HandleTypeDef *hcdc =
|
||||
(USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
USBD_StatusTypeDef ret = USBD_BUSY;
|
||||
|
||||
if (pdev->pClassDataCmsit[pdev->classId] == NULL) {
|
||||
return (uint8_t)USBD_FAIL;
|
||||
}
|
||||
|
||||
if (hcdc->TxState == 0U) {
|
||||
|
||||
/* Tx Transfer in progress */
|
||||
hcdc->TxState = 1U;
|
||||
|
||||
/* Update the packet total length */
|
||||
pdev->ep_in[epnum & 0xFU].total_length = hcdc->TxLength;
|
||||
epnum_change = (epnum & 0xFU);
|
||||
|
||||
/* Transmit next packet */
|
||||
(void)USBD_LL_Transmit(pdev, epnum, hcdc->TxBuffer, hcdc->TxLength);
|
||||
|
||||
// /* Update the packet total length */
|
||||
// pdev->ep_in[CDCInEpAdd & 0xFU].total_length = hcdc->TxLength;
|
||||
// /* Transmit next packet */
|
||||
// (void)USBD_LL_Transmit(pdev, CDCInEpAdd, hcdc->TxBuffer,
|
||||
// hcdc->TxLength);
|
||||
|
||||
ret = USBD_OK;
|
||||
}
|
||||
|
||||
return (uint8_t)ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_CDC_ReceivePacket
|
||||
* prepare OUT Endpoint for reception
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
USBD_CDC_HandleTypeDef *hcdc =
|
||||
(USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
if (pdev->pClassDataCmsit[pdev->classId] == NULL) {
|
||||
return (uint8_t)USBD_FAIL;
|
||||
}
|
||||
|
||||
/* Prepare Out endpoint to receive next packet */
|
||||
(void)USBD_LL_PrepareReceive(pdev, CDCOutEpAdd, hcdc->RxBuffer,
|
||||
CDC_DATA_FS_OUT_PACKET_SIZE);
|
||||
|
||||
return (uint8_t)USBD_OK;
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
/*!
|
||||
* \file usbd_cdc.h
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author MCD Application Team
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USB_CDC_H
|
||||
#define __USB_CDC_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_ioreq.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup usbd_cdc_Exported_Defines
|
||||
* @{
|
||||
*/
|
||||
#ifndef CDC_IN_EP
|
||||
#define CDC_IN_EP 0x81U /* EP1 for data IN */
|
||||
#endif /* CDC_IN_EP */
|
||||
#ifndef CDC_OUT_EP
|
||||
#define CDC_OUT_EP 0x01U /* EP1 for data OUT */
|
||||
#endif /* CDC_OUT_EP */
|
||||
#ifndef CDC_CMD_EP
|
||||
#define CDC_CMD_EP 0x82U /* EP2 for CDC commands */
|
||||
#endif /* CDC_CMD_EP */
|
||||
|
||||
#ifndef CDC_FS_BINTERVAL
|
||||
#define CDC_FS_BINTERVAL 0x10U
|
||||
#endif /* CDC_FS_BINTERVAL */
|
||||
|
||||
/* CDC Endpoints parameters: you can fine tune these values depending on the needed baudrates and performance. */
|
||||
#define CDC_DATA_FS_MAX_PACKET_SIZE 64U /* Endpoint IN & OUT Packet size */
|
||||
#define CDC_CMD_PACKET_SIZE 8U /* Control Endpoint Packet size */
|
||||
|
||||
#define USB_CDC_CONFIG_DESC_SIZ 67U
|
||||
|
||||
#define CDC_DATA_FS_IN_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE
|
||||
#define CDC_DATA_FS_OUT_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE
|
||||
|
||||
#define CDC_REQ_MAX_DATA_SIZE 0x7U
|
||||
/*---------------------------------------------------------------------*/
|
||||
/* CDC definitions */
|
||||
/*---------------------------------------------------------------------*/
|
||||
#define CDC_SEND_ENCAPSULATED_COMMAND 0x00U
|
||||
#define CDC_GET_ENCAPSULATED_RESPONSE 0x01U
|
||||
#define CDC_SET_COMM_FEATURE 0x02U
|
||||
#define CDC_GET_COMM_FEATURE 0x03U
|
||||
#define CDC_CLEAR_COMM_FEATURE 0x04U
|
||||
#define CDC_SET_LINE_CODING 0x20U
|
||||
#define CDC_GET_LINE_CODING 0x21U
|
||||
#define CDC_SET_CONTROL_LINE_STATE 0x22U
|
||||
#define CDC_SEND_BREAK 0x23U
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Typedef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_CORE_Exported_TypesDefinitions
|
||||
* @{
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint32_t bitrate;
|
||||
uint8_t format;
|
||||
uint8_t paritytype;
|
||||
uint8_t datatype;
|
||||
} USBD_CDC_LineCodingTypeDef;
|
||||
|
||||
typedef struct _USBD_CDC_Itf
|
||||
{
|
||||
int8_t (* Init)(void);
|
||||
int8_t (* DeInit)(void);
|
||||
int8_t (* Control)(uint8_t cmd, uint8_t *pbuf, uint16_t length);
|
||||
int8_t (* Receive)(uint8_t *Buf, uint32_t *Len);
|
||||
int8_t (* TransmitCplt)(uint8_t *Buf, uint32_t *Len, uint8_t epnum);
|
||||
} USBD_CDC_ItfTypeDef;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t data[CDC_DATA_FS_MAX_PACKET_SIZE * 2U]; /* Force 32-bit alignment */
|
||||
uint8_t CmdOpCode;
|
||||
uint8_t CmdLength;
|
||||
uint8_t *RxBuffer;
|
||||
uint8_t *TxBuffer;
|
||||
uint32_t RxLength;
|
||||
uint32_t TxLength;
|
||||
|
||||
__IO uint32_t TxState;
|
||||
__IO uint32_t RxState;
|
||||
} USBD_CDC_HandleTypeDef;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_CORE_Exported_Variables
|
||||
* @{
|
||||
*/
|
||||
|
||||
extern USBD_ClassTypeDef USBD_CDC;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USB_CORE_Exported_Functions
|
||||
* @{
|
||||
*/
|
||||
uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev,
|
||||
USBD_CDC_ItfTypeDef *fops);
|
||||
|
||||
uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff,
|
||||
uint32_t length);
|
||||
|
||||
uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff);
|
||||
|
||||
uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev);
|
||||
|
||||
uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, uint8_t epnum);
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USB_CDC_H */
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,264 @@
|
||||
/*!
|
||||
* \file usbd_hid.h
|
||||
*
|
||||
* \brief This file provides the HID core functions.
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author MCD Application Team
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USB_HID_H
|
||||
#define __USB_HID_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_ioreq.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_HID_Exported_Defines
|
||||
* @{
|
||||
*/
|
||||
#if (HID_CLASS_MODE == HID_MOUSE) /* HID_MOUSE */
|
||||
#define HID_EPIN_ADDR 0x81U
|
||||
#define HID_EPIN_SIZE 0x04U
|
||||
|
||||
#define USB_HID_CONFIG_DESC_SIZ 34U
|
||||
#elif (HID_CLASS_MODE == HID_KEYBOARD) /* HID_KEYBOARD */
|
||||
#define HID_EPIN_ADDR 0x81U
|
||||
#define HID_EPIN_SIZE 0x08U
|
||||
|
||||
#define HID_EPOUT_ADDR 0x01U
|
||||
#define HID_EPOUT_SIZE 0x01U
|
||||
|
||||
#define USB_HID_CONFIG_DESC_SIZ 41U
|
||||
|
||||
#define USBD_HID_OUTREPORT_BUF_SIZE 1U
|
||||
#elif (HID_CLASS_MODE == HID_CUSTOM) /* HID_CUSTOM */
|
||||
#define HID_EPIN_ADDR 0x81U
|
||||
#define HID_EPIN_SIZE 0x40U
|
||||
|
||||
#define HID_EPOUT_ADDR 0x01U
|
||||
#define HID_EPOUT_SIZE 0x40U
|
||||
|
||||
#define USB_HID_CONFIG_DESC_SIZ 41U
|
||||
|
||||
#define USBD_HID_OUTREPORT_BUF_SIZE 0x40U
|
||||
|
||||
#elif ((HID_CLASS_MODE == (HID_MOUSE | HID_CUSTOM)) || \
|
||||
(HID_CLASS_MODE == (HID_KEYBOARD | HID_CUSTOM)))/* (HID_MOUSE | HID_CUSTOM) */
|
||||
|
||||
#if ((HID_CLASS_MODE & HID_MOUSE) == HID_MOUSE)
|
||||
#define HID_EPIN_ADDR 0x81U
|
||||
#define HID_EPIN_SIZE 0x04U
|
||||
#endif
|
||||
|
||||
#if ((HID_CLASS_MODE & HID_KEYBOARD) == HID_KEYBOARD)
|
||||
#define HID_EPIN_ADDR 0x81U
|
||||
#define HID_EPIN_SIZE 0x08U
|
||||
|
||||
#define HID_EPOUT_ADDR 0x01U
|
||||
#define HID_EPOUT_SIZE 0x01U
|
||||
|
||||
#define USBD_HID_OUTREPORT_BUF_SIZE 1U
|
||||
#endif
|
||||
|
||||
#define CUSTOM_HID_EPIN_ADDR 0x82U
|
||||
#define CUSTOM_HID_EPIN_SIZE 0x40U
|
||||
|
||||
#define CUSTOM_HID_EPOUT_ADDR 0x02U
|
||||
#define CUSTOM_HID_EPOUT_SIZE 0x40U
|
||||
|
||||
#define CUSTOM_HID_FS_BINTERVAL 0x5U
|
||||
|
||||
#define USBD_CUSTOMHID_OUTREPORT_BUF_SIZE 0x40U
|
||||
|
||||
#if ((HID_CLASS_MODE & HID_MOUSE) == HID_MOUSE)
|
||||
#define USB_HID_CONFIG_DESC_SIZ 66U
|
||||
#endif
|
||||
#if ((HID_CLASS_MODE & HID_KEYBOARD) == HID_KEYBOARD)
|
||||
#define USB_HID_CONFIG_DESC_SIZ 73U
|
||||
#endif
|
||||
|
||||
|
||||
#define HID_DATAIN_EPNUM (HID_EPIN_ADDR & 0x0FU)
|
||||
#define CUSTOM_HID_DATAIN_EPNUM (CUSTOM_HID_EPIN_ADDR & 0x0FU)
|
||||
|
||||
#elif (HID_CLASS_MODE == (HID_KEYBOARD | HID_MOUSE))
|
||||
|
||||
#define HID_EP1IN_ADDR 0x81U
|
||||
#define HID_EP1IN_SIZE 0x08U
|
||||
|
||||
#define HID_EP1OUT_ADDR 0x01U
|
||||
#define HID_EP1OUT_SIZE 0x01U
|
||||
|
||||
#define USBD_HID_OUTREPORT_BUF_SIZE 1U
|
||||
|
||||
#define HID_EP2IN_ADDR 0x82U
|
||||
#define HID_EP2IN_SIZE 0x04U
|
||||
|
||||
#define USB_HID_CONFIG_DESC_SIZ 66U
|
||||
|
||||
#define HID_DATAIN_EPNUM_1 (HID_EP1IN_ADDR & 0x0FU)
|
||||
#define HID_DATAIN_EPNUM_2 (HID_EP2IN_ADDR & 0x0FU)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#define USB_HID_DESC_SIZ 9U
|
||||
#define HID_MOUSE_REPORT_DESC_SIZE 74U
|
||||
#define HID_KEYBOARD_REPORT_DESC_SIZE 63U
|
||||
#define USBD_CUSTOM_REPORT_DESC_SIZE 34U//43U //39U//34U
|
||||
|
||||
#define HID_DESCRIPTOR_TYPE 0x21U
|
||||
#define HID_REPORT_DESC 0x22U
|
||||
|
||||
#ifndef HID_HS_BINTERVAL
|
||||
#define HID_HS_BINTERVAL 0x07U
|
||||
#endif /* HID_HS_BINTERVAL */
|
||||
|
||||
#ifndef HID_FS_BINTERVAL
|
||||
#if ((HID_CLASS_MODE== HID_MOUSE) || \
|
||||
(HID_CLASS_MODE == HID_KEYBOARD))
|
||||
#define HID_FS_BINTERVAL 0x01U
|
||||
#else
|
||||
#define HID_FS_BINTERVAL 0x01U
|
||||
#endif
|
||||
#endif /* HID_FS_BINTERVAL */
|
||||
|
||||
#define HID_REQ_SET_PROTOCOL 0x0BU
|
||||
#define HID_REQ_GET_PROTOCOL 0x03U
|
||||
|
||||
#define HID_REQ_SET_IDLE 0x0AU
|
||||
#define HID_REQ_GET_IDLE 0x02U
|
||||
|
||||
#define HID_REQ_SET_REPORT 0x09U
|
||||
#define HID_REQ_GET_REPORT 0x01U
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Typedef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_CORE_Exported_TypesDefinitions
|
||||
* @{
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
HID_IDLE = 0,
|
||||
HID_BUSY,
|
||||
} HID_StateTypeDef;
|
||||
|
||||
#if (HID_CLASS_MODE == HID_MOUSE)
|
||||
typedef struct
|
||||
{
|
||||
uint32_t Protocol;
|
||||
uint32_t IdleState;
|
||||
uint32_t AltSetting;
|
||||
HID_StateTypeDef state;
|
||||
} USBD_HID_HandleTypeDef;
|
||||
#else
|
||||
typedef struct
|
||||
{
|
||||
uint8_t Report_buf[USBD_HID_OUTREPORT_BUF_SIZE];
|
||||
uint32_t Protocol;
|
||||
uint32_t IdleState;
|
||||
uint32_t AltSetting;
|
||||
uint32_t IsReportAvailable;
|
||||
HID_StateTypeDef state;
|
||||
} USBD_HID_HandleTypeDef;
|
||||
#endif
|
||||
|
||||
|
||||
//#if ((HID_CLASS_MODE & HID_CUSTOM) == HID_CUSTOM)
|
||||
///** @defgroup USBD_CORE_Exported_TypesDefinitions
|
||||
// * @{
|
||||
// */
|
||||
//typedef enum
|
||||
//{
|
||||
// CUSTOM_HID_IDLE = 0U,
|
||||
// CUSTOM_HID_BUSY,
|
||||
//} CUSTOM_HID_StateTypeDef;
|
||||
|
||||
//typedef struct
|
||||
//{
|
||||
// uint8_t Report_buf[USBD_CUSTOMHID_OUTREPORT_BUF_SIZE];
|
||||
// uint32_t Protocol;
|
||||
// uint32_t IdleState;
|
||||
// uint32_t AltSetting;
|
||||
// uint32_t IsReportAvailable;
|
||||
// CUSTOM_HID_StateTypeDef state;
|
||||
//} USBD_CUSTOM_HID_HandleTypeDef;
|
||||
//#endif
|
||||
|
||||
/*
|
||||
* HID Class specification version 1.1
|
||||
* 6.2.1 HID Descriptor
|
||||
*/
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint16_t bcdHID;
|
||||
uint8_t bCountryCode;
|
||||
uint8_t bNumDescriptors;
|
||||
uint8_t bHIDDescriptorType;
|
||||
uint16_t wItemLength;
|
||||
} __PACKED USBD_HIDDescTypeDef;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
extern USBD_ClassTypeDef USBD_HID;
|
||||
#if (HID_CLASS_MODE == HID_KEYBOARD)
|
||||
extern uint8_t Report_buff[1];
|
||||
#endif
|
||||
|
||||
extern uint8_t DataOut_Finished;
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
uint8_t USBD_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *report, uint16_t len);
|
||||
uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev);
|
||||
uint32_t USBD_HID_GetPollingInterval(USBD_HandleTypeDef *pdev);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USB_HID_H */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,275 @@
|
||||
/*!
|
||||
* \file usbd_hid.h
|
||||
*
|
||||
* \brief This file provides the HID core functions.
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author MCD Application Team
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USB_HID_H
|
||||
#define __USB_HID_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_ioreq.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_HID_Exported_Defines
|
||||
* @{
|
||||
*/
|
||||
|
||||
#if (HID_CLASS_MODE == (HID_KEYBOARD | HID_MOUSE))
|
||||
|
||||
#define HID_EP1IN_ADDR 0x81U
|
||||
#define HID_EP1IN_SIZE 0x08U
|
||||
|
||||
#define HID_EP1OUT_ADDR 0x01U
|
||||
#define HID_EP1OUT_SIZE 0x01U
|
||||
|
||||
#define USBD_HID_OUTREPORT_BUF_SIZE 1U
|
||||
|
||||
#define HID_EP2IN_ADDR 0x82U
|
||||
#define HID_EP2IN_SIZE 0x04U
|
||||
|
||||
#define USB_HID_CONFIG_DESC_SIZ 66U
|
||||
|
||||
#define HID_DATAIN_EPNUM_1 (HID_EP1IN_ADDR & 0x0FU)
|
||||
#define HID_DATAIN_EPNUM_2 (HID_EP2IN_ADDR & 0x0FU)
|
||||
|
||||
#endif
|
||||
|
||||
#if (HID_CLASS_MODE == (HID_MOUSE | HID_CUSTOM))
|
||||
#define HID_EPIN_ADDR 0x81U
|
||||
#define HID_EPIN_SIZE 0x04U
|
||||
|
||||
#define CUSTOM_HID_EPIN_ADDR 0x82U
|
||||
#define CUSTOM_HID_EPIN_SIZE 0x40U
|
||||
|
||||
#define CUSTOM_HID_EPOUT_ADDR 0x02U
|
||||
#define CUSTOM_HID_EPOUT_SIZE 0x40U
|
||||
|
||||
#define CUSTOM_HID_FS_BINTERVAL 0x5U
|
||||
|
||||
#define USBD_CUSTOMHID_OUTREPORT_BUF_SIZE 0x40U
|
||||
|
||||
#define HID_DATAIN_EPNUM (HID_EPIN_ADDR & 0x0FU)
|
||||
#define CUSTOM_HID_DATAIN_EPNUM (CUSTOM_HID_EPIN_ADDR & 0x0FU)
|
||||
|
||||
#define USB_HID_CONFIG_DESC_SIZ 66U
|
||||
#endif
|
||||
|
||||
#if (HID_CLASS_MODE == HID_DOUBLE_CUSTOM)
|
||||
#define HID_EPIN_ADDR 0x81U
|
||||
#define HID_EPIN_SIZE 0x40U
|
||||
|
||||
#define HID_EPOUT_ADDR 0x01U
|
||||
#define HID_EPOUT_SIZE 0x40U
|
||||
|
||||
#define HID_EP2IN_ADDR 0x82U
|
||||
#define HID_EP2IN_SIZE 0x40U
|
||||
|
||||
#define HID_EP2OUT_ADDR 0x02U
|
||||
#define HID_EP2OUT_SIZE 0x40U
|
||||
|
||||
#define USB_HID_CONFIG_DESC_SIZ 73U
|
||||
//#define USB_HID_DOUBLE_CUSTOM_CONFIG_DESC_SIZ 82U
|
||||
#define USBD_HID_OUTREPORT_BUF_SIZE 0x40U
|
||||
|
||||
#define HID_DATAIN_EPNUM_1 (HID_EPIN_ADDR & 0x0FU)
|
||||
#define HID_DATAIN_EPNUM_2 (HID_EP2IN_ADDR & 0x0FU)
|
||||
#endif
|
||||
|
||||
|
||||
#define USB_HID_DESC_SIZ 9U
|
||||
#define HID_MOUSE_REPORT_DESC_SIZE 74U
|
||||
#define HID_KEYBOARD_REPORT_DESC_SIZE 63U
|
||||
#define USBD_CUSTOM_REPORT_DESC_SIZE 34U
|
||||
|
||||
#define HID_DESCRIPTOR_TYPE 0x21U
|
||||
#define HID_REPORT_DESC 0x22U
|
||||
|
||||
#ifndef HID_HS_BINTERVAL
|
||||
#define HID_HS_BINTERVAL 0x07U
|
||||
#endif /* HID_HS_BINTERVAL */
|
||||
|
||||
#ifndef HID_FS_BINTERVAL
|
||||
#define HID_FS_BINTERVAL 0x01U
|
||||
#endif /* HID_FS_BINTERVAL */
|
||||
|
||||
#ifndef HID_FS_K_BINTERVAL
|
||||
#define HID_FS_K_BINTERVAL 0x0AU
|
||||
#endif /* HID_FS_BINTERVAL */
|
||||
|
||||
#ifndef HID_FS_M_BINTERVAL
|
||||
#define HID_FS_M_BINTERVAL 0x01U
|
||||
#endif /* HID_FS_BINTERVAL */
|
||||
|
||||
#define HID_REQ_SET_PROTOCOL 0x0BU
|
||||
#define HID_REQ_GET_PROTOCOL 0x03U
|
||||
|
||||
#define HID_REQ_SET_IDLE 0x0AU
|
||||
#define HID_REQ_GET_IDLE 0x02U
|
||||
|
||||
#define HID_REQ_SET_REPORT 0x09U
|
||||
#define HID_REQ_GET_REPORT 0x01U
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Typedef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_CORE_Exported_TypesDefinitions
|
||||
* @{
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
HID_IDLE = 0,
|
||||
HID_BUSY,
|
||||
} HID_StateTypeDef;
|
||||
|
||||
#if ((HID_CLASS_MODE & HID_MOUSE) == HID_MOUSE)
|
||||
typedef struct
|
||||
{
|
||||
uint32_t Protocol;
|
||||
uint32_t IdleState;
|
||||
uint32_t AltSetting;
|
||||
HID_StateTypeDef state;
|
||||
} USBD_HID_HandleTypeDef;
|
||||
#endif
|
||||
|
||||
#if ((HID_CLASS_MODE & HID_CUSTOM) == HID_CUSTOM)
|
||||
/** @defgroup USBD_CORE_Exported_TypesDefinitions
|
||||
* @{
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
CUSTOM_HID_IDLE = 0U,
|
||||
CUSTOM_HID_BUSY,
|
||||
} CUSTOM_HID_StateTypeDef;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t Report_buf[USBD_CUSTOMHID_OUTREPORT_BUF_SIZE];
|
||||
uint32_t Protocol;
|
||||
uint32_t IdleState;
|
||||
uint32_t AltSetting;
|
||||
uint32_t IsReportAvailable;
|
||||
CUSTOM_HID_StateTypeDef state;
|
||||
} USBD_CUSTOM_HID_HandleTypeDef;
|
||||
#endif
|
||||
|
||||
#if (HID_CLASS_MODE == HID_DOUBLE_CUSTOM)
|
||||
typedef struct
|
||||
{
|
||||
uint8_t Report_buf[USBD_HID_OUTREPORT_BUF_SIZE];
|
||||
uint32_t Protocol;
|
||||
uint32_t IdleState;
|
||||
uint32_t AltSetting;
|
||||
uint32_t IsReportAvailable;
|
||||
HID_StateTypeDef state;
|
||||
} USBD_HID_HandleTypeDef;
|
||||
#endif
|
||||
|
||||
|
||||
//#if ((HID_CLASS_MODE & HID_CUSTOM) == HID_CUSTOM)
|
||||
///** @defgroup USBD_CORE_Exported_TypesDefinitions
|
||||
// * @{
|
||||
// */
|
||||
//typedef enum
|
||||
//{
|
||||
// CUSTOM_HID_IDLE = 0U,
|
||||
// CUSTOM_HID_BUSY,
|
||||
//} CUSTOM_HID_StateTypeDef;
|
||||
|
||||
//typedef struct
|
||||
//{
|
||||
// uint8_t Report_buf[USBD_CUSTOMHID_OUTREPORT_BUF_SIZE];
|
||||
// uint32_t Protocol;
|
||||
// uint32_t IdleState;
|
||||
// uint32_t AltSetting;
|
||||
// uint32_t IsReportAvailable;
|
||||
// CUSTOM_HID_StateTypeDef state;
|
||||
//} USBD_CUSTOM_HID_HandleTypeDef;
|
||||
//#endif
|
||||
|
||||
/*
|
||||
* HID Class specification version 1.1
|
||||
* 6.2.1 HID Descriptor
|
||||
*/
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t bLength;
|
||||
uint8_t bDescriptorType;
|
||||
uint16_t bcdHID;
|
||||
uint8_t bCountryCode;
|
||||
uint8_t bNumDescriptors;
|
||||
uint8_t bHIDDescriptorType;
|
||||
uint16_t wItemLength;
|
||||
} __PACKED USBD_HIDDescTypeDef;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
extern USBD_ClassTypeDef USBD_HID;
|
||||
|
||||
#if ((HID_CLASS_MODE & HID_KEYBOARD) == HID_KEYBOARD)
|
||||
extern uint8_t Report_buff[1];
|
||||
#endif
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
#if (HID_CLASS_MODE == (HID_MOUSE | HID_KEYBOARD))
|
||||
uint8_t USBD_Mouse_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
|
||||
uint8_t USBD_Keyboard_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
|
||||
#elif (HID_CLASS_MODE == (HID_MOUSE | HID_CUSTOM))
|
||||
uint8_t USBD_Mouse_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
|
||||
uint8_t USBD_Custom_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
|
||||
#elif (HID_CLASS_MODE == HID_DOUBLE_CUSTOM)
|
||||
uint8_t USBD_Custom1_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
|
||||
uint8_t USBD_Custom2_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
|
||||
#endif
|
||||
uint32_t USBD_HID_GetPollingInterval(USBD_HandleTypeDef *pdev);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USB_HID_H */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,493 @@
|
||||
/*!
|
||||
* \file usbd_msc.c
|
||||
*
|
||||
* \brief This file provides all the MSC core functions.
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author MCD Application Team
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
|
||||
* @verbatim
|
||||
*
|
||||
* ===================================================================
|
||||
* MSC Class Description
|
||||
* ===================================================================
|
||||
* This module manages the MSC class V1.0 following the "Universal
|
||||
* Serial Bus Mass Storage Class (MSC) Bulk-Only Transport (BOT)
|
||||
Version 1.0
|
||||
* Sep. 31, 1999".
|
||||
* This driver implements the following aspects of the specification:
|
||||
* - Bulk-Only Transport protocol
|
||||
* - Subclass : SCSI transparent command set (ref. SCSI Primary
|
||||
Commands - 3 (SPC-3))
|
||||
*
|
||||
* @endverbatim
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_msc.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototype
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup MSC_CORE_Private_FunctionPrototypes
|
||||
* @{
|
||||
*/
|
||||
uint8_t USBD_MSC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
|
||||
uint8_t USBD_MSC_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
|
||||
uint8_t USBD_MSC_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
|
||||
uint8_t USBD_MSC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum);
|
||||
uint8_t USBD_MSC_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum);
|
||||
|
||||
uint8_t *USBD_MSC_GetHSCfgDesc(uint16_t *length);
|
||||
uint8_t *USBD_MSC_GetFSCfgDesc(uint16_t *length);
|
||||
uint8_t *USBD_MSC_GetOtherSpeedCfgDesc(uint16_t *length);
|
||||
uint8_t *USBD_MSC_GetDeviceQualifierDescriptor(uint16_t *length);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Local Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup MSC_CORE_Private_Variables
|
||||
* @{
|
||||
*/
|
||||
|
||||
USBD_ClassTypeDef USBD_MSC = {
|
||||
USBD_MSC_Init,
|
||||
USBD_MSC_DeInit,
|
||||
USBD_MSC_Setup,
|
||||
NULL, /*EP0_TxSent*/
|
||||
NULL, /*EP0_RxReady*/
|
||||
USBD_MSC_DataIn,
|
||||
USBD_MSC_DataOut,
|
||||
NULL, /*SOF */
|
||||
NULL,
|
||||
NULL,
|
||||
USBD_MSC_GetHSCfgDesc,
|
||||
USBD_MSC_GetFSCfgDesc,
|
||||
USBD_MSC_GetOtherSpeedCfgDesc,
|
||||
USBD_MSC_GetDeviceQualifierDescriptor,
|
||||
};
|
||||
|
||||
/* USB Mass storage device Configuration Descriptor */
|
||||
/* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */
|
||||
__ALIGN_BEGIN static uint8_t
|
||||
USBD_MSC_CfgDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END = {
|
||||
0x09, /* bLength: Configuration Descriptor size */
|
||||
USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
|
||||
USB_MSC_CONFIG_DESC_SIZ,
|
||||
|
||||
0x00, 0x01, /* bNumInterfaces: 1 interface */
|
||||
0x01, /* bConfigurationValue */
|
||||
0x04, /* iConfiguration */
|
||||
#if (USBD_SELF_POWERED == 1U)
|
||||
0xC0, /* bmAttributes: Bus Powered according to user configuration */
|
||||
#else
|
||||
0x80, /* bmAttributes: Bus Powered according to user configuration */
|
||||
#endif /* USBD_SELF_POWERED */
|
||||
USBD_MAX_POWER, /* MaxPower (mA) */
|
||||
|
||||
/******************** Mass Storage interface ********************/
|
||||
0x09, /* bLength: Interface Descriptor size */
|
||||
0x04, /* bDescriptorType: */
|
||||
0x00, /* bInterfaceNumber: Number of Interface */
|
||||
0x00, /* bAlternateSetting: Alternate setting */
|
||||
0x02, /* bNumEndpoints */
|
||||
0x08, /* bInterfaceClass: MSC Class */
|
||||
0x06, /* bInterfaceSubClass : SCSI transparent*/
|
||||
0x50, /* nInterfaceProtocol */
|
||||
0x05, /* iInterface: */
|
||||
/******************** Mass Storage Endpoints ********************/
|
||||
0x07, /* Endpoint descriptor length = 7 */
|
||||
0x05, /* Endpoint descriptor type */
|
||||
MSC_EPIN_ADDR, /* Endpoint address (IN, address 1) */
|
||||
0x02, /* Bulk endpoint type */
|
||||
LOBYTE(MSC_MAX_FS_PACKET), HIBYTE(MSC_MAX_FS_PACKET),
|
||||
0x00, /* Polling interval in milliseconds */
|
||||
|
||||
0x07, /* Endpoint descriptor length = 7 */
|
||||
0x05, /* Endpoint descriptor type */
|
||||
MSC_EPOUT_ADDR, /* Endpoint address (OUT, address 1) */
|
||||
0x02, /* Bulk endpoint type */
|
||||
LOBYTE(MSC_MAX_FS_PACKET), HIBYTE(MSC_MAX_FS_PACKET),
|
||||
0x00 /* Polling interval in milliseconds */
|
||||
};
|
||||
|
||||
/* USB Standard Device Descriptor */
|
||||
__ALIGN_BEGIN static uint8_t
|
||||
USBD_MSC_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = {
|
||||
USB_LEN_DEV_QUALIFIER_DESC,
|
||||
USB_DESC_TYPE_DEVICE_QUALIFIER,
|
||||
0x00,
|
||||
0x02,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
MSC_MAX_FS_PACKET,
|
||||
0x01,
|
||||
0x00,
|
||||
};
|
||||
|
||||
uint8_t MSCInEpAdd = MSC_EPIN_ADDR;
|
||||
uint8_t MSCOutEpAdd = MSC_EPOUT_ADDR;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief USBD_MSC_Init
|
||||
* Initialize the mass storage configuration
|
||||
* @param pdev: device instance
|
||||
* @param cfgidx: configuration index
|
||||
* @retval status
|
||||
*/
|
||||
uint8_t USBD_MSC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
|
||||
{
|
||||
UNUSED(cfgidx);
|
||||
USBD_MSC_BOT_HandleTypeDef *hmsc;
|
||||
|
||||
hmsc = (USBD_MSC_BOT_HandleTypeDef *)USBD_malloc(
|
||||
sizeof(USBD_MSC_BOT_HandleTypeDef));
|
||||
|
||||
if (hmsc == NULL) {
|
||||
pdev->pClassDataCmsit[pdev->classId] = NULL;
|
||||
return (uint8_t)USBD_EMEM;
|
||||
}
|
||||
|
||||
pdev->pClassDataCmsit[pdev->classId] = (void *)hmsc;
|
||||
pdev->pClassData = pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
if (pdev->dev_speed == USBD_SPEED_HIGH) {
|
||||
/* Open EP OUT */
|
||||
(void)USBD_LL_OpenEP(pdev, MSCOutEpAdd, USBD_EP_TYPE_BULK,
|
||||
MSC_MAX_HS_PACKET);
|
||||
pdev->ep_out[MSCOutEpAdd & 0xFU].is_used = 1U;
|
||||
|
||||
/* Open EP IN */
|
||||
(void)USBD_LL_OpenEP(pdev, MSCInEpAdd, USBD_EP_TYPE_BULK,
|
||||
MSC_MAX_HS_PACKET);
|
||||
pdev->ep_in[MSCInEpAdd & 0xFU].is_used = 1U;
|
||||
} else {
|
||||
/* Open EP OUT */
|
||||
(void)USBD_LL_OpenEP(pdev, MSCOutEpAdd, USBD_EP_TYPE_BULK,
|
||||
MSC_MAX_FS_PACKET);
|
||||
pdev->ep_out[MSCOutEpAdd & 0xFU].is_used = 1U;
|
||||
|
||||
/* Open EP IN */
|
||||
(void)USBD_LL_OpenEP(pdev, MSCInEpAdd, USBD_EP_TYPE_BULK,
|
||||
MSC_MAX_FS_PACKET);
|
||||
pdev->ep_in[MSCInEpAdd & 0xFU].is_used = 1U;
|
||||
}
|
||||
|
||||
/* Init the BOT layer */
|
||||
MSC_BOT_Init(pdev);
|
||||
|
||||
return (uint8_t)USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_MSC_DeInit
|
||||
* DeInitialize the mass storage configuration
|
||||
* @param pdev: device instance
|
||||
* @param cfgidx: configuration index
|
||||
* @retval status
|
||||
*/
|
||||
uint8_t USBD_MSC_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
|
||||
{
|
||||
UNUSED(cfgidx);
|
||||
|
||||
/* Close MSC EPs */
|
||||
(void)USBD_LL_CloseEP(pdev, MSCOutEpAdd);
|
||||
pdev->ep_out[MSCOutEpAdd & 0xFU].is_used = 0U;
|
||||
|
||||
/* Close EP IN */
|
||||
(void)USBD_LL_CloseEP(pdev, MSCInEpAdd);
|
||||
pdev->ep_in[MSCInEpAdd & 0xFU].is_used = 0U;
|
||||
|
||||
/* Free MSC Class Resources */
|
||||
if (pdev->pClassDataCmsit[pdev->classId] != NULL) {
|
||||
/* De-Init the BOT layer */
|
||||
MSC_BOT_DeInit(pdev);
|
||||
|
||||
(void)USBD_free(pdev->pClassDataCmsit[pdev->classId]);
|
||||
pdev->pClassDataCmsit[pdev->classId] = NULL;
|
||||
pdev->pClassData = NULL;
|
||||
}
|
||||
|
||||
return (uint8_t)USBD_OK;
|
||||
}
|
||||
/**
|
||||
* @brief USBD_MSC_Setup
|
||||
* Handle the MSC specific requests
|
||||
* @param pdev: device instance
|
||||
* @param req: USB request
|
||||
* @retval status
|
||||
*/
|
||||
uint8_t USBD_MSC_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
|
||||
{
|
||||
USBD_MSC_BOT_HandleTypeDef *hmsc =
|
||||
(USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
USBD_StatusTypeDef ret = USBD_OK;
|
||||
uint16_t status_info = 0U;
|
||||
|
||||
if (hmsc == NULL) {
|
||||
return (uint8_t)USBD_FAIL;
|
||||
}
|
||||
|
||||
switch (req->bmRequest & USB_REQ_TYPE_MASK) {
|
||||
/* Class request */
|
||||
case USB_REQ_TYPE_CLASS:
|
||||
switch (req->bRequest) {
|
||||
case BOT_GET_MAX_LUN:
|
||||
if ((req->wValue == 0U) && (req->wLength == 1U) &&
|
||||
((req->bmRequest & 0x80U) == 0x80U)) {
|
||||
hmsc->max_lun = (uint32_t)((USBD_StorageTypeDef *)
|
||||
pdev->pUserData[pdev->classId])
|
||||
->GetMaxLun();
|
||||
(void)USBD_CtlSendData(pdev, (uint8_t *)&hmsc->max_lun, 1U);
|
||||
} else {
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
}
|
||||
break;
|
||||
|
||||
case BOT_RESET:
|
||||
if ((req->wValue == 0U) && (req->wLength == 0U) &&
|
||||
((req->bmRequest & 0x80U) != 0x80U)) {
|
||||
MSC_BOT_Reset(pdev);
|
||||
} else {
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
/* Interface & Endpoint request */
|
||||
case USB_REQ_TYPE_STANDARD:
|
||||
switch (req->bRequest) {
|
||||
case USB_REQ_GET_STATUS:
|
||||
if (pdev->dev_state == USBD_STATE_CONFIGURED) {
|
||||
(void)USBD_CtlSendData(pdev, (uint8_t *)&status_info, 2U);
|
||||
} else {
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
}
|
||||
break;
|
||||
|
||||
case USB_REQ_GET_INTERFACE:
|
||||
if (pdev->dev_state == USBD_STATE_CONFIGURED) {
|
||||
(void)USBD_CtlSendData(pdev, (uint8_t *)&hmsc->interface, 1U);
|
||||
} else {
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
}
|
||||
break;
|
||||
|
||||
case USB_REQ_SET_INTERFACE:
|
||||
if (pdev->dev_state == USBD_STATE_CONFIGURED) {
|
||||
hmsc->interface = (uint8_t)(req->wValue);
|
||||
} else {
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
}
|
||||
break;
|
||||
|
||||
case USB_REQ_CLEAR_FEATURE:
|
||||
if (pdev->dev_state == USBD_STATE_CONFIGURED) {
|
||||
if (req->wValue == USB_FEATURE_EP_HALT) {
|
||||
/* Flush the FIFO */
|
||||
(void)USBD_LL_FlushEP(pdev, (uint8_t)req->wIndex);
|
||||
|
||||
/* Handle BOT error */
|
||||
MSC_BOT_CplClrFeature(pdev, (uint8_t)req->wIndex);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
break;
|
||||
}
|
||||
|
||||
return (uint8_t)ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_MSC_DataIn
|
||||
* handle data IN Stage
|
||||
* @param pdev: device instance
|
||||
* @param epnum: endpoint index
|
||||
* @retval status
|
||||
*/
|
||||
uint8_t USBD_MSC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
|
||||
{
|
||||
MSC_BOT_DataIn(pdev, epnum);
|
||||
|
||||
return (uint8_t)USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_MSC_DataOut
|
||||
* handle data OUT Stage
|
||||
* @param pdev: device instance
|
||||
* @param epnum: endpoint index
|
||||
* @retval status
|
||||
*/
|
||||
uint8_t USBD_MSC_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum)
|
||||
{
|
||||
MSC_BOT_DataOut(pdev, epnum);
|
||||
|
||||
return (uint8_t)USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_MSC_GetHSCfgDesc
|
||||
* return configuration descriptor
|
||||
* @param length : pointer data length
|
||||
* @retval pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t *USBD_MSC_GetHSCfgDesc(uint16_t *length)
|
||||
{
|
||||
USBD_EpDescTypeDef *pEpInDesc =
|
||||
USBD_GetEpDesc(USBD_MSC_CfgDesc, MSC_EPIN_ADDR);
|
||||
USBD_EpDescTypeDef *pEpOutDesc =
|
||||
USBD_GetEpDesc(USBD_MSC_CfgDesc, MSC_EPOUT_ADDR);
|
||||
|
||||
if (pEpInDesc != NULL) {
|
||||
pEpInDesc->wMaxPacketSize = MSC_MAX_HS_PACKET;
|
||||
}
|
||||
|
||||
if (pEpOutDesc != NULL) {
|
||||
pEpOutDesc->wMaxPacketSize = MSC_MAX_HS_PACKET;
|
||||
}
|
||||
|
||||
*length = (uint16_t)sizeof(USBD_MSC_CfgDesc);
|
||||
return USBD_MSC_CfgDesc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_MSC_GetFSCfgDesc
|
||||
* return configuration descriptor
|
||||
* @param length : pointer data length
|
||||
* @retval pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t *USBD_MSC_GetFSCfgDesc(uint16_t *length)
|
||||
{
|
||||
USBD_EpDescTypeDef *pEpInDesc =
|
||||
USBD_GetEpDesc(USBD_MSC_CfgDesc, MSC_EPIN_ADDR);
|
||||
USBD_EpDescTypeDef *pEpOutDesc =
|
||||
USBD_GetEpDesc(USBD_MSC_CfgDesc, MSC_EPOUT_ADDR);
|
||||
|
||||
if (pEpInDesc != NULL) {
|
||||
pEpInDesc->wMaxPacketSize = MSC_MAX_FS_PACKET;
|
||||
}
|
||||
|
||||
if (pEpOutDesc != NULL) {
|
||||
pEpOutDesc->wMaxPacketSize = MSC_MAX_FS_PACKET;
|
||||
}
|
||||
|
||||
*length = (uint16_t)sizeof(USBD_MSC_CfgDesc);
|
||||
return USBD_MSC_CfgDesc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_MSC_GetOtherSpeedCfgDesc
|
||||
* return other speed configuration descriptor
|
||||
* @param length : pointer data length
|
||||
* @retval pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t *USBD_MSC_GetOtherSpeedCfgDesc(uint16_t *length)
|
||||
{
|
||||
USBD_EpDescTypeDef *pEpInDesc =
|
||||
USBD_GetEpDesc(USBD_MSC_CfgDesc, MSC_EPIN_ADDR);
|
||||
USBD_EpDescTypeDef *pEpOutDesc =
|
||||
USBD_GetEpDesc(USBD_MSC_CfgDesc, MSC_EPOUT_ADDR);
|
||||
|
||||
if (pEpInDesc != NULL) {
|
||||
pEpInDesc->wMaxPacketSize = MSC_MAX_FS_PACKET;
|
||||
}
|
||||
|
||||
if (pEpOutDesc != NULL) {
|
||||
pEpOutDesc->wMaxPacketSize = MSC_MAX_FS_PACKET;
|
||||
}
|
||||
|
||||
*length = (uint16_t)sizeof(USBD_MSC_CfgDesc);
|
||||
return USBD_MSC_CfgDesc;
|
||||
}
|
||||
/**
|
||||
* @brief DeviceQualifierDescriptor
|
||||
* return Device Qualifier descriptor
|
||||
* @param length : pointer data length
|
||||
* @retval pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t *USBD_MSC_GetDeviceQualifierDescriptor(uint16_t *length)
|
||||
{
|
||||
*length = (uint16_t)sizeof(USBD_MSC_DeviceQualifierDesc);
|
||||
|
||||
return USBD_MSC_DeviceQualifierDesc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USBD_MSC_RegisterStorage
|
||||
* @param fops: storage callback
|
||||
* @retval status
|
||||
*/
|
||||
uint8_t USBD_MSC_RegisterStorage(USBD_HandleTypeDef *pdev,
|
||||
USBD_StorageTypeDef *fops)
|
||||
{
|
||||
if (fops == NULL) {
|
||||
return (uint8_t)USBD_FAIL;
|
||||
}
|
||||
|
||||
pdev->pUserData[pdev->classId] = fops;
|
||||
|
||||
return (uint8_t)USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
@@ -0,0 +1,134 @@
|
||||
/*!
|
||||
* \file usbd_msc.h
|
||||
*
|
||||
* \brief Header for the usbd_msc.c file
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author MCD Application Team
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_MSC_H
|
||||
#define __USBD_MSC_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_msc_bot.h"
|
||||
#include "usbd_msc_scsi.h"
|
||||
#include "usbd_ioreq.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
------------------------------------------- -----------------------------------------*/
|
||||
/** @defgroup USBD_BOT_Exported_Defines
|
||||
* @{
|
||||
*/
|
||||
/* MSC Class Config */
|
||||
#ifndef MSC_MEDIA_PACKET
|
||||
#define MSC_MEDIA_PACKET 512U
|
||||
#endif /* MSC_MEDIA_PACKET */
|
||||
|
||||
#define MSC_MAX_FS_PACKET 0x40U
|
||||
#define MSC_MAX_HS_PACKET 0x200U
|
||||
|
||||
#define BOT_GET_MAX_LUN 0xFE
|
||||
#define BOT_RESET 0xFF
|
||||
#define USB_MSC_CONFIG_DESC_SIZ 32
|
||||
|
||||
#ifndef MSC_EPIN_ADDR
|
||||
#define MSC_EPIN_ADDR 0x81U
|
||||
#endif /* MSC_EPIN_ADDR */
|
||||
|
||||
#ifndef MSC_EPOUT_ADDR
|
||||
#define MSC_EPOUT_ADDR 0x01U
|
||||
#endif /* MSC_EPOUT_ADDR */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Typedef
|
||||
------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USB_CORE_Exported_Types
|
||||
* @{
|
||||
*/
|
||||
typedef struct _USBD_STORAGE
|
||||
{
|
||||
int8_t (* Init)(uint8_t lun);
|
||||
int8_t (* GetCapacity)(uint8_t lun, uint32_t *block_num, uint16_t *block_size);
|
||||
int8_t (* IsReady)(uint8_t lun);
|
||||
int8_t (* IsWriteProtected)(uint8_t lun);
|
||||
int8_t (* Read)(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
|
||||
int8_t (* Write)(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
|
||||
int8_t (* GetMaxLun)(void);
|
||||
int8_t *pInquiry;
|
||||
|
||||
} USBD_StorageTypeDef;
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t max_lun;
|
||||
uint32_t interface;
|
||||
uint8_t bot_state;
|
||||
uint8_t bot_status;
|
||||
uint32_t bot_data_length;
|
||||
uint8_t bot_data[MSC_MEDIA_PACKET];
|
||||
USBD_MSC_BOT_CBWTypeDef cbw;
|
||||
USBD_MSC_BOT_CSWTypeDef csw;
|
||||
|
||||
USBD_SCSI_SenseTypeDef scsi_sense [SENSE_LIST_DEEPTH];
|
||||
uint8_t scsi_sense_head;
|
||||
uint8_t scsi_sense_tail;
|
||||
uint8_t scsi_medium_state;
|
||||
|
||||
uint16_t scsi_blk_size;
|
||||
uint32_t scsi_blk_nbr;
|
||||
|
||||
uint32_t scsi_blk_addr;
|
||||
uint32_t scsi_blk_len;
|
||||
} USBD_MSC_BOT_HandleTypeDef;
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/* Structure for MSC process */
|
||||
extern USBD_ClassTypeDef USBD_MSC;
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
uint8_t USBD_MSC_RegisterStorage(USBD_HandleTypeDef *pdev,
|
||||
USBD_StorageTypeDef *fops);
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_MSC_H */
|
||||
|
||||
@@ -0,0 +1,381 @@
|
||||
/*!
|
||||
* \file usbd_msc_bot.c
|
||||
*
|
||||
* \brief This file provides all the BOT protocol core functions.
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author MCD Application Team
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_msc_bot.h"
|
||||
#include "usbd_ioreq.h"
|
||||
#include "usbd_msc.h"
|
||||
#include "usbd_msc_scsi.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
extern uint8_t MSCInEpAdd;
|
||||
extern uint8_t MSCOutEpAdd;
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototype
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup MSC_BOT_Private_FunctionPrototypes
|
||||
* @{
|
||||
*/
|
||||
static void MSC_BOT_SendData(USBD_HandleTypeDef *pdev, uint8_t *pbuf,
|
||||
uint32_t len);
|
||||
static void MSC_BOT_CBW_Decode(USBD_HandleTypeDef *pdev);
|
||||
static void MSC_BOT_Abort(USBD_HandleTypeDef *pdev);
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief MSC_BOT_Init
|
||||
* Initialize the BOT Process
|
||||
* @param pdev: device instance
|
||||
* @retval None
|
||||
*/
|
||||
void MSC_BOT_Init(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
USBD_MSC_BOT_HandleTypeDef *hmsc =
|
||||
(USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
if (hmsc == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
hmsc->bot_state = USBD_BOT_IDLE;
|
||||
hmsc->bot_status = USBD_BOT_STATUS_NORMAL;
|
||||
|
||||
hmsc->scsi_sense_tail = 0U;
|
||||
hmsc->scsi_sense_head = 0U;
|
||||
hmsc->scsi_medium_state = SCSI_MEDIUM_UNLOCKED;
|
||||
|
||||
((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->Init(0U);
|
||||
|
||||
(void)USBD_LL_FlushEP(pdev, MSCOutEpAdd);
|
||||
(void)USBD_LL_FlushEP(pdev, MSCInEpAdd);
|
||||
|
||||
/* Prepare EP to Receive First BOT Cmd */
|
||||
(void)USBD_LL_PrepareReceive(pdev, MSCOutEpAdd, (uint8_t *)&hmsc->cbw,
|
||||
USBD_BOT_CBW_LENGTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief MSC_BOT_Reset
|
||||
* Reset the BOT Machine
|
||||
* @param pdev: device instance
|
||||
* @retval None
|
||||
*/
|
||||
void MSC_BOT_Reset(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
USBD_MSC_BOT_HandleTypeDef *hmsc =
|
||||
(USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
if (hmsc == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
hmsc->bot_state = USBD_BOT_IDLE;
|
||||
hmsc->bot_status = USBD_BOT_STATUS_RECOVERY;
|
||||
|
||||
(void)USBD_LL_ClearStallEP(pdev, MSCInEpAdd);
|
||||
(void)USBD_LL_ClearStallEP(pdev, MSCOutEpAdd);
|
||||
|
||||
/* Prepare EP to Receive First BOT Cmd */
|
||||
(void)USBD_LL_PrepareReceive(pdev, MSCOutEpAdd, (uint8_t *)&hmsc->cbw,
|
||||
USBD_BOT_CBW_LENGTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief MSC_BOT_DeInit
|
||||
* DeInitialize the BOT Machine
|
||||
* @param pdev: device instance
|
||||
* @retval None
|
||||
*/
|
||||
void MSC_BOT_DeInit(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
USBD_MSC_BOT_HandleTypeDef *hmsc =
|
||||
(USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
if (hmsc != NULL) {
|
||||
hmsc->bot_state = USBD_BOT_IDLE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief MSC_BOT_DataIn
|
||||
* Handle BOT IN data stage
|
||||
* @param pdev: device instance
|
||||
* @param epnum: endpoint index
|
||||
* @retval None
|
||||
*/
|
||||
void MSC_BOT_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
|
||||
{
|
||||
UNUSED(epnum);
|
||||
|
||||
USBD_MSC_BOT_HandleTypeDef *hmsc =
|
||||
(USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
if (hmsc == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (hmsc->bot_state) {
|
||||
case USBD_BOT_DATA_IN:
|
||||
if (SCSI_ProcessCmd(pdev, hmsc->cbw.bLUN, &hmsc->cbw.CB[0]) < 0) {
|
||||
MSC_BOT_SendCSW(pdev, USBD_CSW_CMD_FAILED);
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_BOT_SEND_DATA:
|
||||
case USBD_BOT_LAST_DATA_IN:
|
||||
MSC_BOT_SendCSW(pdev, USBD_CSW_CMD_PASSED);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief MSC_BOT_DataOut
|
||||
* Process MSC OUT data
|
||||
* @param pdev: device instance
|
||||
* @param epnum: endpoint index
|
||||
* @retval None
|
||||
*/
|
||||
void MSC_BOT_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum)
|
||||
{
|
||||
UNUSED(epnum);
|
||||
|
||||
USBD_MSC_BOT_HandleTypeDef *hmsc =
|
||||
(USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
if (hmsc == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (hmsc->bot_state) {
|
||||
case USBD_BOT_IDLE:
|
||||
MSC_BOT_CBW_Decode(pdev);
|
||||
break;
|
||||
|
||||
case USBD_BOT_DATA_OUT:
|
||||
if (SCSI_ProcessCmd(pdev, hmsc->cbw.bLUN, &hmsc->cbw.CB[0]) < 0) {
|
||||
MSC_BOT_SendCSW(pdev, USBD_CSW_CMD_FAILED);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief MSC_BOT_CBW_Decode
|
||||
* Decode the CBW command and set the BOT state machine accordingly
|
||||
* @param pdev: device instance
|
||||
* @retval None
|
||||
*/
|
||||
static void MSC_BOT_CBW_Decode(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
USBD_MSC_BOT_HandleTypeDef *hmsc =
|
||||
(USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
if (hmsc == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
hmsc->csw.dTag = hmsc->cbw.dTag;
|
||||
hmsc->csw.dDataResidue = hmsc->cbw.dDataLength;
|
||||
|
||||
if ((USBD_LL_GetRxDataSize(pdev, MSCOutEpAdd) != USBD_BOT_CBW_LENGTH) ||
|
||||
(hmsc->cbw.dSignature != USBD_BOT_CBW_SIGNATURE) ||
|
||||
(hmsc->cbw.bLUN > 1U) || (hmsc->cbw.bCBLength < 1U) ||
|
||||
(hmsc->cbw.bCBLength > 16U)) {
|
||||
SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB);
|
||||
|
||||
hmsc->bot_status = USBD_BOT_STATUS_ERROR;
|
||||
MSC_BOT_Abort(pdev);
|
||||
} else {
|
||||
if (SCSI_ProcessCmd(pdev, hmsc->cbw.bLUN, &hmsc->cbw.CB[0]) < 0) {
|
||||
// if (hmsc->bot_state == USBD_BOT_NO_DATA)
|
||||
// {
|
||||
// MSC_BOT_SendCSW(pdev, USBD_CSW_CMD_FAILED);
|
||||
// }
|
||||
// else
|
||||
{
|
||||
MSC_BOT_Abort(pdev);
|
||||
}
|
||||
}
|
||||
/* Burst xfer handled internally */
|
||||
else if ((hmsc->bot_state != USBD_BOT_DATA_IN) &&
|
||||
(hmsc->bot_state != USBD_BOT_DATA_OUT) &&
|
||||
(hmsc->bot_state != USBD_BOT_LAST_DATA_IN)) {
|
||||
if (hmsc->bot_data_length > 0U) {
|
||||
MSC_BOT_SendData(pdev, hmsc->bot_data, hmsc->bot_data_length);
|
||||
} else if (hmsc->bot_data_length == 0U) {
|
||||
MSC_BOT_SendCSW(pdev, USBD_CSW_CMD_PASSED);
|
||||
} else {
|
||||
MSC_BOT_Abort(pdev);
|
||||
}
|
||||
}
|
||||
// else
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief MSC_BOT_SendData
|
||||
* Send the requested data
|
||||
* @param pdev: device instance
|
||||
* @param buf: pointer to data buffer
|
||||
* @param len: Data Length
|
||||
* @retval None
|
||||
*/
|
||||
static void MSC_BOT_SendData(USBD_HandleTypeDef *pdev, uint8_t *pbuf,
|
||||
uint32_t len)
|
||||
{
|
||||
USBD_MSC_BOT_HandleTypeDef *hmsc =
|
||||
(USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
uint32_t length;
|
||||
|
||||
if (hmsc == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
length = MIN(hmsc->cbw.dDataLength, len);
|
||||
|
||||
hmsc->csw.dDataResidue -= len;
|
||||
hmsc->csw.bStatus = USBD_CSW_CMD_PASSED;
|
||||
hmsc->bot_state = USBD_BOT_SEND_DATA;
|
||||
|
||||
(void)USBD_LL_Transmit(pdev, MSCInEpAdd, pbuf, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief MSC_BOT_SendCSW
|
||||
* Send the Command Status Wrapper
|
||||
* @param pdev: device instance
|
||||
* @param status : CSW status
|
||||
* @retval None
|
||||
*/
|
||||
void MSC_BOT_SendCSW(USBD_HandleTypeDef *pdev, uint8_t CSW_Status)
|
||||
{
|
||||
USBD_MSC_BOT_HandleTypeDef *hmsc =
|
||||
(USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
if (hmsc == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
hmsc->csw.dSignature = USBD_BOT_CSW_SIGNATURE;
|
||||
hmsc->csw.bStatus = CSW_Status;
|
||||
hmsc->bot_state = USBD_BOT_IDLE;
|
||||
|
||||
(void)USBD_LL_Transmit(pdev, MSCInEpAdd, (uint8_t *)&hmsc->csw,
|
||||
USBD_BOT_CSW_LENGTH);
|
||||
|
||||
/* Prepare EP to Receive next Cmd */
|
||||
(void)USBD_LL_PrepareReceive(pdev, MSCOutEpAdd, (uint8_t *)&hmsc->cbw,
|
||||
USBD_BOT_CBW_LENGTH);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief MSC_BOT_Abort
|
||||
* Abort the current transfer
|
||||
* @param pdev: device instance
|
||||
* @retval status
|
||||
*/
|
||||
|
||||
static void MSC_BOT_Abort(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
USBD_MSC_BOT_HandleTypeDef *hmsc =
|
||||
(USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
if (hmsc == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((hmsc->cbw.bmFlags == 0U) && (hmsc->cbw.dDataLength != 0U) &&
|
||||
(hmsc->bot_status == USBD_BOT_STATUS_NORMAL)) {
|
||||
(void)USBD_LL_StallEP(pdev, MSCOutEpAdd);
|
||||
}
|
||||
|
||||
(void)USBD_LL_StallEP(pdev, MSCInEpAdd);
|
||||
|
||||
if (hmsc->bot_status == USBD_BOT_STATUS_ERROR) {
|
||||
(void)USBD_LL_StallEP(pdev, MSCInEpAdd);
|
||||
(void)USBD_LL_StallEP(pdev, MSCOutEpAdd);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief MSC_BOT_CplClrFeature
|
||||
* Complete the clear feature request
|
||||
* @param pdev: device instance
|
||||
* @param epnum: endpoint index
|
||||
* @retval None
|
||||
*/
|
||||
|
||||
void MSC_BOT_CplClrFeature(USBD_HandleTypeDef *pdev, uint8_t epnum)
|
||||
{
|
||||
USBD_MSC_BOT_HandleTypeDef *hmsc =
|
||||
(USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
|
||||
|
||||
if (hmsc == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (hmsc->bot_status == USBD_BOT_STATUS_ERROR) /* Bad CBW Signature */
|
||||
{
|
||||
(void)USBD_LL_StallEP(pdev, MSCInEpAdd);
|
||||
(void)USBD_LL_StallEP(pdev, MSCOutEpAdd);
|
||||
} else if (((epnum & 0x80U) == 0x80U) &&
|
||||
(hmsc->bot_status != USBD_BOT_STATUS_RECOVERY)) {
|
||||
MSC_BOT_SendCSW(pdev, USBD_CSW_CMD_FAILED);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
@@ -0,0 +1,138 @@
|
||||
/*!
|
||||
* \file usbd_msc_bot.h
|
||||
*
|
||||
* \brief Header for the usbd_msc_bot.c file.
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author MCD Application Team
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_MSC_BOT_H
|
||||
#define __USBD_MSC_BOT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_core.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
------------------------------------------- -----------------------------------------*/
|
||||
/** @defgroup USBD_CORE_Exported_Defines
|
||||
* @{
|
||||
*/
|
||||
#define USBD_BOT_IDLE 0U /* Idle state */
|
||||
#define USBD_BOT_DATA_OUT 1U /* Data Out state */
|
||||
#define USBD_BOT_DATA_IN 2U /* Data In state */
|
||||
#define USBD_BOT_LAST_DATA_IN 3U /* Last Data In Last */
|
||||
#define USBD_BOT_SEND_DATA 4U /* Send Immediate data */
|
||||
#define USBD_BOT_NO_DATA 5U /* No data Stage */
|
||||
|
||||
#define USBD_BOT_CBW_SIGNATURE 0x43425355U
|
||||
#define USBD_BOT_CSW_SIGNATURE 0x53425355U
|
||||
#define USBD_BOT_CBW_LENGTH 31U
|
||||
#define USBD_BOT_CSW_LENGTH 13U
|
||||
#define USBD_BOT_MAX_DATA 256U
|
||||
|
||||
/* CSW Status Definitions */
|
||||
#define USBD_CSW_CMD_PASSED 0x00U
|
||||
#define USBD_CSW_CMD_FAILED 0x01U
|
||||
#define USBD_CSW_PHASE_ERROR 0x02U
|
||||
|
||||
/* BOT Status */
|
||||
#define USBD_BOT_STATUS_NORMAL 0U
|
||||
#define USBD_BOT_STATUS_RECOVERY 1U
|
||||
#define USBD_BOT_STATUS_ERROR 2U
|
||||
|
||||
|
||||
#define USBD_DIR_IN 0U
|
||||
#define USBD_DIR_OUT 1U
|
||||
#define USBD_BOTH_DIR 2U
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Typedef
|
||||
------------------------------------------- -----------------------------------------*/
|
||||
/** @defgroup MSC_CORE_Private_TypesDefinitions
|
||||
* @{
|
||||
*/
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t dSignature;
|
||||
uint32_t dTag;
|
||||
uint32_t dDataLength;
|
||||
uint8_t bmFlags;
|
||||
uint8_t bLUN;
|
||||
uint8_t bCBLength;
|
||||
uint8_t CB[16];
|
||||
uint8_t ReservedForAlign;
|
||||
} USBD_MSC_BOT_CBWTypeDef;
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t dSignature;
|
||||
uint32_t dTag;
|
||||
uint32_t dDataResidue;
|
||||
uint8_t bStatus;
|
||||
uint8_t ReservedForAlign[3];
|
||||
} USBD_MSC_BOT_CSWTypeDef;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_CORE_Exported_FunctionsPrototypes
|
||||
* @{
|
||||
*/
|
||||
void MSC_BOT_Init(USBD_HandleTypeDef *pdev);
|
||||
void MSC_BOT_Reset(USBD_HandleTypeDef *pdev);
|
||||
void MSC_BOT_DeInit(USBD_HandleTypeDef *pdev);
|
||||
void MSC_BOT_DataIn(USBD_HandleTypeDef *pdev,
|
||||
uint8_t epnum);
|
||||
|
||||
void MSC_BOT_DataOut(USBD_HandleTypeDef *pdev,
|
||||
uint8_t epnum);
|
||||
|
||||
void MSC_BOT_SendCSW(USBD_HandleTypeDef *pdev,
|
||||
uint8_t CSW_Status);
|
||||
|
||||
void MSC_BOT_CplClrFeature(USBD_HandleTypeDef *pdev,
|
||||
uint8_t epnum);
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_MSC_BOT_H */
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/*!
|
||||
* \file usbd_msc_data.c
|
||||
*
|
||||
* \brief This file provides all the vital inquiry pages and sense data.
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author MCD Application Team
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_msc_data.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/* USB Mass storage Page 0 Inquiry Data */
|
||||
uint8_t MSC_Page00_Inquiry_Data[LENGTH_INQUIRY_PAGE00] = {
|
||||
0x00, 0x00, 0x00, (LENGTH_INQUIRY_PAGE00 - 4U),
|
||||
0x00, 0x80
|
||||
// 0x83 //alex revise
|
||||
};
|
||||
|
||||
/* USB Mass storage VPD Page 0x80 Inquiry Data for Unit Serial Number */
|
||||
uint8_t MSC_Page80_Inquiry_Data[LENGTH_INQUIRY_PAGE80] = {
|
||||
0x00, 0x80, 0x00, LENGTH_INQUIRY_PAGE80,
|
||||
0x20, /* Put Product Serial number */
|
||||
0x20, 0x20, 0x20};
|
||||
|
||||
/* USB Mass storage sense 6 Data */
|
||||
uint8_t MSC_Mode_Sense6_data[MODE_SENSE6_LEN] = {
|
||||
0x22, 0x00, 0x00, 0x00, 0x08, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
||||
|
||||
/* USB Mass storage sense 10 Data */
|
||||
uint8_t MSC_Mode_Sense10_data[MODE_SENSE10_LEN] = {
|
||||
0x00, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
|
||||
0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
@@ -0,0 +1,74 @@
|
||||
/*!
|
||||
* \file usbd_msc_data.h
|
||||
*
|
||||
* \brief Header for the usbd_msc_data.c file.
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author MCD Application Team
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_MSC_DATA_H
|
||||
#define __USBD_MSC_DATA_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_conf.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
------------------------------------------- -----------------------------------------*/
|
||||
/** @defgroup USB_INFO_Exported_Defines
|
||||
* @{
|
||||
*/
|
||||
#define MODE_SENSE6_LEN 0x17U
|
||||
#define MODE_SENSE10_LEN 0x1BU
|
||||
#define LENGTH_INQUIRY_PAGE00 0x06U //0x07U//0x06U alex revise
|
||||
#define LENGTH_INQUIRY_PAGE80 0x08U
|
||||
#define LENGTH_FORMAT_CAPACITIES 0x14U
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_INFO_Exported_Variables
|
||||
* @{
|
||||
*/
|
||||
extern uint8_t MSC_Page00_Inquiry_Data[LENGTH_INQUIRY_PAGE00];
|
||||
extern uint8_t MSC_Page80_Inquiry_Data[LENGTH_INQUIRY_PAGE80];
|
||||
extern uint8_t MSC_Mode_Sense6_data[MODE_SENSE6_LEN];
|
||||
extern uint8_t MSC_Mode_Sense10_data[MODE_SENSE10_LEN];
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_MSC_DATA_H */
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,163 @@
|
||||
/*!
|
||||
* \file usbd_msc_scsi.h
|
||||
*
|
||||
* \brief Header for the usbd_msc_scsi.c file.
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author MCD Application Team
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_MSC_SCSI_H
|
||||
#define __USBD_MSC_SCSI_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_def.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
------------------------------------------- -----------------------------------------*/
|
||||
/** @defgroup USBD_SCSI_Exported_Defines
|
||||
* @{
|
||||
*/
|
||||
|
||||
#define SENSE_LIST_DEEPTH 4U
|
||||
|
||||
/* SCSI Commands */
|
||||
#define SCSI_FORMAT_UNIT 0x04U
|
||||
#define SCSI_INQUIRY 0x12U
|
||||
#define SCSI_MODE_SELECT6 0x15U
|
||||
#define SCSI_MODE_SELECT10 0x55U
|
||||
#define SCSI_MODE_SENSE6 0x1AU
|
||||
#define SCSI_MODE_SENSE10 0x5AU
|
||||
#define SCSI_ALLOW_MEDIUM_REMOVAL 0x1EU
|
||||
#define SCSI_READ6 0x08U
|
||||
#define SCSI_READ10 0x28U
|
||||
#define SCSI_READ12 0xA8U
|
||||
#define SCSI_READ16 0x88U
|
||||
|
||||
#define SCSI_READ_CAPACITY10 0x25U
|
||||
#define SCSI_READ_CAPACITY16 0x9EU
|
||||
|
||||
#define SCSI_REQUEST_SENSE 0x03U
|
||||
#define SCSI_START_STOP_UNIT 0x1BU
|
||||
#define SCSI_TEST_UNIT_READY 0x00U
|
||||
#define SCSI_WRITE6 0x0AU
|
||||
#define SCSI_WRITE10 0x2AU
|
||||
#define SCSI_WRITE12 0xAAU
|
||||
#define SCSI_WRITE16 0x8AU
|
||||
|
||||
#define SCSI_VERIFY10 0x2FU
|
||||
#define SCSI_VERIFY12 0xAFU
|
||||
#define SCSI_VERIFY16 0x8FU
|
||||
|
||||
#define SCSI_SEND_DIAGNOSTIC 0x1DU
|
||||
#define SCSI_READ_FORMAT_CAPACITIES 0x23U
|
||||
|
||||
#define NO_SENSE 0U
|
||||
#define RECOVERED_ERROR 1U
|
||||
#define NOT_READY 2U
|
||||
#define MEDIUM_ERROR 3U
|
||||
#define HARDWARE_ERROR 4U
|
||||
#define ILLEGAL_REQUEST 5U
|
||||
#define UNIT_ATTENTION 6U
|
||||
#define DATA_PROTECT 7U
|
||||
#define BLANK_CHECK 8U
|
||||
#define VENDOR_SPECIFIC 9U
|
||||
#define COPY_ABORTED 10U
|
||||
#define ABORTED_COMMAND 11U
|
||||
#define VOLUME_OVERFLOW 13U
|
||||
#define MISCOMPARE 14U
|
||||
|
||||
|
||||
#define INVALID_CDB 0x20U
|
||||
#define INVALID_FIELED_IN_COMMAND 0x24U
|
||||
#define PARAMETER_LIST_LENGTH_ERROR 0x1AU
|
||||
#define INVALID_FIELD_IN_PARAMETER_LIST 0x26U
|
||||
#define ADDRESS_OUT_OF_RANGE 0x21U
|
||||
#define MEDIUM_NOT_PRESENT 0x3AU
|
||||
#define MEDIUM_HAVE_CHANGED 0x28U
|
||||
#define WRITE_PROTECTED 0x27U
|
||||
#define UNRECOVERED_READ_ERROR 0x11U
|
||||
#define WRITE_FAULT 0x03U
|
||||
|
||||
#define READ_FORMAT_CAPACITY_DATA_LEN 0x0CU
|
||||
#define READ_CAPACITY10_DATA_LEN 0x08U
|
||||
#define REQUEST_SENSE_DATA_LEN 0x12U
|
||||
#define STANDARD_INQUIRY_DATA_LEN 0x24U
|
||||
#define BLKVFY 0x04U
|
||||
|
||||
#define SCSI_MEDIUM_UNLOCKED 0x00U
|
||||
#define SCSI_MEDIUM_LOCKED 0x01U
|
||||
#define SCSI_MEDIUM_EJECTED 0x02U
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Typedef
|
||||
------------------------------------------- -----------------------------------------*/
|
||||
/** @defgroup USBD_SCSI_Exported_TypesDefinitions
|
||||
* @{
|
||||
*/
|
||||
|
||||
typedef struct _SENSE_ITEM
|
||||
{
|
||||
uint8_t Skey;
|
||||
union
|
||||
{
|
||||
struct _ASCs
|
||||
{
|
||||
uint8_t ASC;
|
||||
uint8_t ASCQ;
|
||||
} b;
|
||||
uint8_t ASC;
|
||||
uint8_t *pData;
|
||||
} w;
|
||||
} USBD_SCSI_SenseTypeDef;
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_SCSI_Exported_FunctionsPrototype
|
||||
* @{
|
||||
*/
|
||||
int8_t SCSI_ProcessCmd(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *cmd);
|
||||
|
||||
void SCSI_SenseCode(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t sKey,
|
||||
uint8_t ASC);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_MSC_SCSI_H */
|
||||
|
||||
Reference in New Issue
Block a user