ChibiOS/HAL 9.0.0
SDC Driver

Generic SD Card Driver. More...

Detailed Description

Generic SD Card Driver.

This module implements a generic SDC (Secure Digital Card) driver.

Precondition
In order to use the SDC driver the HAL_USE_SDC option must be enabled in halconf.h.

Driver State Machine

This driver implements a state machine internally, see the Abstract I/O Block Device module documentation for details.

Driver Operations

This driver allows to read or write single or multiple 512 bytes blocks on a SD Card.

Collaboration diagram for SDC Driver:

SD card types

#define SDC_MODE_CARDTYPE_MASK   0xFU
#define SDC_MODE_CARDTYPE_SDV11   0U
#define SDC_MODE_CARDTYPE_SDV20   1U
#define SDC_MODE_CARDTYPE_MMC   2U
#define SDC_MODE_HIGH_CAPACITY   0x10U

SDC bus error conditions

#define SDC_NO_ERROR   0U
#define SDC_CMD_CRC_ERROR   1U
#define SDC_DATA_CRC_ERROR   2U
#define SDC_DATA_TIMEOUT   4U
#define SDC_COMMAND_TIMEOUT   8U
#define SDC_TX_UNDERRUN   16U
#define SDC_RX_OVERRUN   32U
#define SDC_STARTBIT_ERROR   64U
#define SDC_OVERFLOW_ERROR   128U
#define SDC_UNHANDLED_ERROR   0xFFFFFFFFU

SDC configuration options

#define SDC_INIT_RETRY   100
 Number of initialization attempts before rejecting the card.
#define SDC_MMC_SUPPORT   FALSE
 Include support for MMC cards.
#define SDC_NICE_WAITING   TRUE
 Delays insertions.
#define SDC_INIT_OCR_V20   0x50FF8000U
 OCR initialization constant for V20 cards.
#define SDC_INIT_OCR   0x80100000U
 OCR initialization constant for non-V20 cards.

Macro Functions

#define sdcIsCardInserted(sdcp)
 Returns the card insertion status.
#define sdcIsWriteProtected(sdcp)
 Returns the write protect status.

PLATFORM configuration options

#define PLATFORM_SDC_USE_SDC1   FALSE
 PWMD1 driver enable switch.

Data Structures

struct  SDCConfig
 Driver configuration structure. More...
struct  SDCDriverVMT
 SDCDriver virtual methods table. More...
struct  SDCDriver
 Structure representing an SDC driver. More...

Macros

#define _sdc_driver_methods    _mmcsd_block_device_methods
 SDCDriver specific methods.

Typedefs

typedef uint32_t sdcmode_t
 Type of card flags.
typedef uint32_t sdcflags_t
 SDC Driver condition flags type.
typedef struct SDCDriver SDCDriver
 Type of a structure representing an SDC driver.

Enumerations

enum  mmc_switch_t { MMC_SWITCH_COMMAND_SET = 0 , MMC_SWITCH_SET_BITS = 1 , MMC_SWITCH_CLEAR_BITS = 2 , MMC_SWITCH_WRITE_BYTE = 3 }
 MMC switch mode. More...
enum  sd_switch_t { SD_SWITCH_CHECK = 0 , SD_SWITCH_SET = 1 }
 SDC switch mode. More...
enum  sd_switch_function_t { SD_SWITCH_FUNCTION_SPEED = 0 , SD_SWITCH_FUNCTION_CMD_SYSTEM = 1 , SD_SWITCH_FUNCTION_DRIVER_STRENGTH = 2 , SD_SWITCH_FUNCTION_CURRENT_LIMIT = 3 }
 SDC switch function. More...
enum  sdcbusmode_t { SDC_MODE_1BIT = 0 , SDC_MODE_4BIT , SDC_MODE_8BIT }
 Type of SDIO bus mode. More...
enum  sdcbusclk_t { SDC_CLK_25MHz = 0 , SDC_CLK_50MHz }
 Max supported clock. More...

Functions

static bool mode_detect (SDCDriver *sdcp)
 Detects card mode.
static bool mmc_init (SDCDriver *sdcp)
 Init procedure for MMC.
static bool sdc_init (SDCDriver *sdcp)
 Init procedure for SDC.
static uint32_t mmc_cmd6_construct (mmc_switch_t access, uint32_t idx, uint32_t value, uint32_t cmd_set)
 Constructs CMD6 argument for MMC.
static uint32_t sdc_cmd6_construct (sd_switch_t mode, sd_switch_function_t function, uint32_t value)
 Constructs CMD6 argument for SDC.
static uint16_t sdc_cmd6_extract_info (sd_switch_function_t function, const uint8_t *buf)
 Extracts information from CMD6 answer.
static bool sdc_cmd6_check_status (sd_switch_function_t function, const uint8_t *buf)
 Checks status after switching using CMD6.
static bool sdc_detect_bus_clk (SDCDriver *sdcp, sdcbusclk_t *clk)
 Reads supported bus clock and switch SDC to appropriate mode.
static bool mmc_detect_bus_clk (SDCDriver *sdcp, sdcbusclk_t *clk)
 Reads supported bus clock and switch MMC to appropriate mode.
static bool detect_bus_clk (SDCDriver *sdcp, sdcbusclk_t *clk)
 Reads supported bus clock and switch card to appropriate mode.
static bool sdc_set_bus_width (SDCDriver *sdcp)
 Sets bus width for SDC.
static bool mmc_set_bus_width (SDCDriver *sdcp)
 Sets bus width for MMC.
