ChibiOS/HAL 9.0.0
|
Generic MMC driver. More...
Generic MMC driver.
This module implements a portable MMC/SD driver that uses a SPI driver as physical layer. Hot plugging and removal are supported through kernel events.
HAL_USE_MMC_SPI
and HAL_USE_SPI
options must be enabled in halconf.h
.This driver implements a state machine internally, see the Abstract I/O Block Device module documentation for details.
This driver allows to read or write single or multiple 512 bytes blocks on a SD Card.
MMC_SPI configuration options | |
#define | MMC_IDLE_TIMEOUT_MS 1000 |
Timeout before assuming a failure while waiting for card idle. | |
#define | MMC_USE_MUTUAL_EXCLUSION TRUE |
Mutual exclusion on the SPI bus. |
Macro Functions | |
#define | mmcIsCardInserted(mmcp) |
Returns the card insertion status. | |
#define | mmcIsWriteProtected(mmcp) |
Returns the write protect status. |
Data Structures | |
struct | mmc_spi_config_t |
Type of a MMC/SD over SPI driver configuration structure. More... | |
struct | mmc_spi_driver_vmt |
MMCDriver virtual methods table. More... | |
struct | mmc_spi_driver_t |
Structure representing a MMC/SD over SPI driver. More... |
Macros | |
#define | MMC_CMD0_RETRY 10U |
#define | MMC_CMD1_RETRY 100U |
#define | MMC_ACMD41_RETRY 100U |
#define | MMC_WAIT_DATA 10000U |
#define | MMC_BUFFER_SIZE 16U |
Size of the buffer to be supplied to the driver. | |
#define | __mmc_driver_methods _mmcsd_block_device_methods |
MMCDriver specific methods. |
Typedefs | |
typedef mmc_spi_config_t | MMCConfig |
Legacy name for compatibility. | |
typedef mmc_spi_driver_t | MMCDriver |
Legacy name for compatibility. |
Functions | |
static bool | mmc_is_card_inserted (void *instance) |
static bool | mmc_is_write_protected (void *instance) |
static bool | mmc_connect (void *instance) |
static bool | mmc_disconnect (void *instance) |
static bool | mmc_read (void *instance, uint32_t startblk, uint8_t *buffer, uint32_t n) |
static bool | mmc_write (void *instance, uint32_t startblk, const uint8_t *buffer, uint32_t n) |
static bool | mmc_sync (void *instance) |
static bool | mmc_get_info (void *instance, BlockDeviceInfo *bdip) |
static uint8_t | mmc_crc7 (uint8_t crc, const uint8_t *buffer, size_t len) |
Calculate the MMC standard CRC-7 based on a lookup table. | |
static bool | mmc_wait_idle (MMCDriver *mmcp) |
Waits an idle condition. | |
static bool | mmc_send_hdr (MMCDriver *mmcp, uint8_t cmd, uint32_t arg) |
Sends a command header. | |
static bool | mmc_recvr1 (MMCDriver *mmcp, uint8_t *r1p) |
Receives a single byte response. | |
static bool | mmc_recvr3 (MMCDriver *mmcp, uint8_t *r1p) |
Receives a three byte response. | |
static bool | mmc_send_command_R1 (MMCDriver *mmcp, uint8_t cmd, uint32_t arg, uint8_t *r1p) |
Sends a command an returns a single byte response. | |
static bool | mmc_send_command_R3 (MMCDriver *mmcp, uint8_t cmd, uint32_t arg, uint8_t *r1p) |
Sends a command which returns a five bytes response (R3). | |
static bool | mmc_read_CxD (MMCDriver *mmcp, uint8_t cmd, uint32_t cxd[4]) |
Reads the CSD. | |
void | mmcInit (void) |
MMC over SPI driver initialization. | |
void | mmcObjectInit (MMCDriver *mmcp, uint8_t *buffer) |
Initializes an instance. | |
msg_t | mmcStart (MMCDriver *mmcp, const MMCConfig *config) |
Configures and activates the MMC peripheral. | |
void | mmcStop (MMCDriver *mmcp) |
Disables the MMC peripheral. | |
bool | mmcConnect (MMCDriver *mmcp) |
Performs the initialization procedure on the inserted card. | |
bool | mmcDisconnect (MMCDriver *mmcp) |
Brings the driver in a state safe for card removal. | |
bool | mmcStartSequentialRead (MMCDriver *mmcp, uint32_t startblk) |
Starts a sequential read. | |
bool | mmcSequentialRead (MMCDriver *mmcp, uint8_t *buffer) |
Reads a block within a sequential read operation. | |
bool | mmcStopSequentialRead (MMCDriver *mmcp) |
Stops a sequential read gracefully. | |
bool | mmcStartSequentialWrite (MMCDriver *mmcp, uint32_t startblk) |
Starts a sequential write. | |
bool | mmcSequentialWrite (MMCDriver *mmcp, const uint8_t *buffer) |
Writes a block within a sequential write operation. | |
bool | mmcStopSequentialWrite (MMCDriver *mmcp) |
Stops a sequential write gracefully. | |
bool | mmcSync (MMCDriver *mmcp) |
Waits for card idle condition. | |
bool | mmcGetInfo (MMCDriver *mmcp, BlockDeviceInfo *bdip) |
Returns the media info. | |
bool | mmcErase (MMCDriver *mmcp, uint32_t startblk, uint32_t endblk) |
Erases blocks. | |
bool | mmc_lld_is_card_inserted (MMCDriver *mmcp) |
bool | mmc_lld_is_write_protected (MMCDriver *mmcp) |
Variables | |
static const struct mmc_spi_driver_vmt | mmc_vmt |
Virtual methods table. | |
static const uint8_t | mmc_crc7_lookup_table [256] |
Lookup table for CRC-7 ( based on polynomial x^7 + x^3 + 1). |
#define MMC_CMD0_RETRY 10U |
Definition at line 34 of file hal_mmc_spi.h.
Referenced by mmcConnect().
#define MMC_CMD1_RETRY 100U |
Definition at line 35 of file hal_mmc_spi.h.
Referenced by mmcConnect().
#define MMC_ACMD41_RETRY 100U |
Definition at line 36 of file hal_mmc_spi.h.
Referenced by mmcConnect().
#define MMC_WAIT_DATA 10000U |
Definition at line 37 of file hal_mmc_spi.h.
Referenced by mmc_read_CxD(), and mmcSequentialRead().
#define MMC_BUFFER_SIZE 16U |
Size of the buffer to be supplied to the driver.
Definition at line 44 of file hal_mmc_spi.h.
#define MMC_IDLE_TIMEOUT_MS 1000 |
Timeout before assuming a failure while waiting for card idle.
Definition at line 59 of file hal_mmc_spi.h.
Referenced by mmc_wait_idle().
#define MMC_USE_MUTUAL_EXCLUSION TRUE |
Mutual exclusion on the SPI bus.
Definition at line 66 of file hal_mmc_spi.h.
#define __mmc_driver_methods _mmcsd_block_device_methods |
MMCDriver
specific methods.
Definition at line 113 of file hal_mmc_spi.h.
#define mmcIsCardInserted | ( | mmcp | ) |
Returns the card insertion status.
sdc_lld_is_card_inserted()
, this function must be provided by the application because it is not part of the SDC driver.[in] | mmcp | pointer to the MMCDriver object |
false | card not inserted. |
true | card inserted. |
Definition at line 178 of file hal_mmc_spi.h.
Referenced by mmc_is_card_inserted().
#define mmcIsWriteProtected | ( | mmcp | ) |
Returns the write protect status.
[in] | mmcp | pointer to the MMCDriver object |
false | card not inserted. |
true | card inserted. |
Definition at line 190 of file hal_mmc_spi.h.
Referenced by mmc_is_write_protected().
typedef mmc_spi_config_t MMCConfig |
typedef mmc_spi_driver_t MMCDriver |
|
static |
Definition at line 105 of file hal_mmc_spi.c.
References mmcIsCardInserted.
|
static |
Definition at line 114 of file hal_mmc_spi.c.
References mmcIsWriteProtected.
|
static |
Definition at line 123 of file hal_mmc_spi.c.
References mmc_spi_driver_t::config, mmcConnect(), spiAcquireBus(), mmc_spi_config_t::spip, and spiReleaseBus().
|
static |
Definition at line 140 of file hal_mmc_spi.c.
References mmc_spi_driver_t::config, mmcDisconnect(), spiAcquireBus(), mmc_spi_config_t::spip, and spiReleaseBus().
|
static |
Definition at line 157 of file hal_mmc_spi.c.
References mmc_spi_driver_t::config, HAL_FAILED, HAL_SUCCESS, MMCSD_BLOCK_SIZE, mmcSequentialRead(), mmcStartSequentialRead(), mmcStopSequentialRead(), spiAcquireBus(), mmc_spi_config_t::spip, and spiReleaseBus().
|
static |
Definition at line 193 of file hal_mmc_spi.c.
References mmc_spi_driver_t::config, HAL_FAILED, HAL_SUCCESS, MMCSD_BLOCK_SIZE, mmcSequentialWrite(), mmcStartSequentialWrite(), mmcStopSequentialWrite(), spiAcquireBus(), mmc_spi_config_t::spip, and spiReleaseBus().
|
static |
Definition at line 229 of file hal_mmc_spi.c.
References mmc_spi_driver_t::config, mmcSync(), spiAcquireBus(), mmc_spi_config_t::spip, and spiReleaseBus().
|
static |
Definition at line 246 of file hal_mmc_spi.c.
References mmcGetInfo().
|
static |
Calculate the MMC standard CRC-7 based on a lookup table.
[in] | crc | start value for CRC |
[in] | buffer | pointer to data buffer |
[in] | len | length of data |
Definition at line 263 of file hal_mmc_spi.c.
References mmc_crc7_lookup_table.
Referenced by mmc_send_hdr().
|
static |
Waits an idle condition.
[in] | mmcp | pointer to the MMCDriver object |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 282 of file hal_mmc_spi.c.
References mmc_spi_driver_t::buffer, mmc_spi_driver_t::config, HAL_FAILED, HAL_SUCCESS, MMC_IDLE_TIMEOUT_MS, osalThreadSleepMilliseconds, mmc_spi_config_t::spip, and spiReceive().
Referenced by mmc_send_hdr(), mmcDisconnect(), mmcSequentialWrite(), and mmcSync().
|
static |
Sends a command header.
[in] | mmcp | pointer to the MMCDriver object |
[in] | cmd | the command id |
[in] | arg | the command argument |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 319 of file hal_mmc_spi.c.
References mmc_spi_driver_t::buffer, mmc_spi_driver_t::config, HAL_FAILED, HAL_SUCCESS, mmc_crc7(), mmc_wait_idle(), mmc_spi_config_t::spip, and spiSend().
Referenced by mmc_read_CxD(), mmc_send_command_R1(), mmc_send_command_R3(), mmcStartSequentialRead(), and mmcStartSequentialWrite().
|
static |
Receives a single byte response.
[in] | mmcp | pointer to the MMCDriver object |
[out] | r1p | pointer to buffer for R1 answer |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 350 of file hal_mmc_spi.c.
References mmc_spi_driver_t::buffer, mmc_spi_driver_t::config, HAL_FAILED, HAL_SUCCESS, mmc_spi_config_t::spip, and spiReceive().
Referenced by mmc_read_CxD(), mmc_recvr3(), mmc_send_command_R1(), mmcStartSequentialRead(), mmcStartSequentialWrite(), and mmcStopSequentialRead().
|
static |
Receives a three byte response.
[in] | mmcp | pointer to the MMCDriver object |
[out] | r1p | pointer to buffer for R1 answer |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 374 of file hal_mmc_spi.c.
References mmc_spi_driver_t::buffer, mmc_spi_driver_t::config, mmc_recvr1(), mmc_spi_config_t::spip, and spiReceive().
Referenced by mmc_send_command_R3().
|
static |
Sends a command an returns a single byte response.
[in] | mmcp | pointer to the MMCDriver object |
[in] | cmd | the command id |
[in] | arg | the command argument |
[out] | r1p | pointer to buffer for R1 answer |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 396 of file hal_mmc_spi.c.
References mmc_spi_driver_t::config, mmc_recvr1(), mmc_send_hdr(), mmc_spi_config_t::spip, spiSelect(), and spiUnselect().
Referenced by mmcConnect(), and mmcErase().
|
static |
Sends a command which returns a five bytes response (R3).
[in] | mmcp | pointer to the MMCDriver object |
[in] | cmd | the command id |
[in] | arg | the command argument |
[out] | r1p | pointer to buffer for R1 answer |
uint8_t
value. 0xFF | timed out. |
Definition at line 423 of file hal_mmc_spi.c.
References mmc_spi_driver_t::config, mmc_recvr3(), mmc_send_hdr(), mmc_spi_config_t::spip, spiSelect(), and spiUnselect().
Referenced by mmcConnect().
|
static |
Reads the CSD.
[in] | mmcp | pointer to the MMCDriver object |
[out] | cmd | command |
[out] | cxd | pointer to the CSD/CID buffer |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 450 of file hal_mmc_spi.c.
References mmc_spi_driver_t::buffer, mmc_spi_driver_t::config, HAL_FAILED, HAL_SUCCESS, mmc_recvr1(), mmc_send_hdr(), MMC_WAIT_DATA, spiIgnore(), mmc_spi_config_t::spip, spiReceive(), spiSelect(), and spiUnselect().
Referenced by mmcConnect().
void mmcInit | ( | void | ) |
MMC over SPI driver initialization.
halInit()
, there is no need to explicitly initialize the driver.Definition at line 504 of file hal_mmc_spi.c.
Referenced by halInit().
void mmcObjectInit | ( | MMCDriver * | mmcp, |
uint8_t * | buffer ) |
Initializes an instance.
[out] | mmcp | pointer to the MMCDriver object |
[in] | buffer | operations buffer |
Definition at line 516 of file hal_mmc_spi.c.
References BLK_STOP, mmc_spi_driver_t::block_addresses, mmc_spi_driver_t::buffer, mmc_spi_driver_t::config, mmc_vmt, and mmc_spi_driver_t::vmt.
Configures and activates the MMC peripheral.
Definition at line 534 of file hal_mmc_spi.c.
References BLK_ACTIVE, BLK_STOP, mmc_spi_driver_t::config, HAL_RET_SUCCESS, osalDbgAssert, and osalDbgCheck.
void mmcStop | ( | MMCDriver * | mmcp | ) |
Disables the MMC peripheral.
[in] | mmcp | pointer to the MMCDriver object |
Definition at line 553 of file hal_mmc_spi.c.
References BLK_ACTIVE, BLK_STOP, mmc_spi_driver_t::config, osalDbgAssert, osalDbgCheck, mmc_spi_config_t::spip, and spiStop().
bool mmcConnect | ( | MMCDriver * | mmcp | ) |
Performs the initialization procedure on the inserted card.
This function should be invoked when a card is inserted and brings the driver in the MMC_READY
state where it is possible to perform read and write operations.
[in] | mmcp | pointer to the MMCDriver object |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 581 of file hal_mmc_spi.c.
References _mmcsd_get_capacity(), BLK_ACTIVE, BLK_CONNECTING, BLK_READY, mmc_spi_driver_t::block_addresses, mmc_spi_driver_t::buffer, mmc_spi_driver_t::config, HAL_FAILED, HAL_SUCCESS, mmc_spi_config_t::hscfg, mmc_spi_config_t::lscfg, MMC_ACMD41_RETRY, MMC_CMD0_RETRY, MMC_CMD1_RETRY, mmc_read_CxD(), mmc_send_command_R1(), mmc_send_command_R3(), MMCSD_BLOCK_SIZE, MMCSD_CMD8_PATTERN, MMCSD_CMD_APP_CMD, MMCSD_CMD_APP_OP_COND, MMCSD_CMD_GO_IDLE_STATE, MMCSD_CMD_INIT, MMCSD_CMD_READ_OCR, MMCSD_CMD_SEND_CID, MMCSD_CMD_SEND_CSD, MMCSD_CMD_SEND_IF_COND, MMCSD_CMD_SET_BLOCKLEN, osalDbgAssert, osalDbgCheck, osalThreadSleepMilliseconds, spiIgnore(), mmc_spi_config_t::spip, spiStart(), and spiStop().
Referenced by mmc_connect().
bool mmcDisconnect | ( | MMCDriver * | mmcp | ) |
Brings the driver in a state safe for card removal.
[in] | mmcp | pointer to the MMCDriver object |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 719 of file hal_mmc_spi.c.
References BLK_ACTIVE, BLK_DISCONNECTING, BLK_READY, mmc_spi_driver_t::config, HAL_SUCCESS, mmc_spi_config_t::hscfg, mmc_wait_idle(), osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), mmc_spi_config_t::spip, spiSelect(), spiStart(), spiStop(), and spiUnselect().
Referenced by mmc_disconnect().
bool mmcStartSequentialRead | ( | MMCDriver * | mmcp, |
uint32_t | startblk ) |
Starts a sequential read.
[in] | mmcp | pointer to the MMCDriver object |
[in] | startblk | first block to read |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 764 of file hal_mmc_spi.c.
References BLK_READING, BLK_READY, mmc_spi_driver_t::block_addresses, mmc_spi_driver_t::config, HAL_FAILED, HAL_SUCCESS, mmc_spi_config_t::hscfg, mmc_recvr1(), mmc_send_hdr(), MMCSD_BLOCK_SIZE, MMCSD_CMD_READ_MULTIPLE_BLOCK, osalDbgAssert, osalDbgCheck, mmc_spi_config_t::spip, spiSelect(), spiStart(), and spiUnselect().
Referenced by mmc_read().
bool mmcSequentialRead | ( | MMCDriver * | mmcp, |
uint8_t * | buffer ) |
Reads a block within a sequential read operation.
[in] | mmcp | pointer to the MMCDriver object |
[out] | buffer | pointer to the read buffer |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 817 of file hal_mmc_spi.c.
References BLK_READING, BLK_READY, mmc_spi_driver_t::buffer, mmc_spi_driver_t::config, HAL_FAILED, HAL_SUCCESS, MMC_WAIT_DATA, MMCSD_BLOCK_SIZE, osalDbgCheck, spiIgnore(), mmc_spi_config_t::spip, spiReceive(), and spiUnselect().
Referenced by mmc_read().
bool mmcStopSequentialRead | ( | MMCDriver * | mmcp | ) |
Stops a sequential read gracefully.
[in] | mmcp | pointer to the MMCDriver object |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 855 of file hal_mmc_spi.c.
References BLK_READING, BLK_READY, mmc_spi_driver_t::config, HAL_FAILED, HAL_SUCCESS, mmc_recvr1(), MMCSD_CMD_STOP_TRANSMISSION, osalDbgCheck, mmc_spi_config_t::spip, spiSend(), and spiUnselect().
Referenced by mmc_read().
bool mmcStartSequentialWrite | ( | MMCDriver * | mmcp, |
uint32_t | startblk ) |
Starts a sequential write.
[in] | mmcp | pointer to the MMCDriver object |
[in] | startblk | first block to write |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 893 of file hal_mmc_spi.c.
References BLK_READY, BLK_WRITING, mmc_spi_driver_t::block_addresses, mmc_spi_driver_t::config, HAL_FAILED, HAL_SUCCESS, mmc_spi_config_t::hscfg, mmc_recvr1(), mmc_send_hdr(), MMCSD_BLOCK_SIZE, MMCSD_CMD_WRITE_MULTIPLE_BLOCK, osalDbgAssert, osalDbgCheck, mmc_spi_config_t::spip, spiSelect(), spiStart(), and spiUnselect().
Referenced by mmc_write().
bool mmcSequentialWrite | ( | MMCDriver * | mmcp, |
const uint8_t * | buffer ) |
Writes a block within a sequential write operation.
[in] | mmcp | pointer to the MMCDriver object |
[out] | buffer | pointer to the write buffer |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 942 of file hal_mmc_spi.c.
References BLK_READY, BLK_WRITING, mmc_spi_driver_t::buffer, mmc_spi_driver_t::config, HAL_FAILED, mmc_wait_idle(), MMCSD_BLOCK_SIZE, osalDbgCheck, spiIgnore(), mmc_spi_config_t::spip, spiReceive(), spiSend(), and spiUnselect().
Referenced by mmc_write().
bool mmcStopSequentialWrite | ( | MMCDriver * | mmcp | ) |
Stops a sequential write gracefully.
[in] | mmcp | pointer to the MMCDriver object |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 978 of file hal_mmc_spi.c.
References BLK_READY, BLK_WRITING, mmc_spi_driver_t::config, HAL_FAILED, HAL_SUCCESS, osalDbgCheck, mmc_spi_config_t::spip, spiSend(), and spiUnselect().
Referenced by mmc_write().
bool mmcSync | ( | MMCDriver * | mmcp | ) |
Waits for card idle condition.
[in] | mmcp | pointer to the MMCDriver object |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 1007 of file hal_mmc_spi.c.
References BLK_READY, BLK_SYNCING, mmc_spi_driver_t::config, HAL_FAILED, mmc_spi_config_t::hscfg, mmc_wait_idle(), osalDbgCheck, mmc_spi_config_t::spip, spiSelect(), spiStart(), and spiUnselect().
Referenced by mmc_sync().
bool mmcGetInfo | ( | MMCDriver * | mmcp, |
BlockDeviceInfo * | bdip ) |
Returns the media info.
[in] | mmcp | pointer to the MMCDriver object |
[out] | bdip | pointer to a BlockDeviceInfo structure |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 1044 of file hal_mmc_spi.c.
References BlockDeviceInfo::blk_num, BLK_READY, BlockDeviceInfo::blk_size, HAL_FAILED, HAL_SUCCESS, MMCSD_BLOCK_SIZE, and osalDbgCheck.
Referenced by mmc_get_info().
bool mmcErase | ( | MMCDriver * | mmcp, |
uint32_t | startblk, | ||
uint32_t | endblk ) |
Erases blocks.
[in] | mmcp | pointer to the MMCDriver object |
[in] | startblk | starting block number |
[in] | endblk | ending block number |
HAL_SUCCESS | if the operation succeeded. |
HAL_FAILED | if the operation failed. |
Definition at line 1071 of file hal_mmc_spi.c.
References BLK_READY, BLK_WRITING, mmc_spi_driver_t::block_addresses, HAL_FAILED, HAL_SUCCESS, mmc_send_command_R1(), MMCSD_BLOCK_SIZE, MMCSD_CMD_ERASE, MMCSD_CMD_ERASE_RW_BLK_END, MMCSD_CMD_ERASE_RW_BLK_START, and osalDbgCheck.
bool mmc_lld_is_card_inserted | ( | MMCDriver * | mmcp | ) |
bool mmc_lld_is_write_protected | ( | MMCDriver * | mmcp | ) |
|
static |
Virtual methods table.
Definition at line 61 of file hal_mmc_spi.c.
Referenced by mmcObjectInit().
|
static |
Lookup table for CRC-7 ( based on polynomial x^7 + x^3 + 1).
Definition at line 76 of file hal_mmc_spi.c.
Referenced by mmc_crc7().