COSA
An Object-Oriented Platform for Arduino Programming
SD Class Reference

#include <SD.hh>

Inheritance diagram for SD:
Inheritance graph
Collaboration diagram for SD:
Collaboration graph

Classes

struct  cid_t
 
union  csd_t
 
struct  csd_v1_t
 
struct  csd_v2_t
 
union  R1
 
union  R2
 
struct  R3
 
union  R6
 
union  R7
 
struct  request_t
 

Public Types

enum  CARD { TYPE_UNKNOWN = 0, TYPE_SD1 = 1, TYPE_SD2 = 2, TYPE_SDHC = 3 }
 

Public Member Functions

 SD (Board::DigitalPin csn=Board::D8)
 
CARD type () const
 
bool begin (SPI::Clock rate=SPI::DIV128_CLOCK)
 
bool end ()
 
bool erase (uint32_t start, uint32_t end)
 
bool read (uint32_t block, uint8_t *dst)
 
bool read (cid_t *cid)
 
bool read (csd_t *csd)
 
bool write (uint32_t block, const uint8_t *src)
 

Static Public Attributes

static const size_t BLOCK_MAX = 512
 

Protected Types

enum  CMD {
  GO_IDLE_STATE = 0, ALL_SEND_CID = 2, SEND_RELATIVE_ADDR = 3, SET_DSR = 4,
  SWITCH_FUNC = 6, SELECT_DESELECT_CARD = 7, SEND_IF_COND = 8, SEND_CSD = 9,
  SEND_CID = 10, VOLTAGE_SWITCH = 11, STOP_TRANSMISSION = 12, SEND_STATUS = 13,
  GO_INACTIVE_STATE = 15, SET_BLOCKLEN = 16, READ_SINGLE_BLOCK = 17, READ_MULTIPLE_BLOCK = 18,
  SEND_TUNING_BLOCK = 19, SPEED_CLASS_CONTROL = 20, SET_BLOCK_COUNT = 23, WRITE_BLOCK = 24,
  WRITE_MULTIPLE_BLOCK = 25, PROGRAM_CSD = 27, SET_WRITE_PROT = 28, CLR_WRITE_PROT = 29,
  SEND_WRITE_PROT = 30, ERASE_WR_BLK_START = 32, ERASE_WR_BLK_END = 33, ERASE = 38,
  LOCK_UNLOCK = 40, APP_CMD = 55, GEN_CMD = 56, READ_OCR = 58,
  CRC_ON_OFF = 59
}
 
enum  ACMD {
  SET_BUS_WIDTH = 6, SD_STATUS = 13, SEND_NUM_WR_BLOCKS = 22, SET_WR_BLK_ERASE_COUNT = 23,
  SD_SEND_OP_COND = 41, SET_CLR_CARD_DETECT = 42, SEND_SCR = 51
}
 
enum  STATE {
  IDLE_STATE = 0, READY_STATE = 1, IDENT_STATE = 2, STBY_STATE = 3,
  TRAN_STATE = 4, DATA_STATE = 5, RCV_STATE = 6, PRG_STATE = 7,
  DIS_STATE = 8
}
 
enum  TOKEN {
  DATA_START_BLOCK = 0XFE, WRITE_MULTIPLE_TOKEN = 0XFC, STOP_TRAN_TOKEN = 0XFD, DATA_RES_MASK = 0X1F,
  DATA_RES_ACCEPTED = 0X05, DATA_RES_CRC_ERR = 0X0B, DATA_RES_WR_ERR = 0X0E
}
 

Protected Member Functions

uint8_t send (CMD command, uint32_t arg=0L)
 
bool send (uint16_t ms, CMD command, uint32_t arg=0L)
 
uint8_t send (ACMD command, uint32_t arg=0L)
 
bool send (uint16_t ms, ACMD command, uint32_t arg=0L)
 
bool await (uint16_t ms=0, uint8_t token=0)
 
uint32_t receive ()
 
bool read (CMD command, uint32_t arg, void *buf, size_t count)
 

Protected Attributes

uint8_t m_response
 
CARD m_type
 

Static Protected Attributes

static const uint8_t CHECK_PATTERN = 0xAA
 
static const uint16_t INIT_TIMEOUT = 2000
 
static const uint16_t ERASE_TIMEOUT = 10000
 
static const uint16_t READ_TIMEOUT = 300
 
static const uint16_t WRITE_TIMEOUT = 600
 
static const uint8_t INIT_PULSES = 10
 
static const uint8_t INIT_RETRY = 200
 
static const uint8_t RESPONSE_RETRY = 100
 

Private Member Functions

void set_clock (Clock rate)
 
void set_clock (uint32_t freq)
 

Static Private Member Functions

static Clock clock (uint32_t freq)
 
static Clock cycle (uint16_t ns)
 

Private Attributes

Driverm_next
 List of drivers. More...
 
Interrupt::Handlerm_irq
 Interrupt handler. More...
 
OutputPin m_cs
 Device chip select pin. More...
 
Pulse m_pulse
 Chip select pulse width. More...
 
uint8_t m_spcr
 SPI/SPCR hardware control register setting. More...
 