static bool _sdc_wait_for_transfer_state_internal (SDCDriver *sdcp, bool crc_check)
 Wait for the card to complete pending operations.
bool _sdc_wait_for_transfer_state (SDCDriver *sdcp)
 Wait for the card to complete pending operations with CRC check.
bool _sdc_wait_for_transfer_state_nocrc (SDCDriver *sdcp)
 Wait for the card to complete pending operations without CRC check.
void sdcInit (void)
 SDC Driver initialization.
void sdcObjectInit (SDCDriver *sdcp)
 Initializes the standard part of a SDCDriver structure.
msg_t sdcStart (SDCDriver *sdcp, const SDCConfig *config)
 Configures and activates the SDC peripheral.
void sdcStop (SDCDriver *sdcp)
 Deactivates the SDC peripheral.
bool sdcConnect (SDCDriver *sdcp)
 Performs the initialization procedure on the inserted card.
bool sdcDisconnect (SDCDriver *sdcp)
 Brings the driver in a state safe for card removal.
bool sdcRead (SDCDriver *sdcp, uint32_t startblk, uint8_t *buf, uint32_t n)
 Reads one or more blocks.
bool sdcWrite (SDCDriver *sdcp, uint32_t startblk, const uint8_t *buf, uint32_t n)
 Writes one or more blocks.
sdcflags_t sdcGetAndClearErrors (SDCDriver *sdcp)
 Returns the errors mask associated to the previous operation.
bool sdcSync (SDCDriver *sdcp)
 Waits for card idle condition.
bool sdcGetInfo (SDCDriver *sdcp, BlockDeviceInfo *bdip)
 Returns the media info.
bool sdcErase (SDCDriver *sdcp, uint32_t startblk, uint32_t endblk)
 Erases the supplied blocks.
void sdc_lld_init (void)
 Low level SDC driver initialization.
void sdc_lld_start (SDCDriver *sdcp)
 Configures and activates the SDC peripheral.
void sdc_lld_stop (SDCDriver *sdcp)
 Deactivates the SDC peripheral.
void sdc_lld_start_clk (SDCDriver *sdcp)
 Starts the SDIO clock and sets it to init mode (400kHz or less).
void sdc_lld_set_data_clk (SDCDriver *sdcp, sdcbusclk_t clk)
 Sets the SDIO clock to data mode (25MHz or less).
void sdc_lld_stop_clk (SDCDriver *sdcp)
 Stops the SDIO clock.
void sdc_lld_set_bus_mode (SDCDriver *sdcp, sdcbusmode_t mode)
 Switches the bus to 4 bits mode.
void sdc_lld_send_cmd_none (SDCDriver *sdcp, uint8_t cmd, uint32_t arg)
 Sends an SDIO command with no response expected.
bool sdc_lld_send_cmd_short (SDCDriver *sdcp, uint8_t cmd, uint32_t arg, uint32_t *resp)
 Sends an SDIO command with a short response expected.
bool sdc_lld_send_cmd_short_crc (SDCDriver *sdcp, uint8_t cmd, uint32_t arg, uint32_t *resp)
 Sends an SDIO command with a short response expected and CRC.
bool sdc_lld_send_cmd_long_crc (SDCDriver *sdcp, uint8_t cmd, uint32_t arg, uint32_t *resp)
 Sends an SDIO command with a long response expected and CRC.
bool sdc_lld_read (SDCDriver *sdcp, uint32_t startblk, uint8_t *buf, uint32_t n)
 Reads one or more blocks.
bool sdc_lld_write (SDCDriver *sdcp, uint32_t startblk, const uint8_t *buf, uint32_t n)
 Writes one or more blocks.
bool sdc_lld_sync (SDCDriver *sdcp)
 Waits for card idle condition.
bool sdc_lld_read_special (SDCDriver *sdcp, uint8_t *buf, size_t bytes, uint8_t cmd, uint32_t argument)
bool sdc_lld_is_card_inserted (SDCDriver *sdcp)
bool sdc_lld_is_write_protected (SDCDriver *sdcp)

Variables

static const struct SDCDriverVMT sdc_vmt
 Virtual methods table.
SDCDriver SDCD1
 SDCD1 driver identifier.

Macro Definition Documentation

◆ SDC_MODE_CARDTYPE_MASK

#define SDC_MODE_CARDTYPE_MASK   0xFU

Definition at line 38 of file hal_sdc.h.

Referenced by detect_bus_clk(), sdc_init(), and sdcConnect().

◆ SDC_MODE_CARDTYPE_SDV11

#define SDC_MODE_CARDTYPE_SDV11   0U

Definition at line 39 of file hal_sdc.h.

Referenced by mode_detect(), and sdcConnect().

◆ SDC_MODE_CARDTYPE_SDV20

#define SDC_MODE_CARDTYPE_SDV20   1U

Definition at line 40 of file hal_sdc.h.

Referenced by mode_detect(), sdc_init(), and sdcConnect().

◆ SDC_MODE_CARDTYPE_MMC

#define SDC_MODE_CARDTYPE_MMC   2U

Definition at line 41 of file hal_sdc.h.

Referenced by detect_bus_clk(), mode_detect(), and sdcConnect().

◆ SDC_MODE_HIGH_CAPACITY

#define SDC_MODE_HIGH_CAPACITY   0x10U

Definition at line 42 of file hal_sdc.h.

Referenced by mmc_init(), sdc_init(), and sdcErase().

◆ SDC_NO_ERROR

#define SDC_NO_ERROR   0U

