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