dnx RTOS 2.2.0 "Eagle"
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
SDIO Driver

Detailed Description


Driver handles SD Cards in native mode (SDIO).

Supported architectures


Meaning of major and minor numbers

There is special meaning of major and minor numbers. The major number selects SD card. There is possibility to use up to 256 SD Cards. The minor number selects volume and partitions as follow:

The stm32f4 port supports only 1 SD card.

Numeration restrictions

Major number can be set in range 0 to 255. The minor number can be used in range from 0 to 4. Each new major number is a new instance of driver that can handle SD Card.

Driver initialization

To initialize driver the following code can be used:

driver_init("SDIO", 0, 0, "/dev/sda"); // entire volume
driver_init("SDIO", 0, 1, "/dev/sda1"); // partition 1
driver_init("SDIO", 0, 2, "/dev/sda2"); // partition 2
driver_init("SDIO", 0, 3, "/dev/sda3"); // partition 3
driver_init("SDIO", 0, 4, "/dev/sda4"); // partition 4

Card 2:

driver_init("SDIO", 1, 0, "/dev/sdb"); // entire volume
driver_init("SDIO", 1, 1, "/dev/sdb1"); // partition 1
driver_init("SDIO", 1, 2, "/dev/sdb2"); // partition 2
driver_init("SDIO", 1, 3, "/dev/sdb3"); // partition 3
driver_init("SDIO", 1, 4, "/dev/sdb4"); // partition 4

If card does not contains any partitions then SDSPIx:0 only can be used as file system data source. If card has e.g. only 1 partition (MBR exist) then only one entry may be created. Created device files can be used directly by file systems.

Driver release

To release driver the following code can be used:

driver_release("SDIO", 0, 4); // disable supporting of card 0 partition 4
driver_release("SDIO", 1, 1); // disable supporting of card 1 partition 1

Driver configuration

Driver configuration can be done only by using configuration files (by Configtool). Configuration is static because there is no necessary to change driver configuration during runtime.

Data write

Writing data to device is the same as writing data to regular file with some restrictions: the seek position should be aligned to sector size (512 bytes) and buffer size should be multiple of 512 bytes.

Data read

Reading data from device is the same as reading data from regular file with some restrictions: the seek position should be aligned to sector size (512 bytes) and buffer size should be multiple of 512 bytes.

Card initialization

There is special ioctl() request (IOCTL_SDIO__INITIALIZE_CARD or IOCTL_STORAGE__INITIALIZE) that initialize selected SD card. Initialization can be done on any partition (e.g. sda1) giving the same effect as initialization on master card file (e.g. sda, sdb). If initialization is already done on selected partition then is not necessary to initialize remained partitions.

After initialization the MBR table should be read by driver. In this case the ioctl() request (IOCTL_SDIO__READ_MBR or IOCTL_STORAGE__READ_MBR) should be used. This procedure load all MBR entries and set required offsets in partition files (/dev/sdxy).

Card initialization example code.

#include <stdio.h>
#include <sys/ioctl.h>
// creating SD card nodes
driver_init("SDIO", 0, 0, "/dev/sda");
driver_init("SDIO", 0, 1, "/dev/sda1");
// SD Card initialization
FILE *f = fopen("/dev/sda", "r+");
if (f) {
if (ioctl(fileno(f), IOCTL_STORAGE__INITIALIZE) != 0) {
puts("SD initialization error");
} else {
if (ioctl(fileno(f), IOCTL_STORAGE__READ_MBR) != 0) {
puts("SD read MBR error");
// file system mount
mkdir("/mnt", 0777);
mount("fatfs", "/dev/sda1", "/mnt", "");
// ...


 Initialize SD card (OS storage request). More...
 Read card's MBR sector and detect partitions (OS storage request). More...

Macro Definition Documentation

On success (card initialized) 0 is returned. On error (card not initialized) -1 is returned and errno is set.
On success (MBR detected) 0 is returned. On error -1 (MBR not exist or IO error) is returned and errno is set.