GPS Device
Loading...
Searching...
No Matches
sd.h File Reference

SD card driver public API. More...

#include "err.h"
#include "stm32f401re.h"
#include "stm32f401re_gpio.h"
#include "configuration.h"
Include dependency graph for sd.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  SD_CFG_ts
 Configuration structure for initializing an SD card handle. More...

Typedefs

typedef struct sd_handle_s SD_HANDLE_ts
 Opaque handle representing an SD card instance.

Functions

ERR_te sd_init_subsys (void)
 Initializes the SD card subsystem.
ERR_te sd_deinit_subsys (void)
 Deinitializes the SD card subsystem.
ERR_te sd_start_subsys (void)
 Starts the SD card subsystem.
ERR_te sd_stop_subsys (void)
 Stops the SD card subsystem.
ERR_te sd_init_handle (SD_CFG_ts *sd_cfg, SD_HANDLE_ts **sd_handle_o)
 Initializes an SD card handle and performs the full SPI-mode power-up sequence.
ERR_te sd_deinit_handle (SD_HANDLE_ts *sd_handle)
 Deinitializes an SD card handle and resets the SPI peripheral.
ERR_te sd_get_handle_init (SD_HANDLE_ts *sd_handle, bool *handle_init_o)
 Retrieves the initialization state of an SD card handle.
ERR_te sd_get_sector_count (SD_HANDLE_ts *sd_handle, uint32_t *sector_count_o)
 Retrieves the total sector (block) count of the SD card.
ERR_te sd_get_sector_size (SD_HANDLE_ts *sd_handle, uint32_t *sector_size_o)
 Retrieves the sector (block) size of the SD card in bytes.
ERR_te sd_read (SD_HANDLE_ts *sd_handle, uint8_t *read_buf, uint32_t start_sector, uint32_t num_sectors)
 Reads one or more sectors from the SD card.
ERR_te sd_write (SD_HANDLE_ts *sd_handle, uint8_t *write_buf, uint32_t start_sector, uint32_t num_sectors)
 Writes one or more sectors to the SD card.
ERR_te sd_ioctl (SD_HANDLE_ts *sd_handle)
 Executes an IOCTL control command on the SD card handle.

Detailed Description

SD card driver public API.

Author
github.com/Baksi675

This module provides an SPI-based SD card driver supporting SDSC and SDHC cards in SPI mode. It handles card initialization, type detection, CSD register parsing, and sector-level read/write operations. The driver is designed to be used as the low-level backend for a FatFs diskio layer.

Card initialization in sd_init_handle performs the full SPI-mode power-up sequence:

  1. Sends at least 74 dummy clock cycles with CS high
  2. Issues CMD0 to enter idle state
  3. Issues CMD8 to detect SD Ver.2 vs Ver.1 / MMC
  4. Issues ACMD41 or CMD1 to complete initialization
  5. Reads the OCR and CSD registers to determine capacity and addressing mode
  6. Ramps the SPI clock to maximum speed after initialization

Typical usage:

Version
0.1
Date
2026-02-05

Definition in file sd.h.