Esempio n. 1
0
DRESULT disk_read (
	BYTE drv,		/* Physical drive nmuber (0..) */
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	/* Sector address (LBA) */
	BYTE count		/* Number of sectors to read (1..255) */
)
{
	
	

	if(count==1)
  {
         err1 = SD_ReadBlock( &buff[0], sector , SECTOR_SIZE );
		
				 SD_WaitReadOperation();
		
         while(SD_GetStatus() != SD_TRANSFER_OK);
		
	}
	else
  {
         err1 = SD_ReadMultiBlocks( &buff[0] , sector ,SECTOR_SIZE , count );
		
		      SD_WaitReadOperation();
		
          while(SD_GetStatus() != SD_TRANSFER_OK);
 
       
	}
  if( err1 == SD_OK)
     return RES_OK;
	else
		return RES_ERROR;
}
Esempio n. 2
0
/**
 * @brief Tests the SD card Single Blocks operations.
 * @param None
 * @retval None
 */
static void SD_SingleBlockTest(void)
{
    SD_Error status;
    /*---------------Block Read/Write----------------*/
    /* Fill the buffer to send */
    Fill_Buffer(aBuffer_Block_Tx, BLOCK_SIZE, 0x320F);

    /* Write Block of 512 bytes on address 0 */
    status = SD_WriteBlock(aBuffer_Block_Tx, 0x00, BLOCK_SIZE);
    /* check if the Transfer finished */
    status = SD_WaitWriteOperation();
    while(SD_GetStatus() != SD_TRANSFER_OK);

    if(status == SD_OK)
    {
	/* Read Block of 512 bytes on address 0 */
	status = SD_ReadBlock(aBuffer_Block_Rx, 0x00, BLOCK_SIZE);
	/* check if the Transfer finished */
	status = SD_WaitReadOperation();
	while(SD_GetStatus() != SD_TRANSFER_OK);
    }

    /* Check the correctness of written data */
    if(status == SD_OK)
    {
	TransferStatus1 = Buffercmp(aBuffer_Block_Tx, aBuffer_Block_Rx,BLOCK_SIZE);
    }

    if(TransferStatus1 == PASSED) printf("SD single block test passed!\n\r");
}
Esempio n. 3
0
/**
 * @brief Tests the SD card Multiple Blocks operations.
 * @param None
 * @retval None
 */
static void SD_MultiBlockTest(void)
{
    SD_Error status;

    /* Fill the buffer to send */
    Fill_Buffer(aBuffer_MultiBlock_Tx, MULTI_BUFFER_SIZE, 0x0);

    /* Write Multiple Block of many bytes on address 0 */
    status = SD_WriteMultiBlocksFIXED(aBuffer_MultiBlock_Tx, 0, BLOCK_SIZE, NUMBER_OF_BLOCKS);
    /* check if the Transfer finished */
    status = SD_WaitWriteOperation();
    while(SD_GetStatus() != SD_TRANSFER_OK);

    if(status == SD_OK)
    {
	/* Read Block of many bytes from address 0 */
	status = SD_ReadMultiBlocksFIXED(aBuffer_MultiBlock_Rx, 0, BLOCK_SIZE, NUMBER_OF_BLOCKS);
	/* check if the Transfer finished */
	status = SD_WaitReadOperation();
	while(SD_GetStatus() != SD_TRANSFER_OK);
    }

    /* Check the correctness of written data */
    if(status == SD_OK)
    {
	TransferStatus2 = Buffercmp(aBuffer_MultiBlock_Tx, aBuffer_MultiBlock_Rx,MULTI_BUFFER_SIZE);
    }

    if(TransferStatus2 == PASSED) printf("SD Multiple block test passed!\n\r");

}
Esempio n. 4
0
DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	/* Sector address in LBA */
	UINT count		/* Number of sectors to read */
)
{
	if(count>1)
	{
		SD_ReadMultiBlocks(buff, sector*BLOCK_SIZE, BLOCK_SIZE, count);
		// check transfer finished
		SD_WaitReadOperation();
		// wait the DMA transfer finished
		while(SD_GetStatus() != SD_TRANSFER_OK);
	}
	else
	{
		SD_ReadBlock(buff, sector*BLOCK_SIZE, BLOCK_SIZE);
		// check transfer finished
		SD_WaitReadOperation();
		// wait the DMA transfer finished
		while(SD_GetStatus() != SD_TRANSFER_OK);

	}
	return RES_OK;	
}
Esempio n. 5
0
DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber to identify the drive */
	const BYTE *buff,	/* Data to be written */
	DWORD sector,		/* Sector address in LBA */
	UINT count			/* Number of sectors to write */
)
{
	if(count>1)
	{
		SD_WriteMultiBlocks((uint8_t *)buff, sector*BLOCK_SIZE, BLOCK_SIZE, count);
		// check transfer finished
		SD_WaitWriteOperation();
		// wait the DMA transfer finished
		while(SD_GetStatus() != SD_TRANSFER_OK);
	}
	else
	{
		SD_WriteBlock((uint8_t *)buff, sector*BLOCK_SIZE, BLOCK_SIZE);
		// check transfer finished
		SD_WaitReadOperation();
		// wait the DMA transfer finished
		while(SD_GetStatus() != SD_TRANSFER_OK);

	}

	return RES_OK;
}
Esempio n. 6
0
DRESULT disk_write (
	BYTE drv,			/* Physical drive nmuber (0..) */
	const BYTE *buff,	/* Data to be written */
	DWORD sector,		/* Sector address (LBA) */
	BYTE count			/* Number of sectors to write (1..255) */
)
{
  //memset(buff2, 0, sizeof(buff2));
	if(count==1)
        {
          //memcpy(buff2,buff,SECTOR_SIZE);
          err1 =  SD_WriteBlock((uint8_t *)(&buff[0]),sector  ,SECTOR_SIZE);
					
					SD_WaitWriteOperation();
					
          while(SD_GetStatus() != SD_TRANSFER_OK);
					
	      }
	else
        {
          //memcpy(buff2,buff,SECTOR_SIZE * count);
          err1 = SD_WriteMultiBlocks((uint8_t *)(&buff[0]),sector  ,SECTOR_SIZE,count);
					
				  SD_WaitWriteOperation();          

					while(SD_GetStatus() != SD_TRANSFER_OK);
					
	      }
        
  if( err1 == SD_OK)
     return RES_OK;
	else
		return RES_ERROR;
}
Esempio n. 7
0
DRESULT disk_read (
	BYTE drv,		/* Physical drive nmuber (0..) */
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	/* Sector address (LBA) */
	BYTE count		/* Number of sectors to read (1..255) */
)
{

	u8 res=0; 
    if (!count)return RES_PARERR;//count不能等于0,否则返回参数错误		 	 
	switch(drv)
	{
		case SD_CARD://SD卡
			if (count > 1)
			{
				SD_ReadMultiBlocks(buff, sector*BLOCK_SIZE, BLOCK_SIZE, count);
			
					  /* Check if the Transfer is finished */
			     SD_WaitReadOperation();  //循环查询dma传输是否结束
			
			    /* Wait until end of DMA transfer */
			    while(SD_GetStatus() != SD_TRANSFER_OK);
		
			}
			else
			{
				
				SD_ReadBlock(buff, sector*BLOCK_SIZE, BLOCK_SIZE);
		
					  /* Check if the Transfer is finished */
			     SD_WaitReadOperation();  //循环查询dma传输是否结束
			
			    /* Wait until end of DMA transfer */
			    while(SD_GetStatus() != SD_TRANSFER_OK);
		
			}
			break;
		case EX_FLASH://外部flash
			for(;count>0;count--)
			{
				SPI_Flash_Read(buff,sector*BLOCK_SIZE,BLOCK_SIZE);
				sector++;
				buff+=BLOCK_SIZE;
			}
			res=0;
			break;
		default:
			res=1; 
	}
   //处理返回值,将SPI_SD_driver.c的返回值转成ff.c的返回值
    if(res==0x00)return RES_OK;	 
    else return RES_ERROR;

}
Esempio n. 8
0
DRESULT disk_write (
	BYTE drv,			/* Physical drive nmuber (0..) */
	const BYTE *buff,	/* Data to be written */
	DWORD sector,		/* Sector address (LBA) */
	BYTE count			/* Number of sectors to write (1..255) */
)
{

	u8 res=0;  
    if (!count)return RES_PARERR;//count不能等于0,否则返回参数错误		 	 
	switch(drv)
	{
		case SD_CARD://SD卡
			if (count > 1)
			{
				SD_WriteMultiBlocks((uint8_t *)buff, sector*BLOCK_SIZE, BLOCK_SIZE, count);
				
				  /* Check if the Transfer is finished */
			  	 SD_WaitWriteOperation();	   //等待dma传输结束
			    while(SD_GetStatus() != SD_TRANSFER_OK); //等待sdio到sd卡传输结束
			}
			else
			{
				SD_WriteBlock((uint8_t *)buff,sector*BLOCK_SIZE, BLOCK_SIZE);
				
				  /* Check if the Transfer is finished */
			   		SD_WaitWriteOperation();	   //等待dma传输结束
			    while(SD_GetStatus() != SD_TRANSFER_OK); //等待sdio到sd卡传输结束
			}
			break;
		case EX_FLASH://外部flash
			for(;count>0;count--)
			{										    
				SPI_Flash_Write((u8*)buff,sector*BLOCK_SIZE,BLOCK_SIZE);
				sector++;
				buff+=BLOCK_SIZE;
			}
			res=0;
			break;
		default:
			res=1; 
	}
    //处理返回值,将SPI_SD_driver.c的返回值转成ff.c的返回值
    if(res == 0x00)return RES_OK;	 
    else return RES_ERROR;	


}
Esempio n. 9
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) */
                     )
{
  uint32_t timeout = 100000;
  SD_Error sdstatus = SD_OK;

        SD_ReadMultiBlocks((BYTE *)buff, (uint32_t )(sector * 512), 512, count);
        /* Check if the Transfer is finished */
        sdstatus = SD_WaitReadOperation();

        while(SD_GetStatus() != SD_TRANSFER_OK)
        {

          if (timeout-- == 0)
          {
            return RES_ERROR;
          }
        }

        if (sdstatus == SD_OK)
        {
          return RES_OK;
        }
      

    return RES_NOTRDY;
}
/*******************************************************************************
* Function Name  : MAL_Read
* Description    : Read sectors
* Input          : None
* Output         : None
* Return         : Buffer pointer
*******************************************************************************/
uint16_t MAL_Read(uint8_t lun, uint32_t Memory_Offset, uint32_t *Readbuff, uint16_t Transfer_Length)
{

  switch (lun)
  {
    case 0:

     // SD_ReadMultiBlocks((uint8_t*)Readbuff, Memory_Offset, Transfer_Length, 1);
      SD_ReadBlock((uint8_t*)Readbuff, Memory_Offset, Transfer_Length);
	  Status = SD_WaitReadOperation();
      while(SD_GetStatus() != SD_TRANSFER_OK)
      {
      }
//    Status = SD_WaitWriteOperation();
//    while(SD_GetStatus() != SD_TRANSFER_OK);
      
      if ( Status != SD_OK )
      {
        return MAL_FAIL;
      }
      break;
//    case 1:
//      NAND_Read(Memory_Offset, Readbuff, Transfer_Length);
//      ;
//      break;
    default:
      return MAL_FAIL;
  }
  return MAL_OK;
}
Esempio n. 11
0
/**
  * @brief  check whether the medium is ready
  * @param  lun : logical unit number
  * @retval Status
  */
