📄 usbhost_ms.c
字号:
/*
**************************************************************************************************************
* NXP USB Host Stack
*
* (c) Copyright 2008, NXP SemiConductors
* (c) Copyright 2008, OnChip Technologies LLC
* All Rights Reserved
*
* www.nxp.com
* www.onchiptech.com
*
* File : usbhost_ms.c
* Programmer(s) : Ravikanth.P
* Version :
*
**************************************************************************************************************
*/
/*
**************************************************************************************************************
* INCLUDE HEADER FILES
**************************************************************************************************************
*/
#include "usbhost_ms.h"
/*
**************************************************************************************************************
* GLOBAL VARIABLES
**************************************************************************************************************
*/
uint32_t MS_BlkSize;
/*
**************************************************************************************************************
* INITIALIZE MASS STORAGE INTERFACE
*
* Description: This function initializes the mass storage interface
*
* Arguments : None
*
* Returns : OK if Success
* ERR_INVALID_BOOTSIG if Failed
*
**************************************************************************************************************
*/
int32_t MS_Init (uint32_t *blkSize, uint32_t *numBlks, uint8_t *inquiryResult)
{
uint8_t retry;
int32_t rc;
MS_GetMaxLUN(); /* Get maximum logical unit number */
retry = 80;
while(retry) {
rc = MS_TestUnitReady(); /* Test whether the unit is ready */
if (rc == OK) {
break;
}
MS_GetSenseInfo(); /* Get sense information */
retry--;
}
if (rc != OK) {
//PRINT_Err(rc);
return (rc);
}
rc = MS_ReadCapacity(numBlks, blkSize); /* Read capacity of the disk */
MS_BlkSize = *blkSize; // Set global
rc = MS_Inquire (inquiryResult);
return (rc);
}
/*
**************************************************************************************************************
* PARSE THE CONFIGURATION
*
* Description: This function is used to parse the configuration
*
* Arguments : None
*
* Returns : OK if Success
* ERR_INVALID_BOOTSIG if Failed
*
**************************************************************************************************************
*/
int32_t MS_ParseConfiguration (void)
{
volatile uint8_t *desc_ptr;
uint8_t ms_int_found;
desc_ptr = TDBuffer;
ms_int_found = 0;
if (desc_ptr[1] != USB_DESCRIPTOR_TYPE_CONFIGURATION) {
return (ERR_BAD_CONFIGURATION);
}
desc_ptr += desc_ptr[0];
while (desc_ptr != TDBuffer + ReadLE16U(&TDBuffer[2])) {
// while (desc_ptr != TDBuffer + *((uint16_t *) &TDBuffer[2])) {
switch (desc_ptr[1]) {
case USB_DESCRIPTOR_TYPE_INTERFACE: /* If it is an interface descriptor */
if (desc_ptr[5] == MASS_STORAGE_CLASS && /* check if the class is mass storage */
desc_ptr[6] == MASS_STORAGE_SUBCLASS_SCSI && /* check if the subclass is SCSI */
desc_ptr[7] == MASS_STORAGE_PROTOCOL_BO) { /* check if the protocol is Bulk only */
ms_int_found = 1;
desc_ptr += desc_ptr[0]; /* Move to next descriptor start */
}
break;
case USB_DESCRIPTOR_TYPE_ENDPOINT: /* If it is an endpoint descriptor */
if ((desc_ptr[3] & 0x03) == 0x02) { /* If it is Bulk endpoint */
if (desc_ptr[2] & 0x80) { /* If it is In endpoint */
EDBulkIn->Control = 1 | /* USB address */
((desc_ptr[2] & 0x7F) << 7) | /* Endpoint address */
(2 << 11) | /* direction */
(ReadLE16U(&desc_ptr[4]) << 16); /* MaxPkt Size */
desc_ptr += desc_ptr[0]; /* Move to next descriptor start */
} else { /* If it is Out endpoint */
EDBulkOut->Control = 1 | /* USB address */
((desc_ptr[2] & 0x7F) << 7) | /* Endpoint address */
(1 << 11) | /* direction */
(ReadLE16U(&desc_ptr[4]) << 16); /* MaxPkt Size */
desc_ptr += desc_ptr[0]; /* Move to next descriptor start */
}
} else { /* If it is not bulk end point */
desc_ptr += desc_ptr[0]; /* Move to next descriptor start */
}
break;
default: /* If the descriptor is neither interface nor endpoint */
desc_ptr += desc_ptr[0]; /* Move to next descriptor start */
break;
}
}
if (ms_int_found) {
//PRINT_Log("Mass Storage device connected\n");
return (OK);
} else {
//PRINT_Log("Not a Mass Storage device\n");
return (ERR_NO_MS_INTERFACE);
}
}
/*
**************************************************************************************************************
* GET MAXIMUM LOGICAL UNIT
*
* Description: This function returns the maximum logical unit from the device
*
* Arguments : None
*
* Returns : OK if Success
* ERR_INVALID_BOOTSIG if Failed
*
**************************************************************************************************************
*/
int32_t MS_GetMaxLUN (void)
{
int32_t rc;
rc = Host_CtrlRecv(USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE,
MS_GET_MAX_LUN_REQ,
0,
0,
1,
TDBuffer);
return (rc);
}
/*
**************************************************************************************************************
* GET SENSE INFORMATION
*
* Description: This function is used to get sense information from the device
*
* Arguments : None
*
* Returns : OK if Success
* ERROR if Failed
*
**************************************************************************************************************
*/
int32_t MS_GetSenseInfo (void)
{
int32_t rc;
Fill_MSCommand(0, 0, 0, MS_DATA_DIR_IN, SCSI_CMD_REQUEST_SENSE, 6);
rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE);
if (rc == OK) {
rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, 18);
if (rc == OK) {
rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE);
if (rc == OK) {
if (TDBuffer[12] != 0) {
rc = ERR_MS_CMD_FAILED;
}
}
}
}
return (rc);
}
/*
**************************************************************************************************************
* TEST UNIT READY
*
* Description: This function is used to test whether the unit is ready or not
*
* Arguments : None
*
* Returns : OK if Success
* ERROR if Failed
*
**************************************************************************************************************
*/
int32_t MS_TestUnitReady (void)
{
int32_t rc;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -