mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Thu Aug 21 15:00:08 2014 +0100
Revision:
296:ec1b66a3d094
Parent:
targets/cmsis/TARGET_STM/TARGET_DISCO_F407VG/stm32f4xx_hal_sd.c@242:7074e42da0b2
Synchronized with git revision bbc120c4786e99dfa586e7a13f8638064f1e5938

Full URL: https://github.com/mbedmicro/mbed/commit/bbc120c4786e99dfa586e7a13f8638064f1e5938/

DISCO_F407VG - add USBDevice support and a variant - ARCH_MAX

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 133:d4dda5c437f0 1 /**
mbed_official 133:d4dda5c437f0 2 ******************************************************************************
mbed_official 133:d4dda5c437f0 3 * @file stm32f4xx_hal_sd.c
mbed_official 133:d4dda5c437f0 4 * @author MCD Application Team
mbed_official 242:7074e42da0b2 5 * @version V1.1.0RC2
mbed_official 242:7074e42da0b2 6 * @date 14-May-2014
mbed_official 133:d4dda5c437f0 7 * @brief SD card HAL module driver.
mbed_official 133:d4dda5c437f0 8 * This file provides firmware functions to manage the following
mbed_official 133:d4dda5c437f0 9 * functionalities of the Secure Digital (SD) peripheral:
mbed_official 133:d4dda5c437f0 10 * + Initialization and de-initialization functions
mbed_official 133:d4dda5c437f0 11 * + IO operation functions
mbed_official 133:d4dda5c437f0 12 * + Peripheral Control functions
mbed_official 133:d4dda5c437f0 13 * + Peripheral State functions
mbed_official 133:d4dda5c437f0 14 *
mbed_official 133:d4dda5c437f0 15 @verbatim
mbed_official 133:d4dda5c437f0 16 ==============================================================================
mbed_official 133:d4dda5c437f0 17 ##### How to use this driver #####
mbed_official 133:d4dda5c437f0 18 ==============================================================================
mbed_official 133:d4dda5c437f0 19 [..]
mbed_official 133:d4dda5c437f0 20 This driver implements a high level communication layer for read and write from/to
mbed_official 133:d4dda5c437f0 21 this memory. The needed STM32 hardware resources (SDIO and GPIO) are performed by
mbed_official 133:d4dda5c437f0 22 the user in HAL_SD_MspInit() function (MSP layer).
mbed_official 133:d4dda5c437f0 23 Basically, the MSP layer configuration should be the same as we provide in the
mbed_official 133:d4dda5c437f0 24 examples.
mbed_official 133:d4dda5c437f0 25 You can easily tailor this configuration according to hardware resources.
mbed_official 133:d4dda5c437f0 26
mbed_official 133:d4dda5c437f0 27 [..]
mbed_official 133:d4dda5c437f0 28 This driver is a generic layered driver for SDIO memories which uses the HAL
mbed_official 133:d4dda5c437f0 29 SDIO driver functions to interface with SD and uSD cards devices.
mbed_official 133:d4dda5c437f0 30 It is used as follows:
mbed_official 133:d4dda5c437f0 31
mbed_official 133:d4dda5c437f0 32 (#)Initialize the SDIO low level resources by implement the HAL_SD_MspInit() API:
mbed_official 133:d4dda5c437f0 33 (##) Enable the SDIO interface clock using __SDIO_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 34 (##) SDIO pins configuration for SD card
mbed_official 133:d4dda5c437f0 35 (+++) Enable the clock for the SDIO GPIOs using the functions __GPIOx_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 36 (+++) Configure these SDIO pins as alternate function pull-up using HAL_GPIO_Init()
mbed_official 133:d4dda5c437f0 37 and according to your pin assignment;
mbed_official 133:d4dda5c437f0 38 (##) DMA Configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
mbed_official 133:d4dda5c437f0 39 and HAL_SD_WriteBlocks_DMA() APIs).
mbed_official 133:d4dda5c437f0 40 (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 41 (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
mbed_official 133:d4dda5c437f0 42 (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
mbed_official 133:d4dda5c437f0 43 (+++) Configure the SDIO and DMA interrupt priorities using functions
mbed_official 133:d4dda5c437f0 44 HAL_NVIC_SetPriority(); DMA priority is superior to SDIO's priority
mbed_official 133:d4dda5c437f0 45 (+++) Enable the NVIC DMA and SDIO IRQs using function HAL_NVIC_EnableIRQ()
mbed_official 133:d4dda5c437f0 46 (+++) SDIO interrupts are managed using the macros __HAL_SD_SDIO_ENABLE_IT()
mbed_official 133:d4dda5c437f0 47 and __HAL_SD_SDIO_DISABLE_IT() inside the communication process.
mbed_official 133:d4dda5c437f0 48 (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_SDIO_GET_IT()
mbed_official 133:d4dda5c437f0 49 and __HAL_SD_SDIO_CLEAR_IT()
mbed_official 133:d4dda5c437f0 50 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
mbed_official 133:d4dda5c437f0 51
mbed_official 133:d4dda5c437f0 52
mbed_official 133:d4dda5c437f0 53 *** SD Card Initialization and configuration ***
mbed_official 133:d4dda5c437f0 54 ================================================
mbed_official 133:d4dda5c437f0 55 [..]
mbed_official 133:d4dda5c437f0 56 To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
mbed_official 133:d4dda5c437f0 57 the SD Card and put it into StandBy State (Ready for data transfer).
mbed_official 133:d4dda5c437f0 58 This function provide the following operations:
mbed_official 133:d4dda5c437f0 59
mbed_official 133:d4dda5c437f0 60 (#) Apply the SD Card initialization process at 400KHz and check the SD Card
mbed_official 133:d4dda5c437f0 61 type (Standard Capacity or High Capacity). You can change or adapt this
mbed_official 133:d4dda5c437f0 62 frequency by adjusting the "ClockDiv" field.
mbed_official 133:d4dda5c437f0 63 The SD Card frequency (SDIO_CK) is computed as follows:
mbed_official 133:d4dda5c437f0 64
mbed_official 133:d4dda5c437f0 65 SDIO_CK = SDIOCLK / (ClockDiv + 2)
mbed_official 133:d4dda5c437f0 66
mbed_official 133:d4dda5c437f0 67 In initialization mode and according to the SD Card standard,
mbed_official 133:d4dda5c437f0 68 make sure that the SDIO_CK frequency doesn't exceed 400KHz.
mbed_official 133:d4dda5c437f0 69
mbed_official 133:d4dda5c437f0 70 (#) Get the SD CID and CSD data. All these information are managed by the SDCardInfo
mbed_official 133:d4dda5c437f0 71 structure. This structure provide also ready computed SD Card capacity
mbed_official 133:d4dda5c437f0 72 and Block size.
mbed_official 133:d4dda5c437f0 73
mbed_official 133:d4dda5c437f0 74 -@- These information are stored in SD handle structure in case of future use.
mbed_official 133:d4dda5c437f0 75
mbed_official 133:d4dda5c437f0 76 (#) Configure the SD Card Data transfer frequency. By Default, the card transfer
mbed_official 133:d4dda5c437f0 77 frequency is set to 24MHz. You can change or adapt this frequency by adjusting
mbed_official 133:d4dda5c437f0 78 the "ClockDiv" field.
mbed_official 133:d4dda5c437f0 79 The SD Card frequency (SDIO_CK) is computed as follows:
mbed_official 133:d4dda5c437f0 80
mbed_official 133:d4dda5c437f0 81 SDIO_CK = SDIOCLK / (ClockDiv + 2)
mbed_official 133:d4dda5c437f0 82
mbed_official 133:d4dda5c437f0 83 In transfer mode and according to the SD Card standard, make sure that the
mbed_official 133:d4dda5c437f0 84 SDIO_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch.
mbed_official 133:d4dda5c437f0 85 To be able to use a frequency higher than 24MHz, you should use the SDIO
mbed_official 133:d4dda5c437f0 86 peripheral in bypass mode. Refer to the corresponding reference manual
mbed_official 133:d4dda5c437f0 87 for more details.
mbed_official 133:d4dda5c437f0 88
mbed_official 133:d4dda5c437f0 89 (#) Select the corresponding SD Card according to the address read with the step 2.
mbed_official 133:d4dda5c437f0 90
mbed_official 133:d4dda5c437f0 91 (#) Configure the SD Card in wide bus mode: 4-bits data.
mbed_official 133:d4dda5c437f0 92
mbed_official 133:d4dda5c437f0 93 *** SD Card Read operation ***
mbed_official 133:d4dda5c437f0 94 ==============================
mbed_official 133:d4dda5c437f0 95 [..]
mbed_official 133:d4dda5c437f0 96 (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
mbed_official 133:d4dda5c437f0 97 This function support only 512-byte block length (the block size should be
mbed_official 133:d4dda5c437f0 98 chosen as 512 byte).
mbed_official 133:d4dda5c437f0 99 You can choose either one block read operation or multiple block read operation
mbed_official 133:d4dda5c437f0 100 by adjusting the "NumberOfBlocks" parameter.
mbed_official 133:d4dda5c437f0 101
mbed_official 133:d4dda5c437f0 102 (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
mbed_official 133:d4dda5c437f0 103 This function support only 512-byte block length (the block size should be
mbed_official 133:d4dda5c437f0 104 chosen as 512 byte).
mbed_official 133:d4dda5c437f0 105 You can choose either one block read operation or multiple block read operation
mbed_official 133:d4dda5c437f0 106 by adjusting the "NumberOfBlocks" parameter.
mbed_official 133:d4dda5c437f0 107 After this, you have to call the function HAL_SD_CheckReadOperation(), to insure
mbed_official 133:d4dda5c437f0 108 that the read transfer is done correctly in both DMA and SD sides.
mbed_official 133:d4dda5c437f0 109
mbed_official 133:d4dda5c437f0 110 *** SD Card Write operation ***
mbed_official 133:d4dda5c437f0 111 ===============================
mbed_official 133:d4dda5c437f0 112 [..]
mbed_official 133:d4dda5c437f0 113 (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
mbed_official 133:d4dda5c437f0 114 This function support only 512-byte block length (the block size should be
mbed_official 133:d4dda5c437f0 115 chosen as 512 byte).
mbed_official 133:d4dda5c437f0 116 You can choose either one block read operation or multiple block read operation
mbed_official 133:d4dda5c437f0 117 by adjusting the "NumberOfBlocks" parameter.
mbed_official 133:d4dda5c437f0 118
mbed_official 133:d4dda5c437f0 119 (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
mbed_official 133:d4dda5c437f0 120 This function support only 512-byte block length (the block size should be
mbed_official 133:d4dda5c437f0 121 chosen as 512 byte).
mbed_official 133:d4dda5c437f0 122 You can choose either one block read operation or multiple block read operation
mbed_official 133:d4dda5c437f0 123 by adjusting the "NumberOfBlocks" parameter.
mbed_official 133:d4dda5c437f0 124 After this, you have to call the function HAL_SD_CheckWriteOperation(), to insure
mbed_official 133:d4dda5c437f0 125 that the write transfer is done correctly in both DMA and SD sides.
mbed_official 133:d4dda5c437f0 126
mbed_official 133:d4dda5c437f0 127 *** SD card status ***
mbed_official 133:d4dda5c437f0 128 ======================
mbed_official 133:d4dda5c437f0 129 [..]
mbed_official 133:d4dda5c437f0 130 (+) At any time, you can check the SD Card status and get the SD card state
mbed_official 133:d4dda5c437f0 131 by using the HAL_SD_GetStatus() function. This function checks first if the
mbed_official 133:d4dda5c437f0 132 SD card is still connected and then get the internal SD Card transfer state.
mbed_official 133:d4dda5c437f0 133 (+) You can also get the SD card SD Status register by using the HAL_SD_SendSDStatus()
mbed_official 133:d4dda5c437f0 134 function.
mbed_official 133:d4dda5c437f0 135
mbed_official 133:d4dda5c437f0 136 *** SD HAL driver macros list ***
mbed_official 133:d4dda5c437f0 137 ==================================
mbed_official 133:d4dda5c437f0 138 [..]
mbed_official 133:d4dda5c437f0 139 Below the list of most used macros in SD HAL driver.
mbed_official 133:d4dda5c437f0 140
mbed_official 133:d4dda5c437f0 141 (+) __HAL_SD_SDIO_ENABLE : Enable the SD device
mbed_official 133:d4dda5c437f0 142 (+) __HAL_SD_SDIO_DISABLE : Disable the SD device
mbed_official 133:d4dda5c437f0 143 (+) __HAL_SD_SDIO_DMA_ENABLE: Enable the SDIO DMA transfer
mbed_official 133:d4dda5c437f0 144 (+) __HAL_SD_SDIO_DMA_DISABLE: Disable the SDIO DMA transfer
mbed_official 133:d4dda5c437f0 145 (+) __HAL_SD_SDIO_ENABLE_IT: Enable the SD device interrupt
mbed_official 133:d4dda5c437f0 146 (+) __HAL_SD_SDIO_DISABLE_IT: Disable the SD device interrupt
mbed_official 133:d4dda5c437f0 147 (+) __HAL_SD_SDIO_GET_FLAG:Check whether the specified SD flag is set or not
mbed_official 133:d4dda5c437f0 148 (+) __HAL_SD_SDIO_CLEAR_FLAG: Clear the SD's pending flags
mbed_official 133:d4dda5c437f0 149
mbed_official 133:d4dda5c437f0 150 (@) You can refer to the SD HAL driver header file for more useful macros
mbed_official 133:d4dda5c437f0 151
mbed_official 133:d4dda5c437f0 152 @endverbatim
mbed_official 133:d4dda5c437f0 153 ******************************************************************************
mbed_official 133:d4dda5c437f0 154 * @attention
mbed_official 133:d4dda5c437f0 155 *
mbed_official 133:d4dda5c437f0 156 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 133:d4dda5c437f0 157 *
mbed_official 133:d4dda5c437f0 158 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 133:d4dda5c437f0 159 * are permitted provided that the following conditions are met:
mbed_official 133:d4dda5c437f0 160 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 133:d4dda5c437f0 161 * this list of conditions and the following disclaimer.
mbed_official 133:d4dda5c437f0 162 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 133:d4dda5c437f0 163 * this list of conditions and the following disclaimer in the documentation
mbed_official 133:d4dda5c437f0 164 * and/or other materials provided with the distribution.
mbed_official 133:d4dda5c437f0 165 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 133:d4dda5c437f0 166 * may be used to endorse or promote products derived from this software
mbed_official 133:d4dda5c437f0 167 * without specific prior written permission.
mbed_official 133:d4dda5c437f0 168 *
mbed_official 133:d4dda5c437f0 169 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 133:d4dda5c437f0 170 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 133:d4dda5c437f0 171 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 133:d4dda5c437f0 172 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 133:d4dda5c437f0 173 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 133:d4dda5c437f0 174 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 133:d4dda5c437f0 175 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 133:d4dda5c437f0 176 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 133:d4dda5c437f0 177 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 133:d4dda5c437f0 178 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 133:d4dda5c437f0 179 *
mbed_official 133:d4dda5c437f0 180 ******************************************************************************
mbed_official 133:d4dda5c437f0 181 */
mbed_official 133:d4dda5c437f0 182
mbed_official 133:d4dda5c437f0 183 /* Includes ------------------------------------------------------------------*/
mbed_official 133:d4dda5c437f0 184 #include "stm32f4xx_hal.h"
mbed_official 133:d4dda5c437f0 185
mbed_official 133:d4dda5c437f0 186 /** @addtogroup STM32F4xx_HAL_Driver
mbed_official 133:d4dda5c437f0 187 * @{
mbed_official 133:d4dda5c437f0 188 */
mbed_official 133:d4dda5c437f0 189
mbed_official 133:d4dda5c437f0 190 /** @defgroup SD
mbed_official 133:d4dda5c437f0 191 * @brief SD HAL module driver
mbed_official 133:d4dda5c437f0 192 * @{
mbed_official 133:d4dda5c437f0 193 */
mbed_official 133:d4dda5c437f0 194
mbed_official 133:d4dda5c437f0 195 #ifdef HAL_SD_MODULE_ENABLED
mbed_official 133:d4dda5c437f0 196
mbed_official 133:d4dda5c437f0 197 /* Private typedef -----------------------------------------------------------*/
mbed_official 133:d4dda5c437f0 198 /* Private define ------------------------------------------------------------*/
mbed_official 133:d4dda5c437f0 199
mbed_official 133:d4dda5c437f0 200 /**
mbed_official 133:d4dda5c437f0 201 * @brief SDIO Static flags, TimeOut, FIFO Address
mbed_official 133:d4dda5c437f0 202 */
mbed_official 133:d4dda5c437f0 203 #define SDIO_STATIC_FLAGS ((uint32_t)0x000005FF)
mbed_official 133:d4dda5c437f0 204 #define SDIO_CMD0TIMEOUT ((uint32_t)0x00010000)
mbed_official 133:d4dda5c437f0 205
mbed_official 133:d4dda5c437f0 206 /**
mbed_official 133:d4dda5c437f0 207 * @brief Mask for errors Card Status R1 (OCR Register)
mbed_official 133:d4dda5c437f0 208 */
mbed_official 133:d4dda5c437f0 209 #define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000)
mbed_official 133:d4dda5c437f0 210 #define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000)
mbed_official 133:d4dda5c437f0 211 #define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000)
mbed_official 133:d4dda5c437f0 212 #define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000)
mbed_official 133:d4dda5c437f0 213 #define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000)
mbed_official 133:d4dda5c437f0 214 #define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000)
mbed_official 133:d4dda5c437f0 215 #define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000)
mbed_official 133:d4dda5c437f0 216 #define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000)
mbed_official 133:d4dda5c437f0 217 #define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000)
mbed_official 133:d4dda5c437f0 218 #define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000)
mbed_official 133:d4dda5c437f0 219 #define SD_OCR_CC_ERROR ((uint32_t)0x00100000)
mbed_official 133:d4dda5c437f0 220 #define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000)
mbed_official 133:d4dda5c437f0 221 #define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000)
mbed_official 133:d4dda5c437f0 222 #define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000)
mbed_official 133:d4dda5c437f0 223 #define SD_OCR_CID_CSD_OVERWRIETE ((uint32_t)0x00010000)
mbed_official 133:d4dda5c437f0 224 #define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000)
mbed_official 133:d4dda5c437f0 225 #define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000)
mbed_official 133:d4dda5c437f0 226 #define SD_OCR_ERASE_RESET ((uint32_t)0x00002000)
mbed_official 133:d4dda5c437f0 227 #define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008)
mbed_official 133:d4dda5c437f0 228 #define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008)
mbed_official 133:d4dda5c437f0 229
mbed_official 133:d4dda5c437f0 230 /**
mbed_official 133:d4dda5c437f0 231 * @brief Masks for R6 Response
mbed_official 133:d4dda5c437f0 232 */
mbed_official 133:d4dda5c437f0 233 #define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000)
mbed_official 133:d4dda5c437f0 234 #define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000)
mbed_official 133:d4dda5c437f0 235 #define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000)
mbed_official 133:d4dda5c437f0 236
mbed_official 133:d4dda5c437f0 237 #define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000)
mbed_official 133:d4dda5c437f0 238 #define SD_HIGH_CAPACITY ((uint32_t)0x40000000)
mbed_official 133:d4dda5c437f0 239 #define SD_STD_CAPACITY ((uint32_t)0x00000000)
mbed_official 133:d4dda5c437f0 240 #define SD_CHECK_PATTERN ((uint32_t)0x000001AA)
mbed_official 133:d4dda5c437f0 241
mbed_official 133:d4dda5c437f0 242 #define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFF)
mbed_official 133:d4dda5c437f0 243 #define SD_ALLZERO ((uint32_t)0x00000000)
mbed_official 133:d4dda5c437f0 244
mbed_official 133:d4dda5c437f0 245 #define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000)
mbed_official 133:d4dda5c437f0 246 #define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000)
mbed_official 133:d4dda5c437f0 247 #define SD_CARD_LOCKED ((uint32_t)0x02000000)
mbed_official 133:d4dda5c437f0 248
mbed_official 133:d4dda5c437f0 249 #define SD_DATATIMEOUT ((uint32_t)0xFFFFFFFF)
mbed_official 133:d4dda5c437f0 250 #define SD_0TO7BITS ((uint32_t)0x000000FF)
mbed_official 133:d4dda5c437f0 251 #define SD_8TO15BITS ((uint32_t)0x0000FF00)
mbed_official 133:d4dda5c437f0 252 #define SD_16TO23BITS ((uint32_t)0x00FF0000)
mbed_official 133:d4dda5c437f0 253 #define SD_24TO31BITS ((uint32_t)0xFF000000)
mbed_official 133:d4dda5c437f0 254 #define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFF)
mbed_official 133:d4dda5c437f0 255
mbed_official 133:d4dda5c437f0 256 #define SD_HALFFIFO ((uint32_t)0x00000008)
mbed_official 133:d4dda5c437f0 257 #define SD_HALFFIFOBYTES ((uint32_t)0x00000020)
mbed_official 133:d4dda5c437f0 258
mbed_official 133:d4dda5c437f0 259 /**
mbed_official 133:d4dda5c437f0 260 * @brief Command Class Supported
mbed_official 133:d4dda5c437f0 261 */
mbed_official 133:d4dda5c437f0 262 #define SD_CCCC_LOCK_UNLOCK ((uint32_t)0x00000080)
mbed_official 133:d4dda5c437f0 263 #define SD_CCCC_WRITE_PROT ((uint32_t)0x00000040)
mbed_official 133:d4dda5c437f0 264 #define SD_CCCC_ERASE ((uint32_t)0x00000020)
mbed_official 133:d4dda5c437f0 265
mbed_official 133:d4dda5c437f0 266 /**
mbed_official 133:d4dda5c437f0 267 * @brief Following commands are SD Card Specific commands.
mbed_official 133:d4dda5c437f0 268 * SDIO_APP_CMD should be sent before sending these commands.
mbed_official 133:d4dda5c437f0 269 */
mbed_official 133:d4dda5c437f0 270 #define SD_SDIO_SEND_IF_COND ((uint32_t)SD_CMD_HS_SEND_EXT_CSD)
mbed_official 133:d4dda5c437f0 271
mbed_official 133:d4dda5c437f0 272
mbed_official 133:d4dda5c437f0 273 /* Private macro -------------------------------------------------------------*/
mbed_official 133:d4dda5c437f0 274 /* Private variables ---------------------------------------------------------*/
mbed_official 133:d4dda5c437f0 275 /* Private function prototypes -----------------------------------------------*/
mbed_official 133:d4dda5c437f0 276 /* Private functions ---------------------------------------------------------*/
mbed_official 133:d4dda5c437f0 277 static HAL_SD_ErrorTypedef SD_Initialize_Cards(SD_HandleTypeDef *hsd);
mbed_official 133:d4dda5c437f0 278 static HAL_SD_ErrorTypedef SD_Select_Deselect(SD_HandleTypeDef *hsd, uint64_t addr);
mbed_official 133:d4dda5c437f0 279 static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd);
mbed_official 133:d4dda5c437f0 280 static HAL_SD_ErrorTypedef SD_PowerOFF(SD_HandleTypeDef *hsd);
mbed_official 133:d4dda5c437f0 281 static HAL_SD_ErrorTypedef SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
mbed_official 133:d4dda5c437f0 282 static HAL_SD_CardStateTypedef SD_GetState(SD_HandleTypeDef *hsd);
mbed_official 133:d4dda5c437f0 283 static HAL_SD_ErrorTypedef SD_IsCardProgramming(SD_HandleTypeDef *hsd, uint8_t *pStatus);
mbed_official 133:d4dda5c437f0 284 static HAL_SD_ErrorTypedef SD_CmdError(SD_HandleTypeDef *hsd);
mbed_official 133:d4dda5c437f0 285 static HAL_SD_ErrorTypedef SD_CmdResp1Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD);
mbed_official 133:d4dda5c437f0 286 static HAL_SD_ErrorTypedef SD_CmdResp7Error(SD_HandleTypeDef *hsd);
mbed_official 133:d4dda5c437f0 287 static HAL_SD_ErrorTypedef SD_CmdResp3Error(SD_HandleTypeDef *hsd);
mbed_official 133:d4dda5c437f0 288 static HAL_SD_ErrorTypedef SD_CmdResp2Error(SD_HandleTypeDef *hsd);
mbed_official 133:d4dda5c437f0 289 static HAL_SD_ErrorTypedef SD_CmdResp6Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA);
mbed_official 133:d4dda5c437f0 290 static HAL_SD_ErrorTypedef SD_WideBus_Enable(SD_HandleTypeDef *hsd);
mbed_official 133:d4dda5c437f0 291 static HAL_SD_ErrorTypedef SD_WideBus_Disable(SD_HandleTypeDef *hsd);
mbed_official 133:d4dda5c437f0 292 static HAL_SD_ErrorTypedef SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR);
mbed_official 133:d4dda5c437f0 293 static void SD_DMA_RxCplt(DMA_HandleTypeDef *hdma);
mbed_official 133:d4dda5c437f0 294 static void SD_DMA_RxError(DMA_HandleTypeDef *hdma);
mbed_official 133:d4dda5c437f0 295 static void SD_DMA_TxCplt(DMA_HandleTypeDef *hdma);
mbed_official 133:d4dda5c437f0 296 static void SD_DMA_TxError(DMA_HandleTypeDef *hdma);
mbed_official 133:d4dda5c437f0 297
mbed_official 133:d4dda5c437f0 298 /** @defgroup SD_Private_Functions
mbed_official 133:d4dda5c437f0 299 * @{
mbed_official 133:d4dda5c437f0 300 */
mbed_official 133:d4dda5c437f0 301
mbed_official 133:d4dda5c437f0 302 /** @defgroup SD_Group1 Initialization and de-initialization functions
mbed_official 133:d4dda5c437f0 303 * @brief Initialization and Configuration functions
mbed_official 133:d4dda5c437f0 304 *
mbed_official 133:d4dda5c437f0 305 @verbatim
mbed_official 133:d4dda5c437f0 306 ==============================================================================
mbed_official 133:d4dda5c437f0 307 ##### Initialization and de-initialization functions #####
mbed_official 133:d4dda5c437f0 308 ==============================================================================
mbed_official 133:d4dda5c437f0 309 [..]
mbed_official 133:d4dda5c437f0 310 This section provides functions allowing to initialize/de-initialize the SD
mbed_official 133:d4dda5c437f0 311 card device to be ready for use.
mbed_official 133:d4dda5c437f0 312
mbed_official 133:d4dda5c437f0 313
mbed_official 133:d4dda5c437f0 314 @endverbatim
mbed_official 133:d4dda5c437f0 315 * @{
mbed_official 133:d4dda5c437f0 316 */
mbed_official 133:d4dda5c437f0 317
mbed_official 133:d4dda5c437f0 318 /**
mbed_official 133:d4dda5c437f0 319 * @brief Initializes the SD card according to the specified parameters in the
mbed_official 133:d4dda5c437f0 320 SD_HandleTypeDef and create the associated handle.
mbed_official 133:d4dda5c437f0 321 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 322 * @param SDCardInfo: HAL_SD_CardInfoTypedef structure for SD card information
mbed_official 133:d4dda5c437f0 323 * @retval HAL SD error state
mbed_official 133:d4dda5c437f0 324 */
mbed_official 133:d4dda5c437f0 325 HAL_SD_ErrorTypedef HAL_SD_Init(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *SDCardInfo)
mbed_official 133:d4dda5c437f0 326 {
mbed_official 133:d4dda5c437f0 327 __IO HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 328 SD_InitTypeDef tmpInit;
mbed_official 133:d4dda5c437f0 329
mbed_official 133:d4dda5c437f0 330 /* Initialize the low level hardware (MSP) */
mbed_official 133:d4dda5c437f0 331 HAL_SD_MspInit(hsd);
mbed_official 133:d4dda5c437f0 332
mbed_official 133:d4dda5c437f0 333 /* Default SDIO peripheral configuration for SD card initialization */
mbed_official 133:d4dda5c437f0 334 tmpInit.ClockEdge = SDIO_CLOCK_EDGE_RISING;
mbed_official 133:d4dda5c437f0 335 tmpInit.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
mbed_official 133:d4dda5c437f0 336 tmpInit.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
mbed_official 133:d4dda5c437f0 337 tmpInit.BusWide = SDIO_BUS_WIDE_1B;
mbed_official 133:d4dda5c437f0 338 tmpInit.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
mbed_official 133:d4dda5c437f0 339 tmpInit.ClockDiv = SDIO_INIT_CLK_DIV;
mbed_official 133:d4dda5c437f0 340
mbed_official 133:d4dda5c437f0 341 /* Initialize SDIO peripheral interface with default configuration */
mbed_official 133:d4dda5c437f0 342 SDIO_Init(hsd->Instance, tmpInit);
mbed_official 133:d4dda5c437f0 343
mbed_official 133:d4dda5c437f0 344 /* Identify card operating voltage */
mbed_official 133:d4dda5c437f0 345 errorState = SD_PowerON(hsd);
mbed_official 133:d4dda5c437f0 346
mbed_official 133:d4dda5c437f0 347 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 348 {
mbed_official 133:d4dda5c437f0 349 return errorState;
mbed_official 133:d4dda5c437f0 350 }
mbed_official 133:d4dda5c437f0 351
mbed_official 133:d4dda5c437f0 352 /* Initialize the present SDIO card(s) and put them in idle state */
mbed_official 133:d4dda5c437f0 353 errorState = SD_Initialize_Cards(hsd);
mbed_official 133:d4dda5c437f0 354
mbed_official 133:d4dda5c437f0 355 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 356 {
mbed_official 133:d4dda5c437f0 357 return errorState;
mbed_official 133:d4dda5c437f0 358 }
mbed_official 133:d4dda5c437f0 359
mbed_official 133:d4dda5c437f0 360 /* Read CSD/CID MSD registers */
mbed_official 133:d4dda5c437f0 361 errorState = HAL_SD_Get_CardInfo(hsd, SDCardInfo);
mbed_official 133:d4dda5c437f0 362
mbed_official 133:d4dda5c437f0 363 if (errorState == SD_OK)
mbed_official 133:d4dda5c437f0 364 {
mbed_official 133:d4dda5c437f0 365 /* Select the Card */
mbed_official 133:d4dda5c437f0 366 errorState = SD_Select_Deselect(hsd, (uint32_t)(((uint32_t)SDCardInfo->RCA) << 16));
mbed_official 133:d4dda5c437f0 367 }
mbed_official 133:d4dda5c437f0 368
mbed_official 133:d4dda5c437f0 369 /* Configure SDIO peripheral interface */
mbed_official 133:d4dda5c437f0 370 SDIO_Init(hsd->Instance, hsd->Init);
mbed_official 133:d4dda5c437f0 371
mbed_official 133:d4dda5c437f0 372 return errorState;
mbed_official 133:d4dda5c437f0 373 }
mbed_official 133:d4dda5c437f0 374
mbed_official 133:d4dda5c437f0 375 /**
mbed_official 133:d4dda5c437f0 376 * @brief De-Initializes the SD card.
mbed_official 133:d4dda5c437f0 377 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 378 * @retval HAL status
mbed_official 133:d4dda5c437f0 379 */
mbed_official 133:d4dda5c437f0 380 HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 381 {
mbed_official 133:d4dda5c437f0 382
mbed_official 133:d4dda5c437f0 383 /* Set SD power state to off */
mbed_official 133:d4dda5c437f0 384 SD_PowerOFF(hsd);
mbed_official 133:d4dda5c437f0 385
mbed_official 133:d4dda5c437f0 386 /* De-Initialize the MSP layer */
mbed_official 133:d4dda5c437f0 387 HAL_SD_MspDeInit(hsd);
mbed_official 133:d4dda5c437f0 388
mbed_official 133:d4dda5c437f0 389 return HAL_OK;
mbed_official 133:d4dda5c437f0 390 }
mbed_official 133:d4dda5c437f0 391
mbed_official 133:d4dda5c437f0 392
mbed_official 133:d4dda5c437f0 393 /**
mbed_official 133:d4dda5c437f0 394 * @brief Initializes the SD MSP.
mbed_official 133:d4dda5c437f0 395 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 396 * @retval None
mbed_official 133:d4dda5c437f0 397 */
mbed_official 133:d4dda5c437f0 398 __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 399 {
mbed_official 133:d4dda5c437f0 400 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 133:d4dda5c437f0 401 the HAL_SD_MspInit could be implemented in the user file
mbed_official 133:d4dda5c437f0 402 */
mbed_official 133:d4dda5c437f0 403 }
mbed_official 133:d4dda5c437f0 404
mbed_official 133:d4dda5c437f0 405 /**
mbed_official 133:d4dda5c437f0 406 * @brief De-Initialize SD MSP.
mbed_official 133:d4dda5c437f0 407 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 408 * @retval None
mbed_official 133:d4dda5c437f0 409 */
mbed_official 133:d4dda5c437f0 410 __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 411 {
mbed_official 133:d4dda5c437f0 412 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 133:d4dda5c437f0 413 the HAL_SD_MspDeInit could be implemented in the user file
mbed_official 133:d4dda5c437f0 414 */
mbed_official 133:d4dda5c437f0 415 }
mbed_official 133:d4dda5c437f0 416
mbed_official 133:d4dda5c437f0 417 /**
mbed_official 133:d4dda5c437f0 418 * @}
mbed_official 133:d4dda5c437f0 419 */
mbed_official 133:d4dda5c437f0 420
mbed_official 133:d4dda5c437f0 421 /** @defgroup SD_Group2 IO operation functions
mbed_official 133:d4dda5c437f0 422 * @brief Data transfer functions
mbed_official 133:d4dda5c437f0 423 *
mbed_official 133:d4dda5c437f0 424 @verbatim
mbed_official 133:d4dda5c437f0 425 ==============================================================================
mbed_official 133:d4dda5c437f0 426 ##### IO operation functions #####
mbed_official 133:d4dda5c437f0 427 ==============================================================================
mbed_official 133:d4dda5c437f0 428 [..]
mbed_official 133:d4dda5c437f0 429 This subsection provides a set of functions allowing to manage the data
mbed_official 133:d4dda5c437f0 430 transfer from/to SD card.
mbed_official 133:d4dda5c437f0 431
mbed_official 133:d4dda5c437f0 432 @endverbatim
mbed_official 133:d4dda5c437f0 433 * @{
mbed_official 133:d4dda5c437f0 434 */
mbed_official 133:d4dda5c437f0 435
mbed_official 133:d4dda5c437f0 436 /**
mbed_official 133:d4dda5c437f0 437 * @brief Reads block(s) from a specified address in a card. The Data transfer
mbed_official 133:d4dda5c437f0 438 * is managed by polling mode.
mbed_official 133:d4dda5c437f0 439 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 440 * @param pReadBuffer: pointer to the buffer that will contain the received data
mbed_official 133:d4dda5c437f0 441 * @param ReadAddr: Address from where data is to be read
mbed_official 133:d4dda5c437f0 442 * @param BlockSize: SD card Data block size
mbed_official 133:d4dda5c437f0 443 * This parameter should be 512
mbed_official 133:d4dda5c437f0 444 * @param NumberOfBlocks: Number of SD blocks to read
mbed_official 133:d4dda5c437f0 445 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 446 */
mbed_official 133:d4dda5c437f0 447 HAL_SD_ErrorTypedef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
mbed_official 133:d4dda5c437f0 448 {
mbed_official 133:d4dda5c437f0 449 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
mbed_official 133:d4dda5c437f0 450 SDIO_DataInitTypeDef SDIO_DataInitStructure;
mbed_official 133:d4dda5c437f0 451 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 452 uint32_t count = 0, *tempbuff = (uint32_t *)pReadBuffer;
mbed_official 133:d4dda5c437f0 453
mbed_official 133:d4dda5c437f0 454 /* Initialize data control register */
mbed_official 133:d4dda5c437f0 455 hsd->Instance->DCTRL = 0;
mbed_official 133:d4dda5c437f0 456
mbed_official 133:d4dda5c437f0 457 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
mbed_official 133:d4dda5c437f0 458 {
mbed_official 133:d4dda5c437f0 459 BlockSize = 512;
mbed_official 133:d4dda5c437f0 460 ReadAddr /= 512;
mbed_official 133:d4dda5c437f0 461 }
mbed_official 133:d4dda5c437f0 462
mbed_official 133:d4dda5c437f0 463 /* Set Block Size for Card */
mbed_official 133:d4dda5c437f0 464 SDIO_CmdInitStructure.Argument = (uint32_t) BlockSize;
mbed_official 133:d4dda5c437f0 465 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
mbed_official 133:d4dda5c437f0 466 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 467 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 468 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 469 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 470
mbed_official 133:d4dda5c437f0 471 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 472 errorState = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
mbed_official 133:d4dda5c437f0 473
mbed_official 133:d4dda5c437f0 474 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 475 {
mbed_official 133:d4dda5c437f0 476 return errorState;
mbed_official 133:d4dda5c437f0 477 }
mbed_official 133:d4dda5c437f0 478
mbed_official 133:d4dda5c437f0 479 /* Configure the SD DPSM (Data Path State Machine) */
mbed_official 133:d4dda5c437f0 480 SDIO_DataInitStructure.DataTimeOut = SD_DATATIMEOUT;
mbed_official 133:d4dda5c437f0 481 SDIO_DataInitStructure.DataLength = NumberOfBlocks * BlockSize;
mbed_official 133:d4dda5c437f0 482 SDIO_DataInitStructure.DataBlockSize = (uint32_t)(9 << 4);
mbed_official 133:d4dda5c437f0 483 SDIO_DataInitStructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
mbed_official 133:d4dda5c437f0 484 SDIO_DataInitStructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
mbed_official 133:d4dda5c437f0 485 SDIO_DataInitStructure.DPSM = SDIO_DPSM_ENABLE;
mbed_official 133:d4dda5c437f0 486 SDIO_DataConfig(hsd->Instance, &SDIO_DataInitStructure);
mbed_official 133:d4dda5c437f0 487
mbed_official 133:d4dda5c437f0 488 if(NumberOfBlocks > 1)
mbed_official 133:d4dda5c437f0 489 {
mbed_official 133:d4dda5c437f0 490 /* Send CMD18 READ_MULT_BLOCK with argument data address */
mbed_official 133:d4dda5c437f0 491 SDIO_CmdInitStructure.CmdIndex = SD_CMD_READ_MULT_BLOCK;
mbed_official 133:d4dda5c437f0 492 }
mbed_official 133:d4dda5c437f0 493 else
mbed_official 133:d4dda5c437f0 494 {
mbed_official 133:d4dda5c437f0 495 /* Send CMD17 READ_SINGLE_BLOCK */
mbed_official 133:d4dda5c437f0 496 SDIO_CmdInitStructure.CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
mbed_official 133:d4dda5c437f0 497 }
mbed_official 133:d4dda5c437f0 498
mbed_official 133:d4dda5c437f0 499 SDIO_CmdInitStructure.Argument = (uint32_t)ReadAddr;
mbed_official 133:d4dda5c437f0 500 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 501
mbed_official 133:d4dda5c437f0 502 /* Read block(s) in polling mode */
mbed_official 133:d4dda5c437f0 503 if(NumberOfBlocks > 1)
mbed_official 133:d4dda5c437f0 504 {
mbed_official 133:d4dda5c437f0 505 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 506 errorState = SD_CmdResp1Error(hsd, SD_CMD_READ_MULT_BLOCK);
mbed_official 133:d4dda5c437f0 507
mbed_official 133:d4dda5c437f0 508 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 509 {
mbed_official 133:d4dda5c437f0 510 return errorState;
mbed_official 133:d4dda5c437f0 511 }
mbed_official 133:d4dda5c437f0 512
mbed_official 133:d4dda5c437f0 513 /* Poll on SDIO flags */
mbed_official 133:d4dda5c437f0 514 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
mbed_official 133:d4dda5c437f0 515 {
mbed_official 133:d4dda5c437f0 516 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
mbed_official 133:d4dda5c437f0 517 {
mbed_official 133:d4dda5c437f0 518 /* Read data from SDIO Rx FIFO */
mbed_official 133:d4dda5c437f0 519 for (count = 0; count < 8; count++)
mbed_official 133:d4dda5c437f0 520 {
mbed_official 133:d4dda5c437f0 521 *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance);
mbed_official 133:d4dda5c437f0 522 }
mbed_official 133:d4dda5c437f0 523
mbed_official 133:d4dda5c437f0 524 tempbuff += 8;
mbed_official 133:d4dda5c437f0 525 }
mbed_official 133:d4dda5c437f0 526 }
mbed_official 133:d4dda5c437f0 527 }
mbed_official 133:d4dda5c437f0 528 else
mbed_official 133:d4dda5c437f0 529 {
mbed_official 133:d4dda5c437f0 530 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 531 errorState = SD_CmdResp1Error(hsd, SD_CMD_READ_SINGLE_BLOCK);
mbed_official 133:d4dda5c437f0 532
mbed_official 133:d4dda5c437f0 533 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 534 {
mbed_official 133:d4dda5c437f0 535 return errorState;
mbed_official 133:d4dda5c437f0 536 }
mbed_official 133:d4dda5c437f0 537
mbed_official 133:d4dda5c437f0 538 /* In case of single block transfer, no need of stop transfer at all */
mbed_official 133:d4dda5c437f0 539 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
mbed_official 133:d4dda5c437f0 540 {
mbed_official 133:d4dda5c437f0 541 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
mbed_official 133:d4dda5c437f0 542 {
mbed_official 133:d4dda5c437f0 543 /* Read data from SDIO Rx FIFO */
mbed_official 133:d4dda5c437f0 544 for (count = 0; count < 8; count++)
mbed_official 133:d4dda5c437f0 545 {
mbed_official 133:d4dda5c437f0 546 *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance);
mbed_official 133:d4dda5c437f0 547 }
mbed_official 133:d4dda5c437f0 548
mbed_official 133:d4dda5c437f0 549 tempbuff += 8;
mbed_official 133:d4dda5c437f0 550 }
mbed_official 133:d4dda5c437f0 551 }
mbed_official 133:d4dda5c437f0 552 }
mbed_official 133:d4dda5c437f0 553
mbed_official 133:d4dda5c437f0 554 /* Send stop transmission command in case of multiblock read */
mbed_official 133:d4dda5c437f0 555 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1))
mbed_official 133:d4dda5c437f0 556 {
mbed_official 133:d4dda5c437f0 557 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) ||\
mbed_official 133:d4dda5c437f0 558 (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
mbed_official 133:d4dda5c437f0 559 (hsd->CardType == HIGH_CAPACITY_SD_CARD))
mbed_official 133:d4dda5c437f0 560 {
mbed_official 133:d4dda5c437f0 561 /* Send stop transmission command */
mbed_official 133:d4dda5c437f0 562 errorState = HAL_SD_StopTransfer(hsd);
mbed_official 133:d4dda5c437f0 563 }
mbed_official 133:d4dda5c437f0 564 }
mbed_official 133:d4dda5c437f0 565
mbed_official 133:d4dda5c437f0 566 /* Get error state */
mbed_official 133:d4dda5c437f0 567 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
mbed_official 133:d4dda5c437f0 568 {
mbed_official 133:d4dda5c437f0 569 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
mbed_official 133:d4dda5c437f0 570
mbed_official 133:d4dda5c437f0 571 errorState = SD_DATA_TIMEOUT;
mbed_official 133:d4dda5c437f0 572
mbed_official 133:d4dda5c437f0 573 return errorState;
mbed_official 133:d4dda5c437f0 574 }
mbed_official 133:d4dda5c437f0 575 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
mbed_official 133:d4dda5c437f0 576 {
mbed_official 133:d4dda5c437f0 577 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
mbed_official 133:d4dda5c437f0 578
mbed_official 133:d4dda5c437f0 579 errorState = SD_DATA_CRC_FAIL;
mbed_official 133:d4dda5c437f0 580
mbed_official 133:d4dda5c437f0 581 return errorState;
mbed_official 133:d4dda5c437f0 582 }
mbed_official 133:d4dda5c437f0 583 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
mbed_official 133:d4dda5c437f0 584 {
mbed_official 133:d4dda5c437f0 585 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
mbed_official 133:d4dda5c437f0 586
mbed_official 133:d4dda5c437f0 587 errorState = SD_RX_OVERRUN;
mbed_official 133:d4dda5c437f0 588
mbed_official 133:d4dda5c437f0 589 return errorState;
mbed_official 133:d4dda5c437f0 590 }
mbed_official 133:d4dda5c437f0 591 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
mbed_official 133:d4dda5c437f0 592 {
mbed_official 133:d4dda5c437f0 593 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
mbed_official 133:d4dda5c437f0 594
mbed_official 133:d4dda5c437f0 595 errorState = SD_START_BIT_ERR;
mbed_official 133:d4dda5c437f0 596
mbed_official 133:d4dda5c437f0 597 return errorState;
mbed_official 133:d4dda5c437f0 598 }
mbed_official 133:d4dda5c437f0 599
mbed_official 133:d4dda5c437f0 600 count = SD_DATATIMEOUT;
mbed_official 133:d4dda5c437f0 601
mbed_official 133:d4dda5c437f0 602 /* Empty FIFO if there is still any data */
mbed_official 133:d4dda5c437f0 603 while ((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (count > 0))
mbed_official 133:d4dda5c437f0 604 {
mbed_official 133:d4dda5c437f0 605 *tempbuff = SDIO_ReadFIFO(hsd->Instance);
mbed_official 133:d4dda5c437f0 606 tempbuff++;
mbed_official 133:d4dda5c437f0 607 count--;
mbed_official 133:d4dda5c437f0 608 }
mbed_official 133:d4dda5c437f0 609
mbed_official 133:d4dda5c437f0 610 /* Clear all the static flags */
mbed_official 133:d4dda5c437f0 611 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
mbed_official 133:d4dda5c437f0 612
mbed_official 133:d4dda5c437f0 613 return errorState;
mbed_official 133:d4dda5c437f0 614 }
mbed_official 133:d4dda5c437f0 615
mbed_official 133:d4dda5c437f0 616 /**
mbed_official 133:d4dda5c437f0 617 * @brief Allows to write block(s) to a specified address in a card. The Data
mbed_official 133:d4dda5c437f0 618 * transfer is managed by polling mode.
mbed_official 133:d4dda5c437f0 619 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 620 * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
mbed_official 133:d4dda5c437f0 621 * @param WriteAddr: Address from where data is to be written
mbed_official 133:d4dda5c437f0 622 * @param BlockSize: SD card Data block size
mbed_official 133:d4dda5c437f0 623 * This parameter should be 512.
mbed_official 133:d4dda5c437f0 624 * @param NumberOfBlocks: Number of SD blocks to write
mbed_official 133:d4dda5c437f0 625 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 626 */
mbed_official 133:d4dda5c437f0 627 HAL_SD_ErrorTypedef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
mbed_official 133:d4dda5c437f0 628 {
mbed_official 133:d4dda5c437f0 629 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
mbed_official 133:d4dda5c437f0 630 SDIO_DataInitTypeDef SDIO_DataInitStructure;
mbed_official 133:d4dda5c437f0 631 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 632 uint32_t TotalNumberOfBytes = 0, bytestransferred = 0, count = 0, restwords = 0;
mbed_official 133:d4dda5c437f0 633 uint32_t *tempbuff = (uint32_t *)pWriteBuffer;
mbed_official 133:d4dda5c437f0 634 uint8_t cardstate = 0;
mbed_official 133:d4dda5c437f0 635
mbed_official 133:d4dda5c437f0 636 /* Initialize data control register */
mbed_official 133:d4dda5c437f0 637 hsd->Instance->DCTRL = 0;
mbed_official 133:d4dda5c437f0 638
mbed_official 133:d4dda5c437f0 639 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
mbed_official 133:d4dda5c437f0 640 {
mbed_official 133:d4dda5c437f0 641 BlockSize = 512;
mbed_official 133:d4dda5c437f0 642 WriteAddr /= 512;
mbed_official 133:d4dda5c437f0 643 }
mbed_official 133:d4dda5c437f0 644
mbed_official 133:d4dda5c437f0 645 /* Set Block Size for Card */
mbed_official 133:d4dda5c437f0 646 SDIO_CmdInitStructure.Argument = (uint32_t)BlockSize;
mbed_official 133:d4dda5c437f0 647 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
mbed_official 133:d4dda5c437f0 648 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 649 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 650 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 651 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 652
mbed_official 133:d4dda5c437f0 653 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 654 errorState = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
mbed_official 133:d4dda5c437f0 655
mbed_official 133:d4dda5c437f0 656 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 657 {
mbed_official 133:d4dda5c437f0 658 return errorState;
mbed_official 133:d4dda5c437f0 659 }
mbed_official 133:d4dda5c437f0 660
mbed_official 133:d4dda5c437f0 661 if(NumberOfBlocks > 1)
mbed_official 133:d4dda5c437f0 662 {
mbed_official 133:d4dda5c437f0 663 /* Send CMD25 WRITE_MULT_BLOCK with argument data address */
mbed_official 133:d4dda5c437f0 664 SDIO_CmdInitStructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
mbed_official 133:d4dda5c437f0 665 }
mbed_official 133:d4dda5c437f0 666 else
mbed_official 133:d4dda5c437f0 667 {
mbed_official 133:d4dda5c437f0 668 /* Send CMD24 WRITE_SINGLE_BLOCK */
mbed_official 133:d4dda5c437f0 669 SDIO_CmdInitStructure.CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
mbed_official 133:d4dda5c437f0 670 }
mbed_official 133:d4dda5c437f0 671
mbed_official 133:d4dda5c437f0 672 SDIO_CmdInitStructure.Argument = (uint32_t)WriteAddr;
mbed_official 133:d4dda5c437f0 673 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 674
mbed_official 133:d4dda5c437f0 675 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 676 if(NumberOfBlocks > 1)
mbed_official 133:d4dda5c437f0 677 {
mbed_official 133:d4dda5c437f0 678 errorState = SD_CmdResp1Error(hsd, SD_CMD_WRITE_MULT_BLOCK);
mbed_official 133:d4dda5c437f0 679 }
mbed_official 133:d4dda5c437f0 680 else
mbed_official 133:d4dda5c437f0 681 {
mbed_official 133:d4dda5c437f0 682 errorState = SD_CmdResp1Error(hsd, SD_CMD_WRITE_SINGLE_BLOCK);
mbed_official 133:d4dda5c437f0 683 }
mbed_official 133:d4dda5c437f0 684
mbed_official 133:d4dda5c437f0 685 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 686 {
mbed_official 133:d4dda5c437f0 687 return errorState;
mbed_official 133:d4dda5c437f0 688 }
mbed_official 133:d4dda5c437f0 689
mbed_official 133:d4dda5c437f0 690 /* Set total number of bytes to write */
mbed_official 133:d4dda5c437f0 691 TotalNumberOfBytes = NumberOfBlocks * BlockSize;
mbed_official 133:d4dda5c437f0 692
mbed_official 133:d4dda5c437f0 693 /* Configure the SD DPSM (Data Path State Machine) */
mbed_official 133:d4dda5c437f0 694 SDIO_DataInitStructure.DataTimeOut = SD_DATATIMEOUT;
mbed_official 133:d4dda5c437f0 695 SDIO_DataInitStructure.DataLength = NumberOfBlocks * BlockSize;
mbed_official 133:d4dda5c437f0 696 SDIO_DataInitStructure.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
mbed_official 133:d4dda5c437f0 697 SDIO_DataInitStructure.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
mbed_official 133:d4dda5c437f0 698 SDIO_DataInitStructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
mbed_official 133:d4dda5c437f0 699 SDIO_DataInitStructure.DPSM = SDIO_DPSM_ENABLE;
mbed_official 133:d4dda5c437f0 700 SDIO_DataConfig(hsd->Instance, &SDIO_DataInitStructure);
mbed_official 133:d4dda5c437f0 701
mbed_official 133:d4dda5c437f0 702 /* Write block(s) in polling mode */
mbed_official 133:d4dda5c437f0 703 if(NumberOfBlocks > 1)
mbed_official 133:d4dda5c437f0 704 {
mbed_official 133:d4dda5c437f0 705 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
mbed_official 133:d4dda5c437f0 706 {
mbed_official 133:d4dda5c437f0 707 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE))
mbed_official 133:d4dda5c437f0 708 {
mbed_official 133:d4dda5c437f0 709 if ((TotalNumberOfBytes - bytestransferred) < 32)
mbed_official 133:d4dda5c437f0 710 {
mbed_official 133:d4dda5c437f0 711 restwords = ((TotalNumberOfBytes - bytestransferred) % 4 == 0) ? ((TotalNumberOfBytes - bytestransferred) / 4) : (( TotalNumberOfBytes - bytestransferred) / 4 + 1);
mbed_official 133:d4dda5c437f0 712
mbed_official 133:d4dda5c437f0 713 /* Write data to SDIO Tx FIFO */
mbed_official 133:d4dda5c437f0 714 for (count = 0; count < restwords; count++)
mbed_official 133:d4dda5c437f0 715 {
mbed_official 133:d4dda5c437f0 716 SDIO_WriteFIFO(hsd->Instance, tempbuff);
mbed_official 133:d4dda5c437f0 717 tempbuff++;
mbed_official 133:d4dda5c437f0 718 bytestransferred += 4;
mbed_official 133:d4dda5c437f0 719 }
mbed_official 133:d4dda5c437f0 720 }
mbed_official 133:d4dda5c437f0 721 else
mbed_official 133:d4dda5c437f0 722 {
mbed_official 133:d4dda5c437f0 723 /* Write data to SDIO Tx FIFO */
mbed_official 133:d4dda5c437f0 724 for (count = 0; count < 8; count++)
mbed_official 133:d4dda5c437f0 725 {
mbed_official 133:d4dda5c437f0 726 SDIO_WriteFIFO(hsd->Instance, (tempbuff + count));
mbed_official 133:d4dda5c437f0 727 }
mbed_official 133:d4dda5c437f0 728
mbed_official 133:d4dda5c437f0 729 tempbuff += 8;
mbed_official 133:d4dda5c437f0 730 bytestransferred += 32;
mbed_official 133:d4dda5c437f0 731 }
mbed_official 133:d4dda5c437f0 732 }
mbed_official 133:d4dda5c437f0 733 }
mbed_official 133:d4dda5c437f0 734 }
mbed_official 133:d4dda5c437f0 735 else
mbed_official 133:d4dda5c437f0 736 {
mbed_official 133:d4dda5c437f0 737 /* In case of single data block transfer no need of stop command at all */
mbed_official 133:d4dda5c437f0 738 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
mbed_official 133:d4dda5c437f0 739 {
mbed_official 133:d4dda5c437f0 740 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE))
mbed_official 133:d4dda5c437f0 741 {
mbed_official 133:d4dda5c437f0 742 if ((TotalNumberOfBytes - bytestransferred) < 32)
mbed_official 133:d4dda5c437f0 743 {
mbed_official 133:d4dda5c437f0 744 restwords = ((TotalNumberOfBytes - bytestransferred) % 4 == 0) ? ((TotalNumberOfBytes - bytestransferred) / 4) : (( TotalNumberOfBytes - bytestransferred) / 4 + 1);
mbed_official 133:d4dda5c437f0 745
mbed_official 133:d4dda5c437f0 746 /* Write data to SDIO Tx FIFO */
mbed_official 133:d4dda5c437f0 747 for (count = 0; count < restwords; count++)
mbed_official 133:d4dda5c437f0 748 {
mbed_official 133:d4dda5c437f0 749 SDIO_WriteFIFO(hsd->Instance, tempbuff);
mbed_official 133:d4dda5c437f0 750 tempbuff++;
mbed_official 133:d4dda5c437f0 751 bytestransferred += 4;
mbed_official 133:d4dda5c437f0 752 }
mbed_official 133:d4dda5c437f0 753 }
mbed_official 133:d4dda5c437f0 754 else
mbed_official 133:d4dda5c437f0 755 {
mbed_official 133:d4dda5c437f0 756 /* Write data to SDIO Tx FIFO */
mbed_official 133:d4dda5c437f0 757 for (count = 0; count < 8; count++)
mbed_official 133:d4dda5c437f0 758 {
mbed_official 133:d4dda5c437f0 759 SDIO_WriteFIFO(hsd->Instance, (tempbuff + count));
mbed_official 133:d4dda5c437f0 760 }
mbed_official 133:d4dda5c437f0 761
mbed_official 133:d4dda5c437f0 762 tempbuff += 8;
mbed_official 133:d4dda5c437f0 763 bytestransferred += 32;
mbed_official 133:d4dda5c437f0 764 }
mbed_official 133:d4dda5c437f0 765 }
mbed_official 133:d4dda5c437f0 766 }
mbed_official 133:d4dda5c437f0 767 }
mbed_official 133:d4dda5c437f0 768
mbed_official 133:d4dda5c437f0 769 /* Send stop transmission command in case of multiblock write */
mbed_official 133:d4dda5c437f0 770 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1))
mbed_official 133:d4dda5c437f0 771 {
mbed_official 133:d4dda5c437f0 772 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
mbed_official 133:d4dda5c437f0 773 (hsd->CardType == HIGH_CAPACITY_SD_CARD))
mbed_official 133:d4dda5c437f0 774 {
mbed_official 133:d4dda5c437f0 775 /* Send stop transmission command */
mbed_official 133:d4dda5c437f0 776 errorState = HAL_SD_StopTransfer(hsd);
mbed_official 133:d4dda5c437f0 777 }
mbed_official 133:d4dda5c437f0 778 }
mbed_official 133:d4dda5c437f0 779
mbed_official 133:d4dda5c437f0 780 /* Get error state */
mbed_official 133:d4dda5c437f0 781 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
mbed_official 133:d4dda5c437f0 782 {
mbed_official 133:d4dda5c437f0 783 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
mbed_official 133:d4dda5c437f0 784
mbed_official 133:d4dda5c437f0 785 errorState = SD_DATA_TIMEOUT;
mbed_official 133:d4dda5c437f0 786
mbed_official 133:d4dda5c437f0 787 return errorState;
mbed_official 133:d4dda5c437f0 788 }
mbed_official 133:d4dda5c437f0 789 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
mbed_official 133:d4dda5c437f0 790 {
mbed_official 133:d4dda5c437f0 791 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
mbed_official 133:d4dda5c437f0 792
mbed_official 133:d4dda5c437f0 793 errorState = SD_DATA_CRC_FAIL;
mbed_official 133:d4dda5c437f0 794
mbed_official 133:d4dda5c437f0 795 return errorState;
mbed_official 133:d4dda5c437f0 796 }
mbed_official 133:d4dda5c437f0 797 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR))
mbed_official 133:d4dda5c437f0 798 {
mbed_official 133:d4dda5c437f0 799 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_TXUNDERR);
mbed_official 133:d4dda5c437f0 800
mbed_official 133:d4dda5c437f0 801 errorState = SD_TX_UNDERRUN;
mbed_official 133:d4dda5c437f0 802
mbed_official 133:d4dda5c437f0 803 return errorState;
mbed_official 133:d4dda5c437f0 804 }
mbed_official 133:d4dda5c437f0 805 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
mbed_official 133:d4dda5c437f0 806 {
mbed_official 133:d4dda5c437f0 807 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
mbed_official 133:d4dda5c437f0 808
mbed_official 133:d4dda5c437f0 809 errorState = SD_START_BIT_ERR;
mbed_official 133:d4dda5c437f0 810
mbed_official 133:d4dda5c437f0 811 return errorState;
mbed_official 133:d4dda5c437f0 812 }
mbed_official 133:d4dda5c437f0 813
mbed_official 133:d4dda5c437f0 814 /* Clear all the static flags */
mbed_official 133:d4dda5c437f0 815 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
mbed_official 133:d4dda5c437f0 816
mbed_official 133:d4dda5c437f0 817 /* Wait till the card is in programming state */
mbed_official 133:d4dda5c437f0 818 errorState = SD_IsCardProgramming(hsd, &cardstate);
mbed_official 133:d4dda5c437f0 819
mbed_official 133:d4dda5c437f0 820 while ((errorState == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING)))
mbed_official 133:d4dda5c437f0 821 {
mbed_official 133:d4dda5c437f0 822 errorState = SD_IsCardProgramming(hsd, &cardstate);
mbed_official 133:d4dda5c437f0 823 }
mbed_official 133:d4dda5c437f0 824
mbed_official 133:d4dda5c437f0 825 return errorState;
mbed_official 133:d4dda5c437f0 826 }
mbed_official 133:d4dda5c437f0 827
mbed_official 133:d4dda5c437f0 828 /**
mbed_official 133:d4dda5c437f0 829 * @brief Reads block(s) from a specified address in a card. The Data transfer
mbed_official 133:d4dda5c437f0 830 * is managed by DMA mode.
mbed_official 133:d4dda5c437f0 831 * @note This API should be followed by the function HAL_SD_CheckReadOperation()
mbed_official 133:d4dda5c437f0 832 * to check the completion of the read process
mbed_official 133:d4dda5c437f0 833 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 834 * @param pReadBuffer: Pointer to the buffer that will contain the received data
mbed_official 133:d4dda5c437f0 835 * @param ReadAddr: Address from where data is to be read
mbed_official 133:d4dda5c437f0 836 * @param BlockSize: SD card Data block size
mbed_official 133:d4dda5c437f0 837 * This paramater should be 512.
mbed_official 133:d4dda5c437f0 838 * @param NumberOfBlocks: Number of blocks to read.
mbed_official 133:d4dda5c437f0 839 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 840 */
mbed_official 133:d4dda5c437f0 841 HAL_SD_ErrorTypedef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
mbed_official 133:d4dda5c437f0 842 {
mbed_official 133:d4dda5c437f0 843 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
mbed_official 133:d4dda5c437f0 844 SDIO_DataInitTypeDef SDIO_DataInitStructure;
mbed_official 133:d4dda5c437f0 845 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 846
mbed_official 133:d4dda5c437f0 847 /* Initialize data control register */
mbed_official 133:d4dda5c437f0 848 hsd->Instance->DCTRL = 0;
mbed_official 133:d4dda5c437f0 849
mbed_official 133:d4dda5c437f0 850 /* Initialize handle flags */
mbed_official 133:d4dda5c437f0 851 hsd->SdTransferCplt = 0;
mbed_official 133:d4dda5c437f0 852 hsd->DmaTransferCplt = 0;
mbed_official 133:d4dda5c437f0 853 hsd->SdTransferErr = SD_OK;
mbed_official 133:d4dda5c437f0 854
mbed_official 133:d4dda5c437f0 855 /* Initialize SD Read operation */
mbed_official 133:d4dda5c437f0 856 if(NumberOfBlocks > 1)
mbed_official 133:d4dda5c437f0 857 {
mbed_official 133:d4dda5c437f0 858 hsd->SdOperation = SD_READ_MULTIPLE_BLOCK;
mbed_official 133:d4dda5c437f0 859 }
mbed_official 133:d4dda5c437f0 860 else
mbed_official 133:d4dda5c437f0 861 {
mbed_official 133:d4dda5c437f0 862 hsd->SdOperation = SD_READ_SINGLE_BLOCK;
mbed_official 133:d4dda5c437f0 863 }
mbed_official 133:d4dda5c437f0 864
mbed_official 133:d4dda5c437f0 865 /* Enable transfer interrupts */
mbed_official 133:d4dda5c437f0 866 __HAL_SD_SDIO_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL |\
mbed_official 133:d4dda5c437f0 867 SDIO_IT_DTIMEOUT |\
mbed_official 133:d4dda5c437f0 868 SDIO_IT_DATAEND |\
mbed_official 133:d4dda5c437f0 869 SDIO_IT_RXOVERR |\
mbed_official 133:d4dda5c437f0 870 SDIO_IT_STBITERR));
mbed_official 133:d4dda5c437f0 871
mbed_official 133:d4dda5c437f0 872 /* Enable SDIO DMA transfer */
mbed_official 133:d4dda5c437f0 873 __HAL_SD_SDIO_DMA_ENABLE();
mbed_official 133:d4dda5c437f0 874
mbed_official 133:d4dda5c437f0 875 /* Configure DMA user callbacks */
mbed_official 133:d4dda5c437f0 876 hsd->hdmarx->XferCpltCallback = SD_DMA_RxCplt;
mbed_official 133:d4dda5c437f0 877 hsd->hdmarx->XferErrorCallback = SD_DMA_RxError;
mbed_official 133:d4dda5c437f0 878
mbed_official 133:d4dda5c437f0 879 /* Enable the DMA Stream */
mbed_official 133:d4dda5c437f0 880 HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)SDIO_FIFO_ADDRESS, (uint32_t)pReadBuffer, (uint32_t)(BlockSize * NumberOfBlocks));
mbed_official 133:d4dda5c437f0 881
mbed_official 133:d4dda5c437f0 882 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
mbed_official 133:d4dda5c437f0 883 {
mbed_official 133:d4dda5c437f0 884 BlockSize = 512;
mbed_official 133:d4dda5c437f0 885 ReadAddr /= 512;
mbed_official 133:d4dda5c437f0 886 }
mbed_official 133:d4dda5c437f0 887
mbed_official 133:d4dda5c437f0 888 /* Set Block Size for Card */
mbed_official 133:d4dda5c437f0 889 SDIO_CmdInitStructure.Argument = (uint32_t)BlockSize;
mbed_official 133:d4dda5c437f0 890 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
mbed_official 133:d4dda5c437f0 891 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 892 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 893 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 894 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 895
mbed_official 133:d4dda5c437f0 896 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 897 errorState = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
mbed_official 133:d4dda5c437f0 898
mbed_official 133:d4dda5c437f0 899 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 900 {
mbed_official 133:d4dda5c437f0 901 return errorState;
mbed_official 133:d4dda5c437f0 902 }
mbed_official 133:d4dda5c437f0 903
mbed_official 133:d4dda5c437f0 904 /* Configure the SD DPSM (Data Path State Machine) */
mbed_official 133:d4dda5c437f0 905 SDIO_DataInitStructure.DataTimeOut = SD_DATATIMEOUT;
mbed_official 133:d4dda5c437f0 906 SDIO_DataInitStructure.DataLength = BlockSize * NumberOfBlocks;
mbed_official 133:d4dda5c437f0 907 SDIO_DataInitStructure.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
mbed_official 133:d4dda5c437f0 908 SDIO_DataInitStructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
mbed_official 133:d4dda5c437f0 909 SDIO_DataInitStructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
mbed_official 133:d4dda5c437f0 910 SDIO_DataInitStructure.DPSM = SDIO_DPSM_ENABLE;
mbed_official 133:d4dda5c437f0 911 SDIO_DataConfig(hsd->Instance, &SDIO_DataInitStructure);
mbed_official 133:d4dda5c437f0 912
mbed_official 133:d4dda5c437f0 913 /* Check number of blocks command */
mbed_official 133:d4dda5c437f0 914 if(NumberOfBlocks > 1)
mbed_official 133:d4dda5c437f0 915 {
mbed_official 133:d4dda5c437f0 916 /* Send CMD18 READ_MULT_BLOCK with argument data address */
mbed_official 133:d4dda5c437f0 917 SDIO_CmdInitStructure.CmdIndex = SD_CMD_READ_MULT_BLOCK;
mbed_official 133:d4dda5c437f0 918 }
mbed_official 133:d4dda5c437f0 919 else
mbed_official 133:d4dda5c437f0 920 {
mbed_official 133:d4dda5c437f0 921 /* Send CMD17 READ_SINGLE_BLOCK */
mbed_official 133:d4dda5c437f0 922 SDIO_CmdInitStructure.CmdIndex = SD_CMD_READ_SINGLE_BLOCK;
mbed_official 133:d4dda5c437f0 923 }
mbed_official 133:d4dda5c437f0 924
mbed_official 133:d4dda5c437f0 925 SDIO_CmdInitStructure.Argument = (uint32_t)ReadAddr;
mbed_official 133:d4dda5c437f0 926 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 927
mbed_official 133:d4dda5c437f0 928 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 929 if(NumberOfBlocks > 1)
mbed_official 133:d4dda5c437f0 930 {
mbed_official 133:d4dda5c437f0 931 errorState = SD_CmdResp1Error(hsd, SD_CMD_READ_MULT_BLOCK);
mbed_official 133:d4dda5c437f0 932 }
mbed_official 133:d4dda5c437f0 933 else
mbed_official 133:d4dda5c437f0 934 {
mbed_official 133:d4dda5c437f0 935 errorState = SD_CmdResp1Error(hsd, SD_CMD_READ_SINGLE_BLOCK);
mbed_official 133:d4dda5c437f0 936 }
mbed_official 133:d4dda5c437f0 937
mbed_official 133:d4dda5c437f0 938 /* Update the SD transfer error in SD handle */
mbed_official 133:d4dda5c437f0 939 hsd->SdTransferErr = errorState;
mbed_official 133:d4dda5c437f0 940
mbed_official 133:d4dda5c437f0 941 return errorState;
mbed_official 133:d4dda5c437f0 942 }
mbed_official 133:d4dda5c437f0 943
mbed_official 133:d4dda5c437f0 944
mbed_official 133:d4dda5c437f0 945 /**
mbed_official 133:d4dda5c437f0 946 * @brief Writes block(s) to a specified address in a card. The Data transfer
mbed_official 133:d4dda5c437f0 947 * is managed by DMA mode.
mbed_official 133:d4dda5c437f0 948 * @note This API should be followed by the function HAL_SD_CheckWriteOperation()
mbed_official 133:d4dda5c437f0 949 * to check the completion of the write process (by SD current status polling).
mbed_official 133:d4dda5c437f0 950 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 951 * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
mbed_official 133:d4dda5c437f0 952 * @param WriteAddr: Address from where data is to be read
mbed_official 133:d4dda5c437f0 953 * @param BlockSize: the SD card Data block size
mbed_official 133:d4dda5c437f0 954 * This parameter should be 512.
mbed_official 133:d4dda5c437f0 955 * @param NumberOfBlocks: Number of blocks to write
mbed_official 133:d4dda5c437f0 956 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 957 */
mbed_official 133:d4dda5c437f0 958 HAL_SD_ErrorTypedef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
mbed_official 133:d4dda5c437f0 959 {
mbed_official 133:d4dda5c437f0 960 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
mbed_official 133:d4dda5c437f0 961 SDIO_DataInitTypeDef SDIO_DataInitStructure;
mbed_official 133:d4dda5c437f0 962 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 963
mbed_official 133:d4dda5c437f0 964 /* Initialize data control register */
mbed_official 133:d4dda5c437f0 965 hsd->Instance->DCTRL = 0;
mbed_official 133:d4dda5c437f0 966
mbed_official 133:d4dda5c437f0 967 /* Initialize handle flags */
mbed_official 133:d4dda5c437f0 968 hsd->SdTransferCplt = 0;
mbed_official 133:d4dda5c437f0 969 hsd->DmaTransferCplt = 0;
mbed_official 133:d4dda5c437f0 970 hsd->SdTransferErr = SD_OK;
mbed_official 133:d4dda5c437f0 971
mbed_official 133:d4dda5c437f0 972 /* Initialize SD Write operation */
mbed_official 133:d4dda5c437f0 973 if(NumberOfBlocks > 1)
mbed_official 133:d4dda5c437f0 974 {
mbed_official 133:d4dda5c437f0 975 hsd->SdOperation = SD_WRITE_MULTIPLE_BLOCK;
mbed_official 133:d4dda5c437f0 976 }
mbed_official 133:d4dda5c437f0 977 else
mbed_official 133:d4dda5c437f0 978 {
mbed_official 133:d4dda5c437f0 979 hsd->SdOperation = SD_WRITE_SINGLE_BLOCK;
mbed_official 133:d4dda5c437f0 980 }
mbed_official 133:d4dda5c437f0 981
mbed_official 133:d4dda5c437f0 982 /* Enable transfer interrupts */
mbed_official 133:d4dda5c437f0 983 __HAL_SD_SDIO_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL |\
mbed_official 133:d4dda5c437f0 984 SDIO_IT_DTIMEOUT |\
mbed_official 133:d4dda5c437f0 985 SDIO_IT_DATAEND |\
mbed_official 133:d4dda5c437f0 986 SDIO_IT_TXUNDERR |\
mbed_official 133:d4dda5c437f0 987 SDIO_IT_STBITERR));
mbed_official 133:d4dda5c437f0 988
mbed_official 133:d4dda5c437f0 989 /* Configure DMA user callbacks */
mbed_official 133:d4dda5c437f0 990 hsd->hdmatx->XferCpltCallback = SD_DMA_TxCplt;
mbed_official 133:d4dda5c437f0 991 hsd->hdmatx->XferErrorCallback = SD_DMA_TxError;
mbed_official 133:d4dda5c437f0 992
mbed_official 133:d4dda5c437f0 993 /* Enable the DMA Stream */
mbed_official 133:d4dda5c437f0 994 HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pWriteBuffer, (uint32_t)SDIO_FIFO_ADDRESS, (uint32_t)(BlockSize * NumberOfBlocks));
mbed_official 133:d4dda5c437f0 995
mbed_official 133:d4dda5c437f0 996 /* Enable SDIO DMA transfer */
mbed_official 133:d4dda5c437f0 997 __HAL_SD_SDIO_DMA_ENABLE();
mbed_official 133:d4dda5c437f0 998
mbed_official 133:d4dda5c437f0 999 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
mbed_official 133:d4dda5c437f0 1000 {
mbed_official 133:d4dda5c437f0 1001 BlockSize = 512;
mbed_official 133:d4dda5c437f0 1002 WriteAddr /= 512;
mbed_official 133:d4dda5c437f0 1003 }
mbed_official 133:d4dda5c437f0 1004
mbed_official 133:d4dda5c437f0 1005 /* Set Block Size for Card */
mbed_official 133:d4dda5c437f0 1006 SDIO_CmdInitStructure.Argument = (uint32_t)BlockSize;
mbed_official 133:d4dda5c437f0 1007 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
mbed_official 133:d4dda5c437f0 1008 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 1009 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 1010 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 1011 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 1012
mbed_official 133:d4dda5c437f0 1013 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 1014 errorState = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
mbed_official 133:d4dda5c437f0 1015
mbed_official 133:d4dda5c437f0 1016 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 1017 {
mbed_official 133:d4dda5c437f0 1018 return errorState;
mbed_official 133:d4dda5c437f0 1019 }
mbed_official 133:d4dda5c437f0 1020
mbed_official 133:d4dda5c437f0 1021 /* Check number of blocks command */
mbed_official 133:d4dda5c437f0 1022 if(NumberOfBlocks <= 1)
mbed_official 133:d4dda5c437f0 1023 {
mbed_official 133:d4dda5c437f0 1024 /* Send CMD24 WRITE_SINGLE_BLOCK */
mbed_official 133:d4dda5c437f0 1025 SDIO_CmdInitStructure.CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK;
mbed_official 133:d4dda5c437f0 1026 }
mbed_official 133:d4dda5c437f0 1027 else
mbed_official 133:d4dda5c437f0 1028 {
mbed_official 133:d4dda5c437f0 1029 /* Send CMD25 WRITE_MULT_BLOCK with argument data address */
mbed_official 133:d4dda5c437f0 1030 SDIO_CmdInitStructure.CmdIndex = SD_CMD_WRITE_MULT_BLOCK;
mbed_official 133:d4dda5c437f0 1031 }
mbed_official 133:d4dda5c437f0 1032
mbed_official 133:d4dda5c437f0 1033 SDIO_CmdInitStructure.Argument = (uint32_t)WriteAddr;
mbed_official 133:d4dda5c437f0 1034 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 1035
mbed_official 133:d4dda5c437f0 1036 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 1037 if(NumberOfBlocks > 1)
mbed_official 133:d4dda5c437f0 1038 {
mbed_official 133:d4dda5c437f0 1039 errorState = SD_CmdResp1Error(hsd, SD_CMD_WRITE_MULT_BLOCK);
mbed_official 133:d4dda5c437f0 1040 }
mbed_official 133:d4dda5c437f0 1041 else
mbed_official 133:d4dda5c437f0 1042 {
mbed_official 133:d4dda5c437f0 1043 errorState = SD_CmdResp1Error(hsd, SD_CMD_WRITE_SINGLE_BLOCK);
mbed_official 133:d4dda5c437f0 1044 }
mbed_official 133:d4dda5c437f0 1045
mbed_official 133:d4dda5c437f0 1046 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 1047 {
mbed_official 133:d4dda5c437f0 1048 return errorState;
mbed_official 133:d4dda5c437f0 1049 }
mbed_official 133:d4dda5c437f0 1050
mbed_official 133:d4dda5c437f0 1051 /* Configure the SD DPSM (Data Path State Machine) */
mbed_official 133:d4dda5c437f0 1052 SDIO_DataInitStructure.DataTimeOut = SD_DATATIMEOUT;
mbed_official 133:d4dda5c437f0 1053 SDIO_DataInitStructure.DataLength = BlockSize * NumberOfBlocks;
mbed_official 133:d4dda5c437f0 1054 SDIO_DataInitStructure.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
mbed_official 133:d4dda5c437f0 1055 SDIO_DataInitStructure.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
mbed_official 133:d4dda5c437f0 1056 SDIO_DataInitStructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
mbed_official 133:d4dda5c437f0 1057 SDIO_DataInitStructure.DPSM = SDIO_DPSM_ENABLE;
mbed_official 133:d4dda5c437f0 1058 SDIO_DataConfig(hsd->Instance, &SDIO_DataInitStructure);
mbed_official 133:d4dda5c437f0 1059
mbed_official 133:d4dda5c437f0 1060 hsd->SdTransferErr = errorState;
mbed_official 133:d4dda5c437f0 1061
mbed_official 133:d4dda5c437f0 1062 return errorState;
mbed_official 133:d4dda5c437f0 1063 }
mbed_official 133:d4dda5c437f0 1064
mbed_official 133:d4dda5c437f0 1065 /**
mbed_official 133:d4dda5c437f0 1066 * @brief This function waits until the SD DMA data read transfer is finished.
mbed_official 133:d4dda5c437f0 1067 * This API should be called after HAL_SD_ReadBlocks_DMA() function
mbed_official 133:d4dda5c437f0 1068 * to insure that all data sent by the card is already transferred by the
mbed_official 133:d4dda5c437f0 1069 * DMA controller.
mbed_official 133:d4dda5c437f0 1070 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 1071 * @param Timeout: Timeout duration
mbed_official 133:d4dda5c437f0 1072 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 1073 */
mbed_official 133:d4dda5c437f0 1074 HAL_SD_ErrorTypedef HAL_SD_CheckReadOperation(SD_HandleTypeDef *hsd, uint32_t Timeout)
mbed_official 133:d4dda5c437f0 1075 {
mbed_official 133:d4dda5c437f0 1076 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 1077 uint32_t timeout = Timeout;
mbed_official 133:d4dda5c437f0 1078 uint32_t tmp1, tmp2;
mbed_official 133:d4dda5c437f0 1079 HAL_SD_ErrorTypedef tmp3;
mbed_official 133:d4dda5c437f0 1080
mbed_official 133:d4dda5c437f0 1081 /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */
mbed_official 133:d4dda5c437f0 1082 tmp1 = hsd->DmaTransferCplt;
mbed_official 133:d4dda5c437f0 1083 tmp2 = hsd->SdTransferCplt;
mbed_official 133:d4dda5c437f0 1084 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
mbed_official 133:d4dda5c437f0 1085
mbed_official 133:d4dda5c437f0 1086 while ((tmp1 == 0) && (tmp2 == 0) && (tmp3 == SD_OK) && (timeout > 0))
mbed_official 133:d4dda5c437f0 1087 {
mbed_official 133:d4dda5c437f0 1088 tmp1 = hsd->DmaTransferCplt;
mbed_official 133:d4dda5c437f0 1089 tmp2 = hsd->SdTransferCplt;
mbed_official 133:d4dda5c437f0 1090 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
mbed_official 133:d4dda5c437f0 1091 timeout--;
mbed_official 133:d4dda5c437f0 1092 }
mbed_official 133:d4dda5c437f0 1093
mbed_official 133:d4dda5c437f0 1094 timeout = Timeout;
mbed_official 133:d4dda5c437f0 1095
mbed_official 133:d4dda5c437f0 1096 /* Wait until the Rx transfer is no longer active */
mbed_official 133:d4dda5c437f0 1097 while((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXACT)) && (timeout > 0))
mbed_official 133:d4dda5c437f0 1098 {
mbed_official 133:d4dda5c437f0 1099 timeout--;
mbed_official 133:d4dda5c437f0 1100 }
mbed_official 133:d4dda5c437f0 1101
mbed_official 133:d4dda5c437f0 1102 /* Send stop command in multiblock read */
mbed_official 133:d4dda5c437f0 1103 if (hsd->SdOperation == SD_READ_MULTIPLE_BLOCK)
mbed_official 133:d4dda5c437f0 1104 {
mbed_official 133:d4dda5c437f0 1105 errorState = HAL_SD_StopTransfer(hsd);
mbed_official 133:d4dda5c437f0 1106 }
mbed_official 133:d4dda5c437f0 1107
mbed_official 133:d4dda5c437f0 1108 if ((timeout == 0) && (errorState == SD_OK))
mbed_official 133:d4dda5c437f0 1109 {
mbed_official 133:d4dda5c437f0 1110 errorState = SD_DATA_TIMEOUT;
mbed_official 133:d4dda5c437f0 1111 }
mbed_official 133:d4dda5c437f0 1112
mbed_official 133:d4dda5c437f0 1113 /* Clear all the static flags */
mbed_official 133:d4dda5c437f0 1114 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
mbed_official 133:d4dda5c437f0 1115
mbed_official 133:d4dda5c437f0 1116 /* Return error state */
mbed_official 133:d4dda5c437f0 1117 if (hsd->SdTransferErr != SD_OK)
mbed_official 133:d4dda5c437f0 1118 {
mbed_official 133:d4dda5c437f0 1119 return (HAL_SD_ErrorTypedef)(hsd->SdTransferErr);
mbed_official 133:d4dda5c437f0 1120 }
mbed_official 133:d4dda5c437f0 1121
mbed_official 133:d4dda5c437f0 1122 return errorState;
mbed_official 133:d4dda5c437f0 1123 }
mbed_official 133:d4dda5c437f0 1124
mbed_official 133:d4dda5c437f0 1125 /**
mbed_official 133:d4dda5c437f0 1126 * @brief This function waits until the SD DMA data write transfer is finished.
mbed_official 133:d4dda5c437f0 1127 * This API should be called after HAL_SD_WriteBlocks_DMA() function
mbed_official 133:d4dda5c437f0 1128 * to insure that all data sent by the card is already transferred by the
mbed_official 133:d4dda5c437f0 1129 * DMA controller.
mbed_official 133:d4dda5c437f0 1130 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 1131 * @param Timeout: Timeout duration
mbed_official 133:d4dda5c437f0 1132 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 1133 */
mbed_official 133:d4dda5c437f0 1134 HAL_SD_ErrorTypedef HAL_SD_CheckWriteOperation(SD_HandleTypeDef *hsd, uint32_t Timeout)
mbed_official 133:d4dda5c437f0 1135 {
mbed_official 133:d4dda5c437f0 1136 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 1137 uint32_t timeout = Timeout;
mbed_official 133:d4dda5c437f0 1138 uint32_t tmp1, tmp2;
mbed_official 133:d4dda5c437f0 1139 HAL_SD_ErrorTypedef tmp3;
mbed_official 133:d4dda5c437f0 1140
mbed_official 133:d4dda5c437f0 1141 /* Wait for DMA/SD transfer end or SD error variables to be in SD handle */
mbed_official 133:d4dda5c437f0 1142 tmp1 = hsd->DmaTransferCplt;
mbed_official 133:d4dda5c437f0 1143 tmp2 = hsd->SdTransferCplt;
mbed_official 133:d4dda5c437f0 1144 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
mbed_official 133:d4dda5c437f0 1145
mbed_official 133:d4dda5c437f0 1146 while ((tmp1 == 0) && (tmp2 == 0) && (tmp3 == SD_OK) && (timeout > 0))
mbed_official 133:d4dda5c437f0 1147 {
mbed_official 133:d4dda5c437f0 1148 tmp1 = hsd->DmaTransferCplt;
mbed_official 133:d4dda5c437f0 1149 tmp2 = hsd->SdTransferCplt;
mbed_official 133:d4dda5c437f0 1150 tmp3 = (HAL_SD_ErrorTypedef)hsd->SdTransferErr;
mbed_official 133:d4dda5c437f0 1151 timeout--;
mbed_official 133:d4dda5c437f0 1152 }
mbed_official 133:d4dda5c437f0 1153
mbed_official 133:d4dda5c437f0 1154 timeout = Timeout;
mbed_official 133:d4dda5c437f0 1155
mbed_official 133:d4dda5c437f0 1156 /* Wait until the Tx transfer is no longer active */
mbed_official 133:d4dda5c437f0 1157 while((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_TXACT)) && (timeout > 0))
mbed_official 133:d4dda5c437f0 1158 {
mbed_official 133:d4dda5c437f0 1159 timeout--;
mbed_official 133:d4dda5c437f0 1160 }
mbed_official 133:d4dda5c437f0 1161
mbed_official 133:d4dda5c437f0 1162 /* Send stop command in multiblock write */
mbed_official 133:d4dda5c437f0 1163 if (hsd->SdOperation == SD_WRITE_MULTIPLE_BLOCK)
mbed_official 133:d4dda5c437f0 1164 {
mbed_official 133:d4dda5c437f0 1165 errorState = HAL_SD_StopTransfer(hsd);
mbed_official 133:d4dda5c437f0 1166 }
mbed_official 133:d4dda5c437f0 1167
mbed_official 133:d4dda5c437f0 1168 if ((timeout == 0) && (errorState == SD_OK))
mbed_official 133:d4dda5c437f0 1169 {
mbed_official 133:d4dda5c437f0 1170 errorState = SD_DATA_TIMEOUT;
mbed_official 133:d4dda5c437f0 1171 }
mbed_official 133:d4dda5c437f0 1172
mbed_official 133:d4dda5c437f0 1173 /* Clear all the static flags */
mbed_official 133:d4dda5c437f0 1174 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
mbed_official 133:d4dda5c437f0 1175
mbed_official 133:d4dda5c437f0 1176 /* Return error state */
mbed_official 133:d4dda5c437f0 1177 if (hsd->SdTransferErr != SD_OK)
mbed_official 133:d4dda5c437f0 1178 {
mbed_official 133:d4dda5c437f0 1179 return (HAL_SD_ErrorTypedef)(hsd->SdTransferErr);
mbed_official 133:d4dda5c437f0 1180 }
mbed_official 133:d4dda5c437f0 1181
mbed_official 133:d4dda5c437f0 1182 /* Wait until write is complete */
mbed_official 133:d4dda5c437f0 1183 while(HAL_SD_GetStatus(hsd) != SD_TRANSFER_OK)
mbed_official 133:d4dda5c437f0 1184 {
mbed_official 133:d4dda5c437f0 1185 }
mbed_official 133:d4dda5c437f0 1186
mbed_official 133:d4dda5c437f0 1187 return errorState;
mbed_official 133:d4dda5c437f0 1188 }
mbed_official 133:d4dda5c437f0 1189
mbed_official 133:d4dda5c437f0 1190 /**
mbed_official 133:d4dda5c437f0 1191 * @brief Erases the specified memory area of the given SD card.
mbed_official 133:d4dda5c437f0 1192 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 1193 * @param startaddr: Start byte address
mbed_official 133:d4dda5c437f0 1194 * @param endaddr: End byte address
mbed_official 133:d4dda5c437f0 1195 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 1196 */
mbed_official 133:d4dda5c437f0 1197 HAL_SD_ErrorTypedef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint64_t startaddr, uint64_t endaddr)
mbed_official 133:d4dda5c437f0 1198 {
mbed_official 133:d4dda5c437f0 1199 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 1200 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
mbed_official 133:d4dda5c437f0 1201
mbed_official 133:d4dda5c437f0 1202 uint32_t delay = 0;
mbed_official 133:d4dda5c437f0 1203 __IO uint32_t maxdelay = 0;
mbed_official 133:d4dda5c437f0 1204 uint8_t cardstate = 0;
mbed_official 133:d4dda5c437f0 1205
mbed_official 133:d4dda5c437f0 1206 /* Check if the card command class supports erase command */
mbed_official 133:d4dda5c437f0 1207 if (((hsd->CSD[1] >> 20) & SD_CCCC_ERASE) == 0)
mbed_official 133:d4dda5c437f0 1208 {
mbed_official 133:d4dda5c437f0 1209 errorState = SD_REQUEST_NOT_APPLICABLE;
mbed_official 133:d4dda5c437f0 1210
mbed_official 133:d4dda5c437f0 1211 return errorState;
mbed_official 133:d4dda5c437f0 1212 }
mbed_official 133:d4dda5c437f0 1213
mbed_official 133:d4dda5c437f0 1214 /* Get max delay value */
mbed_official 133:d4dda5c437f0 1215 maxdelay = 120000 / (((hsd->Instance->CLKCR) & 0xFF) + 2);
mbed_official 133:d4dda5c437f0 1216
mbed_official 133:d4dda5c437f0 1217 if((SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
mbed_official 133:d4dda5c437f0 1218 {
mbed_official 133:d4dda5c437f0 1219 errorState = SD_LOCK_UNLOCK_FAILED;
mbed_official 133:d4dda5c437f0 1220
mbed_official 133:d4dda5c437f0 1221 return errorState;
mbed_official 133:d4dda5c437f0 1222 }
mbed_official 133:d4dda5c437f0 1223
mbed_official 133:d4dda5c437f0 1224 /* Get start and end block for high capacity cards */
mbed_official 133:d4dda5c437f0 1225 if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
mbed_official 133:d4dda5c437f0 1226 {
mbed_official 133:d4dda5c437f0 1227 startaddr /= 512;
mbed_official 133:d4dda5c437f0 1228 endaddr /= 512;
mbed_official 133:d4dda5c437f0 1229 }
mbed_official 133:d4dda5c437f0 1230
mbed_official 133:d4dda5c437f0 1231 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
mbed_official 133:d4dda5c437f0 1232 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
mbed_official 133:d4dda5c437f0 1233 (hsd->CardType == HIGH_CAPACITY_SD_CARD))
mbed_official 133:d4dda5c437f0 1234 {
mbed_official 133:d4dda5c437f0 1235 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
mbed_official 133:d4dda5c437f0 1236 SDIO_CmdInitStructure.Argument =(uint32_t)startaddr;
mbed_official 133:d4dda5c437f0 1237 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SD_ERASE_GRP_START;
mbed_official 133:d4dda5c437f0 1238 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 1239 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 1240 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 1241 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 1242
mbed_official 133:d4dda5c437f0 1243 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 1244 errorState = SD_CmdResp1Error(hsd, SD_CMD_SD_ERASE_GRP_START);
mbed_official 133:d4dda5c437f0 1245
mbed_official 133:d4dda5c437f0 1246 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 1247 {
mbed_official 133:d4dda5c437f0 1248 return errorState;
mbed_official 133:d4dda5c437f0 1249 }
mbed_official 133:d4dda5c437f0 1250
mbed_official 133:d4dda5c437f0 1251 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
mbed_official 133:d4dda5c437f0 1252 SDIO_CmdInitStructure.Argument = (uint32_t)endaddr;
mbed_official 133:d4dda5c437f0 1253 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SD_ERASE_GRP_END;
mbed_official 133:d4dda5c437f0 1254 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 1255
mbed_official 133:d4dda5c437f0 1256 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 1257 errorState = SD_CmdResp1Error(hsd, SD_CMD_SD_ERASE_GRP_END);
mbed_official 133:d4dda5c437f0 1258
mbed_official 133:d4dda5c437f0 1259 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 1260 {
mbed_official 133:d4dda5c437f0 1261 return errorState;
mbed_official 133:d4dda5c437f0 1262 }
mbed_official 133:d4dda5c437f0 1263 }
mbed_official 133:d4dda5c437f0 1264
mbed_official 133:d4dda5c437f0 1265 /* Send CMD38 ERASE */
mbed_official 133:d4dda5c437f0 1266 SDIO_CmdInitStructure.Argument = 0;
mbed_official 133:d4dda5c437f0 1267 SDIO_CmdInitStructure.CmdIndex = SD_CMD_ERASE;
mbed_official 133:d4dda5c437f0 1268 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 1269
mbed_official 133:d4dda5c437f0 1270 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 1271 errorState = SD_CmdResp1Error(hsd, SD_CMD_ERASE);
mbed_official 133:d4dda5c437f0 1272
mbed_official 133:d4dda5c437f0 1273 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 1274 {
mbed_official 133:d4dda5c437f0 1275 return errorState;
mbed_official 133:d4dda5c437f0 1276 }
mbed_official 133:d4dda5c437f0 1277
mbed_official 133:d4dda5c437f0 1278 for (; delay < maxdelay; delay++)
mbed_official 133:d4dda5c437f0 1279 {
mbed_official 133:d4dda5c437f0 1280 }
mbed_official 133:d4dda5c437f0 1281
mbed_official 133:d4dda5c437f0 1282 /* Wait untill the card is in programming state */
mbed_official 133:d4dda5c437f0 1283 errorState = SD_IsCardProgramming(hsd, &cardstate);
mbed_official 133:d4dda5c437f0 1284
mbed_official 133:d4dda5c437f0 1285 delay = SD_DATATIMEOUT;
mbed_official 133:d4dda5c437f0 1286
mbed_official 133:d4dda5c437f0 1287 while ((delay > 0) && (errorState == SD_OK) && ((cardstate == SD_CARD_PROGRAMMING) || (cardstate == SD_CARD_RECEIVING)))
mbed_official 133:d4dda5c437f0 1288 {
mbed_official 133:d4dda5c437f0 1289 errorState = SD_IsCardProgramming(hsd, &cardstate);
mbed_official 133:d4dda5c437f0 1290 delay--;
mbed_official 133:d4dda5c437f0 1291 }
mbed_official 133:d4dda5c437f0 1292
mbed_official 133:d4dda5c437f0 1293 return errorState;
mbed_official 133:d4dda5c437f0 1294 }
mbed_official 133:d4dda5c437f0 1295
mbed_official 133:d4dda5c437f0 1296 /**
mbed_official 133:d4dda5c437f0 1297 * @brief This function handles SD card interrupt request.
mbed_official 133:d4dda5c437f0 1298 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 1299 * @retval None
mbed_official 133:d4dda5c437f0 1300 */
mbed_official 133:d4dda5c437f0 1301 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 1302 {
mbed_official 133:d4dda5c437f0 1303 /* Check for SDIO interrupt flags */
mbed_official 133:d4dda5c437f0 1304 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_DATAEND))
mbed_official 133:d4dda5c437f0 1305 {
mbed_official 133:d4dda5c437f0 1306 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_IT_DATAEND);
mbed_official 133:d4dda5c437f0 1307
mbed_official 133:d4dda5c437f0 1308 /* SD transfer is complete */
mbed_official 133:d4dda5c437f0 1309 hsd->SdTransferCplt = 1;
mbed_official 133:d4dda5c437f0 1310
mbed_official 133:d4dda5c437f0 1311 /* No transfer error */
mbed_official 133:d4dda5c437f0 1312 hsd->SdTransferErr = SD_OK;
mbed_official 133:d4dda5c437f0 1313
mbed_official 133:d4dda5c437f0 1314 HAL_SD_XferCpltCallback(hsd);
mbed_official 133:d4dda5c437f0 1315 }
mbed_official 133:d4dda5c437f0 1316 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_DCRCFAIL))
mbed_official 133:d4dda5c437f0 1317 {
mbed_official 133:d4dda5c437f0 1318 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
mbed_official 133:d4dda5c437f0 1319
mbed_official 133:d4dda5c437f0 1320 hsd->SdTransferErr = SD_DATA_CRC_FAIL;
mbed_official 133:d4dda5c437f0 1321
mbed_official 133:d4dda5c437f0 1322 HAL_SD_XferErrorCallback(hsd);
mbed_official 133:d4dda5c437f0 1323
mbed_official 133:d4dda5c437f0 1324 }
mbed_official 133:d4dda5c437f0 1325 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_DTIMEOUT))
mbed_official 133:d4dda5c437f0 1326 {
mbed_official 133:d4dda5c437f0 1327 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
mbed_official 133:d4dda5c437f0 1328
mbed_official 133:d4dda5c437f0 1329 hsd->SdTransferErr = SD_DATA_TIMEOUT;
mbed_official 133:d4dda5c437f0 1330
mbed_official 133:d4dda5c437f0 1331 HAL_SD_XferErrorCallback(hsd);
mbed_official 133:d4dda5c437f0 1332 }
mbed_official 133:d4dda5c437f0 1333 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_RXOVERR))
mbed_official 133:d4dda5c437f0 1334 {
mbed_official 133:d4dda5c437f0 1335 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
mbed_official 133:d4dda5c437f0 1336
mbed_official 133:d4dda5c437f0 1337 hsd->SdTransferErr = SD_RX_OVERRUN;
mbed_official 133:d4dda5c437f0 1338
mbed_official 133:d4dda5c437f0 1339 HAL_SD_XferErrorCallback(hsd);
mbed_official 133:d4dda5c437f0 1340 }
mbed_official 133:d4dda5c437f0 1341 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_TXUNDERR))
mbed_official 133:d4dda5c437f0 1342 {
mbed_official 133:d4dda5c437f0 1343 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_TXUNDERR);
mbed_official 133:d4dda5c437f0 1344
mbed_official 133:d4dda5c437f0 1345 hsd->SdTransferErr = SD_TX_UNDERRUN;
mbed_official 133:d4dda5c437f0 1346
mbed_official 133:d4dda5c437f0 1347 HAL_SD_XferErrorCallback(hsd);
mbed_official 133:d4dda5c437f0 1348 }
mbed_official 133:d4dda5c437f0 1349 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_IT_STBITERR))
mbed_official 133:d4dda5c437f0 1350 {
mbed_official 133:d4dda5c437f0 1351 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
mbed_official 133:d4dda5c437f0 1352
mbed_official 133:d4dda5c437f0 1353 hsd->SdTransferErr = SD_START_BIT_ERR;
mbed_official 133:d4dda5c437f0 1354
mbed_official 133:d4dda5c437f0 1355 HAL_SD_XferErrorCallback(hsd);
mbed_official 133:d4dda5c437f0 1356 }
mbed_official 133:d4dda5c437f0 1357
mbed_official 133:d4dda5c437f0 1358 /* Disable all SDIO peripheral interrupt sources */
mbed_official 133:d4dda5c437f0 1359 __HAL_SD_SDIO_DISABLE_IT(hsd, SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND |\
mbed_official 133:d4dda5c437f0 1360 SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR |\
mbed_official 133:d4dda5c437f0 1361 SDIO_IT_RXOVERR | SDIO_IT_STBITERR);
mbed_official 133:d4dda5c437f0 1362 }
mbed_official 133:d4dda5c437f0 1363
mbed_official 133:d4dda5c437f0 1364
mbed_official 133:d4dda5c437f0 1365 /**
mbed_official 133:d4dda5c437f0 1366 * @brief SD end of transfer callback.
mbed_official 133:d4dda5c437f0 1367 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 1368 * @retval None
mbed_official 133:d4dda5c437f0 1369 */
mbed_official 133:d4dda5c437f0 1370 __weak void HAL_SD_XferCpltCallback(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 1371 {
mbed_official 133:d4dda5c437f0 1372 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 133:d4dda5c437f0 1373 the HAL_SD_XferCpltCallback could be implemented in the user file
mbed_official 133:d4dda5c437f0 1374 */
mbed_official 133:d4dda5c437f0 1375 }
mbed_official 133:d4dda5c437f0 1376
mbed_official 133:d4dda5c437f0 1377 /**
mbed_official 133:d4dda5c437f0 1378 * @brief SD Transfer Error callback.
mbed_official 133:d4dda5c437f0 1379 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 1380 * @retval None
mbed_official 133:d4dda5c437f0 1381 */
mbed_official 133:d4dda5c437f0 1382 __weak void HAL_SD_XferErrorCallback(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 1383 {
mbed_official 133:d4dda5c437f0 1384 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 133:d4dda5c437f0 1385 the HAL_SD_XferErrorCallback could be implemented in the user file
mbed_official 133:d4dda5c437f0 1386 */
mbed_official 133:d4dda5c437f0 1387 }
mbed_official 133:d4dda5c437f0 1388
mbed_official 133:d4dda5c437f0 1389 /**
mbed_official 133:d4dda5c437f0 1390 * @brief SD Transfer complete Rx callback in non blocking mode.
mbed_official 242:7074e42da0b2 1391 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
mbed_official 242:7074e42da0b2 1392 * the configuration information for the specified DMA module.
mbed_official 133:d4dda5c437f0 1393 * @retval None
mbed_official 133:d4dda5c437f0 1394 */
mbed_official 133:d4dda5c437f0 1395 __weak void HAL_SD_DMA_RxCpltCallback(DMA_HandleTypeDef *hdma)
mbed_official 133:d4dda5c437f0 1396 {
mbed_official 133:d4dda5c437f0 1397 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 133:d4dda5c437f0 1398 the HAL_SD_DMA_RxCpltCallback could be implemented in the user file
mbed_official 133:d4dda5c437f0 1399 */
mbed_official 133:d4dda5c437f0 1400 }
mbed_official 133:d4dda5c437f0 1401
mbed_official 133:d4dda5c437f0 1402 /**
mbed_official 133:d4dda5c437f0 1403 * @brief SD DMA transfer complete Rx error callback.
mbed_official 242:7074e42da0b2 1404 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
mbed_official 242:7074e42da0b2 1405 * the configuration information for the specified DMA module.
mbed_official 133:d4dda5c437f0 1406 * @retval None
mbed_official 133:d4dda5c437f0 1407 */
mbed_official 133:d4dda5c437f0 1408 __weak void HAL_SD_DMA_RxErrorCallback(DMA_HandleTypeDef *hdma)
mbed_official 133:d4dda5c437f0 1409 {
mbed_official 133:d4dda5c437f0 1410 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 133:d4dda5c437f0 1411 the HAL_SD_DMA_RxErrorCallback could be implemented in the user file
mbed_official 133:d4dda5c437f0 1412 */
mbed_official 133:d4dda5c437f0 1413 }
mbed_official 133:d4dda5c437f0 1414
mbed_official 133:d4dda5c437f0 1415 /**
mbed_official 133:d4dda5c437f0 1416 * @brief SD Transfer complete Tx callback in non blocking mode.
mbed_official 242:7074e42da0b2 1417 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
mbed_official 242:7074e42da0b2 1418 * the configuration information for the specified DMA module.
mbed_official 133:d4dda5c437f0 1419 * @retval None
mbed_official 133:d4dda5c437f0 1420 */
mbed_official 133:d4dda5c437f0 1421 __weak void HAL_SD_DMA_TxCpltCallback(DMA_HandleTypeDef *hdma)
mbed_official 133:d4dda5c437f0 1422 {
mbed_official 133:d4dda5c437f0 1423 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 133:d4dda5c437f0 1424 the HAL_SD_DMA_TxCpltCallback could be implemented in the user file
mbed_official 133:d4dda5c437f0 1425 */
mbed_official 133:d4dda5c437f0 1426 }
mbed_official 133:d4dda5c437f0 1427
mbed_official 133:d4dda5c437f0 1428 /**
mbed_official 133:d4dda5c437f0 1429 * @brief SD DMA transfer complete error Tx callback.
mbed_official 242:7074e42da0b2 1430 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
mbed_official 242:7074e42da0b2 1431 * the configuration information for the specified DMA module.
mbed_official 133:d4dda5c437f0 1432 * @retval None
mbed_official 133:d4dda5c437f0 1433 */
mbed_official 133:d4dda5c437f0 1434 __weak void HAL_SD_DMA_TxErrorCallback(DMA_HandleTypeDef *hdma)
mbed_official 133:d4dda5c437f0 1435 {
mbed_official 133:d4dda5c437f0 1436 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 133:d4dda5c437f0 1437 the HAL_SD_DMA_TxErrorCallback could be implemented in the user file
mbed_official 133:d4dda5c437f0 1438 */
mbed_official 133:d4dda5c437f0 1439 }
mbed_official 133:d4dda5c437f0 1440
mbed_official 133:d4dda5c437f0 1441 /**
mbed_official 133:d4dda5c437f0 1442 * @}
mbed_official 133:d4dda5c437f0 1443 */
mbed_official 133:d4dda5c437f0 1444
mbed_official 133:d4dda5c437f0 1445 /** @defgroup SD_Group3 Peripheral Control functions
mbed_official 133:d4dda5c437f0 1446 * @brief management functions
mbed_official 133:d4dda5c437f0 1447 *
mbed_official 133:d4dda5c437f0 1448 @verbatim
mbed_official 133:d4dda5c437f0 1449 ==============================================================================
mbed_official 133:d4dda5c437f0 1450 ##### Peripheral Control functions #####
mbed_official 133:d4dda5c437f0 1451 ==============================================================================
mbed_official 133:d4dda5c437f0 1452 [..]
mbed_official 133:d4dda5c437f0 1453 This subsection provides a set of functions allowing to control the SD card
mbed_official 133:d4dda5c437f0 1454 operations.
mbed_official 133:d4dda5c437f0 1455
mbed_official 133:d4dda5c437f0 1456 @endverbatim
mbed_official 133:d4dda5c437f0 1457 * @{
mbed_official 133:d4dda5c437f0 1458 */
mbed_official 133:d4dda5c437f0 1459
mbed_official 133:d4dda5c437f0 1460 /**
mbed_official 133:d4dda5c437f0 1461 * @brief Returns information about specific card.
mbed_official 133:d4dda5c437f0 1462 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 1463 * @param pCardInfo: Pointer to a HAL_SD_CardInfoTypedef structure that
mbed_official 133:d4dda5c437f0 1464 * contains all SD cardinformation
mbed_official 133:d4dda5c437f0 1465 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 1466 */
mbed_official 133:d4dda5c437f0 1467 HAL_SD_ErrorTypedef HAL_SD_Get_CardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *pCardInfo)
mbed_official 133:d4dda5c437f0 1468 {
mbed_official 133:d4dda5c437f0 1469 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 1470 uint32_t tmp = 0;
mbed_official 133:d4dda5c437f0 1471
mbed_official 133:d4dda5c437f0 1472 pCardInfo->CardType = (uint8_t)(hsd->CardType);
mbed_official 133:d4dda5c437f0 1473 pCardInfo->RCA = (uint16_t)(hsd->RCA);
mbed_official 133:d4dda5c437f0 1474
mbed_official 133:d4dda5c437f0 1475 /* Byte 0 */
mbed_official 133:d4dda5c437f0 1476 tmp = (hsd->CSD[0] & 0xFF000000) >> 24;
mbed_official 133:d4dda5c437f0 1477 pCardInfo->SD_csd.CSDStruct = (uint8_t)((tmp & 0xC0) >> 6);
mbed_official 133:d4dda5c437f0 1478 pCardInfo->SD_csd.SysSpecVersion = (uint8_t)((tmp & 0x3C) >> 2);
mbed_official 133:d4dda5c437f0 1479 pCardInfo->SD_csd.Reserved1 = tmp & 0x03;
mbed_official 133:d4dda5c437f0 1480
mbed_official 133:d4dda5c437f0 1481 /* Byte 1 */
mbed_official 133:d4dda5c437f0 1482 tmp = (hsd->CSD[0] & 0x00FF0000) >> 16;
mbed_official 133:d4dda5c437f0 1483 pCardInfo->SD_csd.TAAC = (uint8_t)tmp;
mbed_official 133:d4dda5c437f0 1484
mbed_official 133:d4dda5c437f0 1485 /* Byte 2 */
mbed_official 133:d4dda5c437f0 1486 tmp = (hsd->CSD[0] & 0x0000FF00) >> 8;
mbed_official 133:d4dda5c437f0 1487 pCardInfo->SD_csd.NSAC = (uint8_t)tmp;
mbed_official 133:d4dda5c437f0 1488
mbed_official 133:d4dda5c437f0 1489 /* Byte 3 */
mbed_official 133:d4dda5c437f0 1490 tmp = hsd->CSD[0] & 0x000000FF;
mbed_official 133:d4dda5c437f0 1491 pCardInfo->SD_csd.MaxBusClkFrec = (uint8_t)tmp;
mbed_official 133:d4dda5c437f0 1492
mbed_official 133:d4dda5c437f0 1493 /* Byte 4 */
mbed_official 133:d4dda5c437f0 1494 tmp = (hsd->CSD[1] & 0xFF000000) >> 24;
mbed_official 133:d4dda5c437f0 1495 pCardInfo->SD_csd.CardComdClasses = (uint16_t)(tmp << 4);
mbed_official 133:d4dda5c437f0 1496
mbed_official 133:d4dda5c437f0 1497 /* Byte 5 */
mbed_official 133:d4dda5c437f0 1498 tmp = (hsd->CSD[1] & 0x00FF0000) >> 16;
mbed_official 133:d4dda5c437f0 1499 pCardInfo->SD_csd.CardComdClasses |= (uint16_t)((tmp & 0xF0) >> 4);
mbed_official 133:d4dda5c437f0 1500 pCardInfo->SD_csd.RdBlockLen = (uint8_t)(tmp & 0x0F);
mbed_official 133:d4dda5c437f0 1501
mbed_official 133:d4dda5c437f0 1502 /* Byte 6 */
mbed_official 133:d4dda5c437f0 1503 tmp = (hsd->CSD[1] & 0x0000FF00) >> 8;
mbed_official 133:d4dda5c437f0 1504 pCardInfo->SD_csd.PartBlockRead = (uint8_t)((tmp & 0x80) >> 7);
mbed_official 133:d4dda5c437f0 1505 pCardInfo->SD_csd.WrBlockMisalign = (uint8_t)((tmp & 0x40) >> 6);
mbed_official 133:d4dda5c437f0 1506 pCardInfo->SD_csd.RdBlockMisalign = (uint8_t)((tmp & 0x20) >> 5);
mbed_official 133:d4dda5c437f0 1507 pCardInfo->SD_csd.DSRImpl = (uint8_t)((tmp & 0x10) >> 4);
mbed_official 133:d4dda5c437f0 1508 pCardInfo->SD_csd.Reserved2 = 0; /*!< Reserved */
mbed_official 133:d4dda5c437f0 1509
mbed_official 133:d4dda5c437f0 1510 if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0))
mbed_official 133:d4dda5c437f0 1511 {
mbed_official 133:d4dda5c437f0 1512 pCardInfo->SD_csd.DeviceSize = (tmp & 0x03) << 10;
mbed_official 133:d4dda5c437f0 1513
mbed_official 133:d4dda5c437f0 1514 /* Byte 7 */
mbed_official 133:d4dda5c437f0 1515 tmp = (uint8_t)(hsd->CSD[1] & 0x000000FF);
mbed_official 133:d4dda5c437f0 1516 pCardInfo->SD_csd.DeviceSize |= (tmp) << 2;
mbed_official 133:d4dda5c437f0 1517
mbed_official 133:d4dda5c437f0 1518 /* Byte 8 */
mbed_official 133:d4dda5c437f0 1519 tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000) >> 24);
mbed_official 133:d4dda5c437f0 1520 pCardInfo->SD_csd.DeviceSize |= (tmp & 0xC0) >> 6;
mbed_official 133:d4dda5c437f0 1521
mbed_official 133:d4dda5c437f0 1522 pCardInfo->SD_csd.MaxRdCurrentVDDMin = (tmp & 0x38) >> 3;
mbed_official 133:d4dda5c437f0 1523 pCardInfo->SD_csd.MaxRdCurrentVDDMax = (tmp & 0x07);
mbed_official 133:d4dda5c437f0 1524
mbed_official 133:d4dda5c437f0 1525 /* Byte 9 */
mbed_official 133:d4dda5c437f0 1526 tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000) >> 16);
mbed_official 133:d4dda5c437f0 1527 pCardInfo->SD_csd.MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5;
mbed_official 133:d4dda5c437f0 1528 pCardInfo->SD_csd.MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2;
mbed_official 133:d4dda5c437f0 1529 pCardInfo->SD_csd.DeviceSizeMul = (tmp & 0x03) << 1;
mbed_official 133:d4dda5c437f0 1530 /* Byte 10 */
mbed_official 133:d4dda5c437f0 1531 tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00) >> 8);
mbed_official 133:d4dda5c437f0 1532 pCardInfo->SD_csd.DeviceSizeMul |= (tmp & 0x80) >> 7;
mbed_official 133:d4dda5c437f0 1533
mbed_official 133:d4dda5c437f0 1534 pCardInfo->CardCapacity = (pCardInfo->SD_csd.DeviceSize + 1) ;
mbed_official 133:d4dda5c437f0 1535 pCardInfo->CardCapacity *= (1 << (pCardInfo->SD_csd.DeviceSizeMul + 2));
mbed_official 133:d4dda5c437f0 1536 pCardInfo->CardBlockSize = 1 << (pCardInfo->SD_csd.RdBlockLen);
mbed_official 133:d4dda5c437f0 1537 pCardInfo->CardCapacity *= pCardInfo->CardBlockSize;
mbed_official 133:d4dda5c437f0 1538 }
mbed_official 133:d4dda5c437f0 1539 else if (hsd->CardType == HIGH_CAPACITY_SD_CARD)
mbed_official 133:d4dda5c437f0 1540 {
mbed_official 133:d4dda5c437f0 1541 /* Byte 7 */
mbed_official 133:d4dda5c437f0 1542 tmp = (uint8_t)(hsd->CSD[1] & 0x000000FF);
mbed_official 133:d4dda5c437f0 1543 pCardInfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16;
mbed_official 133:d4dda5c437f0 1544
mbed_official 133:d4dda5c437f0 1545 /* Byte 8 */
mbed_official 133:d4dda5c437f0 1546 tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000) >> 24);
mbed_official 133:d4dda5c437f0 1547
mbed_official 133:d4dda5c437f0 1548 pCardInfo->SD_csd.DeviceSize |= (tmp << 8);
mbed_official 133:d4dda5c437f0 1549
mbed_official 133:d4dda5c437f0 1550 /* Byte 9 */
mbed_official 133:d4dda5c437f0 1551 tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000) >> 16);
mbed_official 133:d4dda5c437f0 1552
mbed_official 133:d4dda5c437f0 1553 pCardInfo->SD_csd.DeviceSize |= (tmp);
mbed_official 133:d4dda5c437f0 1554
mbed_official 133:d4dda5c437f0 1555 /* Byte 10 */
mbed_official 133:d4dda5c437f0 1556 tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00) >> 8);
mbed_official 133:d4dda5c437f0 1557
mbed_official 133:d4dda5c437f0 1558 pCardInfo->CardCapacity = ((pCardInfo->SD_csd.DeviceSize + 1)) * 512 * 1024;
mbed_official 133:d4dda5c437f0 1559 pCardInfo->CardBlockSize = 512;
mbed_official 133:d4dda5c437f0 1560 }
mbed_official 133:d4dda5c437f0 1561
mbed_official 133:d4dda5c437f0 1562 pCardInfo->SD_csd.EraseGrSize = (tmp & 0x40) >> 6;
mbed_official 133:d4dda5c437f0 1563 pCardInfo->SD_csd.EraseGrMul = (tmp & 0x3F) << 1;
mbed_official 133:d4dda5c437f0 1564
mbed_official 133:d4dda5c437f0 1565 /* Byte 11 */
mbed_official 133:d4dda5c437f0 1566 tmp = (uint8_t)(hsd->CSD[2] & 0x000000FF);
mbed_official 133:d4dda5c437f0 1567 pCardInfo->SD_csd.EraseGrMul |= (tmp & 0x80) >> 7;
mbed_official 133:d4dda5c437f0 1568 pCardInfo->SD_csd.WrProtectGrSize = (tmp & 0x7F);
mbed_official 133:d4dda5c437f0 1569
mbed_official 133:d4dda5c437f0 1570 /* Byte 12 */
mbed_official 133:d4dda5c437f0 1571 tmp = (uint8_t)((hsd->CSD[3] & 0xFF000000) >> 24);
mbed_official 133:d4dda5c437f0 1572 pCardInfo->SD_csd.WrProtectGrEnable = (tmp & 0x80) >> 7;
mbed_official 133:d4dda5c437f0 1573 pCardInfo->SD_csd.ManDeflECC = (tmp & 0x60) >> 5;
mbed_official 133:d4dda5c437f0 1574 pCardInfo->SD_csd.WrSpeedFact = (tmp & 0x1C) >> 2;
mbed_official 133:d4dda5c437f0 1575 pCardInfo->SD_csd.MaxWrBlockLen = (tmp & 0x03) << 2;
mbed_official 133:d4dda5c437f0 1576
mbed_official 133:d4dda5c437f0 1577 /* Byte 13 */
mbed_official 133:d4dda5c437f0 1578 tmp = (uint8_t)((hsd->CSD[3] & 0x00FF0000) >> 16);
mbed_official 133:d4dda5c437f0 1579 pCardInfo->SD_csd.MaxWrBlockLen |= (tmp & 0xC0) >> 6;
mbed_official 133:d4dda5c437f0 1580 pCardInfo->SD_csd.WriteBlockPaPartial = (tmp & 0x20) >> 5;
mbed_official 133:d4dda5c437f0 1581 pCardInfo->SD_csd.Reserved3 = 0;
mbed_official 133:d4dda5c437f0 1582 pCardInfo->SD_csd.ContentProtectAppli = (tmp & 0x01);
mbed_official 133:d4dda5c437f0 1583
mbed_official 133:d4dda5c437f0 1584 /* Byte 14 */
mbed_official 133:d4dda5c437f0 1585 tmp = (uint8_t)((hsd->CSD[3] & 0x0000FF00) >> 8);
mbed_official 133:d4dda5c437f0 1586 pCardInfo->SD_csd.FileFormatGrouop = (tmp & 0x80) >> 7;
mbed_official 133:d4dda5c437f0 1587 pCardInfo->SD_csd.CopyFlag = (tmp & 0x40) >> 6;
mbed_official 133:d4dda5c437f0 1588 pCardInfo->SD_csd.PermWrProtect = (tmp & 0x20) >> 5;
mbed_official 133:d4dda5c437f0 1589 pCardInfo->SD_csd.TempWrProtect = (tmp & 0x10) >> 4;
mbed_official 133:d4dda5c437f0 1590 pCardInfo->SD_csd.FileFormat = (tmp & 0x0C) >> 2;
mbed_official 133:d4dda5c437f0 1591 pCardInfo->SD_csd.ECC = (tmp & 0x03);
mbed_official 133:d4dda5c437f0 1592
mbed_official 133:d4dda5c437f0 1593 /* Byte 15 */
mbed_official 133:d4dda5c437f0 1594 tmp = (uint8_t)(hsd->CSD[3] & 0x000000FF);
mbed_official 133:d4dda5c437f0 1595 pCardInfo->SD_csd.CSD_CRC = (tmp & 0xFE) >> 1;
mbed_official 133:d4dda5c437f0 1596 pCardInfo->SD_csd.Reserved4 = 1;
mbed_official 133:d4dda5c437f0 1597
mbed_official 133:d4dda5c437f0 1598 /* Byte 0 */
mbed_official 133:d4dda5c437f0 1599 tmp = (uint8_t)((hsd->CID[0] & 0xFF000000) >> 24);
mbed_official 133:d4dda5c437f0 1600 pCardInfo->SD_cid.ManufacturerID = tmp;
mbed_official 133:d4dda5c437f0 1601
mbed_official 133:d4dda5c437f0 1602 /* Byte 1 */
mbed_official 133:d4dda5c437f0 1603 tmp = (uint8_t)((hsd->CID[0] & 0x00FF0000) >> 16);
mbed_official 133:d4dda5c437f0 1604 pCardInfo->SD_cid.OEM_AppliID = tmp << 8;
mbed_official 133:d4dda5c437f0 1605
mbed_official 133:d4dda5c437f0 1606 /* Byte 2 */
mbed_official 133:d4dda5c437f0 1607 tmp = (uint8_t)((hsd->CID[0] & 0x000000FF00) >> 8);
mbed_official 133:d4dda5c437f0 1608 pCardInfo->SD_cid.OEM_AppliID |= tmp;
mbed_official 133:d4dda5c437f0 1609
mbed_official 133:d4dda5c437f0 1610 /* Byte 3 */
mbed_official 133:d4dda5c437f0 1611 tmp = (uint8_t)(hsd->CID[0] & 0x000000FF);
mbed_official 133:d4dda5c437f0 1612 pCardInfo->SD_cid.ProdName1 = tmp << 24;
mbed_official 133:d4dda5c437f0 1613
mbed_official 133:d4dda5c437f0 1614 /* Byte 4 */
mbed_official 133:d4dda5c437f0 1615 tmp = (uint8_t)((hsd->CID[1] & 0xFF000000) >> 24);
mbed_official 133:d4dda5c437f0 1616 pCardInfo->SD_cid.ProdName1 |= tmp << 16;
mbed_official 133:d4dda5c437f0 1617
mbed_official 133:d4dda5c437f0 1618 /* Byte 5 */
mbed_official 133:d4dda5c437f0 1619 tmp = (uint8_t)((hsd->CID[1] & 0x00FF0000) >> 16);
mbed_official 133:d4dda5c437f0 1620 pCardInfo->SD_cid.ProdName1 |= tmp << 8;
mbed_official 133:d4dda5c437f0 1621
mbed_official 133:d4dda5c437f0 1622 /* Byte 6 */
mbed_official 133:d4dda5c437f0 1623 tmp = (uint8_t)((hsd->CID[1] & 0x0000FF00) >> 8);
mbed_official 133:d4dda5c437f0 1624 pCardInfo->SD_cid.ProdName1 |= tmp;
mbed_official 133:d4dda5c437f0 1625
mbed_official 133:d4dda5c437f0 1626 /* Byte 7 */
mbed_official 133:d4dda5c437f0 1627 tmp = (uint8_t)(hsd->CID[1] & 0x000000FF);
mbed_official 133:d4dda5c437f0 1628 pCardInfo->SD_cid.ProdName2 = tmp;
mbed_official 133:d4dda5c437f0 1629
mbed_official 133:d4dda5c437f0 1630 /* Byte 8 */
mbed_official 133:d4dda5c437f0 1631 tmp = (uint8_t)((hsd->CID[2] & 0xFF000000) >> 24);
mbed_official 133:d4dda5c437f0 1632 pCardInfo->SD_cid.ProdRev = tmp;
mbed_official 133:d4dda5c437f0 1633
mbed_official 133:d4dda5c437f0 1634 /* Byte 9 */
mbed_official 133:d4dda5c437f0 1635 tmp = (uint8_t)((hsd->CID[2] & 0x00FF0000) >> 16);
mbed_official 133:d4dda5c437f0 1636 pCardInfo->SD_cid.ProdSN = tmp << 24;
mbed_official 133:d4dda5c437f0 1637
mbed_official 133:d4dda5c437f0 1638 /* Byte 10 */
mbed_official 133:d4dda5c437f0 1639 tmp = (uint8_t)((hsd->CID[2] & 0x0000FF00) >> 8);
mbed_official 133:d4dda5c437f0 1640 pCardInfo->SD_cid.ProdSN |= tmp << 16;
mbed_official 133:d4dda5c437f0 1641
mbed_official 133:d4dda5c437f0 1642 /* Byte 11 */
mbed_official 133:d4dda5c437f0 1643 tmp = (uint8_t)(hsd->CID[2] & 0x000000FF);
mbed_official 133:d4dda5c437f0 1644 pCardInfo->SD_cid.ProdSN |= tmp << 8;
mbed_official 133:d4dda5c437f0 1645
mbed_official 133:d4dda5c437f0 1646 /* Byte 12 */
mbed_official 133:d4dda5c437f0 1647 tmp = (uint8_t)((hsd->CID[3] & 0xFF000000) >> 24);
mbed_official 133:d4dda5c437f0 1648 pCardInfo->SD_cid.ProdSN |= tmp;
mbed_official 133:d4dda5c437f0 1649
mbed_official 133:d4dda5c437f0 1650 /* Byte 13 */
mbed_official 133:d4dda5c437f0 1651 tmp = (uint8_t)((hsd->CID[3] & 0x00FF0000) >> 16);
mbed_official 133:d4dda5c437f0 1652 pCardInfo->SD_cid.Reserved1 |= (tmp & 0xF0) >> 4;
mbed_official 133:d4dda5c437f0 1653 pCardInfo->SD_cid.ManufactDate = (tmp & 0x0F) << 8;
mbed_official 133:d4dda5c437f0 1654
mbed_official 133:d4dda5c437f0 1655 /* Byte 14 */
mbed_official 133:d4dda5c437f0 1656 tmp = (uint8_t)((hsd->CID[3] & 0x0000FF00) >> 8);
mbed_official 133:d4dda5c437f0 1657 pCardInfo->SD_cid.ManufactDate |= tmp;
mbed_official 133:d4dda5c437f0 1658
mbed_official 133:d4dda5c437f0 1659 /* Byte 15 */
mbed_official 133:d4dda5c437f0 1660 tmp = (uint8_t)(hsd->CID[3] & 0x000000FF);
mbed_official 133:d4dda5c437f0 1661 pCardInfo->SD_cid.CID_CRC = (tmp & 0xFE) >> 1;
mbed_official 133:d4dda5c437f0 1662 pCardInfo->SD_cid.Reserved2 = 1;
mbed_official 133:d4dda5c437f0 1663
mbed_official 133:d4dda5c437f0 1664 return errorState;
mbed_official 133:d4dda5c437f0 1665 }
mbed_official 133:d4dda5c437f0 1666
mbed_official 133:d4dda5c437f0 1667 /**
mbed_official 133:d4dda5c437f0 1668 * @brief Enables wide bus operation for the requested card if supported by
mbed_official 133:d4dda5c437f0 1669 * card.
mbed_official 133:d4dda5c437f0 1670 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 1671 * @param WideMode: Specifies the SD card wide bus mode
mbed_official 133:d4dda5c437f0 1672 * This parameter can be one of the following values:
mbed_official 133:d4dda5c437f0 1673 * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer (Only for MMC)
mbed_official 133:d4dda5c437f0 1674 * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
mbed_official 133:d4dda5c437f0 1675 * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
mbed_official 133:d4dda5c437f0 1676 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 1677 */
mbed_official 133:d4dda5c437f0 1678 HAL_SD_ErrorTypedef HAL_SD_WideBusOperation_Config(SD_HandleTypeDef *hsd, uint32_t WideMode)
mbed_official 133:d4dda5c437f0 1679 {
mbed_official 133:d4dda5c437f0 1680 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 1681 SDIO_InitTypeDef Init;
mbed_official 133:d4dda5c437f0 1682
mbed_official 133:d4dda5c437f0 1683 /* MMC Card does not support this feature */
mbed_official 133:d4dda5c437f0 1684 if (hsd->CardType == MULTIMEDIA_CARD)
mbed_official 133:d4dda5c437f0 1685 {
mbed_official 133:d4dda5c437f0 1686 errorState = SD_UNSUPPORTED_FEATURE;
mbed_official 133:d4dda5c437f0 1687
mbed_official 133:d4dda5c437f0 1688 return errorState;
mbed_official 133:d4dda5c437f0 1689 }
mbed_official 133:d4dda5c437f0 1690 else if ((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
mbed_official 133:d4dda5c437f0 1691 (hsd->CardType == HIGH_CAPACITY_SD_CARD))
mbed_official 133:d4dda5c437f0 1692 {
mbed_official 133:d4dda5c437f0 1693 if (WideMode == SDIO_BUS_WIDE_8B)
mbed_official 133:d4dda5c437f0 1694 {
mbed_official 133:d4dda5c437f0 1695 errorState = SD_UNSUPPORTED_FEATURE;
mbed_official 133:d4dda5c437f0 1696
mbed_official 133:d4dda5c437f0 1697 return errorState;
mbed_official 133:d4dda5c437f0 1698 }
mbed_official 133:d4dda5c437f0 1699 else if (WideMode == SDIO_BUS_WIDE_4B)
mbed_official 133:d4dda5c437f0 1700 {
mbed_official 133:d4dda5c437f0 1701 errorState = SD_WideBus_Enable(hsd);
mbed_official 133:d4dda5c437f0 1702
mbed_official 133:d4dda5c437f0 1703 if (errorState == SD_OK)
mbed_official 133:d4dda5c437f0 1704 {
mbed_official 133:d4dda5c437f0 1705 /* Configure the SDIO peripheral */
mbed_official 133:d4dda5c437f0 1706 Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
mbed_official 133:d4dda5c437f0 1707 Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
mbed_official 133:d4dda5c437f0 1708 Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
mbed_official 133:d4dda5c437f0 1709 Init.BusWide = SDIO_BUS_WIDE_4B;
mbed_official 133:d4dda5c437f0 1710 Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
mbed_official 133:d4dda5c437f0 1711 Init.ClockDiv = SDIO_TRANSFER_CLK_DIV;
mbed_official 133:d4dda5c437f0 1712
mbed_official 133:d4dda5c437f0 1713 /* Configure SDIO peripheral interface */
mbed_official 133:d4dda5c437f0 1714 SDIO_Init(hsd->Instance, Init);
mbed_official 133:d4dda5c437f0 1715 }
mbed_official 133:d4dda5c437f0 1716 }
mbed_official 133:d4dda5c437f0 1717 else
mbed_official 133:d4dda5c437f0 1718 {
mbed_official 133:d4dda5c437f0 1719 errorState = SD_WideBus_Disable(hsd);
mbed_official 133:d4dda5c437f0 1720
mbed_official 133:d4dda5c437f0 1721 if (errorState == SD_OK)
mbed_official 133:d4dda5c437f0 1722 {
mbed_official 133:d4dda5c437f0 1723 /* Configure the SDIO peripheral */
mbed_official 133:d4dda5c437f0 1724 Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
mbed_official 133:d4dda5c437f0 1725 Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
mbed_official 133:d4dda5c437f0 1726 Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
mbed_official 133:d4dda5c437f0 1727 Init.BusWide = SDIO_BUS_WIDE_1B;
mbed_official 133:d4dda5c437f0 1728 Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
mbed_official 133:d4dda5c437f0 1729 Init.ClockDiv = SDIO_TRANSFER_CLK_DIV;
mbed_official 133:d4dda5c437f0 1730
mbed_official 133:d4dda5c437f0 1731 /* Configure SDIO peripheral interface */
mbed_official 133:d4dda5c437f0 1732 SDIO_Init(hsd->Instance, Init);
mbed_official 133:d4dda5c437f0 1733 }
mbed_official 133:d4dda5c437f0 1734 }
mbed_official 133:d4dda5c437f0 1735 }
mbed_official 133:d4dda5c437f0 1736
mbed_official 133:d4dda5c437f0 1737 return errorState;
mbed_official 133:d4dda5c437f0 1738 }
mbed_official 133:d4dda5c437f0 1739
mbed_official 133:d4dda5c437f0 1740 /**
mbed_official 133:d4dda5c437f0 1741 * @brief Aborts an ongoing data transfer.
mbed_official 133:d4dda5c437f0 1742 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 1743 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 1744 */
mbed_official 133:d4dda5c437f0 1745 HAL_SD_ErrorTypedef HAL_SD_StopTransfer(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 1746 {
mbed_official 133:d4dda5c437f0 1747 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
mbed_official 133:d4dda5c437f0 1748 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 1749
mbed_official 133:d4dda5c437f0 1750 /* Send CMD12 STOP_TRANSMISSION */
mbed_official 133:d4dda5c437f0 1751 SDIO_CmdInitStructure.Argument = 0;
mbed_official 133:d4dda5c437f0 1752 SDIO_CmdInitStructure.CmdIndex = SD_CMD_STOP_TRANSMISSION;
mbed_official 133:d4dda5c437f0 1753 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 1754 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 1755 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 1756 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 1757
mbed_official 133:d4dda5c437f0 1758 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 1759 errorState = SD_CmdResp1Error(hsd, SD_CMD_STOP_TRANSMISSION);
mbed_official 133:d4dda5c437f0 1760
mbed_official 133:d4dda5c437f0 1761 return errorState;
mbed_official 133:d4dda5c437f0 1762 }
mbed_official 133:d4dda5c437f0 1763
mbed_official 133:d4dda5c437f0 1764 /**
mbed_official 133:d4dda5c437f0 1765 * @brief Switches the SD card to High Speed mode.
mbed_official 133:d4dda5c437f0 1766 * This API must be used after "Transfer State"
mbed_official 133:d4dda5c437f0 1767 * @note This operation should be followed by the configuration
mbed_official 133:d4dda5c437f0 1768 * of PLL to have SDIOCK clock between 67 and 75 MHz
mbed_official 133:d4dda5c437f0 1769 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 1770 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 1771 */
mbed_official 133:d4dda5c437f0 1772 HAL_SD_ErrorTypedef HAL_SD_HighSpeed (SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 1773 {
mbed_official 133:d4dda5c437f0 1774 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 1775 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
mbed_official 133:d4dda5c437f0 1776 SDIO_DataInitTypeDef SDIO_DataInitStructure;
mbed_official 133:d4dda5c437f0 1777
mbed_official 133:d4dda5c437f0 1778 uint8_t SD_hs[64] = {0};
mbed_official 133:d4dda5c437f0 1779 uint32_t SD_scr[2] = {0, 0};
mbed_official 133:d4dda5c437f0 1780 uint32_t SD_SPEC = 0 ;
mbed_official 133:d4dda5c437f0 1781 uint32_t count = 0, *tempbuff = (uint32_t *)SD_hs;
mbed_official 133:d4dda5c437f0 1782
mbed_official 133:d4dda5c437f0 1783 /* Initialize the Data control register */
mbed_official 133:d4dda5c437f0 1784 hsd->Instance->DCTRL = 0;
mbed_official 133:d4dda5c437f0 1785
mbed_official 133:d4dda5c437f0 1786 /* Get SCR Register */
mbed_official 133:d4dda5c437f0 1787 errorState = SD_FindSCR(hsd, SD_scr);
mbed_official 133:d4dda5c437f0 1788
mbed_official 133:d4dda5c437f0 1789 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 1790 {
mbed_official 133:d4dda5c437f0 1791 return errorState;
mbed_official 133:d4dda5c437f0 1792 }
mbed_official 133:d4dda5c437f0 1793
mbed_official 133:d4dda5c437f0 1794 /* Test the Version supported by the card*/
mbed_official 133:d4dda5c437f0 1795 SD_SPEC = (SD_scr[1] & 0x01000000) | (SD_scr[1] & 0x02000000);
mbed_official 133:d4dda5c437f0 1796
mbed_official 133:d4dda5c437f0 1797 if (SD_SPEC != SD_ALLZERO)
mbed_official 133:d4dda5c437f0 1798 {
mbed_official 133:d4dda5c437f0 1799 /* Set Block Size for Card */
mbed_official 133:d4dda5c437f0 1800 SDIO_CmdInitStructure.Argument = (uint32_t)64;
mbed_official 133:d4dda5c437f0 1801 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
mbed_official 133:d4dda5c437f0 1802 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 1803 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 1804 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 1805 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 1806
mbed_official 133:d4dda5c437f0 1807 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 1808 errorState = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
mbed_official 133:d4dda5c437f0 1809
mbed_official 133:d4dda5c437f0 1810 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 1811 {
mbed_official 133:d4dda5c437f0 1812 return errorState;
mbed_official 133:d4dda5c437f0 1813 }
mbed_official 133:d4dda5c437f0 1814
mbed_official 133:d4dda5c437f0 1815 /* Configure the SD DPSM (Data Path State Machine) */
mbed_official 133:d4dda5c437f0 1816 SDIO_DataInitStructure.DataTimeOut = SD_DATATIMEOUT;
mbed_official 133:d4dda5c437f0 1817 SDIO_DataInitStructure.DataLength = 64;
mbed_official 133:d4dda5c437f0 1818 SDIO_DataInitStructure.DataBlockSize = SDIO_DATABLOCK_SIZE_64B ;
mbed_official 133:d4dda5c437f0 1819 SDIO_DataInitStructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
mbed_official 133:d4dda5c437f0 1820 SDIO_DataInitStructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
mbed_official 133:d4dda5c437f0 1821 SDIO_DataInitStructure.DPSM = SDIO_DPSM_ENABLE;
mbed_official 133:d4dda5c437f0 1822 SDIO_DataConfig(hsd->Instance, &SDIO_DataInitStructure);
mbed_official 133:d4dda5c437f0 1823
mbed_official 133:d4dda5c437f0 1824 /* Send CMD6 switch mode */
mbed_official 133:d4dda5c437f0 1825 SDIO_CmdInitStructure.Argument = 0x80FFFF01;
mbed_official 133:d4dda5c437f0 1826 SDIO_CmdInitStructure.CmdIndex = SD_CMD_HS_SWITCH;
mbed_official 133:d4dda5c437f0 1827 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 1828
mbed_official 133:d4dda5c437f0 1829 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 1830 errorState = SD_CmdResp1Error(hsd, SD_CMD_HS_SWITCH);
mbed_official 133:d4dda5c437f0 1831
mbed_official 133:d4dda5c437f0 1832 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 1833 {
mbed_official 133:d4dda5c437f0 1834 return errorState;
mbed_official 133:d4dda5c437f0 1835 }
mbed_official 133:d4dda5c437f0 1836
mbed_official 133:d4dda5c437f0 1837 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
mbed_official 133:d4dda5c437f0 1838 {
mbed_official 133:d4dda5c437f0 1839 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
mbed_official 133:d4dda5c437f0 1840 {
mbed_official 133:d4dda5c437f0 1841 for (count = 0; count < 8; count++)
mbed_official 133:d4dda5c437f0 1842 {
mbed_official 133:d4dda5c437f0 1843 *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance);
mbed_official 133:d4dda5c437f0 1844 }
mbed_official 133:d4dda5c437f0 1845
mbed_official 133:d4dda5c437f0 1846 tempbuff += 8;
mbed_official 133:d4dda5c437f0 1847 }
mbed_official 133:d4dda5c437f0 1848 }
mbed_official 133:d4dda5c437f0 1849
mbed_official 133:d4dda5c437f0 1850 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
mbed_official 133:d4dda5c437f0 1851 {
mbed_official 133:d4dda5c437f0 1852 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
mbed_official 133:d4dda5c437f0 1853
mbed_official 133:d4dda5c437f0 1854 errorState = SD_DATA_TIMEOUT;
mbed_official 133:d4dda5c437f0 1855
mbed_official 133:d4dda5c437f0 1856 return errorState;
mbed_official 133:d4dda5c437f0 1857 }
mbed_official 133:d4dda5c437f0 1858 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
mbed_official 133:d4dda5c437f0 1859 {
mbed_official 133:d4dda5c437f0 1860 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
mbed_official 133:d4dda5c437f0 1861
mbed_official 133:d4dda5c437f0 1862 errorState = SD_DATA_CRC_FAIL;
mbed_official 133:d4dda5c437f0 1863
mbed_official 133:d4dda5c437f0 1864 return errorState;
mbed_official 133:d4dda5c437f0 1865 }
mbed_official 133:d4dda5c437f0 1866 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
mbed_official 133:d4dda5c437f0 1867 {
mbed_official 133:d4dda5c437f0 1868 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
mbed_official 133:d4dda5c437f0 1869
mbed_official 133:d4dda5c437f0 1870 errorState = SD_RX_OVERRUN;
mbed_official 133:d4dda5c437f0 1871
mbed_official 133:d4dda5c437f0 1872 return errorState;
mbed_official 133:d4dda5c437f0 1873 }
mbed_official 133:d4dda5c437f0 1874 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
mbed_official 133:d4dda5c437f0 1875 {
mbed_official 133:d4dda5c437f0 1876 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
mbed_official 133:d4dda5c437f0 1877
mbed_official 133:d4dda5c437f0 1878 errorState = SD_START_BIT_ERR;
mbed_official 133:d4dda5c437f0 1879
mbed_official 133:d4dda5c437f0 1880 return errorState;
mbed_official 133:d4dda5c437f0 1881 }
mbed_official 133:d4dda5c437f0 1882
mbed_official 133:d4dda5c437f0 1883 count = SD_DATATIMEOUT;
mbed_official 133:d4dda5c437f0 1884
mbed_official 133:d4dda5c437f0 1885 while ((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (count > 0))
mbed_official 133:d4dda5c437f0 1886 {
mbed_official 133:d4dda5c437f0 1887 *tempbuff = SDIO_ReadFIFO(hsd->Instance);
mbed_official 133:d4dda5c437f0 1888 tempbuff++;
mbed_official 133:d4dda5c437f0 1889 count--;
mbed_official 133:d4dda5c437f0 1890 }
mbed_official 133:d4dda5c437f0 1891
mbed_official 133:d4dda5c437f0 1892 /* Clear all the static flags */
mbed_official 133:d4dda5c437f0 1893 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
mbed_official 133:d4dda5c437f0 1894
mbed_official 133:d4dda5c437f0 1895 /* Test if the switch mode HS is ok */
mbed_official 133:d4dda5c437f0 1896 if ((SD_hs[13]& 2) != 2)
mbed_official 133:d4dda5c437f0 1897 {
mbed_official 133:d4dda5c437f0 1898 errorState = SD_UNSUPPORTED_FEATURE;
mbed_official 133:d4dda5c437f0 1899 }
mbed_official 133:d4dda5c437f0 1900 }
mbed_official 133:d4dda5c437f0 1901
mbed_official 133:d4dda5c437f0 1902 return errorState;
mbed_official 133:d4dda5c437f0 1903 }
mbed_official 133:d4dda5c437f0 1904
mbed_official 133:d4dda5c437f0 1905 /**
mbed_official 133:d4dda5c437f0 1906 * @}
mbed_official 133:d4dda5c437f0 1907 */
mbed_official 133:d4dda5c437f0 1908
mbed_official 133:d4dda5c437f0 1909 /** @defgroup SD_Group4 Peripheral State functions
mbed_official 133:d4dda5c437f0 1910 * @brief Peripheral State functions
mbed_official 133:d4dda5c437f0 1911 *
mbed_official 133:d4dda5c437f0 1912 @verbatim
mbed_official 133:d4dda5c437f0 1913 ==============================================================================
mbed_official 133:d4dda5c437f0 1914 ##### Peripheral State functions #####
mbed_official 133:d4dda5c437f0 1915 ==============================================================================
mbed_official 133:d4dda5c437f0 1916 [..]
mbed_official 133:d4dda5c437f0 1917 This subsection permits to get in runtime the status of the peripheral
mbed_official 133:d4dda5c437f0 1918 and the data flow.
mbed_official 133:d4dda5c437f0 1919
mbed_official 133:d4dda5c437f0 1920 @endverbatim
mbed_official 133:d4dda5c437f0 1921 * @{
mbed_official 133:d4dda5c437f0 1922 */
mbed_official 133:d4dda5c437f0 1923
mbed_official 133:d4dda5c437f0 1924 /**
mbed_official 133:d4dda5c437f0 1925 * @brief Returns the current SD card's status.
mbed_official 133:d4dda5c437f0 1926 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 1927 * @param pSDstatus: Pointer to the buffer that will contain the SD card status
mbed_official 133:d4dda5c437f0 1928 * SD Status register)
mbed_official 133:d4dda5c437f0 1929 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 1930 */
mbed_official 133:d4dda5c437f0 1931 HAL_SD_ErrorTypedef HAL_SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
mbed_official 133:d4dda5c437f0 1932 {
mbed_official 133:d4dda5c437f0 1933 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
mbed_official 133:d4dda5c437f0 1934 SDIO_DataInitTypeDef SDIO_DataInitStructure;
mbed_official 133:d4dda5c437f0 1935 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 1936 uint32_t count = 0;
mbed_official 133:d4dda5c437f0 1937
mbed_official 133:d4dda5c437f0 1938 /* Check SD response */
mbed_official 133:d4dda5c437f0 1939 if ((SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
mbed_official 133:d4dda5c437f0 1940 {
mbed_official 133:d4dda5c437f0 1941 errorState = SD_LOCK_UNLOCK_FAILED;
mbed_official 133:d4dda5c437f0 1942
mbed_official 133:d4dda5c437f0 1943 return errorState;
mbed_official 133:d4dda5c437f0 1944 }
mbed_official 133:d4dda5c437f0 1945
mbed_official 133:d4dda5c437f0 1946 /* Set block size for card if it is not equal to current block size for card */
mbed_official 133:d4dda5c437f0 1947 SDIO_CmdInitStructure.Argument = 64;
mbed_official 133:d4dda5c437f0 1948 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
mbed_official 133:d4dda5c437f0 1949 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 1950 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 1951 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 1952 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 1953
mbed_official 133:d4dda5c437f0 1954 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 1955 errorState = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
mbed_official 133:d4dda5c437f0 1956
mbed_official 133:d4dda5c437f0 1957 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 1958 {
mbed_official 133:d4dda5c437f0 1959 return errorState;
mbed_official 133:d4dda5c437f0 1960 }
mbed_official 133:d4dda5c437f0 1961
mbed_official 133:d4dda5c437f0 1962 /* Send CMD55 */
mbed_official 133:d4dda5c437f0 1963 SDIO_CmdInitStructure.Argument = (uint32_t)(hsd->RCA << 16);
mbed_official 133:d4dda5c437f0 1964 SDIO_CmdInitStructure.CmdIndex = SD_CMD_APP_CMD;
mbed_official 133:d4dda5c437f0 1965 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 1966
mbed_official 133:d4dda5c437f0 1967 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 1968 errorState = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
mbed_official 133:d4dda5c437f0 1969
mbed_official 133:d4dda5c437f0 1970 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 1971 {
mbed_official 133:d4dda5c437f0 1972 return errorState;
mbed_official 133:d4dda5c437f0 1973 }
mbed_official 133:d4dda5c437f0 1974
mbed_official 133:d4dda5c437f0 1975 /* Configure the SD DPSM (Data Path State Machine) */
mbed_official 133:d4dda5c437f0 1976 SDIO_DataInitStructure.DataTimeOut = SD_DATATIMEOUT;
mbed_official 133:d4dda5c437f0 1977 SDIO_DataInitStructure.DataLength = 64;
mbed_official 133:d4dda5c437f0 1978 SDIO_DataInitStructure.DataBlockSize = SDIO_DATABLOCK_SIZE_64B;
mbed_official 133:d4dda5c437f0 1979 SDIO_DataInitStructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
mbed_official 133:d4dda5c437f0 1980 SDIO_DataInitStructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
mbed_official 133:d4dda5c437f0 1981 SDIO_DataInitStructure.DPSM = SDIO_DPSM_ENABLE;
mbed_official 133:d4dda5c437f0 1982 SDIO_DataConfig(hsd->Instance, &SDIO_DataInitStructure);
mbed_official 133:d4dda5c437f0 1983
mbed_official 133:d4dda5c437f0 1984 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
mbed_official 133:d4dda5c437f0 1985 SDIO_CmdInitStructure.Argument = 0;
mbed_official 133:d4dda5c437f0 1986 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SD_APP_STAUS;
mbed_official 133:d4dda5c437f0 1987 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 1988
mbed_official 133:d4dda5c437f0 1989 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 1990 errorState = SD_CmdResp1Error(hsd, SD_CMD_SD_APP_STAUS);
mbed_official 133:d4dda5c437f0 1991
mbed_official 133:d4dda5c437f0 1992 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 1993 {
mbed_official 133:d4dda5c437f0 1994 return errorState;
mbed_official 133:d4dda5c437f0 1995 }
mbed_official 133:d4dda5c437f0 1996
mbed_official 133:d4dda5c437f0 1997 /* Get status data */
mbed_official 133:d4dda5c437f0 1998 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
mbed_official 133:d4dda5c437f0 1999 {
mbed_official 133:d4dda5c437f0 2000 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
mbed_official 133:d4dda5c437f0 2001 {
mbed_official 133:d4dda5c437f0 2002 for (count = 0; count < 8; count++)
mbed_official 133:d4dda5c437f0 2003 {
mbed_official 133:d4dda5c437f0 2004 *(pSDstatus + count) = SDIO_ReadFIFO(hsd->Instance);
mbed_official 133:d4dda5c437f0 2005 }
mbed_official 133:d4dda5c437f0 2006
mbed_official 133:d4dda5c437f0 2007 pSDstatus += 8;
mbed_official 133:d4dda5c437f0 2008 }
mbed_official 133:d4dda5c437f0 2009 }
mbed_official 133:d4dda5c437f0 2010
mbed_official 133:d4dda5c437f0 2011 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
mbed_official 133:d4dda5c437f0 2012 {
mbed_official 133:d4dda5c437f0 2013 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
mbed_official 133:d4dda5c437f0 2014
mbed_official 133:d4dda5c437f0 2015 errorState = SD_DATA_TIMEOUT;
mbed_official 133:d4dda5c437f0 2016
mbed_official 133:d4dda5c437f0 2017 return errorState;
mbed_official 133:d4dda5c437f0 2018 }
mbed_official 133:d4dda5c437f0 2019 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
mbed_official 133:d4dda5c437f0 2020 {
mbed_official 133:d4dda5c437f0 2021 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
mbed_official 133:d4dda5c437f0 2022
mbed_official 133:d4dda5c437f0 2023 errorState = SD_DATA_CRC_FAIL;
mbed_official 133:d4dda5c437f0 2024
mbed_official 133:d4dda5c437f0 2025 return errorState;
mbed_official 133:d4dda5c437f0 2026 }
mbed_official 133:d4dda5c437f0 2027 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
mbed_official 133:d4dda5c437f0 2028 {
mbed_official 133:d4dda5c437f0 2029 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
mbed_official 133:d4dda5c437f0 2030
mbed_official 133:d4dda5c437f0 2031 errorState = SD_RX_OVERRUN;
mbed_official 133:d4dda5c437f0 2032
mbed_official 133:d4dda5c437f0 2033 return errorState;
mbed_official 133:d4dda5c437f0 2034 }
mbed_official 133:d4dda5c437f0 2035 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
mbed_official 133:d4dda5c437f0 2036 {
mbed_official 133:d4dda5c437f0 2037 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
mbed_official 133:d4dda5c437f0 2038
mbed_official 133:d4dda5c437f0 2039 errorState = SD_START_BIT_ERR;
mbed_official 133:d4dda5c437f0 2040
mbed_official 133:d4dda5c437f0 2041 return errorState;
mbed_official 133:d4dda5c437f0 2042 }
mbed_official 133:d4dda5c437f0 2043
mbed_official 133:d4dda5c437f0 2044 count = SD_DATATIMEOUT;
mbed_official 133:d4dda5c437f0 2045 while ((__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (count > 0))
mbed_official 133:d4dda5c437f0 2046 {
mbed_official 133:d4dda5c437f0 2047 *pSDstatus = SDIO_ReadFIFO(hsd->Instance);
mbed_official 133:d4dda5c437f0 2048 pSDstatus++;
mbed_official 133:d4dda5c437f0 2049 count--;
mbed_official 133:d4dda5c437f0 2050 }
mbed_official 133:d4dda5c437f0 2051
mbed_official 133:d4dda5c437f0 2052 /* Clear all the static status flags*/
mbed_official 133:d4dda5c437f0 2053 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
mbed_official 133:d4dda5c437f0 2054
mbed_official 133:d4dda5c437f0 2055 return errorState;
mbed_official 133:d4dda5c437f0 2056 }
mbed_official 133:d4dda5c437f0 2057
mbed_official 133:d4dda5c437f0 2058 /**
mbed_official 133:d4dda5c437f0 2059 * @brief Gets the current sd card data status.
mbed_official 133:d4dda5c437f0 2060 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 2061 * @retval Data Transfer state
mbed_official 133:d4dda5c437f0 2062 */
mbed_official 133:d4dda5c437f0 2063 HAL_SD_TransferStateTypedef HAL_SD_GetStatus(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 2064 {
mbed_official 133:d4dda5c437f0 2065 HAL_SD_CardStateTypedef cardstate = SD_CARD_TRANSFER;
mbed_official 133:d4dda5c437f0 2066
mbed_official 133:d4dda5c437f0 2067 /* Get SD card state */
mbed_official 133:d4dda5c437f0 2068 cardstate = SD_GetState(hsd);
mbed_official 133:d4dda5c437f0 2069
mbed_official 133:d4dda5c437f0 2070 /* Find SD status according to card state*/
mbed_official 133:d4dda5c437f0 2071 if (cardstate == SD_CARD_TRANSFER)
mbed_official 133:d4dda5c437f0 2072 {
mbed_official 133:d4dda5c437f0 2073 return SD_TRANSFER_OK;
mbed_official 133:d4dda5c437f0 2074 }
mbed_official 133:d4dda5c437f0 2075 else if(cardstate == SD_CARD_ERROR)
mbed_official 133:d4dda5c437f0 2076 {
mbed_official 133:d4dda5c437f0 2077 return SD_TRANSFER_ERROR;
mbed_official 133:d4dda5c437f0 2078 }
mbed_official 133:d4dda5c437f0 2079 else
mbed_official 133:d4dda5c437f0 2080 {
mbed_official 133:d4dda5c437f0 2081 return SD_TRANSFER_BUSY;
mbed_official 133:d4dda5c437f0 2082 }
mbed_official 133:d4dda5c437f0 2083 }
mbed_official 133:d4dda5c437f0 2084
mbed_official 133:d4dda5c437f0 2085 /**
mbed_official 133:d4dda5c437f0 2086 * @brief Gets the SD card status.
mbed_official 133:d4dda5c437f0 2087 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 2088 * @param pCardStatus: Pointer to the HAL_SD_CardStatusTypedef structure that
mbed_official 133:d4dda5c437f0 2089 * will contain the SD card status information
mbed_official 133:d4dda5c437f0 2090 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 2091 */
mbed_official 133:d4dda5c437f0 2092 HAL_SD_ErrorTypedef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypedef *pCardStatus)
mbed_official 133:d4dda5c437f0 2093 {
mbed_official 133:d4dda5c437f0 2094 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 2095 uint32_t tmp = 0;
mbed_official 133:d4dda5c437f0 2096 uint32_t SD_STATUS[16];
mbed_official 133:d4dda5c437f0 2097
mbed_official 133:d4dda5c437f0 2098 errorState = HAL_SD_SendSDStatus(hsd, SD_STATUS);
mbed_official 133:d4dda5c437f0 2099
mbed_official 133:d4dda5c437f0 2100 if (errorState != SD_OK)
mbed_official 133:d4dda5c437f0 2101 {
mbed_official 133:d4dda5c437f0 2102 return errorState;
mbed_official 133:d4dda5c437f0 2103 }
mbed_official 133:d4dda5c437f0 2104
mbed_official 133:d4dda5c437f0 2105 /* Byte 0 */
mbed_official 133:d4dda5c437f0 2106 tmp = (SD_STATUS[0] & 0xC0) >> 6;
mbed_official 133:d4dda5c437f0 2107 pCardStatus->DAT_BUS_WIDTH = (uint8_t)tmp;
mbed_official 133:d4dda5c437f0 2108
mbed_official 133:d4dda5c437f0 2109 /* Byte 0 */
mbed_official 133:d4dda5c437f0 2110 tmp = (SD_STATUS[0] & 0x20) >> 5;
mbed_official 133:d4dda5c437f0 2111 pCardStatus->SECURED_MODE = (uint8_t)tmp;
mbed_official 133:d4dda5c437f0 2112
mbed_official 133:d4dda5c437f0 2113 /* Byte 2 */
mbed_official 133:d4dda5c437f0 2114 tmp = (SD_STATUS[2] & 0xFF);
mbed_official 133:d4dda5c437f0 2115 pCardStatus->SD_CARD_TYPE = (uint8_t)(tmp << 8);
mbed_official 133:d4dda5c437f0 2116
mbed_official 133:d4dda5c437f0 2117 /* Byte 3 */
mbed_official 133:d4dda5c437f0 2118 tmp = (SD_STATUS[3] & 0xFF);
mbed_official 133:d4dda5c437f0 2119 pCardStatus->SD_CARD_TYPE |= (uint8_t)tmp;
mbed_official 133:d4dda5c437f0 2120
mbed_official 133:d4dda5c437f0 2121 /* Byte 4 */
mbed_official 133:d4dda5c437f0 2122 tmp = (SD_STATUS[4] & 0xFF);
mbed_official 133:d4dda5c437f0 2123 pCardStatus->SIZE_OF_PROTECTED_AREA = (uint8_t)(tmp << 24);
mbed_official 133:d4dda5c437f0 2124
mbed_official 133:d4dda5c437f0 2125 /* Byte 5 */
mbed_official 133:d4dda5c437f0 2126 tmp = (SD_STATUS[5] & 0xFF);
mbed_official 133:d4dda5c437f0 2127 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)(tmp << 16);
mbed_official 133:d4dda5c437f0 2128
mbed_official 133:d4dda5c437f0 2129 /* Byte 6 */
mbed_official 133:d4dda5c437f0 2130 tmp = (SD_STATUS[6] & 0xFF);
mbed_official 133:d4dda5c437f0 2131 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)(tmp << 8);
mbed_official 133:d4dda5c437f0 2132
mbed_official 133:d4dda5c437f0 2133 /* Byte 7 */
mbed_official 133:d4dda5c437f0 2134 tmp = (SD_STATUS[7] & 0xFF);
mbed_official 133:d4dda5c437f0 2135 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)tmp;
mbed_official 133:d4dda5c437f0 2136
mbed_official 133:d4dda5c437f0 2137 /* Byte 8 */
mbed_official 133:d4dda5c437f0 2138 tmp = (SD_STATUS[8] & 0xFF);
mbed_official 133:d4dda5c437f0 2139 pCardStatus->SPEED_CLASS = (uint8_t)tmp;
mbed_official 133:d4dda5c437f0 2140
mbed_official 133:d4dda5c437f0 2141 /* Byte 9 */
mbed_official 133:d4dda5c437f0 2142 tmp = (SD_STATUS[9] & 0xFF);
mbed_official 133:d4dda5c437f0 2143 pCardStatus->PERFORMANCE_MOVE = (uint8_t)tmp;
mbed_official 133:d4dda5c437f0 2144
mbed_official 133:d4dda5c437f0 2145 /* Byte 10 */
mbed_official 133:d4dda5c437f0 2146 tmp = (SD_STATUS[10] & 0xF0) >> 4;
mbed_official 133:d4dda5c437f0 2147 pCardStatus->AU_SIZE = (uint8_t)tmp;
mbed_official 133:d4dda5c437f0 2148
mbed_official 133:d4dda5c437f0 2149 /* Byte 11 */
mbed_official 133:d4dda5c437f0 2150 tmp = (SD_STATUS[11] & 0xFF);
mbed_official 133:d4dda5c437f0 2151 pCardStatus->ERASE_SIZE = (uint8_t)(tmp << 8);
mbed_official 133:d4dda5c437f0 2152
mbed_official 133:d4dda5c437f0 2153 /* Byte 12 */
mbed_official 133:d4dda5c437f0 2154 tmp = (SD_STATUS[12] & 0xFF);
mbed_official 133:d4dda5c437f0 2155 pCardStatus->ERASE_SIZE |= (uint8_t)tmp;
mbed_official 133:d4dda5c437f0 2156
mbed_official 133:d4dda5c437f0 2157 /* Byte 13 */
mbed_official 133:d4dda5c437f0 2158 tmp = (SD_STATUS[13] & 0xFC) >> 2;
mbed_official 133:d4dda5c437f0 2159 pCardStatus->ERASE_TIMEOUT = (uint8_t)tmp;
mbed_official 133:d4dda5c437f0 2160
mbed_official 133:d4dda5c437f0 2161 /* Byte 13 */
mbed_official 133:d4dda5c437f0 2162 tmp = (SD_STATUS[13] & 0x3);
mbed_official 133:d4dda5c437f0 2163 pCardStatus->ERASE_OFFSET = (uint8_t)tmp;
mbed_official 133:d4dda5c437f0 2164
mbed_official 133:d4dda5c437f0 2165 return errorState;
mbed_official 133:d4dda5c437f0 2166 }
mbed_official 133:d4dda5c437f0 2167
mbed_official 133:d4dda5c437f0 2168 /**
mbed_official 133:d4dda5c437f0 2169 * @}
mbed_official 133:d4dda5c437f0 2170 */
mbed_official 133:d4dda5c437f0 2171
mbed_official 133:d4dda5c437f0 2172 /**
mbed_official 133:d4dda5c437f0 2173 * @brief SD DMA transfer complete Rx callback.
mbed_official 242:7074e42da0b2 2174 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
mbed_official 242:7074e42da0b2 2175 * the configuration information for the specified DMA module.
mbed_official 133:d4dda5c437f0 2176 * @retval None
mbed_official 133:d4dda5c437f0 2177 */
mbed_official 133:d4dda5c437f0 2178 static void SD_DMA_RxCplt(DMA_HandleTypeDef *hdma)
mbed_official 133:d4dda5c437f0 2179 {
mbed_official 133:d4dda5c437f0 2180 SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
mbed_official 133:d4dda5c437f0 2181
mbed_official 133:d4dda5c437f0 2182 /* DMA transfer is complete */
mbed_official 133:d4dda5c437f0 2183 hsd->DmaTransferCplt = 1;
mbed_official 133:d4dda5c437f0 2184
mbed_official 133:d4dda5c437f0 2185 /* Wait until SD transfer is complete */
mbed_official 133:d4dda5c437f0 2186 while(hsd->SdTransferCplt == 0)
mbed_official 133:d4dda5c437f0 2187 {
mbed_official 133:d4dda5c437f0 2188 }
mbed_official 133:d4dda5c437f0 2189
mbed_official 133:d4dda5c437f0 2190 /* Transfer complete user callback */
mbed_official 133:d4dda5c437f0 2191 HAL_SD_DMA_RxCpltCallback(hsd->hdmarx);
mbed_official 133:d4dda5c437f0 2192 }
mbed_official 133:d4dda5c437f0 2193
mbed_official 133:d4dda5c437f0 2194 /**
mbed_official 133:d4dda5c437f0 2195 * @brief SD DMA transfer Error Rx callback.
mbed_official 242:7074e42da0b2 2196 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
mbed_official 242:7074e42da0b2 2197 * the configuration information for the specified DMA module.
mbed_official 133:d4dda5c437f0 2198 * @retval None
mbed_official 133:d4dda5c437f0 2199 */
mbed_official 133:d4dda5c437f0 2200 static void SD_DMA_RxError(DMA_HandleTypeDef *hdma)
mbed_official 133:d4dda5c437f0 2201 {
mbed_official 133:d4dda5c437f0 2202 SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
mbed_official 133:d4dda5c437f0 2203
mbed_official 133:d4dda5c437f0 2204 /* Transfer complete user callback */
mbed_official 133:d4dda5c437f0 2205 HAL_SD_DMA_RxErrorCallback(hsd->hdmarx);
mbed_official 133:d4dda5c437f0 2206 }
mbed_official 133:d4dda5c437f0 2207
mbed_official 133:d4dda5c437f0 2208 /**
mbed_official 133:d4dda5c437f0 2209 * @brief SD DMA transfer complete Tx callback.
mbed_official 242:7074e42da0b2 2210 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
mbed_official 242:7074e42da0b2 2211 * the configuration information for the specified DMA module.
mbed_official 133:d4dda5c437f0 2212 * @retval None
mbed_official 133:d4dda5c437f0 2213 */
mbed_official 133:d4dda5c437f0 2214 static void SD_DMA_TxCplt(DMA_HandleTypeDef *hdma)
mbed_official 133:d4dda5c437f0 2215 {
mbed_official 133:d4dda5c437f0 2216 SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
mbed_official 133:d4dda5c437f0 2217
mbed_official 133:d4dda5c437f0 2218 /* DMA transfer is complete */
mbed_official 133:d4dda5c437f0 2219 hsd->DmaTransferCplt = 1;
mbed_official 133:d4dda5c437f0 2220
mbed_official 133:d4dda5c437f0 2221 /* Wait until SD transfer is complete */
mbed_official 133:d4dda5c437f0 2222 while(hsd->SdTransferCplt == 0)
mbed_official 133:d4dda5c437f0 2223 {
mbed_official 133:d4dda5c437f0 2224 }
mbed_official 133:d4dda5c437f0 2225
mbed_official 133:d4dda5c437f0 2226 /* Transfer complete user callback */
mbed_official 133:d4dda5c437f0 2227 HAL_SD_DMA_TxCpltCallback(hsd->hdmatx);
mbed_official 133:d4dda5c437f0 2228 }
mbed_official 133:d4dda5c437f0 2229
mbed_official 133:d4dda5c437f0 2230 /**
mbed_official 133:d4dda5c437f0 2231 * @brief SD DMA transfer Error Tx callback.
mbed_official 242:7074e42da0b2 2232 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
mbed_official 242:7074e42da0b2 2233 * the configuration information for the specified DMA module.
mbed_official 133:d4dda5c437f0 2234 * @retval None
mbed_official 133:d4dda5c437f0 2235 */
mbed_official 133:d4dda5c437f0 2236 static void SD_DMA_TxError(DMA_HandleTypeDef *hdma)
mbed_official 133:d4dda5c437f0 2237 {
mbed_official 133:d4dda5c437f0 2238 SD_HandleTypeDef *hsd = ( SD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
mbed_official 133:d4dda5c437f0 2239
mbed_official 133:d4dda5c437f0 2240 /* Transfer complete user callback */
mbed_official 133:d4dda5c437f0 2241 HAL_SD_DMA_TxErrorCallback(hsd->hdmatx);
mbed_official 133:d4dda5c437f0 2242 }
mbed_official 133:d4dda5c437f0 2243
mbed_official 133:d4dda5c437f0 2244 /**
mbed_official 133:d4dda5c437f0 2245 * @brief Returns the SD current state.
mbed_official 133:d4dda5c437f0 2246 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 2247 * @retval SD card current state
mbed_official 133:d4dda5c437f0 2248 */
mbed_official 133:d4dda5c437f0 2249 static HAL_SD_CardStateTypedef SD_GetState(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 2250 {
mbed_official 133:d4dda5c437f0 2251 uint32_t resp1 = 0;
mbed_official 133:d4dda5c437f0 2252
mbed_official 133:d4dda5c437f0 2253 if (SD_SendStatus(hsd, &resp1) != SD_OK)
mbed_official 133:d4dda5c437f0 2254 {
mbed_official 133:d4dda5c437f0 2255 return SD_CARD_ERROR;
mbed_official 133:d4dda5c437f0 2256 }
mbed_official 133:d4dda5c437f0 2257 else
mbed_official 133:d4dda5c437f0 2258 {
mbed_official 133:d4dda5c437f0 2259 return (HAL_SD_CardStateTypedef)((resp1 >> 9) & 0x0F);
mbed_official 133:d4dda5c437f0 2260 }
mbed_official 133:d4dda5c437f0 2261 }
mbed_official 133:d4dda5c437f0 2262
mbed_official 133:d4dda5c437f0 2263 /**
mbed_official 133:d4dda5c437f0 2264 * @brief Initializes all cards or single card as the case may be Card(s) come
mbed_official 133:d4dda5c437f0 2265 * into standby state.
mbed_official 133:d4dda5c437f0 2266 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 2267 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 2268 */
mbed_official 133:d4dda5c437f0 2269 static HAL_SD_ErrorTypedef SD_Initialize_Cards(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 2270 {
mbed_official 133:d4dda5c437f0 2271 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
mbed_official 133:d4dda5c437f0 2272 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 2273 uint16_t sd_rca = 1;
mbed_official 133:d4dda5c437f0 2274
mbed_official 133:d4dda5c437f0 2275 if(SDIO_GetPowerState(hsd->Instance) == 0) /* Power off */
mbed_official 133:d4dda5c437f0 2276 {
mbed_official 133:d4dda5c437f0 2277 errorState = SD_REQUEST_NOT_APPLICABLE;
mbed_official 133:d4dda5c437f0 2278
mbed_official 133:d4dda5c437f0 2279 return errorState;
mbed_official 133:d4dda5c437f0 2280 }
mbed_official 133:d4dda5c437f0 2281
mbed_official 133:d4dda5c437f0 2282 if(hsd->CardType != SECURE_DIGITAL_IO_CARD)
mbed_official 133:d4dda5c437f0 2283 {
mbed_official 133:d4dda5c437f0 2284 /* Send CMD2 ALL_SEND_CID */
mbed_official 133:d4dda5c437f0 2285 SDIO_CmdInitStructure.Argument = 0;
mbed_official 133:d4dda5c437f0 2286 SDIO_CmdInitStructure.CmdIndex = SD_CMD_ALL_SEND_CID;
mbed_official 133:d4dda5c437f0 2287 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_LONG;
mbed_official 133:d4dda5c437f0 2288 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 2289 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 2290 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 2291
mbed_official 133:d4dda5c437f0 2292 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 2293 errorState = SD_CmdResp2Error(hsd);
mbed_official 133:d4dda5c437f0 2294
mbed_official 133:d4dda5c437f0 2295 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 2296 {
mbed_official 133:d4dda5c437f0 2297 return errorState;
mbed_official 133:d4dda5c437f0 2298 }
mbed_official 133:d4dda5c437f0 2299
mbed_official 133:d4dda5c437f0 2300 /* Get Card identification number data */
mbed_official 133:d4dda5c437f0 2301 hsd->CID[0] = SDIO_GetResponse(SDIO_RESP1);
mbed_official 133:d4dda5c437f0 2302 hsd->CID[1] = SDIO_GetResponse(SDIO_RESP2);
mbed_official 133:d4dda5c437f0 2303 hsd->CID[2] = SDIO_GetResponse(SDIO_RESP3);
mbed_official 133:d4dda5c437f0 2304 hsd->CID[3] = SDIO_GetResponse(SDIO_RESP4);
mbed_official 133:d4dda5c437f0 2305 }
mbed_official 133:d4dda5c437f0 2306
mbed_official 133:d4dda5c437f0 2307 if((hsd->CardType == STD_CAPACITY_SD_CARD_V1_1) || (hsd->CardType == STD_CAPACITY_SD_CARD_V2_0) ||\
mbed_official 133:d4dda5c437f0 2308 (hsd->CardType == SECURE_DIGITAL_IO_COMBO_CARD) || (hsd->CardType == HIGH_CAPACITY_SD_CARD))
mbed_official 133:d4dda5c437f0 2309 {
mbed_official 133:d4dda5c437f0 2310 /* Send CMD3 SET_REL_ADDR with argument 0 */
mbed_official 133:d4dda5c437f0 2311 /* SD Card publishes its RCA. */
mbed_official 133:d4dda5c437f0 2312 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SET_REL_ADDR;
mbed_official 133:d4dda5c437f0 2313 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 2314 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 2315
mbed_official 133:d4dda5c437f0 2316 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 2317 errorState = SD_CmdResp6Error(hsd, SD_CMD_SET_REL_ADDR, &sd_rca);
mbed_official 133:d4dda5c437f0 2318
mbed_official 133:d4dda5c437f0 2319 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 2320 {
mbed_official 133:d4dda5c437f0 2321 return errorState;
mbed_official 133:d4dda5c437f0 2322 }
mbed_official 133:d4dda5c437f0 2323 }
mbed_official 133:d4dda5c437f0 2324
mbed_official 133:d4dda5c437f0 2325 if (hsd->CardType != SECURE_DIGITAL_IO_CARD)
mbed_official 133:d4dda5c437f0 2326 {
mbed_official 133:d4dda5c437f0 2327 /* Get the SD card RCA */
mbed_official 133:d4dda5c437f0 2328 hsd->RCA = sd_rca;
mbed_official 133:d4dda5c437f0 2329
mbed_official 133:d4dda5c437f0 2330 /* Send CMD9 SEND_CSD with argument as card's RCA */
mbed_official 133:d4dda5c437f0 2331 SDIO_CmdInitStructure.Argument = (uint32_t)(hsd->RCA << 16);
mbed_official 133:d4dda5c437f0 2332 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SEND_CSD;
mbed_official 133:d4dda5c437f0 2333 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_LONG;
mbed_official 133:d4dda5c437f0 2334 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 2335
mbed_official 133:d4dda5c437f0 2336 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 2337 errorState = SD_CmdResp2Error(hsd);
mbed_official 133:d4dda5c437f0 2338
mbed_official 133:d4dda5c437f0 2339 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 2340 {
mbed_official 133:d4dda5c437f0 2341 return errorState;
mbed_official 133:d4dda5c437f0 2342 }
mbed_official 133:d4dda5c437f0 2343
mbed_official 133:d4dda5c437f0 2344 /* Get Card Specific Data */
mbed_official 133:d4dda5c437f0 2345 hsd->CSD[0] = SDIO_GetResponse(SDIO_RESP1);
mbed_official 133:d4dda5c437f0 2346 hsd->CSD[1] = SDIO_GetResponse(SDIO_RESP2);
mbed_official 133:d4dda5c437f0 2347 hsd->CSD[2] = SDIO_GetResponse(SDIO_RESP3);
mbed_official 133:d4dda5c437f0 2348 hsd->CSD[3] = SDIO_GetResponse(SDIO_RESP4);
mbed_official 133:d4dda5c437f0 2349 }
mbed_official 133:d4dda5c437f0 2350
mbed_official 133:d4dda5c437f0 2351 /* All cards are initialized */
mbed_official 133:d4dda5c437f0 2352 return errorState;
mbed_official 133:d4dda5c437f0 2353 }
mbed_official 133:d4dda5c437f0 2354
mbed_official 133:d4dda5c437f0 2355 /**
mbed_official 133:d4dda5c437f0 2356 * @brief Selects od Deselects the corresponding card.
mbed_official 133:d4dda5c437f0 2357 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 2358 * @param addr: Address of the card to be selected
mbed_official 133:d4dda5c437f0 2359 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 2360 */
mbed_official 133:d4dda5c437f0 2361 static HAL_SD_ErrorTypedef SD_Select_Deselect(SD_HandleTypeDef *hsd, uint64_t addr)
mbed_official 133:d4dda5c437f0 2362 {
mbed_official 133:d4dda5c437f0 2363 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
mbed_official 133:d4dda5c437f0 2364 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 2365
mbed_official 133:d4dda5c437f0 2366 /* Send CMD7 SDIO_SEL_DESEL_CARD */
mbed_official 133:d4dda5c437f0 2367 SDIO_CmdInitStructure.Argument = (uint32_t)addr;
mbed_official 133:d4dda5c437f0 2368 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SEL_DESEL_CARD;
mbed_official 133:d4dda5c437f0 2369 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 2370 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 2371 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 2372 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 2373
mbed_official 133:d4dda5c437f0 2374 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 2375 errorState = SD_CmdResp1Error(hsd, SD_CMD_SEL_DESEL_CARD);
mbed_official 133:d4dda5c437f0 2376
mbed_official 133:d4dda5c437f0 2377 return errorState;
mbed_official 133:d4dda5c437f0 2378 }
mbed_official 133:d4dda5c437f0 2379
mbed_official 133:d4dda5c437f0 2380 /**
mbed_official 133:d4dda5c437f0 2381 * @brief Enquires cards about their operating voltage and configures clock
mbed_official 133:d4dda5c437f0 2382 * controls and stores SD information that will be needed in future
mbed_official 133:d4dda5c437f0 2383 * in the SD handle.
mbed_official 133:d4dda5c437f0 2384 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 2385 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 2386 */
mbed_official 133:d4dda5c437f0 2387 static HAL_SD_ErrorTypedef SD_PowerON(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 2388 {
mbed_official 133:d4dda5c437f0 2389 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
mbed_official 133:d4dda5c437f0 2390 __IO HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 2391 uint32_t response = 0, count = 0, validvoltage = 0;
mbed_official 133:d4dda5c437f0 2392 uint32_t SDType = SD_STD_CAPACITY;
mbed_official 133:d4dda5c437f0 2393
mbed_official 133:d4dda5c437f0 2394 /* Power ON Sequence -------------------------------------------------------*/
mbed_official 133:d4dda5c437f0 2395 /* Disable SDIO Clock */
mbed_official 133:d4dda5c437f0 2396 __HAL_SD_SDIO_DISABLE();
mbed_official 133:d4dda5c437f0 2397
mbed_official 133:d4dda5c437f0 2398 /* Set Power State to ON */
mbed_official 133:d4dda5c437f0 2399 SDIO_PowerState_ON(hsd->Instance);
mbed_official 133:d4dda5c437f0 2400
mbed_official 133:d4dda5c437f0 2401 /* Enable SDIO Clock */
mbed_official 133:d4dda5c437f0 2402 __HAL_SD_SDIO_ENABLE();
mbed_official 133:d4dda5c437f0 2403
mbed_official 133:d4dda5c437f0 2404 /* CMD0: GO_IDLE_STATE -----------------------------------------------------*/
mbed_official 133:d4dda5c437f0 2405 /* No CMD response required */
mbed_official 133:d4dda5c437f0 2406 SDIO_CmdInitStructure.Argument = 0;
mbed_official 133:d4dda5c437f0 2407 SDIO_CmdInitStructure.CmdIndex = SD_CMD_GO_IDLE_STATE;
mbed_official 133:d4dda5c437f0 2408 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_NO;
mbed_official 133:d4dda5c437f0 2409 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 2410 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 2411 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 2412
mbed_official 133:d4dda5c437f0 2413 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 2414 errorState = SD_CmdError(hsd);
mbed_official 133:d4dda5c437f0 2415
mbed_official 133:d4dda5c437f0 2416 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 2417 {
mbed_official 133:d4dda5c437f0 2418 /* CMD Response TimeOut (wait for CMDSENT flag) */
mbed_official 133:d4dda5c437f0 2419 return errorState;
mbed_official 133:d4dda5c437f0 2420 }
mbed_official 133:d4dda5c437f0 2421
mbed_official 133:d4dda5c437f0 2422 /* CMD8: SEND_IF_COND ------------------------------------------------------*/
mbed_official 133:d4dda5c437f0 2423 /* Send CMD8 to verify SD card interface operating condition */
mbed_official 133:d4dda5c437f0 2424 /* Argument: - [31:12]: Reserved (shall be set to '0')
mbed_official 133:d4dda5c437f0 2425 - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
mbed_official 133:d4dda5c437f0 2426 - [7:0]: Check Pattern (recommended 0xAA) */
mbed_official 133:d4dda5c437f0 2427 /* CMD Response: R7 */
mbed_official 133:d4dda5c437f0 2428 SDIO_CmdInitStructure.Argument = SD_CHECK_PATTERN;
mbed_official 133:d4dda5c437f0 2429 SDIO_CmdInitStructure.CmdIndex = SD_SDIO_SEND_IF_COND;
mbed_official 133:d4dda5c437f0 2430 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 2431 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 2432
mbed_official 133:d4dda5c437f0 2433 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 2434 errorState = SD_CmdResp7Error(hsd);
mbed_official 133:d4dda5c437f0 2435
mbed_official 133:d4dda5c437f0 2436 if (errorState == SD_OK)
mbed_official 133:d4dda5c437f0 2437 {
mbed_official 133:d4dda5c437f0 2438 /* SD Card 2.0 */
mbed_official 133:d4dda5c437f0 2439 hsd->CardType = STD_CAPACITY_SD_CARD_V2_0;
mbed_official 133:d4dda5c437f0 2440 SDType = SD_HIGH_CAPACITY;
mbed_official 133:d4dda5c437f0 2441 }
mbed_official 133:d4dda5c437f0 2442
mbed_official 133:d4dda5c437f0 2443 /* Send CMD55 */
mbed_official 133:d4dda5c437f0 2444 SDIO_CmdInitStructure.Argument = 0;
mbed_official 133:d4dda5c437f0 2445 SDIO_CmdInitStructure.CmdIndex = SD_CMD_APP_CMD;
mbed_official 133:d4dda5c437f0 2446 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 2447
mbed_official 133:d4dda5c437f0 2448 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 2449 errorState = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
mbed_official 133:d4dda5c437f0 2450
mbed_official 133:d4dda5c437f0 2451 /* If errorState is Command TimeOut, it is a MMC card */
mbed_official 133:d4dda5c437f0 2452 /* If errorState is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch)
mbed_official 133:d4dda5c437f0 2453 or SD card 1.x */
mbed_official 133:d4dda5c437f0 2454 if(errorState == SD_OK)
mbed_official 133:d4dda5c437f0 2455 {
mbed_official 133:d4dda5c437f0 2456 /* SD CARD */
mbed_official 133:d4dda5c437f0 2457 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
mbed_official 133:d4dda5c437f0 2458 while((!validvoltage) && (count < SD_MAX_VOLT_TRIAL))
mbed_official 133:d4dda5c437f0 2459 {
mbed_official 133:d4dda5c437f0 2460
mbed_official 133:d4dda5c437f0 2461 /* SEND CMD55 APP_CMD with RCA as 0 */
mbed_official 133:d4dda5c437f0 2462 SDIO_CmdInitStructure.Argument = 0;
mbed_official 133:d4dda5c437f0 2463 SDIO_CmdInitStructure.CmdIndex = SD_CMD_APP_CMD;
mbed_official 133:d4dda5c437f0 2464 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 2465 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 2466 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 2467 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 2468
mbed_official 133:d4dda5c437f0 2469 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 2470 errorState = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
mbed_official 133:d4dda5c437f0 2471
mbed_official 133:d4dda5c437f0 2472 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 2473 {
mbed_official 133:d4dda5c437f0 2474 return errorState;
mbed_official 133:d4dda5c437f0 2475 }
mbed_official 133:d4dda5c437f0 2476
mbed_official 133:d4dda5c437f0 2477 /* Send CMD41 */
mbed_official 133:d4dda5c437f0 2478 SDIO_CmdInitStructure.Argument = SD_VOLTAGE_WINDOW_SD | SDType;
mbed_official 133:d4dda5c437f0 2479 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SD_APP_OP_COND;
mbed_official 133:d4dda5c437f0 2480 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 2481 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 2482 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 2483 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 2484
mbed_official 133:d4dda5c437f0 2485 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 2486 errorState = SD_CmdResp3Error(hsd);
mbed_official 133:d4dda5c437f0 2487
mbed_official 133:d4dda5c437f0 2488 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 2489 {
mbed_official 133:d4dda5c437f0 2490 return errorState;
mbed_official 133:d4dda5c437f0 2491 }
mbed_official 133:d4dda5c437f0 2492
mbed_official 133:d4dda5c437f0 2493 /* Get command response */
mbed_official 133:d4dda5c437f0 2494 response = SDIO_GetResponse(SDIO_RESP1);
mbed_official 133:d4dda5c437f0 2495
mbed_official 133:d4dda5c437f0 2496 /* Get operating voltage*/
mbed_official 133:d4dda5c437f0 2497 validvoltage = (((response >> 31) == 1) ? 1 : 0);
mbed_official 133:d4dda5c437f0 2498
mbed_official 133:d4dda5c437f0 2499 count++;
mbed_official 133:d4dda5c437f0 2500 }
mbed_official 133:d4dda5c437f0 2501
mbed_official 133:d4dda5c437f0 2502 if(count >= SD_MAX_VOLT_TRIAL)
mbed_official 133:d4dda5c437f0 2503 {
mbed_official 133:d4dda5c437f0 2504 errorState = SD_INVALID_VOLTRANGE;
mbed_official 133:d4dda5c437f0 2505
mbed_official 133:d4dda5c437f0 2506 return errorState;
mbed_official 133:d4dda5c437f0 2507 }
mbed_official 133:d4dda5c437f0 2508
mbed_official 133:d4dda5c437f0 2509 if((response & SD_HIGH_CAPACITY) == SD_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
mbed_official 133:d4dda5c437f0 2510 {
mbed_official 133:d4dda5c437f0 2511 hsd->CardType = HIGH_CAPACITY_SD_CARD;
mbed_official 133:d4dda5c437f0 2512 }
mbed_official 133:d4dda5c437f0 2513
mbed_official 133:d4dda5c437f0 2514 } /* else MMC Card */
mbed_official 133:d4dda5c437f0 2515
mbed_official 133:d4dda5c437f0 2516 return errorState;
mbed_official 133:d4dda5c437f0 2517 }
mbed_official 133:d4dda5c437f0 2518
mbed_official 133:d4dda5c437f0 2519 /**
mbed_official 133:d4dda5c437f0 2520 * @brief Turns the SDIO output signals off.
mbed_official 133:d4dda5c437f0 2521 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 2522 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 2523 */
mbed_official 133:d4dda5c437f0 2524 static HAL_SD_ErrorTypedef SD_PowerOFF(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 2525 {
mbed_official 133:d4dda5c437f0 2526 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 2527
mbed_official 133:d4dda5c437f0 2528 /* Set Power State to OFF */
mbed_official 133:d4dda5c437f0 2529 SDIO_PowerState_OFF(hsd->Instance);
mbed_official 133:d4dda5c437f0 2530
mbed_official 133:d4dda5c437f0 2531 return errorState;
mbed_official 133:d4dda5c437f0 2532 }
mbed_official 133:d4dda5c437f0 2533
mbed_official 133:d4dda5c437f0 2534 /**
mbed_official 133:d4dda5c437f0 2535 * @brief Returns the current card's status.
mbed_official 133:d4dda5c437f0 2536 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 2537 * @param pCardStatus: pointer to the buffer that will contain the SD card
mbed_official 133:d4dda5c437f0 2538 * status (Card Status register)
mbed_official 133:d4dda5c437f0 2539 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 2540 */
mbed_official 133:d4dda5c437f0 2541 static HAL_SD_ErrorTypedef SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
mbed_official 133:d4dda5c437f0 2542 {
mbed_official 133:d4dda5c437f0 2543 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
mbed_official 133:d4dda5c437f0 2544 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 2545
mbed_official 133:d4dda5c437f0 2546 if(pCardStatus == NULL)
mbed_official 133:d4dda5c437f0 2547 {
mbed_official 133:d4dda5c437f0 2548 errorState = SD_INVALID_PARAMETER;
mbed_official 133:d4dda5c437f0 2549
mbed_official 133:d4dda5c437f0 2550 return errorState;
mbed_official 133:d4dda5c437f0 2551 }
mbed_official 133:d4dda5c437f0 2552
mbed_official 133:d4dda5c437f0 2553 /* Send Status command */
mbed_official 133:d4dda5c437f0 2554 SDIO_CmdInitStructure.Argument = (uint32_t)(hsd->RCA << 16);
mbed_official 133:d4dda5c437f0 2555 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SEND_STATUS;
mbed_official 133:d4dda5c437f0 2556 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 2557 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 2558 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 2559 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 2560
mbed_official 133:d4dda5c437f0 2561 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 2562 errorState = SD_CmdResp1Error(hsd, SD_CMD_SEND_STATUS);
mbed_official 133:d4dda5c437f0 2563
mbed_official 133:d4dda5c437f0 2564 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 2565 {
mbed_official 133:d4dda5c437f0 2566 return errorState;
mbed_official 133:d4dda5c437f0 2567 }
mbed_official 133:d4dda5c437f0 2568
mbed_official 133:d4dda5c437f0 2569 /* Get SD card status */
mbed_official 133:d4dda5c437f0 2570 *pCardStatus = SDIO_GetResponse(SDIO_RESP1);
mbed_official 133:d4dda5c437f0 2571
mbed_official 133:d4dda5c437f0 2572 return errorState;
mbed_official 133:d4dda5c437f0 2573 }
mbed_official 133:d4dda5c437f0 2574
mbed_official 133:d4dda5c437f0 2575 /**
mbed_official 133:d4dda5c437f0 2576 * @brief Checks for error conditions for CMD0.
mbed_official 133:d4dda5c437f0 2577 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 2578 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 2579 */
mbed_official 133:d4dda5c437f0 2580 static HAL_SD_ErrorTypedef SD_CmdError(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 2581 {
mbed_official 133:d4dda5c437f0 2582 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 2583 uint32_t timeout, tmp;
mbed_official 133:d4dda5c437f0 2584
mbed_official 133:d4dda5c437f0 2585 timeout = SDIO_CMD0TIMEOUT;
mbed_official 133:d4dda5c437f0 2586
mbed_official 133:d4dda5c437f0 2587 tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CMDSENT);
mbed_official 133:d4dda5c437f0 2588
mbed_official 133:d4dda5c437f0 2589 while((timeout > 0) && (!tmp))
mbed_official 133:d4dda5c437f0 2590 {
mbed_official 133:d4dda5c437f0 2591 tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CMDSENT);
mbed_official 133:d4dda5c437f0 2592 timeout--;
mbed_official 133:d4dda5c437f0 2593 }
mbed_official 133:d4dda5c437f0 2594
mbed_official 133:d4dda5c437f0 2595 if(timeout == 0)
mbed_official 133:d4dda5c437f0 2596 {
mbed_official 133:d4dda5c437f0 2597 errorState = SD_CMD_RSP_TIMEOUT;
mbed_official 133:d4dda5c437f0 2598 return errorState;
mbed_official 133:d4dda5c437f0 2599 }
mbed_official 133:d4dda5c437f0 2600
mbed_official 133:d4dda5c437f0 2601 /* Clear all the static flags */
mbed_official 133:d4dda5c437f0 2602 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
mbed_official 133:d4dda5c437f0 2603
mbed_official 133:d4dda5c437f0 2604 return errorState;
mbed_official 133:d4dda5c437f0 2605 }
mbed_official 133:d4dda5c437f0 2606
mbed_official 133:d4dda5c437f0 2607 /**
mbed_official 133:d4dda5c437f0 2608 * @brief Checks for error conditions for R7 response.
mbed_official 133:d4dda5c437f0 2609 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 2610 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 2611 */
mbed_official 133:d4dda5c437f0 2612 static HAL_SD_ErrorTypedef SD_CmdResp7Error(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 2613 {
mbed_official 133:d4dda5c437f0 2614 HAL_SD_ErrorTypedef errorState = SD_ERROR;
mbed_official 133:d4dda5c437f0 2615 uint32_t timeout = SDIO_CMD0TIMEOUT, tmp;
mbed_official 133:d4dda5c437f0 2616
mbed_official 133:d4dda5c437f0 2617 tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT);
mbed_official 133:d4dda5c437f0 2618
mbed_official 133:d4dda5c437f0 2619 while((!tmp) && (timeout > 0))
mbed_official 133:d4dda5c437f0 2620 {
mbed_official 133:d4dda5c437f0 2621 tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT);
mbed_official 133:d4dda5c437f0 2622 timeout--;
mbed_official 133:d4dda5c437f0 2623 }
mbed_official 133:d4dda5c437f0 2624
mbed_official 133:d4dda5c437f0 2625 tmp = __HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
mbed_official 133:d4dda5c437f0 2626
mbed_official 133:d4dda5c437f0 2627 if((timeout == 0) || tmp)
mbed_official 133:d4dda5c437f0 2628 {
mbed_official 133:d4dda5c437f0 2629 /* Card is not V2.0 compliant or card does not support the set voltage range */
mbed_official 133:d4dda5c437f0 2630 errorState = SD_CMD_RSP_TIMEOUT;
mbed_official 133:d4dda5c437f0 2631
mbed_official 133:d4dda5c437f0 2632 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
mbed_official 133:d4dda5c437f0 2633
mbed_official 133:d4dda5c437f0 2634 return errorState;
mbed_official 133:d4dda5c437f0 2635 }
mbed_official 133:d4dda5c437f0 2636
mbed_official 133:d4dda5c437f0 2637 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CMDREND))
mbed_official 133:d4dda5c437f0 2638 {
mbed_official 133:d4dda5c437f0 2639 /* Card is SD V2.0 compliant */
mbed_official 133:d4dda5c437f0 2640 errorState = SD_OK;
mbed_official 133:d4dda5c437f0 2641
mbed_official 133:d4dda5c437f0 2642 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CMDREND);
mbed_official 133:d4dda5c437f0 2643
mbed_official 133:d4dda5c437f0 2644 return errorState;
mbed_official 133:d4dda5c437f0 2645 }
mbed_official 133:d4dda5c437f0 2646
mbed_official 133:d4dda5c437f0 2647 return errorState;
mbed_official 133:d4dda5c437f0 2648 }
mbed_official 133:d4dda5c437f0 2649
mbed_official 133:d4dda5c437f0 2650 /**
mbed_official 133:d4dda5c437f0 2651 * @brief Checks for error conditions for R1 response.
mbed_official 133:d4dda5c437f0 2652 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 2653 * @param SD_CMD: The sent command index
mbed_official 133:d4dda5c437f0 2654 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 2655 */
mbed_official 133:d4dda5c437f0 2656 static HAL_SD_ErrorTypedef SD_CmdResp1Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD)
mbed_official 133:d4dda5c437f0 2657 {
mbed_official 133:d4dda5c437f0 2658 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 2659 uint32_t response_R1;
mbed_official 133:d4dda5c437f0 2660
mbed_official 133:d4dda5c437f0 2661 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
mbed_official 133:d4dda5c437f0 2662 {
mbed_official 133:d4dda5c437f0 2663 }
mbed_official 133:d4dda5c437f0 2664
mbed_official 133:d4dda5c437f0 2665 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
mbed_official 133:d4dda5c437f0 2666 {
mbed_official 133:d4dda5c437f0 2667 errorState = SD_CMD_RSP_TIMEOUT;
mbed_official 133:d4dda5c437f0 2668
mbed_official 133:d4dda5c437f0 2669 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
mbed_official 133:d4dda5c437f0 2670
mbed_official 133:d4dda5c437f0 2671 return errorState;
mbed_official 133:d4dda5c437f0 2672 }
mbed_official 133:d4dda5c437f0 2673 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL))
mbed_official 133:d4dda5c437f0 2674 {
mbed_official 133:d4dda5c437f0 2675 errorState = SD_CMD_CRC_FAIL;
mbed_official 133:d4dda5c437f0 2676
mbed_official 133:d4dda5c437f0 2677 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL);
mbed_official 133:d4dda5c437f0 2678
mbed_official 133:d4dda5c437f0 2679 return errorState;
mbed_official 133:d4dda5c437f0 2680 }
mbed_official 133:d4dda5c437f0 2681
mbed_official 133:d4dda5c437f0 2682 /* Check response received is of desired command */
mbed_official 133:d4dda5c437f0 2683 if(SDIO_GetCommandResponse(hsd->Instance) != SD_CMD)
mbed_official 133:d4dda5c437f0 2684 {
mbed_official 133:d4dda5c437f0 2685 errorState = SD_ILLEGAL_CMD;
mbed_official 133:d4dda5c437f0 2686
mbed_official 133:d4dda5c437f0 2687 return errorState;
mbed_official 133:d4dda5c437f0 2688 }
mbed_official 133:d4dda5c437f0 2689
mbed_official 133:d4dda5c437f0 2690 /* Clear all the static flags */
mbed_official 133:d4dda5c437f0 2691 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
mbed_official 133:d4dda5c437f0 2692
mbed_official 133:d4dda5c437f0 2693 /* We have received response, retrieve it for analysis */
mbed_official 133:d4dda5c437f0 2694 response_R1 = SDIO_GetResponse(SDIO_RESP1);
mbed_official 133:d4dda5c437f0 2695
mbed_official 133:d4dda5c437f0 2696 if((response_R1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
mbed_official 133:d4dda5c437f0 2697 {
mbed_official 133:d4dda5c437f0 2698 return errorState;
mbed_official 133:d4dda5c437f0 2699 }
mbed_official 133:d4dda5c437f0 2700
mbed_official 133:d4dda5c437f0 2701 if((response_R1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE)
mbed_official 133:d4dda5c437f0 2702 {
mbed_official 133:d4dda5c437f0 2703 return(SD_ADDR_OUT_OF_RANGE);
mbed_official 133:d4dda5c437f0 2704 }
mbed_official 133:d4dda5c437f0 2705
mbed_official 133:d4dda5c437f0 2706 if((response_R1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED)
mbed_official 133:d4dda5c437f0 2707 {
mbed_official 133:d4dda5c437f0 2708 return(SD_ADDR_MISALIGNED);
mbed_official 133:d4dda5c437f0 2709 }
mbed_official 133:d4dda5c437f0 2710
mbed_official 133:d4dda5c437f0 2711 if((response_R1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR)
mbed_official 133:d4dda5c437f0 2712 {
mbed_official 133:d4dda5c437f0 2713 return(SD_BLOCK_LEN_ERR);
mbed_official 133:d4dda5c437f0 2714 }
mbed_official 133:d4dda5c437f0 2715
mbed_official 133:d4dda5c437f0 2716 if((response_R1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR)
mbed_official 133:d4dda5c437f0 2717 {
mbed_official 133:d4dda5c437f0 2718 return(SD_ERASE_SEQ_ERR);
mbed_official 133:d4dda5c437f0 2719 }
mbed_official 133:d4dda5c437f0 2720
mbed_official 133:d4dda5c437f0 2721 if((response_R1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM)
mbed_official 133:d4dda5c437f0 2722 {
mbed_official 133:d4dda5c437f0 2723 return(SD_BAD_ERASE_PARAM);
mbed_official 133:d4dda5c437f0 2724 }
mbed_official 133:d4dda5c437f0 2725
mbed_official 133:d4dda5c437f0 2726 if((response_R1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION)
mbed_official 133:d4dda5c437f0 2727 {
mbed_official 133:d4dda5c437f0 2728 return(SD_WRITE_PROT_VIOLATION);
mbed_official 133:d4dda5c437f0 2729 }
mbed_official 133:d4dda5c437f0 2730
mbed_official 133:d4dda5c437f0 2731 if((response_R1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED)
mbed_official 133:d4dda5c437f0 2732 {
mbed_official 133:d4dda5c437f0 2733 return(SD_LOCK_UNLOCK_FAILED);
mbed_official 133:d4dda5c437f0 2734 }
mbed_official 133:d4dda5c437f0 2735
mbed_official 133:d4dda5c437f0 2736 if((response_R1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED)
mbed_official 133:d4dda5c437f0 2737 {
mbed_official 133:d4dda5c437f0 2738 return(SD_COM_CRC_FAILED);
mbed_official 133:d4dda5c437f0 2739 }
mbed_official 133:d4dda5c437f0 2740
mbed_official 133:d4dda5c437f0 2741 if((response_R1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD)
mbed_official 133:d4dda5c437f0 2742 {
mbed_official 133:d4dda5c437f0 2743 return(SD_ILLEGAL_CMD);
mbed_official 133:d4dda5c437f0 2744 }
mbed_official 133:d4dda5c437f0 2745
mbed_official 133:d4dda5c437f0 2746 if((response_R1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED)
mbed_official 133:d4dda5c437f0 2747 {
mbed_official 133:d4dda5c437f0 2748 return(SD_CARD_ECC_FAILED);
mbed_official 133:d4dda5c437f0 2749 }
mbed_official 133:d4dda5c437f0 2750
mbed_official 133:d4dda5c437f0 2751 if((response_R1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR)
mbed_official 133:d4dda5c437f0 2752 {
mbed_official 133:d4dda5c437f0 2753 return(SD_CC_ERROR);
mbed_official 133:d4dda5c437f0 2754 }
mbed_official 133:d4dda5c437f0 2755
mbed_official 133:d4dda5c437f0 2756 if((response_R1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR)
mbed_official 133:d4dda5c437f0 2757 {
mbed_official 133:d4dda5c437f0 2758 return(SD_GENERAL_UNKNOWN_ERROR);
mbed_official 133:d4dda5c437f0 2759 }
mbed_official 133:d4dda5c437f0 2760
mbed_official 133:d4dda5c437f0 2761 if((response_R1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN)
mbed_official 133:d4dda5c437f0 2762 {
mbed_official 133:d4dda5c437f0 2763 return(SD_STREAM_READ_UNDERRUN);
mbed_official 133:d4dda5c437f0 2764 }
mbed_official 133:d4dda5c437f0 2765
mbed_official 133:d4dda5c437f0 2766 if((response_R1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN)
mbed_official 133:d4dda5c437f0 2767 {
mbed_official 133:d4dda5c437f0 2768 return(SD_STREAM_WRITE_OVERRUN);
mbed_official 133:d4dda5c437f0 2769 }
mbed_official 133:d4dda5c437f0 2770
mbed_official 133:d4dda5c437f0 2771 if((response_R1 & SD_OCR_CID_CSD_OVERWRIETE) == SD_OCR_CID_CSD_OVERWRIETE)
mbed_official 133:d4dda5c437f0 2772 {
mbed_official 133:d4dda5c437f0 2773 return(SD_CID_CSD_OVERWRITE);
mbed_official 133:d4dda5c437f0 2774 }
mbed_official 133:d4dda5c437f0 2775
mbed_official 133:d4dda5c437f0 2776 if((response_R1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP)
mbed_official 133:d4dda5c437f0 2777 {
mbed_official 133:d4dda5c437f0 2778 return(SD_WP_ERASE_SKIP);
mbed_official 133:d4dda5c437f0 2779 }
mbed_official 133:d4dda5c437f0 2780
mbed_official 133:d4dda5c437f0 2781 if((response_R1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED)
mbed_official 133:d4dda5c437f0 2782 {
mbed_official 133:d4dda5c437f0 2783 return(SD_CARD_ECC_DISABLED);
mbed_official 133:d4dda5c437f0 2784 }
mbed_official 133:d4dda5c437f0 2785
mbed_official 133:d4dda5c437f0 2786 if((response_R1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET)
mbed_official 133:d4dda5c437f0 2787 {
mbed_official 133:d4dda5c437f0 2788 return(SD_ERASE_RESET);
mbed_official 133:d4dda5c437f0 2789 }
mbed_official 133:d4dda5c437f0 2790
mbed_official 133:d4dda5c437f0 2791 if((response_R1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR)
mbed_official 133:d4dda5c437f0 2792 {
mbed_official 133:d4dda5c437f0 2793 return(SD_AKE_SEQ_ERROR);
mbed_official 133:d4dda5c437f0 2794 }
mbed_official 133:d4dda5c437f0 2795
mbed_official 133:d4dda5c437f0 2796 return errorState;
mbed_official 133:d4dda5c437f0 2797 }
mbed_official 133:d4dda5c437f0 2798
mbed_official 133:d4dda5c437f0 2799 /**
mbed_official 133:d4dda5c437f0 2800 * @brief Checks for error conditions for R3 (OCR) response.
mbed_official 133:d4dda5c437f0 2801 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 2802 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 2803 */
mbed_official 133:d4dda5c437f0 2804 static HAL_SD_ErrorTypedef SD_CmdResp3Error(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 2805 {
mbed_official 133:d4dda5c437f0 2806 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 2807
mbed_official 133:d4dda5c437f0 2808 while (!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
mbed_official 133:d4dda5c437f0 2809 {
mbed_official 133:d4dda5c437f0 2810 }
mbed_official 133:d4dda5c437f0 2811
mbed_official 133:d4dda5c437f0 2812 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
mbed_official 133:d4dda5c437f0 2813 {
mbed_official 133:d4dda5c437f0 2814 errorState = SD_CMD_RSP_TIMEOUT;
mbed_official 133:d4dda5c437f0 2815
mbed_official 133:d4dda5c437f0 2816 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
mbed_official 133:d4dda5c437f0 2817
mbed_official 133:d4dda5c437f0 2818 return errorState;
mbed_official 133:d4dda5c437f0 2819 }
mbed_official 133:d4dda5c437f0 2820
mbed_official 133:d4dda5c437f0 2821 /* Clear all the static flags */
mbed_official 133:d4dda5c437f0 2822 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
mbed_official 133:d4dda5c437f0 2823
mbed_official 133:d4dda5c437f0 2824 return errorState;
mbed_official 133:d4dda5c437f0 2825 }
mbed_official 133:d4dda5c437f0 2826
mbed_official 133:d4dda5c437f0 2827 /**
mbed_official 133:d4dda5c437f0 2828 * @brief Checks for error conditions for R2 (CID or CSD) response.
mbed_official 133:d4dda5c437f0 2829 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 2830 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 2831 */
mbed_official 133:d4dda5c437f0 2832 static HAL_SD_ErrorTypedef SD_CmdResp2Error(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 2833 {
mbed_official 133:d4dda5c437f0 2834 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 2835
mbed_official 133:d4dda5c437f0 2836 while (!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
mbed_official 133:d4dda5c437f0 2837 {
mbed_official 133:d4dda5c437f0 2838 }
mbed_official 133:d4dda5c437f0 2839
mbed_official 133:d4dda5c437f0 2840 if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
mbed_official 133:d4dda5c437f0 2841 {
mbed_official 133:d4dda5c437f0 2842 errorState = SD_CMD_RSP_TIMEOUT;
mbed_official 133:d4dda5c437f0 2843
mbed_official 133:d4dda5c437f0 2844 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
mbed_official 133:d4dda5c437f0 2845
mbed_official 133:d4dda5c437f0 2846 return errorState;
mbed_official 133:d4dda5c437f0 2847 }
mbed_official 133:d4dda5c437f0 2848 else if (__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL))
mbed_official 133:d4dda5c437f0 2849 {
mbed_official 133:d4dda5c437f0 2850 errorState = SD_CMD_CRC_FAIL;
mbed_official 133:d4dda5c437f0 2851
mbed_official 133:d4dda5c437f0 2852 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL);
mbed_official 133:d4dda5c437f0 2853
mbed_official 133:d4dda5c437f0 2854 return errorState;
mbed_official 133:d4dda5c437f0 2855 }
mbed_official 133:d4dda5c437f0 2856
mbed_official 133:d4dda5c437f0 2857 /* Clear all the static flags */
mbed_official 133:d4dda5c437f0 2858 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
mbed_official 133:d4dda5c437f0 2859
mbed_official 133:d4dda5c437f0 2860 return errorState;
mbed_official 133:d4dda5c437f0 2861 }
mbed_official 133:d4dda5c437f0 2862
mbed_official 133:d4dda5c437f0 2863 /**
mbed_official 133:d4dda5c437f0 2864 * @brief Checks for error conditions for R6 (RCA) response.
mbed_official 133:d4dda5c437f0 2865 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 2866 * @param SD_CMD: The sent command index
mbed_official 133:d4dda5c437f0 2867 * @param pRCA: Pointer to the variable that will contain the SD card relative
mbed_official 133:d4dda5c437f0 2868 * address RCA
mbed_official 133:d4dda5c437f0 2869 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 2870 */
mbed_official 133:d4dda5c437f0 2871 static HAL_SD_ErrorTypedef SD_CmdResp6Error(SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA)
mbed_official 133:d4dda5c437f0 2872 {
mbed_official 133:d4dda5c437f0 2873 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 2874 uint32_t response_R1;
mbed_official 133:d4dda5c437f0 2875
mbed_official 133:d4dda5c437f0 2876 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
mbed_official 133:d4dda5c437f0 2877 {
mbed_official 133:d4dda5c437f0 2878 }
mbed_official 133:d4dda5c437f0 2879
mbed_official 133:d4dda5c437f0 2880 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
mbed_official 133:d4dda5c437f0 2881 {
mbed_official 133:d4dda5c437f0 2882 errorState = SD_CMD_RSP_TIMEOUT;
mbed_official 133:d4dda5c437f0 2883
mbed_official 133:d4dda5c437f0 2884 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
mbed_official 133:d4dda5c437f0 2885
mbed_official 133:d4dda5c437f0 2886 return errorState;
mbed_official 133:d4dda5c437f0 2887 }
mbed_official 133:d4dda5c437f0 2888 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL))
mbed_official 133:d4dda5c437f0 2889 {
mbed_official 133:d4dda5c437f0 2890 errorState = SD_CMD_CRC_FAIL;
mbed_official 133:d4dda5c437f0 2891
mbed_official 133:d4dda5c437f0 2892 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL);
mbed_official 133:d4dda5c437f0 2893
mbed_official 133:d4dda5c437f0 2894 return errorState;
mbed_official 133:d4dda5c437f0 2895 }
mbed_official 133:d4dda5c437f0 2896
mbed_official 133:d4dda5c437f0 2897 /* Check response received is of desired command */
mbed_official 133:d4dda5c437f0 2898 if(SDIO_GetCommandResponse(hsd->Instance) != SD_CMD)
mbed_official 133:d4dda5c437f0 2899 {
mbed_official 133:d4dda5c437f0 2900 errorState = SD_ILLEGAL_CMD;
mbed_official 133:d4dda5c437f0 2901
mbed_official 133:d4dda5c437f0 2902 return errorState;
mbed_official 133:d4dda5c437f0 2903 }
mbed_official 133:d4dda5c437f0 2904
mbed_official 133:d4dda5c437f0 2905 /* Clear all the static flags */
mbed_official 133:d4dda5c437f0 2906 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
mbed_official 133:d4dda5c437f0 2907
mbed_official 133:d4dda5c437f0 2908 /* We have received response, retrieve it. */
mbed_official 133:d4dda5c437f0 2909 response_R1 = SDIO_GetResponse(SDIO_RESP1);
mbed_official 133:d4dda5c437f0 2910
mbed_official 133:d4dda5c437f0 2911 if((response_R1 & (SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD | SD_R6_COM_CRC_FAILED)) == SD_ALLZERO)
mbed_official 133:d4dda5c437f0 2912 {
mbed_official 133:d4dda5c437f0 2913 *pRCA = (uint16_t) (response_R1 >> 16);
mbed_official 133:d4dda5c437f0 2914
mbed_official 133:d4dda5c437f0 2915 return errorState;
mbed_official 133:d4dda5c437f0 2916 }
mbed_official 133:d4dda5c437f0 2917
mbed_official 133:d4dda5c437f0 2918 if((response_R1 & SD_R6_GENERAL_UNKNOWN_ERROR) == SD_R6_GENERAL_UNKNOWN_ERROR)
mbed_official 133:d4dda5c437f0 2919 {
mbed_official 133:d4dda5c437f0 2920 return(SD_GENERAL_UNKNOWN_ERROR);
mbed_official 133:d4dda5c437f0 2921 }
mbed_official 133:d4dda5c437f0 2922
mbed_official 133:d4dda5c437f0 2923 if((response_R1 & SD_R6_ILLEGAL_CMD) == SD_R6_ILLEGAL_CMD)
mbed_official 133:d4dda5c437f0 2924 {
mbed_official 133:d4dda5c437f0 2925 return(SD_ILLEGAL_CMD);
mbed_official 133:d4dda5c437f0 2926 }
mbed_official 133:d4dda5c437f0 2927
mbed_official 133:d4dda5c437f0 2928 if((response_R1 & SD_R6_COM_CRC_FAILED) == SD_R6_COM_CRC_FAILED)
mbed_official 133:d4dda5c437f0 2929 {
mbed_official 133:d4dda5c437f0 2930 return(SD_COM_CRC_FAILED);
mbed_official 133:d4dda5c437f0 2931 }
mbed_official 133:d4dda5c437f0 2932
mbed_official 133:d4dda5c437f0 2933 return errorState;
mbed_official 133:d4dda5c437f0 2934 }
mbed_official 133:d4dda5c437f0 2935
mbed_official 133:d4dda5c437f0 2936 /**
mbed_official 133:d4dda5c437f0 2937 * @brief Enables the SDIO wide bus mode.
mbed_official 133:d4dda5c437f0 2938 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 2939 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 2940 */
mbed_official 133:d4dda5c437f0 2941 static HAL_SD_ErrorTypedef SD_WideBus_Enable(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 2942 {
mbed_official 133:d4dda5c437f0 2943 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
mbed_official 133:d4dda5c437f0 2944 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 2945
mbed_official 133:d4dda5c437f0 2946 uint32_t scr[2] = {0, 0};
mbed_official 133:d4dda5c437f0 2947
mbed_official 133:d4dda5c437f0 2948 if((SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
mbed_official 133:d4dda5c437f0 2949 {
mbed_official 133:d4dda5c437f0 2950 errorState = SD_LOCK_UNLOCK_FAILED;
mbed_official 133:d4dda5c437f0 2951
mbed_official 133:d4dda5c437f0 2952 return errorState;
mbed_official 133:d4dda5c437f0 2953 }
mbed_official 133:d4dda5c437f0 2954
mbed_official 133:d4dda5c437f0 2955 /* Get SCR Register */
mbed_official 133:d4dda5c437f0 2956 errorState = SD_FindSCR(hsd, scr);
mbed_official 133:d4dda5c437f0 2957
mbed_official 133:d4dda5c437f0 2958 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 2959 {
mbed_official 133:d4dda5c437f0 2960 return errorState;
mbed_official 133:d4dda5c437f0 2961 }
mbed_official 133:d4dda5c437f0 2962
mbed_official 133:d4dda5c437f0 2963 /* If requested card supports wide bus operation */
mbed_official 133:d4dda5c437f0 2964 if((scr[1] & SD_WIDE_BUS_SUPPORT) != SD_ALLZERO)
mbed_official 133:d4dda5c437f0 2965 {
mbed_official 133:d4dda5c437f0 2966 /* Send CMD55 APP_CMD with argument as card's RCA.*/
mbed_official 133:d4dda5c437f0 2967 SDIO_CmdInitStructure.Argument = (uint32_t)(hsd->RCA << 16);
mbed_official 133:d4dda5c437f0 2968 SDIO_CmdInitStructure.CmdIndex = SD_CMD_APP_CMD;
mbed_official 133:d4dda5c437f0 2969 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 2970 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 2971 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 2972 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 2973
mbed_official 133:d4dda5c437f0 2974 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 2975 errorState = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
mbed_official 133:d4dda5c437f0 2976
mbed_official 133:d4dda5c437f0 2977 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 2978 {
mbed_official 133:d4dda5c437f0 2979 return errorState;
mbed_official 133:d4dda5c437f0 2980 }
mbed_official 133:d4dda5c437f0 2981
mbed_official 133:d4dda5c437f0 2982 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
mbed_official 133:d4dda5c437f0 2983 SDIO_CmdInitStructure.Argument = 2;
mbed_official 133:d4dda5c437f0 2984 SDIO_CmdInitStructure.CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
mbed_official 133:d4dda5c437f0 2985 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 2986
mbed_official 133:d4dda5c437f0 2987 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 2988 errorState = SD_CmdResp1Error(hsd, SD_CMD_APP_SD_SET_BUSWIDTH);
mbed_official 133:d4dda5c437f0 2989
mbed_official 133:d4dda5c437f0 2990 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 2991 {
mbed_official 133:d4dda5c437f0 2992 return errorState;
mbed_official 133:d4dda5c437f0 2993 }
mbed_official 133:d4dda5c437f0 2994
mbed_official 133:d4dda5c437f0 2995 return errorState;
mbed_official 133:d4dda5c437f0 2996 }
mbed_official 133:d4dda5c437f0 2997 else
mbed_official 133:d4dda5c437f0 2998 {
mbed_official 133:d4dda5c437f0 2999 errorState = SD_REQUEST_NOT_APPLICABLE;
mbed_official 133:d4dda5c437f0 3000
mbed_official 133:d4dda5c437f0 3001 return errorState;
mbed_official 133:d4dda5c437f0 3002 }
mbed_official 133:d4dda5c437f0 3003 }
mbed_official 133:d4dda5c437f0 3004
mbed_official 133:d4dda5c437f0 3005 /**
mbed_official 133:d4dda5c437f0 3006 * @brief Disables the SDIO wide bus mode.
mbed_official 133:d4dda5c437f0 3007 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 3008 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 3009 */
mbed_official 133:d4dda5c437f0 3010 static HAL_SD_ErrorTypedef SD_WideBus_Disable(SD_HandleTypeDef *hsd)
mbed_official 133:d4dda5c437f0 3011 {
mbed_official 133:d4dda5c437f0 3012 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
mbed_official 133:d4dda5c437f0 3013 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 3014
mbed_official 133:d4dda5c437f0 3015 uint32_t scr[2] = {0, 0};
mbed_official 133:d4dda5c437f0 3016
mbed_official 133:d4dda5c437f0 3017 if((SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) == SD_CARD_LOCKED)
mbed_official 133:d4dda5c437f0 3018 {
mbed_official 133:d4dda5c437f0 3019 errorState = SD_LOCK_UNLOCK_FAILED;
mbed_official 133:d4dda5c437f0 3020
mbed_official 133:d4dda5c437f0 3021 return errorState;
mbed_official 133:d4dda5c437f0 3022 }
mbed_official 133:d4dda5c437f0 3023
mbed_official 133:d4dda5c437f0 3024 /* Get SCR Register */
mbed_official 133:d4dda5c437f0 3025 errorState = SD_FindSCR(hsd, scr);
mbed_official 133:d4dda5c437f0 3026
mbed_official 133:d4dda5c437f0 3027 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 3028 {
mbed_official 133:d4dda5c437f0 3029 return errorState;
mbed_official 133:d4dda5c437f0 3030 }
mbed_official 133:d4dda5c437f0 3031
mbed_official 133:d4dda5c437f0 3032 /* If requested card supports 1 bit mode operation */
mbed_official 133:d4dda5c437f0 3033 if((scr[1] & SD_SINGLE_BUS_SUPPORT) != SD_ALLZERO)
mbed_official 133:d4dda5c437f0 3034 {
mbed_official 133:d4dda5c437f0 3035 /* Send CMD55 APP_CMD with argument as card's RCA */
mbed_official 133:d4dda5c437f0 3036 SDIO_CmdInitStructure.Argument = (uint32_t)(hsd->RCA << 16);
mbed_official 133:d4dda5c437f0 3037 SDIO_CmdInitStructure.CmdIndex = SD_CMD_APP_CMD;
mbed_official 133:d4dda5c437f0 3038 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 3039 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 3040 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 3041 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 3042
mbed_official 133:d4dda5c437f0 3043 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 3044 errorState = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
mbed_official 133:d4dda5c437f0 3045
mbed_official 133:d4dda5c437f0 3046 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 3047 {
mbed_official 133:d4dda5c437f0 3048 return errorState;
mbed_official 133:d4dda5c437f0 3049 }
mbed_official 133:d4dda5c437f0 3050
mbed_official 133:d4dda5c437f0 3051 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
mbed_official 133:d4dda5c437f0 3052 SDIO_CmdInitStructure.Argument = 0;
mbed_official 133:d4dda5c437f0 3053 SDIO_CmdInitStructure.CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH;
mbed_official 133:d4dda5c437f0 3054 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 3055
mbed_official 133:d4dda5c437f0 3056 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 3057 errorState = SD_CmdResp1Error(hsd, SD_CMD_APP_SD_SET_BUSWIDTH);
mbed_official 133:d4dda5c437f0 3058
mbed_official 133:d4dda5c437f0 3059 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 3060 {
mbed_official 133:d4dda5c437f0 3061 return errorState;
mbed_official 133:d4dda5c437f0 3062 }
mbed_official 133:d4dda5c437f0 3063
mbed_official 133:d4dda5c437f0 3064 return errorState;
mbed_official 133:d4dda5c437f0 3065 }
mbed_official 133:d4dda5c437f0 3066 else
mbed_official 133:d4dda5c437f0 3067 {
mbed_official 133:d4dda5c437f0 3068 errorState = SD_REQUEST_NOT_APPLICABLE;
mbed_official 133:d4dda5c437f0 3069
mbed_official 133:d4dda5c437f0 3070 return errorState;
mbed_official 133:d4dda5c437f0 3071 }
mbed_official 133:d4dda5c437f0 3072 }
mbed_official 133:d4dda5c437f0 3073
mbed_official 133:d4dda5c437f0 3074
mbed_official 133:d4dda5c437f0 3075 /**
mbed_official 133:d4dda5c437f0 3076 * @brief Finds the SD card SCR register value.
mbed_official 133:d4dda5c437f0 3077 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 3078 * @param pSCR: pointer to the buffer that will contain the SCR value
mbed_official 133:d4dda5c437f0 3079 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 3080 */
mbed_official 133:d4dda5c437f0 3081 static HAL_SD_ErrorTypedef SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
mbed_official 133:d4dda5c437f0 3082 {
mbed_official 133:d4dda5c437f0 3083 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
mbed_official 133:d4dda5c437f0 3084 SDIO_DataInitTypeDef SDIO_DataInitStructure;
mbed_official 133:d4dda5c437f0 3085 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 3086 uint32_t index = 0;
mbed_official 133:d4dda5c437f0 3087 uint32_t tempscr[2] = {0, 0};
mbed_official 133:d4dda5c437f0 3088
mbed_official 133:d4dda5c437f0 3089 /* Set Block Size To 8 Bytes */
mbed_official 133:d4dda5c437f0 3090 /* Send CMD55 APP_CMD with argument as card's RCA */
mbed_official 133:d4dda5c437f0 3091 SDIO_CmdInitStructure.Argument = (uint32_t)8;
mbed_official 133:d4dda5c437f0 3092 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SET_BLOCKLEN;
mbed_official 133:d4dda5c437f0 3093 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 3094 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 3095 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 3096 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 3097
mbed_official 133:d4dda5c437f0 3098 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 3099 errorState = SD_CmdResp1Error(hsd, SD_CMD_SET_BLOCKLEN);
mbed_official 133:d4dda5c437f0 3100
mbed_official 133:d4dda5c437f0 3101 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 3102 {
mbed_official 133:d4dda5c437f0 3103 return errorState;
mbed_official 133:d4dda5c437f0 3104 }
mbed_official 133:d4dda5c437f0 3105
mbed_official 133:d4dda5c437f0 3106 /* Send CMD55 APP_CMD with argument as card's RCA */
mbed_official 133:d4dda5c437f0 3107 SDIO_CmdInitStructure.Argument = (uint32_t)((hsd->RCA) << 16);
mbed_official 133:d4dda5c437f0 3108 SDIO_CmdInitStructure.CmdIndex = SD_CMD_APP_CMD;
mbed_official 133:d4dda5c437f0 3109 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 3110
mbed_official 133:d4dda5c437f0 3111 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 3112 errorState = SD_CmdResp1Error(hsd, SD_CMD_APP_CMD);
mbed_official 133:d4dda5c437f0 3113
mbed_official 133:d4dda5c437f0 3114 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 3115 {
mbed_official 133:d4dda5c437f0 3116 return errorState;
mbed_official 133:d4dda5c437f0 3117 }
mbed_official 133:d4dda5c437f0 3118 SDIO_DataInitStructure.DataTimeOut = SD_DATATIMEOUT;
mbed_official 133:d4dda5c437f0 3119 SDIO_DataInitStructure.DataLength = 8;
mbed_official 133:d4dda5c437f0 3120 SDIO_DataInitStructure.DataBlockSize = SDIO_DATABLOCK_SIZE_8B;
mbed_official 133:d4dda5c437f0 3121 SDIO_DataInitStructure.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
mbed_official 133:d4dda5c437f0 3122 SDIO_DataInitStructure.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
mbed_official 133:d4dda5c437f0 3123 SDIO_DataInitStructure.DPSM = SDIO_DPSM_ENABLE;
mbed_official 133:d4dda5c437f0 3124 SDIO_DataConfig(hsd->Instance, &SDIO_DataInitStructure);
mbed_official 133:d4dda5c437f0 3125
mbed_official 133:d4dda5c437f0 3126 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
mbed_official 133:d4dda5c437f0 3127 SDIO_CmdInitStructure.Argument = 0;
mbed_official 133:d4dda5c437f0 3128 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SD_APP_SEND_SCR;
mbed_official 133:d4dda5c437f0 3129 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 3130
mbed_official 133:d4dda5c437f0 3131 /* Check for error conditions */
mbed_official 133:d4dda5c437f0 3132 errorState = SD_CmdResp1Error(hsd, SD_CMD_SD_APP_SEND_SCR);
mbed_official 133:d4dda5c437f0 3133
mbed_official 133:d4dda5c437f0 3134 if(errorState != SD_OK)
mbed_official 133:d4dda5c437f0 3135 {
mbed_official 133:d4dda5c437f0 3136 return errorState;
mbed_official 133:d4dda5c437f0 3137 }
mbed_official 133:d4dda5c437f0 3138
mbed_official 133:d4dda5c437f0 3139 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
mbed_official 133:d4dda5c437f0 3140 {
mbed_official 133:d4dda5c437f0 3141 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))
mbed_official 133:d4dda5c437f0 3142 {
mbed_official 133:d4dda5c437f0 3143 *(tempscr + index) = SDIO_ReadFIFO(hsd->Instance);
mbed_official 133:d4dda5c437f0 3144 index++;
mbed_official 133:d4dda5c437f0 3145 }
mbed_official 133:d4dda5c437f0 3146 }
mbed_official 133:d4dda5c437f0 3147
mbed_official 133:d4dda5c437f0 3148 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
mbed_official 133:d4dda5c437f0 3149 {
mbed_official 133:d4dda5c437f0 3150 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
mbed_official 133:d4dda5c437f0 3151
mbed_official 133:d4dda5c437f0 3152 errorState = SD_DATA_TIMEOUT;
mbed_official 133:d4dda5c437f0 3153
mbed_official 133:d4dda5c437f0 3154 return errorState;
mbed_official 133:d4dda5c437f0 3155 }
mbed_official 133:d4dda5c437f0 3156 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
mbed_official 133:d4dda5c437f0 3157 {
mbed_official 133:d4dda5c437f0 3158 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
mbed_official 133:d4dda5c437f0 3159
mbed_official 133:d4dda5c437f0 3160 errorState = SD_DATA_CRC_FAIL;
mbed_official 133:d4dda5c437f0 3161
mbed_official 133:d4dda5c437f0 3162 return errorState;
mbed_official 133:d4dda5c437f0 3163 }
mbed_official 133:d4dda5c437f0 3164 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
mbed_official 133:d4dda5c437f0 3165 {
mbed_official 133:d4dda5c437f0 3166 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
mbed_official 133:d4dda5c437f0 3167
mbed_official 133:d4dda5c437f0 3168 errorState = SD_RX_OVERRUN;
mbed_official 133:d4dda5c437f0 3169
mbed_official 133:d4dda5c437f0 3170 return errorState;
mbed_official 133:d4dda5c437f0 3171 }
mbed_official 133:d4dda5c437f0 3172 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_STBITERR))
mbed_official 133:d4dda5c437f0 3173 {
mbed_official 133:d4dda5c437f0 3174 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_STBITERR);
mbed_official 133:d4dda5c437f0 3175
mbed_official 133:d4dda5c437f0 3176 errorState = SD_START_BIT_ERR;
mbed_official 133:d4dda5c437f0 3177
mbed_official 133:d4dda5c437f0 3178 return errorState;
mbed_official 133:d4dda5c437f0 3179 }
mbed_official 133:d4dda5c437f0 3180
mbed_official 133:d4dda5c437f0 3181 /* Clear all the static flags */
mbed_official 133:d4dda5c437f0 3182 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
mbed_official 133:d4dda5c437f0 3183
mbed_official 133:d4dda5c437f0 3184 *(pSCR + 1) = ((tempscr[0] & SD_0TO7BITS) << 24) | ((tempscr[0] & SD_8TO15BITS) << 8) |\
mbed_official 133:d4dda5c437f0 3185 ((tempscr[0] & SD_16TO23BITS) >> 8) | ((tempscr[0] & SD_24TO31BITS) >> 24);
mbed_official 133:d4dda5c437f0 3186
mbed_official 133:d4dda5c437f0 3187 *(pSCR) = ((tempscr[1] & SD_0TO7BITS) << 24) | ((tempscr[1] & SD_8TO15BITS) << 8) |\
mbed_official 133:d4dda5c437f0 3188 ((tempscr[1] & SD_16TO23BITS) >> 8) | ((tempscr[1] & SD_24TO31BITS) >> 24);
mbed_official 133:d4dda5c437f0 3189
mbed_official 133:d4dda5c437f0 3190 return errorState;
mbed_official 133:d4dda5c437f0 3191 }
mbed_official 133:d4dda5c437f0 3192
mbed_official 133:d4dda5c437f0 3193 /**
mbed_official 133:d4dda5c437f0 3194 * @brief Checks if the SD card is in programming state.
mbed_official 133:d4dda5c437f0 3195 * @param hsd: SD handle
mbed_official 133:d4dda5c437f0 3196 * @param pStatus: pointer to the variable that will contain the SD card state
mbed_official 133:d4dda5c437f0 3197 * @retval SD Card error state
mbed_official 133:d4dda5c437f0 3198 */
mbed_official 133:d4dda5c437f0 3199 static HAL_SD_ErrorTypedef SD_IsCardProgramming(SD_HandleTypeDef *hsd, uint8_t *pStatus)
mbed_official 133:d4dda5c437f0 3200 {
mbed_official 133:d4dda5c437f0 3201 SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
mbed_official 133:d4dda5c437f0 3202 HAL_SD_ErrorTypedef errorState = SD_OK;
mbed_official 133:d4dda5c437f0 3203 __IO uint32_t responseR1 = 0;
mbed_official 133:d4dda5c437f0 3204
mbed_official 133:d4dda5c437f0 3205 SDIO_CmdInitStructure.Argument = (uint32_t)(hsd->RCA << 16);
mbed_official 133:d4dda5c437f0 3206 SDIO_CmdInitStructure.CmdIndex = SD_CMD_SEND_STATUS;
mbed_official 133:d4dda5c437f0 3207 SDIO_CmdInitStructure.Response = SDIO_RESPONSE_SHORT;
mbed_official 133:d4dda5c437f0 3208 SDIO_CmdInitStructure.WaitForInterrupt = SDIO_WAIT_NO;
mbed_official 133:d4dda5c437f0 3209 SDIO_CmdInitStructure.CPSM = SDIO_CPSM_ENABLE;
mbed_official 133:d4dda5c437f0 3210 SDIO_SendCommand(hsd->Instance, &SDIO_CmdInitStructure);
mbed_official 133:d4dda5c437f0 3211
mbed_official 133:d4dda5c437f0 3212 while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))
mbed_official 133:d4dda5c437f0 3213 {
mbed_official 133:d4dda5c437f0 3214 }
mbed_official 133:d4dda5c437f0 3215
mbed_official 133:d4dda5c437f0 3216 if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CTIMEOUT))
mbed_official 133:d4dda5c437f0 3217 {
mbed_official 133:d4dda5c437f0 3218 errorState = SD_CMD_RSP_TIMEOUT;
mbed_official 133:d4dda5c437f0 3219
mbed_official 133:d4dda5c437f0 3220 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CTIMEOUT);
mbed_official 133:d4dda5c437f0 3221
mbed_official 133:d4dda5c437f0 3222 return errorState;
mbed_official 133:d4dda5c437f0 3223 }
mbed_official 133:d4dda5c437f0 3224 else if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_CCRCFAIL))
mbed_official 133:d4dda5c437f0 3225 {
mbed_official 133:d4dda5c437f0 3226 errorState = SD_CMD_CRC_FAIL;
mbed_official 133:d4dda5c437f0 3227
mbed_official 133:d4dda5c437f0 3228 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_FLAG_CCRCFAIL);
mbed_official 133:d4dda5c437f0 3229
mbed_official 133:d4dda5c437f0 3230 return errorState;
mbed_official 133:d4dda5c437f0 3231 }
mbed_official 133:d4dda5c437f0 3232
mbed_official 133:d4dda5c437f0 3233 /* Check response received is of desired command */
mbed_official 133:d4dda5c437f0 3234 if((uint32_t)SDIO_GetCommandResponse(hsd->Instance) != SD_CMD_SEND_STATUS)
mbed_official 133:d4dda5c437f0 3235 {
mbed_official 133:d4dda5c437f0 3236 errorState = SD_ILLEGAL_CMD;
mbed_official 133:d4dda5c437f0 3237
mbed_official 133:d4dda5c437f0 3238 return errorState;
mbed_official 133:d4dda5c437f0 3239 }
mbed_official 133:d4dda5c437f0 3240
mbed_official 133:d4dda5c437f0 3241 /* Clear all the static flags */
mbed_official 133:d4dda5c437f0 3242 __HAL_SD_SDIO_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
mbed_official 133:d4dda5c437f0 3243
mbed_official 133:d4dda5c437f0 3244
mbed_official 133:d4dda5c437f0 3245 /* We have received response, retrieve it for analysis */
mbed_official 133:d4dda5c437f0 3246 responseR1 = SDIO_GetResponse(SDIO_RESP1);
mbed_official 133:d4dda5c437f0 3247
mbed_official 133:d4dda5c437f0 3248 /* Find out card status */
mbed_official 133:d4dda5c437f0 3249 *pStatus = (uint8_t)((responseR1 >> 9) & 0x0000000F);
mbed_official 133:d4dda5c437f0 3250
mbed_official 133:d4dda5c437f0 3251 if((responseR1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
mbed_official 133:d4dda5c437f0 3252 {
mbed_official 133:d4dda5c437f0 3253 return errorState;
mbed_official 133:d4dda5c437f0 3254 }
mbed_official 133:d4dda5c437f0 3255
mbed_official 133:d4dda5c437f0 3256 if((responseR1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE)
mbed_official 133:d4dda5c437f0 3257 {
mbed_official 133:d4dda5c437f0 3258 return(SD_ADDR_OUT_OF_RANGE);
mbed_official 133:d4dda5c437f0 3259 }
mbed_official 133:d4dda5c437f0 3260
mbed_official 133:d4dda5c437f0 3261 if((responseR1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED)
mbed_official 133:d4dda5c437f0 3262 {
mbed_official 133:d4dda5c437f0 3263 return(SD_ADDR_MISALIGNED);
mbed_official 133:d4dda5c437f0 3264 }
mbed_official 133:d4dda5c437f0 3265
mbed_official 133:d4dda5c437f0 3266 if((responseR1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR)
mbed_official 133:d4dda5c437f0 3267 {
mbed_official 133:d4dda5c437f0 3268 return(SD_BLOCK_LEN_ERR);
mbed_official 133:d4dda5c437f0 3269 }
mbed_official 133:d4dda5c437f0 3270
mbed_official 133:d4dda5c437f0 3271 if((responseR1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR)
mbed_official 133:d4dda5c437f0 3272 {
mbed_official 133:d4dda5c437f0 3273 return(SD_ERASE_SEQ_ERR);
mbed_official 133:d4dda5c437f0 3274 }
mbed_official 133:d4dda5c437f0 3275
mbed_official 133:d4dda5c437f0 3276 if((responseR1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM)
mbed_official 133:d4dda5c437f0 3277 {
mbed_official 133:d4dda5c437f0 3278 return(SD_BAD_ERASE_PARAM);
mbed_official 133:d4dda5c437f0 3279 }
mbed_official 133:d4dda5c437f0 3280
mbed_official 133:d4dda5c437f0 3281 if((responseR1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION)
mbed_official 133:d4dda5c437f0 3282 {
mbed_official 133:d4dda5c437f0 3283 return(SD_WRITE_PROT_VIOLATION);
mbed_official 133:d4dda5c437f0 3284 }
mbed_official 133:d4dda5c437f0 3285
mbed_official 133:d4dda5c437f0 3286 if((responseR1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED)
mbed_official 133:d4dda5c437f0 3287 {
mbed_official 133:d4dda5c437f0 3288 return(SD_LOCK_UNLOCK_FAILED);
mbed_official 133:d4dda5c437f0 3289 }
mbed_official 133:d4dda5c437f0 3290
mbed_official 133:d4dda5c437f0 3291 if((responseR1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED)
mbed_official 133:d4dda5c437f0 3292 {
mbed_official 133:d4dda5c437f0 3293 return(SD_COM_CRC_FAILED);
mbed_official 133:d4dda5c437f0 3294 }
mbed_official 133:d4dda5c437f0 3295
mbed_official 133:d4dda5c437f0 3296 if((responseR1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD)
mbed_official 133:d4dda5c437f0 3297 {
mbed_official 133:d4dda5c437f0 3298 return(SD_ILLEGAL_CMD);
mbed_official 133:d4dda5c437f0 3299 }
mbed_official 133:d4dda5c437f0 3300
mbed_official 133:d4dda5c437f0 3301 if((responseR1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED)
mbed_official 133:d4dda5c437f0 3302 {
mbed_official 133:d4dda5c437f0 3303 return(SD_CARD_ECC_FAILED);
mbed_official 133:d4dda5c437f0 3304 }
mbed_official 133:d4dda5c437f0 3305
mbed_official 133:d4dda5c437f0 3306 if((responseR1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR)
mbed_official 133:d4dda5c437f0 3307 {
mbed_official 133:d4dda5c437f0 3308 return(SD_CC_ERROR);
mbed_official 133:d4dda5c437f0 3309 }
mbed_official 133:d4dda5c437f0 3310
mbed_official 133:d4dda5c437f0 3311 if((responseR1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR)
mbed_official 133:d4dda5c437f0 3312 {
mbed_official 133:d4dda5c437f0 3313 return(SD_GENERAL_UNKNOWN_ERROR);
mbed_official 133:d4dda5c437f0 3314 }
mbed_official 133:d4dda5c437f0 3315
mbed_official 133:d4dda5c437f0 3316 if((responseR1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN)
mbed_official 133:d4dda5c437f0 3317 {
mbed_official 133:d4dda5c437f0 3318 return(SD_STREAM_READ_UNDERRUN);
mbed_official 133:d4dda5c437f0 3319 }
mbed_official 133:d4dda5c437f0 3320
mbed_official 133:d4dda5c437f0 3321 if((responseR1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN)
mbed_official 133:d4dda5c437f0 3322 {
mbed_official 133:d4dda5c437f0 3323 return(SD_STREAM_WRITE_OVERRUN);
mbed_official 133:d4dda5c437f0 3324 }
mbed_official 133:d4dda5c437f0 3325
mbed_official 133:d4dda5c437f0 3326 if((responseR1 & SD_OCR_CID_CSD_OVERWRIETE) == SD_OCR_CID_CSD_OVERWRIETE)
mbed_official 133:d4dda5c437f0 3327 {
mbed_official 133:d4dda5c437f0 3328 return(SD_CID_CSD_OVERWRITE);
mbed_official 133:d4dda5c437f0 3329 }
mbed_official 133:d4dda5c437f0 3330
mbed_official 133:d4dda5c437f0 3331 if((responseR1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP)
mbed_official 133:d4dda5c437f0 3332 {
mbed_official 133:d4dda5c437f0 3333 return(SD_WP_ERASE_SKIP);
mbed_official 133:d4dda5c437f0 3334 }
mbed_official 133:d4dda5c437f0 3335
mbed_official 133:d4dda5c437f0 3336 if((responseR1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED)
mbed_official 133:d4dda5c437f0 3337 {
mbed_official 133:d4dda5c437f0 3338 return(SD_CARD_ECC_DISABLED);
mbed_official 133:d4dda5c437f0 3339 }
mbed_official 133:d4dda5c437f0 3340
mbed_official 133:d4dda5c437f0 3341 if((responseR1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET)
mbed_official 133:d4dda5c437f0 3342 {
mbed_official 133:d4dda5c437f0 3343 return(SD_ERASE_RESET);
mbed_official 133:d4dda5c437f0 3344 }
mbed_official 133:d4dda5c437f0 3345
mbed_official 133:d4dda5c437f0 3346 if((responseR1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR)
mbed_official 133:d4dda5c437f0 3347 {
mbed_official 133:d4dda5c437f0 3348 return(SD_AKE_SEQ_ERROR);
mbed_official 133:d4dda5c437f0 3349 }
mbed_official 133:d4dda5c437f0 3350
mbed_official 133:d4dda5c437f0 3351 return errorState;
mbed_official 133:d4dda5c437f0 3352 }
mbed_official 133:d4dda5c437f0 3353
mbed_official 133:d4dda5c437f0 3354 /**
mbed_official 133:d4dda5c437f0 3355 * @}
mbed_official 133:d4dda5c437f0 3356 */
mbed_official 133:d4dda5c437f0 3357
mbed_official 133:d4dda5c437f0 3358 #endif /* HAL_SD_MODULE_ENABLED */
mbed_official 133:d4dda5c437f0 3359 /**
mbed_official 133:d4dda5c437f0 3360 * @}
mbed_official 133:d4dda5c437f0 3361 */
mbed_official 133:d4dda5c437f0 3362
mbed_official 133:d4dda5c437f0 3363 /**
mbed_official 133:d4dda5c437f0 3364 * @}
mbed_official 133:d4dda5c437f0 3365 */
mbed_official 133:d4dda5c437f0 3366
mbed_official 133:d4dda5c437f0 3367 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/