int8_t  STORAGE_IsReady (uint8_t lun)
{
#ifndef USE_STM3210C_EVAL   
 
  static int8_t last_status = 0;
 
  if(last_status  < 0)
  {
    SD_Init();
    last_status = 0;
  }
  
  if(SD_GetStatus() != 0)
  {
    last_status = -1;
    return (-1); 
  }
#else
  if( SD_Init() != 0)
  {
    return (-1);
  }  
#endif
  return (0);
}
Esempio n. 12
0
/**
 * @brief Tests the SD card erase operation.
 * @param None
 * @retval None
 */
static void SD_EraseTest(void)
{
    SD_Error status = SD_OK;
    /*---------------Block Erase----------------*/
    /* Erase NumberOfBlocks Blocks of WRITE_BL_LEN(512 Bytes) */
    status = SD_Erase(0x00, (BLOCK_SIZE * NUMBER_OF_BLOCKS));
    if(status != SD_OK) printf("SD_Erase failed: %d.\n\r",status);

    if(status == SD_OK)
    {
	printf("SD_Erase ok, performing SD_ReadMultiBlocks.\n\r");
	status = SD_ReadMultiBlocks(aBuffer_MultiBlock_Rx, 0x0, BLOCK_SIZE, NUMBER_OF_BLOCKS);
	if(status != SD_OK){printf("SD_ReadMultiBlocks Failed\n\r");}

	/* Check if the Transfer is finished */
	status = SD_WaitReadOperation();
	if(status != SD_OK){printf("SD_WaitReadOperation Failed\n\r");}

	/* Wait until end of the DMA transfer */
	while(SD_GetStatus() != SD_TRANSFER_OK);

	printf("SD_TRANSFER OK\n\r");
    }

    /* Check the correctness of erased blocks */
    if(status == SD_OK)
    {
	EraseStatus = eBuffercmp(aBuffer_MultiBlock_Rx, MULTI_BUFFER_SIZE);
    }

    if(EraseStatus == PASSED) printf("SD erase test passed!\n\r");
}
Esempio n. 13
0
/*******************************************************************************
* Function Name  : MAL_Read
* Description    : Read sectors
* Input          : None
* Output         : None
* Return         : Buffer pointer
*******************************************************************************/
uint16_t MAL_Read(uint8_t lun, uint32_t Memory_Offset, uint32_t *Readbuff, uint16_t Transfer_Length)
{
  if(ejected)
    return MAL_FAIL;

  switch (lun)
  {
    case 0:
      SD_ReadMultiBlocks((uint8_t*)Readbuff, Memory_Offset, Transfer_Length, 1);
// #if defined(USE_STM3210E_EVAL) || defined(USE_STM32L152D_EVAL)
      Status = SD_WaitReadOperation();
      while(SD_GetStatus() != SD_TRANSFER_OK)
      {
      }
      
      if ( Status != SD_OK )
      {
        return MAL_FAIL;
      }
// #endif /* USE_STM3210E_EVAL */      
      break;
// #ifdef USE_STM3210E_EVAL
//     case 1:
//       NAND_Read(Memory_Offset, Readbuff, Transfer_Length);
//       ;
//       break;
// #endif
    default:
      return MAL_FAIL;
  }
  return MAL_OK;
}
Esempio n. 14
0
static int8_t  SDStorage_StateChanged (void)
{
  SDTransferState status = SD_TRANSFER_OK;
  static SDTransferState prev_status = SD_TRANSFER_OK;

  if((status = SD_GetStatus()) != SD_TRANSFER_OK)
  {
    SD_Init();
  }

  if (prev_status != status)
  {
    prev_status = status;

    if(status != SD_TRANSFER_OK)
    {
      return -1;
    }
    else
    {
      return 0;
    }
  }

  return 0;
}
Esempio n. 15
0
/**
  * @brief  Tests the SD card erase operation.
  * @param  None
  * @retval None
  */