Definition at line 49 of file hal_sdc.h.

Referenced by sdcGetAndClearErrors(), and sdcObjectInit().

◆ SDC_CMD_CRC_ERROR

#define SDC_CMD_CRC_ERROR   1U

Definition at line 50 of file hal_sdc.h.

◆ SDC_DATA_CRC_ERROR

#define SDC_DATA_CRC_ERROR   2U

Definition at line 51 of file hal_sdc.h.

◆ SDC_DATA_TIMEOUT

#define SDC_DATA_TIMEOUT   4U

Definition at line 52 of file hal_sdc.h.

◆ SDC_COMMAND_TIMEOUT

#define SDC_COMMAND_TIMEOUT   8U

Definition at line 53 of file hal_sdc.h.

◆ SDC_TX_UNDERRUN

#define SDC_TX_UNDERRUN   16U

Definition at line 54 of file hal_sdc.h.

◆ SDC_RX_OVERRUN

#define SDC_RX_OVERRUN   32U

Definition at line 55 of file hal_sdc.h.

◆ SDC_STARTBIT_ERROR

#define SDC_STARTBIT_ERROR   64U

Definition at line 56 of file hal_sdc.h.

◆ SDC_OVERFLOW_ERROR

#define SDC_OVERFLOW_ERROR   128U

Definition at line 57 of file hal_sdc.h.

Referenced by sdcRead(), and sdcWrite().

◆ SDC_UNHANDLED_ERROR

#define SDC_UNHANDLED_ERROR   0xFFFFFFFFU

Definition at line 58 of file hal_sdc.h.

◆ SDC_INIT_RETRY

#define SDC_INIT_RETRY   100

Number of initialization attempts before rejecting the card.

Note
Attempts are performed at 10mS intervals.

Definition at line 74 of file hal_sdc.h.

Referenced by mmc_init(), and sdc_init().

◆ SDC_MMC_SUPPORT

#define SDC_MMC_SUPPORT   FALSE

Include support for MMC cards.

Note
MMC support is not yet implemented so this option must be kept at FALSE.

Definition at line 83 of file hal_sdc.h.

◆ SDC_NICE_WAITING

#define SDC_NICE_WAITING   TRUE

Delays insertions.

If enabled this options inserts delays into the MMC waiting routines releasing some extra CPU time for the threads with lower priority, this may slow down the driver a bit however.

Definition at line 93 of file hal_sdc.h.

◆ SDC_INIT_OCR_V20

#define SDC_INIT_OCR_V20   0x50FF8000U

OCR initialization constant for V20 cards.

Definition at line 100 of file hal_sdc.h.

Referenced by sdc_init().

◆ SDC_INIT_OCR

#define SDC_INIT_OCR   0x80100000U

OCR initialization constant for non-V20 cards.

Definition at line 107 of file hal_sdc.h.

Referenced by sdc_init().

◆ sdcIsCardInserted

#define sdcIsCardInserted ( sdcp)
Value:
bool sdc_lld_is_card_inserted(SDCDriver *sdcp)

Returns the card insertion status.

Note
This macro wraps a low level function named sdc_lld_is_card_inserted(), this function must be provided by the application because it is not part of the SDC driver.
Parameters
[in]sdcppointer to the SDCDriver object
Returns
The card state.
Return values
falsecard not inserted.
truecard inserted.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 160 of file hal_sdc.h.

◆ sdcIsWriteProtected

#define sdcIsWriteProtected ( sdcp)
Value:
bool sdc_lld_is_write_protected(SDCDriver *sdcp)

Returns the write protect status.

Note
This macro wraps a low level function named sdc_lld_is_write_protected(), this function must be provided by the application because it is not part of the SDC driver.
Parameters
[in]sdcppointer to the SDCDriver object
Returns
The card state.
Return values
falsenot write protected.
truewrite protected.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 176 of file hal_sdc.h.

◆ PLATFORM_SDC_USE_SDC1

#define PLATFORM_SDC_USE_SDC1   FALSE

PWMD1 driver enable switch.

If set to TRUE the support for PWM1 is included.

Note
The default is FALSE.

Definition at line 48 of file hal_sdc_lld.h.

◆ _sdc_driver_methods

#define _sdc_driver_methods    _mmcsd_block_device_methods

SDCDriver specific methods.

Definition at line 90 of file hal_sdc_lld.h.

Typedef Documentation

◆ sdcmode_t

typedef uint32_t sdcmode_t

Type of card flags.

Definition at line 63 of file hal_sdc_lld.h.

◆ sdcflags_t

typedef uint32_t sdcflags_t

SDC Driver condition flags type.

Definition at line 68 of file hal_sdc_lld.h.

◆ SDCDriver

typedef struct SDCDriver SDCDriver

Type of a structure representing an SDC driver.

Definition at line 73 of file hal_sdc_lld.h.

Enumeration Type Documentation

◆ mmc_switch_t

MMC switch mode.

Enumerator
MMC_SWITCH_COMMAND_SET 
MMC_SWITCH_SET_BITS 
MMC_SWITCH_CLEAR_BITS 
MMC_SWITCH_WRITE_BYTE 

Definition at line 38 of file hal_sdc.c.

◆ sd_switch_t

SDC switch mode.

Enumerator
SD_SWITCH_CHECK 
SD_SWITCH_SET 

Definition at line 48 of file hal_sdc.c.

◆ sd_switch_function_t

SDC switch function.

