Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Implementation of SD/MMC/SDIO Stack

Modules

 Compiler abstraction layer and code utilities
 Compiler abstraction layer and code utilities for AT91SAM.
 
 Preprocessor - Token Paste
 
 Preprocessor - Stringize
 
 Preprocessor - Macro Repeat
 
 Global interrupt management
 This is a driver for global enabling and disabling of interrupts.
 

Data Structures

struct  sd_mmc_card
 SD/MMC card information structure. More...
 

Files

file  compiler.h
 Commonly used includes, types and macros.
 
file  interrupt.h
 Global interrupt management for 8- and 32-bit AVR.
 
file  parts.h
 Atmel part identification macros.
 
file  parts.h
 Atmel part identification macros.
 
file  pmc.h
 Power Management Controller (PMC) driver for SAM.
 
file  tpaste.h
 Preprocessor token pasting utils.
 

Macros

#define driver   hsmci
 
#define driver_adtc_start   ATPASTE2(driver, _adtc_start)
 
#define driver_adtc_stop   ATPASTE2(driver, _send_cmd)
 
#define driver_deselect_device   ATPASTE2(driver, _deselect_device)
 
#define driver_get_bus_width   ATPASTE2(driver, _get_bus_width)
 
#define driver_get_response   ATPASTE2(driver, _get_response)
 
#define driver_get_response_128   ATPASTE2(driver, _get_response_128)
 
#define driver_init   ATPASTE2(driver, _init)
 
#define driver_is_high_speed_capable   ATPASTE2(driver, _is_high_speed_capable)
 
#define driver_read_word   ATPASTE2(driver, _read_word)
 
#define driver_select_device   ATPASTE2(driver, _select_device)
 
#define driver_send_clock   ATPASTE2(driver, _send_clock)
 
#define driver_send_cmd   ATPASTE2(driver, _send_cmd)
 
#define driver_start_read_blocks   ATPASTE2(driver, _start_read_blocks)
 
#define driver_start_write_blocks   ATPASTE2(driver, _start_write_blocks)
 
#define driver_wait_end_of_read_blocks   ATPASTE2(driver, _wait_end_of_read_blocks)
 
#define driver_wait_end_of_write_blocks   ATPASTE2(driver, _wait_end_of_write_blocks)
 
#define driver_write_word   ATPASTE2(driver, _write_word)
 
#define IS_SDIO()   (sd_mmc_card->type & CARD_TYPE_SDIO)
 
#define sd_mmc_debug(...)
 
#define sd_mmc_is_mci()   (!sd_mmc_is_spi())
 
#define sd_mmc_is_spi()   false
 
#define SD_MMC_MCI_MEM_CNT   0
 
#define SD_MMC_MEM_CNT   SD_MMC_HSMCI_MEM_CNT
 
#define SD_MMC_SPI_MEM_CNT   0
 
#define SD_MMC_VOLTAGE_SUPPORT
 This SD MMC stack supports only the high voltage. More...
 

Enumerations

enum  card_state {
  SD_MMC_CARD_STATE_READY = 0,
  SD_MMC_CARD_STATE_DEBOUNCE = 1,
  SD_MMC_CARD_STATE_INIT = 2,
  SD_MMC_CARD_STATE_UNUSABLE = 3,
  SD_MMC_CARD_STATE_NO_CARD = 4
}
 SD/MMC card states. More...
 

Functions

sd_mmc_err_t sd_mmc_check (uint8_t slot)
 Performs a card checks. More...
 
uint32_t sd_mmc_get_capacity (uint8_t slot)
 Get the memory capacity. More...
 
card_type_t sd_mmc_get_type (uint8_t slot)
 Get the card type. More...
 
card_version_t sd_mmc_get_version (uint8_t slot)
 Get the card version. More...
 
sd_mmc_err_t sd_mmc_init_read_blocks (uint8_t slot, uint32_t start, uint16_t nb_block)
 Initialize the read blocks of data from the card. More...
 
sd_mmc_err_t sd_mmc_init_write_blocks (uint8_t slot, uint32_t start, uint16_t nb_block)
 Initialize the write blocks of data. More...
 
bool sd_mmc_is_write_protected (uint8_t slot)
 Get the card write protection status. More...
 
uint8_t sd_mmc_nb_slot (void)
 Return the number of slot available. More...
 
sd_mmc_err_t sd_mmc_start_read_blocks (void *dest, uint16_t nb_block)
 Start the read blocks of data from the card. More...
 
sd_mmc_err_t sd_mmc_start_write_blocks (const void *src, uint16_t nb_block)
 Start the write blocks of data. More...
 
sd_mmc_err_t sd_mmc_wait_end_of_read_blocks (bool abort)
 Wait the end of read blocks of data from the card. More...
 
sd_mmc_err_t sd_mmc_wait_end_of_write_blocks (bool abort)
 Wait the end of write blocks of data. More...
 
sd_mmc_err_t sdio_read_direct (uint8_t slot, uint8_t func_num, uint32_t addr, uint8_t *dest)
 Read one byte from SDIO using RW_DIRECT command. More...
 
sd_mmc_err_t sdio_read_extended (uint8_t slot, uint8_t func_num, uint32_t addr, uint8_t inc_addr, uint8_t *dest, uint16_t size)
 Read bytes from SDIO using RW_EXTENDED command. More...
 
sd_mmc_err_t sdio_write_direct (uint8_t slot, uint8_t func_num, uint32_t addr, uint8_t data)
 Write one byte to SDIO using RW_DIRECT command. More...
 
sd_mmc_err_t sdio_write_extended (uint8_t slot, uint8_t func_num, uint32_t addr, uint8_t inc_addr, uint8_t *src, uint16_t size)
 Write bytes to SDIO using RW_EXTENDED command. More...
 

Variables

const uint32_t mmc_trans_multipliers [16]
 MMC transfer multiplier factor codes (1/10) list. More...
 
static struct sd_mmc_cardsd_mmc_card
 Pointer on current slot configurated. More...
 
