Microchip® Advanced Software Framework

sd_mmc.c File Reference

Common SD/MMC stack.

Copyright (c) 2012-2018 Microchip Technology Inc. and its subsidiaries.

#include <string.h>
#include "conf_board.h"
#include "board.h"
#include "conf_sd_mmc.h"
#include "sd_mmc_protocol.h"
#include "sd_mmc.h"
#include "delay.h"
#include "ioport.h"
#include "sd_mmc_spi.h"
#include "pmc.h"

Data Structures

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

Macros

#define driver   sd_mmc_spi
 
#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_CD(slot, unused)   {.cd_gpio = SD_MMC_##slot##_CD_GPIO},
 
#define sd_mmc_debug(...)
 
#define SD_MMC_ENABLE_CD_PIN(slot, unused)   pmc_enable_periph_clk(SD_MMC_##slot##_CD_PIO_ID);
 
#define SD_MMC_ENABLE_WP_PIN(slot, unused)   pmc_enable_periph_clk(SD_MMC_##slot##_WP_PIO_ID);
 
#define SD_MMC_HSMCI_MEM_CNT   0
 
#define sd_mmc_is_mci()   (!sd_mmc_is_spi())
 
#define sd_mmc_is_spi()   true
 
#define SD_MMC_MCI_MEM_CNT   0
 
#define SD_MMC_MEM_CNT   SD_MMC_SPI_MEM_CNT
 
#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...
 
void sd_mmc_init (void)
 Initialize the SD/MMC stack and low level driver required. 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...
 
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...
 

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...
 

Internal functions to manage a large timeout after a card insertion

#define SD_MMC_DEBOUNCE_TIMEOUT   1000
 
#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_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()
 
static t_cpu_time timer
 
static bool sd_mmc_sam_systick_used
 

#define SD_MMC_CD (   slot,
  unused 
)    {.cd_gpio = SD_MMC_##slot##_CD_GPIO},
#define SD_MMC_ENABLE_CD_PIN (   slot,
  unused 
)    pmc_enable_periph_clk(SD_MMC_##slot##_CD_PIO_ID);

Referenced by sd_mmc_init().

#define SD_MMC_ENABLE_WP_PIN (   slot,
  unused 
)    pmc_enable_periph_clk(SD_MMC_##slot##_WP_PIO_ID);

Referenced by sd_mmc_init().