DRESULT TM_FATFS_USB_disk_write (
	const BYTE *buff,	/* Data to be written */
	DWORD sector,		/* Sector address (LBA) */
	UINT count			/* Number of sectors to write (1..128) */
)
{
	BYTE status = USBH_MSC_OK;
	if (!count) {
		return RES_PARERR;
	}
	if (USB_Stat & STA_NOINIT) {
		return RES_NOTRDY;
	}
//	if (TM_USB_MSCHOST_INT_Result == TM_USB_MSCHOST_Result_WriteProtected) {
//		return RES_WRPRT;
//	}

	if (HCD_IsDeviceConnected(&USB_OTG_Core) && USBH_USR_MSC_IsReady()) {
		//USBH_MSC_Init(&USB_OTG_Core);
		do
		{
			status = USBH_MSC_Write10(&USB_OTG_Core, (BYTE*)buff, sector, 512 * count);
			USBH_MSC_HandleBOTXfer(&USB_OTG_Core, &USB_Host);

			if (!HCD_IsDeviceConnected(&USB_OTG_Core)) {
				return RES_ERROR;
			}
		} while (status == USBH_MSC_BUSY);
	}

	if (status == USBH_MSC_OK) {
		return RES_OK;
	}
	return RES_ERROR;
}
示例#2
0
/*-----------------------------------------------------------------------*/
DRESULT TM_FATFS_USB_disk_read (
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	/* Sector address (LBA) */
	UINT count		/* Number of sectors to read (1..128) */
)
{
	BYTE status = USBH_MSC_OK;
	uint32_t timeout;

	if (!count) {
		return RES_PARERR;
	}
	if (USB_Stat & STA_NOINIT) {
		return RES_NOTRDY;
	}

	if (HCD_IsDeviceConnected(&USB_OTG_Core) && TM_USB_MSCHOST_INT_Result == TM_USB_MSCHOST_Result_Connected) {
		timeout = FATFS_USB_TIMEOUT;
		do
		{
			status = USBH_MSC_Read10(&USB_OTG_Core, buff, sector, 512 * count);
			USBH_MSC_HandleBOTXfer(&USB_OTG_Core, &USB_Host);

			if (!HCD_IsDeviceConnected(&USB_OTG_Core)) { 
				return RES_ERROR;
			}
		} while (status == USBH_MSC_BUSY && timeout--);
	}

	if (status == USBH_MSC_OK) {
		return RES_OK;
	}
	return RES_ERROR;
}
/*-----------------------------------------------------------------------*/
DRESULT TM_FATFS_USB_disk_read (
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	/* Sector address (LBA) */
	UINT count		/* Number of sectors to read (1..128) */
)
{
	BYTE status = USBH_MSC_OK;

	if (!count) {
		return RES_PARERR;
	}
	if (USB_Stat & STA_NOINIT) {
		return RES_NOTRDY;
	}

	if (HCD_IsDeviceConnected(&USB_OTG_Core) && USBH_USR_MSC_IsReady()) {
		//USBH_MSC_Init(&USB_OTG_Core);
		do
		{
			status = USBH_MSC_Read10(&USB_OTG_Core, buff, sector, 512 * count);
			USBH_MSC_HandleBOTXfer(&USB_OTG_Core, &USB_Host);

			if (!HCD_IsDeviceConnected(&USB_OTG_Core)) { 
				return RES_ERROR;
			}
		} while (status == USBH_MSC_BUSY);
	}

	if (status == USBH_MSC_OK) {
		return RES_OK;
	}
	return RES_ERROR;
}
示例#4
0
DRESULT disk_read (
                   BYTE drv,			/* Physical drive number (0) */
                   BYTE *buff,			/* Pointer to the data buffer to store read data */
                   DWORD sector,		/* Start sector number (LBA) */
                   BYTE count			/* Sector count (1..255) */
                     )
{
  BYTE status = USBH_MSC_OK;

  if (drv || !count) return RES_PARERR;
  if (Stat & STA_NOINIT) return RES_NOTRDY;


  if(HCD_IsDeviceConnected(&USB_OTG_Core))
  {

    do
    {
      status = USBH_MSC_Read10(&USB_OTG_Core, buff,sector,512);
      USBH_MSC_HandleBOTXfer(&USB_OTG_Core ,&USB_Host);

      if(!HCD_IsDeviceConnected(&USB_OTG_Core))
      {
        return RES_ERROR;
      }
    }
    while(status == USBH_MSC_BUSY );
  }

  if(status == USBH_MSC_OK)
    return RES_OK;
  return RES_ERROR;

}
示例#5
0
DRESULT disk_write (
                    BYTE drv,			/* Physical drive number (0) */
                    const BYTE *buff,	/* Pointer to the data to be written */
                    DWORD sector,		/* Start sector number (LBA) */
                    BYTE count			/* Sector count (1..255) */
                      )
{
  BYTE status = USBH_MSC_OK;
  if (drv || !count) return RES_PARERR;
  if (Stat & STA_NOINIT) return RES_NOTRDY;
  if (Stat & STA_PROTECT) return RES_WRPRT;
  
  
  if(HCD_IsDeviceConnected(&usbOTGHost))
  {  
    do
    {
      status = USBH_MSC_Write10(&usbOTGHost,(BYTE*)buff, sector, 512*count);
      USBH_MSC_HandleBOTXfer(&usbOTGHost, &usbHost);
      
      if(!HCD_IsDeviceConnected(&usbOTGHost))
      { 
        return RES_ERROR;
      }
    }
    
    while(status == USBH_MSC_BUSY );
    
  }
  
  if(status == USBH_MSC_OK)
    return RES_OK;
  return RES_ERROR;
}
示例#6
0
文件: usbh_usr.c 项目: mildrock/705_1
/***********************************************************
* Function:       // 函数名称
* Description:    // 函数功能、性能等的描述
* Input:          // 1.输入参数1,说明,包括每个参数的作用、取值说明及参数间关系
* Input:          // 2.输入参数2,说明,包括每个参数的作用、取值说明及参数间关系
* Output:         // 1.输出参数1,说明
* Return:         // 函数返回值的说明
* Others:         // 其它说明
***********************************************************/
static rt_size_t msc_write( rt_device_t dev, rt_off_t sector, const void* buff, rt_size_t count )
{
	BYTE status = USBH_MSC_OK;
	if( HCD_IsDeviceConnected( &USB_OTG_Core ) )
	{
		do
		{
			status = USBH_MSC_Write10( &USB_OTG_Core, (BYTE*)buff, sector, 512 * count );
			USBH_MSC_HandleBOTXfer( &USB_OTG_Core, &USB_Host );

			if( !HCD_IsDeviceConnected( &USB_OTG_Core ) )
			{
				//return RES_ERROR;
				rt_kprintf( "\n%s error", __func__ );
				return USBH_MSC_FAIL;
			}
		}

		while( status == USBH_MSC_BUSY );
	}
	if( status == USBH_MSC_OK )
	{
		return count;
	}
	return 0xff;
}
示例#7
0
/***********************************************************
* Function:       // 函数名称
* Description:    // 函数功能、性能等的描述
* Input:          // 1.输入参数1,说明,包括每个参数的作用、取值说明及参数间关系
* Input:          // 2.输入参数2,说明,包括每个参数的作用、取值说明及参数间关系
* Output:         // 1.输出参数1,说明
* Return:         // 函数返回值的说明
* Others:         // 其它说明
***********************************************************/
static rt_size_t msc_read( rt_device_t dev, rt_off_t sector, void *buff, rt_size_t count )
{
    __IO uint8_t status = USBH_MSC_OK;
    if( HCD_IsDeviceConnected( &USB_OTG_Core ) )
    {
        do
        {
            status = USBH_MSC_Read10( &USB_OTG_Core, buff, sector, 512 * count );
            USBH_MSC_HandleBOTXfer( &USB_OTG_Core, &USB_Host );

            if( !HCD_IsDeviceConnected( &USB_OTG_Core ) )
            {
                //return RES_ERROR;
                rt_kprintf( "%s error\r\n", __func__ );
                return USBH_MSC_FAIL;
            }
        }
        while( status == USBH_MSC_BUSY );
    }
    if( status == USBH_MSC_OK )
    {
        return count;
    }
    return 0xff;
}
BOOL USB_MSC_Driver::Read( void* context, ByteAddress Address, UINT32 NumBytes, BYTE * pSectorBuff ){

	USB_OTG_CORE_HANDLE *pDev = (USB_OTG_CORE_HANDLE*)context;
	BYTE status = USBH_MSC_OK;

	if(!HCD_IsDeviceConnected(pDev)){
		return FALSE;
	}
	//flat adressing
	SectorAddress startSector = Address / 512;
	if (Address % 512 != 0) {
		CLR_Debug::Printf( "USB_MSC_Driver::Read error: Adress must match a sector start, sector=%08x, Address=%08x, NumBytes %d\r\n", startSector, Address, NumBytes);
		return FALSE;
	}
	//CLR_Debug::Printf( "USB_MSC_Driver::Read() address=%lld, numBytes=%d sector=%d\r\n", Address, NumBytes, startSector);
	//CLR_Debug::Printf( "USB_MSC_Driver::Read offset=%d, StartSector=0x%08x, Address=%08x, NumBytes %d\r\n", offset, StartSector, Address, NumBytes);
	USBH_MSC_BOTXferParam.CmdStateMachine = CMD_SEND_STATE;
	do {
		status = USBH_MSC_Read10(pDev, pSectorBuff, startSector, NumBytes);
		USBH_MSC_HandleBOTXfer(pDev ,&USB_Host);

		if(!HCD_IsDeviceConnected(pDev)) { 
			//CLR_Debug::Printf( "USB_MSC_Driver::Read() USB device disconnection when reading\r\n");
			return FALSE;
		}      
	} while(status == USBH_MSC_BUSY );

	if(status != USBH_MSC_OK)
		return FALSE;

	return TRUE;
}
BOOL USB_MSC_Driver::Write( void* context, ByteAddress Address, UINT32 NumBytes, BYTE * pSectorBuff, BOOL ReadModifyWrite ){
	//CLR_Debug::Printf( "USB_MSC_Driver::Write(*, %08x, %08x, *)\r\n", Address, NumBytes);

	USB_OTG_CORE_HANDLE *pDev = (USB_OTG_CORE_HANDLE*)context;
	BYTE status = USBH_MSC_OK;

	if(!HCD_IsDeviceConnected(pDev)){
		//CLR_Debug::Printf( "USB_MSC_Driver::Read() no USB device connected\r\n");
		return FALSE;
	}

	UINT32 StartSector = g_USB_MSC_DeviceInfo.PhysicalToSectorAddress( &g_USB_MSC_DeviceInfo.Regions[0], Address);
	//flat adressing
	SectorAddress startSector = Address / 512;
	if (Address % 512 != 0) {
		CLR_Debug::Printf( "USB_MSC_Driver::Write error: Adress must match a sector start, sector=%08x, Address=%08x, NumBytes %d\r\n", startSector, Address, NumBytes);
		return FALSE;
	}
	USBH_MSC_BOTXferParam.CmdStateMachine=CMD_SEND_STATE;
	do {
		status = USBH_MSC_Write10(pDev, pSectorBuff, StartSector, NumBytes);
		USBH_MSC_HandleBOTXfer(pDev ,&USB_Host);

		if(!HCD_IsDeviceConnected(pDev)) { 
			//CLR_Debug::Printf( "USB_MSC_Driver::Write() USB device disconnection when writing\r\n");
			return FALSE;
		}      
	} while(status == USBH_MSC_BUSY );

	if(status != USBH_MSC_OK)
		return FALSE;

	return TRUE;
}
int8_t if_writeBuf(hwInterface* file,uint32_t address,uint8_t* buf)
{
  int8_t status = EFS_ERROR;
  
  if(HCD_IsDeviceConnected(&USB_OTG_FS_dev))
  {  
    do
    {
      status = USBH_MSC_Write10(buf,address,USBH_MSC_PAGE_LENGTH);
      USBH_MSC_HandleBOTXfer();
    }
    while((status == USBH_MSC_BUSY ) && \
      (HCD_IsDeviceConnected(&USB_OTG_FS_dev)));
  }
  return(status);
}
int8_t if_TestUnitReady(void)
{
  int8_t status = EFS_ERROR;
  if(HCD_IsDeviceConnected(&USB_OTG_FS_dev))
  {  
    do
    {
      status = USBH_MSC_TestUnitReady();
      USBH_MSC_HandleBOTXfer();
    }
    while((status == USBH_MSC_BUSY ) && \
      (HCD_IsDeviceConnected(&USB_OTG_FS_dev)));
  }
  
  return(status);
  
}
int8_t if_readBuf(hwInterface* file,uint32_t address,uint8_t* buf)
{
  int8_t status = EFS_ERROR;
  if(HCD_IsDeviceConnected(&USB_OTG_FS_dev))
  {  
    
    do
    {
      status = USBH_MSC_Read10(buf,address,512);
      USBH_MSC_HandleBOTXfer();
    }
    while((status == USBH_MSC_BUSY ) && (HCD_IsDeviceConnected(&USB_OTG_FS_dev)));
    
  }
  
  return(status);
  
}
static USBH_Status USBH_MSC_Handle(USB_OTG_CORE_HANDLE *pdev , 
								   void   *phost)
{
	USBH_HOST *pphost = phost;

	USBH_Status status = USBH_BUSY;
	uint8_t mscStatus = USBH_MSC_BUSY;
	uint8_t appliStatus = 0;

	static uint8_t maxLunExceed = FALSE;


	if(HCD_IsDeviceConnected(pdev))
	{   
		//static uint8_t MSCState_old = -1;
		//if (USBH_MSC_BOTXferParam.MSCState != MSCState_old){
		//	USBH_SetState(1, USBH_MSC_BOTXferParam.MSCState);
		//	MSCState_old = USBH_MSC_BOTXferParam.MSCState;
		//}

		switch(USBH_MSC_BOTXferParam.MSCState)
		{
		case USBH_MSC_BOT_INIT_STATE:
			USBH_MSC_Init(pdev);
			USBH_MSC_BOTXferParam.MSCState = USBH_MSC_BOT_RESET;  
			break;

		case USBH_MSC_BOT_RESET:   
			/* Issue BOT RESET request */
			status = USBH_MSC_BOTReset(pdev, phost);
			if(status == USBH_OK )
			{
				USBH_MSC_BOTXferParam.MSCState = USBH_MSC_GET_MAX_LUN;
			}

			if(status == USBH_NOT_SUPPORTED )
			{
				/* If the Command has failed, then we need to move to Next State, after
				STALL condition is cleared by Control-Transfer */
				USBH_MSC_BOTXferParam.MSCStateBkp = USBH_MSC_GET_MAX_LUN; 

				/* a Clear Feature should be issued here */
				USBH_MSC_BOTXferParam.MSCState = USBH_MSC_CTRL_ERROR_STATE;
			}  
			break;

		case USBH_MSC_GET_MAX_LUN:
			/* Issue GetMaxLUN request */
			status = USBH_MSC_GETMaxLUN(pdev, phost);

			if(status == USBH_OK )
			{
				MSC_Machine.maxLun = *(MSC_Machine.buff) ;

				/* If device has more that one logical unit then it is not supported */
				if((MSC_Machine.maxLun > 0) && (maxLunExceed == FALSE))
				{
					maxLunExceed = TRUE;
					pphost->usr_cb->DeviceNotSupported();

					break;
				}
				USBH_MSC_BOTXferParam.MSCState = USBH_MSC_TEST_UNIT_READY;
			}

			if(status == USBH_NOT_SUPPORTED )
			{
				/* If the Command has failed, then we need to move to Next State, after
				STALL condition is cleared by Control-Transfer */
				USBH_MSC_BOTXferParam.MSCStateBkp = USBH_MSC_TEST_UNIT_READY; 

				/* a Clear Feature should be issued here */
				USBH_MSC_BOTXferParam.MSCState = USBH_MSC_CTRL_ERROR_STATE;
			}    
			break;

		case USBH_MSC_CTRL_ERROR_STATE:
			/* Issue Clearfeature request */
			status = USBH_ClrFeature(pdev,
				phost,
				0x00,
				pphost->Control.hc_num_out);
			if(status == USBH_OK )
			{
				/* If GetMaxLun Request not support, assume Single LUN configuration */
				MSC_Machine.maxLun = 0;  

				USBH_MSC_BOTXferParam.MSCState = USBH_MSC_BOTXferParam.MSCStateBkp;     
			}
			break;  

		case USBH_MSC_TEST_UNIT_READY:
			/* Issue SCSI command TestUnitReady */ 
			mscStatus = USBH_MSC_TestUnitReady(pdev);

			if(mscStatus == USBH_MSC_OK )
			{
				USBH_MSC_BOTXferParam.MSCState = USBH_MSC_READ_CAPACITY10;
				MSCErrorCount = 0;
				status = USBH_OK;
			}
			else
			{
				USBH_MSC_ErrorHandle(mscStatus);
			} 
			break;

		case USBH_MSC_READ_CAPACITY10:
			/* Issue READ_CAPACITY10 SCSI command */
			mscStatus = USBH_MSC_ReadCapacity10(pdev);
			if(mscStatus == USBH_MSC_OK )
			{
				USBH_MSC_BOTXferParam.MSCState = USBH_MSC_MODE_SENSE6;
				MSCErrorCount = 0;
				status = USBH_OK;
			}
			else
			{
				USBH_MSC_ErrorHandle(mscStatus);
			}
			break;

		case USBH_MSC_MODE_SENSE6:
			/* Issue ModeSense6 SCSI command for detecting if device is write-protected */
			mscStatus = USBH_MSC_ModeSense6(pdev);
			if(mscStatus == USBH_MSC_OK )
			{
				USBH_MSC_BOTXferParam.MSCState = USBH_MSC_DEFAULT_APPLI_STATE;
				MSCErrorCount = 0;
				status = USBH_OK;
			}
			else
			{
				USBH_MSC_ErrorHandle(mscStatus);
			}
			break;

		case USBH_MSC_REQUEST_SENSE:
			/* Issue RequestSense SCSI command for retreiving error code */
			mscStatus = USBH_MSC_RequestSense(pdev);
			if(mscStatus == USBH_MSC_OK )
			{
				USBH_MSC_BOTXferParam.MSCState = USBH_MSC_BOTXferParam.MSCStateBkp;
				status = USBH_OK;
			}
			else
			{
				USBH_MSC_ErrorHandle(mscStatus);
			}  
			break;

		case USBH_MSC_BOT_USB_TRANSFERS:
			/* Process the BOT state machine */
			USBH_MSC_HandleBOTXfer(pdev , phost);
			break;

		case USBH_MSC_DEFAULT_APPLI_STATE:
			/* Process Application callback for MSC */
			appliStatus = pphost->usr_cb->UserApplication();
			if(appliStatus == 0)
			{
				USBH_MSC_BOTXferParam.MSCState = USBH_MSC_DEFAULT_APPLI_STATE;
			}
			else if (appliStatus == 1) 
			{
				/* De-init requested from application layer */
				status =  USBH_APPLY_DEINIT;
			}
			break;

		case USBH_MSC_UNRECOVERED_STATE:

			status = USBH_UNRECOVERED_ERROR;

			break;

		default:
			break; 

		}
	}
	return status;
}