TUKS MCU Introductory course / TUKS-COURSE-THERMOMETER

Fork of TUKS-COURSE-TIMER by TUKS MCU Introductory course

Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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