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;
}
Ejemplo n.º 2
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;

}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
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_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;
}
/*-----------------------------------------------------------------------*/
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;
}
Ejemplo n.º 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;
}
Ejemplo n.º 8
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;
}
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;
}
Ejemplo n.º 10
0
USBH_Status USBH_MSC_Issue_GETMaxLUN(USB_OTG_CORE_HANDLE *pdev)
{
  USBH_Status status = USBH_BUSY;
  USBH_MSC_BOTXferParam.CmdStateMachine = CMD_SEND_STATE;
  if(HCD_IsDeviceConnected(&USB_OTG_FS_dev))
  {  
    do
    {
      status = USBH_MSC_GETMaxLUN(pdev);
    }
    while((status == USBH_BUSY ) && HCD_IsDeviceConnected(&USB_OTG_FS_dev));
  } 
  return(status);
}
Ejemplo n.º 11
0
/**
  * @brief  Handle Modules Background processes in the main task
  * @param  None
  * @retval None
*/
void MOD_HandleModulesBackground (void)
{
  uint32_t idx = 0, group = 0;  
  
   
  for (group = 0; group < MAX_GROUP_NUM; group ++)
  {
    for (idx = 0 ; idx < MOD_table[group].counter ; idx ++)
    {
      if (MOD_table[group].module[idx]->background != NULL)
      {
        MOD_table[group].module[idx]->background();
      }
    }
  }

  if((USB_Host_Application_Ready == 0) || (HCD_IsDeviceConnected(&USB_OTG_Core) == 0))
  {
    USBH_Process(&USB_OTG_Core, &USB_Host);   
  }
  
  if(SDStorage_StateChanged() < 0)
  {
      MOD_HandleModulesClanup(MSD_MEDIA_STORAGE);
  }
}
Ejemplo n.º 12
0
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);
}
Ejemplo n.º 13
0
/**
* @brief  Show_Image 
*         Displays BMP image
* @param  None
* @retval None
*/
static void Show_Image(void)
{
  uint16_t i = 0;
  uint16_t numOfReadBytes = 0;
  FRESULT res; 

  LCD_SetDisplayWindow(0, 0, 320, 240);
  LCD_WriteReg(SSD2119_ENTRY_MODE_REG, ENTRY_MODE_BMP);
  LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */

  /* Bypass Bitmap header */ 
  f_lseek (&file, 54);
  
  while (HCD_IsDeviceConnected(&USB_OTG_Core))
  {
    res = f_read(&file, Image_Buf, IMAGE_BUFFER_SIZE, (void *)&numOfReadBytes);
    if ((numOfReadBytes == 0) || (res != FR_OK)) {
      /*EOF or Error*/ 
      LCD_WriteReg(SSD2119_ENTRY_MODE_REG, ENTRY_MODE_DEFAULT);		
      break; 
    }
    for (i = 0 ; i < IMAGE_BUFFER_SIZE; i+= 2) {
      LCD_WriteRAM(Image_Buf[i+1] << 8 | Image_Buf[i]); 
    } 
  }
}
Ejemplo n.º 14
0
/**
  * @brief  Handle Modules Background processes in the main task
  * @param  None
  * @retval None
*/
void USBH_USR_BackgroundProcess (void)
{
  if((USB_Host_Application_Ready == 0) || (HCD_IsDeviceConnected(&USB_OTG_Core) == 0))
  {
    USBH_Process(&USB_OTG_Core, &USB_Host);   
  }
}
Ejemplo n.º 15
0
/**
* @brief  Show_Image
*         Displays BMP image
* @param  None
* @retval None
*/
static void Show_Image(void)
{

  uint16_t i = 0;
  uint16_t numOfReadBytes = 0;
  FRESULT res;

  //LCD_SetDisplayWindow(239, 319, 240, 320);
  //LCD_WriteReg(R3, 0x1008);
  //LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */

  /* Bypass Bitmap header */
  f_lseek (&file, 54);

  while (HCD_IsDeviceConnected(&USB_OTG_Core))
  {
    res = f_read(&file, Image_Buf, IMAGE_BUFFER_SIZE, (void *)&numOfReadBytes);
    if((numOfReadBytes == 0) || (res != FR_OK)) /*EOF or Error*/
    {
      break;
    }
    for(i = 0 ; i < IMAGE_BUFFER_SIZE; i+= 2)
    {
      //LCD_WriteRAM(Image_Buf[i+1] << 8 | Image_Buf[i]);
    }
  }

}
Ejemplo n.º 16
0
/**
  * @brief  if_initInterface
  *         Initialises the EFSL interface parameters
  * @param  file : File pointer
  * @param  opts : Optional Parameter. Not used here.
  * @retval Status : 0 -> Pass, -1 -> Fail
  */