void SD_EraseTest(void)
{
  /*------------------- Block Erase ------------------------------------------*/
  if (Status == SD_OK)
  {
    /* Erase NumberOfBlocks Blocks of WRITE_BL_LEN(512 Bytes) */
    Status = SD_Erase(0x00, (BLOCK_SIZE * NUMBER_OF_BLOCKS));
  }

  if (Status == SD_OK)
  {
    Status = SD_ReadMultiBlocks(Buffer_MultiBlock_Rx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS);

    /* Check if the Transfer is finished */
    Status = SD_WaitReadOperation();

    /* Wait until end of DMA transfer */
    while(SD_GetStatus() != SD_TRANSFER_OK);
  }

  /* Check the correctness of erased blocks */
  if (Status == SD_OK)
  {
    EraseStatus = eBuffercmp(Buffer_MultiBlock_Rx, MULTI_BUFFER_SIZE);
  }
  
  if(EraseStatus == PASSED)
  {
	  xprintf("Erase test passed\n");
  }
  else
  {
	  xprintf("Erase test failed\n");
  }
}
Esempio n. 16
0
/*******************************************************************************
* Function Name  : MAL_Write
* Description    : Write sectors
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
uint16_t MAL_Write(uint8_t lun, uint32_t Memory_Offset, uint32_t *Writebuff, uint16_t Transfer_Length)
{

  switch (lun)
  {
    case 0:
    Status = SD_WriteMultiBlocks((uint8_t*)Writebuff, Memory_Offset, Transfer_Length,1);
#if defined(USE_STM3210E_EVAL) || defined(USE_STM32L152D_EVAL)
    Status = SD_WaitWriteOperation();  
    while(SD_GetStatus() != SD_TRANSFER_OK);
      if ( Status != SD_OK )
      {
        return MAL_FAIL;
      }      
#endif /* USE_STM3210E_EVAL ||USE_STM32L152D_EVAL*/      
      break;
