Functions

sdtransfer.c File Reference

SD card data transfer functions. More...

#include <util/crc16.h>
#include "spi.h"
#include "sdcmd.h"

Functions

uint8_t sd_recv_data (uint8_t *data, sd_blen_t blocklen)
 read data from SD card
void sd_send_data (const uint8_t *data, sd_blen_t blocklen, uint8_t token)
 write data to SD card

Detailed Description

SD card data transfer functions.


Function Documentation

uint8_t sd_recv_data ( uint8_t *  data,
sd_blen_t  blocklen 
)

read data from SD card

Parameters:
data pointer to buffer for data
blocklen number of bytes to receive

{
  uint16_t crc = 0x0000;
  uint16_t crc_read;
  sd_blen_t i;
  uint8_t t;

  do {
    t = spi_transfer (0xff, 0, 1);
  } while (t == 0xff); /* wait for start block token */

  for (i=blocklen; i; i--) {
    uint8_t b = spi_transfer (0xff, 0, 1);
    *data++ = b;
    crc = _crc_xmodem_update (crc, b);
  }
  crc_read = spi_transfer (0xff, 0, 1); crc_read <<= 8;
  crc_read |= spi_transfer (0xff, 0, 1);

  return crc_read == crc;
}

void sd_send_data ( const uint8_t *  data,
sd_blen_t  blocklen,
uint8_t  token 
)

write data to SD card

Parameters:
data pointer to buffer with data
blocklen number of bytes to send
token token value to start transmission

{
  uint16_t crc = 0x0000;
  sd_blen_t i;

  spi_write (token);
  for (i = blocklen; i; i--) {
    uint8_t b = *data++;
    spi_write (b);
    crc = _crc_xmodem_update (crc, b);
  }
  if (data) {
    /* only send CRC when we actually did send some data */
    spi_write (crc >> 8);
    spi_write (crc & 0xff);
  }
}