/**
  * @brief  Reads data from the medium.
  * @param  lun: Logical unit number
  * @param  blk_addr: Logical block address
  * @param  blk_len: Blocks number
  * @retval Status (0: Ok / -1: Error)
  */
int8_t STORAGE_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
  int8_t ret = -1;  
  
  if(BSP_SD_IsDetected() != SD_NOT_PRESENT)
  {  
    BSP_SD_ReadBlocks_DMA((uint32_t *)buf, blk_addr * STORAGE_BLK_SIZ, STORAGE_BLK_SIZ, blk_len);
    ret = 0;
  }
  return ret;
}
Example #2
0
/**
  * @brief  Reads Sector(s)
  * @param  *buff: Data buffer to store read data
  * @param  sector: Sector address (LBA)
  * @param  count: Number of sectors to read (1..128)
  * @retval DRESULT: Operation result
  */
DRESULT SD_read(BYTE *buff, DWORD sector, BYTE count)
{
  uint32_t timeout = 100000;
  DWORD scratch [BLOCK_SIZE / 4];  /* Alignment ensured, need enough stack */
  uint8_t SD_state = MSD_OK;
    
  if ((DWORD)buff & 3) /* DMA Alignment issue, do single up to aligned buffer */
  {
    while (count--)
    {
      SD_state = BSP_SD_ReadBlocks_DMA((uint32_t*)scratch, (uint64_t) ((sector + count) * BLOCK_SIZE), BLOCK_SIZE, 1);
      
      while(BSP_SD_GetStatus() != SD_TRANSFER_OK)
      {
        if (timeout-- == 0)
        {
          return RES_ERROR;
        }
      }
      memcpy (&buff[count * BLOCK_SIZE] ,scratch, BLOCK_SIZE);
    }
  }
  else
  {
    SD_state = BSP_SD_ReadBlocks_DMA((uint32_t*)buff, (uint64_t) (sector * BLOCK_SIZE), BLOCK_SIZE, count);
    
    while(BSP_SD_GetStatus() != SD_TRANSFER_OK)
    {  
      if (timeout-- == 0)
      {
        return RES_ERROR;
      }
    }
  }
  if (SD_state == MSD_OK)
  {
    return RES_OK;
  }
  
  return RES_ERROR;
}
Example #3
0
/**
  * @brief  Reads Sector(s)
  * @param  lun : not used
  * @param  *buff: Data buffer to store read data
  * @param  sector: Sector address (LBA)
  * @param  count: Number of sectors to read (1..128)
  * @retval DRESULT: Operation result
  */
DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)
{
  DRESULT res = RES_OK;
  
  if(BSP_SD_ReadBlocks_DMA((uint32_t*)buff, 
                           (uint64_t) (sector * BLOCK_SIZE), 
                           BLOCK_SIZE, 
                           count) != MSD_OK)
  {
    res = RES_ERROR;
  }
  
  return res;
}
/**
  * @brief  Reads Sector(s)
  * @param  lun : not used
  * @param  *buff: Data buffer to store read data
  * @param  sector: Sector address (LBA)
  * @param  count: Number of sectors to read (1..128)
  * @retval DRESULT: Operation result
  */
DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)
{
  DRESULT res = RES_ERROR;
  ReadStatus = 0;
  uint32_t timeout;
#if (ENABLE_SD_DMA_CACHE_MAINTENANCE == 1)
  uint32_t alignedAddr;
#endif

  if(BSP_SD_ReadBlocks_DMA((uint32_t*)buff,
                           (uint32_t) (sector),
                           count) == MSD_OK)
  {
    /* Wait for the Rading process is completed or a timeout occurs */
    timeout = HAL_GetTick();
    while((ReadStatus == 0) && ((HAL_GetTick() - timeout) < SD_TIMEOUT))
    {
    }
    /* incase of a timeout return error */
    if (ReadStatus == 0)
    {
      res = RES_ERROR;
    }
    else
    {
      ReadStatus = 0;
      timeout = HAL_GetTick();

      while((HAL_GetTick() - timeout) < SD_TIMEOUT)
      {
        if (BSP_SD_GetCardState() == SD_TRANSFER_OK)
        {
          res = RES_OK;
#if (ENABLE_SD_DMA_CACHE_MAINTENANCE == 1)
            /*
               the SCB_InvalidateDCache_by_Addr() requires a 32-Bit aligned address,
               adjust the address and the D-Cache size to invalidate accordingly.
             */
            alignedAddr = (uint32_t)buff & ~3;
            SCB_InvalidateDCache_by_Addr((uint32_t*)alignedAddr, count*BLOCKSIZE + ((uint32_t)buff - alignedAddr));
#endif
           break;
        }
      }
    }
  }

  return res;
}
Example #5
0
unsigned int _mmcsd_read(void *SdStruct, void *ptr, unsigned long block, unsigned int nblks)
{
	//HAL_SD_CardInfoTypedef *_SDCardInfo = (HAL_SD_CardInfoTypedef *)SdStruct;
	if(status_led)
		gpio.out(status_led, 1);
	HAL_SD_ErrorTypedef status = MSD_OK;
	status =  BSP_SD_ReadBlocks_DMA((unsigned long *)ptr, block << 9, 512, nblks);
	if (status == MSD_OK) {
		if(status_led)
			gpio.out(status_led, 0);
		return 1;
	} else {
		if(status_led)
			gpio.out(status_led, 0);
		return 0;
	}
}