nRF5 SDK v13.0.0
SD card library

The SD card library provides the functionality to communicate with a Secure Digital memory card (SDC) using the SPI bus.

Key features include:

Configuration

The SD card library uses one SPI instance which can be selected in the SDK configuration file sdk_config.h by changing the value of APP_SDCARD_SPI_INSTANCE. You can adjust the SPI interface speed by setting the value of APP_SDCARD_FREQ_INIT (frequency during initialization) and APP_SDCARD_FREQ_DATA (frequency during data transfer). The frequency during an initialization process should not exceed 250kHz. Pin definitions are provided during runtime.

Initialization and operation

All of the SD card operations (except uninitialization) are performed asynchronously. The library uses Protothreads for implementation of the coroutines. If the operation request function returns NRF_SUCCESS, then the operation result will be passed to the event handler function after completion. Event handler will not be called if the operation was not started successfully. Note that operation request functions return generic error codes (ret_code_t) and the operation result passed to the handler is of type sdc_result_t.

To initialize the card, call app_sdc_init providing a pointer to the configuration structure of type app_sdc_config_t and an event handler. If NRF_SUCCESS is returned, wait for the event handler to get the initialization result. Otherwise, initialization has failed. The read/write operations are executed in the same manner.

Usage

The example code below shows the basic usage of the SD card library.

void sdc_handler(sdc_evt_t const * p_event)
{
switch(p_event->type)
{
if (p_event->result != SDC_SUCCESS)
{
/* Initialization failed. */
}
else
{
/* Initialization successful. */
}
if (p_event->result != SDC_SUCCESS)
{
/* Read error. */
}
else
{
/* Read successful. */
}
/* ... */
default: break;
}
return;
}
/* ... */
uint8_t buffer[SDC_SECTOR_SIZE];
static const app_sdc_config_t sdc_cfg = {
.mosi_pin = SDC_MOSI_PIN,
.miso_pin = SDC_MISO_PIN,
.sck_pin = SDC_SCK_PIN,
.cs_pin = SDC_CS_PIN
};
ret_code_t err_code = app_sdc_init(&sdc_cfg, sdc_handler);
if (err_code != NRF_SUCCESS)
{
/* Initialization failed. */
}
{
wait();
}
/* Read the first block from the card. */
err_code = app_sdc_block_read(buffer, 0, 1);
if (err_code != NRF_SUCCESS)
{
/* Read request error. */
}

Documentation feedback | Developer Zone | Subscribe | Updated