Enumerator
SD_SWITCH_FUNCTION_SPEED 
SD_SWITCH_FUNCTION_CMD_SYSTEM 
SD_SWITCH_FUNCTION_DRIVER_STRENGTH 
SD_SWITCH_FUNCTION_CURRENT_LIMIT 

Definition at line 56 of file hal_sdc.c.

◆ sdcbusmode_t

Type of SDIO bus mode.

Enumerator
SDC_MODE_1BIT 
SDC_MODE_4BIT 
SDC_MODE_8BIT 

Definition at line 122 of file hal_sdc.h.

◆ sdcbusclk_t

Max supported clock.

Enumerator
SDC_CLK_25MHz 
SDC_CLK_50MHz 

Definition at line 131 of file hal_sdc.h.

Function Documentation

◆ mode_detect()

bool mode_detect ( SDCDriver * sdcp)
static

Detects card mode.

Parameters
[in]sdcppointer to the SDCDriver object
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 100 of file hal_sdc.c.

References SDCDriver::cardmode, HAL_FAILED, HAL_SUCCESS, MMCSD_CMD8_PATTERN, MMCSD_CMD_APP_CMD, MMCSD_CMD_GO_IDLE_STATE, MMCSD_CMD_SEND_IF_COND, MMCSD_R1_ERROR, sdc_lld_send_cmd_none(), sdc_lld_send_cmd_short_crc(), SDC_MODE_CARDTYPE_MMC, SDC_MODE_CARDTYPE_SDV11, and SDC_MODE_CARDTYPE_SDV20.

Referenced by sdcConnect().

Here is the call graph for this function:

◆ mmc_init()

bool mmc_init ( SDCDriver * sdcp)
static

Init procedure for MMC.

Parameters
[in]sdcppointer to the SDCDriver object
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 144 of file hal_sdc.c.

References SDCDriver::cardmode, HAL_FAILED, HAL_SUCCESS, MMCSD_CMD_INIT, osalThreadSleepMilliseconds, SDC_INIT_RETRY, sdc_lld_send_cmd_short(), and SDC_MODE_HIGH_CAPACITY.

Referenced by sdcConnect().

Here is the call graph for this function:

◆ sdc_init()

bool sdc_init ( SDCDriver * sdcp)
static

Init procedure for SDC.

Parameters
[in]sdcppointer to the SDCDriver object
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 181 of file hal_sdc.c.

References SDCDriver::cardmode, HAL_FAILED, HAL_SUCCESS, MMCSD_CMD_APP_CMD, MMCSD_CMD_APP_OP_COND, MMCSD_R1_ERROR, osalThreadSleepMilliseconds, SDC_INIT_OCR, SDC_INIT_OCR_V20, SDC_INIT_RETRY, sdc_lld_send_cmd_short(), sdc_lld_send_cmd_short_crc(), SDC_MODE_CARDTYPE_MASK, SDC_MODE_CARDTYPE_SDV20, and SDC_MODE_HIGH_CAPACITY.

Referenced by sdcConnect().

Here is the call graph for this function:

◆ mmc_cmd6_construct()

uint32_t mmc_cmd6_construct ( mmc_switch_t access,
uint32_t idx,
uint32_t value,
uint32_t cmd_set )
static

Constructs CMD6 argument for MMC.

Parameters
[in]accessEXT_CSD access mode
[in]idxEXT_CSD byte number
[in]valuevalue to be written in target field
[in]cmd_setswitch current command set
Returns
CMD6 argument.
Function Class:
Not an API, this function is for internal use only.

Definition at line 229 of file hal_sdc.c.

References osalDbgAssert.

Referenced by mmc_detect_bus_clk(), and mmc_set_bus_width().

◆ sdc_cmd6_construct()

uint32_t sdc_cmd6_construct ( sd_switch_t mode,
sd_switch_function_t function,
uint32_t value )
static

Constructs CMD6 argument for SDC.

Parameters
[in]modeswitch/test mode
[in]functionfunction number to be switched
[in]valuevalue to be written in target function
Returns
CMD6 argument.
Function Class:
Not an API, this function is for internal use only.

Definition at line 249 of file hal_sdc.c.

References osalDbgAssert.

Referenced by sdc_detect_bus_clk().

◆ sdc_cmd6_extract_info()

uint16_t sdc_cmd6_extract_info ( sd_switch_function_t function,
const uint8_t * buf )
static

Extracts information from CMD6 answer.

Parameters
[in]functionfunction number to be switched
[in]bufbuffer with answer
Returns
extracted answer.
Function Class:
Not an API, this function is for internal use only.

Definition at line 271 of file hal_sdc.c.

Referenced by sdc_detect_bus_clk().

◆ sdc_cmd6_check_status()

bool sdc_cmd6_check_status ( sd_switch_function_t function,
const uint8_t * buf )
static

Checks status after switching using CMD6.

Parameters
[in]functionfunction number to be switched
[in]bufbuffer with answer
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 291 of file hal_sdc.c.

References HAL_FAILED, and HAL_SUCCESS.

Referenced by sdc_detect_bus_clk().

◆ sdc_detect_bus_clk()

bool sdc_detect_bus_clk ( SDCDriver * sdcp,
sdcbusclk_t * clk )
static

Reads supported bus clock and switch SDC to appropriate mode.

Parameters
[in]sdcppointer to the SDCDriver object
[out]clkpointer to clock enum
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 319 of file hal_sdc.c.

References _mmcsd_get_slice(), SDCDriver::buf, HAL_FAILED, HAL_SUCCESS, MMCSD_CMD_SWITCH, MMCSD_CSD_10_CSD_STRUCTURE_SLICE, SD_SWITCH_FUNCTION_SPEED, SD_SWITCH_SET, SDC_CLK_25MHz, SDC_CLK_50MHz, sdc_cmd6_check_status(), sdc_cmd6_construct(), sdc_cmd6_extract_info(), and sdc_lld_read_special().

Referenced by detect_bus_clk().

Here is the call graph for this function:

◆ mmc_detect_bus_clk()

bool mmc_detect_bus_clk ( SDCDriver * sdcp,
sdcbusclk_t * clk )
static

Reads supported bus clock and switch MMC to appropriate mode.

Parameters
[in]sdcppointer to the SDCDriver object
[out]clkpointer to clock enum
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 373 of file hal_sdc.c.

References HAL_SUCCESS, mmc_cmd6_construct(), MMC_SWITCH_WRITE_BYTE, MMCSD_CMD_SWITCH, MMCSD_R1_ERROR, SDC_CLK_25MHz, SDC_CLK_50MHz, and sdc_lld_send_cmd_short_crc().

Referenced by detect_bus_clk().

Here is the call graph for this function:

◆ detect_bus_clk()

bool detect_bus_clk ( SDCDriver * sdcp,
sdcbusclk_t * clk )
static

Reads supported bus clock and switch card to appropriate mode.

Parameters
[in]sdcppointer to the SDCDriver object
[out]clkpointer to clock enum
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 401 of file hal_sdc.c.

References SDCDriver::cardmode, mmc_detect_bus_clk(), sdc_detect_bus_clk(), SDC_MODE_CARDTYPE_MASK, and SDC_MODE_CARDTYPE_MMC.

Referenced by sdcConnect().

Here is the call graph for this function:

◆ sdc_set_bus_width()

bool sdc_set_bus_width ( SDCDriver * sdcp)
static

Sets bus width for SDC.

Parameters
[in]sdcppointer to the SDCDriver object
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 420 of file hal_sdc.c.

References SDCConfig::bus_width, SDCDriver::config, HAL_FAILED, HAL_SUCCESS, MMCSD_CMD_APP_CMD, MMCSD_CMD_SET_BUS_WIDTH, MMCSD_R1_ERROR, SDCDriver::rca, sdc_lld_send_cmd_short_crc(), sdc_lld_set_bus_mode(), SDC_MODE_1BIT, and SDC_MODE_4BIT.

Referenced by sdcConnect().

Here is the call graph for this function:

◆ mmc_set_bus_width()

bool mmc_set_bus_width ( SDCDriver * sdcp)
static

Sets bus width for MMC.

Parameters
[in]sdcppointer to the SDCDriver object
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 458 of file hal_sdc.c.

References SDCConfig::bus_width, SDCDriver::config, HAL_FAILED, HAL_SUCCESS, mmc_cmd6_construct(), MMC_SWITCH_WRITE_BYTE, MMCSD_CMD_SWITCH, MMCSD_R1_ERROR, osalDbgAssert, sdc_lld_send_cmd_short_crc(), sdc_lld_set_bus_mode(), SDC_MODE_1BIT, SDC_MODE_4BIT, and SDC_MODE_8BIT.

Referenced by sdcConnect().

Here is the call graph for this function:

◆ _sdc_wait_for_transfer_state_internal()

bool _sdc_wait_for_transfer_state_internal ( SDCDriver * sdcp,
bool crc_check )
static

Wait for the card to complete pending operations.

Parameters
[in]sdcppointer to the SDCDriver object
[in]crc_checkCRC check flag
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 498 of file hal_sdc.c.

References HAL_FAILED, HAL_SUCCESS, MMCSD_CMD_SEND_STATUS, MMCSD_R1_ERROR, MMCSD_R1_STS, MMCSD_STS_DATA, MMCSD_STS_PRG, MMCSD_STS_RCV, MMCSD_STS_TRAN, osalThreadSleepMilliseconds, SDCDriver::rca, sdc_lld_send_cmd_short(), and sdc_lld_send_cmd_short_crc().

Referenced by _sdc_wait_for_transfer_state(), and _sdc_wait_for_transfer_state_nocrc().

Here is the call graph for this function:

◆ _sdc_wait_for_transfer_state()

bool _sdc_wait_for_transfer_state ( SDCDriver * sdcp)

Wait for the card to complete pending operations with CRC check.

Parameters
[in]sdcppointer to the SDCDriver object
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 548 of file hal_sdc.c.

References _sdc_wait_for_transfer_state_internal().

Referenced by sdcDisconnect(), and sdcErase().

Here is the call graph for this function:

◆ _sdc_wait_for_transfer_state_nocrc()

bool _sdc_wait_for_transfer_state_nocrc ( SDCDriver * sdcp)

Wait for the card to complete pending operations without CRC check.

Parameters
[in]sdcppointer to the SDCDriver object
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 564 of file hal_sdc.c.

References _sdc_wait_for_transfer_state_internal().

Referenced by sdcConnect().

Here is the call graph for this function:

◆ sdcInit()

void sdcInit ( void )

SDC Driver initialization.

Note
This function is implicitly invoked by halInit(), there is no need to explicitly initialize the driver.
Function Class:
Object or module nitializer function.

Definition at line 576 of file hal_sdc.c.

References sdc_lld_init().

Referenced by halInit().

Here is the call graph for this function:

◆ sdcObjectInit()

void sdcObjectInit ( SDCDriver * sdcp)

Initializes the standard part of a SDCDriver structure.

Parameters
[out]sdcppointer to the SDCDriver object
Function Class:
Object or module nitializer function.

Definition at line 588 of file hal_sdc.c.

References BLK_STOP, SDCDriver::config, SDCDriver::errors, SDC_NO_ERROR, sdc_vmt, and SDCDriver::vmt.

Referenced by sdc_lld_init().

◆ sdcStart()

msg_t sdcStart ( SDCDriver * sdcp,
const SDCConfig * config )

Configures and activates the SDC peripheral.

Parameters
[in]sdcppointer to the SDCDriver object
[in]configpointer to the SDCConfig object, can be NULL if the driver supports a default configuration or requires no configuration
Returns
The operation status.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 608 of file hal_sdc.c.

References BLK_ACTIVE, BLK_STOP, SDCDriver::config, HAL_RET_SUCCESS, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), and sdc_lld_start().

Here is the call graph for this function:

◆ sdcStop()

void sdcStop ( SDCDriver * sdcp)

Deactivates the SDC peripheral.

Parameters
[in]sdcppointer to the SDCDriver object
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 645 of file hal_sdc.c.

References BLK_ACTIVE, BLK_STOP, SDCDriver::config, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), and sdc_lld_stop().

Here is the call graph for this function:

◆ sdcConnect()

bool sdcConnect ( SDCDriver * sdcp)

Performs the initialization procedure on the inserted card.

This function should be invoked when a card is inserted and brings the driver in the BLK_READY state where it is possible to perform read and write operations.

Parameters
[in]sdcppointer to the SDCDriver object
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 675 of file hal_sdc.c.

References _mmcsd_get_capacity(), _mmcsd_get_capacity_ext(), _mmcsd_get_slice(), _sdc_wait_for_transfer_state_nocrc(), BLK_ACTIVE, BLK_CONNECTING, BLK_READY, SDCDriver::buf, SDCDriver::cardmode, detect_bus_clk(), HAL_FAILED, HAL_SUCCESS, mmc_init(), mmc_set_bus_width(), MMCSD_BLOCK_SIZE, MMCSD_CMD_ALL_SEND_CID, MMCSD_CMD_GO_IDLE_STATE, MMCSD_CMD_SEL_DESEL_CARD, MMCSD_CMD_SEND_CSD, MMCSD_CMD_SEND_EXT_CSD, MMCSD_CMD_SEND_RELATIVE_ADDR, MMCSD_CMD_SET_BLOCKLEN, MMCSD_CSD_MMC_CSD_STRUCTURE_SLICE, MMCSD_R1_ERROR, mode_detect(), osalDbgAssert, osalDbgCheck, SDCDriver::rca, SDC_CLK_25MHz, sdc_init(), sdc_lld_read_special(), sdc_lld_send_cmd_long_crc(), sdc_lld_send_cmd_none(), sdc_lld_send_cmd_short_crc(), sdc_lld_set_data_clk(), sdc_lld_start_clk(), sdc_lld_stop_clk(), SDC_MODE_CARDTYPE_MASK, SDC_MODE_CARDTYPE_MMC, SDC_MODE_CARDTYPE_SDV11, SDC_MODE_CARDTYPE_SDV20, and sdc_set_bus_width().

Here is the call graph for this function:

◆ sdcDisconnect()

bool sdcDisconnect ( SDCDriver * sdcp)

Brings the driver in a state safe for card removal.

Parameters
[in]sdcppointer to the SDCDriver object
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 815 of file hal_sdc.c.

References _sdc_wait_for_transfer_state(), BLK_ACTIVE, BLK_DISCONNECTING, BLK_READY, HAL_FAILED, HAL_SUCCESS, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), and sdc_lld_stop_clk().

Here is the call graph for this function:

◆ sdcRead()

bool sdcRead ( SDCDriver * sdcp,
uint32_t startblk,
uint8_t * buf,
uint32_t n )

Reads one or more blocks.

Precondition
The driver must be in the BLK_READY state after a successful sdcConnect() invocation.
Parameters
[in]sdcppointer to the SDCDriver object
[in]startblkfirst block to read
[out]bufpointer to the read buffer
[in]nnumber of blocks to read
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 858 of file hal_sdc.c.

References BLK_READING, BLK_READY, SDCDriver::errors, HAL_FAILED, osalDbgAssert, osalDbgCheck, sdc_lld_read(), and SDC_OVERFLOW_ERROR.

Here is the call graph for this function:

◆ sdcWrite()

bool sdcWrite ( SDCDriver * sdcp,
uint32_t startblk,
const uint8_t * buf,
uint32_t n )

Writes one or more blocks.

Precondition
The driver must be in the BLK_READY state after a successful sdcConnect() invocation.
Parameters
[in]sdcppointer to the SDCDriver object
[in]startblkfirst block to write
[out]bufpointer to the write buffer
[in]nnumber of blocks to write
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 895 of file hal_sdc.c.

References BLK_READY, BLK_WRITING, SDCDriver::errors, HAL_FAILED, osalDbgAssert, osalDbgCheck, sdc_lld_write(), and SDC_OVERFLOW_ERROR.

Here is the call graph for this function:

◆ sdcGetAndClearErrors()

sdcflags_t sdcGetAndClearErrors ( SDCDriver * sdcp)

Returns the errors mask associated to the previous operation.

Parameters
[in]sdcppointer to the SDCDriver object
Returns
The errors mask.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 925 of file hal_sdc.c.

References BLK_READY, SDCDriver::errors, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), and SDC_NO_ERROR.

Here is the call graph for this function:

◆ sdcSync()