#ifdef USE_STM3210E_EVAL
    case 1:
      NAND_Write(Memory_Offset, Writebuff, Transfer_Length);
      break;
#endif /* USE_STM3210E_EVAL */  
    default:
      return MAL_FAIL;
  }
  return MAL_OK;
}
Esempio n. 17
0
/*-----------------------------------------------------------------------*/
DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	/* Sector address (LBA) */
	UINT count		/* Number of sectors to read (1..128) */
)
{
	SD_Error Status;

  if(pdrv || !count)
    return RES_PARERR;

	if(count==1)
		Status = SD_ReadBlock(buff ,sector<< 9 , SDCardInfo.CardBlockSize );                                                              
	else
		Status = SD_ReadMultiBlocks(buff ,sector<< 9 ,SDCardInfo.CardBlockSize,count);

  /* Check if the Transfer is finished */
  Status = SD_WaitReadOperation();
  while(SD_GetStatus() != SD_TRANSFER_OK);

	if(Status == SD_OK)
		return RES_OK;
	else
		return RES_ERROR;
}
Esempio n. 18
0
/**
   * @brief  Read Sector(s) 
   * @param   drv : driver index
   * @retval DSTATUS : operation status
  */
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) */
                     )
{
  
  SD_Error sdstatus = SD_OK;
 
  if (drv == 0)
  {
    SD_ReadMultiBlocks(buff, sector << 9, 512, count);
    
    /* Check if the Transfer is finished */
    sdstatus =  SD_WaitReadOperation();
    while(SD_GetStatus() != SD_TRANSFER_OK);
    
    if (sdstatus == SD_OK)
    {
      return RES_OK;
    }
  }
  return RES_ERROR;
}
Esempio n. 19
0
DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber (0..) */
	const BYTE *buff,	/* Data to be written */
	DWORD sector,		/* Sector address (LBA) */
	UINT count			/* Number of sectors to write (1..128) */
)
{
	SD_Error Status;

  if(pdrv || !count)
    return RES_PARERR;

	if(count==1)
		Status = SD_WriteBlock((u8 *)buff ,sector << 9 , SDCardInfo.CardBlockSize);                                                
	else {
		Status = SD_WriteMultiBlocks((u8 *)buff ,sector << 9 ,SDCardInfo.CardBlockSize,count);	  
		Status = SD_WaitWriteOperation();
	}

  /* Check if the Transfer is finished */
  Status = SD_WaitReadOperation();
	while(SD_GetStatus() != SD_TRANSFER_OK);
	
	if(Status == SD_OK)
		return RES_OK;
	else
		return RES_ERROR;
}
Esempio n. 20
0
//=========================================================================================
DSTATUS FATFS_SD_SDIO_disk_status(void) 
{
	Stat = STA_NOINIT;

	/* Check SDCARD status */
	if (SD_GetStatus() == MSD_OK) Stat &= ~STA_NOINIT;
	else Stat |= STA_NOINIT;

	return Stat;
}
/**
  * @brief  Tests the SD card Multiple Blocks operations.
  * @param  None
  * @retval None
  */