uint8_t m_spsr
 SPI/SPSR hardware status register. More...
 

Detailed Description

Cosa SD low-level device driver class. Implements disk driver connect/disconnect, erase, read and write block.

References

  1. SD Specification, Part 1: Physical Layer, Simplified Specification, Version 4.10, January 22, 2013. https://www.sdcard.org/downloads/pls/simplified_specs/part1_410.pdf

Definition at line 35 of file SD.hh.

Member Enumeration Documentation

enum SD::ACMD
protected

Table 4-28: Application-Specific Commands.

Enumerator
SET_BUS_WIDTH 

Defines the data bus width.

SD_STATUS 

Card status.

SEND_NUM_WR_BLOCKS 

Number of write blocks.

SET_WR_BLK_ERASE_COUNT 

Number of pre-erased write blocks.

SD_SEND_OP_COND 

Host capacity support information.

SET_CLR_CARD_DETECT 

Enable pull-up resistor on CD/DAT3 (pin 1).

SEND_SCR 

Reads the Configuration Register.

Definition at line 188 of file SD.hh.

enum SD::CARD

Supported card types.

Enumerator
TYPE_UNKNOWN 
TYPE_SD1 
TYPE_SD2 
TYPE_SDHC 

Definition at line 41 of file SD.hh.

enum SD::CMD
protected

Command Abbreviations.

Enumerator
GO_IDLE_STATE 

Reset the SD Memory Card.

ALL_SEND_CID 

Sends host capacity support information.

SEND_RELATIVE_ADDR 

Checks switchable function.

SET_DSR 

Programs the DSR of all cards.

SWITCH_FUNC 

Checks switchable function.

SELECT_DESELECT_CARD 

Toggles card state (stdby/prog and disc).

SEND_IF_COND 

Sends SD Memory Card interface condition.

SEND_CSD 

Asks the selected card to send CSD.

SEND_CID 

Asks the selected card to send CID.

VOLTAGE_SWITCH 

Switch to 1V8 bus signaling level.

STOP_TRANSMISSION 

Stop Multiple Block Read.

SEND_STATUS 

Asks the selected card status register.

GO_INACTIVE_STATE 

Addressed card into inactive state.

SET_BLOCKLEN 

Set block length (in bytes).

READ_SINGLE_BLOCK 

Read block length bytes.

READ_MULTIPLE_BLOCK 

Read blocks until STOP_TRANSMISSION.

SEND_TUNING_BLOCK 

Send 64 byte tuning pattern to card.

SPEED_CLASS_CONTROL 

Check speed class.

SET_BLOCK_COUNT 

Specify block count for multiple block.

WRITE_BLOCK 

Write block length bytes.

WRITE_MULTIPLE_BLOCK 

Write block until STOP_TRANSMISSION.

PROGRAM_CSD 

Set programmable bits in CSD.

SET_WRITE_PROT 

Set write protect bit.

CLR_WRITE_PROT 

Clears write protect bit.

SEND_WRITE_PROT 

Read write protect bit.

ERASE_WR_BLK_START 

Set first write block to be erased.

ERASE_WR_BLK_END 

Set last write block to be erased.

ERASE 

Erases selected write blocks.

LOCK_UNLOCK 

Set/reset password or unlock/lock card.

APP_CMD 

Next command is application specific command.

GEN_CMD 

Data block for application specific command.

READ_OCR 

Read OCR register of a card.

CRC_ON_OFF 

Turns the CRC on/off.

Definition at line 151 of file SD.hh.

enum SD::STATE
protected

Card States.

Enumerator
IDLE_STATE 
READY_STATE 
IDENT_STATE 
STBY_STATE 
TRAN_STATE 
DATA_STATE 
RCV_STATE 
PRG_STATE 
DIS_STATE 

Definition at line 199 of file SD.hh.

enum SD::TOKEN
protected
Enumerator
DATA_START_BLOCK 
WRITE_MULTIPLE_TOKEN 
STOP_TRAN_TOKEN 
DATA_RES_MASK 
DATA_RES_ACCEPTED 
DATA_RES_CRC_ERR 
DATA_RES_WR_ERR 

Definition at line 280 of file SD.hh.

Constructor & Destructor Documentation

SD::SD ( Board::DigitalPin  csn = Board::D8)
inline

Construct Secure Disk low-level SPI device driver with given chip select pin.

Parameters
[in]csnchip select pin.

Definition at line 399 of file SD.hh.

Member Function Documentation

bool SD::await ( uint16_t  ms = 0,
uint8_t  token = 0 
)
protected

Await the completion of a command. Wait for at most given period in milli-seconds (default zero will block). Return true if command was completed otherwise false if the time limit was exceeded.

Parameters
[in]mstimeout period in number of milli-seconds (default 0).
[in]tokento await (default any).
Returns
bool.

Definition at line 150 of file SD.cpp.

bool SD::begin ( SPI::Clock  rate = SPI::DIV128_CLOCK)

Initiate communication with memory card. Returns true if successful and a memory card was detected, otherwise false.

Parameters
[in]rateclock for device.
Returns
bool.

