Exemple #1
0
/***********************************************************
* 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;
}
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) && TM_USB_MSCHOST_INT_Result == TM_USB_MSCHOST_Result_Connected) {
		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;
}
DRESULT disk_write (
                    BYTE pdrv,			/* Physical drive number (0) */
                    const BYTE *buff,	/* Pointer to the data to be written */
                    DWORD sector,		/* Start sector number (LBA) */
                    UINT count			/* Sector count (1..255) */
                      )
{
  BYTE status = USBH_MSC_OK;
  if (pdrv || !count) return RES_PARERR;
  if (Stat & STA_NOINIT) return RES_NOTRDY;
  if (Stat & STA_PROTECT) return RES_WRPRT;
  
  
  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;
      }
    }
    
    while(status == USBH_MSC_BUSY );
    
  }
  
  if(status == USBH_MSC_OK)
    return RES_OK;
  return RES_ERROR;
}
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);
}