void SD_MultiBlockTest(void)
{
  /*--------------- Multiple Block Read/Write ---------------------*/
  /* Fill the buffer to send */
  Fill_Buffer(Buffer_MultiBlock_Tx, MULTI_BUFFER_SIZE, 0x0);

  if (Status == SD_OK)
  {
    /* Write multiple block of many bytes on address 0 */
    Status = SD_WriteMultiBlocks(Buffer_MultiBlock_Tx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS);
    /* Check if the Transfer is finished */
    Status = SD_WaitWriteOperation();
    while(SD_GetStatus() != SD_TRANSFER_OK);
  }

  if (Status == SD_OK)
  {
    /* Read block of many bytes from address 0 */
    Status = SD_ReadMultiBlocks(Buffer_MultiBlock_Rx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS);
    /* Check if the Transfer is finished */
    Status = SD_WaitReadOperation();
    while(SD_GetStatus() != SD_TRANSFER_OK);
  }

  /* Check the correctness of written data */
  if (Status == SD_OK)
  {
    TransferStatus2 = Buffercmp(Buffer_MultiBlock_Tx, Buffer_MultiBlock_Rx, MULTI_BUFFER_SIZE);
  }
  
  if(TransferStatus2 == PASSED)
  {
    STM_EVAL_LEDOn(LED3);
	LCD_DisplayStringLine(LCD_LINE_2, "Passed");
  }
  else
  {
    STM_EVAL_LEDOff(LED3);
    STM_EVAL_LEDOn(LED4);    
	LCD_DisplayStringLine(LCD_LINE_2, "failed");
  }
}
/**
  * @brief  Tests the SD card Single Blocks operations.
  * @param  None
  * @retval None
  */
