Ported MMC_SPI.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/branches/kernel_3_dev@6145 35acf78f-673a-0410-8e92-d51de3d6d3f4
This commit is contained in:
gdisirio 2013-08-12 16:44:22 +00:00
parent a840a0d418
commit 60c6d531d4
8 changed files with 1741 additions and 4 deletions

View File

@ -90,7 +90,7 @@
* @brief Enables the MMC_SPI subsystem.
*/
#if !defined(HAL_USE_MMC_SPI) || defined(__DOXYGEN__)
#define HAL_USE_MMC_SPI FALSE
#define HAL_USE_MMC_SPI TRUE
#endif
/**

View File

@ -2,11 +2,13 @@
# from this list, you can disable parts of the kernel by editing halconf.h.
HALSRC = ${CHIBIOS}/os/hal/src/hal.c \
${CHIBIOS}/os/hal/src/hal_queues.c \
${CHIBIOS}/os/hal/src/hal_mmcsd.c \
${CHIBIOS}/os/hal/src/adc.c \
${CHIBIOS}/os/hal/src/can.c \
${CHIBIOS}/os/hal/src/ext.c \
${CHIBIOS}/os/hal/src/gpt.c \
${CHIBIOS}/os/hal/src/icu.c \
${CHIBIOS}/os/hal/src/mmc_spi.c \
${CHIBIOS}/os/hal/src/pal.c \
${CHIBIOS}/os/hal/src/pwm.c \
${CHIBIOS}/os/hal/src/serial.c \

View File

@ -38,8 +38,8 @@
/* Abstract interfaces.*/
#include "hal_streams.h"
#include "hal_channels.h"
//#include "io_block.h"
//#include "mmcsd.h"
#include "hal_ioblock.h"
#include "hal_mmcsd.h"
/* Shared headers.*/
#include "hal_queues.h"
@ -63,7 +63,7 @@
//#include "usb.h"
/* Complex drivers.*/
//#include "mmc_spi.h"
#include "mmc_spi.h"
//#include "serial_usb.h"
/*===========================================================================*/

View File

@ -0,0 +1,269 @@
/*
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
2011,2012,2013 Giovanni Di Sirio.
This file is part of ChibiOS/RT.
ChibiOS/RT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
ChibiOS/RT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file hal_ioblock.h
* @brief I/O block devices access.
* @details This header defines an abstract interface useful to access generic
* I/O block devices in a standardized way.
*
* @addtogroup IO_BLOCK
* @details This module defines an abstract interface for accessing generic
* block devices.<br>
* Note that no code is present, just abstract interfaces-like
* structures, you should look at the system as to a set of
* abstract C++ classes (even if written in C). This system
* has then advantage to make the access to block devices
* independent from the implementation logic.
* @{
*/
#ifndef _HAL_IOBLOCK_H_
#define _HAL_IOBLOCK_H_
/**
* @brief Driver state machine possible states.
*/
typedef enum {
BLK_UNINIT = 0, /**< Not initialized. */
BLK_STOP = 1, /**< Stopped. */
BLK_ACTIVE = 2, /**< Interface active. */
BLK_CONNECTING = 3, /**< Connection in progress. */
BLK_DISCONNECTING = 4, /**< Disconnection in progress. */
BLK_READY = 5, /**< Device ready. */
BLK_READING = 6, /**< Read operation in progress. */
BLK_WRITING = 7, /**< Write operation in progress. */
BLK_SYNCING = 8 /**< Sync. operation in progress. */
} blkstate_t;
/**
* @brief Block device info.
*/
typedef struct {
uint32_t blk_size; /**< @brief Block size in bytes. */
uint32_t blk_num; /**< @brief Total number of blocks. */
} BlockDeviceInfo;
/**
* @brief @p BaseBlockDevice specific methods.
*/
#define _base_block_device_methods \
/* Removable media detection.*/ \
bool (*is_inserted)(void *instance); \
/* Removable write protection detection.*/ \
bool (*is_protected)(void *instance); \
/* Connection to the block device.*/ \
bool (*connect)(void *instance); \
/* Disconnection from the block device.*/ \
bool (*disconnect)(void *instance); \
/* Reads one or more blocks.*/ \
bool (*read)(void *instance, uint32_t startblk, \
uint8_t *buffer, uint32_t n); \
/* Writes one or more blocks.*/ \
bool (*write)(void *instance, uint32_t startblk, \
const uint8_t *buffer, uint32_t n); \
/* Write operations synchronization.*/ \
bool (*sync)(void *instance); \
/* Obtains info about the media.*/ \
bool (*get_info)(void *instance, BlockDeviceInfo *bdip);
/**
* @brief @p BaseBlockDevice specific data.
*/
#define _base_block_device_data \
/* Driver state.*/ \
blkstate_t state;
/**
* @brief @p BaseBlockDevice virtual methods table.
*/
struct BaseBlockDeviceVMT {
_base_block_device_methods
};
/**
* @brief Base block device class.
* @details This class represents a generic, block-accessible, device.
*/
typedef struct {
/** @brief Virtual Methods Table.*/
const struct BaseBlockDeviceVMT *vmt;
_base_block_device_data
} BaseBlockDevice;
/**
* @name Macro Functions (BaseBlockDevice)
* @{
*/
/**
* @brief Returns the driver state.
* @note Can be called in ISR context.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
*
* @return The driver state.
*
* @special
*/
#define blkGetDriverState(ip) ((ip)->state)
/**
* @brief Determines if the device is transferring data.
* @note Can be called in ISR context.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
*
* @return The driver state.
* @retval FALSE the device is not transferring data.
* @retval TRUE the device not transferring data.
*
* @special
*/
#define blkIsTransferring(ip) ((((ip)->state) == BLK_CONNECTING) || \
(((ip)->state) == BLK_DISCONNECTING) || \
(((ip)->state) == BLK_READING) || \
(((ip)->state) == BLK_WRITING))
/**
* @brief Returns the media insertion status.
* @note On some implementations this function can only be called if the
* device is not transferring data.
* The function @p blkIsTransferring() should be used before calling
* this function.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
*
* @return The media state.
* @retval FALSE media not inserted.
* @retval TRUE media inserted.
*
* @api
*/
#define blkIsInserted(ip) ((ip)->vmt->is_inserted(ip))
/**
* @brief Returns the media write protection status.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
*
* @return The media state.
* @retval FALSE writable media.
* @retval TRUE non writable media.
*
* @api
*/
#define blkIsWriteProtected(ip) ((ip)->vmt->is_protected(ip))
/**
* @brief Performs the initialization procedure on the block device.
* @details This function should be performed before I/O operations can be
* attempted on the block device and after insertion has been
* confirmed using @p blkIsInserted().
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
*
* @return The operation status.
* @retval CH_SUCCESS operation succeeded.
* @retval CH_FAILED operation failed.
*
* @api
*/
#define blkConnect(ip) ((ip)->vmt->connect(ip))
/**
* @brief Terminates operations on the block device.
* @details This operation safely terminates operations on the block device.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
*
* @return The operation status.
* @retval CH_SUCCESS operation succeeded.
* @retval CH_FAILED operation failed.
*
* @api
*/
#define blkDisconnect(ip) ((ip)->vmt->disconnect(ip))
/**
* @brief Reads one or more blocks.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
* @param[in] startblk first block to read
* @param[out] buf pointer to the read buffer
* @param[in] n number of blocks to read
*
* @return The operation status.
* @retval CH_SUCCESS operation succeeded.
* @retval CH_FAILED operation failed.
*
* @api
*/
#define blkRead(ip, startblk, buf, n) \
((ip)->vmt->read(ip, startblk, buf, n))
/**
* @brief Writes one or more blocks.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
* @param[in] startblk first block to write
* @param[out] buf pointer to the write buffer
* @param[in] n number of blocks to write
*
* @return The operation status.
* @retval CH_SUCCESS operation succeeded.
* @retval CH_FAILED operation failed.
*
* @api
*/
#define blkWrite(ip, startblk, buf, n) \
((ip)->vmt->write(ip, startblk, buf, n))
/**
* @brief Ensures write synchronization.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
*
* @return The operation status.
* @retval CH_SUCCESS operation succeeded.
* @retval CH_FAILED operation failed.
*
* @api
*/
#define blkSync(ip) ((ip)->vmt->sync(ip))
/**
* @brief Returns a media information structure.
*
* @param[in] ip pointer to a @p BaseBlockDevice or derived class
* @param[out] bdip pointer to a @p BlockDeviceInfo structure
*
* @return The operation status.
* @retval CH_SUCCESS operation succeeded.
* @retval CH_FAILED operation failed.
*
* @api
*/
#define blkGetInfo(ip, bdip) ((ip)->vmt->get_info(ip, bdip))
/** @} */
#endif /* _HAL_IOBLOCK_H_ */
/** @} */

