drivers/storage/spi_sdcard.c
/*
 * sd_spi_read_logical_block - reads a logical block from the sd card
 *
 * @spi     - the SPI device that we will be using
 * @buffer  - place where we store the data
 * @lba_index - logical block index
 * @offset_in_lba - the offset in a 512 byte logical block
 * @requested_read_size - the number of bytes that we want to read
 *
 *  This function reads up to requested_read_size and returns 0 upon
 *  success.
 */


This function reads up to requested_read_size and returns 0 upon
success.
This function reads up to requested_read_size and returns 0 upon success .
static int sd_spi_read_logical_block(spi_master_dev_t *spi,
                                     uint8_t *buffer,
                                     uint32_t lba_index,
                                     uint8_t offset_in_lba,
                                     size_t requested_read_size)
{
  int timer = 100, trailing_bytes;
  uint8_t spi_rsp;
  if (spi == NULL ||
      g_sd_card.type == SD_BAD_TYPE) {
    return -ENODEV;
  }
  if (offset_in_lba + requested_read_size > SD_LOGICAL_BLOCK_SIZE ||
      buffer == NULL) {
    return -EINVAL;
  }
  if (g_sd_card.type == SD_LOW_CAPACITY_CARD) {
    lba_index *= SD_LOGICAL_BLOCK_SIZE;
  }
  if (lba_index > g_sd_card.num_blocks * 1024) {
    return -ESPIPE;
  }
  sd_spi_set_cs(0);
  sd_spi_send_cmd(SPI_READ_SINGLE_BLOCK, lba_index);
  sd_spi_set_cs(1);
  spi_rsp = sd_read_byte_ignore_char(0xFF);
  if (spi_rsp != 0) {
    LOG_ERR("read logical block %d\n", lba_index);
    return -ENOSYS;
  }
  sd_spi_set_cs(0);
  do {
    sd_spi_read(&spi_rsp, 1);
  } while (--timer > 0 && spi_rsp != 0xFE);
  sd_spi_set_cs(1);
  if (timer == 0 && spi_rsp != 0xFE) {
    LOG_ERR("timeout trying to read block %d\n", lba_index);
    return -ENOSYS;
  }
  trailing_bytes = (SD_LOGICAL_BLOCK_SIZE + SD_CRC_BLOCK_SIZE) - offset_in_lba
    - requested_read_size;
  sd_spi_set_cs(0);
  while (offset_in_lba) {
    sd_spi_write(0xFF);
  }
  while (requested_read_size > 0) {
    *buffer++ = sd_spi_write(0xFF);
    requested_read_size--;
  }
  while (trailing_bytes > 0) {
    sd_spi_write(0xFF);
    trailing_bytes--;
  }
  sd_spi_set_cs(1);
  return 0;
}
static int sd_spi_read_logical_block ( spi_master_dev_t * spi , uint8_t * buffer , uint32_t lba_index , uint8_t offset_in_lba , size_t requested_read_size ) { int timer = 100 , trailing_bytes ; uint8_t spi_rsp ; if ( spi == NULL || g_sd_card . type == SD_BAD_TYPE ) { return - ENODEV ; } if ( offset_in_lba + requested_read_size > SD_LOGICAL_BLOCK_SIZE || buffer == NULL ) { return - EINVAL ; } if ( g_sd_card . type == SD_LOW_CAPACITY_CARD ) { lba_index *= SD_LOGICAL_BLOCK_SIZE ; } if ( lba_index > g_sd_card . num_blocks * 1024 ) { return - ESPIPE ; } sd_spi_set_cs ( 0 ) ; sd_spi_send_cmd ( SPI_READ_SINGLE_BLOCK , lba_index ) ; sd_spi_set_cs ( 1 ) ; spi_rsp = sd_read_byte_ignore_char ( 0xFF ) ; if ( spi_rsp != 0 ) { LOG_ERR ( " \n " , lba_index ) ; return - ENOSYS ; } sd_spi_set_cs ( 0 ) ; do { sd_spi_read ( & spi_rsp , 1 ) ; } while ( -- timer > 0 && spi_rsp != 0xFE ) ; sd_spi_set_cs ( 1 ) ; if ( timer == 0 && spi_rsp != 0xFE ) { LOG_ERR ( " \n " , lba_index ) ; return - ENOSYS ; } trailing_bytes = ( SD_LOGICAL_BLOCK_SIZE + SD_CRC_BLOCK_SIZE ) - offset_in_lba - requested_read_size ; sd_spi_set_cs ( 0 ) ; while ( offset_in_lba ) { sd_spi_write ( 0xFF ) ; } while ( requested_read_size > 0 ) { * buffer ++ = sd_spi_write ( 0xFF ) ; requested_read_size -- ; } while ( trailing_bytes > 0 ) { sd_spi_write ( 0xFF ) ; trailing_bytes -- ; } sd_spi_set_cs ( 1 ) ; return 0 ; }
sd_spi_read_logical_block - reads a logical block from the sd card
 @spi     - the SPI device that we will be using
 @buffer  - place where we store the data
 @lba_index - logical block index
 @offset_in_lba - the offset in a 512 byte logical block
 @requested_read_size - the number of bytes that we want to read
sd_spi_read_logical_block - reads a logical block from the sd card @spi - the SPI device that we will be using @buffer - place where we store the data @lba_index - logical block index @offset_in_lba - the offset in a 512 byte logical block @requested_read_size - the number of bytes that we want to read
/* Some mandatory checks */