static struct sd_mmc_card sd_mmc_cards [SD_MMC_MEM_CNT]
 SD/MMC card list Note: Initialize card detect pin fields if present. More...
 
static uint16_t sd_mmc_nb_block_remaining = 0
 Number of block remaining to read or write on the current transfer. More...
 
static uint16_t sd_mmc_nb_block_to_tranfer = 0
 Number of block to read or write on the current transfer. More...
 
static uint8_t sd_mmc_slot_sel
 Index of current slot configurated. More...
 
const uint32_t sd_mmc_trans_units [7]
 SD/MMC transfer rate unit codes (10K) list. More...
 
const uint32_t sd_trans_multipliers [16]
 SD transfer multiplier factor codes (1/10) list. More...
 

MMC, SD and SDIO commands process

static bool mmc_spi_op_cond (void)
 Sends operation condition command and read OCR (SPI only) More...
 
static bool mmc_mci_op_cond (void)
 Sends operation condition command and read OCR (MCI only) More...
 
static bool sd_spi_op_cond (uint8_t v2)
 Ask to all cards to send their operations conditions (SPI only). More...
 
static bool sd_mci_op_cond (uint8_t v2)
 Ask to all cards to send their operations conditions (MCI only). More...
 
static bool sdio_op_cond (void)
 Try to get the SDIO card's operating condition. More...
 
static bool sdio_get_max_speed (void)
 Get SDIO max transfer speed in Hz. More...
 
static bool sdio_cmd52_set_bus_width (void)
 CMD52 for SDIO - Switches the bus width mode to 4. More...
 
static bool sdio_cmd52_set_high_speed (void)
 CMD52 for SDIO - Enable the high speed mode. More...
 
static bool sd_cm6_set_high_speed (void)
 CMD6 for SD - Switch card in high speed mode. More...
 
static bool mmc_cmd6_set_bus_width (uint8_t bus_width)
 CMD6 for MMC - Switches the bus width mode. More...
 
static bool mmc_cmd6_set_high_speed (void)
 CMD6 for MMC - Switches in high speed mode. More...
 
static bool sd_cmd8 (uint8_t *v2)
 CMD8 for SD card - Send Interface Condition Command. More...
 
static bool mmc_cmd8 (uint8_t *b_authorize_high_speed)
 CMD8 - The card sends its EXT_CSD register as a block of data. More...
 
static bool sd_mmc_cmd9_spi (void)
 CMD9: Addressed card sends its card-specific data (CSD) on the CMD line spi. More...
 
static bool sd_mmc_cmd9_mci (void)
 CMD9: Addressed card sends its card-specific data (CSD) on the CMD line mci. More...
 
static void mmc_decode_csd (void)
 Decodes MMC CSD register. More...
 
static void sd_decode_csd (void)
 Decodes SD CSD register. More...
 
static bool sd_mmc_cmd13 (void)
 CMD13 - Addressed card sends its status register. More...
 
static bool sdio_cmd52 (uint8_t rw_flag, uint8_t func_nb, uint32_t reg_addr, uint8_t rd_after_wr, uint8_t *io_data)
 CMD52 - SDIO IO_RW_DIRECT command. More...
 
static bool sdio_cmd53 (uint8_t rw_flag, uint8_t func_nb, uint32_t reg_addr, uint8_t inc_addr, uint32_t size, bool access_block)
 CMD53 - SDIO IO_RW_EXTENDED command This implementation support only the SDIO multi-byte transfer mode which is similar to the single block transfer on memory. More...
 
static bool sd_acmd6 (void)
 ACMD6 - Define the data bus width to 4 bits bus. More...
 
static bool sd_acmd51 (void)
 ACMD51 - Read the SD Configuration Register. More...
 

Internal function to process the initialization and install

static sd_mmc_err_t sd_mmc_select_slot (uint8_t slot)
 Select a card slot and initialize the associated driver. More...
 
static void sd_mmc_configure_slot (void)
 Configures the driver with the selected card configuration. More...
 
static void sd_mmc_deselect_slot (void)
 Deselect the current card slot. More...
 
static bool sd_mmc_spi_card_init (void)
 Initialize the SD card in SPI mode. More...
 
static bool sd_mmc_mci_card_init (void)
 Initialize the SD card in MCI mode. More...
 
static bool sd_mmc_spi_install_mmc (void)
 Initialize the MMC card in SPI mode. More...
 
static bool sd_mmc_mci_install_mmc (void)
 Initialize the MMC card in MCI mode. More...
 

Internal functions to manage a large timeout after a card insertion

static t_cpu_time timer
 
static bool sd_mmc_sam_systick_used
 
#define SD_MMC_START_TIMEOUT()   delay_ms(SD_MMC_DEBOUNCE_TIMEOUT)
 
#define SD_MMC_IS_TIMEOUT()   true
 
#define SD_MMC_STOP_TIMEOUT()
 
#define SD_MMC_DEBOUNCE_TIMEOUT   1000
 
#define SD_MMC_START_TIMEOUT()   cpu_set_timeout(cpu_ms_2_cy(SD_MMC_DEBOUNCE_TIMEOUT, sysclk_get_cpu_hz()), &timer)
 
#define SD_MMC_IS_TIMEOUT()   cpu_is_timeout(&timer)
 
#define SD_MMC_STOP_TIMEOUT()
 

Quick start guide for the SAM PMC module

This is the quick start guide for the PMC module, with step-by-step instructions on how to configure and use the driver in a selection of use cases.The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.

PMC use cases

Basic use case - Switch Main Clock sources

In this use case, the PMC module is configured for a variety of system clock sources and speeds. A LED is used to visually indicate the current clock speed as the source is switched.

Setup

Prerequisites

  1. General Purpose I/O Management (gpio)

Code

The following function needs to be added to the user application, to flash a board LED a variable number of times at a rate given in CPU ticks.

#define FLASH_TICK_COUNT 0x00012345
void flash_led(uint32_t tick_count, uint8_t flash_count)
{
SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
SysTick->LOAD = tick_count;
while (flash_count--)
{
while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));
while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));
}
}

