2013-08-04 13:38:53 +00:00
|
|
|
/*
|
2015-01-11 13:56:55 +00:00
|
|
|
ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2015-09-17 12:23:10 +00:00
|
|
|
* @file STM32/SDIOv1/sdc_lld.c
|
2013-08-04 13:38:53 +00:00
|
|
|
* @brief STM32 SDC subsystem low level driver source.
|
|
|
|
*
|
|
|
|
* @addtogroup SDC
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "hal.h"
|
|
|
|
|
|
|
|
#if HAL_USE_SDC || defined(__DOXYGEN__)
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local definitions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
#define DMA_CHANNEL \
|
|
|
|
STM32_DMA_GETCHANNEL(STM32_SDC_SDIO_DMA_STREAM, \
|
|
|
|
STM32_SDC_SDIO_DMA_CHN)
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/** @brief SDCD1 driver identifier.*/
|
|
|
|
SDCDriver SDCD1;
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local variables and types. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
#if STM32_SDC_SDIO_UNALIGNED_SUPPORT
|
|
|
|
/**
|
|
|
|
* @brief Buffer for temporary storage during unaligned transfers.
|
|
|
|
*/
|
|
|
|
static union {
|
|
|
|
uint32_t alignment;
|
|
|
|
uint8_t buf[MMCSD_BLOCK_SIZE];
|
|
|
|
} u;
|
|
|
|
#endif /* STM32_SDC_SDIO_UNALIGNED_SUPPORT */
|
|
|
|
|
2015-04-20 09:58:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief SDIO default configuration.
|
|
|
|
*/
|
|
|
|
static const SDCConfig sdc_default_cfg = {
|
|
|
|
NULL,
|
|
|
|
SDC_MODE_4BIT
|
|
|
|
};
|
|
|
|
|
2013-08-04 13:38:53 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2015-02-18 13:46:06 +00:00
|
|
|
/**
|
2015-05-11 14:33:14 +00:00
|
|
|
* @brief Prepares to handle read transaction.
|
2015-02-18 13:46:06 +00:00
|
|
|
* @details Designed for read special registers from card.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
* @param[out] buf pointer to the read buffer
|
|
|
|
* @param[in] bytes number of bytes to read
|
|
|
|
*
|
|
|
|
* @return The operation status.
|
|
|
|
* @retval HAL_SUCCESS operation succeeded.
|
|
|
|
* @retval HAL_FAILED operation failed.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2015-05-11 14:33:14 +00:00
|
|
|
static bool sdc_lld_prepare_read_bytes(SDCDriver *sdcp,
|
|
|
|
uint8_t *buf, uint32_t bytes) {
|
2015-02-18 13:46:06 +00:00
|
|
|
osalDbgCheck(bytes < 0x1000000);
|
|
|
|
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->DTIMER = STM32_SDC_READ_TIMEOUT;
|
2015-02-18 13:46:06 +00:00
|
|
|
|
|
|
|
/* Checks for errors and waits for the card to be ready for reading.*/
|
|
|
|
if (_sdc_wait_for_transfer_state(sdcp))
|
|
|
|
return HAL_FAILED;
|
|
|
|
|
|
|
|
/* Prepares the DMA channel for writing.*/
|
|
|
|
dmaStreamSetMemory0(sdcp->dma, buf);
|
|
|
|
dmaStreamSetTransactionSize(sdcp->dma, bytes / sizeof (uint32_t));
|
|
|
|
dmaStreamSetMode(sdcp->dma, sdcp->dmamode | STM32_DMA_CR_DIR_P2M);
|
|
|
|
dmaStreamEnable(sdcp->dma);
|
|
|
|
|
|
|
|
/* Setting up data transfer.*/
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->ICR = STM32_SDIO_ICR_ALL_FLAGS;
|
|
|
|
sdcp->sdio->MASK = SDIO_MASK_DCRCFAILIE |
|
|
|
|
SDIO_MASK_DTIMEOUTIE |
|
|
|
|
SDIO_MASK_STBITERRIE |
|
|
|
|
SDIO_MASK_RXOVERRIE |
|
|
|
|
SDIO_MASK_DATAENDIE;
|
|
|
|
sdcp->sdio->DLEN = bytes;
|
2015-02-18 13:46:06 +00:00
|
|
|
|
|
|
|
/* Transaction starts just after DTEN bit setting.*/
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->DCTRL = SDIO_DCTRL_DTDIR |
|
|
|
|
SDIO_DCTRL_DTMODE | /* multibyte data transfer */
|
|
|
|
SDIO_DCTRL_DMAEN |
|
|
|
|
SDIO_DCTRL_DTEN;
|
2015-02-18 13:46:06 +00:00
|
|
|
|
|
|
|
return HAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2013-08-04 13:38:53 +00:00
|
|
|
/**
|
|
|
|
* @brief Prepares card to handle read transaction.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
* @param[in] startblk first block to read
|
|
|
|
* @param[in] n number of blocks to read
|
|
|
|
* @param[in] resp pointer to the response buffer
|
|
|
|
*
|
|
|
|
* @return The operation status.
|
2013-09-20 12:25:11 +00:00
|
|
|
* @retval HAL_SUCCESS operation succeeded.
|
|
|
|
* @retval HAL_FAILED operation failed.
|
2013-08-04 13:38:53 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2015-05-11 14:33:14 +00:00
|
|
|
static bool sdc_lld_prepare_read(SDCDriver *sdcp, uint32_t startblk,
|
|
|
|
uint32_t n, uint32_t *resp) {
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
/* Driver handles data in 512 bytes blocks (just like HC cards). But if we
|
|
|
|
have not HC card than we must convert address from blocks to bytes.*/
|
|
|
|
if (!(sdcp->cardmode & SDC_MODE_HIGH_CAPACITY))
|
|
|
|
startblk *= MMCSD_BLOCK_SIZE;
|
|
|
|
|
|
|
|
if (n > 1) {
|
|
|
|
/* Send read multiple blocks command to card.*/
|
|
|
|
if (sdc_lld_send_cmd_short_crc(sdcp, MMCSD_CMD_READ_MULTIPLE_BLOCK,
|
|
|
|
startblk, resp) || MMCSD_R1_ERROR(resp[0]))
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_FAILED;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
/* Send read single block command.*/
|
|
|
|
if (sdc_lld_send_cmd_short_crc(sdcp, MMCSD_CMD_READ_SINGLE_BLOCK,
|
|
|
|
startblk, resp) || MMCSD_R1_ERROR(resp[0]))
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_FAILED;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_SUCCESS;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Prepares card to handle write transaction.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
* @param[in] startblk first block to read
|
|
|
|
* @param[in] n number of blocks to write
|
|
|
|
* @param[in] resp pointer to the response buffer
|
|
|
|
*
|
|
|
|
* @return The operation status.
|
2013-09-20 12:25:11 +00:00
|
|
|
* @retval HAL_SUCCESS operation succeeded.
|
|
|
|
* @retval HAL_FAILED operation failed.
|
2013-08-04 13:38:53 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2013-09-20 12:25:11 +00:00
|
|
|
static bool sdc_lld_prepare_write(SDCDriver *sdcp, uint32_t startblk,
|
|
|
|
uint32_t n, uint32_t *resp) {
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
/* Driver handles data in 512 bytes blocks (just like HC cards). But if we
|
|
|
|
have not HC card than we must convert address from blocks to bytes.*/
|
|
|
|
if (!(sdcp->cardmode & SDC_MODE_HIGH_CAPACITY))
|
|
|
|
startblk *= MMCSD_BLOCK_SIZE;
|
|
|
|
|
|
|
|
if (n > 1) {
|
|
|
|
/* Write multiple blocks command.*/
|
|
|
|
if (sdc_lld_send_cmd_short_crc(sdcp, MMCSD_CMD_WRITE_MULTIPLE_BLOCK,
|
|
|
|
startblk, resp) || MMCSD_R1_ERROR(resp[0]))
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_FAILED;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
/* Write single block command.*/
|
|
|
|
if (sdc_lld_send_cmd_short_crc(sdcp, MMCSD_CMD_WRITE_BLOCK,
|
|
|
|
startblk, resp) || MMCSD_R1_ERROR(resp[0]))
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_FAILED;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_SUCCESS;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Wait end of data transaction and performs finalizations.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
* @param[in] n number of blocks in transaction
|
|
|
|
* @param[in] resp pointer to the response buffer
|
|
|
|
*
|
|
|
|
* @return The operation status.
|
2013-09-20 12:25:11 +00:00
|
|
|
* @retval HAL_SUCCESS operation succeeded.
|
|
|
|
* @retval HAL_FAILED operation failed.
|
2013-08-04 13:38:53 +00:00
|
|
|
*/
|
2013-09-20 12:25:11 +00:00
|
|
|
static bool sdc_lld_wait_transaction_end(SDCDriver *sdcp, uint32_t n,
|
|
|
|
uint32_t *resp) {
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
/* Note the mask is checked before going to sleep because the interrupt
|
|
|
|
may have occurred before reaching the critical zone.*/
|
2013-09-20 12:25:11 +00:00
|
|
|
osalSysLock();
|
2015-05-12 08:12:19 +00:00
|
|
|
if (sdcp->sdio->MASK != 0)
|
2013-09-20 12:25:11 +00:00
|
|
|
osalThreadSuspendS(&sdcp->thread);
|
2015-05-12 08:12:19 +00:00
|
|
|
if ((sdcp->sdio->STA & SDIO_STA_DATAEND) == 0) {
|
2013-09-20 12:25:11 +00:00
|
|
|
osalSysUnlock();
|
|
|
|
return HAL_FAILED;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if (defined(STM32F4XX) || defined(STM32F2XX))
|
|
|
|
/* Wait until DMA channel enabled to be sure that all data transferred.*/
|
|
|
|
while (sdcp->dma->stream->CR & STM32_DMA_CR_EN)
|
|
|
|
;
|
|
|
|
|
|
|
|
/* DMA event flags must be manually cleared.*/
|
|
|
|
dmaStreamClearInterrupt(sdcp->dma);
|
|
|
|
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->ICR = STM32_SDIO_ICR_ALL_FLAGS;
|
|
|
|
sdcp->sdio->DCTRL = 0;
|
2013-09-20 12:25:11 +00:00
|
|
|
osalSysUnlock();
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
/* Wait until interrupt flags to be cleared.*/
|
|
|
|
/*while (((DMA2->LISR) >> (sdcp->dma->ishift)) & STM32_DMA_ISR_TCIF)
|
|
|
|
dmaStreamClearInterrupt(sdcp->dma);*/
|
|
|
|
#else
|
|
|
|
/* Waits for transfer completion at DMA level, the the stream is
|
|
|
|
disabled and cleared.*/
|
|
|
|
dmaWaitCompletion(sdcp->dma);
|
|
|
|
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->ICR = STM32_SDIO_ICR_ALL_FLAGS;
|
|
|
|
sdcp->sdio->DCTRL = 0;
|
2013-09-20 12:25:11 +00:00
|
|
|
osalSysUnlock();
|
2013-08-04 13:38:53 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Finalize transaction.*/
|
|
|
|
if (n > 1)
|
|
|
|
return sdc_lld_send_cmd_short_crc(sdcp, MMCSD_CMD_STOP_TRANSMISSION, 0, resp);
|
|
|
|
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_SUCCESS;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Gets SDC errors.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
* @param[in] sta value of the STA register
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
static void sdc_lld_collect_errors(SDCDriver *sdcp, uint32_t sta) {
|
|
|
|
uint32_t errors = SDC_NO_ERROR;
|
|
|
|
|
|
|
|
if (sta & SDIO_STA_CCRCFAIL)
|
|
|
|
errors |= SDC_CMD_CRC_ERROR;
|
|
|
|
if (sta & SDIO_STA_DCRCFAIL)
|
|
|
|
errors |= SDC_DATA_CRC_ERROR;
|
|
|
|
if (sta & SDIO_STA_CTIMEOUT)
|
|
|
|
errors |= SDC_COMMAND_TIMEOUT;
|
|
|
|
if (sta & SDIO_STA_DTIMEOUT)
|
|
|
|
errors |= SDC_DATA_TIMEOUT;
|
|
|
|
if (sta & SDIO_STA_TXUNDERR)
|
|
|
|
errors |= SDC_TX_UNDERRUN;
|
|
|
|
if (sta & SDIO_STA_RXOVERR)
|
|
|
|
errors |= SDC_RX_OVERRUN;
|
|
|
|
if (sta & SDIO_STA_STBITERR)
|
|
|
|
errors |= SDC_STARTBIT_ERROR;
|
|
|
|
|
|
|
|
sdcp->errors |= errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Performs clean transaction stopping in case of errors.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
* @param[in] n number of blocks in transaction
|
|
|
|
* @param[in] resp pointer to the response buffer
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
static void sdc_lld_error_cleanup(SDCDriver *sdcp,
|
|
|
|
uint32_t n,
|
|
|
|
uint32_t *resp) {
|
2015-05-12 08:12:19 +00:00
|
|
|
uint32_t sta = sdcp->sdio->STA;
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
dmaStreamClearInterrupt(sdcp->dma);
|
|
|
|
dmaStreamDisable(sdcp->dma);
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->ICR = STM32_SDIO_ICR_ALL_FLAGS;
|
|
|
|
sdcp->sdio->MASK = 0;
|
|
|
|
sdcp->sdio->DCTRL = 0;
|
2013-08-04 13:38:53 +00:00
|
|
|
sdc_lld_collect_errors(sdcp, sta);
|
|
|
|
if (n > 1)
|
|
|
|
sdc_lld_send_cmd_short_crc(sdcp, MMCSD_CMD_STOP_TRANSMISSION, 0, resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver interrupt handlers. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
#if !defined(STM32_SDIO_HANDLER)
|
|
|
|
#error "STM32_SDIO_HANDLER not defined"
|
|
|
|
#endif
|
|
|
|
/**
|
|
|
|
* @brief SDIO IRQ handler.
|
|
|
|
* @details It just wakes transaction thread. All error handling performs in
|
|
|
|
* that thread.
|
|
|
|
*
|
|
|
|
* @isr
|
|
|
|
*/
|
2013-09-20 12:25:11 +00:00
|
|
|
OSAL_IRQ_HANDLER(STM32_SDIO_HANDLER) {
|
2013-08-04 13:38:53 +00:00
|
|
|
|
2013-09-20 12:25:11 +00:00
|
|
|
OSAL_IRQ_PROLOGUE();
|
2013-08-04 13:38:53 +00:00
|
|
|
|
2013-09-20 12:25:11 +00:00
|
|
|
osalSysLockFromISR();
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
/* Disables the source but the status flags are not reset because the
|
|
|
|
read/write functions needs to check them.*/
|
|
|
|
SDIO->MASK = 0;
|
|
|
|
|
2013-09-20 12:25:11 +00:00
|
|
|
osalThreadResumeI(&SDCD1.thread, MSG_OK);
|
2013-08-04 13:38:53 +00:00
|
|
|
|
2013-09-20 12:25:11 +00:00
|
|
|
osalSysUnlockFromISR();
|
2013-08-04 13:38:53 +00:00
|
|
|
|
2013-09-20 12:25:11 +00:00
|
|
|
OSAL_IRQ_EPILOGUE();
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Low level SDC driver initialization.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void sdc_lld_init(void) {
|
|
|
|
|
|
|
|
sdcObjectInit(&SDCD1);
|
|
|
|
SDCD1.thread = NULL;
|
|
|
|
SDCD1.dma = STM32_DMA_STREAM(STM32_SDC_SDIO_DMA_STREAM);
|
|
|
|
SDCD1.sdio = SDIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Configures and activates the SDC peripheral.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void sdc_lld_start(SDCDriver *sdcp) {
|
|
|
|
|
2015-04-20 09:58:32 +00:00
|
|
|
/* Checking configuration, using a default if NULL has been passed.*/
|
|
|
|
if (sdcp->config == NULL) {
|
|
|
|
sdcp->config = &sdc_default_cfg;
|
|
|
|
}
|
|
|
|
|
2013-08-04 13:38:53 +00:00
|
|
|
sdcp->dmamode = STM32_DMA_CR_CHSEL(DMA_CHANNEL) |
|
|
|
|
STM32_DMA_CR_PL(STM32_SDC_SDIO_DMA_PRIORITY) |
|
|
|
|
STM32_DMA_CR_PSIZE_WORD |
|
|
|
|
STM32_DMA_CR_MSIZE_WORD |
|
|
|
|
STM32_DMA_CR_MINC;
|
|
|
|
|
|
|
|
#if (defined(STM32F4XX) || defined(STM32F2XX))
|
|
|
|
sdcp->dmamode |= STM32_DMA_CR_PFCTRL |
|
|
|
|
STM32_DMA_CR_PBURST_INCR4 |
|
|
|
|
STM32_DMA_CR_MBURST_INCR4;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (sdcp->state == BLK_STOP) {
|
|
|
|
/* Note, the DMA must be enabled before the IRQs.*/
|
2013-09-20 12:25:11 +00:00
|
|
|
bool b;
|
2013-08-04 13:38:53 +00:00
|
|
|
b = dmaStreamAllocate(sdcp->dma, STM32_SDC_SDIO_IRQ_PRIORITY, NULL, NULL);
|
2013-09-20 12:25:11 +00:00
|
|
|
osalDbgAssert(!b, "stream already allocated");
|
2015-05-12 08:12:19 +00:00
|
|
|
dmaStreamSetPeripheral(sdcp->dma, &sdcp->sdio->FIFO);
|
2013-08-04 13:38:53 +00:00
|
|
|
#if (defined(STM32F4XX) || defined(STM32F2XX))
|
|
|
|
dmaStreamSetFIFO(sdcp->dma, STM32_DMA_FCR_DMDIS | STM32_DMA_FCR_FTH_FULL);
|
|
|
|
#endif
|
2013-09-20 12:25:11 +00:00
|
|
|
nvicEnableVector(STM32_SDIO_NUMBER, STM32_SDC_SDIO_IRQ_PRIORITY);
|
2013-08-04 13:38:53 +00:00
|
|
|
rccEnableSDIO(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configuration, card clock is initially stopped.*/
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->POWER = 0;
|
|
|
|
sdcp->sdio->CLKCR = 0;
|
|
|
|
sdcp->sdio->DCTRL = 0;
|
|
|
|
sdcp->sdio->DTIMER = 0;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Deactivates the SDC peripheral.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void sdc_lld_stop(SDCDriver *sdcp) {
|
|
|
|
|
|
|
|
if (sdcp->state != BLK_STOP) {
|
|
|
|
|
|
|
|
/* SDIO deactivation.*/
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->POWER = 0;
|
|
|
|
sdcp->sdio->CLKCR = 0;
|
|
|
|
sdcp->sdio->DCTRL = 0;
|
|
|
|
sdcp->sdio->DTIMER = 0;
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
/* Clock deactivation.*/
|
|
|
|
nvicDisableVector(STM32_SDIO_NUMBER);
|
|
|
|
dmaStreamRelease(sdcp->dma);
|
|
|
|
rccDisableSDIO(FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Starts the SDIO clock and sets it to init mode (400kHz or less).
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void sdc_lld_start_clk(SDCDriver *sdcp) {
|
|
|
|
|
|
|
|
/* Initial clock setting: 400kHz, 1bit mode.*/
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->CLKCR = STM32_SDIO_DIV_LS;
|
|
|
|
sdcp->sdio->POWER |= SDIO_POWER_PWRCTRL_0 | SDIO_POWER_PWRCTRL_1;
|
|
|
|
sdcp->sdio->CLKCR |= SDIO_CLKCR_CLKEN;
|
2014-02-08 15:50:16 +00:00
|
|
|
|
|
|
|
/* Clock activation delay.*/
|
2015-09-17 12:23:10 +00:00
|
|
|
osalThreadSleep(OSAL_MS2ST(STM32_SDC_CLOCK_ACTIVATION_DELAY));
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sets the SDIO clock to data mode (25MHz or less).
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
2015-04-06 09:55:44 +00:00
|
|
|
* @param[in] clk the clock mode
|
2013-08-04 13:38:53 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2015-02-18 13:46:06 +00:00
|
|
|
void sdc_lld_set_data_clk(SDCDriver *sdcp, sdcbusclk_t clk) {
|
2015-05-12 08:12:19 +00:00
|
|
|
#if 0
|
2015-02-18 13:46:06 +00:00
|
|
|
if (SDC_CLK_50MHz == clk) {
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->CLKCR = (sdcp->sdio->CLKCR & 0xFFFFFF00U) | STM32_SDIO_DIV_HS
|
|
|
|
| SDIO_CLKCR_BYPASS;
|
2015-02-18 13:46:06 +00:00
|
|
|
}
|
|
|
|
else
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->CLKCR = (sdcp->sdio->CLKCR & 0xFFFFFF00U) | STM32_SDIO_DIV_HS;
|
|
|
|
#else
|
|
|
|
(void)clk;
|
|
|
|
|
|
|
|
sdcp->sdio->CLKCR = (sdcp->sdio->CLKCR & 0xFFFFFF00U) | STM32_SDIO_DIV_HS;
|
|
|
|
#endif
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Stops the SDIO clock.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void sdc_lld_stop_clk(SDCDriver *sdcp) {
|
|
|
|
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->CLKCR = 0;
|
|
|
|
sdcp->sdio->POWER = 0;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Switches the bus to 4 bits mode.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
* @param[in] mode bus mode
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void sdc_lld_set_bus_mode(SDCDriver *sdcp, sdcbusmode_t mode) {
|
2015-05-12 08:12:19 +00:00
|
|
|
uint32_t clk = sdcp->sdio->CLKCR & ~SDIO_CLKCR_WIDBUS;
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case SDC_MODE_1BIT:
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->CLKCR = clk;
|
2013-08-04 13:38:53 +00:00
|
|
|
break;
|
|
|
|
case SDC_MODE_4BIT:
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->CLKCR = clk | SDIO_CLKCR_WIDBUS_0;
|
2013-08-04 13:38:53 +00:00
|
|
|
break;
|
|
|
|
case SDC_MODE_8BIT:
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->CLKCR = clk | SDIO_CLKCR_WIDBUS_1;
|
2013-08-04 13:38:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sends an SDIO command with no response expected.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
* @param[in] cmd card command
|
|
|
|
* @param[in] arg command argument
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void sdc_lld_send_cmd_none(SDCDriver *sdcp, uint8_t cmd, uint32_t arg) {
|
|
|
|
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->ARG = arg;
|
|
|
|
sdcp->sdio->CMD = (uint32_t)cmd | SDIO_CMD_CPSMEN;
|
|
|
|
while ((sdcp->sdio->STA & SDIO_STA_CMDSENT) == 0)
|
2013-08-04 13:38:53 +00:00
|
|
|
;
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->ICR = SDIO_ICR_CMDSENTC;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sends an SDIO command with a short response expected.
|
|
|
|
* @note The CRC is not verified.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
* @param[in] cmd card command
|
|
|
|
* @param[in] arg command argument
|
|
|
|
* @param[out] resp pointer to the response buffer (one word)
|
|
|
|
*
|
|
|
|
* @return The operation status.
|
2013-09-20 12:25:11 +00:00
|
|
|
* @retval HAL_SUCCESS operation succeeded.
|
|
|
|
* @retval HAL_FAILED operation failed.
|
2013-08-04 13:38:53 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2013-09-20 12:25:11 +00:00
|
|
|
bool sdc_lld_send_cmd_short(SDCDriver *sdcp, uint8_t cmd, uint32_t arg,
|
|
|
|
uint32_t *resp) {
|
2013-08-04 13:38:53 +00:00
|
|
|
uint32_t sta;
|
|
|
|
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->ARG = arg;
|
|
|
|
sdcp->sdio->CMD = (uint32_t)cmd | SDIO_CMD_WAITRESP_0 | SDIO_CMD_CPSMEN;
|
|
|
|
while (((sta = sdcp->sdio->STA) & (SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT |
|
|
|
|
SDIO_STA_CCRCFAIL)) == 0)
|
2013-08-04 13:38:53 +00:00
|
|
|
;
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->ICR = sta & (SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT |
|
|
|
|
SDIO_STA_CCRCFAIL);
|
2013-08-04 13:38:53 +00:00
|
|
|
if ((sta & (SDIO_STA_CTIMEOUT)) != 0) {
|
|
|
|
sdc_lld_collect_errors(sdcp, sta);
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_FAILED;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
2015-05-12 08:12:19 +00:00
|
|
|
*resp = sdcp->sdio->RESP1;
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_SUCCESS;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sends an SDIO command with a short response expected and CRC.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
* @param[in] cmd card command
|
|
|
|
* @param[in] arg command argument
|
|
|
|
* @param[out] resp pointer to the response buffer (one word)
|
|
|
|
*
|
|
|
|
* @return The operation status.
|
2013-09-20 12:25:11 +00:00
|
|
|
* @retval HAL_SUCCESS operation succeeded.
|
|
|
|
* @retval HAL_FAILED operation failed.
|
2013-08-04 13:38:53 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2013-09-20 12:25:11 +00:00
|
|
|
bool sdc_lld_send_cmd_short_crc(SDCDriver *sdcp, uint8_t cmd, uint32_t arg,
|
|
|
|
uint32_t *resp) {
|
2013-08-04 13:38:53 +00:00
|
|
|
uint32_t sta;
|
|
|
|
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->ARG = arg;
|
|
|
|
sdcp->sdio->CMD = (uint32_t)cmd | SDIO_CMD_WAITRESP_0 | SDIO_CMD_CPSMEN;
|
|
|
|
while (((sta = sdcp->sdio->STA) & (SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT |
|
|
|
|
SDIO_STA_CCRCFAIL)) == 0)
|
2013-08-04 13:38:53 +00:00
|
|
|
;
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->ICR = sta & (SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT | SDIO_STA_CCRCFAIL);
|
2013-08-04 13:38:53 +00:00
|
|
|
if ((sta & (SDIO_STA_CTIMEOUT | SDIO_STA_CCRCFAIL)) != 0) {
|
|
|
|
sdc_lld_collect_errors(sdcp, sta);
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_FAILED;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
2015-05-12 08:12:19 +00:00
|
|
|
*resp = sdcp->sdio->RESP1;
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_SUCCESS;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sends an SDIO command with a long response expected and CRC.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
* @param[in] cmd card command
|
|
|
|
* @param[in] arg command argument
|
|
|
|
* @param[out] resp pointer to the response buffer (four words)
|
|
|
|
*
|
|
|
|
* @return The operation status.
|
2013-09-20 12:25:11 +00:00
|
|
|
* @retval HAL_SUCCESS operation succeeded.
|
|
|
|
* @retval HAL_FAILED operation failed.
|
2013-08-04 13:38:53 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2013-09-20 12:25:11 +00:00
|
|
|
bool sdc_lld_send_cmd_long_crc(SDCDriver *sdcp, uint8_t cmd, uint32_t arg,
|
|
|
|
uint32_t *resp) {
|
2013-08-04 13:38:53 +00:00
|
|
|
uint32_t sta;
|
|
|
|
|
|
|
|
(void)sdcp;
|
|
|
|
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->ARG = arg;
|
|
|
|
sdcp->sdio->CMD = (uint32_t)cmd | SDIO_CMD_WAITRESP_0 | SDIO_CMD_WAITRESP_1 |
|
|
|
|
SDIO_CMD_CPSMEN;
|
|
|
|
while (((sta = sdcp->sdio->STA) & (SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT |
|
|
|
|
SDIO_STA_CCRCFAIL)) == 0)
|
2013-08-04 13:38:53 +00:00
|
|
|
;
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->ICR = sta & (SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT |
|
|
|
|
SDIO_STA_CCRCFAIL);
|
2013-08-04 13:38:53 +00:00
|
|
|
if ((sta & (STM32_SDIO_STA_ERROR_MASK)) != 0) {
|
|
|
|
sdc_lld_collect_errors(sdcp, sta);
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_FAILED;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
/* Save bytes in reverse order because MSB in response comes first.*/
|
2015-05-12 08:12:19 +00:00
|
|
|
*resp++ = sdcp->sdio->RESP4;
|
|
|
|
*resp++ = sdcp->sdio->RESP3;
|
|
|
|
*resp++ = sdcp->sdio->RESP2;
|
|
|
|
*resp = sdcp->sdio->RESP1;
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_SUCCESS;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-11 14:33:14 +00:00
|
|
|
* @brief Reads special registers using data bus.
|
|
|
|
* @details Needs only during card detection procedure.
|
2013-08-04 13:38:53 +00:00
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
* @param[out] buf pointer to the read buffer
|
2015-05-11 14:33:14 +00:00
|
|
|
* @param[in] bytes number of bytes to read
|
|
|
|
* @param[in] cmd card command
|
|
|
|
* @param[in] arg argument for command
|
2013-08-04 13:38:53 +00:00
|
|
|
*
|
|
|
|
* @return The operation status.
|
2013-09-20 12:25:11 +00:00
|
|
|
* @retval HAL_SUCCESS operation succeeded.
|
|
|
|
* @retval HAL_FAILED operation failed.
|
2013-08-04 13:38:53 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2015-05-11 14:33:14 +00:00
|
|
|
bool sdc_lld_read_special(SDCDriver *sdcp, uint8_t *buf, size_t bytes,
|
|
|
|
uint8_t cmd, uint32_t arg) {
|
2013-08-04 13:38:53 +00:00
|
|
|
uint32_t resp[1];
|
|
|
|
|
2015-05-11 14:33:14 +00:00
|
|
|
if(sdc_lld_prepare_read_bytes(sdcp, buf, bytes))
|
2015-02-18 13:46:06 +00:00
|
|
|
goto error;
|
2013-08-04 13:38:53 +00:00
|
|
|
|
2015-05-11 14:33:14 +00:00
|
|
|
if (sdc_lld_send_cmd_short_crc(sdcp, cmd, arg, resp)
|
|
|
|
|| MMCSD_R1_ERROR(resp[0]))
|
2015-02-18 13:46:06 +00:00
|
|
|
goto error;
|
2013-08-04 13:38:53 +00:00
|
|
|
|
2015-05-11 14:33:14 +00:00
|
|
|
if (sdc_lld_wait_transaction_end(sdcp, 1, resp))
|
2015-02-18 13:46:06 +00:00
|
|
|
goto error;
|
2013-08-04 13:38:53 +00:00
|
|
|
|
2015-02-18 13:46:06 +00:00
|
|
|
return HAL_SUCCESS;
|
2013-08-04 13:38:53 +00:00
|
|
|
|
2015-02-18 13:46:06 +00:00
|
|
|
error:
|
2015-05-11 14:33:14 +00:00
|
|
|
sdc_lld_error_cleanup(sdcp, 1, resp);
|
2015-02-18 13:46:06 +00:00
|
|
|
return HAL_FAILED;
|
|
|
|
}
|
2013-08-04 13:38:53 +00:00
|
|
|
|
2015-02-18 13:46:06 +00:00
|
|
|
/**
|
2015-05-11 14:33:14 +00:00
|
|
|
* @brief Reads one or more blocks.
|
2015-02-18 13:46:06 +00:00
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
2015-05-11 14:33:14 +00:00
|
|
|
* @param[in] startblk first block to read
|
2015-02-18 13:46:06 +00:00
|
|
|
* @param[out] buf pointer to the read buffer
|
2015-05-11 14:33:14 +00:00
|
|
|
* @param[in] blocks number of blocks to read
|
2015-02-18 13:46:06 +00:00
|
|
|
*
|
|
|
|
* @return The operation status.
|
|
|
|
* @retval HAL_SUCCESS operation succeeded.
|
|
|
|
* @retval HAL_FAILED operation failed.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2015-05-11 14:33:14 +00:00
|
|
|
bool sdc_lld_read_aligned(SDCDriver *sdcp, uint32_t startblk,
|
|
|
|
uint8_t *buf, uint32_t blocks) {
|
2015-02-18 13:46:06 +00:00
|
|
|
uint32_t resp[1];
|
2014-01-18 09:16:25 +00:00
|
|
|
|
2015-05-11 14:33:14 +00:00
|
|
|
osalDbgCheck(blocks < 0x1000000 / MMCSD_BLOCK_SIZE);
|
2015-02-18 13:46:06 +00:00
|
|
|
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->DTIMER = STM32_SDC_READ_TIMEOUT;
|
2015-05-11 14:33:14 +00:00
|
|
|
|
|
|
|
/* Checks for errors and waits for the card to be ready for reading.*/
|
|
|
|
if (_sdc_wait_for_transfer_state(sdcp))
|
|
|
|
return HAL_FAILED;
|
|
|
|
|
|
|
|
/* Prepares the DMA channel for writing.*/
|
|
|
|
dmaStreamSetMemory0(sdcp->dma, buf);
|
|
|
|
dmaStreamSetTransactionSize(sdcp->dma,
|
|
|
|
(blocks * MMCSD_BLOCK_SIZE) / sizeof (uint32_t));
|
|
|
|
dmaStreamSetMode(sdcp->dma, sdcp->dmamode | STM32_DMA_CR_DIR_P2M);
|
|
|
|
dmaStreamEnable(sdcp->dma);
|
|
|
|
|
|
|
|
/* Setting up data transfer.*/
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->ICR = STM32_SDIO_ICR_ALL_FLAGS;
|
|
|
|
sdcp->sdio->MASK = SDIO_MASK_DCRCFAILIE |
|
|
|
|
SDIO_MASK_DTIMEOUTIE |
|
|
|
|
SDIO_MASK_STBITERRIE |
|
|
|
|
SDIO_MASK_RXOVERRIE |
|
|
|
|
SDIO_MASK_DATAENDIE;
|
|
|
|
sdcp->sdio->DLEN = blocks * MMCSD_BLOCK_SIZE;
|
2015-05-11 14:33:14 +00:00
|
|
|
|
|
|
|
/* Transaction starts just after DTEN bit setting.*/
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->DCTRL = SDIO_DCTRL_DTDIR |
|
|
|
|
SDIO_DCTRL_DBLOCKSIZE_3 |
|
|
|
|
SDIO_DCTRL_DBLOCKSIZE_0 |
|
|
|
|
SDIO_DCTRL_DMAEN |
|
|
|
|
SDIO_DCTRL_DTEN;
|
2015-05-11 14:33:14 +00:00
|
|
|
|
|
|
|
if (sdc_lld_prepare_read(sdcp, startblk, blocks, resp) == TRUE)
|
2014-01-18 09:16:25 +00:00
|
|
|
goto error;
|
2015-02-18 13:46:06 +00:00
|
|
|
|
2015-05-11 14:33:14 +00:00
|
|
|
if (sdc_lld_wait_transaction_end(sdcp, blocks, resp) == TRUE)
|
2013-08-04 13:38:53 +00:00
|
|
|
goto error;
|
|
|
|
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_SUCCESS;
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
error:
|
2015-05-11 14:33:14 +00:00
|
|
|
sdc_lld_error_cleanup(sdcp, blocks, resp);
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_FAILED;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Writes one or more blocks.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
* @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.
|
2013-09-20 12:25:11 +00:00
|
|
|
* @retval HAL_SUCCESS operation succeeded.
|
|
|
|
* @retval HAL_FAILED operation failed.
|
2013-08-04 13:38:53 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2013-09-20 12:25:11 +00:00
|
|
|
bool sdc_lld_write_aligned(SDCDriver *sdcp, uint32_t startblk,
|
2015-02-18 13:46:06 +00:00
|
|
|
const uint8_t *buf, uint32_t blocks) {
|
2013-08-04 13:38:53 +00:00
|
|
|
uint32_t resp[1];
|
|
|
|
|
2015-02-18 13:46:06 +00:00
|
|
|
osalDbgCheck(blocks < 0x1000000 / MMCSD_BLOCK_SIZE);
|
2013-08-04 13:38:53 +00:00
|
|
|
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->DTIMER = STM32_SDC_WRITE_TIMEOUT;
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
/* Checks for errors and waits for the card to be ready for writing.*/
|
|
|
|
if (_sdc_wait_for_transfer_state(sdcp))
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_FAILED;
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
/* Prepares the DMA channel for writing.*/
|
|
|
|
dmaStreamSetMemory0(sdcp->dma, buf);
|
|
|
|
dmaStreamSetTransactionSize(sdcp->dma,
|
2015-02-18 13:46:06 +00:00
|
|
|
(blocks * MMCSD_BLOCK_SIZE) / sizeof (uint32_t));
|
2013-08-04 13:38:53 +00:00
|
|
|
dmaStreamSetMode(sdcp->dma, sdcp->dmamode | STM32_DMA_CR_DIR_M2P);
|
|
|
|
dmaStreamEnable(sdcp->dma);
|
|
|
|
|
|
|
|
/* Setting up data transfer.*/
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->ICR = STM32_SDIO_ICR_ALL_FLAGS;
|
|
|
|
sdcp->sdio->MASK = SDIO_MASK_DCRCFAILIE |
|
|
|
|
SDIO_MASK_DTIMEOUTIE |
|
|
|
|
SDIO_MASK_STBITERRIE |
|
|
|
|
SDIO_MASK_TXUNDERRIE |
|
|
|
|
SDIO_MASK_DATAENDIE;
|
|
|
|
sdcp->sdio->DLEN = blocks * MMCSD_BLOCK_SIZE;
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
/* Talk to card what we want from it.*/
|
2015-02-18 13:46:06 +00:00
|
|
|
if (sdc_lld_prepare_write(sdcp, startblk, blocks, resp) == TRUE)
|
2013-08-04 13:38:53 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* Transaction starts just after DTEN bit setting.*/
|
2015-05-12 08:12:19 +00:00
|
|
|
sdcp->sdio->DCTRL = SDIO_DCTRL_DBLOCKSIZE_3 |
|
|
|
|
SDIO_DCTRL_DBLOCKSIZE_0 |
|
|
|
|
SDIO_DCTRL_DMAEN |
|
|
|
|
SDIO_DCTRL_DTEN;
|
2015-05-11 14:33:14 +00:00
|
|
|
|
2015-02-18 13:46:06 +00:00
|
|
|
if (sdc_lld_wait_transaction_end(sdcp, blocks, resp) == TRUE)
|
2013-08-04 13:38:53 +00:00
|
|
|
goto error;
|
|
|
|
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_SUCCESS;
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
error:
|
2015-02-18 13:46:06 +00:00
|
|
|
sdc_lld_error_cleanup(sdcp, blocks, resp);
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_FAILED;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Reads one or more blocks.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
* @param[in] startblk first block to read
|
|
|
|
* @param[out] buf pointer to the read buffer
|
2015-02-18 13:46:06 +00:00
|
|
|
* @param[in] blocks number of blocks to read
|
2013-08-04 13:38:53 +00:00
|
|
|
*
|
|
|
|
* @return The operation status.
|
2013-09-20 12:25:11 +00:00
|
|
|
* @retval HAL_SUCCESS operation succeeded.
|
|
|
|
* @retval HAL_FAILED operation failed.
|
2013-08-04 13:38:53 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2013-09-20 12:25:11 +00:00
|
|
|
bool sdc_lld_read(SDCDriver *sdcp, uint32_t startblk,
|
2015-02-18 13:46:06 +00:00
|
|
|
uint8_t *buf, uint32_t blocks) {
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
#if STM32_SDC_SDIO_UNALIGNED_SUPPORT
|
|
|
|
if (((unsigned)buf & 3) != 0) {
|
|
|
|
uint32_t i;
|
2015-02-18 13:46:06 +00:00
|
|
|
for (i = 0; i < blocks; i++) {
|
2013-08-04 13:38:53 +00:00
|
|
|
if (sdc_lld_read_aligned(sdcp, startblk, u.buf, 1))
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_FAILED;
|
2013-08-04 13:38:53 +00:00
|
|
|
memcpy(buf, u.buf, MMCSD_BLOCK_SIZE);
|
|
|
|
buf += MMCSD_BLOCK_SIZE;
|
|
|
|
startblk++;
|
|
|
|
}
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_SUCCESS;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
#endif /* STM32_SDC_SDIO_UNALIGNED_SUPPORT */
|
2015-02-18 13:46:06 +00:00
|
|
|
return sdc_lld_read_aligned(sdcp, startblk, buf, blocks);
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Writes one or more blocks.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
* @param[in] startblk first block to write
|
|
|
|
* @param[out] buf pointer to the write buffer
|
2015-02-18 13:46:06 +00:00
|
|
|
* @param[in] blocks number of blocks to write
|
2013-08-04 13:38:53 +00:00
|
|
|
*
|
|
|
|
* @return The operation status.
|
2013-09-20 12:25:11 +00:00
|
|
|
* @retval HAL_SUCCESS operation succeeded.
|
|
|
|
* @retval HAL_FAILED operation failed.
|
2013-08-04 13:38:53 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2013-09-20 12:25:11 +00:00
|
|
|
bool sdc_lld_write(SDCDriver *sdcp, uint32_t startblk,
|
2015-02-18 13:46:06 +00:00
|
|
|
const uint8_t *buf, uint32_t blocks) {
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
#if STM32_SDC_SDIO_UNALIGNED_SUPPORT
|
|
|
|
if (((unsigned)buf & 3) != 0) {
|
|
|
|
uint32_t i;
|
2015-02-18 13:46:06 +00:00
|
|
|
for (i = 0; i < blocks; i++) {
|
2013-08-04 13:38:53 +00:00
|
|
|
memcpy(u.buf, buf, MMCSD_BLOCK_SIZE);
|
|
|
|
buf += MMCSD_BLOCK_SIZE;
|
|
|
|
if (sdc_lld_write_aligned(sdcp, startblk, u.buf, 1))
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_FAILED;
|
2013-08-04 13:38:53 +00:00
|
|
|
startblk++;
|
|
|
|
}
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_SUCCESS;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
#endif /* STM32_SDC_SDIO_UNALIGNED_SUPPORT */
|
2015-02-18 13:46:06 +00:00
|
|
|
return sdc_lld_write_aligned(sdcp, startblk, buf, blocks);
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Waits for card idle condition.
|
|
|
|
*
|
|
|
|
* @param[in] sdcp pointer to the @p SDCDriver object
|
|
|
|
*
|
|
|
|
* @return The operation status.
|
2013-09-20 12:25:11 +00:00
|
|
|
* @retval HAL_SUCCESS the operation succeeded.
|
|
|
|
* @retval HAL_FAILED the operation failed.
|
2013-08-04 13:38:53 +00:00
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2013-09-20 12:25:11 +00:00
|
|
|
bool sdc_lld_sync(SDCDriver *sdcp) {
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
/* TODO: Implement.*/
|
|
|
|
(void)sdcp;
|
2013-09-20 12:25:11 +00:00
|
|
|
return HAL_SUCCESS;
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAL_USE_SDC */
|
|
|
|
|
|
|
|
/** @} */
|