279
os/hal/include/hal_mmcsd.h Normal file
View File

@ -0,0 +1,279 @@
/*
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
2011,2012,2013 Giovanni Di Sirio.
This file is part of ChibiOS/RT.
ChibiOS/RT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
ChibiOS/RT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file hal_mmcsd.h
* @brief MMC/SD cards common header.
* @details This header defines an abstract interface useful to access MMC/SD
* I/O block devices in a standardized way.
*
* @addtogroup MMCSD
* @{
*/
#ifndef _HAL_MMCSD_H_
#define _HAL_MMCSD_H_
#if HAL_USE_MMC_SPI || HAL_USE_SDC || defined(__DOXYGEN__)
/*===========================================================================*/
/* Driver constants. */
/*===========================================================================*/
/**
* @brief Fixed block size for MMC/SD block devices.
*/
#define MMCSD_BLOCK_SIZE 512
/**
* @brief Mask of error bits in R1 responses.
*/
#define MMCSD_R1_ERROR_MASK 0xFDFFE008
/**
* @brief Fixed pattern for CMD8.
*/
#define MMCSD_CMD8_PATTERN 0x000001AA
/**
* @name SD/MMC status conditions
* @{
*/
#define MMCSD_STS_IDLE 0
#define MMCSD_STS_READY 1
#define MMCSD_STS_IDENT 2
#define MMCSD_STS_STBY 3
#define MMCSD_STS_TRAN 4
#define MMCSD_STS_DATA 5
#define MMCSD_STS_RCV 6
#define MMCSD_STS_PRG 7
#define MMCSD_STS_DIS 8
/** @} */
/**
* @name SD/MMC commands
* @{
*/
#define MMCSD_CMD_GO_IDLE_STATE 0
#define MMCSD_CMD_INIT 1
#define MMCSD_CMD_ALL_SEND_CID 2
#define MMCSD_CMD_SEND_RELATIVE_ADDR 3
#define MMCSD_CMD_SET_BUS_WIDTH 6
#define MMCSD_CMD_SEL_DESEL_CARD 7
#define MMCSD_CMD_SEND_IF_COND 8
#define MMCSD_CMD_SEND_CSD 9
#define MMCSD_CMD_SEND_CID 10
#define MMCSD_CMD_STOP_TRANSMISSION 12
#define MMCSD_CMD_SEND_STATUS 13
#define MMCSD_CMD_SET_BLOCKLEN 16
#define MMCSD_CMD_READ_SINGLE_BLOCK 17
#define MMCSD_CMD_READ_MULTIPLE_BLOCK 18
#define MMCSD_CMD_SET_BLOCK_COUNT 23
#define MMCSD_CMD_WRITE_BLOCK 24
#define MMCSD_CMD_WRITE_MULTIPLE_BLOCK 25
#define MMCSD_CMD_ERASE_RW_BLK_START 32
#define MMCSD_CMD_ERASE_RW_BLK_END 33
#define MMCSD_CMD_ERASE 38
#define MMCSD_CMD_APP_OP_COND 41
#define MMCSD_CMD_LOCK_UNLOCK 42
#define MMCSD_CMD_APP_CMD 55
#define MMCSD_CMD_READ_OCR 58
/** @} */
/**
* @name CSD record offsets
*/
/**
* @brief Slice position of values in CSD register.
*/
/* CSD version 2.0 */
#define MMCSD_CSD_20_CRC_SLICE 7,1
#define MMCSD_CSD_20_FILE_FORMAT_SLICE 11,10
#define MMCSD_CSD_20_TMP_WRITE_PROTECT_SLICE 12,12
#define MMCSD_CSD_20_PERM_WRITE_PROTECT_SLICE 13,13
#define MMCSD_CSD_20_COPY_SLICE 14,14
#define MMCSD_CSD_20_FILE_FORMAT_GRP_SLICE 15,15
#define MMCSD_CSD_20_WRITE_BL_PARTIAL_SLICE 21,21
#define MMCSD_CSD_20_WRITE_BL_LEN_SLICE 25,12
#define MMCSD_CSD_20_R2W_FACTOR_SLICE 28,26
#define MMCSD_CSD_20_WP_GRP_ENABLE_SLICE 31,31
#define MMCSD_CSD_20_WP_GRP_SIZE_SLICE 38,32
#define MMCSD_CSD_20_ERASE_SECTOR_SIZE_SLICE 45,39
#define MMCSD_CSD_20_ERASE_BLK_EN_SLICE 46,46
#define MMCSD_CSD_20_C_SIZE_SLICE 69,48
#define MMCSD_CSD_20_DSR_IMP_SLICE 76,76
#define MMCSD_CSD_20_READ_BLK_MISALIGN_SLICE 77,77
#define MMCSD_CSD_20_WRITE_BLK_MISALIGN_SLICE 78,78
#define MMCSD_CSD_20_READ_BL_PARTIAL_SLICE 79,79
#define MMCSD_CSD_20_READ_BL_LEN_SLICE 83,80
#define MMCSD_CSD_20_CCC_SLICE 95,84
#define MMCSD_CSD_20_TRANS_SPEED_SLICE 103,96
#define MMCSD_CSD_20_NSAC_SLICE 111,104
#define MMCSD_CSD_20_TAAC_SLICE 119,112
#define MMCSD_CSD_20_STRUCTURE_SLICE 127,126
/* CSD version 1.0 */
#define MMCSD_CSD_10_CRC_SLICE MMCSD_CSD_20_CRC_SLICE
#define MMCSD_CSD_10_FILE_FORMAT_SLICE MMCSD_CSD_20_FILE_FORMAT_SLICE
#define MMCSD_CSD_10_TMP_WRITE_PROTECT_SLICE MMCSD_CSD_20_TMP_WRITE_PROTECT_SLICE
#define MMCSD_CSD_10_PERM_WRITE_PROTECT_SLICE MMCSD_CSD_20_PERM_WRITE_PROTECT_SLICE
#define MMCSD_CSD_10_COPY_SLICE MMCSD_CSD_20_COPY_SLICE
#define MMCSD_CSD_10_FILE_FORMAT_GRP_SLICE MMCSD_CSD_20_FILE_FORMAT_GRP_SLICE
#define MMCSD_CSD_10_WRITE_BL_PARTIAL_SLICE MMCSD_CSD_20_WRITE_BL_PARTIAL_SLICE
#define MMCSD_CSD_10_WRITE_BL_LEN_SLICE MMCSD_CSD_20_WRITE_BL_LEN_SLICE
#define MMCSD_CSD_10_R2W_FACTOR_SLICE MMCSD_CSD_20_R2W_FACTOR_SLICE
#define MMCSD_CSD_10_WP_GRP_ENABLE_SLICE MMCSD_CSD_20_WP_GRP_ENABLE_SLICE
#define MMCSD_CSD_10_WP_GRP_SIZE_SLICE MMCSD_CSD_20_WP_GRP_SIZE_SLICE
#define MMCSD_CSD_10_ERASE_SECTOR_SIZE_SLICE MMCSD_CSD_20_ERASE_SECTOR_SIZE_SLICE
#define MMCSD_CSD_10_ERASE_BLK_EN_SLICE MMCSD_CSD_20_ERASE_BLK_EN_SLICE
#define MMCSD_CSD_10_C_SIZE_MULT_SLICE 49,47
#define MMCSD_CSD_10_VDD_W_CURR_MAX_SLICE 52,50
#define MMCSD_CSD_10_VDD_W_CURR_MIN_SLICE 55,53
#define MMCSD_CSD_10_VDD_R_CURR_MAX_SLICE 58,56
#define MMCSD_CSD_10_VDD_R_CURR_MIX_SLICE 61,59
#define MMCSD_CSD_10_C_SIZE_SLICE 73,62
#define MMCSD_CSD_10_DSR_IMP_SLICE MMCSD_CSD_20_DSR_IMP_SLICE
#define MMCSD_CSD_10_READ_BLK_MISALIGN_SLICE MMCSD_CSD_20_READ_BLK_MISALIGN_SLICE
#define MMCSD_CSD_10_WRITE_BLK_MISALIGN_SLICE MMCSD_CSD_20_WRITE_BLK_MISALIGN_SLICE
#define MMCSD_CSD_10_READ_BL_PARTIAL_SLICE MMCSD_CSD_20_READ_BL_PARTIAL_SLICE
#define MMCSD_CSD_10_READ_BL_LEN_SLICE 83, 80
#define MMCSD_CSD_10_CCC_SLICE MMCSD_CSD_20_CCC_SLICE
#define MMCSD_CSD_10_TRANS_SPEED_SLICE MMCSD_CSD_20_TRANS_SPEED_SLICE
#define MMCSD_CSD_10_NSAC_SLICE MMCSD_CSD_20_NSAC_SLICE
#define MMCSD_CSD_10_TAAC_SLICE MMCSD_CSD_20_TAAC_SLICE
#define MMCSD_CSD_10_STRUCTURE_SLICE MMCSD_CSD_20_STRUCTURE_SLICE
/** @} */
/*===========================================================================*/
/* Driver pre-compile time settings. */
/*===========================================================================*/
/*===========================================================================*/
/* Derived constants and error checks. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver data structures and types. */
/*===========================================================================*/
/**
* @brief @p MMCSDBlockDevice specific methods.
*/
#define _mmcsd_block_device_methods \
_base_block_device_methods
/**
* @brief @p MMCSDBlockDevice specific data.
* @note It is empty because @p MMCSDBlockDevice is only an interface
* without implementation.
*/
#define _mmcsd_block_device_data \
_base_block_device_data \
/* Card CID.*/ \
uint32_t cid[4]; \
/* Card CSD.*/ \
uint32_t csd[4]; \
/* Total number of blocks in card.*/ \
uint32_t capacity;
/**
* @extends BaseBlockDeviceVMT
*
* @brief @p MMCSDBlockDevice virtual methods table.
*/
struct MMCSDBlockDeviceVMT {
_base_block_device_methods
};
/**
* @extends BaseBlockDevice
*
* @brief MCC/SD block device class.
* @details This class represents a, block-accessible, MMC/SD device.
*/
typedef struct {
/** @brief Virtual Methods Table.*/
const struct MMCSDBlockDeviceVMT *vmt;
_mmcsd_block_device_data
} MMCSDBlockDevice;
/*===========================================================================*/
/* Driver macros. */
/*===========================================================================*/
/**
* @name R1 response utilities
* @{
*/
/**
* @brief Evaluates to @p TRUE if the R1 response contains error flags.
*
* @param[in] r1 the r1 response
*/
#define MMCSD_R1_ERROR(r1) (((r1) & MMCSD_R1_ERROR_MASK) != 0)
/**
* @brief Returns the status field of an R1 response.
*
* @param[in] r1 the r1 response
*/
#define MMCSD_R1_STS(r1) (((r1) >> 9) & 15)
/**
* @brief Evaluates to @p TRUE if the R1 response indicates a locked card.
*
* @param[in] r1 the r1 response
*/
#define MMCSD_R1_IS_CARD_LOCKED(r1) (((r1) >> 21) & 1)
/** @} */
/**
* @name Macro Functions
* @{
*/
/**
* @brief Returns the card capacity in blocks.
*
* @param[in] ip pointer to a @p MMCSDBlockDevice or derived class
*
* @return The card capacity.
*
* @api
*/
#define mmcsdGetCardCapacity(ip) ((ip)->capacity)
/** @} */
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/
#ifdef __cplusplus
extern "C" {
#endif
uint32_t mmcsdGetCapacity(uint32_t csd[4]);
#ifdef __cplusplus
}
#endif
#endif /* HAL_USE_MMC_SPI || HAL_USE_MMC_SDC*/
#endif /* _HAL_MMCSD_H_ */
/** @} */

