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