hpw422初始版本
This commit is contained in:
@@ -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