199
os/hal/include/mmc_spi.h Normal file
View File

@ -0,0 +1,199 @@
/*
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
2011,2012,2013 Giovanni Di Sirio.
This file is part of ChibiOS/RT.
ChibiOS/RT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
ChibiOS/RT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file mmc_spi.h
* @brief MMC over SPI driver header.
*
* @addtogroup MMC_SPI
* @{
*/
#ifndef _MMC_SPI_H_
#define _MMC_SPI_H_
#if HAL_USE_MMC_SPI || defined(__DOXYGEN__)
/*===========================================================================*/
/* Driver constants. */
/*===========================================================================*/
#define MMC_CMD0_RETRY 10
#define MMC_CMD1_RETRY 100
#define MMC_ACMD41_RETRY 100
#define MMC_WAIT_DATA 10000
/*===========================================================================*/
/* Driver pre-compile time settings. */
/*===========================================================================*/
/**
* @name MMC_SPI configuration options
* @{
*/
/**
* @brief Delays insertions.
* @details 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.
*/
#if !defined(MMC_NICE_WAITING) || defined(__DOXYGEN__)
#define MMC_NICE_WAITING TRUE
#endif
/** @} */
/*===========================================================================*/
/* Derived constants and error checks. */
/*===========================================================================*/
#if !HAL_USE_SPI || !SPI_USE_WAIT
#error "MMC_SPI driver requires HAL_USE_SPI and SPI_USE_WAIT"
#endif
/*===========================================================================*/
/* Driver data structures and types. */
/*===========================================================================*/
/**
* @brief MMC/SD over SPI driver configuration structure.
*/
typedef struct {
/**
* @brief SPI driver associated to this MMC driver.
*/
SPIDriver *spip;
/**
* @brief SPI low speed configuration used during initialization.
*/
const SPIConfig *lscfg;
/**
* @brief SPI high speed configuration used during transfers.
*/
const SPIConfig *hscfg;
} MMCConfig;
/**
* @brief @p MMCDriver specific methods.
*/
#define _mmc_driver_methods \
_mmcsd_block_device_methods
/**
* @extends MMCSDBlockDeviceVMT
*
* @brief @p MMCDriver virtual methods table.
*/
struct MMCDriverVMT {
_mmc_driver_methods
};
/**
* @extends MMCSDBlockDevice
*
* @brief Structure representing a MMC/SD over SPI driver.
*/
typedef struct {
/**
* @brief Virtual Methods Table.
*/
const struct MMCDriverVMT *vmt;
_mmcsd_block_device_data
/**
* @brief Current configuration data.
*/
const MMCConfig *config;
/***
* @brief Addresses use blocks instead of bytes.
*/
bool block_addresses;
} MMCDriver;
/*===========================================================================*/
/* Driver macros. */
/*===========================================================================*/
/**
* @name Macro Functions
* @{
*/
/**
* @brief Returns the card insertion status.
* @note This macro wraps a low level function named
* @p sdc_lld_is_card_inserted(), this function must be
* provided by the application because it is not part of the
* SDC driver.
*
* @param[in] mmcp pointer to the @p MMCDriver object
* @return The card state.
* @retval FALSE card not inserted.
* @retval TRUE card inserted.
*
* @api
*/
#define mmcIsCardInserted(mmcp) mmc_lld_is_card_inserted(mmcp)
/**
* @brief Returns the write protect status.
*
* @param[in] mmcp pointer to the @p MMCDriver object
* @return The card state.
* @retval FALSE card not inserted.
* @retval TRUE card inserted.
*
* @api
*/
#define mmcIsWriteProtected(mmcp) mmc_lld_is_write_protected(mmcp)
/** @} */
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/
#ifdef __cplusplus
extern "C" {
#endif
void mmcInit(void);
void mmcObjectInit(MMCDriver *mmcp);
void mmcStart(MMCDriver *mmcp, const MMCConfig *config);
void mmcStop(MMCDriver *mmcp);
bool mmcConnect(MMCDriver *mmcp);
bool mmcDisconnect(MMCDriver *mmcp);
bool mmcStartSequentialRead(MMCDriver *mmcp, uint32_t startblk);
bool mmcSequentialRead(MMCDriver *mmcp, uint8_t *buffer);
bool mmcStopSequentialRead(MMCDriver *mmcp);
bool mmcStartSequentialWrite(MMCDriver *mmcp, uint32_t startblk);
bool mmcSequentialWrite(MMCDriver *mmcp, const uint8_t *buffer);
bool mmcStopSequentialWrite(MMCDriver *mmcp);
bool mmcSync(MMCDriver *mmcp);
bool mmcGetInfo(MMCDriver *mmcp, BlockDeviceInfo *bdip);
bool mmcErase(MMCDriver *mmcp, uint32_t startblk, uint32_t endblk);
bool mmc_lld_is_card_inserted(MMCDriver *mmcp);
bool mmc_lld_is_write_protected(MMCDriver *mmcp);
#ifdef __cplusplus
}
#endif
#endif /* HAL_USE_MMC_SPI */
#endif /* _MMC_SPI_H_ */
/** @} */