Definition at line 226 of file SD.cpp.

bool SD::end ( )

Stop communication with memory card. Flush internal state and allow card to be removed. Returns true if successful otherwise false.

Returns
bool.

Definition at line 283 of file SD.cpp.

bool SD::erase ( uint32_t  start,
uint32_t  end 
)

Erase given block interval from start to end. Returns true if successful otherwise false.

Parameters
[in]startblock address.
[in]endblock address.
Returns
bool.

Definition at line 289 of file SD.cpp.

bool SD::read ( CMD  command,
uint32_t  arg,
void *  buf,
size_t  count 
)
protected

Send given command and argument and transfer data response into given buffer with given number of bytes. Returns true if successful otherwise false.

Parameters
[in]commandto send.
[in]argargument.
[in]bufpointer to buffer for response data.
[in]countnumber of bytes.
Returns
bool.

Definition at line 183 of file SD.cpp.

bool SD::read ( uint32_t  block,
uint8_t *  dst 
)
inline

Read given block into given destination buffer. The buffer must be able to hold BLOCK_MAX bytes. Returns true if successful otherwise false.

Parameters
[in]blockaddress.
[in]dstpointer to destination buffer.
Returns
bool.

Definition at line 446 of file SD.hh.

bool SD::read ( cid_t cid)
inline

Read card CID register. The CID contains card identification information such as Manufacturer ID, Product name, Product serial number and Manufacturing date.

Parameters
[out]cidpointer to cid data store.
Returns
true for successful otherwise false.

Definition at line 460 of file SD.hh.

bool SD::read ( csd_t csd)
inline

Read card CSD register. The CSD contains that provides information regarding access to the card's contents.

Parameters
[out]csdpointer to csd data store.
Returns
true for successful otherwise false.

Definition at line 472 of file SD.hh.

uint32_t SD::receive ( )
protected

Receive 32-bit response from device.

Returns
long reponse.

Definition at line 164 of file SD.cpp.

uint8_t SD::send ( CMD  command,
uint32_t  arg = 0L 
)
protected

Send given command and argument. Returns R1 response byte.

Parameters
[in]commandto send.
[in]argargument (default 0L).
Returns
response byte (R1).

Definition at line 96 of file SD.cpp.

bool SD::send ( uint16_t  ms,
CMD  command,
uint32_t  arg = 0L 
)
protected

Send given command and argument. Resend if response any error state. Return true if command was issued successfully otherwise false.

Parameters
[in]msnumber of milli-seconds to resend.
[in]commandto send.
[in]argargument (default 0L).
Returns
bool.

Definition at line 120 of file SD.cpp.

uint8_t SD::send ( ACMD  command,
uint32_t  arg = 0L 
)
protected

Send given application specific command and argument. Returns R1 response byte.

Parameters
[in]commandto send.
[in]argargument (default 0L).
Returns
response byte (R1).

Definition at line 131 of file SD.cpp.

bool SD::send ( uint16_t  ms,
ACMD  command,
uint32_t  arg = 0L 
)
protected

Send given application specific command and argument. Resend if response any error state. Return true if command was issued successfully otherwise false.

Parameters
[in]msnumber of milli-seconds to resend.
[in]commandto send.
[in]argargument (default 0L).
Returns
bool.

Definition at line 138 of file SD.cpp.

CARD SD::type ( ) const
inline

Return detected card type.

Definition at line 408 of file SD.hh.

bool SD::write ( uint32_t  block,
const uint8_t *  src 
)

Write given source buffer with BLOCK_MAX bytes to the given block. Returns true if successful otherwise false.

Parameters
[in]blockaddress.
[in]srcpointer to source buffer.
Returns
bool.

Definition at line 314 of file SD.cpp.

Member Data Documentation

const size_t SD::BLOCK_MAX = 512
static

Max size of block.

Definition at line 38 of file SD.hh.

const uint8_t SD::CHECK_PATTERN = 0xAA
staticprotected

SD initialization check pattern.

Definition at line 278 of file SD.hh.

const uint16_t SD::ERASE_TIMEOUT = 10000
staticprotected

Definition at line 299 of file SD.hh.

const uint8_t SD::INIT_PULSES = 10
staticprotected

Internal number of init pulses.

Definition at line 304 of file SD.hh.

const uint8_t SD::INIT_RETRY = 200
staticprotected

Internal number of retry.

Definition at line 307 of file SD.hh.

const uint16_t SD::INIT_TIMEOUT = 2000
staticprotected

Internal timeout periods.

Definition at line 298 of file SD.hh.

uint8_t SD::m_response
protected

Response from latest command.

Definition at line 311 of file SD.hh.

CARD SD::m_type
protected

Detected card type.

Definition at line 314 of file SD.hh.

const uint16_t SD::READ_TIMEOUT = 300
staticprotected

Definition at line 300 of file SD.hh.

const uint8_t SD::RESPONSE_RETRY = 100
staticprotected

Definition at line 308 of file SD.hh.

const uint16_t SD::WRITE_TIMEOUT = 600
staticprotected

Definition at line 301 of file SD.hh.


The documentation for this class was generated from the following files: