|
ChibiOS/HAL
6.1.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.
Macros | |
| #define | _mmc_driver_methods _mmcsd_block_device_methods |
MMCDriver specific methods. More... | |
MMC_SPI configuration options | |
| #define | MMC_NICE_WAITING TRUE |
| Delays insertions. More... | |
Macro Functions | |
| #define | mmcIsCardInserted(mmcp) mmc_lld_is_card_inserted(mmcp) |
| Returns the card insertion status. More... | |
| #define | mmcIsWriteProtected(mmcp) mmc_lld_is_write_protected(mmcp) |
| Returns the write protect status. More... | |
Data Structures | |
| struct | MMCConfig |
| MMC/SD over SPI driver configuration structure. More... | |
| struct | MMCDriverVMT |
MMCDriver virtual methods table. More... | |
| struct | MMCDriver |
| Structure representing a MMC/SD over SPI driver. More... | |
Functions | |
| static uint8_t | crc7 (uint8_t crc, const uint8_t *buffer, size_t len) |
| Calculate the MMC standard CRC-7 based on a lookup table. More... | |
| static void | wait (MMCDriver *mmcp) |
| Waits an idle condition. More... | |
| static void | send_hdr (MMCDriver *mmcp, uint8_t cmd, uint32_t arg) |
| Sends a command header. More... | |
| static uint8_t | recvr1 (MMCDriver *mmcp) |
| Receives a single byte response. More... | |
| static uint8_t | recvr3 (MMCDriver *mmcp, uint8_t *buffer) |
| Receives a three byte response. More... | |
| static uint8_t | send_command_R1 (MMCDriver *mmcp, uint8_t cmd, uint32_t arg) |
| Sends a command an returns a single byte response. More... | |
| static uint8_t | send_command_R3 (MMCDriver *mmcp, uint8_t cmd, uint32_t arg, uint8_t *response) |
| Sends a command which returns a five bytes response (R3). More... | |
| static bool | read_CxD (MMCDriver *mmcp, uint8_t cmd, uint32_t cxd[4]) |
| Reads the CSD. More... | |
| static void | sync (MMCDriver *mmcp) |
| Waits that the card reaches an idle state. More... | |
| void | mmcInit (void) |
| MMC over SPI driver initialization. More... | |
| void | mmcObjectInit (MMCDriver *mmcp) |
| Initializes an instance. More... | |
| void | mmcStart (MMCDriver *mmcp, const MMCConfig *config) |
| Configures and activates the MMC peripheral. More... | |
| void | mmcStop (MMCDriver *mmcp) |
| Disables the MMC peripheral. More... | |
| bool | mmcConnect (MMCDriver *mmcp) |
| Performs the initialization procedure on the inserted card. More... | |
| bool | mmcDisconnect (MMCDriver *mmcp) |
| Brings the driver in a state safe for card removal. More... | |
| bool | mmcStartSequentialRead (MMCDriver *mmcp, uint32_t startblk) |
| Starts a sequential read. More... | |
| bool | mmcSequentialRead (MMCDriver *mmcp, uint8_t *buffer) |
| Reads a block within a sequential read operation. More... | |
| bool | mmcStopSequentialRead (MMCDriver *mmcp) |
| Stops a sequential read gracefully. More... | |
| bool | mmcStartSequentialWrite (MMCDriver *mmcp, uint32_t startblk) |
| Starts a sequential write. More... | |
| bool | mmcSequentialWrite (MMCDriver *mmcp, const uint8_t *buffer) |
| Writes a block within a sequential write operation. More... | |
| bool | mmcStopSequentialWrite (MMCDriver *mmcp) |
| Stops a sequential write gracefully. More... | |
| bool | mmcSync (MMCDriver *mmcp) |
| Waits for card idle condition. More... | |
| bool | mmcGetInfo (MMCDriver *mmcp, BlockDeviceInfo *bdip) |
| Returns the media info. More... | |
| bool | mmcErase (MMCDriver *mmcp, uint32_t startblk, uint32_t endblk) |
| Erases blocks. More... | |
Variables | |
| static const struct MMCDriverVMT | mmc_vmt |
| Virtual methods table. More... | |
| static const uint8_t | crc7_lookup_table [256] |
| Lookup table for CRC-7 ( based on polynomial x^7 + x^3 + 1). More... | |
| #define MMC_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. This option is recommended also if the SPI driver does not use a DMA channel and heavily loads the CPU.
Definition at line 56 of file hal_mmc_spi.h.
| #define _mmc_driver_methods _mmcsd_block_device_methods |
MMCDriver specific methods.
Definition at line 93 of file hal_mmc_spi.h.
| #define mmcIsCardInserted | ( | mmcp | ) | mmc_lld_is_card_inserted(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 148 of file hal_mmc_spi.h.
| #define mmcIsWriteProtected | ( | mmcp | ) | mmc_lld_is_write_protected(mmcp) |
Returns the write protect status.
| [in] | mmcp | pointer to the MMCDriver object |
| false | card not inserted. |
| true | card inserted. |
Definition at line 160 of file hal_mmc_spi.h.
|
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 149 of file hal_mmc_spi.c.
References crc7_lookup_table.
Referenced by send_hdr().
|
static |
Waits an idle condition.
| [in] | mmcp | pointer to the MMCDriver object |
Definition at line 165 of file hal_mmc_spi.c.
References MMCDriver::config, osalThreadSleepMilliseconds, MMCConfig::spip, and spiReceive().
Referenced by mmcSequentialWrite(), and send_hdr().

|
static |
Sends a command header.
| [in] | mmcp | pointer to the MMCDriver object |
| [in] | cmd | the command id |
| [in] | arg | the command argument |
Definition at line 197 of file hal_mmc_spi.c.
References MMCDriver::config, crc7(), MMCConfig::spip, spiSend(), and wait().
Referenced by mmcStartSequentialRead(), mmcStartSequentialWrite(), read_CxD(), send_command_R1(), and send_command_R3().

|
static |
Receives a single byte response.
| [in] | mmcp | pointer to the MMCDriver object |
uint8_t value. | 0xFF | timed out. |
Definition at line 223 of file hal_mmc_spi.c.
References MMCDriver::config, MMCConfig::spip, and spiReceive().
Referenced by mmcStartSequentialRead(), mmcStartSequentialWrite(), mmcStopSequentialRead(), read_CxD(), recvr3(), and send_command_R1().

|
static |
Receives a three byte response.
| [in] | mmcp | pointer to the MMCDriver object |
| [out] | buffer | pointer to four bytes wide buffer |
uint8_t value. | 0xFF | timed out. |
Definition at line 246 of file hal_mmc_spi.c.
References MMCDriver::config, recvr1(), MMCConfig::spip, and spiReceive().
Referenced by 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 |
uint8_t value. | 0xFF | timed out. |
Definition at line 266 of file hal_mmc_spi.c.
References MMCDriver::config, recvr1(), send_hdr(), MMCConfig::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] | response | pointer to four bytes wide uint8_t buffer |
uint8_t value. | 0xFF | timed out. |
Definition at line 289 of file hal_mmc_spi.c.
References MMCDriver::config, recvr3(), send_hdr(), MMCConfig::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 | the operation succeeded. |
| HAL_FAILED | the operation failed. |
Definition at line 313 of file hal_mmc_spi.c.
References MMCDriver::config, recvr1(), send_hdr(), spiIgnore(), MMCConfig::spip, spiReceive(), spiSelect(), and spiUnselect().
Referenced by mmcConnect().

|
static |
Waits that the card reaches an idle state.
| [in] | mmcp | pointer to the MMCDriver object |
Definition at line 355 of file hal_mmc_spi.c.
References MMCDriver::config, osalThreadSleepMilliseconds, MMCConfig::spip, spiReceive(), spiSelect(), and spiUnselect().
Referenced by mmcDisconnect(), and mmcSync().

| void mmcInit | ( | void | ) |
MMC over SPI driver initialization.
halInit(), there is no need to explicitly initialize the driver.Definition at line 383 of file hal_mmc_spi.c.
Referenced by halInit().
| void mmcObjectInit | ( | MMCDriver * | mmcp | ) |
Initializes an instance.
| [out] | mmcp | pointer to the MMCDriver object |
Definition at line 394 of file hal_mmc_spi.c.
References BLK_STOP, MMCDriver::config, mmc_vmt, and MMCDriver::vmt.
Configures and activates the MMC peripheral.
Definition at line 410 of file hal_mmc_spi.c.
References BLK_ACTIVE, BLK_STOP, MMCDriver::config, osalDbgAssert, and osalDbgCheck.
| void mmcStop | ( | MMCDriver * | mmcp | ) |
Disables the MMC peripheral.
| [in] | mmcp | pointer to the MMCDriver object |
Definition at line 427 of file hal_mmc_spi.c.
References BLK_ACTIVE, BLK_STOP, MMCDriver::config, osalDbgAssert, osalDbgCheck, MMCConfig::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 | the operation succeeded and the driver is now in the MMC_READY state. |
| HAL_FAILED | the operation failed. |
Definition at line 455 of file hal_mmc_spi.c.
References _mmcsd_get_capacity(), BLK_ACTIVE, BLK_CONNECTING, BLK_READY, MMCDriver::config, MMCConfig::hscfg, MMCConfig::lscfg, MMCSD_BLOCK_SIZE, MMCSD_CMD8_PATTERN, osalDbgAssert, osalDbgCheck, osalThreadSleepMilliseconds, read_CxD(), send_command_R1(), send_command_R3(), spiIgnore(), MMCConfig::spip, spiStart(), and spiStop().