113
os/hal/src/hal_mmcsd.c Normal file
View File

@ -0,0 +1,113 @@
/*
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
2011,2012,2013 Giovanni Di Sirio.
This file is part of ChibiOS/RT.
ChibiOS/RT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
ChibiOS/RT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file hal_mmcsd.c
* @brief MMC/SD cards common code.
*
* @addtogroup MMCSD
* @{
*/
#include "hal.h"
#if HAL_USE_MMC_SPI || HAL_USE_SDC || defined(__DOXYGEN__)
/*===========================================================================*/
/* Driver local definitions. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver exported variables. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local variables and types. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local functions. */
/*===========================================================================*/
/**
* @brief Gets a bit field from a words array.
* @note The bit zero is the LSb of the first word.
*
* @param[in] data pointer to the words array
* @param[in] end bit offset of the last bit of the field, inclusive
* @param[in] start bit offset of the first bit of the field, inclusive
*
* @return The bits field value, left aligned.
*
* @notapi
*/
static uint32_t mmcsd_get_slice(uint32_t *data, uint32_t end, uint32_t start) {
unsigned startidx, endidx, startoff;
uint32_t endmask;
osalDbgCheck((end >= start) && ((end - start) < 32));
startidx = start / 32;
startoff = start % 32;
endidx = end / 32;
endmask = (1 << ((end % 32) + 1)) - 1;
/* One or two pieces?*/
if (startidx < endidx)
return (data[startidx] >> startoff) | /* Two pieces case. */
((data[endidx] & endmask) << (32 - startoff));
return (data[startidx] & endmask) >> startoff; /* One piece case. */
}
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
/**
* @brief Extract card capacity from a CSD.
* @details The capacity is returned as number of available blocks.
*
* @param[in] csd the CSD record
*
* @return The card capacity.
* @retval 0 CSD format error
*/
uint32_t mmcsdGetCapacity(uint32_t csd[4]) {
switch (csd[3] >> 30) {
uint32_t a, b, c;
case 0:
/* CSD version 1.0 */
a = mmcsd_get_slice(csd, MMCSD_CSD_10_C_SIZE_SLICE);
b = mmcsd_get_slice(csd, MMCSD_CSD_10_C_SIZE_MULT_SLICE);
c = mmcsd_get_slice(csd, MMCSD_CSD_10_READ_BL_LEN_SLICE);
return (a + 1) << (b + 2) << (c - 9); /* 2^9 == MMCSD_BLOCK_SIZE. */
case 1:
/* CSD version 2.0.*/
return 1024 * (mmcsd_get_slice(csd, MMCSD_CSD_20_C_SIZE_SLICE) + 1);
default:
/* Reserved value detected.*/
return 0;
}
}
#endif /* HAL_USE_MMC_SPI || HAL_USE_SDC */
/** @} */

