flash_sdcard.c File Reference

library to communicate with an SD card flash memory using the SPI mode (code) More...

Go to the source code of this file.

## Macros

#define | FLASH_SDCARD_SPI 1
| SPI peripheral.

## Functions

static void | flash_sdcard_spi_wait (void)
| wait one SPI round (one SPI word)
static uint16_t | flash_sdcard_spi_read (void)
| read one SPI word More...
static bool | flash_sdcard_card_detect (void)
| test if card is present More...
static void | flash_sdcard_send_command (uint8_t index, uint32_t argument)
| transmit command token More...
static uint8_t | flash_sdcard_command_response (uint8_t index, uint32_t argument, uint8_t *response, size_t size)
| transmit command token and receive response token More...
static uint8_t | flash_sdcard_read_block (uint8_t *data, size_t size)
| read a data block More...
static uint8_t | flash_sdcard_write_block (uint8_t *data, size_t size)
| write a data block More...
static uint16_t | flash_sdcard_status (uint8_t *status)
| get card status More...
static uint8_t | flash_sdcard_data_read (uint8_t index, uint32_t argument, uint8_t *data, size_t size)
| transmit command token, receive response token and data block More...
static uint8_t | flash_sdcard_data_write (uint8_t index, uint32_t argument, uint8_t *data, size_t size)
| transmit command token, receive response token and write data block More...
bool | flash_sdcard_setup (void)
| setup communication with SD card More...
uint64_t | flash_sdcard_size (void)
| get size of SD card flash memory More...
uint32_t | flash_sdcard_erase_size (void)
| get size of a erase block More...
bool | flash_sdcard_read_data (uint32_t block, uint8_t *data)
| read data on flash of SD card More...
bool | flash_sdcard_write_data (uint32_t block, uint8_t *data)
| write data on flash of SD card More...

## Variables