| bool mmcDisconnect | ( | MMCDriver * | mmcp | ) |
Brings the driver in a state safe for card removal.
| [in] | mmcp | pointer to the MMCDriver object |
| HAL_SUCCESS | the operation succeeded and the driver is now in the MMC_INSERTED state. |
| HAL_FAILED | the operation failed. |
Definition at line 577 of file hal_mmc_spi.c.
References BLK_ACTIVE, BLK_DISCONNECTING, BLK_READY, MMCDriver::config, MMCConfig::hscfg, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), MMCConfig::spip, spiStart(), spiStop(), and sync().

| 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 | the operation succeeded. |
| HAL_FAILED | the operation failed. |
Definition at line 612 of file hal_mmc_spi.c.
References BLK_READING, BLK_READY, MMCDriver::config, MMCConfig::hscfg, MMCSD_BLOCK_SIZE, osalDbgAssert, osalDbgCheck, recvr1(), send_hdr(), MMCConfig::spip, spiSelect(), spiStart(), and spiStop().

| 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 | the operation succeeded. |
| HAL_FAILED | the operation failed. |
Definition at line 652 of file hal_mmc_spi.c.
References BLK_READING, BLK_READY, MMCDriver::config, MMCSD_BLOCK_SIZE, osalDbgCheck, spiIgnore(), MMCConfig::spip, spiReceive(), spiStop(), and spiUnselect().

| bool mmcStopSequentialRead | ( | MMCDriver * | mmcp | ) |
Stops a sequential read gracefully.
| [in] | mmcp | pointer to the MMCDriver object |
| HAL_SUCCESS | the operation succeeded. |
| HAL_FAILED | the operation failed. |
Definition at line 688 of file hal_mmc_spi.c.
References BLK_READING, BLK_READY, MMCDriver::config, osalDbgCheck, recvr1(), MMCConfig::spip, spiSend(), and spiUnselect().

| 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 | the operation succeeded. |
| HAL_FAILED | the operation failed. |
Definition at line 722 of file hal_mmc_spi.c.
References BLK_READY, BLK_WRITING, MMCDriver::config, MMCConfig::hscfg, MMCSD_BLOCK_SIZE, osalDbgAssert, osalDbgCheck, recvr1(), send_hdr(), MMCConfig::spip, spiSelect(), spiStart(), and spiStop().

| 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 | the operation succeeded. |
| HAL_FAILED | the operation failed. |
Definition at line 760 of file hal_mmc_spi.c.
References BLK_READY, BLK_WRITING, MMCDriver::config, MMCSD_BLOCK_SIZE, osalDbgCheck, spiIgnore(), MMCConfig::spip, spiReceive(), spiSend(), spiStop(), spiUnselect(), and wait().

| bool mmcStopSequentialWrite | ( | MMCDriver * | mmcp | ) |
Stops a sequential write gracefully.
| [in] | mmcp | pointer to the MMCDriver object |
| HAL_SUCCESS | the operation succeeded. |
| HAL_FAILED | the operation failed. |
Definition at line 797 of file hal_mmc_spi.c.
References BLK_READY, BLK_WRITING, MMCDriver::config, osalDbgCheck, MMCConfig::spip, spiSend(), and spiUnselect().

| bool mmcSync | ( | MMCDriver * | mmcp | ) |
Waits for card idle condition.
| [in] | mmcp | pointer to the MMCDriver object |
| HAL_SUCCESS | the operation succeeded. |
| HAL_FAILED | the operation failed. |
Definition at line 825 of file hal_mmc_spi.c.
References BLK_READY, BLK_SYNCING, MMCDriver::config, MMCConfig::hscfg, osalDbgCheck, MMCConfig::spip, spiStart(), and 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 | the operation succeeded. |
| HAL_FAILED | the operation failed. |
Definition at line 856 of file hal_mmc_spi.c.
References BlockDeviceInfo::blk_num, BLK_READY, BlockDeviceInfo::blk_size, MMCSD_BLOCK_SIZE, and osalDbgCheck.
| 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 | the operation succeeded. |
| HAL_FAILED | the operation failed. |
Definition at line 883 of file hal_mmc_spi.c.
References BLK_READY, BLK_WRITING, MMCDriver::config, MMCSD_BLOCK_SIZE, osalDbgCheck, send_command_R1(), MMCConfig::spip, and spiStop().

|
static |
Virtual methods table.
Definition at line 55 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 70 of file hal_mmc_spi.c.
Referenced by crc7().