875
os/hal/src/mmc_spi.c Normal file
View File

@ -0,0 +1,875 @@
/*
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
2011,2012,2013 Giovanni Di Sirio.
This file is part of ChibiOS/RT.
ChibiOS/RT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
ChibiOS/RT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Parts of this file have been contributed by Matthias Blaicher.
*/
/**
* @file mmc_spi.c
* @brief MMC over SPI driver code.
*
* @addtogroup MMC_SPI
* @{
*/
#include <string.h>
#include "hal.h"
#if HAL_USE_MMC_SPI || defined(__DOXYGEN__)
/*===========================================================================*/
/* Driver local definitions. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver exported variables. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local variables and types. */
/*===========================================================================*/
/* Forward declarations required by mmc_vmt.*/
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);
/**
* @brief Virtual methods table.
*/
static const struct MMCDriverVMT mmc_vmt = {
(bool (*)(void *))mmc_lld_is_card_inserted,
(bool (*)(void *))mmc_lld_is_write_protected,
(bool (*)(void *))mmcConnect,
(bool (*)(void *))mmcDisconnect,
mmc_read,
mmc_write,
(bool (*)(void *))mmcSync,
(bool (*)(void *, BlockDeviceInfo *))mmcGetInfo
};
/**
* @brief Lookup table for CRC-7 ( based on polynomial x^7 + x^3 + 1).
*/
static const uint8_t crc7_lookup_table[256] = {
0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53,
0x6c, 0x65, 0x7e, 0x77, 0x19, 0x10, 0x0b, 0x02, 0x3d, 0x34, 0x2f, 0x26,
0x51, 0x58, 0x43, 0x4a, 0x75, 0x7c, 0x67, 0x6e, 0x32, 0x3b, 0x20, 0x29,
0x16, 0x1f, 0x04, 0x0d, 0x7a, 0x73, 0x68, 0x61, 0x5e, 0x57, 0x4c, 0x45,
0x2b, 0x22, 0x39, 0x30, 0x0f, 0x06, 0x1d, 0x14, 0x63, 0x6a, 0x71, 0x78,
0x47, 0x4e, 0x55, 0x5c, 0x64, 0x6d, 0x76, 0x7f, 0x40, 0x49, 0x52, 0x5b,
0x2c, 0x25, 0x3e, 0x37, 0x08, 0x01, 0x1a, 0x13, 0x7d, 0x74, 0x6f, 0x66,
0x59, 0x50, 0x4b, 0x42, 0x35, 0x3c, 0x27, 0x2e, 0x11, 0x18, 0x03, 0x0a,
0x56, 0x5f, 0x44, 0x4d, 0x72, 0x7b, 0x60, 0x69, 0x1e, 0x17, 0x0c, 0x05,
0x3a, 0x33, 0x28, 0x21, 0x4f, 0x46, 0x5d, 0x54, 0x6b, 0x62, 0x79, 0x70,
0x07, 0x0e, 0x15, 0x1c, 0x23, 0x2a, 0x31, 0x38, 0x41, 0x48, 0x53, 0x5a,
0x65, 0x6c, 0x77, 0x7e, 0x09, 0x00, 0x1b, 0x12, 0x2d, 0x24, 0x3f, 0x36,
0x58, 0x51, 0x4a, 0x43, 0x7c, 0x75, 0x6e, 0x67, 0x10, 0x19, 0x02, 0x0b,
0x34, 0x3d, 0x26, 0x2f, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c,
0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04, 0x6a, 0x63, 0x78, 0x71,
0x4e, 0x47, 0x5c, 0x55, 0x22, 0x2b, 0x30, 0x39, 0x06, 0x0f, 0x14, 0x1d,
0x25, 0x2c, 0x37, 0x3e, 0x01, 0x08, 0x13, 0x1a, 0x6d, 0x64, 0x7f, 0x76,
0x49, 0x40, 0x5b, 0x52, 0x3c, 0x35, 0x2e, 0x27, 0x18, 0x11, 0x0a, 0x03,
0x74, 0x7d, 0x66, 0x6f, 0x50, 0x59, 0x42, 0x4b, 0x17, 0x1e, 0x05, 0x0c,
0x33, 0x3a, 0x21, 0x28, 0x5f, 0x56, 0x4d, 0x44, 0x7b, 0x72, 0x69, 0x60,
0x0e, 0x07, 0x1c, 0x15, 0x2a, 0x23, 0x38, 0x31, 0x46, 0x4f, 0x54, 0x5d,
0x62, 0x6b, 0x70, 0x79
};
/*===========================================================================*/
/* Driver local functions. */
/*===========================================================================*/
static bool mmc_read(void *instance, uint32_t startblk,
uint8_t *buffer, uint32_t n) {
if (mmcStartSequentialRead((MMCDriver *)instance, startblk))
return CH_FAILED;
while (n > 0) {
if (mmcSequentialRead((MMCDriver *)instance, buffer))
return CH_FAILED;
buffer += MMCSD_BLOCK_SIZE;
n--;
}
if (mmcStopSequentialRead((MMCDriver *)instance))
return CH_FAILED;
return CH_SUCCESS;
}
static bool mmc_write(void *instance, uint32_t startblk,
const uint8_t *buffer, uint32_t n) {
if (mmcStartSequentialWrite((MMCDriver *)instance, startblk))
return CH_FAILED;
while (n > 0) {
if (mmcSequentialWrite((MMCDriver *)instance, buffer))
return CH_FAILED;
buffer += MMCSD_BLOCK_SIZE;
n--;
}
if (mmcStopSequentialWrite((MMCDriver *)instance))
return CH_FAILED;
return CH_SUCCESS;
}
/**
* @brief Calculate the MMC standard CRC-7 based on a lookup table.
*
* @param[in] crc start value for CRC
* @param[in] buffer pointer to data buffer
* @param[in] len length of data
* @return Calculated CRC
*/
static uint8_t crc7(uint8_t crc, const uint8_t *buffer, size_t len) {
while (len--)
crc = crc7_lookup_table[(crc << 1) ^ (*buffer++)];
return crc;
}
/**
* @brief Waits an idle condition.
*
* @param[in] mmcp pointer to the @p MMCDriver object
*
* @notapi
*/
static void wait(MMCDriver *mmcp) {
int i;
uint8_t buf[4];
for (i = 0; i < 16; i++) {
spiReceive(mmcp->config->spip, 1, buf);
if (buf[0] == 0xFF)
return;
}
/* Looks like it is a long wait.*/
while (TRUE) {
spiReceive(mmcp->config->spip, 1, buf);
if (buf[0] == 0xFF)
break;
#ifdef MMC_NICE_WAITING
/* Trying to be nice with the other threads.*/
chThdSleep(1);
#endif
}
}
/**
* @brief Sends a command header.
*
* @param[in] mmcp pointer to the @p MMCDriver object
* @param[in] cmd the command id
* @param[in] arg the command argument
*
* @notapi
*/
static void send_hdr(MMCDriver *mmcp, uint8_t cmd, uint32_t arg) {
uint8_t buf[6];
/* Wait for the bus to become idle if a write operation was in progress.*/
wait(mmcp);
buf[0] = 0x40 | cmd;
buf[1] = arg >> 24;
buf[2] = arg >> 16;
buf[3] = arg >> 8;
buf[4] = arg;
/* Calculate CRC for command header, shift to right position, add stop bit.*/
buf[5] = ((crc7(0, buf, 5) & 0x7F) << 1) | 0x01;
spiSend(mmcp->config->spip, 6, buf);
}
/**
* @brief Receives a single byte response.
*
* @param[in] mmcp pointer to the @p MMCDriver object
* @return The response as an @p uint8_t value.
* @retval 0xFF timed out.
*
* @notapi
*/
static uint8_t recvr1(MMCDriver *mmcp) {
int i;
uint8_t r1[1];
for (i = 0; i < 9; i++) {
spiReceive(mmcp->config->spip, 1, r1);
if (r1[0] != 0xFF)
return r1[0];
}
return 0xFF;
}
/**
* @brief Receives a three byte response.
*
* @param[in] mmcp pointer to the @p MMCDriver object
* @param[out] buffer pointer to four bytes wide buffer
* @return First response byte as an @p uint8_t value.
* @retval 0xFF timed out.
*
* @notapi
*/
static uint8_t recvr3(MMCDriver *mmcp, uint8_t* buffer) {
uint8_t r1;
r1 = recvr1(mmcp);
spiReceive(mmcp->config->spip, 4, buffer);
return r1;
}
/**
* @brief Sends a command an returns a single byte response.
*
* @param[in] mmcp pointer to the @p MMCDriver object
* @param[in] cmd the command id
* @param[in] arg the command argument
* @return The response as an @p uint8_t value.
* @retval 0xFF timed out.
*
* @notapi
*/
static uint8_t send_command_R1(MMCDriver *mmcp, uint8_t cmd, uint32_t arg) {
uint8_t r1;
spiSelect(mmcp->config->spip);
send_hdr(mmcp, cmd, arg);
r1 = recvr1(mmcp);
spiUnselect(mmcp->config->spip);
return r1;
}
/**
* @brief Sends a command which returns a five bytes response (R3).
*
* @param[in] mmcp pointer to the @p MMCDriver object
* @param[in] cmd the command id
* @param[in] arg the command argument
* @param[out] response pointer to four bytes wide uint8_t buffer
* @return The first byte of the response (R1) as an @p
* uint8_t value.
* @retval 0xFF timed out.
*
* @notapi
*/
static uint8_t send_command_R3(MMCDriver *mmcp, uint8_t cmd, uint32_t arg,
uint8_t *response) {
uint8_t r1;
spiSelect(mmcp->config->spip);
send_hdr(mmcp, cmd, arg);
r1 = recvr3(mmcp, response);
spiUnselect(mmcp->config->spip);
return r1;
}
/**
* @brief Reads the CSD.
*
* @param[in] mmcp pointer to the @p MMCDriver object
* @param[out] csd pointer to the CSD buffer
*
* @return The operation status.
* @retval CH_SUCCESS the operation succeeded.
* @retval CH_FAILED the operation failed.
*
* @notapi
*/
static bool read_CxD(MMCDriver *mmcp, uint8_t cmd, uint32_t cxd[4]) {
unsigned i;
uint8_t *bp, buf[16];
spiSelect(mmcp->config->spip);
send_hdr(mmcp, cmd, 0);
if (recvr1(mmcp) != 0x00) {
spiUnselect(mmcp->config->spip);
return CH_FAILED;
}
/* Wait for data availability.*/
for (i = 0; i < MMC_WAIT_DATA; i++) {
spiReceive(mmcp->config->spip, 1, buf);
if (buf[0] == 0xFE) {
uint32_t *wp;
spiReceive(mmcp->config->spip, 16, buf);
bp = buf;
for (wp = &cxd[3]; wp >= cxd; wp--) {
*wp = ((uint32_t)bp[0] << 24) | ((uint32_t)bp[1] << 16) |
((uint32_t)bp[2] << 8) | (uint32_t)bp[3];
bp += 4;
}
/* CRC ignored then end of transaction. */
spiIgnore(mmcp->config->spip, 2);
spiUnselect(mmcp->config->spip);
return CH_SUCCESS;
}
}
return CH_FAILED;
}
/**
* @brief Waits that the card reaches an idle state.
*
* @param[in] mmcp pointer to the @p MMCDriver object
*
* @notapi
*/
static void sync(MMCDriver *mmcp) {
uint8_t buf[1];
spiSelect(mmcp->config->spip);
while (TRUE) {
spiReceive(mmcp->config->spip, 1, buf);
if (buf[0] == 0xFF)
break;
#ifdef MMC_NICE_WAITING
chThdSleep(1); /* Trying to be nice with the other threads.*/
#endif
}
spiUnselect(mmcp->config->spip);
}
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
/**
* @brief MMC over SPI driver initialization.
* @note This function is implicitly invoked by @p halInit(), there is
* no need to explicitly initialize the driver.
*
* @init
*/
void mmcInit(void) {
}
/**
* @brief Initializes an instance.
*
* @param[out] mmcp pointer to the @p MMCDriver object
*
* @init
*/
void mmcObjectInit(MMCDriver *mmcp) {
mmcp->vmt = &mmc_vmt;
mmcp->state = BLK_STOP;
mmcp->config = NULL;
mmcp->block_addresses = FALSE;
}
/**
* @brief Configures and activates the MMC peripheral.
*
* @param[in] mmcp pointer to the @p MMCDriver object
* @param[in] config pointer to the @p MMCConfig object.
*
* @api
*/
void mmcStart(MMCDriver *mmcp, const MMCConfig *config) {
osalDbgCheck((mmcp != NULL) && (config != NULL));
osalDbgAssert((mmcp->state == BLK_STOP) || (mmcp->state == BLK_ACTIVE),
"invalid state");
mmcp->config = config;
mmcp->state = BLK_ACTIVE;
}
/**
* @brief Disables the MMC peripheral.
*
* @param[in] mmcp pointer to the @p MMCDriver object
*
* @api
*/
void mmcStop(MMCDriver *mmcp) {
osalDbgCheck(mmcp != NULL);
osalDbgAssert((mmcp->state == BLK_STOP) || (mmcp->state == BLK_ACTIVE),
"invalid state");
spiStop(mmcp->config->spip);
mmcp->state = BLK_STOP;
}
/**
* @brief Performs the initialization procedure on the inserted card.
* @details This function should be invoked when a card is inserted and
* brings the driver in the @p MMC_READY state where it is possible
* to perform read and write operations.
* @note It is possible to invoke this function from the insertion event
* handler.
*
* @param[in] mmcp pointer to the @p MMCDriver object
*
* @return The operation status.
* @retval CH_SUCCESS the operation succeeded and the driver is now
* in the @p MMC_READY state.
* @retval CH_FAILED the operation failed.
*
* @api
*/
bool mmcConnect(MMCDriver *mmcp) {
unsigned i;
uint8_t r3[4];
osalDbgCheck(mmcp != NULL);
osalDbgAssert((mmcp->state == BLK_ACTIVE) || (mmcp->state == BLK_READY),
"invalid state");
/* Connection procedure in progress.*/
mmcp->state = BLK_CONNECTING;
/* Slow clock mode and 128 clock pulses.*/
spiStart(mmcp->config->spip, mmcp->config->lscfg);
spiIgnore(mmcp->config->spip, 16);
/* SPI mode selection.*/
i = 0;
while (TRUE) {
if (send_command_R1(mmcp, MMCSD_CMD_GO_IDLE_STATE, 0) == 0x01)
break;
if (++i >= MMC_CMD0_RETRY)
goto failed;
chThdSleepMilliseconds(10);
}
/* Try to detect if this is a high capacity card and switch to block
addresses if possible.
This method is based on "How to support SDC Ver2 and high capacity cards"
by ElmChan.*/
if (send_command_R3(mmcp, MMCSD_CMD_SEND_IF_COND,
MMCSD_CMD8_PATTERN, r3) != 0x05) {
/* Switch to SDHC mode.*/
i = 0;
while (TRUE) {
if ((send_command_R1(mmcp, MMCSD_CMD_APP_CMD, 0) == 0x01) &&
(send_command_R3(mmcp, MMCSD_CMD_APP_OP_COND,
0x400001aa, r3) == 0x00))
break;
if (++i >= MMC_ACMD41_RETRY)
goto failed;
chThdSleepMilliseconds(10);
}
/* Execute dedicated read on OCR register */
send_command_R3(mmcp, MMCSD_CMD_READ_OCR, 0, r3);
/* Check if CCS is set in response. Card operates in block mode if set.*/
if (r3[0] & 0x40)
mmcp->block_addresses = TRUE;
}
/* Initialization.*/
i = 0;
while (TRUE) {
uint8_t b = send_command_R1(mmcp, MMCSD_CMD_INIT, 0);
if (b == 0x00)
break;
if (b != 0x01)
goto failed;
if (++i >= MMC_CMD1_RETRY)
goto failed;
chThdSleepMilliseconds(10);
}
/* Initialization complete, full speed.*/
spiStart(mmcp->config->spip, mmcp->config->hscfg);
/* Setting block size.*/
if (send_command_R1(mmcp, MMCSD_CMD_SET_BLOCKLEN,
MMCSD_BLOCK_SIZE) != 0x00)
goto failed;
/* Determine capacity.*/
if (read_CxD(mmcp, MMCSD_CMD_SEND_CSD, mmcp->csd))
goto failed;
mmcp->capacity = mmcsdGetCapacity(mmcp->csd);
if (mmcp->capacity == 0)
goto failed;
if (read_CxD(mmcp, MMCSD_CMD_SEND_CID, mmcp->cid))
goto failed;
mmcp->state = BLK_READY;
return CH_SUCCESS;
/* Connection failed, state reset to BLK_ACTIVE.*/
failed:
spiStop(mmcp->config->spip);
mmcp->state = BLK_ACTIVE;
return CH_FAILED;
}
/**
* @brief Brings the driver in a state safe for card removal.
*
* @param[in] mmcp pointer to the @p MMCDriver object
* @return The operation status.
*
* @retval CH_SUCCESS the operation succeeded and the driver is now
* in the @p MMC_INSERTED state.
* @retval CH_FAILED the operation failed.
*
* @api
*/
bool mmcDisconnect(MMCDriver *mmcp) {
osalDbgCheck(mmcp != NULL);
osalSysLock();
osalDbgAssert((mmcp->state == BLK_ACTIVE) || (mmcp->state == BLK_READY),
"invalid state");
if (mmcp->state == BLK_ACTIVE) {
osalSysUnlock();
return CH_SUCCESS;
}
mmcp->state = BLK_DISCONNECTING;
osalSysUnlock();
/* Wait for the pending write operations to complete.*/
spiStart(mmcp->config->spip, mmcp->config->hscfg);
sync(mmcp);
spiStop(mmcp->config->spip);
mmcp->state = BLK_ACTIVE;
return CH_SUCCESS;
}
/**
* @brief Starts a sequential read.
*
* @param[in] mmcp pointer to the @p MMCDriver object
* @param[in] startblk first block to read
*
* @return The operation status.
* @retval CH_SUCCESS the operation succeeded.
* @retval CH_FAILED the operation failed.
*
* @api
*/
bool mmcStartSequentialRead(MMCDriver *mmcp, uint32_t startblk) {
osalDbgCheck(mmcp != NULL);
osalDbgAssert(mmcp->state == BLK_READY, "invalid state");
/* Read operation in progress.*/
mmcp->state = BLK_READING;
/* (Re)starting the SPI in case it has been reprogrammed externally, it can
happen if the SPI bus is shared among multiple peripherals.*/
spiStart(mmcp->config->spip, mmcp->config->hscfg);
spiSelect(mmcp->config->spip);
if (mmcp->block_addresses)
send_hdr(mmcp, MMCSD_CMD_READ_MULTIPLE_BLOCK, startblk);
else
send_hdr(mmcp, MMCSD_CMD_READ_MULTIPLE_BLOCK, startblk * MMCSD_BLOCK_SIZE);
if (recvr1(mmcp) != 0x00) {
spiStop(mmcp->config->spip);
mmcp->state = BLK_READY;
return CH_FAILED;
}
return CH_SUCCESS;
}
/**
* @brief Reads a block within a sequential read operation.
*
* @param[in] mmcp pointer to the @p MMCDriver object
* @param[out] buffer pointer to the read buffer
*
* @return The operation status.
* @retval CH_SUCCESS the operation succeeded.
* @retval CH_FAILED the operation failed.
*
* @api
*/
bool mmcSequentialRead(MMCDriver *mmcp, uint8_t *buffer) {
int i;
osalDbgCheck((mmcp != NULL) && (buffer != NULL));
if (mmcp->state != BLK_READING)
return CH_FAILED;
for (i = 0; i < MMC_WAIT_DATA; i++) {
spiReceive(mmcp->config->spip, 1, buffer);
if (buffer[0] == 0xFE) {
spiReceive(mmcp->config->spip, MMCSD_BLOCK_SIZE, buffer);
/* CRC ignored. */
spiIgnore(mmcp->config->spip, 2);
return CH_SUCCESS;
}
}
/* Timeout.*/
spiUnselect(mmcp->config->spip);
spiStop(mmcp->config->spip);
mmcp->state = BLK_READY;
return CH_FAILED;
}
/**
* @brief Stops a sequential read gracefully.
*
* @param[in] mmcp pointer to the @p MMCDriver object
*
* @return The operation status.
* @retval CH_SUCCESS the operation succeeded.
* @retval CH_FAILED the operation failed.
*
* @api
*/
bool mmcStopSequentialRead(MMCDriver *mmcp) {
static const uint8_t stopcmd[] = {0x40 | MMCSD_CMD_STOP_TRANSMISSION,
0, 0, 0, 0, 1, 0xFF};
osalDbgCheck(mmcp != NULL);
if (mmcp->state != BLK_READING)
return CH_FAILED;
spiSend(mmcp->config->spip, sizeof(stopcmd), stopcmd);
/* result = recvr1(mmcp) != 0x00;*/
/* Note, ignored r1 response, it can be not zero, unknown issue.*/
(void) recvr1(mmcp);
/* Read operation finished.*/
spiUnselect(mmcp->config->spip);
mmcp->state = BLK_READY;
return CH_SUCCESS;
}
/**
* @brief Starts a sequential write.
*
* @param[in] mmcp pointer to the @p MMCDriver object
* @param[in] startblk first block to write
*
* @return The operation status.
* @retval CH_SUCCESS the operation succeeded.
* @retval CH_FAILED the operation failed.
*
* @api
*/
bool mmcStartSequentialWrite(MMCDriver *mmcp, uint32_t startblk) {
osalDbgCheck(mmcp != NULL);
osalDbgAssert(mmcp->state == BLK_READY, "invalid state");
/* Write operation in progress.*/
mmcp->state = BLK_WRITING;
spiStart(mmcp->config->spip, mmcp->config->hscfg);
spiSelect(mmcp->config->spip);
if (mmcp->block_addresses)
send_hdr(mmcp, MMCSD_CMD_WRITE_MULTIPLE_BLOCK, startblk);
else
send_hdr(mmcp, MMCSD_CMD_WRITE_MULTIPLE_BLOCK,
startblk * MMCSD_BLOCK_SIZE);
if (recvr1(mmcp) != 0x00) {
spiStop(mmcp->config->spip);
mmcp->state = BLK_READY;
return CH_FAILED;
}
return CH_SUCCESS;
}
/**
* @brief Writes a block within a sequential write operation.
*
* @param[in] mmcp pointer to the @p MMCDriver object
* @param[out] buffer pointer to the write buffer
*
* @return The operation status.
* @retval CH_SUCCESS the operation succeeded.
* @retval CH_FAILED the operation failed.
*
* @api
*/
bool mmcSequentialWrite(MMCDriver *mmcp, const uint8_t *buffer) {
static const uint8_t start[] = {0xFF, 0xFC};
uint8_t b[1];
osalDbgCheck((mmcp != NULL) && (buffer != NULL));
if (mmcp->state != BLK_WRITING)
return CH_FAILED;
spiSend(mmcp->config->spip, sizeof(start), start); /* Data prologue. */
spiSend(mmcp->config->spip, MMCSD_BLOCK_SIZE, buffer);/* Data. */
spiIgnore(mmcp->config->spip, 2); /* CRC ignored. */
spiReceive(mmcp->config->spip, 1, b);
if ((b[0] & 0x1F) == 0x05) {
wait(mmcp);
return CH_SUCCESS;
}
/* Error.*/
spiUnselect(mmcp->config->spip);
spiStop(mmcp->config->spip);
mmcp->state = BLK_READY;
return CH_FAILED;
}
/**
* @brief Stops a sequential write gracefully.
*
* @param[in] mmcp pointer to the @p MMCDriver object
*
* @return The operation status.
* @retval CH_SUCCESS the operation succeeded.
* @retval CH_FAILED the operation failed.
*
* @api
*/
bool mmcStopSequentialWrite(MMCDriver *mmcp) {
static const uint8_t stop[] = {0xFD, 0xFF};
osalDbgCheck(mmcp != NULL);
if (mmcp->state != BLK_WRITING)
return CH_FAILED;
spiSend(mmcp->config->spip, sizeof(stop), stop);
spiUnselect(mmcp->config->spip);
/* Write operation finished.*/
mmcp->state = BLK_READY;
return CH_SUCCESS;
}
/**
* @brief Waits for card idle condition.
*
* @param[in] mmcp pointer to the @p MMCDriver object
*
* @return The operation status.
* @retval CH_SUCCESS the operation succeeded.
* @retval CH_FAILED the operation failed.
*
* @api
*/
bool mmcSync(MMCDriver *mmcp) {
osalDbgCheck(mmcp != NULL);
if (mmcp->state != BLK_READY)
return CH_FAILED;
/* Synchronization operation in progress.*/
mmcp->state = BLK_SYNCING;
spiStart(mmcp->config->spip, mmcp->config->hscfg);
sync(mmcp);
/* Synchronization operation finished.*/
mmcp->state = BLK_READY;
return CH_SUCCESS;
}
/**
* @brief Returns the media info.
*
* @param[in] mmcp pointer to the @p MMCDriver object
* @param[out] bdip pointer to a @p BlockDeviceInfo structure
*
* @return The operation status.
* @retval CH_SUCCESS the operation succeeded.
* @retval CH_FAILED the operation failed.
*
* @api
*/
bool mmcGetInfo(MMCDriver *mmcp, BlockDeviceInfo *bdip) {
osalDbgCheck((mmcp != NULL) && (bdip != NULL));
if (mmcp->state != BLK_READY)
return CH_FAILED;
bdip->blk_num = mmcp->capacity;
bdip->blk_size = MMCSD_BLOCK_SIZE;
return CH_SUCCESS;
}
/**
* @brief Erases blocks.
*
* @param[in] mmcp pointer to the @p MMCDriver object
* @param[in] startblk starting block number
* @param[in] endblk ending block number
*
* @return The operation status.
* @retval CH_SUCCESS the operation succeeded.
* @retval CH_FAILED the operation failed.
*
* @api
*/
bool mmcErase(MMCDriver *mmcp, uint32_t startblk, uint32_t endblk) {
osalDbgCheck((mmcp != NULL));
/* Erase operation in progress.*/
mmcp->state = BLK_WRITING;
/* Handling command differences between HC and normal cards.*/
if (!mmcp->block_addresses) {
startblk *= MMCSD_BLOCK_SIZE;
endblk *= MMCSD_BLOCK_SIZE;
}
if (send_command_R1(mmcp, MMCSD_CMD_ERASE_RW_BLK_START, startblk))
goto failed;
if (send_command_R1(mmcp, MMCSD_CMD_ERASE_RW_BLK_END, endblk))
goto failed;
if (send_command_R1(mmcp, MMCSD_CMD_ERASE, 0))
goto failed;
mmcp->state = BLK_READY;
return CH_SUCCESS;
/* Command failed, state reset to BLK_ACTIVE.*/
failed:
spiStop(mmcp->config->spip);
mmcp->state = BLK_READY;
return CH_FAILED;
}
#endif /* HAL_USE_MMC_SPI */
/** @} */