mbed library sources

Dependents:   Marvino mbot

Fork of mbed-src by mbed official

Committer:
jaerts
Date:
Tue Dec 22 13:22:16 2015 +0000
Revision:
637:ed69428d4850
Parent:
610:813dcc80987e
Add very shady LPC1768 CAN Filter implementation

Who changed what in which revision?

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