bool sdcSync ( SDCDriver * sdcp)

Waits for card idle condition.

Parameters
[in]sdcppointer to the SDCDriver object
Returns
The operation status.
Return values
HAL_SUCCESSthe operation succeeded.
HAL_FAILEDthe operation failed.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 949 of file hal_sdc.c.

References BLK_READY, BLK_SYNCING, HAL_FAILED, osalDbgCheck, and sdc_lld_sync().

Here is the call graph for this function:

◆ sdcGetInfo()

bool sdcGetInfo ( SDCDriver * sdcp,
BlockDeviceInfo * bdip )

Returns the media info.

Parameters
[in]sdcppointer to the SDCDriver object
[out]bdippointer to a BlockDeviceInfo structure
Returns
The operation status.
Return values
HAL_SUCCESSthe operation succeeded.
HAL_FAILEDthe operation failed.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 980 of file hal_sdc.c.

References BlockDeviceInfo::blk_num, BLK_READY, BlockDeviceInfo::blk_size, HAL_FAILED, HAL_SUCCESS, MMCSD_BLOCK_SIZE, and osalDbgCheck.

◆ sdcErase()

bool sdcErase ( SDCDriver * sdcp,
uint32_t startblk,
uint32_t endblk )

Erases the supplied blocks.

Parameters
[in]sdcppointer to the SDCDriver object
[in]startblkstarting block number
[in]endblkending block number
Returns
The operation status.
Return values
HAL_SUCCESSthe operation succeeded.
HAL_FAILEDthe operation failed.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 1007 of file hal_sdc.c.

References _sdc_wait_for_transfer_state(), BLK_READY, BLK_WRITING, SDCDriver::cardmode, HAL_FAILED, HAL_SUCCESS, MMCSD_BLOCK_SIZE, MMCSD_CMD_ERASE, MMCSD_CMD_ERASE_RW_BLK_END, MMCSD_CMD_ERASE_RW_BLK_START, MMCSD_R1_ERROR, osalDbgAssert, osalDbgCheck, sdc_lld_send_cmd_short_crc(), and SDC_MODE_HIGH_CAPACITY.

Here is the call graph for this function:

◆ sdc_lld_init()

void sdc_lld_init ( void )

Low level SDC driver initialization.

Function Class:
Not an API, this function is for internal use only.

Definition at line 65 of file hal_sdc_lld.c.

References SDCD1, and sdcObjectInit().

Referenced by sdcInit().

Here is the call graph for this function:

◆ sdc_lld_start()

void sdc_lld_start ( SDCDriver * sdcp)

Configures and activates the SDC peripheral.

Parameters
[in]sdcppointer to the SDCDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 79 of file hal_sdc_lld.c.

References BLK_STOP.

Referenced by sdcStart().

◆ sdc_lld_stop()

void sdc_lld_stop ( SDCDriver * sdcp)

Deactivates the SDC peripheral.

Parameters
[in]sdcppointer to the SDCDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 93 of file hal_sdc_lld.c.

References BLK_STOP.

Referenced by sdcStop().

◆ sdc_lld_start_clk()

void sdc_lld_start_clk ( SDCDriver * sdcp)

Starts the SDIO clock and sets it to init mode (400kHz or less).

Parameters
[in]sdcppointer to the SDCDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 107 of file hal_sdc_lld.c.

Referenced by sdcConnect().

◆ sdc_lld_set_data_clk()

void sdc_lld_set_data_clk ( SDCDriver * sdcp,
sdcbusclk_t clk )

Sets the SDIO clock to data mode (25MHz or less).

Parameters
[in]sdcppointer to the SDCDriver object
[in]clkthe clock mode
Function Class:
Not an API, this function is for internal use only.

Definition at line 120 of file hal_sdc_lld.c.

Referenced by sdcConnect().

◆ sdc_lld_stop_clk()

void sdc_lld_stop_clk ( SDCDriver * sdcp)

Stops the SDIO clock.

Parameters
[in]sdcppointer to the SDCDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 133 of file hal_sdc_lld.c.

Referenced by sdcConnect(), and sdcDisconnect().

◆ sdc_lld_set_bus_mode()

void sdc_lld_set_bus_mode ( SDCDriver * sdcp,
sdcbusmode_t mode )

Switches the bus to 4 bits mode.

Parameters
[in]sdcppointer to the SDCDriver object
[in]modebus mode
Function Class:
Not an API, this function is for internal use only.

Definition at line 146 of file hal_sdc_lld.c.

References osalDbgAssert, SDC_MODE_1BIT, SDC_MODE_4BIT, and SDC_MODE_8BIT.

Referenced by mmc_set_bus_width(), and sdc_set_bus_width().

◆ sdc_lld_send_cmd_none()

void sdc_lld_send_cmd_none ( SDCDriver * sdcp,
uint8_t cmd,
uint32_t arg )

Sends an SDIO command with no response expected.

Parameters
[in]sdcppointer to the SDCDriver object
[in]cmdcard command
[in]argcommand argument
Function Class:
Not an API, this function is for internal use only.

Definition at line 175 of file hal_sdc_lld.c.

Referenced by mode_detect(), and sdcConnect().

◆ sdc_lld_send_cmd_short()

bool sdc_lld_send_cmd_short ( SDCDriver * sdcp,
uint8_t cmd,
uint32_t arg,
uint32_t * resp )

Sends an SDIO command with a short response expected.