int8_t if_initInterface(hwInterface* file, char* opts)
{
  if(HCD_IsDeviceConnected(&USB_OTG_FS_dev))
  {  
        file->sectorCount = USBH_MSC_Param.MSCapacity; 
  }
  return(EFS_PASS);
}
Ejemplo n.º 17
0
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);
  
}
Ejemplo n.º 18
0
void USBH_USR_BackgroundProcess(void)
{
	if ((	USBH_USR_ApplicationState != USH_USR_READY
		&&	USBH_USR_ApplicationState != USH_USR_PROCESS
			)
	||  HCD_IsDeviceConnected(&USB_OTG_Core) == 0
		)
		USBH_Process(&USB_OTG_Core, &USB_Host);
}
/*-----------------------------------------------------------------------*/
DSTATUS TM_FATFS_USB_disk_initialize(void) {
	if (HCD_IsDeviceConnected(&USB_OTG_Core) && USBH_USR_MSC_IsReady()) {
		USB_Stat &= ~STA_NOINIT;
	} else {
		USB_Stat |= STA_NOINIT;
	}

	return USB_Stat;
}
Ejemplo n.º 20
0
/*-----------------------------------------------------------------------*/
DSTATUS TM_FATFS_USB_disk_initialize(void) {
	if (HCD_IsDeviceConnected(&USB_OTG_Core) && TM_USB_MSCHOST_INT_Result == TM_USB_MSCHOST_Result_Connected) {
		USB_Stat &= ~STA_NOINIT;
	} else {
		USB_Stat |= STA_NOINIT;
	}

	return USB_Stat;
}
Ejemplo n.º 21
0
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);
  
}
Ejemplo n.º 22
0
uint32_t write_file(char* _filename, void* _buffer, uint32_t _buf_size)
{
  UINT ret = 0;
  f_unlink(_filename);
  if((HCD_IsDeviceConnected(&USB_OTG_Core) != 1) || (f_open(&file, _filename, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK))
    while(1);
  f_write(&file, _buffer, (UINT)_buf_size, &ret);
  f_close(&file);
  return((uint32_t)ret);
}
Ejemplo n.º 23
0
/**
 * @brief  USBH_MSC_Init 
 *         Initializes the mass storage parameters
 * @param  None
 * @retval None
 */
void USBH_MSC_Init(USB_OTG_CORE_HANDLE *pdev) {
    if (HCD_IsDeviceConnected(pdev)) {
        USBH_MSC_CBWData.field.CBWSignature = USBH_MSC_BOT_CBW_SIGNATURE;
        USBH_MSC_CBWData.field.CBWTag = USBH_MSC_BOT_CBW_TAG;
        USBH_MSC_CBWData.field.CBWLUN = 0; /*Only one LUN is supported*/
        USBH_MSC_BOTXferParam.CmdStateMachine = CMD_SEND_STATE;
    }

    BOTStallErrorCount = 0;
    MSCErrorCount = 0;
}
Ejemplo n.º 24
0
DSTATUS disk_initialize (BYTE drv)    /* Physical drive nmuber (0..) */
{
  DSTATUS stat = STA_NOINIT;
  
  if(HCD_IsDeviceConnected(&USB_OTG_Core_dev))
  {  
    stat &= ~STA_NOINIT;
  }
  
  return stat;
  
}
Ejemplo n.º 25
0
static uint8_t Image_Browser (char* path)
{
  FRESULT res;
  uint8_t ret = 1;
  FILINFO fno;
  DIR dir;
  char *fn;
  
  res = f_opendir(&dir, path);
  if (res == FR_OK) {
    
    for (;;) {
      res = f_readdir(&dir, &fno);
      if (res != FR_OK || fno.fname[0] == 0) break;
      if (fno.fname[0] == '.') continue;

      fn = fno.fname;
 
      if (fno.fattrib & AM_DIR) 
      {
        continue;
      } 
      else 
      {
        if((strstr(fn, "bmp")) || (strstr(fn, "BMP")))
        {
          res = f_open(&file, fn, FA_OPEN_EXISTING | FA_READ);
          Show_Image();
          USB_OTG_BSP_mDelay(100);
          ret = 0;
          while((HCD_IsDeviceConnected(&USB_OTG_Core)) && \
            (STM_EVAL_PBGetState (BUTTON_USER) != SET))
          {
            Toggle_Leds();
          }
          f_close(&file);
          
        }
      }
    }  
  }
  
#ifdef USE_USB_OTG_HS  
  LCD_LOG_SetHeader("PDF Creat");
#else
  LCD_LOG_SetHeader(" USB OTG FS MSC Host");
#endif
  LCD_LOG_SetFooter ("     USB Host Library v2.1.0" );
  LCD_UsrLog("> Disk capacity : %d Bytes\n", USBH_MSC_Param.MSCapacity * \
      USBH_MSC_Param.MSPageLength); 
  USBH_USR_ApplicationState = USH_USR_FS_READLIST;
  return ret;
}
Ejemplo n.º 26
0
DSTATUS disk_initialize (
                         BYTE drv		/* Physical drive number (0) */
                           )
{
  
  if(HCD_IsDeviceConnected(&usbOTGHost))
  {  
    Stat &= ~STA_NOINIT;
  }
  
  return Stat;
  
  
}
Ejemplo n.º 27
0
/**
  * @brief  Programs the internal Flash memory. 
  * @param  None
  * @retval None
  */
void COMMAND_ProgramFlashMemory(void)
{
  __IO uint32_t programcounter = 0x00;
  uint8_t readflag = TRUE;
  uint16_t BytesRead;
  
  /* RAM Address Initialization */
  RamAddress = (uint32_t) & RAM_Buf;
  
  /* Erase address init */
  LastPGAddress = APPLICATION_ADDRESS;
  
  /* While file still contain data */
  while ((readflag == TRUE) && (HCD_IsDeviceConnected(&USB_OTG_Core) == 1))
  {
    /* Read maximum 512 Kbyte from the selected file */
    f_read (&fileR, RAM_Buf, BUFFER_SIZE, (void *)&BytesRead);
    
    /* Temp variable */
    TmpReadSize = BytesRead;
    
    /* The read data < "BUFFER_SIZE" Kbyte */
    if (TmpReadSize < BUFFER_SIZE)
    {
      readflag = FALSE;
    }
    
    /* Program flash memory */
    for (programcounter = TmpReadSize; programcounter != 0; programcounter -= 4)
    {
      TmpProgramCounter = programcounter;
      /* Write word into flash memory */
      if (FLASH_If_ProgramWord((LastPGAddress - TmpProgramCounter + TmpReadSize), \
        *(__IO uint32_t *)(RamAddress - programcounter + TmpReadSize)) != FLASH_COMPLETE)
      {
        /* Toggle Red LED in infinite loop: Flash programming error */
        Fail_Handler();
      }
    }
    /* Update last programmed address value */
    LastPGAddress = LastPGAddress + TmpReadSize;
  }
}
Ejemplo n.º 28
0
int USBH_USR_MSC_Application()
{
  static uint8_t is_rdy = 0;
  if(is_rdy)
  {
    if(HCD_IsDeviceConnected(&USB_OTG_Core) != 1)
      while(1);
    playback(result);
    //write_file("audio.wav", (void*)data, sizeof(uint16_t)*MAX_BUF_SIZE);
    f_mount(0, 0);
    STM_EVAL_LEDOff(LED6);
    while(1);
  }
  else
  {
    if(f_mount( 0, &fatfs ) != FR_OK )
      while(1);
    if(USBH_MSC_Param.MSWriteProtect == DISK_WRITE_PROTECTED)
      while(1);
    is_rdy = 1;
  }
  return 0;
}
Ejemplo n.º 29
0
/**
* @brief  USBH_Process
*         USB Host core main state machine process
* @param  None
* @retval None
*/
void USBH_Process(USB_OTG_CORE_HANDLE *pdev , USBH_HOST *phost)
{
  volatile USBH_Status status = USBH_FAIL;


  /* check for Host port events */
  if ((HCD_IsDeviceConnected(pdev) == 0)&& (phost->gState != HOST_IDLE))
  {
    if(phost->gState != HOST_DEV_DISCONNECTED)
    {
      phost->gState = HOST_DEV_DISCONNECTED;
    }
  }

  switch (phost->gState)
  {

  case HOST_IDLE :

    if (HCD_IsDeviceConnected(pdev))
    {
      phost->gState = HOST_DEV_ATTACHED;
      USB_OTG_BSP_mDelay(100);
    }
    break;

  case HOST_DEV_ATTACHED :

    phost->usr_cb->DeviceAttached();
    phost->Control.hc_num_out = USBH_Alloc_Channel(pdev, 0x00);
    phost->Control.hc_num_in = USBH_Alloc_Channel(pdev, 0x80);

    /* Reset USB Device */
    if ( HCD_ResetPort(pdev) == 0)
    {
      phost->usr_cb->ResetDevice();
      /*  Wait for USB USBH_ISR_PrtEnDisableChange()
      Host is Now ready to start the Enumeration
      */

      phost->device_prop.speed = HCD_GetCurrentSpeed(pdev);

      phost->gState = HOST_ENUMERATION;
      phost->usr_cb->DeviceSpeedDetected(phost->device_prop.speed);

      /* Open Control pipes */
      USBH_Open_Channel (pdev,
                           phost->Control.hc_num_in,
                           phost->device_prop.address,
                           phost->device_prop.speed,
                           EP_TYPE_CTRL,
                           phost->Control.ep0size);

      /* Open Control pipes */
      USBH_Open_Channel (pdev,
                           phost->Control.hc_num_out,
                           phost->device_prop.address,
                           phost->device_prop.speed,
                           EP_TYPE_CTRL,
                           phost->Control.ep0size);
   }
    break;

  case HOST_ENUMERATION:
    /* Check for enumeration status */
    if ( USBH_HandleEnum(pdev , phost) == USBH_OK)
    {
      /* The function shall return USBH_OK when full enumeration is complete */

      /* user callback for end of device basic enumeration */
      phost->usr_cb->EnumerationDone();

      phost->gState  = HOST_USR_INPUT;
    }
    break;

  case HOST_USR_INPUT:
    /*The function should return user response true to move to class state */
    if ( phost->usr_cb->UserInput() == USBH_USR_RESP_OK)
    {
      if((phost->class_cb->Init(pdev, phost))\
        == USBH_OK)
      {
        phost->gState  = HOST_CLASS_REQUEST;
      }
    }
    break;

  case HOST_CLASS_REQUEST:
    /* process class standard contol requests state machine */
    status = phost->class_cb->Requests(pdev, phost);

     if(status == USBH_OK)
     {
       phost->gState  = HOST_CLASS;
     }

     else
     {
       USBH_ErrorHandle(phost, status);
     }


    break;
  case HOST_CLASS:
    /* process class state machine */
    status = phost->class_cb->Machine(pdev, phost);
    USBH_ErrorHandle(phost, status);
    break;

  case HOST_CTRL_XFER:
    /* process control transfer state machine */
    USBH_HandleControl(pdev, phost);
    break;

  case HOST_SUSPENDED:
    break;

  case HOST_ERROR_STATE:
    /* Re-Initilaize Host for new Enumeration */
    USBH_DeInit(pdev, phost);
    phost->usr_cb->DeInit();
    phost->class_cb->DeInit(pdev, &phost->device_prop);
    break;

  case HOST_DEV_DISCONNECTED :

    /* Manage User disconnect operations*/
    phost->usr_cb->DeviceDisconnected();

    /* Re-Initilaize Host for new Enumeration */
    USBH_DeInit(pdev, phost);
    phost->usr_cb->DeInit();
    phost->class_cb->DeInit(pdev, &phost->device_prop);
    USBH_DeAllocate_AllChannel(pdev);
    phost->gState = HOST_IDLE;

    break;

  default :
    break;
  }

}
Ejemplo n.º 30
0
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;
}