drivers/storage/spi_sdcard.c
/*
 * sd_spi_write_logical_block - write a logical block to 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_write_size - the number of bytes that we want to write
 *
 *  This function writes up to requested_write_size and returns 0 upon
 *  success.
 */


This function writes up to requested_write_size and returns 0 upon
success.
This function writes up to requested_write_size and returns 0 upon success .
static int sd_spi_write_logical_block(spi_master_dev_t *spi,
                                      const uint8_t *buffer,
                                      uint32_t lba_index,
                                      uint8_t offset_in_lba,
                                      size_t requested_write_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_write_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(SD_CMD_SET_WRITE_BLOCK, lba_index);
  sd_spi_set_cs(1);
  spi_rsp = sd_read_byte_ignore_char(0xFF);
  if (spi_rsp != 0) {
    LOG_ERR("write logical block %d offset_lba %d w_size %d\n", lba_index,
      offset_in_lba, requested_write_size);
    return -ENOSYS;
  }
  sd_spi_set_cs(0);
  sd_spi_write(SD_TOKEN_FLOATING_BUS);
  sd_spi_write(SD_TOKEN_START);
  for (int i = 0; i < offset_in_lba; i++)
    sd_spi_write(0x00);
  for (int i = offset_in_lba; i < requested_write_size; i++) {
    sd_spi_write(buffer[i - offset_in_lba]);
  }
  offset_in_lba += requested_write_size;
  for (int i = offset_in_lba; i < SD_LOGICAL_BLOCK_SIZE; i++) {
    sd_spi_write(0x00);
  }
  sd_spi_write(0x00);
  sd_spi_write(0x00);
  for (int bc = 65000; sd_spi_write(0xFF) != 0xFF && bc; bc--);
  sd_spi_set_cs(1);
  return 0;
}
static int sd_spi_write_logical_block ( spi_master_dev_t * spi , const uint8_t * buffer , uint32_t lba_index , uint8_t offset_in_lba , size_t requested_write_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_write_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 ( SD_CMD_SET_WRITE_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 , offset_in_lba , requested_write_size ) ; return - ENOSYS ; } sd_spi_set_cs ( 0 ) ; sd_spi_write ( SD_TOKEN_FLOATING_BUS ) ; sd_spi_write ( SD_TOKEN_START ) ; for ( int i = 0 ; i < offset_in_lba ; i ++ ) sd_spi_write ( 0x00 ) ; for ( int i = offset_in_lba ; i < requested_write_size ; i ++ ) { sd_spi_write ( buffer [ i - offset_in_lba ] ) ; } offset_in_lba += requested_write_size ; for ( int i = offset_in_lba ; i < SD_LOGICAL_BLOCK_SIZE ; i ++ ) { sd_spi_write ( 0x00 ) ; } sd_spi_write ( 0x00 ) ; sd_spi_write ( 0x00 ) ; for ( int bc = 65000 ; sd_spi_write ( 0xFF ) != 0xFF && bc ; bc -- ) ; sd_spi_set_cs ( 1 ) ; return 0 ; }
sd_spi_write_logical_block - write a logical block to 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_write_size - the number of bytes that we want to write
sd_spi_write_logical_block - write a logical block to 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_write_size - the number of bytes that we want to write
/* Some mandatory checks */ /* Pad data with 0 */ /* Write dummy CRC */