Note
The CRC is not verified.
Parameters
[in]sdcppointer to the SDCDriver object
[in]cmdcard command
[in]argcommand argument
[out]resppointer to the response buffer (one word)
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 197 of file hal_sdc_lld.c.

References HAL_SUCCESS.

Referenced by _sdc_wait_for_transfer_state_internal(), mmc_init(), and sdc_init().

◆ sdc_lld_send_cmd_short_crc()

bool sdc_lld_send_cmd_short_crc ( SDCDriver * sdcp,
uint8_t cmd,
uint32_t arg,
uint32_t * resp )

Sends an SDIO command with a short response expected and CRC.

Parameters
[in]sdcppointer to the SDCDriver object
[in]cmdcard command
[in]argcommand argument
[out]resppointer to the response buffer (one word)
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 222 of file hal_sdc_lld.c.

References HAL_SUCCESS.

Referenced by _sdc_wait_for_transfer_state_internal(), mmc_detect_bus_clk(), mmc_set_bus_width(), mode_detect(), sdc_init(), sdc_set_bus_width(), sdcConnect(), and sdcErase().

◆ sdc_lld_send_cmd_long_crc()

bool sdc_lld_send_cmd_long_crc ( SDCDriver * sdcp,
uint8_t cmd,
uint32_t arg,
uint32_t * resp )

Sends an SDIO command with a long response expected and CRC.

Parameters
[in]sdcppointer to the SDCDriver object
[in]cmdcard command
[in]argcommand argument
[out]resppointer to the response buffer (four words)
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 247 of file hal_sdc_lld.c.

References HAL_SUCCESS.

Referenced by sdcConnect().

◆ sdc_lld_read()

bool sdc_lld_read ( SDCDriver * sdcp,
uint32_t startblk,
uint8_t * buf,
uint32_t n )

Reads one or more blocks.

Parameters
[in]sdcppointer to the SDCDriver object
[in]startblkfirst block to read
[out]bufpointer to the read buffer
[in]nnumber of blocks to read
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 272 of file hal_sdc_lld.c.

References HAL_SUCCESS.

Referenced by sdcRead().

◆ sdc_lld_write()

bool sdc_lld_write ( SDCDriver * sdcp,
uint32_t startblk,
const uint8_t * buf,
uint32_t n )

Writes one or more blocks.

Parameters
[in]sdcppointer to the SDCDriver object
[in]startblkfirst block to write
[out]bufpointer to the write buffer
[in]nnumber of blocks to write
Returns
The operation status.
Return values
HAL_SUCCESSoperation succeeded.
HAL_FAILEDoperation failed.
Function Class:
Not an API, this function is for internal use only.

Definition at line 297 of file hal_sdc_lld.c.

References HAL_SUCCESS.

Referenced by sdcWrite().

◆ sdc_lld_sync()

bool sdc_lld_sync ( SDCDriver * sdcp)

Waits for card idle condition.

Parameters
[in]sdcppointer to the SDCDriver object
Returns
The operation status.
Return values
HAL_SUCCESSthe operation succeeded.
HAL_FAILEDthe operation failed.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 319 of file hal_sdc_lld.c.

References HAL_SUCCESS.

Referenced by sdcSync().

◆ sdc_lld_read_special()

bool sdc_lld_read_special ( SDCDriver * sdcp,
uint8_t * buf,
size_t bytes,
uint8_t cmd,
uint32_t argument )

Referenced by sdc_detect_bus_clk(), and sdcConnect().

◆ sdc_lld_is_card_inserted()

bool sdc_lld_is_card_inserted ( SDCDriver * sdcp)

◆ sdc_lld_is_write_protected()

bool sdc_lld_is_write_protected ( SDCDriver * sdcp)

Variable Documentation

◆ sdc_vmt

const struct SDCDriverVMT sdc_vmt
static
Initial value:
= {
(size_t)0,
(bool (*)(void *))sdc_lld_is_card_inserted,
(bool (*)(void *))sdc_lld_is_write_protected,
(bool (*)(void *))sdcConnect,
(bool (*)(void *))sdcDisconnect,
(bool (*)(void *, uint32_t, uint8_t *, uint32_t))sdcRead,
(bool (*)(void *, uint32_t, const uint8_t *, uint32_t))sdcWrite,
(bool (*)(void *))sdcSync,
(bool (*)(void *, BlockDeviceInfo *))sdcGetInfo
}
bool sdcRead(SDCDriver *sdcp, uint32_t startblk, uint8_t *buf, uint32_t n)
Reads one or more blocks.
Definition hal_sdc.c:858
bool sdcConnect(SDCDriver *sdcp)
Performs the initialization procedure on the inserted card.
Definition hal_sdc.c:675
bool sdcWrite(SDCDriver *sdcp, uint32_t startblk, const uint8_t *buf, uint32_t n)
Writes one or more blocks.
Definition hal_sdc.c:895
bool sdcDisconnect(SDCDriver *sdcp)
Brings the driver in a state safe for card removal.
Definition hal_sdc.c:815
bool sdcSync(SDCDriver *sdcp)
Waits for card idle condition.
Definition hal_sdc.c:949
bool sdcGetInfo(SDCDriver *sdcp, BlockDeviceInfo *bdip)
Returns the media info.
Definition hal_sdc.c:980
Block device info.
Definition hal_ioblock.h:55

Virtual methods table.

Definition at line 74 of file hal_sdc.c.

Referenced by sdcObjectInit().

◆ SDCD1

SDCDriver SDCD1

SDCD1 driver identifier.

Definition at line 41 of file hal_sdc_lld.c.

Referenced by sdc_lld_init().