static bool | initialized = false
| if the card has been initialized successfully
static uint32_t | n_ac = 8
| maximum N_AC value (in 8-clock cycles) (time between the response token R1 and data block when reading data (see section 7.5.4) More...
static bool | sdsc = false
| is it a Standard Capacity SD card (true), or High Capacity SD cards (false) More...
static uint64_t | sdcard_size = 0
| size of card in bytes
static uint32_t | erase_size = 0
| size of an erase block bytes
static const uint8_t | crc7_table []
| table for CRC-7 calculation for the command messages (see section 4.5) More...

## Detailed Description

library to communicate with an SD card flash memory using the SPI mode (code)

Author
  King Kévin kingk.nosp@m.evin.nosp@m.@cuvo.nosp@m.odoo.nosp@m..info

Date
  2017

Note
  peripherals used: SPI SPI used to communication with SD card

Warning
  all calls are blocking Specifications, Part 1, Physical Layer, Simplified Specification, Version 6.00, 10 April 10 2017

Todo:
  use SPI unidirectional mode, use DMA, force/wait going to idle state when initializing, filter out reserved values, check sector against size

Definition in file flash_sdcard.c.

## Function Documentation

## flash_sdcard_card_detect()

static bool flash_sdcard_card_detect | ( | void |  | ) |

static

test if card is present

Returns
  if card has been detected

Note
  this use the SD card detection mechanism (CD/CS is high card is inserted due to the internal 50 kOhm resistor)

Definition at line 106 of file flash_sdcard.c.

## flash_sdcard_command_response()

static uint8_t flash_sdcard_command_response | ( | uint8_t | index,
|  | uint32_t | argument,
|  | uint8_t * | response,
|  | size_t | size
| ) |  |

static

transmit command token and receive response token

Parameters

  [in] | index | command index
  [in] | argument | command argument
  [out] | response | response data to read (if no error occurred)
  [in] | size | size of response to read

Returns
  response token R1 or 0xff if error occurred or card is not present

Definition at line 137 of file flash_sdcard.c.

## flash_sdcard_data_read()

static uint8_t flash_sdcard_data_read | ( | uint8_t | index,
|  | uint32_t | argument,
|  | uint8_t * | data,
|  | size_t | size
| ) |  |

static

transmit command token, receive response token and data block

Parameters

  [in] | index | command index
  [in] | argument | command argument
  [out] | data | data block to read (if no error occurred)
  [in] | size | size of data to read (a multiple of 2)

Returns
  response token R1 or 0xff if error occurred or card is not present

Definition at line 309 of file flash_sdcard.c.

## flash_sdcard_data_write()

static uint8_t flash_sdcard_data_write | ( | uint8_t | index,
|  | uint32_t | argument,
|  | uint8_t * | data,
|  | size_t | size
| ) |  |

static

transmit command token, receive response token and write data block

Parameters

  [in] | index | command index
  [in] | argument | command argument
  [out] | data | data block to write
  [in] | size | size of data to write (a multiple of 2)

Returns
  data response token, or 0xff if error occurred or card is not present

Note
  at the end of a write operation the SD status should be check to ensure no error occurred during programming

Definition at line 349 of file flash_sdcard.c.

## flash_sdcard_erase_size()

uint32_t flash_sdcard_erase_size | ( | void |  | ) |

get size of a erase block

Returns
  size of a erase block (in bytes)

Definition at line 562 of file flash_sdcard.c.

## flash_sdcard_read_block()

static uint8_t flash_sdcard_read_block | ( | uint8_t * | data,
|  | size_t | size
| ) |  |

static

read a data block

Parameters

  [out] | data | data block to read (if no error occurred)
  [in] | size | size of response to read (a multiple of 2)

Returns
  0 if succeeded, else control token (0xff for other errors)

Definition at line 168 of file flash_sdcard.c.

## flash_sdcard_read_data()

bool flash_sdcard_read_data | ( | uint32_t | block,
|  | uint8_t * | data
| ) |  |

read data on flash of SD card

Parameters

  [in] | block | address of data to read (in block in 512 bytes unit)
  [out] | data | data block to read (with a size of 512 bytes)

Returns
  if read succeeded

Definition at line 567 of file flash_sdcard.c.

## flash_sdcard_send_command()

static void flash_sdcard_send_command | ( | uint8_t | index,
|  | uint32_t | argument
| ) |  |

static

transmit command token

Parameters

  [in] | index | command index
  [in] | argument | command argument

Definition at line 118 of file flash_sdcard.c.

## flash_sdcard_setup()

bool flash_sdcard_setup | ( | void |  | ) |

setup communication with SD card

Returns
  if card has been initialized correctly

Definition at line 381 of file flash_sdcard.c.

## flash_sdcard_size()

uint64_t flash_sdcard_size | ( | void |  | ) |

get size of SD card flash memory

Returns
  size of SD card flash memory (in bytes)

Definition at line 557 of file flash_sdcard.c.

## flash_sdcard_spi_read()

static uint16_t flash_sdcard_spi_read | ( | void |  | ) |

static

read one SPI word

Returns
  SPI word read

Definition at line 93 of file flash_sdcard.c.

## flash_sdcard_status()

static uint16_t flash_sdcard_status | ( | uint8_t * | status | ) |

static

get card status

Parameters

  [out] | status | SD status (512 bits)

Returns
  response token R2 or 0xffff if error occurred or card is not present

Definition at line 265 of file flash_sdcard.c.

## flash_sdcard_write_block()

static uint8_t flash_sdcard_write_block | ( | uint8_t * | data,
|  | size_t | size
| ) |  |

static

write a data block

Parameters

  [in] | data | data block to write
  [in] | size | size of response to read (a multiple of 2)

Returns
  data response token (0xff for other errors)

Definition at line 222 of file flash_sdcard.c.

## flash_sdcard_write_data()

bool flash_sdcard_write_data | ( | uint32_t | block,
|  | uint8_t * | data
| ) |  |

write data on flash of SD card

Parameters

  [in] | block | address of data to write (in block in 512 bytes unit)
  [in] | data | data block to write (with a size of 512 bytes)

Returns
  if write succeeded

Definition at line 582 of file flash_sdcard.c.

## Variable Documentation

## crc7_table

const uint8_t crc7_table[]

static

Initial value:
= {
0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77,
0x19, 0x10, 0x0b, 0x02, 0x3d, 0x34, 0x2f, 0x26, 0x51, 0x58, 0x43, 0x4a, 0x75, 0x7c, 0x67, 0x6e,
0x32, 0x3b, 0x20, 0x29, 0x16, 0x1f, 0x04, 0x0d, 0x7a, 0x73, 0x68, 0x61, 0x5e, 0x57, 0x4c, 0x45,
0x2b, 0x22, 0x39, 0x30, 0x0f, 0x06, 0x1d, 0x14, 0x63, 0x6a, 0x71, 0x78, 0x47, 0x4e, 0x55, 0x5c,
0x64, 0x6d, 0x76, 0x7f, 0x40, 0x49, 0x52, 0x5b, 0x2c, 0x25, 0x3e, 0x37, 0x08, 0x01, 0x1a, 0x13,
0x7d, 0x74, 0x6f, 0x66, 0x59, 0x50, 0x4b, 0x42, 0x35, 0x3c, 0x27, 0x2e, 0x11, 0x18, 0x03, 0x0a,
0x56, 0x5f, 0x44, 0x4d, 0x72, 0x7b, 0x60, 0x69, 0x1e, 0x17, 0x0c, 0x05, 0x3a, 0x33, 0x28, 0x21,
0x4f, 0x46, 0x5d, 0x54, 0x6b, 0x62, 0x79, 0x70, 0x07, 0x0e, 0x15, 0x1c, 0x23, 0x2a, 0x31, 0x38,
0x41, 0x48, 0x53, 0x5a, 0x65, 0x6c, 0x77, 0x7e, 0x09, 0x00, 0x1b, 0x12, 0x2d, 0x24, 0x3f, 0x36,
0x58, 0x51, 0x4a, 0x43, 0x7c, 0x75, 0x6e, 0x67, 0x10, 0x19, 0x02, 0x0b, 0x34, 0x3d, 0x26, 0x2f,
0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c, 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04,
0x6a, 0x63, 0x78, 0x71, 0x4e, 0x47, 0x5c, 0x55, 0x22, 0x2b, 0x30, 0x39, 0x06, 0x0f, 0x14, 0x1d,
0x25, 0x2c, 0x37, 0x3e, 0x01, 0x08, 0x13, 0x1a, 0x6d, 0x64, 0x7f, 0x76, 0x49, 0x40, 0x5b, 0x52,
0x3c, 0x35, 0x2e, 0x27, 0x18, 0x11, 0x0a, 0x03, 0x74, 0x7d, 0x66, 0x6f, 0x50, 0x59, 0x42, 0x4b,
0x17, 0x1e, 0x05, 0x0c, 0x33, 0x3a, 0x21, 0x28, 0x5f, 0x56, 0x4d, 0x44, 0x7b, 0x72, 0x69, 0x60,
0x0e, 0x07, 0x1c, 0x15, 0x2a, 0x23, 0x38, 0x31, 0x46, 0x4f, 0x54, 0x5d, 0x62, 0x6b, 0x70, 0x79
}

table for CRC-7 calculation for the command messages (see section 4.5)

Note
  faster than calculating the CRC and doesn't cost a lot of space
  generated using pycrc –width=7 –poly=0x09 –reflect-in=false –reflect-out=false –xor-in=0x00 –xor-out=0x00 –generate=table

Definition at line 64 of file flash_sdcard.c.

## n_ac

uint32_t n_ac = 8

static

maximum N_AC value (in 8-clock cycles) (time between the response token R1 and data block when reading data (see section 7.5.4)

Note
  this is set to N_CR until we can read CSD (see section 7.2.6)

Definition at line 49 of file flash_sdcard.c.

## sdsc

bool sdsc = false

static

is it a Standard Capacity SD card (true), or High Capacity SD cards (false)

Note
  this is indicated in the Card Capacity Status bit or OCR (set for high capacity)
  this is important for addressing: for standard capacity cards the address is the byte number, for high capacity cards it is the 512-byte block number

Definition at line 54 of file flash_sdcard.c.