void SD_SingleBlockTest(void)
{
  /*------------------- Block Read/Write --------------------------*/
  /* Fill the buffer to send */
  Fill_Buffer(Buffer_Block_Tx, BLOCK_SIZE, 0x320F);

  if (Status == SD_OK)
  {
    /* Write block of 512 bytes on address 0 */
    Status = SD_WriteBlock(Buffer_Block_Tx, 0x00, BLOCK_SIZE);
    /* Check if the Transfer is finished */
    Status = SD_WaitWriteOperation();
    while(SD_GetStatus() != SD_TRANSFER_OK);
  }

  if (Status == SD_OK)
  {
    /* Read block of 512 bytes from address 0 */
    Status = SD_ReadBlock(Buffer_Block_Rx, 0x00, BLOCK_SIZE);
    /* Check if the Transfer is finished */
    Status = SD_WaitReadOperation();
    while(SD_GetStatus() != SD_TRANSFER_OK);
  }

  /* Check the correctness of written data */
  if (Status == SD_OK)
  {
    TransferStatus1 = Buffercmp(Buffer_Block_Tx, Buffer_Block_Rx, BLOCK_SIZE);
  }
  
  if(TransferStatus1 == PASSED)
  {
    STM_EVAL_LEDOn(LED2);
	LCD_DisplayStringLine(LCD_LINE_2, "Passed");
  }
  else
  {
    STM_EVAL_LEDOff(LED2);
    STM_EVAL_LEDOn(LED4);    
	LCD_DisplayStringLine(LCD_LINE_2, "failed");
  }
}
Esempio n. 23
0
DRESULT disk_read (
        BYTE drv,               /* Physical drive nmuber (0..) */
        BYTE *buff,             /* Data buffer to store read data */
        DWORD sector,   				/* Sector address (LBA) */
        BYTE count              /* Number of sectors to read (1..255) */
)
{
	SD_Error Status;

#ifdef DBGIO
	printf("disk_read %d %p %10d %d\n",drv,buff,sector,count);
#endif
	
	if (SD_Detect() != SD_PRESENT)
		return(RES_NOTRDY);

	if ((DWORD)buff & 3) // DMA Alignment failure, do single up to aligned buffer
	{
		DRESULT res = RES_OK;
		DWORD scratch[BLOCK_SIZE / 4]; // Alignment assured, you'll need a sufficiently big stack

		while(count--)
		{
			res = disk_read(drv, (void *)scratch, sector++, 1);

			if (res != RES_OK)
				break;

			memcpy(buff, scratch, BLOCK_SIZE);

			buff += BLOCK_SIZE;
		}

		return(res);
	}

  Status = SD_ReadMultiBlocksFIXED(buff, sector, BLOCK_SIZE, count); // 4GB Compliant

	if (Status == SD_OK)
	{
		SDTransferState State;

		Status = SD_WaitReadOperation(); // Check if the Transfer is finished

		while((State = SD_GetStatus()) == SD_TRANSFER_BUSY); // BUSY, OK (DONE), ERROR (FAIL)

		if ((State == SD_TRANSFER_ERROR) || (Status != SD_OK))
			return(RES_ERROR);
		else
			return(RES_OK);
	}
	else
		return(RES_ERROR);
}
Esempio n. 24
0
/**
   * @brief  Get Disk Status  
   * @param   drv : driver index
   * @retval DSTATUS : operation status
*/
DSTATUS disk_status ( BYTE drv	)
{  
  Stat = STA_NOINIT;
  
  if ((drv == 0) && (SD_GetStatus() == 0))
  {
    Stat &= ~STA_NOINIT;
  }
  
  return Stat;  
}
BOOL SD_GetStatus_WithTimeOut(int timeout)
{  
    //while(SD_GetStatus() != SD_TRANSFER_OK);
    for (;timeout>0; timeout--)
    {
      HAL_Time_Sleep_MicroSeconds_InterruptEnabled(1);
      if (SD_GetStatus() == SD_TRANSFER_OK) 
        return TRUE;
    }
    return FALSE;
}
    bool SdioSecureDigitalCard::handleWriteReturn(SD_Error sdError,uint32_t errorCode) {

        if(sdError==SD_OK) {

            if((sdError=SD_WaitWriteOperation())!=SD_OK)
                return handleReturnCode(sdError,errorCode);

            while(SD_GetStatus()!=SD_TRANSFER_OK);
            return true;
        }
        else
            return handleReturnCode(sdError,errorCode);
    }
Esempio n. 27
0
/**
  * @brief  Tests the SD card Single Blocks operations.
  * @param  None
  * @retval None
  */
void SD_SingleBlockTest(void)
{
  /*------------------- Block Read/Write --------------------------*/
  /* Fill the buffer to send */
  Fill_Buffer(Buffer_Block_Tx, BLOCK_SIZE, 0x320F);

  if (Status == SD_OK)
  {
    /* Write block of 512 bytes on address 0 */
    Status = SD_WriteBlock(Buffer_Block_Tx, 0x00, BLOCK_SIZE);
    /* Check if the Transfer is finished */
    Status = SD_WaitWriteOperation();
    while(SD_GetStatus() != SD_TRANSFER_OK);
  }

  if (Status == SD_OK)
  {
    /* Read block of 512 bytes from address 0 */
    Status = SD_ReadBlock(Buffer_Block_Rx, 0x00, BLOCK_SIZE);
    /* Check if the Transfer is finished */
    Status = SD_WaitReadOperation();
    while(SD_GetStatus() != SD_TRANSFER_OK);
  }

  /* Check the correctness of written data */
  if (Status == SD_OK)
  {
    TransferStatus1 = Buffercmp(Buffer_Block_Tx, Buffer_Block_Rx, BLOCK_SIZE);
  }
  
  if(TransferStatus1 == PASSED)
  {
	  xprintf("Block test passed\n");
  }
  else
  {
	  xprintf("Block test failed\n");
  }
}
Esempio n. 28
0
/**
 * @brief  Tests the SD card Multiple Blocks operations.
 * @param  None
 * @retval None
 */
static void SD_MultiBlockTest (void)
{
        /* Fill the buffer to send */
        Fill_Buffer (aBuffer_MultiBlock_Tx, MULTI_BUFFER_SIZE, 0x0);

        if (Status == SD_OK) {
                /* Write multiple block of many bytes on address 0 */
                Status = SD_WriteMultiBlocks (aBuffer_MultiBlock_Tx, 0, BLOCK_SIZE, NUMBER_OF_BLOCKS);

                /* Check if the Transfer is finished */
                Status = SD_WaitWriteOperation ();
                while (SD_GetStatus () != SD_TRANSFER_OK)
                        ;
        }

        if (Status == SD_OK) {
                /* Read block of many bytes from address 0 */
                Status = SD_ReadMultiBlocks (aBuffer_MultiBlock_Rx, 0, BLOCK_SIZE, NUMBER_OF_BLOCKS);

                /* Check if the Transfer is finished */
                Status = SD_WaitReadOperation ();
                while (SD_GetStatus () != SD_TRANSFER_OK)
                        ;
        }

        /* Check the correctness of written data */
        if (Status == SD_OK) {
                TransferStatus2 = Buffercmp (aBuffer_MultiBlock_Tx, aBuffer_MultiBlock_Rx, MULTI_BUFFER_SIZE);
        }

        if (TransferStatus2 == PASSED) {
                logf ("Multiple block test passed\r\n");
        }
        else {
                logf ("Multiple block test failed\r\n");
        }
}
BOOL SD_BS_Driver::ReadSector(SectorAddress Address, UINT32 Offset, UINT32 NumBytes, BYTE *pSectorBuff, UINT32 BytesPerSector)
{
	__IO SD_Error errorstatus = SD_OK;

	errorstatus = SD_ReadBlock(pSectorBuff, ((Address * BytesPerSector) + Offset), BytesPerSector);

	while(SD_GetStatus() != SD_TRANSFER_OK);

	//CPU_FlushCaches();

	if(errorstatus == SD_OK)
		return TRUE;
	else
		return FALSE;
}
Esempio n. 30
0
/* Read Sector(s)                                                        */
DRESULT disk_read (
	BYTE drv,		/* Physical drive nmuber (0..) */
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	/* Sector address (LBA) */
	BYTE count		/* Number of sectors to read (1..255) */
)
{
	//printf("Disk read: Drive %x, Sector: %x, Count: %x\r\n", drv, sector, count);
	SD_Error Status;
	//uint32_t timeout = 1000000;

	if (!count) return RES_PARERR;

	if(drv==0)
	{
		//DMA
		// Status = SD_ReadMultiBlocks(buff, (uint32_t)(sector*512), BlockSize, count);
		// Status = SD_WaitReadOperation();

		// while(SD_GetStatus() != SD_TRANSFER_OK)
		// {
		// 	if(timeout-- == 0)
		// 	{
		// 		printf("SDIO Timeout\r\n");
		// 		return RES_ERROR;
		// 	}
		// }

		//Polling
		for(int i = 0; i < count; i++)
		{
			Status = SD_ReadBlock((BYTE*)(((uintptr_t)buff)+BlockSize*i), (uint32_t)(sector*BlockSize), BlockSize);
			
			while(SD_GetStatus() != SD_TRANSFER_OK);
			
			if(Status != SD_OK)
				return RES_ERROR;
		}

		if(Status == SD_OK)
			return RES_OK;
		else
			return RES_ERROR;
	}
	else
		return RES_ERROR;  
}