Use case

Example code

Add to application C-file:

for (;;)
{
pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_12_MHz);
flash_led(FLASH_TICK_COUNT, 5);
pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_8_MHz);
flash_led(FLASH_TICK_COUNT, 5);
pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_4_MHz);
flash_led(FLASH_TICK_COUNT, 5);
flash_led(FLASH_TICK_COUNT, 5);
}

Workflow

  1. Wrap the code in an infinite loop:
    for (;;)
  2. Switch the Master CPU frequency to the internal 12MHz RC oscillator, flash a LED on the board several times:
    pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_12_MHz);
    flash_led(FLASH_TICK_COUNT, 5);
  3. Switch the Master CPU frequency to the internal 8MHz RC oscillator, flash a LED on the board several times:
    pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_8_MHz);
    flash_led(FLASH_TICK_COUNT, 5);
  4. Switch the Master CPU frequency to the internal 4MHz RC oscillator, flash a LED on the board several times:
    pmc_switch_mainck_to_fastrc(CKGR_MOR_MOSCRCF_4_MHz);
    flash_led(FLASH_TICK_COUNT, 5);
  5. Switch the Master CPU frequency to the external crystal oscillator, flash a LED on the board several times:
    flash_led(FLASH_TICK_COUNT, 5);

Use case #2 - Configure Programmable Clocks

In this use case, the PMC module is configured to start the Slow Clock from an attached 32KHz crystal, and start one of the Programmable Clock modules sourced from the Slow Clock divided down with a prescale factor of 64.

Setup

Prerequisites

  1. Parallel Input/Output Controller (pio)

Code

The following code must be added to the user application:

Workflow

  1. Configure the PCK1 pin to output on a specific port pin (in this case, PIOA pin 17) of the microcontroller.
    Note
    The peripheral selection and pin will vary according to your selected SAM device model. Refer to the "Peripheral Signal Multiplexing on I/O Lines" of your device's datasheet.

Use case

The generated PCK1 clock output can be viewed on an oscilloscope attached to the correct pin of the microcontroller.

Example code

Add to application C-file:

Workflow

  1. Switch the Slow Clock source input to an external 32KHz crystal:
  2. Switch the Programmable Clock module PCK1 source clock to the Slow Clock, with a prescaler of 64:
    pmc_switch_pck_to_sclk(PMC_PCK_1, PMC_PCK_PRES_CLK_64);
  3. Enable Programmable Clock module PCK1:
  4. Enter an infinite loop:
    for (;;)
    {
    // Do Nothing
    }

#define driver   hsmci
#define driver_adtc_stop   ATPASTE2(driver, _send_cmd)
#define driver_deselect_device   ATPASTE2(driver, _deselect_device)

Referenced by sd_mmc_deselect_slot().

#define driver_get_bus_width   ATPASTE2(driver, _get_bus_width)
#define driver_get_response_128   ATPASTE2(driver, _get_response_128)

Referenced by sd_mmc_cmd9_mci().

#define driver_init   ATPASTE2(driver, _init)
#define driver_is_high_speed_capable   ATPASTE2(driver, _is_high_speed_capable)
#define driver_read_word   ATPASTE2(driver, _read_word)

Referenced by mmc_cmd8().

#define driver_select_device   ATPASTE2(driver, _select_device)

Referenced by sd_mmc_configure_slot().

#define driver_send_clock   ATPASTE2(driver, _send_clock)
#define driver_start_read_blocks   ATPASTE2(driver, _start_read_blocks)
#define driver_start_write_blocks   ATPASTE2(driver, _start_write_blocks)
#define driver_wait_end_of_read_blocks   ATPASTE2(driver, _wait_end_of_read_blocks)
#define driver_wait_end_of_write_blocks   ATPASTE2(driver, _wait_end_of_write_blocks)
#define driver_write_word   ATPASTE2(driver, _write_word)
#define IS_SDIO ( )    (sd_mmc_card->type & CARD_TYPE_SDIO)
#define SD_MMC_DEBOUNCE_TIMEOUT   1000
#define sd_mmc_is_spi ( )    false

Referenced by sd_mmc_check(), and sd_mmc_cmd13().

static bool SD_MMC_IS_TIMEOUT (   void)    true
inline

Referenced by sd_mmc_select_slot().

#define SD_MMC_IS_TIMEOUT ( )    cpu_is_timeout(&timer)
#define SD_MMC_MCI_MEM_CNT   0
#define SD_MMC_MEM_CNT   SD_MMC_HSMCI_MEM_CNT
#define SD_MMC_SPI_MEM_CNT   0
static void SD_MMC_START_TIMEOUT (   void)    delay_ms(SD_MMC_DEBOUNCE_TIMEOUT)
inline

Referenced by sd_mmc_select_slot().

#define SD_MMC_START_TIMEOUT ( )    cpu_set_timeout(cpu_ms_2_cy(SD_MMC_DEBOUNCE_TIMEOUT, sysclk_get_cpu_hz()), &timer)
static void SD_MMC_STOP_TIMEOUT (   void)
inline

Referenced by sd_mmc_select_slot().

#define SD_MMC_STOP_TIMEOUT ( )
#define SD_MMC_VOLTAGE_SUPPORT
Value:
#define OCR_VDD_30_31
Definition: sd_mmc_protocol.h:740
#define OCR_VDD_32_33
Definition: sd_mmc_protocol.h:742
#define OCR_VDD_28_29
Definition: sd_mmc_protocol.h:738
#define OCR_VDD_29_30
Definition: sd_mmc_protocol.h:739
#define OCR_VDD_27_28
Definition: sd_mmc_protocol.h:737
#define OCR_VDD_31_32
Definition: sd_mmc_protocol.h:741

This SD MMC stack supports only the high voltage.

Referenced by mmc_mci_op_cond(), sd_mci_op_cond(), and sdio_op_cond().

enum card_state

SD/MMC card states.

Enumerator
SD_MMC_CARD_STATE_READY 

Ready to use.

SD_MMC_CARD_STATE_DEBOUNCE 

Debounce on going.

SD_MMC_CARD_STATE_INIT 

Initialization on going.

SD_MMC_CARD_STATE_UNUSABLE 

Unusable card.

SD_MMC_CARD_STATE_NO_CARD 

No SD/MMC card inserted.

static bool mmc_cmd6_set_bus_width ( uint8_t  bus_width)
static

CMD6 for MMC - Switches the bus width mode.

Note
CMD6 is valid under the "trans" state.
sd_mmc_card->bus_width is updated.
Parameters
bus_widthBus width to set
Returns
true if success, otherwise false

References sd_mmc_card::bus_width, CARD_STATUS_SWITCH_ERROR, driver_get_response, driver_send_cmd, MMC_CMD6_ACCESS_SET_BITS, MMC_CMD6_INDEX_BUS_WIDTH, MMC_CMD6_SWITCH, MMC_CMD6_VALUE_BUS_WIDTH_1BIT, MMC_CMD6_VALUE_BUS_WIDTH_4BIT, MMC_CMD6_VALUE_BUS_WIDTH_8BIT, and sd_mmc_debug.

Referenced by sd_mmc_mci_install_mmc().

static bool mmc_cmd6_set_high_speed ( void  )
static

CMD6 for MMC - Switches in high speed mode.

Note
CMD6 is valid under the "trans" state.
sd_mmc_card->high_speed is updated.
sd_mmc_card->clock is updated.
Returns
true if success, otherwise false

References CARD_STATUS_SWITCH_ERROR, sd_mmc_card::clock, driver_get_response, driver_send_cmd, sd_mmc_card::high_speed, MMC_CMD6_ACCESS_WRITE_BYTE, MMC_CMD6_INDEX_HS_TIMING, MMC_CMD6_SWITCH, MMC_CMD6_VALUE_HS_TIMING_ENABLE, and sd_mmc_debug.

Referenced by sd_mmc_mci_install_mmc().

static bool mmc_cmd8 ( uint8_t *  b_authorize_high_speed)
static

CMD8 - The card sends its EXT_CSD register as a block of data.

Parameters
b_authorize_high_speedPointer to update with the high speed support information
Returns
true if success, otherwise false

References sd_mmc_card::capacity, sd_mmc_card::csd, driver_adtc_start, driver_read_word, EXT_CSD_BSIZE, EXT_CSD_CARD_TYPE_INDEX, EXT_CSD_SEC_COUNT_INDEX, MMC_CMD8_SEND_EXT_CSD, MMC_CSD_C_SIZE, and MMC_CTYPE_52MHZ.

Referenced by sd_mmc_mci_install_mmc(), and sd_mmc_spi_install_mmc().

static bool mmc_mci_op_cond ( void  )
static

Sends operation condition command and read OCR (MCI only)

  • CMD1 sends operation condition command
  • CMD1 reads OCR
Returns
true if success, otherwise false

References CARD_TYPE_HC, driver_get_response, driver_send_cmd, MMC_MCI_CMD1_SEND_OP_COND, OCR_ACCESS_MODE_MASK, OCR_ACCESS_MODE_SECTOR, OCR_POWER_UP_BUSY, sd_mmc_debug, SD_MMC_VOLTAGE_SUPPORT, and sd_mmc_card::type.

Referenced by sd_mmc_mci_install_mmc().

static bool mmc_spi_op_cond ( void  )
static

Sends operation condition command and read OCR (SPI only)

  • CMD1 sends operation condition command
  • CMD58 reads OCR
Returns
true if success, otherwise false

References CARD_TYPE_HC, driver_get_response, driver_send_cmd, MMC_SPI_CMD1_SEND_OP_COND, OCR_ACCESS_MODE_MASK, OCR_ACCESS_MODE_SECTOR, R1_SPI_IDLE, sd_mmc_debug, SDMMC_SPI_CMD58_READ_OCR, and sd_mmc_card::type.

Referenced by sd_mmc_spi_install_mmc().

static bool sd_acmd51 ( void  )
static

ACMD51 - Read the SD Configuration Register.

Note
SD Card Configuration Register (SCR) provides information on the SD Memory Card's special features that were configured into the given card. The size of SCR register is 64 bits.
Returns
true if success, otherwise false

References CARD_VER_SD_1_0, CARD_VER_SD_1_10, CARD_VER_SD_2_0, CARD_VER_SD_3_0, driver_adtc_start, driver_send_cmd, driver_start_read_blocks, driver_wait_end_of_read_blocks, sd_mmc_card::rca, SD_ACMD51_SEND_SCR, SD_SCR_REG_BSIZE, SD_SCR_SD_SPEC, SD_SCR_SD_SPEC3, SD_SCR_SD_SPEC_1_0_01, SD_SCR_SD_SPEC_1_10, SD_SCR_SD_SPEC_2_00, SD_SCR_SD_SPEC_3_00, SDMMC_CMD55_APP_CMD, and sd_mmc_card::version.

Referenced by sd_mmc_mci_card_init(), and sd_mmc_spi_card_init().

static bool sd_acmd6 ( void  )
static

ACMD6 - Define the data bus width to 4 bits bus.

Returns
true if success, otherwise false

References sd_mmc_card::bus_width, driver_send_cmd, sd_mmc_card::rca, SD_ACMD6_SET_BUS_WIDTH, sd_mmc_debug, and SDMMC_CMD55_APP_CMD.

Referenced by sd_mmc_mci_card_init().

static bool sd_cmd8 ( uint8_t *  v2)
static

CMD8 for SD card - Send Interface Condition Command.

Note
Send SD Memory Card interface condition, which includes host supply voltage information and asks the card whether card supports voltage. Should be performed at initialization time to detect the card type.
Parameters
v2Pointer to v2 flag to update
Returns
true if success, otherwise false with a update of sd_mmc_err.

References driver_get_response, driver_send_cmd, SD_CMD8_HIGH_VOLTAGE, SD_CMD8_MASK_PATTERN, SD_CMD8_MASK_VOLTAGE, SD_CMD8_PATTERN, SD_CMD8_SEND_IF_COND, and sd_mmc_debug.

Referenced by sd_mmc_mci_card_init(), and sd_mmc_spi_card_init().

static bool sd_mci_op_cond ( uint8_t  v2)
static

Ask to all cards to send their operations conditions (MCI only).

  • ACMD41 sends operation condition command.
  • ACMD41 reads OCR
Parameters
v2Shall be 1 if it is a SD card V2
Returns
true if success, otherwise false

References CARD_TYPE_HC, driver_get_response, driver_send_cmd, OCR_CCS, OCR_POWER_UP_BUSY, SD_ACMD41_HCS, SD_MCI_ACMD41_SD_SEND_OP_COND, sd_mmc_debug, SD_MMC_VOLTAGE_SUPPORT, SDMMC_CMD55_APP_CMD, and sd_mmc_card::type.

Referenced by sd_mmc_mci_card_init().

sd_mmc_err_t sd_mmc_check ( uint8_t  slot)

Performs a card checks.

Parameters
slotCard slot to use
Return values
SD_MMC_OKCard ready
SD_MMC_INIT_ONGOINGInitialization on going
SD_MMC_ERR_NO_CARDCard not present in slot
Othervalue for error cases, see sd_mmc_err_t

References SD_MMC_CARD_STATE_READY, SD_MMC_CARD_STATE_UNUSABLE, sd_mmc_debug, sd_mmc_deselect_slot(), SD_MMC_ERR_UNUSABLE, SD_MMC_INIT_ONGOING, sd_mmc_is_spi, sd_mmc_mci_card_init(), sd_mmc_select_slot(), sd_mmc_spi_card_init(), and sd_mmc_card::state.

Referenced by main().

static bool sd_mmc_cmd13 ( void  )
static

CMD13 - Addressed card sends its status register.

This function waits the clear of the busy flag

Returns
true if success, otherwise false

References CARD_STATUS_READY_FOR_DATA, driver_get_response, driver_send_cmd, sd_mmc_card::rca, sd_mmc_debug, sd_mmc_is_spi, SDMMC_MCI_CMD13_SEND_STATUS, and SDMMC_SPI_CMD13_SEND_STATUS.

Referenced by sd_mmc_init_read_blocks(), sd_mmc_spi_card_init(), and sd_mmc_spi_install_mmc().

static bool sd_mmc_cmd9_mci ( void  )
static

CMD9: Addressed card sends its card-specific data (CSD) on the CMD line mci.

Returns
true if success, otherwise false

References sd_mmc_card::csd, driver_get_response_128, driver_send_cmd, sd_mmc_card::rca, and SDMMC_MCI_CMD9_SEND_CSD.

Referenced by sd_mmc_mci_card_init(), and sd_mmc_mci_install_mmc().

static bool sd_mmc_cmd9_spi ( void  )
static

CMD9: Addressed card sends its card-specific data (CSD) on the CMD line spi.

Returns
true if success, otherwise false

References sd_mmc_card::csd, CSD_REG_BSIZE, driver_adtc_start, driver_start_read_blocks, driver_wait_end_of_read_blocks, sd_mmc_card::rca, and SDMMC_SPI_CMD9_SEND_CSD.

Referenced by sd_mmc_spi_card_init(), and sd_mmc_spi_install_mmc().

static void sd_mmc_configure_slot ( void  )
static
uint32_t sd_mmc_get_capacity ( uint8_t  slot)

Get the memory capacity.

Parameters
slotCard slot
Returns
Capacity (unit KB)

References sd_mmc_card::capacity, sd_mmc_deselect_slot(), SD_MMC_OK, and sd_mmc_select_slot().

Referenced by main_display_info_card(), and main_test_memory().

card_type_t sd_mmc_get_type ( uint8_t  slot)

Get the card type.

Parameters
slotCard slot
Returns
Card type (card_type_t)

References CARD_TYPE_UNKNOWN, sd_mmc_deselect_slot(), SD_MMC_OK, sd_mmc_select_slot(), and sd_mmc_card::type.

Referenced by main(), and main_display_info_card().

card_version_t sd_mmc_get_version ( uint8_t  slot)

Get the card version.

Parameters
slotCard slot
Returns
Card version (card_version_t)

References CARD_VER_UNKNOWN, sd_mmc_deselect_slot(), SD_MMC_OK, sd_mmc_select_slot(), and sd_mmc_card::version.

sd_mmc_err_t sd_mmc_init_read_blocks ( uint8_t  slot,
uint32_t  start,
uint16_t  nb_block 
)

Initialize the read blocks of data from the card.

Parameters
slotCard slot to use
startStart block number to to read.
nb_blockTotal number of blocks to be read.
Returns
return SD_MMC_OK if success, otherwise return an error code (sd_mmc_err_t).

References CARD_STATUS_ERR_RD_WR, CARD_TYPE_HC, driver_adtc_start, driver_get_response, SD_MMC_BLOCK_SIZE, sd_mmc_cmd13(), sd_mmc_debug, sd_mmc_deselect_slot(), SD_MMC_ERR_COMM, sd_mmc_is_mci, sd_mmc_nb_block_remaining, sd_mmc_nb_block_to_tranfer, SD_MMC_OK, sd_mmc_select_slot(), SDMMC_CMD17_READ_SINGLE_BLOCK, SDMMC_CMD18_READ_MULTIPLE_BLOCK, SDMMC_CMD_GET_INDEX, and sd_mmc_card::type.

Referenced by main_test_memory().

sd_mmc_err_t sd_mmc_init_write_blocks ( uint8_t  slot,
uint32_t  start,
uint16_t  nb_block 
)

Initialize the write blocks of data.

Parameters
slotCard slot to use
startStart block number to be written.
nb_blockTotal number of blocks to be written.
Returns
return SD_MMC_OK if success, otherwise return an error code (sd_mmc_err_t).

References CARD_STATUS_ERR_RD_WR, CARD_TYPE_HC, driver_adtc_start, driver_get_response, SD_MMC_BLOCK_SIZE, sd_mmc_debug, sd_mmc_deselect_slot(), SD_MMC_ERR_COMM, SD_MMC_ERR_WP, sd_mmc_is_mci, sd_mmc_is_write_protected(), sd_mmc_nb_block_remaining, sd_mmc_nb_block_to_tranfer, SD_MMC_OK, sd_mmc_select_slot(), SDMMC_CMD24_WRITE_BLOCK, SDMMC_CMD25_WRITE_MULTIPLE_BLOCK, SDMMC_CMD_GET_INDEX, and sd_mmc_card::type.

Referenced by main_test_memory().

bool sd_mmc_is_write_protected ( uint8_t  slot)

Get the card write protection status.

Parameters
slotCard slot
Returns
true, if write portected

References ioport_get_pin_level(), sd_mmc_cards, and UNUSED.

Referenced by main_test_memory(), and sd_mmc_init_write_blocks().

static bool sd_mmc_mci_install_mmc ( void  )
static

Initialize the MMC card in MCI mode.

Note
This function runs the initialization procedure and the identification process, then it sets the SD/MMC card in transfer state. At last, it will automaticly enable maximum bus width and transfer speed.
Returns
true if success, otherwise false

References CARD_VER_MMC_4, driver_get_bus_width, driver_is_high_speed_capable, driver_send_cmd, MMC_CMD3_SET_RELATIVE_ADDR, mmc_cmd6_set_bus_width(), mmc_cmd6_set_high_speed(), mmc_cmd8(), mmc_decode_csd(), mmc_mci_op_cond(), sd_mmc_card::rca, SD_MMC_BLOCK_SIZE, sd_mmc_cmd9_mci(), sd_mmc_configure_slot(), sd_mmc_slot_sel, SDMMC_CMD16_SET_BLOCKLEN, SDMMC_CMD2_ALL_SEND_CID, SDMMC_CMD7_SELECT_CARD_CMD, SDMMC_MCI_CMD0_GO_IDLE_STATE, and sd_mmc_card::version.

Referenced by sd_mmc_mci_card_init().

uint8_t sd_mmc_nb_slot ( void  )

Return the number of slot available.

Returns
Number of card slot available

References SD_MMC_MEM_CNT.

Referenced by main().

static bool sd_mmc_spi_card_init ( void  )
static

Initialize the SD card in SPI mode.

Note
This function runs the initialization procedure and the identification process, then it sets the SD/MMC card in transfer state. At last, it will automaticly enable maximum bus width and transfer speed.
Returns
true if success, otherwise false

References CARD_TYPE_HC, CARD_TYPE_MMC, CARD_TYPE_SD, CARD_VER_UNKNOWN, driver_send_clock, driver_send_cmd, IS_SDIO, sd_mmc_card::rca, sd_acmd51(), sd_cmd8(), sd_decode_csd(), SD_MMC_BLOCK_SIZE, sd_mmc_cmd13(), sd_mmc_cmd9_spi(), sd_mmc_configure_slot(), sd_mmc_debug, sd_mmc_spi_install_mmc(), sd_spi_op_cond(), sdio_get_max_speed(), sdio_op_cond(), SDMMC_CMD16_SET_BLOCKLEN, SDMMC_SPI_CMD0_GO_IDLE_STATE, SDMMC_SPI_CMD59_CRC_ON_OFF, sd_mmc_card::type, and sd_mmc_card::version.

Referenced by sd_mmc_check().

static bool sd_mmc_spi_install_mmc ( void  )
static

Initialize the MMC card in SPI mode.

Note
This function runs the initialization procedure and the identification process, then it sets the SD/MMC card in transfer state. At last, it will automaticly enable maximum bus width and transfer speed.
Returns
true if success, otherwise false

References CARD_VER_MMC_4, driver_send_cmd, mmc_cmd8(), mmc_decode_csd(), mmc_spi_op_cond(), SD_MMC_BLOCK_SIZE, sd_mmc_cmd13(), sd_mmc_cmd9_spi(), sd_mmc_configure_slot(), SDMMC_CMD16_SET_BLOCKLEN, SDMMC_SPI_CMD0_GO_IDLE_STATE, SDMMC_SPI_CMD59_CRC_ON_OFF, and sd_mmc_card::version.

Referenced by sd_mmc_spi_card_init().

sd_mmc_err_t sd_mmc_start_read_blocks ( void *  dest,
uint16_t  nb_block 
)

Start the read blocks of data from the card.

Parameters
destPointer to read buffer.
nb_blockNumber of blocks to be read.
Returns
return SD_MMC_OK if started, otherwise return an error code (sd_mmc_err_t).

References Assert, driver_start_read_blocks, SD_MMC_ERR_COMM, sd_mmc_nb_block_remaining, and SD_MMC_OK.

Referenced by main_test_memory().

sd_mmc_err_t sd_mmc_start_write_blocks ( const void *  src,
uint16_t  nb_block 
)

Start the write blocks of data.

Parameters
srcPointer to write buffer.
nb_blockNumber of blocks to be written.
Returns
return SD_MMC_OK if started, otherwise return an error code (sd_mmc_err_t).

References Assert, driver_start_write_blocks, SD_MMC_ERR_COMM, sd_mmc_nb_block_remaining, and SD_MMC_OK.

Referenced by main_test_memory().

sd_mmc_err_t sd_mmc_wait_end_of_read_blocks ( bool  abort)

Wait the end of read blocks of data from the card.

Parameters
abortAbort reading process initialized by sd_mmc_init_read_blocks() after the reading issued by sd_mmc_start_read_blocks() is done
Returns
return SD_MMC_OK if success, otherwise return an error code (sd_mmc_err_t).

References driver_adtc_stop, driver_wait_end_of_read_blocks, sd_mmc_deselect_slot(), SD_MMC_ERR_COMM, sd_mmc_nb_block_remaining, sd_mmc_nb_block_to_tranfer, SD_MMC_OK, and SDMMC_CMD12_STOP_TRANSMISSION.

Referenced by main_test_memory().

sd_mmc_err_t sd_mmc_wait_end_of_write_blocks ( bool  abort)

Wait the end of write blocks of data.

Parameters
abortAbort writing process initialized by sd_mmc_init_write_blocks() after the writing issued by sd_mmc_start_write_blocks() is done
Returns
return SD_MMC_OK if success, otherwise return an error code (sd_mmc_err_t).

References driver_adtc_stop, driver_wait_end_of_write_blocks, sd_mmc_deselect_slot(), SD_MMC_ERR_COMM, sd_mmc_is_mci, sd_mmc_nb_block_remaining, sd_mmc_nb_block_to_tranfer, SD_MMC_OK, and SDMMC_CMD12_STOP_TRANSMISSION.

Referenced by main_test_memory().

static bool sd_spi_op_cond ( uint8_t  v2)
static

Ask to all cards to send their operations conditions (SPI only).

  • ACMD41 sends operation condition command.
  • CMD58 reads OCR
Parameters
v2Shall be 1 if it is a SD card V2
Returns
true if success, otherwise false

References CARD_TYPE_HC, driver_get_response, driver_send_cmd, OCR_CCS, R1_SPI_IDLE, SD_ACMD41_HCS, sd_mmc_debug, SD_SPI_ACMD41_SD_SEND_OP_COND, SDMMC_CMD55_APP_CMD, SDMMC_SPI_CMD58_READ_OCR, and sd_mmc_card::type.

Referenced by sd_mmc_spi_card_init().

static bool sdio_cmd52 ( uint8_t  rw_flag,
uint8_t  func_nb,
uint32_t  reg_addr,
uint8_t  rd_after_wr,
uint8_t *  io_data 
)
static

CMD52 - SDIO IO_RW_DIRECT command.

Parameters
rw_flagDirection, 1:write, 0:read.
func_nbNumber of the function.
rd_after_wrRead after Write flag.
reg_addrregister address.
io_dataPointer to input argument and response buffer.
Returns
true if success, otherwise false

References Assert, driver_get_response, driver_send_cmd, SDIO_CMD52_FUNCTION_NUM, SDIO_CMD52_IO_RW_DIRECT, SDIO_CMD52_RAW_FLAG, SDIO_CMD52_REG_ADRR, SDIO_CMD52_RW_FLAG, and SDIO_CMD52_WR_DATA.

Referenced by sd_mmc_mci_card_init(), sdio_cmd52_set_bus_width(), sdio_cmd52_set_high_speed(), sdio_get_max_speed(), sdio_read_direct(), and sdio_write_direct().

static bool sdio_cmd52_set_bus_width ( void  )
static

CMD52 for SDIO - Switches the bus width mode to 4.

Note
sd_mmc_card->bus_width is updated.
Returns
true if success, otherwise false

A SD memory card always supports bus 4bit A SD COMBO card always supports bus 4bit A SDIO Full-Speed alone always supports 4bit A SDIO Low-Speed alone can supports 4bit (Optional)

References sd_mmc_card::bus_width, sd_mmc_debug, SDIO_BUSWIDTH_4B, SDIO_CAP_4BLS, SDIO_CCCR_BUS_CTRL, SDIO_CCCR_CAP, SDIO_CIA, sdio_cmd52(), SDIO_CMD52_READ_FLAG, and SDIO_CMD52_WRITE_FLAG.

Referenced by sd_mmc_mci_card_init().

static bool sdio_cmd52_set_high_speed ( void  )
static

CMD52 for SDIO - Enable the high speed mode.

Note
sd_mmc_card->high_speed is updated.
sd_mmc_card->clock is updated.
Returns
true if success, otherwise false

References sd_mmc_card::clock, sd_mmc_card::high_speed, SDIO_CCCR_HS, SDIO_CIA, sdio_cmd52(), SDIO_CMD52_READ_FLAG, SDIO_CMD52_WRITE_FLAG, SDIO_EHS, and SDIO_SHS.

Referenced by sd_mmc_mci_card_init().

static bool sdio_cmd53 ( uint8_t  rw_flag,
uint8_t  func_nb,
uint32_t  reg_addr,
uint8_t  inc_addr,
uint32_t  size,
bool  access_block 
)
static

CMD53 - SDIO IO_RW_EXTENDED command This implementation support only the SDIO multi-byte transfer mode which is similar to the single block transfer on memory.

Note: The SDIO block transfer mode is optional for SDIO card.

Parameters
rw_flagDirection, 1:write, 0:read.
func_nbNumber of the function.
reg_addrRegister address.
inc_addr1:Incrementing address, 0: fixed.
sizeTransfer data size.
access_blocktrue, if the block access (DMA) is used
Returns
true if success, otherwise false

References Assert, driver_adtc_start, SDIO_CMD53_BLOCK_MODE, SDIO_CMD53_COUNT, SDIO_CMD53_FUNCTION_NUM, SDIO_CMD53_IO_R_BYTE_EXTENDED, SDIO_CMD53_IO_W_BYTE_EXTENDED, SDIO_CMD53_OP_CODE, SDIO_CMD53_READ_FLAG, SDIO_CMD53_REG_ADDR, and SDIO_CMD53_RW_FLAG.

Referenced by sdio_read_extended(), and sdio_write_extended().

static bool sdio_get_max_speed ( void  )
static

Get SDIO max transfer speed in Hz.

  • CMD53 reads CIS area address in CCCR area.
  • Nx CMD53 search Fun0 tuple in CIS area
  • CMD53 reads TPLFE_MAX_TRAN_SPEED in Fun0 tuple
  • Compute maximum speed of SDIO and update sd_mmc_card->clock
Returns
true if success, otherwise false

Note: A combo card shall be a Full-Speed SDIO card which supports upto 25MHz. A SDIO card alone can be:

  • a Low-Speed SDIO card which supports 400Khz minimum
  • a Full-Speed SDIO card which supports upto 25MHz

References sd_mmc_card::clock, sd_mmc_trans_units, sd_trans_multipliers, SDIO_CCCR_CIS_PTR, SDIO_CIA, SDIO_CISTPL_END, SDIO_CISTPL_FUNCE, sdio_cmd52(), and SDIO_CMD52_READ_FLAG.

Referenced by sd_mmc_mci_card_init(), and sd_mmc_spi_card_init().

static bool sdio_op_cond ( void  )
static

Try to get the SDIO card's operating condition.

  • CMD5 to read OCR NF field
  • CMD5 to wait OCR power up busy
  • CMD5 to read OCR MP field sd_mmc_card->type is updated
Returns
true if success, otherwise false

References CARD_TYPE_SD_COMBO, CARD_TYPE_SDIO, driver_get_response, driver_send_cmd, OCR_POWER_UP_BUSY, OCR_SDIO_MP, OCR_SDIO_NF, sd_mmc_debug, SD_MMC_VOLTAGE_SUPPORT, SDIO_CMD5_SEND_OP_COND, and sd_mmc_card::type.

Referenced by sd_mmc_mci_card_init(), and sd_mmc_spi_card_init().

sd_mmc_err_t sdio_read_direct ( uint8_t  slot,
uint8_t  func_num,
uint32_t  addr,
uint8_t *  dest 
)

Read one byte from SDIO using RW_DIRECT command.

Parameters
slotCard slot to use
func_numFunction number.
addrRegister address to read from.
destPointer to read buffer.
Returns
return SD_MMC_OK if success, otherwise return an error code (sd_mmc_err_t).

References sd_mmc_deselect_slot(), SD_MMC_ERR_COMM, SD_MMC_ERR_PARAM, SD_MMC_OK, sd_mmc_select_slot(), sdio_cmd52(), and SDIO_CMD52_READ_FLAG.

Referenced by main_test_sdio().

sd_mmc_err_t sdio_read_extended ( uint8_t  slot,
uint8_t  func_num,
uint32_t  addr,
uint8_t  inc_addr,
uint8_t *  dest,
uint16_t  size 
)

Read bytes from SDIO using RW_EXTENDED command.

Parameters
slotCard slot to use
func_numFunction number.
addrFirst register address to read from.
inc_addr0 - The data address is fixed. 1 - The data address increase automatically.
destPointer to read buffer.
sizeNumber of bytes to read (1 ~ 512).
Returns
return SD_MMC_OK if success, otherwise return an error code (sd_mmc_err_t).

References driver_start_read_blocks, driver_wait_end_of_read_blocks, sd_mmc_deselect_slot(), SD_MMC_ERR_COMM, SD_MMC_ERR_PARAM, SD_MMC_OK, sd_mmc_select_slot(), sdio_cmd53(), and SDIO_CMD53_READ_FLAG.

Referenced by main_test_sdio().

sd_mmc_err_t sdio_write_direct ( uint8_t  slot,
uint8_t  func_num,
uint32_t  addr,
uint8_t  data 
)

Write one byte to SDIO using RW_DIRECT command.

Parameters
slotCard slot to use
func_numFunction number.
addrRegister address to read from.
dataData to be written.
Returns
return SD_MMC_OK if success, otherwise return an error code (sd_mmc_err_t).

References sd_mmc_deselect_slot(), SD_MMC_ERR_COMM, SD_MMC_OK, sd_mmc_select_slot(), sdio_cmd52(), and SDIO_CMD52_WRITE_FLAG.

Referenced by main_test_sdio().

sd_mmc_err_t sdio_write_extended ( uint8_t  slot,
uint8_t  func_num,
uint32_t  addr,
uint8_t  inc_addr,
uint8_t *  src,
uint16_t  size 
)

Write bytes to SDIO using RW_EXTENDED command.

Parameters
slotCard slot to use
func_numFunction number.
addrFirst register address to write to.
inc_addr0 - The data address is fixed. 1 - The data address increase automatically.
srcPointer to write buffer.
sizeNumber of bytes to read (1 ~ 512).
Returns
return SD_MMC_OK if success, otherwise return an error code (sd_mmc_err_t).

References driver_start_write_blocks, driver_wait_end_of_write_blocks, sd_mmc_deselect_slot(), SD_MMC_ERR_COMM, SD_MMC_ERR_PARAM, SD_MMC_OK, sd_mmc_select_slot(), sdio_cmd53(), and SDIO_CMD53_WRITE_FLAG.

Referenced by main_test_sdio().

const uint32_t mmc_trans_multipliers[16]
Initial value:
= {
0, 10, 12, 13, 15, 20, 26, 30, 35, 40, 45, 52, 55, 60, 70, 80
}

MMC transfer multiplier factor codes (1/10) list.

Referenced by mmc_decode_csd().

struct sd_mmc_card* sd_mmc_card
static

Pointer on current slot configurated.

struct sd_mmc_card sd_mmc_cards[SD_MMC_MEM_CNT]
static
Initial value:
= {
#define SD_MMC_CD(slot, unused)
}

SD/MMC card list Note: Initialize card detect pin fields if present.

Referenced by sd_mmc_is_write_protected(), and sd_mmc_select_slot().

uint16_t sd_mmc_nb_block_remaining = 0
static
uint16_t sd_mmc_nb_block_to_tranfer = 0
static

Number of block to read or write on the current transfer.

Referenced by sd_mmc_init_read_blocks(), sd_mmc_init_write_blocks(), sd_mmc_wait_end_of_read_blocks(), and sd_mmc_wait_end_of_write_blocks().

bool sd_mmc_sam_systick_used
static
uint8_t sd_mmc_slot_sel
static
const uint32_t sd_mmc_trans_units[7]
Initial value:
= {
10, 100, 1000, 10000, 0, 0, 0
}

SD/MMC transfer rate unit codes (10K) list.

Referenced by mmc_decode_csd(), sd_decode_csd(), and sdio_get_max_speed().

const uint32_t sd_trans_multipliers[16]
Initial value:
= {
0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
}

SD transfer multiplier factor codes (1/10) list.

Referenced by sd_decode_csd(), and sdio_get_max_speed().

t_cpu_time timer
static