Initial commit
mbed-dev-master/targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_hal_sd.c@0:bb348c97df44, 2020-09-16 (annotated)
- Committer:
- lypinator
- Date:
- Wed Sep 16 01:11:49 2020 +0000
- Revision:
- 0:bb348c97df44
Added PWM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lypinator | 0:bb348c97df44 | 1 | /** |
lypinator | 0:bb348c97df44 | 2 | ****************************************************************************** |
lypinator | 0:bb348c97df44 | 3 | * @file stm32f4xx_hal_sd.c |
lypinator | 0:bb348c97df44 | 4 | * @author MCD Application Team |
lypinator | 0:bb348c97df44 | 5 | * @brief SD card HAL module driver. |
lypinator | 0:bb348c97df44 | 6 | * This file provides firmware functions to manage the following |
lypinator | 0:bb348c97df44 | 7 | * functionalities of the Secure Digital (SD) peripheral: |
lypinator | 0:bb348c97df44 | 8 | * + Initialization and de-initialization functions |
lypinator | 0:bb348c97df44 | 9 | * + IO operation functions |
lypinator | 0:bb348c97df44 | 10 | * + Peripheral Control functions |
lypinator | 0:bb348c97df44 | 11 | * + SD card Control functions |
lypinator | 0:bb348c97df44 | 12 | * |
lypinator | 0:bb348c97df44 | 13 | @verbatim |
lypinator | 0:bb348c97df44 | 14 | ============================================================================== |
lypinator | 0:bb348c97df44 | 15 | ##### How to use this driver ##### |
lypinator | 0:bb348c97df44 | 16 | ============================================================================== |
lypinator | 0:bb348c97df44 | 17 | [..] |
lypinator | 0:bb348c97df44 | 18 | This driver implements a high level communication layer for read and write from/to |
lypinator | 0:bb348c97df44 | 19 | this memory. The needed STM32 hardware resources (SDIO and GPIO) are performed by |
lypinator | 0:bb348c97df44 | 20 | the user in HAL_SD_MspInit() function (MSP layer). |
lypinator | 0:bb348c97df44 | 21 | Basically, the MSP layer configuration should be the same as we provide in the |
lypinator | 0:bb348c97df44 | 22 | examples. |
lypinator | 0:bb348c97df44 | 23 | You can easily tailor this configuration according to hardware resources. |
lypinator | 0:bb348c97df44 | 24 | |
lypinator | 0:bb348c97df44 | 25 | [..] |
lypinator | 0:bb348c97df44 | 26 | This driver is a generic layered driver for SDIO memories which uses the HAL |
lypinator | 0:bb348c97df44 | 27 | SDIO driver functions to interface with SD and uSD cards devices. |
lypinator | 0:bb348c97df44 | 28 | It is used as follows: |
lypinator | 0:bb348c97df44 | 29 | |
lypinator | 0:bb348c97df44 | 30 | (#)Initialize the SDIO low level resources by implement the HAL_SD_MspInit() API: |
lypinator | 0:bb348c97df44 | 31 | (##) Enable the SDIO interface clock using __HAL_RCC_SDIO_CLK_ENABLE(); |
lypinator | 0:bb348c97df44 | 32 | (##) SDIO pins configuration for SD card |
lypinator | 0:bb348c97df44 | 33 | (+++) Enable the clock for the SDIO GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE(); |
lypinator | 0:bb348c97df44 | 34 | (+++) Configure these SDIO pins as alternate function pull-up using HAL_GPIO_Init() |
lypinator | 0:bb348c97df44 | 35 | and according to your pin assignment; |
lypinator | 0:bb348c97df44 | 36 | (##) DMA Configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA() |
lypinator | 0:bb348c97df44 | 37 | and HAL_SD_WriteBlocks_DMA() APIs). |
lypinator | 0:bb348c97df44 | 38 | (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE(); |
lypinator | 0:bb348c97df44 | 39 | (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled. |
lypinator | 0:bb348c97df44 | 40 | (##) NVIC configuration if you need to use interrupt process when using DMA transfer. |
lypinator | 0:bb348c97df44 | 41 | (+++) Configure the SDIO and DMA interrupt priorities using functions |
lypinator | 0:bb348c97df44 | 42 | HAL_NVIC_SetPriority(); DMA priority is superior to SDIO's priority |
lypinator | 0:bb348c97df44 | 43 | (+++) Enable the NVIC DMA and SDIO IRQs using function HAL_NVIC_EnableIRQ() |
lypinator | 0:bb348c97df44 | 44 | (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT() |
lypinator | 0:bb348c97df44 | 45 | and __HAL_SD_DISABLE_IT() inside the communication process. |
lypinator | 0:bb348c97df44 | 46 | (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_GET_IT() |
lypinator | 0:bb348c97df44 | 47 | and __HAL_SD_CLEAR_IT() |
lypinator | 0:bb348c97df44 | 48 | (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT() |
lypinator | 0:bb348c97df44 | 49 | and HAL_SD_WriteBlocks_IT() APIs). |
lypinator | 0:bb348c97df44 | 50 | (+++) Configure the SDIO interrupt priorities using function |
lypinator | 0:bb348c97df44 | 51 | HAL_NVIC_SetPriority(); |
lypinator | 0:bb348c97df44 | 52 | (+++) Enable the NVIC SDIO IRQs using function HAL_NVIC_EnableIRQ() |
lypinator | 0:bb348c97df44 | 53 | (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT() |
lypinator | 0:bb348c97df44 | 54 | and __HAL_SD_DISABLE_IT() inside the communication process. |
lypinator | 0:bb348c97df44 | 55 | (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_GET_IT() |
lypinator | 0:bb348c97df44 | 56 | and __HAL_SD_CLEAR_IT() |
lypinator | 0:bb348c97df44 | 57 | (#) At this stage, you can perform SD read/write/erase operations after SD card initialization |
lypinator | 0:bb348c97df44 | 58 | |
lypinator | 0:bb348c97df44 | 59 | |
lypinator | 0:bb348c97df44 | 60 | *** SD Card Initialization and configuration *** |
lypinator | 0:bb348c97df44 | 61 | ================================================ |
lypinator | 0:bb348c97df44 | 62 | [..] |
lypinator | 0:bb348c97df44 | 63 | To initialize the SD Card, use the HAL_SD_Init() function. It Initializes |
lypinator | 0:bb348c97df44 | 64 | SDIO IP(STM32 side) and the SD Card, and put it into StandBy State (Ready for data transfer). |
lypinator | 0:bb348c97df44 | 65 | This function provide the following operations: |
lypinator | 0:bb348c97df44 | 66 | |
lypinator | 0:bb348c97df44 | 67 | (#) Initialize the SDIO peripheral interface with defaullt configuration. |
lypinator | 0:bb348c97df44 | 68 | The initialization process is done at 400KHz. You can change or adapt |
lypinator | 0:bb348c97df44 | 69 | this frequency by adjusting the "ClockDiv" field. |
lypinator | 0:bb348c97df44 | 70 | The SD Card frequency (SDIO_CK) is computed as follows: |
lypinator | 0:bb348c97df44 | 71 | |
lypinator | 0:bb348c97df44 | 72 | SDIO_CK = SDIOCLK / (ClockDiv + 2) |
lypinator | 0:bb348c97df44 | 73 | |
lypinator | 0:bb348c97df44 | 74 | In initialization mode and according to the SD Card standard, |
lypinator | 0:bb348c97df44 | 75 | make sure that the SDIO_CK frequency doesn't exceed 400KHz. |
lypinator | 0:bb348c97df44 | 76 | |
lypinator | 0:bb348c97df44 | 77 | This phase of initialization is done through SDIO_Init() and |
lypinator | 0:bb348c97df44 | 78 | SDIO_PowerState_ON() SDIO low level APIs. |
lypinator | 0:bb348c97df44 | 79 | |
lypinator | 0:bb348c97df44 | 80 | (#) Initialize the SD card. The API used is HAL_SD_InitCard(). |
lypinator | 0:bb348c97df44 | 81 | This phase allows the card initialization and identification |
lypinator | 0:bb348c97df44 | 82 | and check the SD Card type (Standard Capacity or High Capacity) |
lypinator | 0:bb348c97df44 | 83 | The initialization flow is compatible with SD standard. |
lypinator | 0:bb348c97df44 | 84 | |
lypinator | 0:bb348c97df44 | 85 | This API (HAL_SD_InitCard()) could be used also to reinitialize the card in case |
lypinator | 0:bb348c97df44 | 86 | of plug-off plug-in. |
lypinator | 0:bb348c97df44 | 87 | |
lypinator | 0:bb348c97df44 | 88 | (#) Configure the SD Card Data transfer frequency. By Default, the card transfer |
lypinator | 0:bb348c97df44 | 89 | frequency is set to 24MHz. You can change or adapt this frequency by adjusting |
lypinator | 0:bb348c97df44 | 90 | the "ClockDiv" field. |
lypinator | 0:bb348c97df44 | 91 | In transfer mode and according to the SD Card standard, make sure that the |
lypinator | 0:bb348c97df44 | 92 | SDIO_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch. |
lypinator | 0:bb348c97df44 | 93 | To be able to use a frequency higher than 24MHz, you should use the SDIO |
lypinator | 0:bb348c97df44 | 94 | peripheral in bypass mode. Refer to the corresponding reference manual |
lypinator | 0:bb348c97df44 | 95 | for more details. |
lypinator | 0:bb348c97df44 | 96 | |
lypinator | 0:bb348c97df44 | 97 | (#) Select the corresponding SD Card according to the address read with the step 2. |
lypinator | 0:bb348c97df44 | 98 | |
lypinator | 0:bb348c97df44 | 99 | (#) Configure the SD Card in wide bus mode: 4-bits data. |
lypinator | 0:bb348c97df44 | 100 | |
lypinator | 0:bb348c97df44 | 101 | *** SD Card Read operation *** |
lypinator | 0:bb348c97df44 | 102 | ============================== |
lypinator | 0:bb348c97df44 | 103 | [..] |
lypinator | 0:bb348c97df44 | 104 | (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks(). |
lypinator | 0:bb348c97df44 | 105 | This function allows the read of 512 bytes blocks. |
lypinator | 0:bb348c97df44 | 106 | You can choose either one block read operation or multiple block read operation |
lypinator | 0:bb348c97df44 | 107 | by adjusting the "NumberOfBlocks" parameter. |
lypinator | 0:bb348c97df44 | 108 | After this, you have to ensure that the transfer is done correctly. The check is done |
lypinator | 0:bb348c97df44 | 109 | through HAL_SD_GetCardState() function for SD card state. |
lypinator | 0:bb348c97df44 | 110 | |
lypinator | 0:bb348c97df44 | 111 | (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA(). |
lypinator | 0:bb348c97df44 | 112 | This function allows the read of 512 bytes blocks. |
lypinator | 0:bb348c97df44 | 113 | You can choose either one block read operation or multiple block read operation |
lypinator | 0:bb348c97df44 | 114 | by adjusting the "NumberOfBlocks" parameter. |
lypinator | 0:bb348c97df44 | 115 | After this, you have to ensure that the transfer is done correctly. The check is done |
lypinator | 0:bb348c97df44 | 116 | through HAL_SD_GetCardState() function for SD card state. |
lypinator | 0:bb348c97df44 | 117 | You could also check the DMA transfer process through the SD Rx interrupt event. |
lypinator | 0:bb348c97df44 | 118 | |
lypinator | 0:bb348c97df44 | 119 | (+) You can read from SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT(). |
lypinator | 0:bb348c97df44 | 120 | This function allows the read of 512 bytes blocks. |
lypinator | 0:bb348c97df44 | 121 | You can choose either one block read operation or multiple block read operation |
lypinator | 0:bb348c97df44 | 122 | by adjusting the "NumberOfBlocks" parameter. |
lypinator | 0:bb348c97df44 | 123 | After this, you have to ensure that the transfer is done correctly. The check is done |
lypinator | 0:bb348c97df44 | 124 | through HAL_SD_GetCardState() function for SD card state. |
lypinator | 0:bb348c97df44 | 125 | You could also check the IT transfer process through the SD Rx interrupt event. |
lypinator | 0:bb348c97df44 | 126 | |
lypinator | 0:bb348c97df44 | 127 | *** SD Card Write operation *** |
lypinator | 0:bb348c97df44 | 128 | =============================== |
lypinator | 0:bb348c97df44 | 129 | [..] |
lypinator | 0:bb348c97df44 | 130 | (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks(). |
lypinator | 0:bb348c97df44 | 131 | This function allows the read of 512 bytes blocks. |
lypinator | 0:bb348c97df44 | 132 | You can choose either one block read operation or multiple block read operation |
lypinator | 0:bb348c97df44 | 133 | by adjusting the "NumberOfBlocks" parameter. |
lypinator | 0:bb348c97df44 | 134 | After this, you have to ensure that the transfer is done correctly. The check is done |
lypinator | 0:bb348c97df44 | 135 | through HAL_SD_GetCardState() function for SD card state. |
lypinator | 0:bb348c97df44 | 136 | |
lypinator | 0:bb348c97df44 | 137 | (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA(). |
lypinator | 0:bb348c97df44 | 138 | This function allows the read of 512 bytes blocks. |
lypinator | 0:bb348c97df44 | 139 | You can choose either one block read operation or multiple block read operation |
lypinator | 0:bb348c97df44 | 140 | by adjusting the "NumberOfBlocks" parameter. |
lypinator | 0:bb348c97df44 | 141 | After this, you have to ensure that the transfer is done correctly. The check is done |
lypinator | 0:bb348c97df44 | 142 | through HAL_SD_GetCardState() function for SD card state. |
lypinator | 0:bb348c97df44 | 143 | You could also check the DMA transfer process through the SD Tx interrupt event. |
lypinator | 0:bb348c97df44 | 144 | |
lypinator | 0:bb348c97df44 | 145 | (+) You can write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT(). |
lypinator | 0:bb348c97df44 | 146 | This function allows the read of 512 bytes blocks. |
lypinator | 0:bb348c97df44 | 147 | You can choose either one block read operation or multiple block read operation |
lypinator | 0:bb348c97df44 | 148 | by adjusting the "NumberOfBlocks" parameter. |
lypinator | 0:bb348c97df44 | 149 | After this, you have to ensure that the transfer is done correctly. The check is done |
lypinator | 0:bb348c97df44 | 150 | through HAL_SD_GetCardState() function for SD card state. |
lypinator | 0:bb348c97df44 | 151 | You could also check the IT transfer process through the SD Tx interrupt event. |
lypinator | 0:bb348c97df44 | 152 | |
lypinator | 0:bb348c97df44 | 153 | *** SD card status *** |
lypinator | 0:bb348c97df44 | 154 | ====================== |
lypinator | 0:bb348c97df44 | 155 | [..] |
lypinator | 0:bb348c97df44 | 156 | (+) The SD Status contains status bits that are related to the SD Memory |
lypinator | 0:bb348c97df44 | 157 | Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus(). |
lypinator | 0:bb348c97df44 | 158 | |
lypinator | 0:bb348c97df44 | 159 | *** SD card information *** |
lypinator | 0:bb348c97df44 | 160 | =========================== |
lypinator | 0:bb348c97df44 | 161 | [..] |
lypinator | 0:bb348c97df44 | 162 | (+) To get SD card information, you can use the function HAL_SD_GetCardInfo(). |
lypinator | 0:bb348c97df44 | 163 | It returns useful information about the SD card such as block size, card type, |
lypinator | 0:bb348c97df44 | 164 | block number ... |
lypinator | 0:bb348c97df44 | 165 | |
lypinator | 0:bb348c97df44 | 166 | *** SD card CSD register *** |
lypinator | 0:bb348c97df44 | 167 | ============================ |
lypinator | 0:bb348c97df44 | 168 | [..] |
lypinator | 0:bb348c97df44 | 169 | (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register. |
lypinator | 0:bb348c97df44 | 170 | Some of the CSD parameters are useful for card initialization and identification. |
lypinator | 0:bb348c97df44 | 171 | |
lypinator | 0:bb348c97df44 | 172 | *** SD card CID register *** |
lypinator | 0:bb348c97df44 | 173 | ============================ |
lypinator | 0:bb348c97df44 | 174 | [..] |
lypinator | 0:bb348c97df44 | 175 | (+) The HAL_SD_GetCardCID() API allows to get the parameters of the CID register. |
lypinator | 0:bb348c97df44 | 176 | Some of the CSD parameters are useful for card initialization and identification. |
lypinator | 0:bb348c97df44 | 177 | |
lypinator | 0:bb348c97df44 | 178 | *** SD HAL driver macros list *** |
lypinator | 0:bb348c97df44 | 179 | ================================== |
lypinator | 0:bb348c97df44 | 180 | [..] |
lypinator | 0:bb348c97df44 | 181 | Below the list of most used macros in SD HAL driver. |
lypinator | 0:bb348c97df44 | 182 | |
lypinator | 0:bb348c97df44 | 183 | (+) __HAL_SD_ENABLE : Enable the SD device |
lypinator | 0:bb348c97df44 | 184 | (+) __HAL_SD_DISABLE : Disable the SD device |
lypinator | 0:bb348c97df44 | 185 | (+) __HAL_SD_DMA_ENABLE: Enable the SDIO DMA transfer |
lypinator | 0:bb348c97df44 | 186 | (+) __HAL_SD_DMA_DISABLE: Disable the SDIO DMA transfer |
lypinator | 0:bb348c97df44 | 187 | (+) __HAL_SD_ENABLE_IT: Enable the SD device interrupt |
lypinator | 0:bb348c97df44 | 188 | (+) __HAL_SD_DISABLE_IT: Disable the SD device interrupt |
lypinator | 0:bb348c97df44 | 189 | (+) __HAL_SD_GET_FLAG:Check whether the specified SD flag is set or not |
lypinator | 0:bb348c97df44 | 190 | (+) __HAL_SD_CLEAR_FLAG: Clear the SD's pending flags |
lypinator | 0:bb348c97df44 | 191 | |
lypinator | 0:bb348c97df44 | 192 | [..] |
lypinator | 0:bb348c97df44 | 193 | (@) You can refer to the SD HAL driver header file for more useful macros |
lypinator | 0:bb348c97df44 | 194 | |
lypinator | 0:bb348c97df44 | 195 | @endverbatim |
lypinator | 0:bb348c97df44 | 196 | ****************************************************************************** |
lypinator | 0:bb348c97df44 | 197 | * @attention |
lypinator | 0:bb348c97df44 | 198 | * |
lypinator | 0:bb348c97df44 | 199 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
lypinator | 0:bb348c97df44 | 200 | * |
lypinator | 0:bb348c97df44 | 201 | * Redistribution and use in source and binary forms, with or without modification, |
lypinator | 0:bb348c97df44 | 202 | * are permitted provided that the following conditions are met: |
lypinator | 0:bb348c97df44 | 203 | * 1. Redistributions of source code must retain the above copyright notice, |
lypinator | 0:bb348c97df44 | 204 | * this list of conditions and the following disclaimer. |
lypinator | 0:bb348c97df44 | 205 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
lypinator | 0:bb348c97df44 | 206 | * this list of conditions and the following disclaimer in the documentation |
lypinator | 0:bb348c97df44 | 207 | * and/or other materials provided with the distribution. |
lypinator | 0:bb348c97df44 | 208 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
lypinator | 0:bb348c97df44 | 209 | * may be used to endorse or promote products derived from this software |
lypinator | 0:bb348c97df44 | 210 | * without specific prior written permission. |
lypinator | 0:bb348c97df44 | 211 | * |
lypinator | 0:bb348c97df44 | 212 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
lypinator | 0:bb348c97df44 | 213 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
lypinator | 0:bb348c97df44 | 214 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
lypinator | 0:bb348c97df44 | 215 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
lypinator | 0:bb348c97df44 | 216 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
lypinator | 0:bb348c97df44 | 217 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
lypinator | 0:bb348c97df44 | 218 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
lypinator | 0:bb348c97df44 | 219 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
lypinator | 0:bb348c97df44 | 220 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
lypinator | 0:bb348c97df44 | 221 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
lypinator | 0:bb348c97df44 | 222 | * |
lypinator | 0:bb348c97df44 | 223 | ****************************************************************************** |
lypinator | 0:bb348c97df44 | 224 | */ |
lypinator | 0:bb348c97df44 | 225 | |
lypinator | 0:bb348c97df44 | 226 | /* Includes ------------------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 227 | #include "stm32f4xx_hal.h" |
lypinator | 0:bb348c97df44 | 228 | |
lypinator | 0:bb348c97df44 | 229 | /** @addtogroup STM32F4xx_HAL_Driver |
lypinator | 0:bb348c97df44 | 230 | * @{ |
lypinator | 0:bb348c97df44 | 231 | */ |
lypinator | 0:bb348c97df44 | 232 | |
lypinator | 0:bb348c97df44 | 233 | /** @addtogroup SD |
lypinator | 0:bb348c97df44 | 234 | * @{ |
lypinator | 0:bb348c97df44 | 235 | */ |
lypinator | 0:bb348c97df44 | 236 | |
lypinator | 0:bb348c97df44 | 237 | #ifdef HAL_SD_MODULE_ENABLED |
lypinator | 0:bb348c97df44 | 238 | |
lypinator | 0:bb348c97df44 | 239 | #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || \ |
lypinator | 0:bb348c97df44 | 240 | defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \ |
lypinator | 0:bb348c97df44 | 241 | defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE) || defined(STM32F446xx) || \ |
lypinator | 0:bb348c97df44 | 242 | defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \ |
lypinator | 0:bb348c97df44 | 243 | defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx) |
lypinator | 0:bb348c97df44 | 244 | |
lypinator | 0:bb348c97df44 | 245 | /* Private typedef -----------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 246 | /* Private define ------------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 247 | /** @addtogroup SD_Private_Defines |
lypinator | 0:bb348c97df44 | 248 | * @{ |
lypinator | 0:bb348c97df44 | 249 | */ |
lypinator | 0:bb348c97df44 | 250 | |
lypinator | 0:bb348c97df44 | 251 | /** |
lypinator | 0:bb348c97df44 | 252 | * @} |
lypinator | 0:bb348c97df44 | 253 | */ |
lypinator | 0:bb348c97df44 | 254 | |
lypinator | 0:bb348c97df44 | 255 | /* Private macro -------------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 256 | /* Private variables ---------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 257 | /* Private function prototypes -----------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 258 | /* Private functions ---------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 259 | /** @defgroup SD_Private_Functions SD Private Functions |
lypinator | 0:bb348c97df44 | 260 | * @{ |
lypinator | 0:bb348c97df44 | 261 | */ |
lypinator | 0:bb348c97df44 | 262 | static uint32_t SD_InitCard(SD_HandleTypeDef *hsd); |
lypinator | 0:bb348c97df44 | 263 | static uint32_t SD_PowerON(SD_HandleTypeDef *hsd); |
lypinator | 0:bb348c97df44 | 264 | static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus); |
lypinator | 0:bb348c97df44 | 265 | static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus); |
lypinator | 0:bb348c97df44 | 266 | static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd); |
lypinator | 0:bb348c97df44 | 267 | static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd); |
lypinator | 0:bb348c97df44 | 268 | static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR); |
lypinator | 0:bb348c97df44 | 269 | static HAL_StatusTypeDef SD_PowerOFF(SD_HandleTypeDef *hsd); |
lypinator | 0:bb348c97df44 | 270 | static HAL_StatusTypeDef SD_Write_IT(SD_HandleTypeDef *hsd); |
lypinator | 0:bb348c97df44 | 271 | static HAL_StatusTypeDef SD_Read_IT(SD_HandleTypeDef *hsd); |
lypinator | 0:bb348c97df44 | 272 | static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 273 | static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 274 | static void SD_DMAError(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 275 | static void SD_DMATxAbort(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 276 | static void SD_DMARxAbort(DMA_HandleTypeDef *hdma); |
lypinator | 0:bb348c97df44 | 277 | /** |
lypinator | 0:bb348c97df44 | 278 | * @} |
lypinator | 0:bb348c97df44 | 279 | */ |
lypinator | 0:bb348c97df44 | 280 | |
lypinator | 0:bb348c97df44 | 281 | /* Exported functions --------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 282 | /** @addtogroup SD_Exported_Functions |
lypinator | 0:bb348c97df44 | 283 | * @{ |
lypinator | 0:bb348c97df44 | 284 | */ |
lypinator | 0:bb348c97df44 | 285 | |
lypinator | 0:bb348c97df44 | 286 | /** @addtogroup SD_Exported_Functions_Group1 |
lypinator | 0:bb348c97df44 | 287 | * @brief Initialization and de-initialization functions |
lypinator | 0:bb348c97df44 | 288 | * |
lypinator | 0:bb348c97df44 | 289 | @verbatim |
lypinator | 0:bb348c97df44 | 290 | ============================================================================== |
lypinator | 0:bb348c97df44 | 291 | ##### Initialization and de-initialization functions ##### |
lypinator | 0:bb348c97df44 | 292 | ============================================================================== |
lypinator | 0:bb348c97df44 | 293 | [..] |
lypinator | 0:bb348c97df44 | 294 | This section provides functions allowing to initialize/de-initialize the SD |
lypinator | 0:bb348c97df44 | 295 | card device to be ready for use. |
lypinator | 0:bb348c97df44 | 296 | |
lypinator | 0:bb348c97df44 | 297 | @endverbatim |
lypinator | 0:bb348c97df44 | 298 | * @{ |
lypinator | 0:bb348c97df44 | 299 | */ |
lypinator | 0:bb348c97df44 | 300 | |
lypinator | 0:bb348c97df44 | 301 | /** |
lypinator | 0:bb348c97df44 | 302 | * @brief Initializes the SD according to the specified parameters in the |
lypinator | 0:bb348c97df44 | 303 | SD_HandleTypeDef and create the associated handle. |
lypinator | 0:bb348c97df44 | 304 | * @param hsd Pointer to the SD handle |
lypinator | 0:bb348c97df44 | 305 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 306 | */ |
lypinator | 0:bb348c97df44 | 307 | HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 308 | { |
lypinator | 0:bb348c97df44 | 309 | /* Check the SD handle allocation */ |
lypinator | 0:bb348c97df44 | 310 | if(hsd == NULL) |
lypinator | 0:bb348c97df44 | 311 | { |
lypinator | 0:bb348c97df44 | 312 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 313 | } |
lypinator | 0:bb348c97df44 | 314 | |
lypinator | 0:bb348c97df44 | 315 | /* Check the parameters */ |
lypinator | 0:bb348c97df44 | 316 | assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance)); |
lypinator | 0:bb348c97df44 | 317 | assert_param(IS_SDIO_CLOCK_EDGE(hsd->Init.ClockEdge)); |
lypinator | 0:bb348c97df44 | 318 | assert_param(IS_SDIO_CLOCK_BYPASS(hsd->Init.ClockBypass)); |
lypinator | 0:bb348c97df44 | 319 | assert_param(IS_SDIO_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave)); |
lypinator | 0:bb348c97df44 | 320 | assert_param(IS_SDIO_BUS_WIDE(hsd->Init.BusWide)); |
lypinator | 0:bb348c97df44 | 321 | assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl)); |
lypinator | 0:bb348c97df44 | 322 | assert_param(IS_SDIO_CLKDIV(hsd->Init.ClockDiv)); |
lypinator | 0:bb348c97df44 | 323 | |
lypinator | 0:bb348c97df44 | 324 | if(hsd->State == HAL_SD_STATE_RESET) |
lypinator | 0:bb348c97df44 | 325 | { |
lypinator | 0:bb348c97df44 | 326 | /* Allocate lock resource and initialize it */ |
lypinator | 0:bb348c97df44 | 327 | hsd->Lock = HAL_UNLOCKED; |
lypinator | 0:bb348c97df44 | 328 | /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */ |
lypinator | 0:bb348c97df44 | 329 | HAL_SD_MspInit(hsd); |
lypinator | 0:bb348c97df44 | 330 | } |
lypinator | 0:bb348c97df44 | 331 | |
lypinator | 0:bb348c97df44 | 332 | hsd->State = HAL_SD_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 333 | |
lypinator | 0:bb348c97df44 | 334 | /* Initialize the Card parameters */ |
lypinator | 0:bb348c97df44 | 335 | HAL_SD_InitCard(hsd); |
lypinator | 0:bb348c97df44 | 336 | |
lypinator | 0:bb348c97df44 | 337 | /* Initialize the error code */ |
lypinator | 0:bb348c97df44 | 338 | hsd->ErrorCode = HAL_DMA_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 339 | |
lypinator | 0:bb348c97df44 | 340 | /* Initialize the SD operation */ |
lypinator | 0:bb348c97df44 | 341 | hsd->Context = SD_CONTEXT_NONE; |
lypinator | 0:bb348c97df44 | 342 | |
lypinator | 0:bb348c97df44 | 343 | /* Initialize the SD state */ |
lypinator | 0:bb348c97df44 | 344 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 345 | |
lypinator | 0:bb348c97df44 | 346 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 347 | } |
lypinator | 0:bb348c97df44 | 348 | |
lypinator | 0:bb348c97df44 | 349 | /** |
lypinator | 0:bb348c97df44 | 350 | * @brief Initializes the SD Card. |
lypinator | 0:bb348c97df44 | 351 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 352 | * @note This function initializes the SD card. It could be used when a card |
lypinator | 0:bb348c97df44 | 353 | re-initialization is needed. |
lypinator | 0:bb348c97df44 | 354 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 355 | */ |
lypinator | 0:bb348c97df44 | 356 | HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 357 | { |
lypinator | 0:bb348c97df44 | 358 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 359 | SD_InitTypeDef Init; |
lypinator | 0:bb348c97df44 | 360 | |
lypinator | 0:bb348c97df44 | 361 | /* Default SDIO peripheral configuration for SD card initialization */ |
lypinator | 0:bb348c97df44 | 362 | Init.ClockEdge = SDIO_CLOCK_EDGE_RISING; |
lypinator | 0:bb348c97df44 | 363 | Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE; |
lypinator | 0:bb348c97df44 | 364 | Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE; |
lypinator | 0:bb348c97df44 | 365 | Init.BusWide = SDIO_BUS_WIDE_1B; |
lypinator | 0:bb348c97df44 | 366 | Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE; |
lypinator | 0:bb348c97df44 | 367 | Init.ClockDiv = SDIO_INIT_CLK_DIV; |
lypinator | 0:bb348c97df44 | 368 | |
lypinator | 0:bb348c97df44 | 369 | /* Initialize SDIO peripheral interface with default configuration */ |
lypinator | 0:bb348c97df44 | 370 | SDIO_Init(hsd->Instance, Init); |
lypinator | 0:bb348c97df44 | 371 | |
lypinator | 0:bb348c97df44 | 372 | /* Disable SDIO Clock */ |
lypinator | 0:bb348c97df44 | 373 | __HAL_SD_DISABLE(hsd); |
lypinator | 0:bb348c97df44 | 374 | |
lypinator | 0:bb348c97df44 | 375 | /* Set Power State to ON */ |
lypinator | 0:bb348c97df44 | 376 | SDIO_PowerState_ON(hsd->Instance); |
lypinator | 0:bb348c97df44 | 377 | |
lypinator | 0:bb348c97df44 | 378 | /* Enable SDIO Clock */ |
lypinator | 0:bb348c97df44 | 379 | __HAL_SD_ENABLE(hsd); |
lypinator | 0:bb348c97df44 | 380 | |
lypinator | 0:bb348c97df44 | 381 | /* Required power up waiting time before starting the SD initialization |
lypinator | 0:bb348c97df44 | 382 | sequence */ |
lypinator | 0:bb348c97df44 | 383 | HAL_Delay(2U); |
lypinator | 0:bb348c97df44 | 384 | |
lypinator | 0:bb348c97df44 | 385 | /* Identify card operating voltage */ |
lypinator | 0:bb348c97df44 | 386 | errorstate = SD_PowerON(hsd); |
lypinator | 0:bb348c97df44 | 387 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 388 | { |
lypinator | 0:bb348c97df44 | 389 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 390 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 391 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 392 | } |
lypinator | 0:bb348c97df44 | 393 | |
lypinator | 0:bb348c97df44 | 394 | /* Card initialization */ |
lypinator | 0:bb348c97df44 | 395 | errorstate = SD_InitCard(hsd); |
lypinator | 0:bb348c97df44 | 396 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 397 | { |
lypinator | 0:bb348c97df44 | 398 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 399 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 400 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 401 | } |
lypinator | 0:bb348c97df44 | 402 | |
lypinator | 0:bb348c97df44 | 403 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 404 | } |
lypinator | 0:bb348c97df44 | 405 | |
lypinator | 0:bb348c97df44 | 406 | /** |
lypinator | 0:bb348c97df44 | 407 | * @brief De-Initializes the SD card. |
lypinator | 0:bb348c97df44 | 408 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 409 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 410 | */ |
lypinator | 0:bb348c97df44 | 411 | HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 412 | { |
lypinator | 0:bb348c97df44 | 413 | /* Check the SD handle allocation */ |
lypinator | 0:bb348c97df44 | 414 | if(hsd == NULL) |
lypinator | 0:bb348c97df44 | 415 | { |
lypinator | 0:bb348c97df44 | 416 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 417 | } |
lypinator | 0:bb348c97df44 | 418 | |
lypinator | 0:bb348c97df44 | 419 | /* Check the parameters */ |
lypinator | 0:bb348c97df44 | 420 | assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance)); |
lypinator | 0:bb348c97df44 | 421 | |
lypinator | 0:bb348c97df44 | 422 | hsd->State = HAL_SD_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 423 | |
lypinator | 0:bb348c97df44 | 424 | /* Set SD power state to off */ |
lypinator | 0:bb348c97df44 | 425 | SD_PowerOFF(hsd); |
lypinator | 0:bb348c97df44 | 426 | |
lypinator | 0:bb348c97df44 | 427 | /* De-Initialize the MSP layer */ |
lypinator | 0:bb348c97df44 | 428 | HAL_SD_MspDeInit(hsd); |
lypinator | 0:bb348c97df44 | 429 | |
lypinator | 0:bb348c97df44 | 430 | hsd->ErrorCode = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 431 | hsd->State = HAL_SD_STATE_RESET; |
lypinator | 0:bb348c97df44 | 432 | |
lypinator | 0:bb348c97df44 | 433 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 434 | } |
lypinator | 0:bb348c97df44 | 435 | |
lypinator | 0:bb348c97df44 | 436 | |
lypinator | 0:bb348c97df44 | 437 | /** |
lypinator | 0:bb348c97df44 | 438 | * @brief Initializes the SD MSP. |
lypinator | 0:bb348c97df44 | 439 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 440 | * @retval None |
lypinator | 0:bb348c97df44 | 441 | */ |
lypinator | 0:bb348c97df44 | 442 | __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 443 | { |
lypinator | 0:bb348c97df44 | 444 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 445 | UNUSED(hsd); |
lypinator | 0:bb348c97df44 | 446 | |
lypinator | 0:bb348c97df44 | 447 | /* NOTE : This function Should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 448 | the HAL_SD_MspInit could be implemented in the user file |
lypinator | 0:bb348c97df44 | 449 | */ |
lypinator | 0:bb348c97df44 | 450 | } |
lypinator | 0:bb348c97df44 | 451 | |
lypinator | 0:bb348c97df44 | 452 | /** |
lypinator | 0:bb348c97df44 | 453 | * @brief De-Initialize SD MSP. |
lypinator | 0:bb348c97df44 | 454 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 455 | * @retval None |
lypinator | 0:bb348c97df44 | 456 | */ |
lypinator | 0:bb348c97df44 | 457 | __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 458 | { |
lypinator | 0:bb348c97df44 | 459 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 460 | UNUSED(hsd); |
lypinator | 0:bb348c97df44 | 461 | |
lypinator | 0:bb348c97df44 | 462 | /* NOTE : This function Should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 463 | the HAL_SD_MspDeInit could be implemented in the user file |
lypinator | 0:bb348c97df44 | 464 | */ |
lypinator | 0:bb348c97df44 | 465 | } |
lypinator | 0:bb348c97df44 | 466 | |
lypinator | 0:bb348c97df44 | 467 | /** |
lypinator | 0:bb348c97df44 | 468 | * @} |
lypinator | 0:bb348c97df44 | 469 | */ |
lypinator | 0:bb348c97df44 | 470 | |
lypinator | 0:bb348c97df44 | 471 | /** @addtogroup SD_Exported_Functions_Group2 |
lypinator | 0:bb348c97df44 | 472 | * @brief Data transfer functions |
lypinator | 0:bb348c97df44 | 473 | * |
lypinator | 0:bb348c97df44 | 474 | @verbatim |
lypinator | 0:bb348c97df44 | 475 | ============================================================================== |
lypinator | 0:bb348c97df44 | 476 | ##### IO operation functions ##### |
lypinator | 0:bb348c97df44 | 477 | ============================================================================== |
lypinator | 0:bb348c97df44 | 478 | [..] |
lypinator | 0:bb348c97df44 | 479 | This subsection provides a set of functions allowing to manage the data |
lypinator | 0:bb348c97df44 | 480 | transfer from/to SD card. |
lypinator | 0:bb348c97df44 | 481 | |
lypinator | 0:bb348c97df44 | 482 | @endverbatim |
lypinator | 0:bb348c97df44 | 483 | * @{ |
lypinator | 0:bb348c97df44 | 484 | */ |
lypinator | 0:bb348c97df44 | 485 | |
lypinator | 0:bb348c97df44 | 486 | /** |
lypinator | 0:bb348c97df44 | 487 | * @brief Reads block(s) from a specified address in a card. The Data transfer |
lypinator | 0:bb348c97df44 | 488 | * is managed by polling mode. |
lypinator | 0:bb348c97df44 | 489 | * @note This API should be followed by a check on the card state through |
lypinator | 0:bb348c97df44 | 490 | * HAL_SD_GetCardState(). |
lypinator | 0:bb348c97df44 | 491 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 492 | * @param pData pointer to the buffer that will contain the received data |
lypinator | 0:bb348c97df44 | 493 | * @param BlockAdd Block Address from where data is to be read |
lypinator | 0:bb348c97df44 | 494 | * @param NumberOfBlocks Number of SD blocks to read |
lypinator | 0:bb348c97df44 | 495 | * @param Timeout Specify timeout value |
lypinator | 0:bb348c97df44 | 496 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 497 | */ |
lypinator | 0:bb348c97df44 | 498 | HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 499 | { |
lypinator | 0:bb348c97df44 | 500 | SDIO_DataInitTypeDef config; |
lypinator | 0:bb348c97df44 | 501 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 502 | uint32_t tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 503 | uint32_t count = 0U, *tempbuff = (uint32_t *)pData; |
lypinator | 0:bb348c97df44 | 504 | |
lypinator | 0:bb348c97df44 | 505 | if(NULL == pData) |
lypinator | 0:bb348c97df44 | 506 | { |
lypinator | 0:bb348c97df44 | 507 | hsd->ErrorCode |= HAL_SD_ERROR_PARAM; |
lypinator | 0:bb348c97df44 | 508 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 509 | } |
lypinator | 0:bb348c97df44 | 510 | |
lypinator | 0:bb348c97df44 | 511 | if(hsd->State == HAL_SD_STATE_READY) |
lypinator | 0:bb348c97df44 | 512 | { |
lypinator | 0:bb348c97df44 | 513 | hsd->ErrorCode = HAL_DMA_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 514 | |
lypinator | 0:bb348c97df44 | 515 | if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) |
lypinator | 0:bb348c97df44 | 516 | { |
lypinator | 0:bb348c97df44 | 517 | hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; |
lypinator | 0:bb348c97df44 | 518 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 519 | } |
lypinator | 0:bb348c97df44 | 520 | |
lypinator | 0:bb348c97df44 | 521 | hsd->State = HAL_SD_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 522 | |
lypinator | 0:bb348c97df44 | 523 | /* Initialize data control register */ |
lypinator | 0:bb348c97df44 | 524 | hsd->Instance->DCTRL = 0U; |
lypinator | 0:bb348c97df44 | 525 | |
lypinator | 0:bb348c97df44 | 526 | if(hsd->SdCard.CardType != CARD_SDHC_SDXC) |
lypinator | 0:bb348c97df44 | 527 | { |
lypinator | 0:bb348c97df44 | 528 | BlockAdd *= 512U; |
lypinator | 0:bb348c97df44 | 529 | } |
lypinator | 0:bb348c97df44 | 530 | |
lypinator | 0:bb348c97df44 | 531 | /* Set Block Size for Card */ |
lypinator | 0:bb348c97df44 | 532 | errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); |
lypinator | 0:bb348c97df44 | 533 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 534 | { |
lypinator | 0:bb348c97df44 | 535 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 536 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 537 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 538 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 539 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 540 | } |
lypinator | 0:bb348c97df44 | 541 | |
lypinator | 0:bb348c97df44 | 542 | /* Configure the SD DPSM (Data Path State Machine) */ |
lypinator | 0:bb348c97df44 | 543 | config.DataTimeOut = SDMMC_DATATIMEOUT; |
lypinator | 0:bb348c97df44 | 544 | config.DataLength = NumberOfBlocks * BLOCKSIZE; |
lypinator | 0:bb348c97df44 | 545 | config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; |
lypinator | 0:bb348c97df44 | 546 | config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; |
lypinator | 0:bb348c97df44 | 547 | config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; |
lypinator | 0:bb348c97df44 | 548 | config.DPSM = SDIO_DPSM_ENABLE; |
lypinator | 0:bb348c97df44 | 549 | SDIO_ConfigData(hsd->Instance, &config); |
lypinator | 0:bb348c97df44 | 550 | |
lypinator | 0:bb348c97df44 | 551 | /* Read block(s) in polling mode */ |
lypinator | 0:bb348c97df44 | 552 | if(NumberOfBlocks > 1U) |
lypinator | 0:bb348c97df44 | 553 | { |
lypinator | 0:bb348c97df44 | 554 | hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK; |
lypinator | 0:bb348c97df44 | 555 | |
lypinator | 0:bb348c97df44 | 556 | /* Read Multi Block command */ |
lypinator | 0:bb348c97df44 | 557 | errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd); |
lypinator | 0:bb348c97df44 | 558 | } |
lypinator | 0:bb348c97df44 | 559 | else |
lypinator | 0:bb348c97df44 | 560 | { |
lypinator | 0:bb348c97df44 | 561 | hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK; |
lypinator | 0:bb348c97df44 | 562 | |
lypinator | 0:bb348c97df44 | 563 | /* Read Single Block command */ |
lypinator | 0:bb348c97df44 | 564 | errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd); |
lypinator | 0:bb348c97df44 | 565 | } |
lypinator | 0:bb348c97df44 | 566 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 567 | { |
lypinator | 0:bb348c97df44 | 568 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 569 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 570 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 571 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 572 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 573 | } |
lypinator | 0:bb348c97df44 | 574 | |
lypinator | 0:bb348c97df44 | 575 | /* Poll on SDIO flags */ |
lypinator | 0:bb348c97df44 | 576 | #ifdef SDIO_STA_STBITERR |
lypinator | 0:bb348c97df44 | 577 | while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_STA_STBITERR)) |
lypinator | 0:bb348c97df44 | 578 | #else /* SDIO_STA_STBITERR not defined */ |
lypinator | 0:bb348c97df44 | 579 | while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND)) |
lypinator | 0:bb348c97df44 | 580 | #endif /* SDIO_STA_STBITERR */ |
lypinator | 0:bb348c97df44 | 581 | { |
lypinator | 0:bb348c97df44 | 582 | if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF)) |
lypinator | 0:bb348c97df44 | 583 | { |
lypinator | 0:bb348c97df44 | 584 | /* Read data from SDIO Rx FIFO */ |
lypinator | 0:bb348c97df44 | 585 | for(count = 0U; count < 8U; count++) |
lypinator | 0:bb348c97df44 | 586 | { |
lypinator | 0:bb348c97df44 | 587 | *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance); |
lypinator | 0:bb348c97df44 | 588 | } |
lypinator | 0:bb348c97df44 | 589 | tempbuff += 8U; |
lypinator | 0:bb348c97df44 | 590 | } |
lypinator | 0:bb348c97df44 | 591 | |
lypinator | 0:bb348c97df44 | 592 | if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout)) |
lypinator | 0:bb348c97df44 | 593 | { |
lypinator | 0:bb348c97df44 | 594 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 595 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 596 | hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT; |
lypinator | 0:bb348c97df44 | 597 | hsd->State= HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 598 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 599 | } |
lypinator | 0:bb348c97df44 | 600 | } |
lypinator | 0:bb348c97df44 | 601 | |
lypinator | 0:bb348c97df44 | 602 | /* Send stop transmission command in case of multiblock read */ |
lypinator | 0:bb348c97df44 | 603 | if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U)) |
lypinator | 0:bb348c97df44 | 604 | { |
lypinator | 0:bb348c97df44 | 605 | if(hsd->SdCard.CardType != CARD_SECURED) |
lypinator | 0:bb348c97df44 | 606 | { |
lypinator | 0:bb348c97df44 | 607 | /* Send stop transmission command */ |
lypinator | 0:bb348c97df44 | 608 | errorstate = SDMMC_CmdStopTransfer(hsd->Instance); |
lypinator | 0:bb348c97df44 | 609 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 610 | { |
lypinator | 0:bb348c97df44 | 611 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 612 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 613 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 614 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 615 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 616 | } |
lypinator | 0:bb348c97df44 | 617 | } |
lypinator | 0:bb348c97df44 | 618 | } |
lypinator | 0:bb348c97df44 | 619 | |
lypinator | 0:bb348c97df44 | 620 | /* Get error state */ |
lypinator | 0:bb348c97df44 | 621 | if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) |
lypinator | 0:bb348c97df44 | 622 | { |
lypinator | 0:bb348c97df44 | 623 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 624 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 625 | hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT; |
lypinator | 0:bb348c97df44 | 626 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 627 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 628 | } |
lypinator | 0:bb348c97df44 | 629 | else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) |
lypinator | 0:bb348c97df44 | 630 | { |
lypinator | 0:bb348c97df44 | 631 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 632 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 633 | hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL; |
lypinator | 0:bb348c97df44 | 634 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 635 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 636 | } |
lypinator | 0:bb348c97df44 | 637 | else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR)) |
lypinator | 0:bb348c97df44 | 638 | { |
lypinator | 0:bb348c97df44 | 639 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 640 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 641 | hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN; |
lypinator | 0:bb348c97df44 | 642 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 643 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 644 | } |
lypinator | 0:bb348c97df44 | 645 | |
lypinator | 0:bb348c97df44 | 646 | /* Empty FIFO if there is still any data */ |
lypinator | 0:bb348c97df44 | 647 | while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))) |
lypinator | 0:bb348c97df44 | 648 | { |
lypinator | 0:bb348c97df44 | 649 | *tempbuff = SDIO_ReadFIFO(hsd->Instance); |
lypinator | 0:bb348c97df44 | 650 | tempbuff++; |
lypinator | 0:bb348c97df44 | 651 | |
lypinator | 0:bb348c97df44 | 652 | if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout)) |
lypinator | 0:bb348c97df44 | 653 | { |
lypinator | 0:bb348c97df44 | 654 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 655 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 656 | hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT; |
lypinator | 0:bb348c97df44 | 657 | hsd->State= HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 658 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 659 | } |
lypinator | 0:bb348c97df44 | 660 | } |
lypinator | 0:bb348c97df44 | 661 | |
lypinator | 0:bb348c97df44 | 662 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 663 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 664 | |
lypinator | 0:bb348c97df44 | 665 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 666 | |
lypinator | 0:bb348c97df44 | 667 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 668 | } |
lypinator | 0:bb348c97df44 | 669 | else |
lypinator | 0:bb348c97df44 | 670 | { |
lypinator | 0:bb348c97df44 | 671 | hsd->ErrorCode |= HAL_SD_ERROR_BUSY; |
lypinator | 0:bb348c97df44 | 672 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 673 | } |
lypinator | 0:bb348c97df44 | 674 | } |
lypinator | 0:bb348c97df44 | 675 | |
lypinator | 0:bb348c97df44 | 676 | /** |
lypinator | 0:bb348c97df44 | 677 | * @brief Allows to write block(s) to a specified address in a card. The Data |
lypinator | 0:bb348c97df44 | 678 | * transfer is managed by polling mode. |
lypinator | 0:bb348c97df44 | 679 | * @note This API should be followed by a check on the card state through |
lypinator | 0:bb348c97df44 | 680 | * HAL_SD_GetCardState(). |
lypinator | 0:bb348c97df44 | 681 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 682 | * @param pData pointer to the buffer that will contain the data to transmit |
lypinator | 0:bb348c97df44 | 683 | * @param BlockAdd Block Address where data will be written |
lypinator | 0:bb348c97df44 | 684 | * @param NumberOfBlocks Number of SD blocks to write |
lypinator | 0:bb348c97df44 | 685 | * @param Timeout Specify timeout value |
lypinator | 0:bb348c97df44 | 686 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 687 | */ |
lypinator | 0:bb348c97df44 | 688 | HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout) |
lypinator | 0:bb348c97df44 | 689 | { |
lypinator | 0:bb348c97df44 | 690 | SDIO_DataInitTypeDef config; |
lypinator | 0:bb348c97df44 | 691 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 692 | uint32_t tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 693 | uint32_t count = 0U; |
lypinator | 0:bb348c97df44 | 694 | uint32_t *tempbuff = (uint32_t *)pData; |
lypinator | 0:bb348c97df44 | 695 | |
lypinator | 0:bb348c97df44 | 696 | if(NULL == pData) |
lypinator | 0:bb348c97df44 | 697 | { |
lypinator | 0:bb348c97df44 | 698 | hsd->ErrorCode |= HAL_SD_ERROR_PARAM; |
lypinator | 0:bb348c97df44 | 699 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 700 | } |
lypinator | 0:bb348c97df44 | 701 | |
lypinator | 0:bb348c97df44 | 702 | if(hsd->State == HAL_SD_STATE_READY) |
lypinator | 0:bb348c97df44 | 703 | { |
lypinator | 0:bb348c97df44 | 704 | hsd->ErrorCode = HAL_DMA_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 705 | |
lypinator | 0:bb348c97df44 | 706 | if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) |
lypinator | 0:bb348c97df44 | 707 | { |
lypinator | 0:bb348c97df44 | 708 | hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; |
lypinator | 0:bb348c97df44 | 709 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 710 | } |
lypinator | 0:bb348c97df44 | 711 | |
lypinator | 0:bb348c97df44 | 712 | hsd->State = HAL_SD_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 713 | |
lypinator | 0:bb348c97df44 | 714 | /* Initialize data control register */ |
lypinator | 0:bb348c97df44 | 715 | hsd->Instance->DCTRL = 0U; |
lypinator | 0:bb348c97df44 | 716 | |
lypinator | 0:bb348c97df44 | 717 | if(hsd->SdCard.CardType != CARD_SDHC_SDXC) |
lypinator | 0:bb348c97df44 | 718 | { |
lypinator | 0:bb348c97df44 | 719 | BlockAdd *= 512U; |
lypinator | 0:bb348c97df44 | 720 | } |
lypinator | 0:bb348c97df44 | 721 | |
lypinator | 0:bb348c97df44 | 722 | /* Set Block Size for Card */ |
lypinator | 0:bb348c97df44 | 723 | errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); |
lypinator | 0:bb348c97df44 | 724 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 725 | { |
lypinator | 0:bb348c97df44 | 726 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 727 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 728 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 729 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 730 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 731 | } |
lypinator | 0:bb348c97df44 | 732 | |
lypinator | 0:bb348c97df44 | 733 | /* Write Blocks in Polling mode */ |
lypinator | 0:bb348c97df44 | 734 | if(NumberOfBlocks > 1U) |
lypinator | 0:bb348c97df44 | 735 | { |
lypinator | 0:bb348c97df44 | 736 | hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK; |
lypinator | 0:bb348c97df44 | 737 | |
lypinator | 0:bb348c97df44 | 738 | /* Write Multi Block command */ |
lypinator | 0:bb348c97df44 | 739 | errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd); |
lypinator | 0:bb348c97df44 | 740 | } |
lypinator | 0:bb348c97df44 | 741 | else |
lypinator | 0:bb348c97df44 | 742 | { |
lypinator | 0:bb348c97df44 | 743 | hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK; |
lypinator | 0:bb348c97df44 | 744 | |
lypinator | 0:bb348c97df44 | 745 | /* Write Single Block command */ |
lypinator | 0:bb348c97df44 | 746 | errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd); |
lypinator | 0:bb348c97df44 | 747 | } |
lypinator | 0:bb348c97df44 | 748 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 749 | { |
lypinator | 0:bb348c97df44 | 750 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 751 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 752 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 753 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 754 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 755 | } |
lypinator | 0:bb348c97df44 | 756 | |
lypinator | 0:bb348c97df44 | 757 | /* Configure the SD DPSM (Data Path State Machine) */ |
lypinator | 0:bb348c97df44 | 758 | config.DataTimeOut = SDMMC_DATATIMEOUT; |
lypinator | 0:bb348c97df44 | 759 | config.DataLength = NumberOfBlocks * BLOCKSIZE; |
lypinator | 0:bb348c97df44 | 760 | config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; |
lypinator | 0:bb348c97df44 | 761 | config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD; |
lypinator | 0:bb348c97df44 | 762 | config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; |
lypinator | 0:bb348c97df44 | 763 | config.DPSM = SDIO_DPSM_ENABLE; |
lypinator | 0:bb348c97df44 | 764 | SDIO_ConfigData(hsd->Instance, &config); |
lypinator | 0:bb348c97df44 | 765 | |
lypinator | 0:bb348c97df44 | 766 | /* Write block(s) in polling mode */ |
lypinator | 0:bb348c97df44 | 767 | #ifdef SDIO_STA_STBITERR |
lypinator | 0:bb348c97df44 | 768 | while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR)) |
lypinator | 0:bb348c97df44 | 769 | #else /* SDIO_STA_STBITERR not defined */ |
lypinator | 0:bb348c97df44 | 770 | while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND)) |
lypinator | 0:bb348c97df44 | 771 | #endif /* SDIO_STA_STBITERR */ |
lypinator | 0:bb348c97df44 | 772 | { |
lypinator | 0:bb348c97df44 | 773 | if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE)) |
lypinator | 0:bb348c97df44 | 774 | { |
lypinator | 0:bb348c97df44 | 775 | /* Write data to SDIO Tx FIFO */ |
lypinator | 0:bb348c97df44 | 776 | for(count = 0U; count < 8U; count++) |
lypinator | 0:bb348c97df44 | 777 | { |
lypinator | 0:bb348c97df44 | 778 | SDIO_WriteFIFO(hsd->Instance, (tempbuff + count)); |
lypinator | 0:bb348c97df44 | 779 | } |
lypinator | 0:bb348c97df44 | 780 | tempbuff += 8U; |
lypinator | 0:bb348c97df44 | 781 | } |
lypinator | 0:bb348c97df44 | 782 | |
lypinator | 0:bb348c97df44 | 783 | if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout)) |
lypinator | 0:bb348c97df44 | 784 | { |
lypinator | 0:bb348c97df44 | 785 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 786 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 787 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 788 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 789 | return HAL_TIMEOUT; |
lypinator | 0:bb348c97df44 | 790 | } |
lypinator | 0:bb348c97df44 | 791 | } |
lypinator | 0:bb348c97df44 | 792 | |
lypinator | 0:bb348c97df44 | 793 | /* Send stop transmission command in case of multiblock write */ |
lypinator | 0:bb348c97df44 | 794 | if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U)) |
lypinator | 0:bb348c97df44 | 795 | { |
lypinator | 0:bb348c97df44 | 796 | if(hsd->SdCard.CardType != CARD_SECURED) |
lypinator | 0:bb348c97df44 | 797 | { |
lypinator | 0:bb348c97df44 | 798 | /* Send stop transmission command */ |
lypinator | 0:bb348c97df44 | 799 | errorstate = SDMMC_CmdStopTransfer(hsd->Instance); |
lypinator | 0:bb348c97df44 | 800 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 801 | { |
lypinator | 0:bb348c97df44 | 802 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 803 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 804 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 805 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 806 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 807 | } |
lypinator | 0:bb348c97df44 | 808 | } |
lypinator | 0:bb348c97df44 | 809 | } |
lypinator | 0:bb348c97df44 | 810 | |
lypinator | 0:bb348c97df44 | 811 | /* Get error state */ |
lypinator | 0:bb348c97df44 | 812 | if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) |
lypinator | 0:bb348c97df44 | 813 | { |
lypinator | 0:bb348c97df44 | 814 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 815 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 816 | hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT; |
lypinator | 0:bb348c97df44 | 817 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 818 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 819 | } |
lypinator | 0:bb348c97df44 | 820 | else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) |
lypinator | 0:bb348c97df44 | 821 | { |
lypinator | 0:bb348c97df44 | 822 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 823 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 824 | hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL; |
lypinator | 0:bb348c97df44 | 825 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 826 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 827 | } |
lypinator | 0:bb348c97df44 | 828 | else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR)) |
lypinator | 0:bb348c97df44 | 829 | { |
lypinator | 0:bb348c97df44 | 830 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 831 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 832 | hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN; |
lypinator | 0:bb348c97df44 | 833 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 834 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 835 | } |
lypinator | 0:bb348c97df44 | 836 | |
lypinator | 0:bb348c97df44 | 837 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 838 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 839 | |
lypinator | 0:bb348c97df44 | 840 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 841 | |
lypinator | 0:bb348c97df44 | 842 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 843 | } |
lypinator | 0:bb348c97df44 | 844 | else |
lypinator | 0:bb348c97df44 | 845 | { |
lypinator | 0:bb348c97df44 | 846 | hsd->ErrorCode |= HAL_SD_ERROR_BUSY; |
lypinator | 0:bb348c97df44 | 847 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 848 | } |
lypinator | 0:bb348c97df44 | 849 | } |
lypinator | 0:bb348c97df44 | 850 | |
lypinator | 0:bb348c97df44 | 851 | /** |
lypinator | 0:bb348c97df44 | 852 | * @brief Reads block(s) from a specified address in a card. The Data transfer |
lypinator | 0:bb348c97df44 | 853 | * is managed in interrupt mode. |
lypinator | 0:bb348c97df44 | 854 | * @note This API should be followed by a check on the card state through |
lypinator | 0:bb348c97df44 | 855 | * HAL_SD_GetCardState(). |
lypinator | 0:bb348c97df44 | 856 | * @note You could also check the IT transfer process through the SD Rx |
lypinator | 0:bb348c97df44 | 857 | * interrupt event. |
lypinator | 0:bb348c97df44 | 858 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 859 | * @param pData Pointer to the buffer that will contain the received data |
lypinator | 0:bb348c97df44 | 860 | * @param BlockAdd Block Address from where data is to be read |
lypinator | 0:bb348c97df44 | 861 | * @param NumberOfBlocks Number of blocks to read. |
lypinator | 0:bb348c97df44 | 862 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 863 | */ |
lypinator | 0:bb348c97df44 | 864 | HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks) |
lypinator | 0:bb348c97df44 | 865 | { |
lypinator | 0:bb348c97df44 | 866 | SDIO_DataInitTypeDef config; |
lypinator | 0:bb348c97df44 | 867 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 868 | |
lypinator | 0:bb348c97df44 | 869 | if(NULL == pData) |
lypinator | 0:bb348c97df44 | 870 | { |
lypinator | 0:bb348c97df44 | 871 | hsd->ErrorCode |= HAL_SD_ERROR_PARAM; |
lypinator | 0:bb348c97df44 | 872 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 873 | } |
lypinator | 0:bb348c97df44 | 874 | |
lypinator | 0:bb348c97df44 | 875 | if(hsd->State == HAL_SD_STATE_READY) |
lypinator | 0:bb348c97df44 | 876 | { |
lypinator | 0:bb348c97df44 | 877 | hsd->ErrorCode = HAL_DMA_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 878 | |
lypinator | 0:bb348c97df44 | 879 | if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) |
lypinator | 0:bb348c97df44 | 880 | { |
lypinator | 0:bb348c97df44 | 881 | hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; |
lypinator | 0:bb348c97df44 | 882 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 883 | } |
lypinator | 0:bb348c97df44 | 884 | |
lypinator | 0:bb348c97df44 | 885 | hsd->State = HAL_SD_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 886 | |
lypinator | 0:bb348c97df44 | 887 | /* Initialize data control register */ |
lypinator | 0:bb348c97df44 | 888 | hsd->Instance->DCTRL = 0U; |
lypinator | 0:bb348c97df44 | 889 | |
lypinator | 0:bb348c97df44 | 890 | hsd->pRxBuffPtr = (uint32_t *)pData; |
lypinator | 0:bb348c97df44 | 891 | hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks; |
lypinator | 0:bb348c97df44 | 892 | |
lypinator | 0:bb348c97df44 | 893 | #ifdef SDIO_STA_STBITER |
lypinator | 0:bb348c97df44 | 894 | __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF | SDIO_IT_STBITERR)); |
lypinator | 0:bb348c97df44 | 895 | #else /* SDIO_STA_STBITERR not defined */ |
lypinator | 0:bb348c97df44 | 896 | __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF)); |
lypinator | 0:bb348c97df44 | 897 | #endif /* SDIO_STA_STBITERR */ |
lypinator | 0:bb348c97df44 | 898 | |
lypinator | 0:bb348c97df44 | 899 | if(hsd->SdCard.CardType != CARD_SDHC_SDXC) |
lypinator | 0:bb348c97df44 | 900 | { |
lypinator | 0:bb348c97df44 | 901 | BlockAdd *= 512U; |
lypinator | 0:bb348c97df44 | 902 | } |
lypinator | 0:bb348c97df44 | 903 | |
lypinator | 0:bb348c97df44 | 904 | /* Configure the SD DPSM (Data Path State Machine) */ |
lypinator | 0:bb348c97df44 | 905 | config.DataTimeOut = SDMMC_DATATIMEOUT; |
lypinator | 0:bb348c97df44 | 906 | config.DataLength = BLOCKSIZE * NumberOfBlocks; |
lypinator | 0:bb348c97df44 | 907 | config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; |
lypinator | 0:bb348c97df44 | 908 | config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; |
lypinator | 0:bb348c97df44 | 909 | config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; |
lypinator | 0:bb348c97df44 | 910 | config.DPSM = SDIO_DPSM_ENABLE; |
lypinator | 0:bb348c97df44 | 911 | SDIO_ConfigData(hsd->Instance, &config); |
lypinator | 0:bb348c97df44 | 912 | |
lypinator | 0:bb348c97df44 | 913 | /* Set Block Size for Card */ |
lypinator | 0:bb348c97df44 | 914 | errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); |
lypinator | 0:bb348c97df44 | 915 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 916 | { |
lypinator | 0:bb348c97df44 | 917 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 918 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 919 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 920 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 921 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 922 | } |
lypinator | 0:bb348c97df44 | 923 | |
lypinator | 0:bb348c97df44 | 924 | /* Read Blocks in IT mode */ |
lypinator | 0:bb348c97df44 | 925 | if(NumberOfBlocks > 1U) |
lypinator | 0:bb348c97df44 | 926 | { |
lypinator | 0:bb348c97df44 | 927 | hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT); |
lypinator | 0:bb348c97df44 | 928 | |
lypinator | 0:bb348c97df44 | 929 | /* Read Multi Block command */ |
lypinator | 0:bb348c97df44 | 930 | errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd); |
lypinator | 0:bb348c97df44 | 931 | } |
lypinator | 0:bb348c97df44 | 932 | else |
lypinator | 0:bb348c97df44 | 933 | { |
lypinator | 0:bb348c97df44 | 934 | hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT); |
lypinator | 0:bb348c97df44 | 935 | |
lypinator | 0:bb348c97df44 | 936 | /* Read Single Block command */ |
lypinator | 0:bb348c97df44 | 937 | errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd); |
lypinator | 0:bb348c97df44 | 938 | } |
lypinator | 0:bb348c97df44 | 939 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 940 | { |
lypinator | 0:bb348c97df44 | 941 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 942 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 943 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 944 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 945 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 946 | } |
lypinator | 0:bb348c97df44 | 947 | |
lypinator | 0:bb348c97df44 | 948 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 949 | } |
lypinator | 0:bb348c97df44 | 950 | else |
lypinator | 0:bb348c97df44 | 951 | { |
lypinator | 0:bb348c97df44 | 952 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 953 | } |
lypinator | 0:bb348c97df44 | 954 | } |
lypinator | 0:bb348c97df44 | 955 | |
lypinator | 0:bb348c97df44 | 956 | /** |
lypinator | 0:bb348c97df44 | 957 | * @brief Writes block(s) to a specified address in a card. The Data transfer |
lypinator | 0:bb348c97df44 | 958 | * is managed in interrupt mode. |
lypinator | 0:bb348c97df44 | 959 | * @note This API should be followed by a check on the card state through |
lypinator | 0:bb348c97df44 | 960 | * HAL_SD_GetCardState(). |
lypinator | 0:bb348c97df44 | 961 | * @note You could also check the IT transfer process through the SD Tx |
lypinator | 0:bb348c97df44 | 962 | * interrupt event. |
lypinator | 0:bb348c97df44 | 963 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 964 | * @param pData Pointer to the buffer that will contain the data to transmit |
lypinator | 0:bb348c97df44 | 965 | * @param BlockAdd Block Address where data will be written |
lypinator | 0:bb348c97df44 | 966 | * @param NumberOfBlocks Number of blocks to write |
lypinator | 0:bb348c97df44 | 967 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 968 | */ |
lypinator | 0:bb348c97df44 | 969 | HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks) |
lypinator | 0:bb348c97df44 | 970 | { |
lypinator | 0:bb348c97df44 | 971 | SDIO_DataInitTypeDef config; |
lypinator | 0:bb348c97df44 | 972 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 973 | |
lypinator | 0:bb348c97df44 | 974 | if(NULL == pData) |
lypinator | 0:bb348c97df44 | 975 | { |
lypinator | 0:bb348c97df44 | 976 | hsd->ErrorCode |= HAL_SD_ERROR_PARAM; |
lypinator | 0:bb348c97df44 | 977 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 978 | } |
lypinator | 0:bb348c97df44 | 979 | |
lypinator | 0:bb348c97df44 | 980 | if(hsd->State == HAL_SD_STATE_READY) |
lypinator | 0:bb348c97df44 | 981 | { |
lypinator | 0:bb348c97df44 | 982 | hsd->ErrorCode = HAL_DMA_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 983 | |
lypinator | 0:bb348c97df44 | 984 | if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) |
lypinator | 0:bb348c97df44 | 985 | { |
lypinator | 0:bb348c97df44 | 986 | hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; |
lypinator | 0:bb348c97df44 | 987 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 988 | } |
lypinator | 0:bb348c97df44 | 989 | |
lypinator | 0:bb348c97df44 | 990 | hsd->State = HAL_SD_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 991 | |
lypinator | 0:bb348c97df44 | 992 | /* Initialize data control register */ |
lypinator | 0:bb348c97df44 | 993 | hsd->Instance->DCTRL = 0U; |
lypinator | 0:bb348c97df44 | 994 | |
lypinator | 0:bb348c97df44 | 995 | hsd->pTxBuffPtr = (uint32_t *)pData; |
lypinator | 0:bb348c97df44 | 996 | hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks; |
lypinator | 0:bb348c97df44 | 997 | |
lypinator | 0:bb348c97df44 | 998 | /* Enable transfer interrupts */ |
lypinator | 0:bb348c97df44 | 999 | #ifdef SDIO_STA_STBITER |
lypinator | 0:bb348c97df44 | 1000 | __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE | SDIO_IT_STBITERR)); |
lypinator | 0:bb348c97df44 | 1001 | #else /* SDIO_STA_STBITERR not defined */ |
lypinator | 0:bb348c97df44 | 1002 | __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE)); |
lypinator | 0:bb348c97df44 | 1003 | #endif /* SDIO_STA_STBITERR */ |
lypinator | 0:bb348c97df44 | 1004 | |
lypinator | 0:bb348c97df44 | 1005 | if(hsd->SdCard.CardType != CARD_SDHC_SDXC) |
lypinator | 0:bb348c97df44 | 1006 | { |
lypinator | 0:bb348c97df44 | 1007 | BlockAdd *= 512U; |
lypinator | 0:bb348c97df44 | 1008 | } |
lypinator | 0:bb348c97df44 | 1009 | |
lypinator | 0:bb348c97df44 | 1010 | /* Set Block Size for Card */ |
lypinator | 0:bb348c97df44 | 1011 | errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); |
lypinator | 0:bb348c97df44 | 1012 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 1013 | { |
lypinator | 0:bb348c97df44 | 1014 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 1015 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 1016 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 1017 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1018 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1019 | } |
lypinator | 0:bb348c97df44 | 1020 | |
lypinator | 0:bb348c97df44 | 1021 | /* Write Blocks in Polling mode */ |
lypinator | 0:bb348c97df44 | 1022 | if(NumberOfBlocks > 1U) |
lypinator | 0:bb348c97df44 | 1023 | { |
lypinator | 0:bb348c97df44 | 1024 | hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT); |
lypinator | 0:bb348c97df44 | 1025 | |
lypinator | 0:bb348c97df44 | 1026 | /* Write Multi Block command */ |
lypinator | 0:bb348c97df44 | 1027 | errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd); |
lypinator | 0:bb348c97df44 | 1028 | } |
lypinator | 0:bb348c97df44 | 1029 | else |
lypinator | 0:bb348c97df44 | 1030 | { |
lypinator | 0:bb348c97df44 | 1031 | hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT); |
lypinator | 0:bb348c97df44 | 1032 | |
lypinator | 0:bb348c97df44 | 1033 | /* Write Single Block command */ |
lypinator | 0:bb348c97df44 | 1034 | errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd); |
lypinator | 0:bb348c97df44 | 1035 | } |
lypinator | 0:bb348c97df44 | 1036 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 1037 | { |
lypinator | 0:bb348c97df44 | 1038 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 1039 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 1040 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 1041 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1042 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1043 | } |
lypinator | 0:bb348c97df44 | 1044 | |
lypinator | 0:bb348c97df44 | 1045 | /* Configure the SD DPSM (Data Path State Machine) */ |
lypinator | 0:bb348c97df44 | 1046 | config.DataTimeOut = SDMMC_DATATIMEOUT; |
lypinator | 0:bb348c97df44 | 1047 | config.DataLength = BLOCKSIZE * NumberOfBlocks; |
lypinator | 0:bb348c97df44 | 1048 | config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; |
lypinator | 0:bb348c97df44 | 1049 | config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD; |
lypinator | 0:bb348c97df44 | 1050 | config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; |
lypinator | 0:bb348c97df44 | 1051 | config.DPSM = SDIO_DPSM_ENABLE; |
lypinator | 0:bb348c97df44 | 1052 | SDIO_ConfigData(hsd->Instance, &config); |
lypinator | 0:bb348c97df44 | 1053 | |
lypinator | 0:bb348c97df44 | 1054 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1055 | } |
lypinator | 0:bb348c97df44 | 1056 | else |
lypinator | 0:bb348c97df44 | 1057 | { |
lypinator | 0:bb348c97df44 | 1058 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 1059 | } |
lypinator | 0:bb348c97df44 | 1060 | } |
lypinator | 0:bb348c97df44 | 1061 | |
lypinator | 0:bb348c97df44 | 1062 | /** |
lypinator | 0:bb348c97df44 | 1063 | * @brief Reads block(s) from a specified address in a card. The Data transfer |
lypinator | 0:bb348c97df44 | 1064 | * is managed by DMA mode. |
lypinator | 0:bb348c97df44 | 1065 | * @note This API should be followed by a check on the card state through |
lypinator | 0:bb348c97df44 | 1066 | * HAL_SD_GetCardState(). |
lypinator | 0:bb348c97df44 | 1067 | * @note You could also check the DMA transfer process through the SD Rx |
lypinator | 0:bb348c97df44 | 1068 | * interrupt event. |
lypinator | 0:bb348c97df44 | 1069 | * @param hsd Pointer SD handle |
lypinator | 0:bb348c97df44 | 1070 | * @param pData Pointer to the buffer that will contain the received data |
lypinator | 0:bb348c97df44 | 1071 | * @param BlockAdd Block Address from where data is to be read |
lypinator | 0:bb348c97df44 | 1072 | * @param NumberOfBlocks Number of blocks to read. |
lypinator | 0:bb348c97df44 | 1073 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1074 | */ |
lypinator | 0:bb348c97df44 | 1075 | HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks) |
lypinator | 0:bb348c97df44 | 1076 | { |
lypinator | 0:bb348c97df44 | 1077 | SDIO_DataInitTypeDef config; |
lypinator | 0:bb348c97df44 | 1078 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 1079 | |
lypinator | 0:bb348c97df44 | 1080 | if(NULL == pData) |
lypinator | 0:bb348c97df44 | 1081 | { |
lypinator | 0:bb348c97df44 | 1082 | hsd->ErrorCode |= HAL_SD_ERROR_PARAM; |
lypinator | 0:bb348c97df44 | 1083 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1084 | } |
lypinator | 0:bb348c97df44 | 1085 | |
lypinator | 0:bb348c97df44 | 1086 | if(hsd->State == HAL_SD_STATE_READY) |
lypinator | 0:bb348c97df44 | 1087 | { |
lypinator | 0:bb348c97df44 | 1088 | hsd->ErrorCode = HAL_DMA_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 1089 | |
lypinator | 0:bb348c97df44 | 1090 | if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) |
lypinator | 0:bb348c97df44 | 1091 | { |
lypinator | 0:bb348c97df44 | 1092 | hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; |
lypinator | 0:bb348c97df44 | 1093 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1094 | } |
lypinator | 0:bb348c97df44 | 1095 | |
lypinator | 0:bb348c97df44 | 1096 | hsd->State = HAL_SD_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1097 | |
lypinator | 0:bb348c97df44 | 1098 | /* Initialize data control register */ |
lypinator | 0:bb348c97df44 | 1099 | hsd->Instance->DCTRL = 0U; |
lypinator | 0:bb348c97df44 | 1100 | |
lypinator | 0:bb348c97df44 | 1101 | #ifdef SDIO_STA_STBITER |
lypinator | 0:bb348c97df44 | 1102 | __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_IT_STBITERR)); |
lypinator | 0:bb348c97df44 | 1103 | #else /* SDIO_STA_STBITERR not defined */ |
lypinator | 0:bb348c97df44 | 1104 | __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND)); |
lypinator | 0:bb348c97df44 | 1105 | #endif /* SDIO_STA_STBITERR */ |
lypinator | 0:bb348c97df44 | 1106 | |
lypinator | 0:bb348c97df44 | 1107 | /* Set the DMA transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1108 | hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt; |
lypinator | 0:bb348c97df44 | 1109 | |
lypinator | 0:bb348c97df44 | 1110 | /* Set the DMA error callback */ |
lypinator | 0:bb348c97df44 | 1111 | hsd->hdmarx->XferErrorCallback = SD_DMAError; |
lypinator | 0:bb348c97df44 | 1112 | |
lypinator | 0:bb348c97df44 | 1113 | /* Set the DMA Abort callback */ |
lypinator | 0:bb348c97df44 | 1114 | hsd->hdmarx->XferAbortCallback = NULL; |
lypinator | 0:bb348c97df44 | 1115 | |
lypinator | 0:bb348c97df44 | 1116 | /* Enable the DMA Channel */ |
lypinator | 0:bb348c97df44 | 1117 | HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4); |
lypinator | 0:bb348c97df44 | 1118 | |
lypinator | 0:bb348c97df44 | 1119 | /* Enable SD DMA transfer */ |
lypinator | 0:bb348c97df44 | 1120 | __HAL_SD_DMA_ENABLE(hsd); |
lypinator | 0:bb348c97df44 | 1121 | |
lypinator | 0:bb348c97df44 | 1122 | if(hsd->SdCard.CardType != CARD_SDHC_SDXC) |
lypinator | 0:bb348c97df44 | 1123 | { |
lypinator | 0:bb348c97df44 | 1124 | BlockAdd *= 512U; |
lypinator | 0:bb348c97df44 | 1125 | } |
lypinator | 0:bb348c97df44 | 1126 | |
lypinator | 0:bb348c97df44 | 1127 | /* Configure the SD DPSM (Data Path State Machine) */ |
lypinator | 0:bb348c97df44 | 1128 | config.DataTimeOut = SDMMC_DATATIMEOUT; |
lypinator | 0:bb348c97df44 | 1129 | config.DataLength = BLOCKSIZE * NumberOfBlocks; |
lypinator | 0:bb348c97df44 | 1130 | config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; |
lypinator | 0:bb348c97df44 | 1131 | config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; |
lypinator | 0:bb348c97df44 | 1132 | config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; |
lypinator | 0:bb348c97df44 | 1133 | config.DPSM = SDIO_DPSM_ENABLE; |
lypinator | 0:bb348c97df44 | 1134 | SDIO_ConfigData(hsd->Instance, &config); |
lypinator | 0:bb348c97df44 | 1135 | |
lypinator | 0:bb348c97df44 | 1136 | /* Set Block Size for Card */ |
lypinator | 0:bb348c97df44 | 1137 | errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); |
lypinator | 0:bb348c97df44 | 1138 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 1139 | { |
lypinator | 0:bb348c97df44 | 1140 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 1141 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 1142 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 1143 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1144 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1145 | } |
lypinator | 0:bb348c97df44 | 1146 | |
lypinator | 0:bb348c97df44 | 1147 | /* Read Blocks in DMA mode */ |
lypinator | 0:bb348c97df44 | 1148 | if(NumberOfBlocks > 1U) |
lypinator | 0:bb348c97df44 | 1149 | { |
lypinator | 0:bb348c97df44 | 1150 | hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA); |
lypinator | 0:bb348c97df44 | 1151 | |
lypinator | 0:bb348c97df44 | 1152 | /* Read Multi Block command */ |
lypinator | 0:bb348c97df44 | 1153 | errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd); |
lypinator | 0:bb348c97df44 | 1154 | } |
lypinator | 0:bb348c97df44 | 1155 | else |
lypinator | 0:bb348c97df44 | 1156 | { |
lypinator | 0:bb348c97df44 | 1157 | hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA); |
lypinator | 0:bb348c97df44 | 1158 | |
lypinator | 0:bb348c97df44 | 1159 | /* Read Single Block command */ |
lypinator | 0:bb348c97df44 | 1160 | errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd); |
lypinator | 0:bb348c97df44 | 1161 | } |
lypinator | 0:bb348c97df44 | 1162 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 1163 | { |
lypinator | 0:bb348c97df44 | 1164 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 1165 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 1166 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 1167 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1168 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1169 | } |
lypinator | 0:bb348c97df44 | 1170 | |
lypinator | 0:bb348c97df44 | 1171 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1172 | } |
lypinator | 0:bb348c97df44 | 1173 | else |
lypinator | 0:bb348c97df44 | 1174 | { |
lypinator | 0:bb348c97df44 | 1175 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 1176 | } |
lypinator | 0:bb348c97df44 | 1177 | } |
lypinator | 0:bb348c97df44 | 1178 | |
lypinator | 0:bb348c97df44 | 1179 | /** |
lypinator | 0:bb348c97df44 | 1180 | * @brief Writes block(s) to a specified address in a card. The Data transfer |
lypinator | 0:bb348c97df44 | 1181 | * is managed by DMA mode. |
lypinator | 0:bb348c97df44 | 1182 | * @note This API should be followed by a check on the card state through |
lypinator | 0:bb348c97df44 | 1183 | * HAL_SD_GetCardState(). |
lypinator | 0:bb348c97df44 | 1184 | * @note You could also check the DMA transfer process through the SD Tx |
lypinator | 0:bb348c97df44 | 1185 | * interrupt event. |
lypinator | 0:bb348c97df44 | 1186 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 1187 | * @param pData Pointer to the buffer that will contain the data to transmit |
lypinator | 0:bb348c97df44 | 1188 | * @param BlockAdd Block Address where data will be written |
lypinator | 0:bb348c97df44 | 1189 | * @param NumberOfBlocks Number of blocks to write |
lypinator | 0:bb348c97df44 | 1190 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1191 | */ |
lypinator | 0:bb348c97df44 | 1192 | HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks) |
lypinator | 0:bb348c97df44 | 1193 | { |
lypinator | 0:bb348c97df44 | 1194 | SDIO_DataInitTypeDef config; |
lypinator | 0:bb348c97df44 | 1195 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 1196 | |
lypinator | 0:bb348c97df44 | 1197 | if(NULL == pData) |
lypinator | 0:bb348c97df44 | 1198 | { |
lypinator | 0:bb348c97df44 | 1199 | hsd->ErrorCode |= HAL_SD_ERROR_PARAM; |
lypinator | 0:bb348c97df44 | 1200 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1201 | } |
lypinator | 0:bb348c97df44 | 1202 | |
lypinator | 0:bb348c97df44 | 1203 | if(hsd->State == HAL_SD_STATE_READY) |
lypinator | 0:bb348c97df44 | 1204 | { |
lypinator | 0:bb348c97df44 | 1205 | hsd->ErrorCode = HAL_DMA_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 1206 | |
lypinator | 0:bb348c97df44 | 1207 | if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) |
lypinator | 0:bb348c97df44 | 1208 | { |
lypinator | 0:bb348c97df44 | 1209 | hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; |
lypinator | 0:bb348c97df44 | 1210 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1211 | } |
lypinator | 0:bb348c97df44 | 1212 | |
lypinator | 0:bb348c97df44 | 1213 | hsd->State = HAL_SD_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1214 | |
lypinator | 0:bb348c97df44 | 1215 | /* Initialize data control register */ |
lypinator | 0:bb348c97df44 | 1216 | hsd->Instance->DCTRL = 0U; |
lypinator | 0:bb348c97df44 | 1217 | |
lypinator | 0:bb348c97df44 | 1218 | /* Enable SD Error interrupts */ |
lypinator | 0:bb348c97df44 | 1219 | #ifdef SDIO_STA_STBITER |
lypinator | 0:bb348c97df44 | 1220 | __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR)); |
lypinator | 0:bb348c97df44 | 1221 | #else /* SDIO_STA_STBITERR not defined */ |
lypinator | 0:bb348c97df44 | 1222 | __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR)); |
lypinator | 0:bb348c97df44 | 1223 | #endif /* SDIO_STA_STBITERR */ |
lypinator | 0:bb348c97df44 | 1224 | |
lypinator | 0:bb348c97df44 | 1225 | /* Set the DMA transfer complete callback */ |
lypinator | 0:bb348c97df44 | 1226 | hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt; |
lypinator | 0:bb348c97df44 | 1227 | |
lypinator | 0:bb348c97df44 | 1228 | /* Set the DMA error callback */ |
lypinator | 0:bb348c97df44 | 1229 | hsd->hdmatx->XferErrorCallback = SD_DMAError; |
lypinator | 0:bb348c97df44 | 1230 | |
lypinator | 0:bb348c97df44 | 1231 | /* Set the DMA Abort callback */ |
lypinator | 0:bb348c97df44 | 1232 | hsd->hdmatx->XferAbortCallback = NULL; |
lypinator | 0:bb348c97df44 | 1233 | |
lypinator | 0:bb348c97df44 | 1234 | if(hsd->SdCard.CardType != CARD_SDHC_SDXC) |
lypinator | 0:bb348c97df44 | 1235 | { |
lypinator | 0:bb348c97df44 | 1236 | BlockAdd *= 512U; |
lypinator | 0:bb348c97df44 | 1237 | } |
lypinator | 0:bb348c97df44 | 1238 | |
lypinator | 0:bb348c97df44 | 1239 | /* Set Block Size for Card */ |
lypinator | 0:bb348c97df44 | 1240 | errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); |
lypinator | 0:bb348c97df44 | 1241 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 1242 | { |
lypinator | 0:bb348c97df44 | 1243 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 1244 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 1245 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 1246 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1247 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1248 | } |
lypinator | 0:bb348c97df44 | 1249 | |
lypinator | 0:bb348c97df44 | 1250 | /* Write Blocks in Polling mode */ |
lypinator | 0:bb348c97df44 | 1251 | if(NumberOfBlocks > 1U) |
lypinator | 0:bb348c97df44 | 1252 | { |
lypinator | 0:bb348c97df44 | 1253 | hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA); |
lypinator | 0:bb348c97df44 | 1254 | |
lypinator | 0:bb348c97df44 | 1255 | /* Write Multi Block command */ |
lypinator | 0:bb348c97df44 | 1256 | errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd); |
lypinator | 0:bb348c97df44 | 1257 | } |
lypinator | 0:bb348c97df44 | 1258 | else |
lypinator | 0:bb348c97df44 | 1259 | { |
lypinator | 0:bb348c97df44 | 1260 | hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA); |
lypinator | 0:bb348c97df44 | 1261 | |
lypinator | 0:bb348c97df44 | 1262 | /* Write Single Block command */ |
lypinator | 0:bb348c97df44 | 1263 | errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd); |
lypinator | 0:bb348c97df44 | 1264 | } |
lypinator | 0:bb348c97df44 | 1265 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 1266 | { |
lypinator | 0:bb348c97df44 | 1267 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 1268 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 1269 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 1270 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1271 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1272 | } |
lypinator | 0:bb348c97df44 | 1273 | |
lypinator | 0:bb348c97df44 | 1274 | /* Enable SDIO DMA transfer */ |
lypinator | 0:bb348c97df44 | 1275 | __HAL_SD_DMA_ENABLE(hsd); |
lypinator | 0:bb348c97df44 | 1276 | |
lypinator | 0:bb348c97df44 | 1277 | /* Enable the DMA Channel */ |
lypinator | 0:bb348c97df44 | 1278 | HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4); |
lypinator | 0:bb348c97df44 | 1279 | |
lypinator | 0:bb348c97df44 | 1280 | /* Configure the SD DPSM (Data Path State Machine) */ |
lypinator | 0:bb348c97df44 | 1281 | config.DataTimeOut = SDMMC_DATATIMEOUT; |
lypinator | 0:bb348c97df44 | 1282 | config.DataLength = BLOCKSIZE * NumberOfBlocks; |
lypinator | 0:bb348c97df44 | 1283 | config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; |
lypinator | 0:bb348c97df44 | 1284 | config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD; |
lypinator | 0:bb348c97df44 | 1285 | config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; |
lypinator | 0:bb348c97df44 | 1286 | config.DPSM = SDIO_DPSM_ENABLE; |
lypinator | 0:bb348c97df44 | 1287 | SDIO_ConfigData(hsd->Instance, &config); |
lypinator | 0:bb348c97df44 | 1288 | |
lypinator | 0:bb348c97df44 | 1289 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1290 | } |
lypinator | 0:bb348c97df44 | 1291 | else |
lypinator | 0:bb348c97df44 | 1292 | { |
lypinator | 0:bb348c97df44 | 1293 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 1294 | } |
lypinator | 0:bb348c97df44 | 1295 | } |
lypinator | 0:bb348c97df44 | 1296 | |
lypinator | 0:bb348c97df44 | 1297 | /** |
lypinator | 0:bb348c97df44 | 1298 | * @brief Erases the specified memory area of the given SD card. |
lypinator | 0:bb348c97df44 | 1299 | * @note This API should be followed by a check on the card state through |
lypinator | 0:bb348c97df44 | 1300 | * HAL_SD_GetCardState(). |
lypinator | 0:bb348c97df44 | 1301 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 1302 | * @param BlockStartAdd Start Block address |
lypinator | 0:bb348c97df44 | 1303 | * @param BlockEndAdd End Block address |
lypinator | 0:bb348c97df44 | 1304 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1305 | */ |
lypinator | 0:bb348c97df44 | 1306 | HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd) |
lypinator | 0:bb348c97df44 | 1307 | { |
lypinator | 0:bb348c97df44 | 1308 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 1309 | |
lypinator | 0:bb348c97df44 | 1310 | if(hsd->State == HAL_SD_STATE_READY) |
lypinator | 0:bb348c97df44 | 1311 | { |
lypinator | 0:bb348c97df44 | 1312 | hsd->ErrorCode = HAL_DMA_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 1313 | |
lypinator | 0:bb348c97df44 | 1314 | if(BlockEndAdd < BlockStartAdd) |
lypinator | 0:bb348c97df44 | 1315 | { |
lypinator | 0:bb348c97df44 | 1316 | hsd->ErrorCode |= HAL_SD_ERROR_PARAM; |
lypinator | 0:bb348c97df44 | 1317 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1318 | } |
lypinator | 0:bb348c97df44 | 1319 | |
lypinator | 0:bb348c97df44 | 1320 | if(BlockEndAdd > (hsd->SdCard.LogBlockNbr)) |
lypinator | 0:bb348c97df44 | 1321 | { |
lypinator | 0:bb348c97df44 | 1322 | hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; |
lypinator | 0:bb348c97df44 | 1323 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1324 | } |
lypinator | 0:bb348c97df44 | 1325 | |
lypinator | 0:bb348c97df44 | 1326 | hsd->State = HAL_SD_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 1327 | |
lypinator | 0:bb348c97df44 | 1328 | /* Check if the card command class supports erase command */ |
lypinator | 0:bb348c97df44 | 1329 | if(((hsd->SdCard.Class) & SDIO_CCCC_ERASE) == 0U) |
lypinator | 0:bb348c97df44 | 1330 | { |
lypinator | 0:bb348c97df44 | 1331 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 1332 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 1333 | hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; |
lypinator | 0:bb348c97df44 | 1334 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1335 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1336 | } |
lypinator | 0:bb348c97df44 | 1337 | |
lypinator | 0:bb348c97df44 | 1338 | if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED) |
lypinator | 0:bb348c97df44 | 1339 | { |
lypinator | 0:bb348c97df44 | 1340 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 1341 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 1342 | hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED; |
lypinator | 0:bb348c97df44 | 1343 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1344 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1345 | } |
lypinator | 0:bb348c97df44 | 1346 | |
lypinator | 0:bb348c97df44 | 1347 | /* Get start and end block for high capacity cards */ |
lypinator | 0:bb348c97df44 | 1348 | if(hsd->SdCard.CardType != CARD_SDHC_SDXC) |
lypinator | 0:bb348c97df44 | 1349 | { |
lypinator | 0:bb348c97df44 | 1350 | BlockStartAdd *= 512U; |
lypinator | 0:bb348c97df44 | 1351 | BlockEndAdd *= 512U; |
lypinator | 0:bb348c97df44 | 1352 | } |
lypinator | 0:bb348c97df44 | 1353 | |
lypinator | 0:bb348c97df44 | 1354 | /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */ |
lypinator | 0:bb348c97df44 | 1355 | if(hsd->SdCard.CardType != CARD_SECURED) |
lypinator | 0:bb348c97df44 | 1356 | { |
lypinator | 0:bb348c97df44 | 1357 | /* Send CMD32 SD_ERASE_GRP_START with argument as addr */ |
lypinator | 0:bb348c97df44 | 1358 | errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, BlockStartAdd); |
lypinator | 0:bb348c97df44 | 1359 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 1360 | { |
lypinator | 0:bb348c97df44 | 1361 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 1362 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 1363 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 1364 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1365 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1366 | } |
lypinator | 0:bb348c97df44 | 1367 | |
lypinator | 0:bb348c97df44 | 1368 | /* Send CMD33 SD_ERASE_GRP_END with argument as addr */ |
lypinator | 0:bb348c97df44 | 1369 | errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, BlockEndAdd); |
lypinator | 0:bb348c97df44 | 1370 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 1371 | { |
lypinator | 0:bb348c97df44 | 1372 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 1373 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 1374 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 1375 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1376 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1377 | } |
lypinator | 0:bb348c97df44 | 1378 | } |
lypinator | 0:bb348c97df44 | 1379 | |
lypinator | 0:bb348c97df44 | 1380 | /* Send CMD38 ERASE */ |
lypinator | 0:bb348c97df44 | 1381 | errorstate = SDMMC_CmdErase(hsd->Instance); |
lypinator | 0:bb348c97df44 | 1382 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 1383 | { |
lypinator | 0:bb348c97df44 | 1384 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 1385 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 1386 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 1387 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1388 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1389 | } |
lypinator | 0:bb348c97df44 | 1390 | |
lypinator | 0:bb348c97df44 | 1391 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1392 | |
lypinator | 0:bb348c97df44 | 1393 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1394 | } |
lypinator | 0:bb348c97df44 | 1395 | else |
lypinator | 0:bb348c97df44 | 1396 | { |
lypinator | 0:bb348c97df44 | 1397 | return HAL_BUSY; |
lypinator | 0:bb348c97df44 | 1398 | } |
lypinator | 0:bb348c97df44 | 1399 | } |
lypinator | 0:bb348c97df44 | 1400 | |
lypinator | 0:bb348c97df44 | 1401 | /** |
lypinator | 0:bb348c97df44 | 1402 | * @brief This function handles SD card interrupt request. |
lypinator | 0:bb348c97df44 | 1403 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 1404 | * @retval None |
lypinator | 0:bb348c97df44 | 1405 | */ |
lypinator | 0:bb348c97df44 | 1406 | void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 1407 | { |
lypinator | 0:bb348c97df44 | 1408 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 1409 | |
lypinator | 0:bb348c97df44 | 1410 | /* Check for SDIO interrupt flags */ |
lypinator | 0:bb348c97df44 | 1411 | if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DATAEND) != RESET) |
lypinator | 0:bb348c97df44 | 1412 | { |
lypinator | 0:bb348c97df44 | 1413 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DATAEND); |
lypinator | 0:bb348c97df44 | 1414 | |
lypinator | 0:bb348c97df44 | 1415 | #ifdef SDIO_STA_STBITERR |
lypinator | 0:bb348c97df44 | 1416 | __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ |
lypinator | 0:bb348c97df44 | 1417 | SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR | SDIO_IT_STBITERR); |
lypinator | 0:bb348c97df44 | 1418 | #else /* SDIO_STA_STBITERR not defined */ |
lypinator | 0:bb348c97df44 | 1419 | __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ |
lypinator | 0:bb348c97df44 | 1420 | SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR); |
lypinator | 0:bb348c97df44 | 1421 | #endif |
lypinator | 0:bb348c97df44 | 1422 | |
lypinator | 0:bb348c97df44 | 1423 | if((hsd->Context & SD_CONTEXT_IT) != RESET) |
lypinator | 0:bb348c97df44 | 1424 | { |
lypinator | 0:bb348c97df44 | 1425 | if(((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != RESET) || ((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET)) |
lypinator | 0:bb348c97df44 | 1426 | { |
lypinator | 0:bb348c97df44 | 1427 | errorstate = SDMMC_CmdStopTransfer(hsd->Instance); |
lypinator | 0:bb348c97df44 | 1428 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 1429 | { |
lypinator | 0:bb348c97df44 | 1430 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 1431 | HAL_SD_ErrorCallback(hsd); |
lypinator | 0:bb348c97df44 | 1432 | } |
lypinator | 0:bb348c97df44 | 1433 | } |
lypinator | 0:bb348c97df44 | 1434 | |
lypinator | 0:bb348c97df44 | 1435 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 1436 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 1437 | |
lypinator | 0:bb348c97df44 | 1438 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1439 | if(((hsd->Context & SD_CONTEXT_READ_SINGLE_BLOCK) != RESET) || ((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != RESET)) |
lypinator | 0:bb348c97df44 | 1440 | { |
lypinator | 0:bb348c97df44 | 1441 | HAL_SD_RxCpltCallback(hsd); |
lypinator | 0:bb348c97df44 | 1442 | } |
lypinator | 0:bb348c97df44 | 1443 | else |
lypinator | 0:bb348c97df44 | 1444 | { |
lypinator | 0:bb348c97df44 | 1445 | HAL_SD_TxCpltCallback(hsd); |
lypinator | 0:bb348c97df44 | 1446 | } |
lypinator | 0:bb348c97df44 | 1447 | } |
lypinator | 0:bb348c97df44 | 1448 | else if((hsd->Context & SD_CONTEXT_DMA) != RESET) |
lypinator | 0:bb348c97df44 | 1449 | { |
lypinator | 0:bb348c97df44 | 1450 | if((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET) |
lypinator | 0:bb348c97df44 | 1451 | { |
lypinator | 0:bb348c97df44 | 1452 | errorstate = SDMMC_CmdStopTransfer(hsd->Instance); |
lypinator | 0:bb348c97df44 | 1453 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 1454 | { |
lypinator | 0:bb348c97df44 | 1455 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 1456 | HAL_SD_ErrorCallback(hsd); |
lypinator | 0:bb348c97df44 | 1457 | } |
lypinator | 0:bb348c97df44 | 1458 | } |
lypinator | 0:bb348c97df44 | 1459 | if(((hsd->Context & SD_CONTEXT_READ_SINGLE_BLOCK) == RESET) && ((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == RESET)) |
lypinator | 0:bb348c97df44 | 1460 | { |
lypinator | 0:bb348c97df44 | 1461 | /* Disable the DMA transfer for transmit request by setting the DMAEN bit |
lypinator | 0:bb348c97df44 | 1462 | in the SD DCTRL register */ |
lypinator | 0:bb348c97df44 | 1463 | hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN); |
lypinator | 0:bb348c97df44 | 1464 | |
lypinator | 0:bb348c97df44 | 1465 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1466 | |
lypinator | 0:bb348c97df44 | 1467 | HAL_SD_TxCpltCallback(hsd); |
lypinator | 0:bb348c97df44 | 1468 | } |
lypinator | 0:bb348c97df44 | 1469 | } |
lypinator | 0:bb348c97df44 | 1470 | } |
lypinator | 0:bb348c97df44 | 1471 | |
lypinator | 0:bb348c97df44 | 1472 | else if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_TXFIFOHE) != RESET) |
lypinator | 0:bb348c97df44 | 1473 | { |
lypinator | 0:bb348c97df44 | 1474 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_TXFIFOHE); |
lypinator | 0:bb348c97df44 | 1475 | |
lypinator | 0:bb348c97df44 | 1476 | SD_Write_IT(hsd); |
lypinator | 0:bb348c97df44 | 1477 | } |
lypinator | 0:bb348c97df44 | 1478 | |
lypinator | 0:bb348c97df44 | 1479 | else if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_RXFIFOHF) != RESET) |
lypinator | 0:bb348c97df44 | 1480 | { |
lypinator | 0:bb348c97df44 | 1481 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_RXFIFOHF); |
lypinator | 0:bb348c97df44 | 1482 | |
lypinator | 0:bb348c97df44 | 1483 | SD_Read_IT(hsd); |
lypinator | 0:bb348c97df44 | 1484 | } |
lypinator | 0:bb348c97df44 | 1485 | |
lypinator | 0:bb348c97df44 | 1486 | #ifdef SDIO_STA_STBITERR |
lypinator | 0:bb348c97df44 | 1487 | else if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR) != RESET) |
lypinator | 0:bb348c97df44 | 1488 | { |
lypinator | 0:bb348c97df44 | 1489 | /* Set Error code */ |
lypinator | 0:bb348c97df44 | 1490 | if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DCRCFAIL) != RESET) |
lypinator | 0:bb348c97df44 | 1491 | { |
lypinator | 0:bb348c97df44 | 1492 | hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL; |
lypinator | 0:bb348c97df44 | 1493 | } |
lypinator | 0:bb348c97df44 | 1494 | if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DTIMEOUT) != RESET) |
lypinator | 0:bb348c97df44 | 1495 | { |
lypinator | 0:bb348c97df44 | 1496 | hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1497 | } |
lypinator | 0:bb348c97df44 | 1498 | if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_RXOVERR) != RESET) |
lypinator | 0:bb348c97df44 | 1499 | { |
lypinator | 0:bb348c97df44 | 1500 | hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN; |
lypinator | 0:bb348c97df44 | 1501 | } |
lypinator | 0:bb348c97df44 | 1502 | if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_TXUNDERR) != RESET) |
lypinator | 0:bb348c97df44 | 1503 | { |
lypinator | 0:bb348c97df44 | 1504 | hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN; |
lypinator | 0:bb348c97df44 | 1505 | } |
lypinator | 0:bb348c97df44 | 1506 | if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_STBITERR) != RESET) |
lypinator | 0:bb348c97df44 | 1507 | { |
lypinator | 0:bb348c97df44 | 1508 | hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1509 | } |
lypinator | 0:bb348c97df44 | 1510 | |
lypinator | 0:bb348c97df44 | 1511 | /* Clear All flags */ |
lypinator | 0:bb348c97df44 | 1512 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS | SDIO_FLAG_STBITERR); |
lypinator | 0:bb348c97df44 | 1513 | |
lypinator | 0:bb348c97df44 | 1514 | /* Disable all interrupts */ |
lypinator | 0:bb348c97df44 | 1515 | __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ |
lypinator | 0:bb348c97df44 | 1516 | SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR |SDIO_IT_STBITERR); |
lypinator | 0:bb348c97df44 | 1517 | |
lypinator | 0:bb348c97df44 | 1518 | if((hsd->Context & SD_CONTEXT_DMA) != RESET) |
lypinator | 0:bb348c97df44 | 1519 | { |
lypinator | 0:bb348c97df44 | 1520 | /* Abort the SD DMA Streams */ |
lypinator | 0:bb348c97df44 | 1521 | if(hsd->hdmatx != NULL) |
lypinator | 0:bb348c97df44 | 1522 | { |
lypinator | 0:bb348c97df44 | 1523 | /* Set the DMA Tx abort callback */ |
lypinator | 0:bb348c97df44 | 1524 | hsd->hdmatx->XferAbortCallback = SD_DMATxAbort; |
lypinator | 0:bb348c97df44 | 1525 | /* Abort DMA in IT mode */ |
lypinator | 0:bb348c97df44 | 1526 | if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK) |
lypinator | 0:bb348c97df44 | 1527 | { |
lypinator | 0:bb348c97df44 | 1528 | SD_DMATxAbort(hsd->hdmatx); |
lypinator | 0:bb348c97df44 | 1529 | } |
lypinator | 0:bb348c97df44 | 1530 | } |
lypinator | 0:bb348c97df44 | 1531 | else if(hsd->hdmarx != NULL) |
lypinator | 0:bb348c97df44 | 1532 | { |
lypinator | 0:bb348c97df44 | 1533 | /* Set the DMA Rx abort callback */ |
lypinator | 0:bb348c97df44 | 1534 | hsd->hdmarx->XferAbortCallback = SD_DMARxAbort; |
lypinator | 0:bb348c97df44 | 1535 | /* Abort DMA in IT mode */ |
lypinator | 0:bb348c97df44 | 1536 | if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK) |
lypinator | 0:bb348c97df44 | 1537 | { |
lypinator | 0:bb348c97df44 | 1538 | SD_DMARxAbort(hsd->hdmarx); |
lypinator | 0:bb348c97df44 | 1539 | } |
lypinator | 0:bb348c97df44 | 1540 | } |
lypinator | 0:bb348c97df44 | 1541 | else |
lypinator | 0:bb348c97df44 | 1542 | { |
lypinator | 0:bb348c97df44 | 1543 | hsd->ErrorCode = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 1544 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1545 | HAL_SD_AbortCallback(hsd); |
lypinator | 0:bb348c97df44 | 1546 | } |
lypinator | 0:bb348c97df44 | 1547 | } |
lypinator | 0:bb348c97df44 | 1548 | else if((hsd->Context & SD_CONTEXT_IT) != RESET) |
lypinator | 0:bb348c97df44 | 1549 | { |
lypinator | 0:bb348c97df44 | 1550 | /* Set the SD state to ready to be able to start again the process */ |
lypinator | 0:bb348c97df44 | 1551 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1552 | HAL_SD_ErrorCallback(hsd); |
lypinator | 0:bb348c97df44 | 1553 | } |
lypinator | 0:bb348c97df44 | 1554 | } |
lypinator | 0:bb348c97df44 | 1555 | #else /* SDIO_STA_STBITERR not defined */ |
lypinator | 0:bb348c97df44 | 1556 | else if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_TXUNDERR) != RESET) |
lypinator | 0:bb348c97df44 | 1557 | { |
lypinator | 0:bb348c97df44 | 1558 | /* Set Error code */ |
lypinator | 0:bb348c97df44 | 1559 | if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DCRCFAIL) != RESET) |
lypinator | 0:bb348c97df44 | 1560 | { |
lypinator | 0:bb348c97df44 | 1561 | hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL; |
lypinator | 0:bb348c97df44 | 1562 | } |
lypinator | 0:bb348c97df44 | 1563 | if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DTIMEOUT) != RESET) |
lypinator | 0:bb348c97df44 | 1564 | { |
lypinator | 0:bb348c97df44 | 1565 | hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT; |
lypinator | 0:bb348c97df44 | 1566 | } |
lypinator | 0:bb348c97df44 | 1567 | if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_RXOVERR) != RESET) |
lypinator | 0:bb348c97df44 | 1568 | { |
lypinator | 0:bb348c97df44 | 1569 | hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN; |
lypinator | 0:bb348c97df44 | 1570 | } |
lypinator | 0:bb348c97df44 | 1571 | if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_TXUNDERR) != RESET) |
lypinator | 0:bb348c97df44 | 1572 | { |
lypinator | 0:bb348c97df44 | 1573 | hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN; |
lypinator | 0:bb348c97df44 | 1574 | } |
lypinator | 0:bb348c97df44 | 1575 | |
lypinator | 0:bb348c97df44 | 1576 | /* Clear All flags */ |
lypinator | 0:bb348c97df44 | 1577 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 1578 | |
lypinator | 0:bb348c97df44 | 1579 | /* Disable all interrupts */ |
lypinator | 0:bb348c97df44 | 1580 | __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ |
lypinator | 0:bb348c97df44 | 1581 | SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR); |
lypinator | 0:bb348c97df44 | 1582 | |
lypinator | 0:bb348c97df44 | 1583 | if((hsd->Context & SD_CONTEXT_DMA) != RESET) |
lypinator | 0:bb348c97df44 | 1584 | { |
lypinator | 0:bb348c97df44 | 1585 | /* Abort the SD DMA Streams */ |
lypinator | 0:bb348c97df44 | 1586 | if(hsd->hdmatx != NULL) |
lypinator | 0:bb348c97df44 | 1587 | { |
lypinator | 0:bb348c97df44 | 1588 | /* Set the DMA Tx abort callback */ |
lypinator | 0:bb348c97df44 | 1589 | hsd->hdmatx->XferAbortCallback = SD_DMATxAbort; |
lypinator | 0:bb348c97df44 | 1590 | /* Abort DMA in IT mode */ |
lypinator | 0:bb348c97df44 | 1591 | if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK) |
lypinator | 0:bb348c97df44 | 1592 | { |
lypinator | 0:bb348c97df44 | 1593 | SD_DMATxAbort(hsd->hdmatx); |
lypinator | 0:bb348c97df44 | 1594 | } |
lypinator | 0:bb348c97df44 | 1595 | } |
lypinator | 0:bb348c97df44 | 1596 | else if(hsd->hdmarx != NULL) |
lypinator | 0:bb348c97df44 | 1597 | { |
lypinator | 0:bb348c97df44 | 1598 | /* Set the DMA Rx abort callback */ |
lypinator | 0:bb348c97df44 | 1599 | hsd->hdmarx->XferAbortCallback = SD_DMARxAbort; |
lypinator | 0:bb348c97df44 | 1600 | /* Abort DMA in IT mode */ |
lypinator | 0:bb348c97df44 | 1601 | if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK) |
lypinator | 0:bb348c97df44 | 1602 | { |
lypinator | 0:bb348c97df44 | 1603 | SD_DMARxAbort(hsd->hdmarx); |
lypinator | 0:bb348c97df44 | 1604 | } |
lypinator | 0:bb348c97df44 | 1605 | } |
lypinator | 0:bb348c97df44 | 1606 | else |
lypinator | 0:bb348c97df44 | 1607 | { |
lypinator | 0:bb348c97df44 | 1608 | hsd->ErrorCode = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 1609 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1610 | HAL_SD_AbortCallback(hsd); |
lypinator | 0:bb348c97df44 | 1611 | } |
lypinator | 0:bb348c97df44 | 1612 | } |
lypinator | 0:bb348c97df44 | 1613 | else if((hsd->Context & SD_CONTEXT_IT) != RESET) |
lypinator | 0:bb348c97df44 | 1614 | { |
lypinator | 0:bb348c97df44 | 1615 | /* Set the SD state to ready to be able to start again the process */ |
lypinator | 0:bb348c97df44 | 1616 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1617 | HAL_SD_ErrorCallback(hsd); |
lypinator | 0:bb348c97df44 | 1618 | } |
lypinator | 0:bb348c97df44 | 1619 | } |
lypinator | 0:bb348c97df44 | 1620 | #endif |
lypinator | 0:bb348c97df44 | 1621 | } |
lypinator | 0:bb348c97df44 | 1622 | |
lypinator | 0:bb348c97df44 | 1623 | /** |
lypinator | 0:bb348c97df44 | 1624 | * @brief return the SD state |
lypinator | 0:bb348c97df44 | 1625 | * @param hsd Pointer to sd handle |
lypinator | 0:bb348c97df44 | 1626 | * @retval HAL state |
lypinator | 0:bb348c97df44 | 1627 | */ |
lypinator | 0:bb348c97df44 | 1628 | HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 1629 | { |
lypinator | 0:bb348c97df44 | 1630 | return hsd->State; |
lypinator | 0:bb348c97df44 | 1631 | } |
lypinator | 0:bb348c97df44 | 1632 | |
lypinator | 0:bb348c97df44 | 1633 | /** |
lypinator | 0:bb348c97df44 | 1634 | * @brief Return the SD error code |
lypinator | 0:bb348c97df44 | 1635 | * @param hsd Pointer to a SD_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 1636 | * the configuration information. |
lypinator | 0:bb348c97df44 | 1637 | * @retval SD Error Code |
lypinator | 0:bb348c97df44 | 1638 | */ |
lypinator | 0:bb348c97df44 | 1639 | uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 1640 | { |
lypinator | 0:bb348c97df44 | 1641 | return hsd->ErrorCode; |
lypinator | 0:bb348c97df44 | 1642 | } |
lypinator | 0:bb348c97df44 | 1643 | |
lypinator | 0:bb348c97df44 | 1644 | /** |
lypinator | 0:bb348c97df44 | 1645 | * @brief Tx Transfer completed callbacks |
lypinator | 0:bb348c97df44 | 1646 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 1647 | * @retval None |
lypinator | 0:bb348c97df44 | 1648 | */ |
lypinator | 0:bb348c97df44 | 1649 | __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 1650 | { |
lypinator | 0:bb348c97df44 | 1651 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 1652 | UNUSED(hsd); |
lypinator | 0:bb348c97df44 | 1653 | |
lypinator | 0:bb348c97df44 | 1654 | /* NOTE : This function should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 1655 | the HAL_SD_TxCpltCallback can be implemented in the user file |
lypinator | 0:bb348c97df44 | 1656 | */ |
lypinator | 0:bb348c97df44 | 1657 | } |
lypinator | 0:bb348c97df44 | 1658 | |
lypinator | 0:bb348c97df44 | 1659 | /** |
lypinator | 0:bb348c97df44 | 1660 | * @brief Rx Transfer completed callbacks |
lypinator | 0:bb348c97df44 | 1661 | * @param hsd Pointer SD handle |
lypinator | 0:bb348c97df44 | 1662 | * @retval None |
lypinator | 0:bb348c97df44 | 1663 | */ |
lypinator | 0:bb348c97df44 | 1664 | __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 1665 | { |
lypinator | 0:bb348c97df44 | 1666 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 1667 | UNUSED(hsd); |
lypinator | 0:bb348c97df44 | 1668 | |
lypinator | 0:bb348c97df44 | 1669 | /* NOTE : This function should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 1670 | the HAL_SD_RxCpltCallback can be implemented in the user file |
lypinator | 0:bb348c97df44 | 1671 | */ |
lypinator | 0:bb348c97df44 | 1672 | } |
lypinator | 0:bb348c97df44 | 1673 | |
lypinator | 0:bb348c97df44 | 1674 | /** |
lypinator | 0:bb348c97df44 | 1675 | * @brief SD error callbacks |
lypinator | 0:bb348c97df44 | 1676 | * @param hsd Pointer SD handle |
lypinator | 0:bb348c97df44 | 1677 | * @retval None |
lypinator | 0:bb348c97df44 | 1678 | */ |
lypinator | 0:bb348c97df44 | 1679 | __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 1680 | { |
lypinator | 0:bb348c97df44 | 1681 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 1682 | UNUSED(hsd); |
lypinator | 0:bb348c97df44 | 1683 | |
lypinator | 0:bb348c97df44 | 1684 | /* NOTE : This function should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 1685 | the HAL_SD_ErrorCallback can be implemented in the user file |
lypinator | 0:bb348c97df44 | 1686 | */ |
lypinator | 0:bb348c97df44 | 1687 | } |
lypinator | 0:bb348c97df44 | 1688 | |
lypinator | 0:bb348c97df44 | 1689 | /** |
lypinator | 0:bb348c97df44 | 1690 | * @brief SD Abort callbacks |
lypinator | 0:bb348c97df44 | 1691 | * @param hsd Pointer SD handle |
lypinator | 0:bb348c97df44 | 1692 | * @retval None |
lypinator | 0:bb348c97df44 | 1693 | */ |
lypinator | 0:bb348c97df44 | 1694 | __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 1695 | { |
lypinator | 0:bb348c97df44 | 1696 | /* Prevent unused argument(s) compilation warning */ |
lypinator | 0:bb348c97df44 | 1697 | UNUSED(hsd); |
lypinator | 0:bb348c97df44 | 1698 | |
lypinator | 0:bb348c97df44 | 1699 | /* NOTE : This function should not be modified, when the callback is needed, |
lypinator | 0:bb348c97df44 | 1700 | the HAL_SD_ErrorCallback can be implemented in the user file |
lypinator | 0:bb348c97df44 | 1701 | */ |
lypinator | 0:bb348c97df44 | 1702 | } |
lypinator | 0:bb348c97df44 | 1703 | |
lypinator | 0:bb348c97df44 | 1704 | |
lypinator | 0:bb348c97df44 | 1705 | /** |
lypinator | 0:bb348c97df44 | 1706 | * @} |
lypinator | 0:bb348c97df44 | 1707 | */ |
lypinator | 0:bb348c97df44 | 1708 | |
lypinator | 0:bb348c97df44 | 1709 | /** @addtogroup SD_Exported_Functions_Group3 |
lypinator | 0:bb348c97df44 | 1710 | * @brief management functions |
lypinator | 0:bb348c97df44 | 1711 | * |
lypinator | 0:bb348c97df44 | 1712 | @verbatim |
lypinator | 0:bb348c97df44 | 1713 | ============================================================================== |
lypinator | 0:bb348c97df44 | 1714 | ##### Peripheral Control functions ##### |
lypinator | 0:bb348c97df44 | 1715 | ============================================================================== |
lypinator | 0:bb348c97df44 | 1716 | [..] |
lypinator | 0:bb348c97df44 | 1717 | This subsection provides a set of functions allowing to control the SD card |
lypinator | 0:bb348c97df44 | 1718 | operations and get the related information |
lypinator | 0:bb348c97df44 | 1719 | |
lypinator | 0:bb348c97df44 | 1720 | @endverbatim |
lypinator | 0:bb348c97df44 | 1721 | * @{ |
lypinator | 0:bb348c97df44 | 1722 | */ |
lypinator | 0:bb348c97df44 | 1723 | |
lypinator | 0:bb348c97df44 | 1724 | /** |
lypinator | 0:bb348c97df44 | 1725 | * @brief Returns information the information of the card which are stored on |
lypinator | 0:bb348c97df44 | 1726 | * the CID register. |
lypinator | 0:bb348c97df44 | 1727 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 1728 | * @param pCID Pointer to a HAL_SD_CIDTypeDef structure that |
lypinator | 0:bb348c97df44 | 1729 | * contains all CID register parameters |
lypinator | 0:bb348c97df44 | 1730 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1731 | */ |
lypinator | 0:bb348c97df44 | 1732 | HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID) |
lypinator | 0:bb348c97df44 | 1733 | { |
lypinator | 0:bb348c97df44 | 1734 | uint32_t tmp = 0U; |
lypinator | 0:bb348c97df44 | 1735 | |
lypinator | 0:bb348c97df44 | 1736 | /* Byte 0 */ |
lypinator | 0:bb348c97df44 | 1737 | tmp = (uint8_t)((hsd->CID[0U] & 0xFF000000U) >> 24U); |
lypinator | 0:bb348c97df44 | 1738 | pCID->ManufacturerID = tmp; |
lypinator | 0:bb348c97df44 | 1739 | |
lypinator | 0:bb348c97df44 | 1740 | /* Byte 1 */ |
lypinator | 0:bb348c97df44 | 1741 | tmp = (uint8_t)((hsd->CID[0U] & 0x00FF0000U) >> 16U); |
lypinator | 0:bb348c97df44 | 1742 | pCID->OEM_AppliID = tmp << 8U; |
lypinator | 0:bb348c97df44 | 1743 | |
lypinator | 0:bb348c97df44 | 1744 | /* Byte 2 */ |
lypinator | 0:bb348c97df44 | 1745 | tmp = (uint8_t)((hsd->CID[0U] & 0x000000FF00U) >> 8U); |
lypinator | 0:bb348c97df44 | 1746 | pCID->OEM_AppliID |= tmp; |
lypinator | 0:bb348c97df44 | 1747 | |
lypinator | 0:bb348c97df44 | 1748 | /* Byte 3 */ |
lypinator | 0:bb348c97df44 | 1749 | tmp = (uint8_t)(hsd->CID[0U] & 0x000000FFU); |
lypinator | 0:bb348c97df44 | 1750 | pCID->ProdName1 = tmp << 24U; |
lypinator | 0:bb348c97df44 | 1751 | |
lypinator | 0:bb348c97df44 | 1752 | /* Byte 4 */ |
lypinator | 0:bb348c97df44 | 1753 | tmp = (uint8_t)((hsd->CID[1U] & 0xFF000000U) >> 24U); |
lypinator | 0:bb348c97df44 | 1754 | pCID->ProdName1 |= tmp << 16; |
lypinator | 0:bb348c97df44 | 1755 | |
lypinator | 0:bb348c97df44 | 1756 | /* Byte 5 */ |
lypinator | 0:bb348c97df44 | 1757 | tmp = (uint8_t)((hsd->CID[1U] & 0x00FF0000U) >> 16U); |
lypinator | 0:bb348c97df44 | 1758 | pCID->ProdName1 |= tmp << 8U; |
lypinator | 0:bb348c97df44 | 1759 | |
lypinator | 0:bb348c97df44 | 1760 | /* Byte 6 */ |
lypinator | 0:bb348c97df44 | 1761 | tmp = (uint8_t)((hsd->CID[1U] & 0x0000FF00U) >> 8U); |
lypinator | 0:bb348c97df44 | 1762 | pCID->ProdName1 |= tmp; |
lypinator | 0:bb348c97df44 | 1763 | |
lypinator | 0:bb348c97df44 | 1764 | /* Byte 7 */ |
lypinator | 0:bb348c97df44 | 1765 | tmp = (uint8_t)(hsd->CID[1U] & 0x000000FFU); |
lypinator | 0:bb348c97df44 | 1766 | pCID->ProdName2 = tmp; |
lypinator | 0:bb348c97df44 | 1767 | |
lypinator | 0:bb348c97df44 | 1768 | /* Byte 8 */ |
lypinator | 0:bb348c97df44 | 1769 | tmp = (uint8_t)((hsd->CID[2U] & 0xFF000000U) >> 24U); |
lypinator | 0:bb348c97df44 | 1770 | pCID->ProdRev = tmp; |
lypinator | 0:bb348c97df44 | 1771 | |
lypinator | 0:bb348c97df44 | 1772 | /* Byte 9 */ |
lypinator | 0:bb348c97df44 | 1773 | tmp = (uint8_t)((hsd->CID[2U] & 0x00FF0000U) >> 16U); |
lypinator | 0:bb348c97df44 | 1774 | pCID->ProdSN = tmp << 24U; |
lypinator | 0:bb348c97df44 | 1775 | |
lypinator | 0:bb348c97df44 | 1776 | /* Byte 10 */ |
lypinator | 0:bb348c97df44 | 1777 | tmp = (uint8_t)((hsd->CID[2U] & 0x0000FF00U) >> 8U); |
lypinator | 0:bb348c97df44 | 1778 | pCID->ProdSN |= tmp << 16U; |
lypinator | 0:bb348c97df44 | 1779 | |
lypinator | 0:bb348c97df44 | 1780 | /* Byte 11 */ |
lypinator | 0:bb348c97df44 | 1781 | tmp = (uint8_t)(hsd->CID[2U] & 0x000000FFU); |
lypinator | 0:bb348c97df44 | 1782 | pCID->ProdSN |= tmp << 8U; |
lypinator | 0:bb348c97df44 | 1783 | |
lypinator | 0:bb348c97df44 | 1784 | /* Byte 12 */ |
lypinator | 0:bb348c97df44 | 1785 | tmp = (uint8_t)((hsd->CID[3U] & 0xFF000000U) >> 24U); |
lypinator | 0:bb348c97df44 | 1786 | pCID->ProdSN |= tmp; |
lypinator | 0:bb348c97df44 | 1787 | |
lypinator | 0:bb348c97df44 | 1788 | /* Byte 13 */ |
lypinator | 0:bb348c97df44 | 1789 | tmp = (uint8_t)((hsd->CID[3U] & 0x00FF0000U) >> 16U); |
lypinator | 0:bb348c97df44 | 1790 | pCID->Reserved1 |= (tmp & 0xF0U) >> 4U; |
lypinator | 0:bb348c97df44 | 1791 | pCID->ManufactDate = (tmp & 0x0FU) << 8U; |
lypinator | 0:bb348c97df44 | 1792 | |
lypinator | 0:bb348c97df44 | 1793 | /* Byte 14 */ |
lypinator | 0:bb348c97df44 | 1794 | tmp = (uint8_t)((hsd->CID[3U] & 0x0000FF00U) >> 8U); |
lypinator | 0:bb348c97df44 | 1795 | pCID->ManufactDate |= tmp; |
lypinator | 0:bb348c97df44 | 1796 | |
lypinator | 0:bb348c97df44 | 1797 | /* Byte 15 */ |
lypinator | 0:bb348c97df44 | 1798 | tmp = (uint8_t)(hsd->CID[3U] & 0x000000FFU); |
lypinator | 0:bb348c97df44 | 1799 | pCID->CID_CRC = (tmp & 0xFEU) >> 1U; |
lypinator | 0:bb348c97df44 | 1800 | pCID->Reserved2 = 1U; |
lypinator | 0:bb348c97df44 | 1801 | |
lypinator | 0:bb348c97df44 | 1802 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1803 | } |
lypinator | 0:bb348c97df44 | 1804 | |
lypinator | 0:bb348c97df44 | 1805 | /** |
lypinator | 0:bb348c97df44 | 1806 | * @brief Returns information the information of the card which are stored on |
lypinator | 0:bb348c97df44 | 1807 | * the CSD register. |
lypinator | 0:bb348c97df44 | 1808 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 1809 | * @param pCSD Pointer to a HAL_SD_CardCSDTypeDef structure that |
lypinator | 0:bb348c97df44 | 1810 | * contains all CSD register parameters |
lypinator | 0:bb348c97df44 | 1811 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1812 | */ |
lypinator | 0:bb348c97df44 | 1813 | HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD) |
lypinator | 0:bb348c97df44 | 1814 | { |
lypinator | 0:bb348c97df44 | 1815 | uint32_t tmp = 0U; |
lypinator | 0:bb348c97df44 | 1816 | |
lypinator | 0:bb348c97df44 | 1817 | /* Byte 0 */ |
lypinator | 0:bb348c97df44 | 1818 | tmp = (hsd->CSD[0U] & 0xFF000000U) >> 24U; |
lypinator | 0:bb348c97df44 | 1819 | pCSD->CSDStruct = (uint8_t)((tmp & 0xC0U) >> 6U); |
lypinator | 0:bb348c97df44 | 1820 | pCSD->SysSpecVersion = (uint8_t)((tmp & 0x3CU) >> 2U); |
lypinator | 0:bb348c97df44 | 1821 | pCSD->Reserved1 = tmp & 0x03U; |
lypinator | 0:bb348c97df44 | 1822 | |
lypinator | 0:bb348c97df44 | 1823 | /* Byte 1 */ |
lypinator | 0:bb348c97df44 | 1824 | tmp = (hsd->CSD[0U] & 0x00FF0000U) >> 16U; |
lypinator | 0:bb348c97df44 | 1825 | pCSD->TAAC = (uint8_t)tmp; |
lypinator | 0:bb348c97df44 | 1826 | |
lypinator | 0:bb348c97df44 | 1827 | /* Byte 2 */ |
lypinator | 0:bb348c97df44 | 1828 | tmp = (hsd->CSD[0U] & 0x0000FF00U) >> 8U; |
lypinator | 0:bb348c97df44 | 1829 | pCSD->NSAC = (uint8_t)tmp; |
lypinator | 0:bb348c97df44 | 1830 | |
lypinator | 0:bb348c97df44 | 1831 | /* Byte 3 */ |
lypinator | 0:bb348c97df44 | 1832 | tmp = hsd->CSD[0U] & 0x000000FFU; |
lypinator | 0:bb348c97df44 | 1833 | pCSD->MaxBusClkFrec = (uint8_t)tmp; |
lypinator | 0:bb348c97df44 | 1834 | |
lypinator | 0:bb348c97df44 | 1835 | /* Byte 4 */ |
lypinator | 0:bb348c97df44 | 1836 | tmp = (hsd->CSD[1U] & 0xFF000000U) >> 24U; |
lypinator | 0:bb348c97df44 | 1837 | pCSD->CardComdClasses = (uint16_t)(tmp << 4U); |
lypinator | 0:bb348c97df44 | 1838 | |
lypinator | 0:bb348c97df44 | 1839 | /* Byte 5 */ |
lypinator | 0:bb348c97df44 | 1840 | tmp = (hsd->CSD[1U] & 0x00FF0000U) >> 16U; |
lypinator | 0:bb348c97df44 | 1841 | pCSD->CardComdClasses |= (uint16_t)((tmp & 0xF0U) >> 4U); |
lypinator | 0:bb348c97df44 | 1842 | pCSD->RdBlockLen = (uint8_t)(tmp & 0x0FU); |
lypinator | 0:bb348c97df44 | 1843 | |
lypinator | 0:bb348c97df44 | 1844 | /* Byte 6 */ |
lypinator | 0:bb348c97df44 | 1845 | tmp = (hsd->CSD[1U] & 0x0000FF00U) >> 8U; |
lypinator | 0:bb348c97df44 | 1846 | pCSD->PartBlockRead = (uint8_t)((tmp & 0x80U) >> 7U); |
lypinator | 0:bb348c97df44 | 1847 | pCSD->WrBlockMisalign = (uint8_t)((tmp & 0x40U) >> 6U); |
lypinator | 0:bb348c97df44 | 1848 | pCSD->RdBlockMisalign = (uint8_t)((tmp & 0x20U) >> 5U); |
lypinator | 0:bb348c97df44 | 1849 | pCSD->DSRImpl = (uint8_t)((tmp & 0x10U) >> 4U); |
lypinator | 0:bb348c97df44 | 1850 | pCSD->Reserved2 = 0U; /*!< Reserved */ |
lypinator | 0:bb348c97df44 | 1851 | |
lypinator | 0:bb348c97df44 | 1852 | if(hsd->SdCard.CardType == CARD_SDSC) |
lypinator | 0:bb348c97df44 | 1853 | { |
lypinator | 0:bb348c97df44 | 1854 | pCSD->DeviceSize = (tmp & 0x03U) << 10U; |
lypinator | 0:bb348c97df44 | 1855 | |
lypinator | 0:bb348c97df44 | 1856 | /* Byte 7 */ |
lypinator | 0:bb348c97df44 | 1857 | tmp = (uint8_t)(hsd->CSD[1U] & 0x000000FFU); |
lypinator | 0:bb348c97df44 | 1858 | pCSD->DeviceSize |= (tmp) << 2U; |
lypinator | 0:bb348c97df44 | 1859 | |
lypinator | 0:bb348c97df44 | 1860 | /* Byte 8 */ |
lypinator | 0:bb348c97df44 | 1861 | tmp = (uint8_t)((hsd->CSD[2U] & 0xFF000000U) >> 24U); |
lypinator | 0:bb348c97df44 | 1862 | pCSD->DeviceSize |= (tmp & 0xC0U) >> 6U; |
lypinator | 0:bb348c97df44 | 1863 | |
lypinator | 0:bb348c97df44 | 1864 | pCSD->MaxRdCurrentVDDMin = (tmp & 0x38U) >> 3U; |
lypinator | 0:bb348c97df44 | 1865 | pCSD->MaxRdCurrentVDDMax = (tmp & 0x07U); |
lypinator | 0:bb348c97df44 | 1866 | |
lypinator | 0:bb348c97df44 | 1867 | /* Byte 9 */ |
lypinator | 0:bb348c97df44 | 1868 | tmp = (uint8_t)((hsd->CSD[2U] & 0x00FF0000U) >> 16U); |
lypinator | 0:bb348c97df44 | 1869 | pCSD->MaxWrCurrentVDDMin = (tmp & 0xE0U) >> 5U; |
lypinator | 0:bb348c97df44 | 1870 | pCSD->MaxWrCurrentVDDMax = (tmp & 0x1CU) >> 2U; |
lypinator | 0:bb348c97df44 | 1871 | pCSD->DeviceSizeMul = (tmp & 0x03U) << 1U; |
lypinator | 0:bb348c97df44 | 1872 | /* Byte 10 */ |
lypinator | 0:bb348c97df44 | 1873 | tmp = (uint8_t)((hsd->CSD[2U] & 0x0000FF00U) >> 8U); |
lypinator | 0:bb348c97df44 | 1874 | pCSD->DeviceSizeMul |= (tmp & 0x80U) >> 7U; |
lypinator | 0:bb348c97df44 | 1875 | |
lypinator | 0:bb348c97df44 | 1876 | hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ; |
lypinator | 0:bb348c97df44 | 1877 | hsd->SdCard.BlockNbr *= (1U << (pCSD->DeviceSizeMul + 2U)); |
lypinator | 0:bb348c97df44 | 1878 | hsd->SdCard.BlockSize = 1U << (pCSD->RdBlockLen); |
lypinator | 0:bb348c97df44 | 1879 | |
lypinator | 0:bb348c97df44 | 1880 | hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U); |
lypinator | 0:bb348c97df44 | 1881 | hsd->SdCard.LogBlockSize = 512U; |
lypinator | 0:bb348c97df44 | 1882 | } |
lypinator | 0:bb348c97df44 | 1883 | else if(hsd->SdCard.CardType == CARD_SDHC_SDXC) |
lypinator | 0:bb348c97df44 | 1884 | { |
lypinator | 0:bb348c97df44 | 1885 | /* Byte 7 */ |
lypinator | 0:bb348c97df44 | 1886 | tmp = (uint8_t)(hsd->CSD[1U] & 0x000000FFU); |
lypinator | 0:bb348c97df44 | 1887 | pCSD->DeviceSize = (tmp & 0x3FU) << 16U; |
lypinator | 0:bb348c97df44 | 1888 | |
lypinator | 0:bb348c97df44 | 1889 | /* Byte 8 */ |
lypinator | 0:bb348c97df44 | 1890 | tmp = (uint8_t)((hsd->CSD[2U] & 0xFF000000U) >> 24U); |
lypinator | 0:bb348c97df44 | 1891 | |
lypinator | 0:bb348c97df44 | 1892 | pCSD->DeviceSize |= (tmp << 8U); |
lypinator | 0:bb348c97df44 | 1893 | |
lypinator | 0:bb348c97df44 | 1894 | /* Byte 9 */ |
lypinator | 0:bb348c97df44 | 1895 | tmp = (uint8_t)((hsd->CSD[2U] & 0x00FF0000U) >> 16U); |
lypinator | 0:bb348c97df44 | 1896 | |
lypinator | 0:bb348c97df44 | 1897 | pCSD->DeviceSize |= (tmp); |
lypinator | 0:bb348c97df44 | 1898 | |
lypinator | 0:bb348c97df44 | 1899 | /* Byte 10 */ |
lypinator | 0:bb348c97df44 | 1900 | tmp = (uint8_t)((hsd->CSD[2U] & 0x0000FF00U) >> 8U); |
lypinator | 0:bb348c97df44 | 1901 | |
lypinator | 0:bb348c97df44 | 1902 | hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr = (((uint64_t)pCSD->DeviceSize + 1U) * 1024U); |
lypinator | 0:bb348c97df44 | 1903 | hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize = 512U; |
lypinator | 0:bb348c97df44 | 1904 | } |
lypinator | 0:bb348c97df44 | 1905 | else |
lypinator | 0:bb348c97df44 | 1906 | { |
lypinator | 0:bb348c97df44 | 1907 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 1908 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 1909 | hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; |
lypinator | 0:bb348c97df44 | 1910 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1911 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1912 | } |
lypinator | 0:bb348c97df44 | 1913 | |
lypinator | 0:bb348c97df44 | 1914 | pCSD->EraseGrSize = (tmp & 0x40U) >> 6U; |
lypinator | 0:bb348c97df44 | 1915 | pCSD->EraseGrMul = (tmp & 0x3FU) << 1U; |
lypinator | 0:bb348c97df44 | 1916 | |
lypinator | 0:bb348c97df44 | 1917 | /* Byte 11 */ |
lypinator | 0:bb348c97df44 | 1918 | tmp = (uint8_t)(hsd->CSD[2U] & 0x000000FFU); |
lypinator | 0:bb348c97df44 | 1919 | pCSD->EraseGrMul |= (tmp & 0x80U) >> 7U; |
lypinator | 0:bb348c97df44 | 1920 | pCSD->WrProtectGrSize = (tmp & 0x7FU); |
lypinator | 0:bb348c97df44 | 1921 | |
lypinator | 0:bb348c97df44 | 1922 | /* Byte 12 */ |
lypinator | 0:bb348c97df44 | 1923 | tmp = (uint8_t)((hsd->CSD[3U] & 0xFF000000U) >> 24U); |
lypinator | 0:bb348c97df44 | 1924 | pCSD->WrProtectGrEnable = (tmp & 0x80U) >> 7U; |
lypinator | 0:bb348c97df44 | 1925 | pCSD->ManDeflECC = (tmp & 0x60U) >> 5U; |
lypinator | 0:bb348c97df44 | 1926 | pCSD->WrSpeedFact = (tmp & 0x1CU) >> 2U; |
lypinator | 0:bb348c97df44 | 1927 | pCSD->MaxWrBlockLen = (tmp & 0x03U) << 2U; |
lypinator | 0:bb348c97df44 | 1928 | |
lypinator | 0:bb348c97df44 | 1929 | /* Byte 13 */ |
lypinator | 0:bb348c97df44 | 1930 | tmp = (uint8_t)((hsd->CSD[3U] & 0x00FF0000U) >> 16U); |
lypinator | 0:bb348c97df44 | 1931 | pCSD->MaxWrBlockLen |= (tmp & 0xC0U) >> 6U; |
lypinator | 0:bb348c97df44 | 1932 | pCSD->WriteBlockPaPartial = (tmp & 0x20U) >> 5U; |
lypinator | 0:bb348c97df44 | 1933 | pCSD->Reserved3 = 0U; |
lypinator | 0:bb348c97df44 | 1934 | pCSD->ContentProtectAppli = (tmp & 0x01U); |
lypinator | 0:bb348c97df44 | 1935 | |
lypinator | 0:bb348c97df44 | 1936 | /* Byte 14 */ |
lypinator | 0:bb348c97df44 | 1937 | tmp = (uint8_t)((hsd->CSD[3U] & 0x0000FF00U) >> 8U); |
lypinator | 0:bb348c97df44 | 1938 | pCSD->FileFormatGrouop = (tmp & 0x80U) >> 7U; |
lypinator | 0:bb348c97df44 | 1939 | pCSD->CopyFlag = (tmp & 0x40U) >> 6U; |
lypinator | 0:bb348c97df44 | 1940 | pCSD->PermWrProtect = (tmp & 0x20U) >> 5U; |
lypinator | 0:bb348c97df44 | 1941 | pCSD->TempWrProtect = (tmp & 0x10U) >> 4U; |
lypinator | 0:bb348c97df44 | 1942 | pCSD->FileFormat = (tmp & 0x0CU) >> 2U; |
lypinator | 0:bb348c97df44 | 1943 | pCSD->ECC = (tmp & 0x03U); |
lypinator | 0:bb348c97df44 | 1944 | |
lypinator | 0:bb348c97df44 | 1945 | /* Byte 15 */ |
lypinator | 0:bb348c97df44 | 1946 | tmp = (uint8_t)(hsd->CSD[3U] & 0x000000FFU); |
lypinator | 0:bb348c97df44 | 1947 | pCSD->CSD_CRC = (tmp & 0xFEU) >> 1U; |
lypinator | 0:bb348c97df44 | 1948 | pCSD->Reserved4 = 1U; |
lypinator | 0:bb348c97df44 | 1949 | |
lypinator | 0:bb348c97df44 | 1950 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 1951 | } |
lypinator | 0:bb348c97df44 | 1952 | |
lypinator | 0:bb348c97df44 | 1953 | /** |
lypinator | 0:bb348c97df44 | 1954 | * @brief Gets the SD status info. |
lypinator | 0:bb348c97df44 | 1955 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 1956 | * @param pStatus Pointer to the HAL_SD_CardStatusTypeDef structure that |
lypinator | 0:bb348c97df44 | 1957 | * will contain the SD card status information |
lypinator | 0:bb348c97df44 | 1958 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 1959 | */ |
lypinator | 0:bb348c97df44 | 1960 | HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus) |
lypinator | 0:bb348c97df44 | 1961 | { |
lypinator | 0:bb348c97df44 | 1962 | uint32_t tmp = 0U; |
lypinator | 0:bb348c97df44 | 1963 | uint32_t sd_status[16U]; |
lypinator | 0:bb348c97df44 | 1964 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 1965 | |
lypinator | 0:bb348c97df44 | 1966 | errorstate = SD_SendSDStatus(hsd, sd_status); |
lypinator | 0:bb348c97df44 | 1967 | if(errorstate != HAL_OK) |
lypinator | 0:bb348c97df44 | 1968 | { |
lypinator | 0:bb348c97df44 | 1969 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 1970 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 1971 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 1972 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 1973 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 1974 | } |
lypinator | 0:bb348c97df44 | 1975 | else |
lypinator | 0:bb348c97df44 | 1976 | { |
lypinator | 0:bb348c97df44 | 1977 | /* Byte 0 */ |
lypinator | 0:bb348c97df44 | 1978 | tmp = (sd_status[0U] & 0xC0U) >> 6U; |
lypinator | 0:bb348c97df44 | 1979 | pStatus->DataBusWidth = (uint8_t)tmp; |
lypinator | 0:bb348c97df44 | 1980 | |
lypinator | 0:bb348c97df44 | 1981 | /* Byte 0 */ |
lypinator | 0:bb348c97df44 | 1982 | tmp = (sd_status[0U] & 0x20U) >> 5U; |
lypinator | 0:bb348c97df44 | 1983 | pStatus->SecuredMode = (uint8_t)tmp; |
lypinator | 0:bb348c97df44 | 1984 | |
lypinator | 0:bb348c97df44 | 1985 | /* Byte 2 */ |
lypinator | 0:bb348c97df44 | 1986 | tmp = (sd_status[0U] & 0x00FF0000U) >> 16U; |
lypinator | 0:bb348c97df44 | 1987 | pStatus->CardType = (uint16_t)(tmp << 8U); |
lypinator | 0:bb348c97df44 | 1988 | |
lypinator | 0:bb348c97df44 | 1989 | /* Byte 3 */ |
lypinator | 0:bb348c97df44 | 1990 | tmp = (sd_status[0U] & 0xFF000000U) >> 24U; |
lypinator | 0:bb348c97df44 | 1991 | pStatus->CardType |= (uint16_t)tmp; |
lypinator | 0:bb348c97df44 | 1992 | |
lypinator | 0:bb348c97df44 | 1993 | /* Byte 4 */ |
lypinator | 0:bb348c97df44 | 1994 | tmp = (sd_status[1U] & 0xFFU); |
lypinator | 0:bb348c97df44 | 1995 | pStatus->ProtectedAreaSize = (uint32_t)(tmp << 24U); |
lypinator | 0:bb348c97df44 | 1996 | |
lypinator | 0:bb348c97df44 | 1997 | /* Byte 5 */ |
lypinator | 0:bb348c97df44 | 1998 | tmp = (sd_status[1U] & 0xFF00U) >> 8U; |
lypinator | 0:bb348c97df44 | 1999 | pStatus->ProtectedAreaSize |= (uint32_t)(tmp << 16U); |
lypinator | 0:bb348c97df44 | 2000 | |
lypinator | 0:bb348c97df44 | 2001 | /* Byte 6 */ |
lypinator | 0:bb348c97df44 | 2002 | tmp = (sd_status[1U] & 0xFF0000U) >> 16U; |
lypinator | 0:bb348c97df44 | 2003 | pStatus->ProtectedAreaSize |= (uint32_t)(tmp << 8U); |
lypinator | 0:bb348c97df44 | 2004 | |
lypinator | 0:bb348c97df44 | 2005 | /* Byte 7 */ |
lypinator | 0:bb348c97df44 | 2006 | tmp = (sd_status[1U] & 0xFF000000U) >> 24U; |
lypinator | 0:bb348c97df44 | 2007 | pStatus->ProtectedAreaSize |= (uint32_t)tmp; |
lypinator | 0:bb348c97df44 | 2008 | |
lypinator | 0:bb348c97df44 | 2009 | /* Byte 8 */ |
lypinator | 0:bb348c97df44 | 2010 | tmp = (sd_status[2U] & 0xFFU); |
lypinator | 0:bb348c97df44 | 2011 | pStatus->SpeedClass = (uint8_t)tmp; |
lypinator | 0:bb348c97df44 | 2012 | |
lypinator | 0:bb348c97df44 | 2013 | /* Byte 9 */ |
lypinator | 0:bb348c97df44 | 2014 | tmp = (sd_status[2U] & 0xFF00U) >> 8U; |
lypinator | 0:bb348c97df44 | 2015 | pStatus->PerformanceMove = (uint8_t)tmp; |
lypinator | 0:bb348c97df44 | 2016 | |
lypinator | 0:bb348c97df44 | 2017 | /* Byte 10 */ |
lypinator | 0:bb348c97df44 | 2018 | tmp = (sd_status[2U] & 0xF00000U) >> 20U; |
lypinator | 0:bb348c97df44 | 2019 | pStatus->AllocationUnitSize = (uint8_t)tmp; |
lypinator | 0:bb348c97df44 | 2020 | |
lypinator | 0:bb348c97df44 | 2021 | /* Byte 11 */ |
lypinator | 0:bb348c97df44 | 2022 | tmp = (sd_status[2U] & 0xFF000000U) >> 24U; |
lypinator | 0:bb348c97df44 | 2023 | pStatus->EraseSize = (uint16_t)(tmp << 8U); |
lypinator | 0:bb348c97df44 | 2024 | |
lypinator | 0:bb348c97df44 | 2025 | /* Byte 12 */ |
lypinator | 0:bb348c97df44 | 2026 | tmp = (sd_status[3U] & 0xFFU); |
lypinator | 0:bb348c97df44 | 2027 | pStatus->EraseSize |= (uint16_t)tmp; |
lypinator | 0:bb348c97df44 | 2028 | |
lypinator | 0:bb348c97df44 | 2029 | /* Byte 13 */ |
lypinator | 0:bb348c97df44 | 2030 | tmp = (sd_status[3U] & 0xFC00U) >> 10U; |
lypinator | 0:bb348c97df44 | 2031 | pStatus->EraseTimeout = (uint8_t)tmp; |
lypinator | 0:bb348c97df44 | 2032 | |
lypinator | 0:bb348c97df44 | 2033 | /* Byte 13 */ |
lypinator | 0:bb348c97df44 | 2034 | tmp = (sd_status[3U] & 0x0300U) >> 8U; |
lypinator | 0:bb348c97df44 | 2035 | pStatus->EraseOffset = (uint8_t)tmp; |
lypinator | 0:bb348c97df44 | 2036 | } |
lypinator | 0:bb348c97df44 | 2037 | |
lypinator | 0:bb348c97df44 | 2038 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2039 | } |
lypinator | 0:bb348c97df44 | 2040 | |
lypinator | 0:bb348c97df44 | 2041 | /** |
lypinator | 0:bb348c97df44 | 2042 | * @brief Gets the SD card info. |
lypinator | 0:bb348c97df44 | 2043 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 2044 | * @param pCardInfo Pointer to the HAL_SD_CardInfoTypeDef structure that |
lypinator | 0:bb348c97df44 | 2045 | * will contain the SD card status information |
lypinator | 0:bb348c97df44 | 2046 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2047 | */ |
lypinator | 0:bb348c97df44 | 2048 | HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo) |
lypinator | 0:bb348c97df44 | 2049 | { |
lypinator | 0:bb348c97df44 | 2050 | pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType); |
lypinator | 0:bb348c97df44 | 2051 | pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion); |
lypinator | 0:bb348c97df44 | 2052 | pCardInfo->Class = (uint32_t)(hsd->SdCard.Class); |
lypinator | 0:bb348c97df44 | 2053 | pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd); |
lypinator | 0:bb348c97df44 | 2054 | pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr); |
lypinator | 0:bb348c97df44 | 2055 | pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize); |
lypinator | 0:bb348c97df44 | 2056 | pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr); |
lypinator | 0:bb348c97df44 | 2057 | pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize); |
lypinator | 0:bb348c97df44 | 2058 | |
lypinator | 0:bb348c97df44 | 2059 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2060 | } |
lypinator | 0:bb348c97df44 | 2061 | |
lypinator | 0:bb348c97df44 | 2062 | /** |
lypinator | 0:bb348c97df44 | 2063 | * @brief Enables wide bus operation for the requested card if supported by |
lypinator | 0:bb348c97df44 | 2064 | * card. |
lypinator | 0:bb348c97df44 | 2065 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 2066 | * @param WideMode Specifies the SD card wide bus mode |
lypinator | 0:bb348c97df44 | 2067 | * This parameter can be one of the following values: |
lypinator | 0:bb348c97df44 | 2068 | * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer |
lypinator | 0:bb348c97df44 | 2069 | * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer |
lypinator | 0:bb348c97df44 | 2070 | * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer |
lypinator | 0:bb348c97df44 | 2071 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2072 | */ |
lypinator | 0:bb348c97df44 | 2073 | HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode) |
lypinator | 0:bb348c97df44 | 2074 | { |
lypinator | 0:bb348c97df44 | 2075 | SDIO_InitTypeDef Init; |
lypinator | 0:bb348c97df44 | 2076 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2077 | |
lypinator | 0:bb348c97df44 | 2078 | /* Check the parameters */ |
lypinator | 0:bb348c97df44 | 2079 | assert_param(IS_SDIO_BUS_WIDE(WideMode)); |
lypinator | 0:bb348c97df44 | 2080 | |
lypinator | 0:bb348c97df44 | 2081 | /* Chnage Satte */ |
lypinator | 0:bb348c97df44 | 2082 | hsd->State = HAL_SD_STATE_BUSY; |
lypinator | 0:bb348c97df44 | 2083 | |
lypinator | 0:bb348c97df44 | 2084 | if(hsd->SdCard.CardType != CARD_SECURED) |
lypinator | 0:bb348c97df44 | 2085 | { |
lypinator | 0:bb348c97df44 | 2086 | if(WideMode == SDIO_BUS_WIDE_8B) |
lypinator | 0:bb348c97df44 | 2087 | { |
lypinator | 0:bb348c97df44 | 2088 | hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; |
lypinator | 0:bb348c97df44 | 2089 | } |
lypinator | 0:bb348c97df44 | 2090 | else if(WideMode == SDIO_BUS_WIDE_4B) |
lypinator | 0:bb348c97df44 | 2091 | { |
lypinator | 0:bb348c97df44 | 2092 | errorstate = SD_WideBus_Enable(hsd); |
lypinator | 0:bb348c97df44 | 2093 | |
lypinator | 0:bb348c97df44 | 2094 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 2095 | } |
lypinator | 0:bb348c97df44 | 2096 | else if(WideMode == SDIO_BUS_WIDE_1B) |
lypinator | 0:bb348c97df44 | 2097 | { |
lypinator | 0:bb348c97df44 | 2098 | errorstate = SD_WideBus_Disable(hsd); |
lypinator | 0:bb348c97df44 | 2099 | |
lypinator | 0:bb348c97df44 | 2100 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 2101 | } |
lypinator | 0:bb348c97df44 | 2102 | else |
lypinator | 0:bb348c97df44 | 2103 | { |
lypinator | 0:bb348c97df44 | 2104 | /* WideMode is not a valid argument*/ |
lypinator | 0:bb348c97df44 | 2105 | hsd->ErrorCode |= HAL_SD_ERROR_PARAM; |
lypinator | 0:bb348c97df44 | 2106 | } |
lypinator | 0:bb348c97df44 | 2107 | } |
lypinator | 0:bb348c97df44 | 2108 | else |
lypinator | 0:bb348c97df44 | 2109 | { |
lypinator | 0:bb348c97df44 | 2110 | /* MMC Card does not support this feature */ |
lypinator | 0:bb348c97df44 | 2111 | hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; |
lypinator | 0:bb348c97df44 | 2112 | } |
lypinator | 0:bb348c97df44 | 2113 | |
lypinator | 0:bb348c97df44 | 2114 | if(hsd->ErrorCode != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2115 | { |
lypinator | 0:bb348c97df44 | 2116 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 2117 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 2118 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 2119 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 2120 | } |
lypinator | 0:bb348c97df44 | 2121 | else |
lypinator | 0:bb348c97df44 | 2122 | { |
lypinator | 0:bb348c97df44 | 2123 | /* Configure the SDIO peripheral */ |
lypinator | 0:bb348c97df44 | 2124 | Init.ClockEdge = hsd->Init.ClockEdge; |
lypinator | 0:bb348c97df44 | 2125 | Init.ClockBypass = hsd->Init.ClockBypass; |
lypinator | 0:bb348c97df44 | 2126 | Init.ClockPowerSave = hsd->Init.ClockPowerSave; |
lypinator | 0:bb348c97df44 | 2127 | Init.BusWide = WideMode; |
lypinator | 0:bb348c97df44 | 2128 | Init.HardwareFlowControl = hsd->Init.HardwareFlowControl; |
lypinator | 0:bb348c97df44 | 2129 | Init.ClockDiv = hsd->Init.ClockDiv; |
lypinator | 0:bb348c97df44 | 2130 | SDIO_Init(hsd->Instance, Init); |
lypinator | 0:bb348c97df44 | 2131 | } |
lypinator | 0:bb348c97df44 | 2132 | |
lypinator | 0:bb348c97df44 | 2133 | /* Change State */ |
lypinator | 0:bb348c97df44 | 2134 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 2135 | |
lypinator | 0:bb348c97df44 | 2136 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2137 | } |
lypinator | 0:bb348c97df44 | 2138 | |
lypinator | 0:bb348c97df44 | 2139 | |
lypinator | 0:bb348c97df44 | 2140 | /** |
lypinator | 0:bb348c97df44 | 2141 | * @brief Gets the current sd card data state. |
lypinator | 0:bb348c97df44 | 2142 | * @param hsd pointer to SD handle |
lypinator | 0:bb348c97df44 | 2143 | * @retval Card state |
lypinator | 0:bb348c97df44 | 2144 | */ |
lypinator | 0:bb348c97df44 | 2145 | HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 2146 | { |
lypinator | 0:bb348c97df44 | 2147 | HAL_SD_CardStateTypeDef cardstate = HAL_SD_CARD_TRANSFER; |
lypinator | 0:bb348c97df44 | 2148 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2149 | uint32_t resp1 = 0; |
lypinator | 0:bb348c97df44 | 2150 | |
lypinator | 0:bb348c97df44 | 2151 | errorstate = SD_SendStatus(hsd, &resp1); |
lypinator | 0:bb348c97df44 | 2152 | if(errorstate != HAL_OK) |
lypinator | 0:bb348c97df44 | 2153 | { |
lypinator | 0:bb348c97df44 | 2154 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 2155 | } |
lypinator | 0:bb348c97df44 | 2156 | |
lypinator | 0:bb348c97df44 | 2157 | cardstate = (HAL_SD_CardStateTypeDef)((resp1 >> 9U) & 0x0FU); |
lypinator | 0:bb348c97df44 | 2158 | |
lypinator | 0:bb348c97df44 | 2159 | return cardstate; |
lypinator | 0:bb348c97df44 | 2160 | } |
lypinator | 0:bb348c97df44 | 2161 | |
lypinator | 0:bb348c97df44 | 2162 | /** |
lypinator | 0:bb348c97df44 | 2163 | * @brief Abort the current transfer and disable the SD. |
lypinator | 0:bb348c97df44 | 2164 | * @param hsd pointer to a SD_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2165 | * the configuration information for SD module. |
lypinator | 0:bb348c97df44 | 2166 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2167 | */ |
lypinator | 0:bb348c97df44 | 2168 | HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 2169 | { |
lypinator | 0:bb348c97df44 | 2170 | HAL_SD_CardStateTypeDef CardState; |
lypinator | 0:bb348c97df44 | 2171 | |
lypinator | 0:bb348c97df44 | 2172 | /* DIsable All interrupts */ |
lypinator | 0:bb348c97df44 | 2173 | __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ |
lypinator | 0:bb348c97df44 | 2174 | SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR); |
lypinator | 0:bb348c97df44 | 2175 | |
lypinator | 0:bb348c97df44 | 2176 | /* Clear All flags */ |
lypinator | 0:bb348c97df44 | 2177 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 2178 | |
lypinator | 0:bb348c97df44 | 2179 | if((hsd->hdmatx != NULL) || (hsd->hdmarx != NULL)) |
lypinator | 0:bb348c97df44 | 2180 | { |
lypinator | 0:bb348c97df44 | 2181 | /* Disable the SD DMA request */ |
lypinator | 0:bb348c97df44 | 2182 | hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN); |
lypinator | 0:bb348c97df44 | 2183 | |
lypinator | 0:bb348c97df44 | 2184 | /* Abort the SD DMA Tx Stream */ |
lypinator | 0:bb348c97df44 | 2185 | if(hsd->hdmatx != NULL) |
lypinator | 0:bb348c97df44 | 2186 | { |
lypinator | 0:bb348c97df44 | 2187 | HAL_DMA_Abort(hsd->hdmatx); |
lypinator | 0:bb348c97df44 | 2188 | } |
lypinator | 0:bb348c97df44 | 2189 | /* Abort the SD DMA Rx Stream */ |
lypinator | 0:bb348c97df44 | 2190 | if(hsd->hdmarx != NULL) |
lypinator | 0:bb348c97df44 | 2191 | { |
lypinator | 0:bb348c97df44 | 2192 | HAL_DMA_Abort(hsd->hdmarx); |
lypinator | 0:bb348c97df44 | 2193 | } |
lypinator | 0:bb348c97df44 | 2194 | } |
lypinator | 0:bb348c97df44 | 2195 | |
lypinator | 0:bb348c97df44 | 2196 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 2197 | CardState = HAL_SD_GetCardState(hsd); |
lypinator | 0:bb348c97df44 | 2198 | if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) |
lypinator | 0:bb348c97df44 | 2199 | { |
lypinator | 0:bb348c97df44 | 2200 | hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance); |
lypinator | 0:bb348c97df44 | 2201 | } |
lypinator | 0:bb348c97df44 | 2202 | if(hsd->ErrorCode != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2203 | { |
lypinator | 0:bb348c97df44 | 2204 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 2205 | } |
lypinator | 0:bb348c97df44 | 2206 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2207 | } |
lypinator | 0:bb348c97df44 | 2208 | |
lypinator | 0:bb348c97df44 | 2209 | /** |
lypinator | 0:bb348c97df44 | 2210 | * @brief Abort the current transfer and disable the SD (IT mode). |
lypinator | 0:bb348c97df44 | 2211 | * @param hsd pointer to a SD_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2212 | * the configuration information for SD module. |
lypinator | 0:bb348c97df44 | 2213 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2214 | */ |
lypinator | 0:bb348c97df44 | 2215 | HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 2216 | { |
lypinator | 0:bb348c97df44 | 2217 | HAL_SD_CardStateTypeDef CardState; |
lypinator | 0:bb348c97df44 | 2218 | |
lypinator | 0:bb348c97df44 | 2219 | /* DIsable All interrupts */ |
lypinator | 0:bb348c97df44 | 2220 | __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ |
lypinator | 0:bb348c97df44 | 2221 | SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR); |
lypinator | 0:bb348c97df44 | 2222 | |
lypinator | 0:bb348c97df44 | 2223 | /* Clear All flags */ |
lypinator | 0:bb348c97df44 | 2224 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 2225 | |
lypinator | 0:bb348c97df44 | 2226 | if((hsd->hdmatx != NULL) || (hsd->hdmarx != NULL)) |
lypinator | 0:bb348c97df44 | 2227 | { |
lypinator | 0:bb348c97df44 | 2228 | /* Disable the SD DMA request */ |
lypinator | 0:bb348c97df44 | 2229 | hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN); |
lypinator | 0:bb348c97df44 | 2230 | |
lypinator | 0:bb348c97df44 | 2231 | /* Abort the SD DMA Tx Stream */ |
lypinator | 0:bb348c97df44 | 2232 | if(hsd->hdmatx != NULL) |
lypinator | 0:bb348c97df44 | 2233 | { |
lypinator | 0:bb348c97df44 | 2234 | hsd->hdmatx->XferAbortCallback = SD_DMATxAbort; |
lypinator | 0:bb348c97df44 | 2235 | if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK) |
lypinator | 0:bb348c97df44 | 2236 | { |
lypinator | 0:bb348c97df44 | 2237 | hsd->hdmatx = NULL; |
lypinator | 0:bb348c97df44 | 2238 | } |
lypinator | 0:bb348c97df44 | 2239 | } |
lypinator | 0:bb348c97df44 | 2240 | /* Abort the SD DMA Rx Stream */ |
lypinator | 0:bb348c97df44 | 2241 | if(hsd->hdmarx != NULL) |
lypinator | 0:bb348c97df44 | 2242 | { |
lypinator | 0:bb348c97df44 | 2243 | hsd->hdmarx->XferAbortCallback = SD_DMARxAbort; |
lypinator | 0:bb348c97df44 | 2244 | if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK) |
lypinator | 0:bb348c97df44 | 2245 | { |
lypinator | 0:bb348c97df44 | 2246 | hsd->hdmarx = NULL; |
lypinator | 0:bb348c97df44 | 2247 | } |
lypinator | 0:bb348c97df44 | 2248 | } |
lypinator | 0:bb348c97df44 | 2249 | } |
lypinator | 0:bb348c97df44 | 2250 | |
lypinator | 0:bb348c97df44 | 2251 | /* No transfer ongoing on both DMA channels*/ |
lypinator | 0:bb348c97df44 | 2252 | if((hsd->hdmatx == NULL) && (hsd->hdmarx == NULL)) |
lypinator | 0:bb348c97df44 | 2253 | { |
lypinator | 0:bb348c97df44 | 2254 | CardState = HAL_SD_GetCardState(hsd); |
lypinator | 0:bb348c97df44 | 2255 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 2256 | if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) |
lypinator | 0:bb348c97df44 | 2257 | { |
lypinator | 0:bb348c97df44 | 2258 | hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance); |
lypinator | 0:bb348c97df44 | 2259 | } |
lypinator | 0:bb348c97df44 | 2260 | if(hsd->ErrorCode != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2261 | { |
lypinator | 0:bb348c97df44 | 2262 | return HAL_ERROR; |
lypinator | 0:bb348c97df44 | 2263 | } |
lypinator | 0:bb348c97df44 | 2264 | else |
lypinator | 0:bb348c97df44 | 2265 | { |
lypinator | 0:bb348c97df44 | 2266 | HAL_SD_AbortCallback(hsd); |
lypinator | 0:bb348c97df44 | 2267 | } |
lypinator | 0:bb348c97df44 | 2268 | } |
lypinator | 0:bb348c97df44 | 2269 | |
lypinator | 0:bb348c97df44 | 2270 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2271 | } |
lypinator | 0:bb348c97df44 | 2272 | |
lypinator | 0:bb348c97df44 | 2273 | /** |
lypinator | 0:bb348c97df44 | 2274 | * @} |
lypinator | 0:bb348c97df44 | 2275 | */ |
lypinator | 0:bb348c97df44 | 2276 | |
lypinator | 0:bb348c97df44 | 2277 | /** |
lypinator | 0:bb348c97df44 | 2278 | * @} |
lypinator | 0:bb348c97df44 | 2279 | */ |
lypinator | 0:bb348c97df44 | 2280 | |
lypinator | 0:bb348c97df44 | 2281 | /* Private function ----------------------------------------------------------*/ |
lypinator | 0:bb348c97df44 | 2282 | /** @addtogroup SD_Private_Functions |
lypinator | 0:bb348c97df44 | 2283 | * @{ |
lypinator | 0:bb348c97df44 | 2284 | */ |
lypinator | 0:bb348c97df44 | 2285 | |
lypinator | 0:bb348c97df44 | 2286 | /** |
lypinator | 0:bb348c97df44 | 2287 | * @brief DMA SD transmit process complete callback |
lypinator | 0:bb348c97df44 | 2288 | * @param hdma DMA handle |
lypinator | 0:bb348c97df44 | 2289 | * @retval None |
lypinator | 0:bb348c97df44 | 2290 | */ |
lypinator | 0:bb348c97df44 | 2291 | static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 2292 | { |
lypinator | 0:bb348c97df44 | 2293 | SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent); |
lypinator | 0:bb348c97df44 | 2294 | |
lypinator | 0:bb348c97df44 | 2295 | /* Enable DATAEND Interrupt */ |
lypinator | 0:bb348c97df44 | 2296 | __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DATAEND)); |
lypinator | 0:bb348c97df44 | 2297 | } |
lypinator | 0:bb348c97df44 | 2298 | |
lypinator | 0:bb348c97df44 | 2299 | /** |
lypinator | 0:bb348c97df44 | 2300 | * @brief DMA SD receive process complete callback |
lypinator | 0:bb348c97df44 | 2301 | * @param hdma DMA handle |
lypinator | 0:bb348c97df44 | 2302 | * @retval None |
lypinator | 0:bb348c97df44 | 2303 | */ |
lypinator | 0:bb348c97df44 | 2304 | static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 2305 | { |
lypinator | 0:bb348c97df44 | 2306 | SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent); |
lypinator | 0:bb348c97df44 | 2307 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2308 | |
lypinator | 0:bb348c97df44 | 2309 | /* Send stop command in multiblock write */ |
lypinator | 0:bb348c97df44 | 2310 | if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA)) |
lypinator | 0:bb348c97df44 | 2311 | { |
lypinator | 0:bb348c97df44 | 2312 | errorstate = SDMMC_CmdStopTransfer(hsd->Instance); |
lypinator | 0:bb348c97df44 | 2313 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2314 | { |
lypinator | 0:bb348c97df44 | 2315 | hsd->ErrorCode |= errorstate; |
lypinator | 0:bb348c97df44 | 2316 | HAL_SD_ErrorCallback(hsd); |
lypinator | 0:bb348c97df44 | 2317 | } |
lypinator | 0:bb348c97df44 | 2318 | } |
lypinator | 0:bb348c97df44 | 2319 | |
lypinator | 0:bb348c97df44 | 2320 | /* Disable the DMA transfer for transmit request by setting the DMAEN bit |
lypinator | 0:bb348c97df44 | 2321 | in the SD DCTRL register */ |
lypinator | 0:bb348c97df44 | 2322 | hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN); |
lypinator | 0:bb348c97df44 | 2323 | |
lypinator | 0:bb348c97df44 | 2324 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 2325 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 2326 | |
lypinator | 0:bb348c97df44 | 2327 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 2328 | |
lypinator | 0:bb348c97df44 | 2329 | HAL_SD_RxCpltCallback(hsd); |
lypinator | 0:bb348c97df44 | 2330 | } |
lypinator | 0:bb348c97df44 | 2331 | |
lypinator | 0:bb348c97df44 | 2332 | /** |
lypinator | 0:bb348c97df44 | 2333 | * @brief DMA SD communication error callback |
lypinator | 0:bb348c97df44 | 2334 | * @param hdma DMA handle |
lypinator | 0:bb348c97df44 | 2335 | * @retval None |
lypinator | 0:bb348c97df44 | 2336 | */ |
lypinator | 0:bb348c97df44 | 2337 | static void SD_DMAError(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 2338 | { |
lypinator | 0:bb348c97df44 | 2339 | SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent); |
lypinator | 0:bb348c97df44 | 2340 | HAL_SD_CardStateTypeDef CardState; |
lypinator | 0:bb348c97df44 | 2341 | |
lypinator | 0:bb348c97df44 | 2342 | if((hsd->hdmarx->ErrorCode == HAL_DMA_ERROR_TE) || (hsd->hdmatx->ErrorCode == HAL_DMA_ERROR_TE)) |
lypinator | 0:bb348c97df44 | 2343 | { |
lypinator | 0:bb348c97df44 | 2344 | /* Clear All flags */ |
lypinator | 0:bb348c97df44 | 2345 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 2346 | |
lypinator | 0:bb348c97df44 | 2347 | /* Disable All interrupts */ |
lypinator | 0:bb348c97df44 | 2348 | __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ |
lypinator | 0:bb348c97df44 | 2349 | SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR); |
lypinator | 0:bb348c97df44 | 2350 | |
lypinator | 0:bb348c97df44 | 2351 | hsd->ErrorCode |= HAL_SD_ERROR_DMA; |
lypinator | 0:bb348c97df44 | 2352 | CardState = HAL_SD_GetCardState(hsd); |
lypinator | 0:bb348c97df44 | 2353 | if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) |
lypinator | 0:bb348c97df44 | 2354 | { |
lypinator | 0:bb348c97df44 | 2355 | hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance); |
lypinator | 0:bb348c97df44 | 2356 | } |
lypinator | 0:bb348c97df44 | 2357 | |
lypinator | 0:bb348c97df44 | 2358 | hsd->State= HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 2359 | } |
lypinator | 0:bb348c97df44 | 2360 | |
lypinator | 0:bb348c97df44 | 2361 | HAL_SD_ErrorCallback(hsd); |
lypinator | 0:bb348c97df44 | 2362 | } |
lypinator | 0:bb348c97df44 | 2363 | |
lypinator | 0:bb348c97df44 | 2364 | /** |
lypinator | 0:bb348c97df44 | 2365 | * @brief DMA SD Tx Abort callback |
lypinator | 0:bb348c97df44 | 2366 | * @param hdma DMA handle |
lypinator | 0:bb348c97df44 | 2367 | * @retval None |
lypinator | 0:bb348c97df44 | 2368 | */ |
lypinator | 0:bb348c97df44 | 2369 | static void SD_DMATxAbort(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 2370 | { |
lypinator | 0:bb348c97df44 | 2371 | SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent); |
lypinator | 0:bb348c97df44 | 2372 | HAL_SD_CardStateTypeDef CardState; |
lypinator | 0:bb348c97df44 | 2373 | |
lypinator | 0:bb348c97df44 | 2374 | if(hsd->hdmatx != NULL) |
lypinator | 0:bb348c97df44 | 2375 | { |
lypinator | 0:bb348c97df44 | 2376 | hsd->hdmatx = NULL; |
lypinator | 0:bb348c97df44 | 2377 | } |
lypinator | 0:bb348c97df44 | 2378 | |
lypinator | 0:bb348c97df44 | 2379 | /* All DMA channels are aborted */ |
lypinator | 0:bb348c97df44 | 2380 | if(hsd->hdmarx == NULL) |
lypinator | 0:bb348c97df44 | 2381 | { |
lypinator | 0:bb348c97df44 | 2382 | CardState = HAL_SD_GetCardState(hsd); |
lypinator | 0:bb348c97df44 | 2383 | hsd->ErrorCode = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2384 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 2385 | if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) |
lypinator | 0:bb348c97df44 | 2386 | { |
lypinator | 0:bb348c97df44 | 2387 | hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance); |
lypinator | 0:bb348c97df44 | 2388 | |
lypinator | 0:bb348c97df44 | 2389 | if(hsd->ErrorCode != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2390 | { |
lypinator | 0:bb348c97df44 | 2391 | HAL_SD_AbortCallback(hsd); |
lypinator | 0:bb348c97df44 | 2392 | } |
lypinator | 0:bb348c97df44 | 2393 | else |
lypinator | 0:bb348c97df44 | 2394 | { |
lypinator | 0:bb348c97df44 | 2395 | HAL_SD_ErrorCallback(hsd); |
lypinator | 0:bb348c97df44 | 2396 | } |
lypinator | 0:bb348c97df44 | 2397 | } |
lypinator | 0:bb348c97df44 | 2398 | } |
lypinator | 0:bb348c97df44 | 2399 | } |
lypinator | 0:bb348c97df44 | 2400 | |
lypinator | 0:bb348c97df44 | 2401 | /** |
lypinator | 0:bb348c97df44 | 2402 | * @brief DMA SD Rx Abort callback |
lypinator | 0:bb348c97df44 | 2403 | * @param hdma DMA handle |
lypinator | 0:bb348c97df44 | 2404 | * @retval None |
lypinator | 0:bb348c97df44 | 2405 | */ |
lypinator | 0:bb348c97df44 | 2406 | static void SD_DMARxAbort(DMA_HandleTypeDef *hdma) |
lypinator | 0:bb348c97df44 | 2407 | { |
lypinator | 0:bb348c97df44 | 2408 | SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent); |
lypinator | 0:bb348c97df44 | 2409 | HAL_SD_CardStateTypeDef CardState; |
lypinator | 0:bb348c97df44 | 2410 | |
lypinator | 0:bb348c97df44 | 2411 | if(hsd->hdmarx != NULL) |
lypinator | 0:bb348c97df44 | 2412 | { |
lypinator | 0:bb348c97df44 | 2413 | hsd->hdmarx = NULL; |
lypinator | 0:bb348c97df44 | 2414 | } |
lypinator | 0:bb348c97df44 | 2415 | |
lypinator | 0:bb348c97df44 | 2416 | /* All DMA channels are aborted */ |
lypinator | 0:bb348c97df44 | 2417 | if(hsd->hdmatx == NULL) |
lypinator | 0:bb348c97df44 | 2418 | { |
lypinator | 0:bb348c97df44 | 2419 | CardState = HAL_SD_GetCardState(hsd); |
lypinator | 0:bb348c97df44 | 2420 | hsd->ErrorCode = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2421 | hsd->State = HAL_SD_STATE_READY; |
lypinator | 0:bb348c97df44 | 2422 | if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) |
lypinator | 0:bb348c97df44 | 2423 | { |
lypinator | 0:bb348c97df44 | 2424 | hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance); |
lypinator | 0:bb348c97df44 | 2425 | |
lypinator | 0:bb348c97df44 | 2426 | if(hsd->ErrorCode != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2427 | { |
lypinator | 0:bb348c97df44 | 2428 | HAL_SD_AbortCallback(hsd); |
lypinator | 0:bb348c97df44 | 2429 | } |
lypinator | 0:bb348c97df44 | 2430 | else |
lypinator | 0:bb348c97df44 | 2431 | { |
lypinator | 0:bb348c97df44 | 2432 | HAL_SD_ErrorCallback(hsd); |
lypinator | 0:bb348c97df44 | 2433 | } |
lypinator | 0:bb348c97df44 | 2434 | } |
lypinator | 0:bb348c97df44 | 2435 | } |
lypinator | 0:bb348c97df44 | 2436 | } |
lypinator | 0:bb348c97df44 | 2437 | |
lypinator | 0:bb348c97df44 | 2438 | |
lypinator | 0:bb348c97df44 | 2439 | /** |
lypinator | 0:bb348c97df44 | 2440 | * @brief Initializes the sd card. |
lypinator | 0:bb348c97df44 | 2441 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 2442 | * @retval SD Card error state |
lypinator | 0:bb348c97df44 | 2443 | */ |
lypinator | 0:bb348c97df44 | 2444 | static uint32_t SD_InitCard(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 2445 | { |
lypinator | 0:bb348c97df44 | 2446 | HAL_SD_CardCSDTypeDef CSD; |
lypinator | 0:bb348c97df44 | 2447 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2448 | uint16_t sd_rca = 1U; |
lypinator | 0:bb348c97df44 | 2449 | |
lypinator | 0:bb348c97df44 | 2450 | /* Check the power State */ |
lypinator | 0:bb348c97df44 | 2451 | if(SDIO_GetPowerState(hsd->Instance) == 0U) |
lypinator | 0:bb348c97df44 | 2452 | { |
lypinator | 0:bb348c97df44 | 2453 | /* Power off */ |
lypinator | 0:bb348c97df44 | 2454 | return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; |
lypinator | 0:bb348c97df44 | 2455 | } |
lypinator | 0:bb348c97df44 | 2456 | |
lypinator | 0:bb348c97df44 | 2457 | if(hsd->SdCard.CardType != CARD_SECURED) |
lypinator | 0:bb348c97df44 | 2458 | { |
lypinator | 0:bb348c97df44 | 2459 | /* Send CMD2 ALL_SEND_CID */ |
lypinator | 0:bb348c97df44 | 2460 | errorstate = SDMMC_CmdSendCID(hsd->Instance); |
lypinator | 0:bb348c97df44 | 2461 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2462 | { |
lypinator | 0:bb348c97df44 | 2463 | return errorstate; |
lypinator | 0:bb348c97df44 | 2464 | } |
lypinator | 0:bb348c97df44 | 2465 | else |
lypinator | 0:bb348c97df44 | 2466 | { |
lypinator | 0:bb348c97df44 | 2467 | /* Get Card identification number data */ |
lypinator | 0:bb348c97df44 | 2468 | hsd->CID[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); |
lypinator | 0:bb348c97df44 | 2469 | hsd->CID[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2); |
lypinator | 0:bb348c97df44 | 2470 | hsd->CID[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3); |
lypinator | 0:bb348c97df44 | 2471 | hsd->CID[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4); |
lypinator | 0:bb348c97df44 | 2472 | } |
lypinator | 0:bb348c97df44 | 2473 | } |
lypinator | 0:bb348c97df44 | 2474 | |
lypinator | 0:bb348c97df44 | 2475 | if(hsd->SdCard.CardType != CARD_SECURED) |
lypinator | 0:bb348c97df44 | 2476 | { |
lypinator | 0:bb348c97df44 | 2477 | /* Send CMD3 SET_REL_ADDR with argument 0 */ |
lypinator | 0:bb348c97df44 | 2478 | /* SD Card publishes its RCA. */ |
lypinator | 0:bb348c97df44 | 2479 | errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca); |
lypinator | 0:bb348c97df44 | 2480 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2481 | { |
lypinator | 0:bb348c97df44 | 2482 | return errorstate; |
lypinator | 0:bb348c97df44 | 2483 | } |
lypinator | 0:bb348c97df44 | 2484 | } |
lypinator | 0:bb348c97df44 | 2485 | if(hsd->SdCard.CardType != CARD_SECURED) |
lypinator | 0:bb348c97df44 | 2486 | { |
lypinator | 0:bb348c97df44 | 2487 | /* Get the SD card RCA */ |
lypinator | 0:bb348c97df44 | 2488 | hsd->SdCard.RelCardAdd = sd_rca; |
lypinator | 0:bb348c97df44 | 2489 | |
lypinator | 0:bb348c97df44 | 2490 | /* Send CMD9 SEND_CSD with argument as card's RCA */ |
lypinator | 0:bb348c97df44 | 2491 | errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); |
lypinator | 0:bb348c97df44 | 2492 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2493 | { |
lypinator | 0:bb348c97df44 | 2494 | return errorstate; |
lypinator | 0:bb348c97df44 | 2495 | } |
lypinator | 0:bb348c97df44 | 2496 | else |
lypinator | 0:bb348c97df44 | 2497 | { |
lypinator | 0:bb348c97df44 | 2498 | /* Get Card Specific Data */ |
lypinator | 0:bb348c97df44 | 2499 | hsd->CSD[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); |
lypinator | 0:bb348c97df44 | 2500 | hsd->CSD[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2); |
lypinator | 0:bb348c97df44 | 2501 | hsd->CSD[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3); |
lypinator | 0:bb348c97df44 | 2502 | hsd->CSD[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4); |
lypinator | 0:bb348c97df44 | 2503 | } |
lypinator | 0:bb348c97df44 | 2504 | } |
lypinator | 0:bb348c97df44 | 2505 | |
lypinator | 0:bb348c97df44 | 2506 | /* Get the Card Class */ |
lypinator | 0:bb348c97df44 | 2507 | hsd->SdCard.Class = (SDIO_GetResponse(hsd->Instance, SDIO_RESP2) >> 20U); |
lypinator | 0:bb348c97df44 | 2508 | |
lypinator | 0:bb348c97df44 | 2509 | /* Get CSD parameters */ |
lypinator | 0:bb348c97df44 | 2510 | HAL_SD_GetCardCSD(hsd, &CSD); |
lypinator | 0:bb348c97df44 | 2511 | |
lypinator | 0:bb348c97df44 | 2512 | /* Select the Card */ |
lypinator | 0:bb348c97df44 | 2513 | errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U)); |
lypinator | 0:bb348c97df44 | 2514 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2515 | { |
lypinator | 0:bb348c97df44 | 2516 | return errorstate; |
lypinator | 0:bb348c97df44 | 2517 | } |
lypinator | 0:bb348c97df44 | 2518 | |
lypinator | 0:bb348c97df44 | 2519 | /* Configure SDIO peripheral interface */ |
lypinator | 0:bb348c97df44 | 2520 | SDIO_Init(hsd->Instance, hsd->Init); |
lypinator | 0:bb348c97df44 | 2521 | |
lypinator | 0:bb348c97df44 | 2522 | /* All cards are initialized */ |
lypinator | 0:bb348c97df44 | 2523 | return HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2524 | } |
lypinator | 0:bb348c97df44 | 2525 | |
lypinator | 0:bb348c97df44 | 2526 | /** |
lypinator | 0:bb348c97df44 | 2527 | * @brief Enquires cards about their operating voltage and configures clock |
lypinator | 0:bb348c97df44 | 2528 | * controls and stores SD information that will be needed in future |
lypinator | 0:bb348c97df44 | 2529 | * in the SD handle. |
lypinator | 0:bb348c97df44 | 2530 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 2531 | * @retval error state |
lypinator | 0:bb348c97df44 | 2532 | */ |
lypinator | 0:bb348c97df44 | 2533 | static uint32_t SD_PowerON(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 2534 | { |
lypinator | 0:bb348c97df44 | 2535 | __IO uint32_t count = 0U; |
lypinator | 0:bb348c97df44 | 2536 | uint32_t response = 0U, validvoltage = 0U; |
lypinator | 0:bb348c97df44 | 2537 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2538 | |
lypinator | 0:bb348c97df44 | 2539 | /* CMD0: GO_IDLE_STATE */ |
lypinator | 0:bb348c97df44 | 2540 | errorstate = SDMMC_CmdGoIdleState(hsd->Instance); |
lypinator | 0:bb348c97df44 | 2541 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2542 | { |
lypinator | 0:bb348c97df44 | 2543 | return errorstate; |
lypinator | 0:bb348c97df44 | 2544 | } |
lypinator | 0:bb348c97df44 | 2545 | |
lypinator | 0:bb348c97df44 | 2546 | /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */ |
lypinator | 0:bb348c97df44 | 2547 | errorstate = SDMMC_CmdOperCond(hsd->Instance); |
lypinator | 0:bb348c97df44 | 2548 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2549 | { |
lypinator | 0:bb348c97df44 | 2550 | hsd->SdCard.CardVersion = CARD_V1_X; |
lypinator | 0:bb348c97df44 | 2551 | |
lypinator | 0:bb348c97df44 | 2552 | /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */ |
lypinator | 0:bb348c97df44 | 2553 | while(validvoltage == 0U) |
lypinator | 0:bb348c97df44 | 2554 | { |
lypinator | 0:bb348c97df44 | 2555 | if(count++ == SDMMC_MAX_VOLT_TRIAL) |
lypinator | 0:bb348c97df44 | 2556 | { |
lypinator | 0:bb348c97df44 | 2557 | return HAL_SD_ERROR_INVALID_VOLTRANGE; |
lypinator | 0:bb348c97df44 | 2558 | } |
lypinator | 0:bb348c97df44 | 2559 | |
lypinator | 0:bb348c97df44 | 2560 | /* SEND CMD55 APP_CMD with RCA as 0 */ |
lypinator | 0:bb348c97df44 | 2561 | errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0U); |
lypinator | 0:bb348c97df44 | 2562 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2563 | { |
lypinator | 0:bb348c97df44 | 2564 | return HAL_SD_ERROR_UNSUPPORTED_FEATURE; |
lypinator | 0:bb348c97df44 | 2565 | } |
lypinator | 0:bb348c97df44 | 2566 | |
lypinator | 0:bb348c97df44 | 2567 | /* Send CMD41 */ |
lypinator | 0:bb348c97df44 | 2568 | errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_STD_CAPACITY); |
lypinator | 0:bb348c97df44 | 2569 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2570 | { |
lypinator | 0:bb348c97df44 | 2571 | return HAL_SD_ERROR_UNSUPPORTED_FEATURE; |
lypinator | 0:bb348c97df44 | 2572 | } |
lypinator | 0:bb348c97df44 | 2573 | |
lypinator | 0:bb348c97df44 | 2574 | /* Get command response */ |
lypinator | 0:bb348c97df44 | 2575 | response = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); |
lypinator | 0:bb348c97df44 | 2576 | |
lypinator | 0:bb348c97df44 | 2577 | /* Get operating voltage*/ |
lypinator | 0:bb348c97df44 | 2578 | validvoltage = (((response >> 31U) == 1U) ? 1U : 0U); |
lypinator | 0:bb348c97df44 | 2579 | } |
lypinator | 0:bb348c97df44 | 2580 | /* Card type is SDSC */ |
lypinator | 0:bb348c97df44 | 2581 | hsd->SdCard.CardType = CARD_SDSC; |
lypinator | 0:bb348c97df44 | 2582 | } |
lypinator | 0:bb348c97df44 | 2583 | else |
lypinator | 0:bb348c97df44 | 2584 | { |
lypinator | 0:bb348c97df44 | 2585 | hsd->SdCard.CardVersion = CARD_V2_X; |
lypinator | 0:bb348c97df44 | 2586 | |
lypinator | 0:bb348c97df44 | 2587 | /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */ |
lypinator | 0:bb348c97df44 | 2588 | while(validvoltage == 0U) |
lypinator | 0:bb348c97df44 | 2589 | { |
lypinator | 0:bb348c97df44 | 2590 | if(count++ == SDMMC_MAX_VOLT_TRIAL) |
lypinator | 0:bb348c97df44 | 2591 | { |
lypinator | 0:bb348c97df44 | 2592 | return HAL_SD_ERROR_INVALID_VOLTRANGE; |
lypinator | 0:bb348c97df44 | 2593 | } |
lypinator | 0:bb348c97df44 | 2594 | |
lypinator | 0:bb348c97df44 | 2595 | /* SEND CMD55 APP_CMD with RCA as 0 */ |
lypinator | 0:bb348c97df44 | 2596 | errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0U); |
lypinator | 0:bb348c97df44 | 2597 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2598 | { |
lypinator | 0:bb348c97df44 | 2599 | return errorstate; |
lypinator | 0:bb348c97df44 | 2600 | } |
lypinator | 0:bb348c97df44 | 2601 | |
lypinator | 0:bb348c97df44 | 2602 | /* Send CMD41 */ |
lypinator | 0:bb348c97df44 | 2603 | errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_HIGH_CAPACITY); |
lypinator | 0:bb348c97df44 | 2604 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2605 | { |
lypinator | 0:bb348c97df44 | 2606 | return errorstate; |
lypinator | 0:bb348c97df44 | 2607 | } |
lypinator | 0:bb348c97df44 | 2608 | |
lypinator | 0:bb348c97df44 | 2609 | /* Get command response */ |
lypinator | 0:bb348c97df44 | 2610 | response = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); |
lypinator | 0:bb348c97df44 | 2611 | |
lypinator | 0:bb348c97df44 | 2612 | /* Get operating voltage*/ |
lypinator | 0:bb348c97df44 | 2613 | validvoltage = (((response >> 31U) == 1U) ? 1U : 0U); |
lypinator | 0:bb348c97df44 | 2614 | } |
lypinator | 0:bb348c97df44 | 2615 | |
lypinator | 0:bb348c97df44 | 2616 | if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */ |
lypinator | 0:bb348c97df44 | 2617 | { |
lypinator | 0:bb348c97df44 | 2618 | hsd->SdCard.CardType = CARD_SDHC_SDXC; |
lypinator | 0:bb348c97df44 | 2619 | } |
lypinator | 0:bb348c97df44 | 2620 | else |
lypinator | 0:bb348c97df44 | 2621 | { |
lypinator | 0:bb348c97df44 | 2622 | hsd->SdCard.CardType = CARD_SDSC; |
lypinator | 0:bb348c97df44 | 2623 | } |
lypinator | 0:bb348c97df44 | 2624 | } |
lypinator | 0:bb348c97df44 | 2625 | |
lypinator | 0:bb348c97df44 | 2626 | return HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2627 | } |
lypinator | 0:bb348c97df44 | 2628 | |
lypinator | 0:bb348c97df44 | 2629 | /** |
lypinator | 0:bb348c97df44 | 2630 | * @brief Turns the SDIO output signals off. |
lypinator | 0:bb348c97df44 | 2631 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 2632 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2633 | */ |
lypinator | 0:bb348c97df44 | 2634 | static HAL_StatusTypeDef SD_PowerOFF(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 2635 | { |
lypinator | 0:bb348c97df44 | 2636 | /* Set Power State to OFF */ |
lypinator | 0:bb348c97df44 | 2637 | SDIO_PowerState_OFF(hsd->Instance); |
lypinator | 0:bb348c97df44 | 2638 | |
lypinator | 0:bb348c97df44 | 2639 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2640 | } |
lypinator | 0:bb348c97df44 | 2641 | |
lypinator | 0:bb348c97df44 | 2642 | /** |
lypinator | 0:bb348c97df44 | 2643 | * @brief Send Status info command. |
lypinator | 0:bb348c97df44 | 2644 | * @param hsd pointer to SD handle |
lypinator | 0:bb348c97df44 | 2645 | * @param pSDstatus Pointer to the buffer that will contain the SD card status |
lypinator | 0:bb348c97df44 | 2646 | * SD Status register) |
lypinator | 0:bb348c97df44 | 2647 | * @retval error state |
lypinator | 0:bb348c97df44 | 2648 | */ |
lypinator | 0:bb348c97df44 | 2649 | static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus) |
lypinator | 0:bb348c97df44 | 2650 | { |
lypinator | 0:bb348c97df44 | 2651 | SDIO_DataInitTypeDef config; |
lypinator | 0:bb348c97df44 | 2652 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2653 | uint32_t tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 2654 | uint32_t count = 0U; |
lypinator | 0:bb348c97df44 | 2655 | |
lypinator | 0:bb348c97df44 | 2656 | /* Check SD response */ |
lypinator | 0:bb348c97df44 | 2657 | if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED) |
lypinator | 0:bb348c97df44 | 2658 | { |
lypinator | 0:bb348c97df44 | 2659 | return HAL_SD_ERROR_LOCK_UNLOCK_FAILED; |
lypinator | 0:bb348c97df44 | 2660 | } |
lypinator | 0:bb348c97df44 | 2661 | |
lypinator | 0:bb348c97df44 | 2662 | /* Set block size for card if it is not equal to current block size for card */ |
lypinator | 0:bb348c97df44 | 2663 | errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U); |
lypinator | 0:bb348c97df44 | 2664 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2665 | { |
lypinator | 0:bb348c97df44 | 2666 | hsd->ErrorCode |= HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2667 | return errorstate; |
lypinator | 0:bb348c97df44 | 2668 | } |
lypinator | 0:bb348c97df44 | 2669 | |
lypinator | 0:bb348c97df44 | 2670 | /* Send CMD55 */ |
lypinator | 0:bb348c97df44 | 2671 | errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); |
lypinator | 0:bb348c97df44 | 2672 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2673 | { |
lypinator | 0:bb348c97df44 | 2674 | hsd->ErrorCode |= HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2675 | return errorstate; |
lypinator | 0:bb348c97df44 | 2676 | } |
lypinator | 0:bb348c97df44 | 2677 | |
lypinator | 0:bb348c97df44 | 2678 | /* Configure the SD DPSM (Data Path State Machine) */ |
lypinator | 0:bb348c97df44 | 2679 | config.DataTimeOut = SDMMC_DATATIMEOUT; |
lypinator | 0:bb348c97df44 | 2680 | config.DataLength = 64U; |
lypinator | 0:bb348c97df44 | 2681 | config.DataBlockSize = SDIO_DATABLOCK_SIZE_64B; |
lypinator | 0:bb348c97df44 | 2682 | config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; |
lypinator | 0:bb348c97df44 | 2683 | config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; |
lypinator | 0:bb348c97df44 | 2684 | config.DPSM = SDIO_DPSM_ENABLE; |
lypinator | 0:bb348c97df44 | 2685 | SDIO_ConfigData(hsd->Instance, &config); |
lypinator | 0:bb348c97df44 | 2686 | |
lypinator | 0:bb348c97df44 | 2687 | /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */ |
lypinator | 0:bb348c97df44 | 2688 | errorstate = SDMMC_CmdStatusRegister(hsd->Instance); |
lypinator | 0:bb348c97df44 | 2689 | if(errorstate != HAL_SD_ERROR_NONE) |
lypinator | 0:bb348c97df44 | 2690 | { |
lypinator | 0:bb348c97df44 | 2691 | hsd->ErrorCode |= HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2692 | return errorstate; |
lypinator | 0:bb348c97df44 | 2693 | } |
lypinator | 0:bb348c97df44 | 2694 | |
lypinator | 0:bb348c97df44 | 2695 | /* Get status data */ |
lypinator | 0:bb348c97df44 | 2696 | while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND)) |
lypinator | 0:bb348c97df44 | 2697 | { |
lypinator | 0:bb348c97df44 | 2698 | if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF)) |
lypinator | 0:bb348c97df44 | 2699 | { |
lypinator | 0:bb348c97df44 | 2700 | for(count = 0U; count < 8U; count++) |
lypinator | 0:bb348c97df44 | 2701 | { |
lypinator | 0:bb348c97df44 | 2702 | *(pSDstatus + count) = SDIO_ReadFIFO(hsd->Instance); |
lypinator | 0:bb348c97df44 | 2703 | } |
lypinator | 0:bb348c97df44 | 2704 | |
lypinator | 0:bb348c97df44 | 2705 | pSDstatus += 8U; |
lypinator | 0:bb348c97df44 | 2706 | } |
lypinator | 0:bb348c97df44 | 2707 | |
lypinator | 0:bb348c97df44 | 2708 | if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT) |
lypinator | 0:bb348c97df44 | 2709 | { |
lypinator | 0:bb348c97df44 | 2710 | return HAL_SD_ERROR_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2711 | } |
lypinator | 0:bb348c97df44 | 2712 | } |
lypinator | 0:bb348c97df44 | 2713 | |
lypinator | 0:bb348c97df44 | 2714 | if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) |
lypinator | 0:bb348c97df44 | 2715 | { |
lypinator | 0:bb348c97df44 | 2716 | return HAL_SD_ERROR_DATA_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2717 | } |
lypinator | 0:bb348c97df44 | 2718 | else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) |
lypinator | 0:bb348c97df44 | 2719 | { |
lypinator | 0:bb348c97df44 | 2720 | return HAL_SD_ERROR_DATA_CRC_FAIL; |
lypinator | 0:bb348c97df44 | 2721 | } |
lypinator | 0:bb348c97df44 | 2722 | else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR)) |
lypinator | 0:bb348c97df44 | 2723 | { |
lypinator | 0:bb348c97df44 | 2724 | return HAL_SD_ERROR_RX_OVERRUN; |
lypinator | 0:bb348c97df44 | 2725 | } |
lypinator | 0:bb348c97df44 | 2726 | |
lypinator | 0:bb348c97df44 | 2727 | while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))) |
lypinator | 0:bb348c97df44 | 2728 | { |
lypinator | 0:bb348c97df44 | 2729 | *pSDstatus = SDIO_ReadFIFO(hsd->Instance); |
lypinator | 0:bb348c97df44 | 2730 | pSDstatus++; |
lypinator | 0:bb348c97df44 | 2731 | |
lypinator | 0:bb348c97df44 | 2732 | if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT) |
lypinator | 0:bb348c97df44 | 2733 | { |
lypinator | 0:bb348c97df44 | 2734 | return HAL_SD_ERROR_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2735 | } |
lypinator | 0:bb348c97df44 | 2736 | } |
lypinator | 0:bb348c97df44 | 2737 | |
lypinator | 0:bb348c97df44 | 2738 | /* Clear all the static status flags*/ |
lypinator | 0:bb348c97df44 | 2739 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 2740 | |
lypinator | 0:bb348c97df44 | 2741 | return HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2742 | } |
lypinator | 0:bb348c97df44 | 2743 | |
lypinator | 0:bb348c97df44 | 2744 | /** |
lypinator | 0:bb348c97df44 | 2745 | * @brief Returns the current card's status. |
lypinator | 0:bb348c97df44 | 2746 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 2747 | * @param pCardStatus pointer to the buffer that will contain the SD card |
lypinator | 0:bb348c97df44 | 2748 | * status (Card Status register) |
lypinator | 0:bb348c97df44 | 2749 | * @retval error state |
lypinator | 0:bb348c97df44 | 2750 | */ |
lypinator | 0:bb348c97df44 | 2751 | static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus) |
lypinator | 0:bb348c97df44 | 2752 | { |
lypinator | 0:bb348c97df44 | 2753 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2754 | |
lypinator | 0:bb348c97df44 | 2755 | if(pCardStatus == NULL) |
lypinator | 0:bb348c97df44 | 2756 | { |
lypinator | 0:bb348c97df44 | 2757 | return HAL_SD_ERROR_PARAM; |
lypinator | 0:bb348c97df44 | 2758 | } |
lypinator | 0:bb348c97df44 | 2759 | |
lypinator | 0:bb348c97df44 | 2760 | /* Send Status command */ |
lypinator | 0:bb348c97df44 | 2761 | errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); |
lypinator | 0:bb348c97df44 | 2762 | if(errorstate != HAL_OK) |
lypinator | 0:bb348c97df44 | 2763 | { |
lypinator | 0:bb348c97df44 | 2764 | return errorstate; |
lypinator | 0:bb348c97df44 | 2765 | } |
lypinator | 0:bb348c97df44 | 2766 | |
lypinator | 0:bb348c97df44 | 2767 | /* Get SD card status */ |
lypinator | 0:bb348c97df44 | 2768 | *pCardStatus = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); |
lypinator | 0:bb348c97df44 | 2769 | |
lypinator | 0:bb348c97df44 | 2770 | return HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2771 | } |
lypinator | 0:bb348c97df44 | 2772 | |
lypinator | 0:bb348c97df44 | 2773 | /** |
lypinator | 0:bb348c97df44 | 2774 | * @brief Enables the SDIO wide bus mode. |
lypinator | 0:bb348c97df44 | 2775 | * @param hsd pointer to SD handle |
lypinator | 0:bb348c97df44 | 2776 | * @retval error state |
lypinator | 0:bb348c97df44 | 2777 | */ |
lypinator | 0:bb348c97df44 | 2778 | static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 2779 | { |
lypinator | 0:bb348c97df44 | 2780 | uint32_t scr[2U] = {0U, 0U}; |
lypinator | 0:bb348c97df44 | 2781 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2782 | |
lypinator | 0:bb348c97df44 | 2783 | if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED) |
lypinator | 0:bb348c97df44 | 2784 | { |
lypinator | 0:bb348c97df44 | 2785 | return HAL_SD_ERROR_LOCK_UNLOCK_FAILED; |
lypinator | 0:bb348c97df44 | 2786 | } |
lypinator | 0:bb348c97df44 | 2787 | |
lypinator | 0:bb348c97df44 | 2788 | /* Get SCR Register */ |
lypinator | 0:bb348c97df44 | 2789 | errorstate = SD_FindSCR(hsd, scr); |
lypinator | 0:bb348c97df44 | 2790 | if(errorstate != HAL_OK) |
lypinator | 0:bb348c97df44 | 2791 | { |
lypinator | 0:bb348c97df44 | 2792 | return errorstate; |
lypinator | 0:bb348c97df44 | 2793 | } |
lypinator | 0:bb348c97df44 | 2794 | |
lypinator | 0:bb348c97df44 | 2795 | /* If requested card supports wide bus operation */ |
lypinator | 0:bb348c97df44 | 2796 | if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO) |
lypinator | 0:bb348c97df44 | 2797 | { |
lypinator | 0:bb348c97df44 | 2798 | /* Send CMD55 APP_CMD with argument as card's RCA.*/ |
lypinator | 0:bb348c97df44 | 2799 | errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); |
lypinator | 0:bb348c97df44 | 2800 | if(errorstate != HAL_OK) |
lypinator | 0:bb348c97df44 | 2801 | { |
lypinator | 0:bb348c97df44 | 2802 | return errorstate; |
lypinator | 0:bb348c97df44 | 2803 | } |
lypinator | 0:bb348c97df44 | 2804 | |
lypinator | 0:bb348c97df44 | 2805 | /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */ |
lypinator | 0:bb348c97df44 | 2806 | errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U); |
lypinator | 0:bb348c97df44 | 2807 | if(errorstate != HAL_OK) |
lypinator | 0:bb348c97df44 | 2808 | { |
lypinator | 0:bb348c97df44 | 2809 | return errorstate; |
lypinator | 0:bb348c97df44 | 2810 | } |
lypinator | 0:bb348c97df44 | 2811 | |
lypinator | 0:bb348c97df44 | 2812 | return HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2813 | } |
lypinator | 0:bb348c97df44 | 2814 | else |
lypinator | 0:bb348c97df44 | 2815 | { |
lypinator | 0:bb348c97df44 | 2816 | return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; |
lypinator | 0:bb348c97df44 | 2817 | } |
lypinator | 0:bb348c97df44 | 2818 | } |
lypinator | 0:bb348c97df44 | 2819 | |
lypinator | 0:bb348c97df44 | 2820 | /** |
lypinator | 0:bb348c97df44 | 2821 | * @brief Disables the SDIO wide bus mode. |
lypinator | 0:bb348c97df44 | 2822 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 2823 | * @retval error state |
lypinator | 0:bb348c97df44 | 2824 | */ |
lypinator | 0:bb348c97df44 | 2825 | static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 2826 | { |
lypinator | 0:bb348c97df44 | 2827 | uint32_t scr[2U] = {0U, 0U}; |
lypinator | 0:bb348c97df44 | 2828 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2829 | |
lypinator | 0:bb348c97df44 | 2830 | if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED) |
lypinator | 0:bb348c97df44 | 2831 | { |
lypinator | 0:bb348c97df44 | 2832 | return HAL_SD_ERROR_LOCK_UNLOCK_FAILED; |
lypinator | 0:bb348c97df44 | 2833 | } |
lypinator | 0:bb348c97df44 | 2834 | |
lypinator | 0:bb348c97df44 | 2835 | /* Get SCR Register */ |
lypinator | 0:bb348c97df44 | 2836 | errorstate = SD_FindSCR(hsd, scr); |
lypinator | 0:bb348c97df44 | 2837 | if(errorstate != HAL_OK) |
lypinator | 0:bb348c97df44 | 2838 | { |
lypinator | 0:bb348c97df44 | 2839 | return errorstate; |
lypinator | 0:bb348c97df44 | 2840 | } |
lypinator | 0:bb348c97df44 | 2841 | |
lypinator | 0:bb348c97df44 | 2842 | /* If requested card supports 1 bit mode operation */ |
lypinator | 0:bb348c97df44 | 2843 | if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO) |
lypinator | 0:bb348c97df44 | 2844 | { |
lypinator | 0:bb348c97df44 | 2845 | /* Send CMD55 APP_CMD with argument as card's RCA */ |
lypinator | 0:bb348c97df44 | 2846 | errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); |
lypinator | 0:bb348c97df44 | 2847 | if(errorstate != HAL_OK) |
lypinator | 0:bb348c97df44 | 2848 | { |
lypinator | 0:bb348c97df44 | 2849 | return errorstate; |
lypinator | 0:bb348c97df44 | 2850 | } |
lypinator | 0:bb348c97df44 | 2851 | |
lypinator | 0:bb348c97df44 | 2852 | /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */ |
lypinator | 0:bb348c97df44 | 2853 | errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U); |
lypinator | 0:bb348c97df44 | 2854 | if(errorstate != HAL_OK) |
lypinator | 0:bb348c97df44 | 2855 | { |
lypinator | 0:bb348c97df44 | 2856 | return errorstate; |
lypinator | 0:bb348c97df44 | 2857 | } |
lypinator | 0:bb348c97df44 | 2858 | |
lypinator | 0:bb348c97df44 | 2859 | return HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2860 | } |
lypinator | 0:bb348c97df44 | 2861 | else |
lypinator | 0:bb348c97df44 | 2862 | { |
lypinator | 0:bb348c97df44 | 2863 | return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; |
lypinator | 0:bb348c97df44 | 2864 | } |
lypinator | 0:bb348c97df44 | 2865 | } |
lypinator | 0:bb348c97df44 | 2866 | |
lypinator | 0:bb348c97df44 | 2867 | |
lypinator | 0:bb348c97df44 | 2868 | /** |
lypinator | 0:bb348c97df44 | 2869 | * @brief Finds the SD card SCR register value. |
lypinator | 0:bb348c97df44 | 2870 | * @param hsd Pointer to SD handle |
lypinator | 0:bb348c97df44 | 2871 | * @param pSCR pointer to the buffer that will contain the SCR value |
lypinator | 0:bb348c97df44 | 2872 | * @retval error state |
lypinator | 0:bb348c97df44 | 2873 | */ |
lypinator | 0:bb348c97df44 | 2874 | static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR) |
lypinator | 0:bb348c97df44 | 2875 | { |
lypinator | 0:bb348c97df44 | 2876 | SDIO_DataInitTypeDef config; |
lypinator | 0:bb348c97df44 | 2877 | uint32_t errorstate = HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2878 | uint32_t tickstart = HAL_GetTick(); |
lypinator | 0:bb348c97df44 | 2879 | uint32_t index = 0U; |
lypinator | 0:bb348c97df44 | 2880 | uint32_t tempscr[2U] = {0U, 0U}; |
lypinator | 0:bb348c97df44 | 2881 | |
lypinator | 0:bb348c97df44 | 2882 | /* Set Block Size To 8 Bytes */ |
lypinator | 0:bb348c97df44 | 2883 | errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U); |
lypinator | 0:bb348c97df44 | 2884 | if(errorstate != HAL_OK) |
lypinator | 0:bb348c97df44 | 2885 | { |
lypinator | 0:bb348c97df44 | 2886 | return errorstate; |
lypinator | 0:bb348c97df44 | 2887 | } |
lypinator | 0:bb348c97df44 | 2888 | |
lypinator | 0:bb348c97df44 | 2889 | /* Send CMD55 APP_CMD with argument as card's RCA */ |
lypinator | 0:bb348c97df44 | 2890 | errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U)); |
lypinator | 0:bb348c97df44 | 2891 | if(errorstate != HAL_OK) |
lypinator | 0:bb348c97df44 | 2892 | { |
lypinator | 0:bb348c97df44 | 2893 | return errorstate; |
lypinator | 0:bb348c97df44 | 2894 | } |
lypinator | 0:bb348c97df44 | 2895 | |
lypinator | 0:bb348c97df44 | 2896 | config.DataTimeOut = SDMMC_DATATIMEOUT; |
lypinator | 0:bb348c97df44 | 2897 | config.DataLength = 8U; |
lypinator | 0:bb348c97df44 | 2898 | config.DataBlockSize = SDIO_DATABLOCK_SIZE_8B; |
lypinator | 0:bb348c97df44 | 2899 | config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; |
lypinator | 0:bb348c97df44 | 2900 | config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; |
lypinator | 0:bb348c97df44 | 2901 | config.DPSM = SDIO_DPSM_ENABLE; |
lypinator | 0:bb348c97df44 | 2902 | SDIO_ConfigData(hsd->Instance, &config); |
lypinator | 0:bb348c97df44 | 2903 | |
lypinator | 0:bb348c97df44 | 2904 | /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */ |
lypinator | 0:bb348c97df44 | 2905 | errorstate = SDMMC_CmdSendSCR(hsd->Instance); |
lypinator | 0:bb348c97df44 | 2906 | if(errorstate != HAL_OK) |
lypinator | 0:bb348c97df44 | 2907 | { |
lypinator | 0:bb348c97df44 | 2908 | return errorstate; |
lypinator | 0:bb348c97df44 | 2909 | } |
lypinator | 0:bb348c97df44 | 2910 | |
lypinator | 0:bb348c97df44 | 2911 | while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND)) |
lypinator | 0:bb348c97df44 | 2912 | { |
lypinator | 0:bb348c97df44 | 2913 | if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) |
lypinator | 0:bb348c97df44 | 2914 | { |
lypinator | 0:bb348c97df44 | 2915 | *(tempscr + index) = SDIO_ReadFIFO(hsd->Instance); |
lypinator | 0:bb348c97df44 | 2916 | index++; |
lypinator | 0:bb348c97df44 | 2917 | } |
lypinator | 0:bb348c97df44 | 2918 | |
lypinator | 0:bb348c97df44 | 2919 | if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT) |
lypinator | 0:bb348c97df44 | 2920 | { |
lypinator | 0:bb348c97df44 | 2921 | return HAL_SD_ERROR_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2922 | } |
lypinator | 0:bb348c97df44 | 2923 | } |
lypinator | 0:bb348c97df44 | 2924 | |
lypinator | 0:bb348c97df44 | 2925 | if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) |
lypinator | 0:bb348c97df44 | 2926 | { |
lypinator | 0:bb348c97df44 | 2927 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT); |
lypinator | 0:bb348c97df44 | 2928 | |
lypinator | 0:bb348c97df44 | 2929 | return HAL_SD_ERROR_DATA_TIMEOUT; |
lypinator | 0:bb348c97df44 | 2930 | } |
lypinator | 0:bb348c97df44 | 2931 | else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) |
lypinator | 0:bb348c97df44 | 2932 | { |
lypinator | 0:bb348c97df44 | 2933 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL); |
lypinator | 0:bb348c97df44 | 2934 | |
lypinator | 0:bb348c97df44 | 2935 | return HAL_SD_ERROR_DATA_CRC_FAIL; |
lypinator | 0:bb348c97df44 | 2936 | } |
lypinator | 0:bb348c97df44 | 2937 | else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR)) |
lypinator | 0:bb348c97df44 | 2938 | { |
lypinator | 0:bb348c97df44 | 2939 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR); |
lypinator | 0:bb348c97df44 | 2940 | |
lypinator | 0:bb348c97df44 | 2941 | return HAL_SD_ERROR_RX_OVERRUN; |
lypinator | 0:bb348c97df44 | 2942 | } |
lypinator | 0:bb348c97df44 | 2943 | else |
lypinator | 0:bb348c97df44 | 2944 | { |
lypinator | 0:bb348c97df44 | 2945 | /* No error flag set */ |
lypinator | 0:bb348c97df44 | 2946 | /* Clear all the static flags */ |
lypinator | 0:bb348c97df44 | 2947 | __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); |
lypinator | 0:bb348c97df44 | 2948 | |
lypinator | 0:bb348c97df44 | 2949 | *(pSCR + 1U) = ((tempscr[0U] & SDMMC_0TO7BITS) << 24U) | ((tempscr[0U] & SDMMC_8TO15BITS) << 8U) |\ |
lypinator | 0:bb348c97df44 | 2950 | ((tempscr[0U] & SDMMC_16TO23BITS) >> 8U) | ((tempscr[0U] & SDMMC_24TO31BITS) >> 24U); |
lypinator | 0:bb348c97df44 | 2951 | |
lypinator | 0:bb348c97df44 | 2952 | *(pSCR) = ((tempscr[1U] & SDMMC_0TO7BITS) << 24U) | ((tempscr[1U] & SDMMC_8TO15BITS) << 8U) |\ |
lypinator | 0:bb348c97df44 | 2953 | ((tempscr[1U] & SDMMC_16TO23BITS) >> 8U) | ((tempscr[1U] & SDMMC_24TO31BITS) >> 24U); |
lypinator | 0:bb348c97df44 | 2954 | } |
lypinator | 0:bb348c97df44 | 2955 | |
lypinator | 0:bb348c97df44 | 2956 | return HAL_SD_ERROR_NONE; |
lypinator | 0:bb348c97df44 | 2957 | } |
lypinator | 0:bb348c97df44 | 2958 | |
lypinator | 0:bb348c97df44 | 2959 | /** |
lypinator | 0:bb348c97df44 | 2960 | * @brief Wrap up reading in non-blocking mode. |
lypinator | 0:bb348c97df44 | 2961 | * @param hsd pointer to a SD_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2962 | * the configuration information. |
lypinator | 0:bb348c97df44 | 2963 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2964 | */ |
lypinator | 0:bb348c97df44 | 2965 | static HAL_StatusTypeDef SD_Read_IT(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 2966 | { |
lypinator | 0:bb348c97df44 | 2967 | uint32_t count = 0U; |
lypinator | 0:bb348c97df44 | 2968 | uint32_t* tmp; |
lypinator | 0:bb348c97df44 | 2969 | |
lypinator | 0:bb348c97df44 | 2970 | tmp = (uint32_t*)hsd->pRxBuffPtr; |
lypinator | 0:bb348c97df44 | 2971 | |
lypinator | 0:bb348c97df44 | 2972 | /* Read data from SDIO Rx FIFO */ |
lypinator | 0:bb348c97df44 | 2973 | for(count = 0U; count < 8U; count++) |
lypinator | 0:bb348c97df44 | 2974 | { |
lypinator | 0:bb348c97df44 | 2975 | *(tmp + count) = SDIO_ReadFIFO(hsd->Instance); |
lypinator | 0:bb348c97df44 | 2976 | } |
lypinator | 0:bb348c97df44 | 2977 | |
lypinator | 0:bb348c97df44 | 2978 | hsd->pRxBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 2979 | |
lypinator | 0:bb348c97df44 | 2980 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 2981 | } |
lypinator | 0:bb348c97df44 | 2982 | |
lypinator | 0:bb348c97df44 | 2983 | /** |
lypinator | 0:bb348c97df44 | 2984 | * @brief Wrap up writing in non-blocking mode. |
lypinator | 0:bb348c97df44 | 2985 | * @param hsd pointer to a SD_HandleTypeDef structure that contains |
lypinator | 0:bb348c97df44 | 2986 | * the configuration information. |
lypinator | 0:bb348c97df44 | 2987 | * @retval HAL status |
lypinator | 0:bb348c97df44 | 2988 | */ |
lypinator | 0:bb348c97df44 | 2989 | static HAL_StatusTypeDef SD_Write_IT(SD_HandleTypeDef *hsd) |
lypinator | 0:bb348c97df44 | 2990 | { |
lypinator | 0:bb348c97df44 | 2991 | uint32_t count = 0U; |
lypinator | 0:bb348c97df44 | 2992 | uint32_t* tmp; |
lypinator | 0:bb348c97df44 | 2993 | |
lypinator | 0:bb348c97df44 | 2994 | tmp = (uint32_t*)hsd->pTxBuffPtr; |
lypinator | 0:bb348c97df44 | 2995 | |
lypinator | 0:bb348c97df44 | 2996 | /* Write data to SDIO Tx FIFO */ |
lypinator | 0:bb348c97df44 | 2997 | for(count = 0U; count < 8U; count++) |
lypinator | 0:bb348c97df44 | 2998 | { |
lypinator | 0:bb348c97df44 | 2999 | SDIO_WriteFIFO(hsd->Instance, (tmp + count)); |
lypinator | 0:bb348c97df44 | 3000 | } |
lypinator | 0:bb348c97df44 | 3001 | |
lypinator | 0:bb348c97df44 | 3002 | hsd->pTxBuffPtr += 8U; |
lypinator | 0:bb348c97df44 | 3003 | |
lypinator | 0:bb348c97df44 | 3004 | return HAL_OK; |
lypinator | 0:bb348c97df44 | 3005 | } |
lypinator | 0:bb348c97df44 | 3006 | |
lypinator | 0:bb348c97df44 | 3007 | /** |
lypinator | 0:bb348c97df44 | 3008 | * @} |
lypinator | 0:bb348c97df44 | 3009 | */ |
lypinator | 0:bb348c97df44 | 3010 | |
lypinator | 0:bb348c97df44 | 3011 | #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx || |
lypinator | 0:bb348c97df44 | 3012 | STM32F401xC || STM32F401xE || STM32F411xE || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx || |
lypinator | 0:bb348c97df44 | 3013 | STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */ |
lypinator | 0:bb348c97df44 | 3014 | |
lypinator | 0:bb348c97df44 | 3015 | #endif /* HAL_SD_MODULE_ENABLED */ |
lypinator | 0:bb348c97df44 | 3016 | |
lypinator | 0:bb348c97df44 | 3017 | /** |
lypinator | 0:bb348c97df44 | 3018 | * @} |
lypinator | 0:bb348c97df44 | 3019 | */ |
lypinator | 0:bb348c97df44 | 3020 | |
lypinator | 0:bb348c97df44 | 3021 | /** |
lypinator | 0:bb348c97df44 | 3022 | * @} |
lypinator | 0:bb348c97df44 | 3023 | */ |
lypinator | 0:bb348c97df44 | 3024 | |
lypinator | 0:bb348c97df44 | 3025 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |