Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lypinator 0:bb348c97df44 1 /**
lypinator 0:bb348c97df44 2 ******************************************************************************
lypinator 0:bb348c97df44 3 * @file stm32f4xx_hal_mmc.c
lypinator 0:bb348c97df44 4 * @author MCD Application Team
lypinator 0:bb348c97df44 5 * @brief MMC 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 (MMC) 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 * + MMC 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 (SDMMC and GPIO) are performed by
lypinator 0:bb348c97df44 20 the user in HAL_MMC_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 SDMMC memories which uses the HAL
lypinator 0:bb348c97df44 27 SDMMC driver functions to interface with MMC and eMMC cards devices.
lypinator 0:bb348c97df44 28 It is used as follows:
lypinator 0:bb348c97df44 29
lypinator 0:bb348c97df44 30 (#)Initialize the SDMMC low level resources by implement the HAL_MMC_MspInit() API:
lypinator 0:bb348c97df44 31 (##) Enable the SDMMC interface clock using __HAL_RCC_SDMMC_CLK_ENABLE();
lypinator 0:bb348c97df44 32 (##) SDMMC pins configuration for MMC card
lypinator 0:bb348c97df44 33 (+++) Enable the clock for the SDMMC GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
lypinator 0:bb348c97df44 34 (+++) Configure these SDMMC 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_MMC_ReadBlocks_DMA()
lypinator 0:bb348c97df44 37 and HAL_MMC_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 SDMMC and DMA interrupt priorities using functions
lypinator 0:bb348c97df44 42 HAL_NVIC_SetPriority(); DMA priority is superior to SDMMC's priority
lypinator 0:bb348c97df44 43 (+++) Enable the NVIC DMA and SDMMC IRQs using function HAL_NVIC_EnableIRQ()
lypinator 0:bb348c97df44 44 (+++) SDMMC interrupts are managed using the macros __HAL_MMC_ENABLE_IT()
lypinator 0:bb348c97df44 45 and __HAL_MMC_DISABLE_IT() inside the communication process.
lypinator 0:bb348c97df44 46 (+++) SDMMC interrupts pending bits are managed using the macros __HAL_MMC_GET_IT()
lypinator 0:bb348c97df44 47 and __HAL_MMC_CLEAR_IT()
lypinator 0:bb348c97df44 48 (##) NVIC configuration if you need to use interrupt process (HAL_MMC_ReadBlocks_IT()
lypinator 0:bb348c97df44 49 and HAL_MMC_WriteBlocks_IT() APIs).
lypinator 0:bb348c97df44 50 (+++) Configure the SDMMC interrupt priorities using function
lypinator 0:bb348c97df44 51 HAL_NVIC_SetPriority();
lypinator 0:bb348c97df44 52 (+++) Enable the NVIC SDMMC IRQs using function HAL_NVIC_EnableIRQ()
lypinator 0:bb348c97df44 53 (+++) SDMMC interrupts are managed using the macros __HAL_MMC_ENABLE_IT()
lypinator 0:bb348c97df44 54 and __HAL_MMC_DISABLE_IT() inside the communication process.
lypinator 0:bb348c97df44 55 (+++) SDMMC interrupts pending bits are managed using the macros __HAL_MMC_GET_IT()
lypinator 0:bb348c97df44 56 and __HAL_MMC_CLEAR_IT()
lypinator 0:bb348c97df44 57 (#) At this stage, you can perform MMC read/write/erase operations after MMC card initialization
lypinator 0:bb348c97df44 58
lypinator 0:bb348c97df44 59
lypinator 0:bb348c97df44 60 *** MMC Card Initialization and configuration ***
lypinator 0:bb348c97df44 61 ================================================
lypinator 0:bb348c97df44 62 [..]
lypinator 0:bb348c97df44 63 To initialize the MMC Card, use the HAL_MMC_Init() function. It Initializes
lypinator 0:bb348c97df44 64 SDMMC IP (STM32 side) and the MMC 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 SDMMC 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 MMC Card frequency (SDMMC_CK) is computed as follows:
lypinator 0:bb348c97df44 71
lypinator 0:bb348c97df44 72 SDMMC_CK = SDMMCCLK / (ClockDiv + 2)
lypinator 0:bb348c97df44 73
lypinator 0:bb348c97df44 74 In initialization mode and according to the MMC Card standard,
lypinator 0:bb348c97df44 75 make sure that the SDMMC_CK frequency doesn't exceed 400KHz.
lypinator 0:bb348c97df44 76
lypinator 0:bb348c97df44 77 This phase of initialization is done through SDMMC_Init() and
lypinator 0:bb348c97df44 78 SDMMC_PowerState_ON() SDMMC low level APIs.
lypinator 0:bb348c97df44 79
lypinator 0:bb348c97df44 80 (#) Initialize the MMC card. The API used is HAL_MMC_InitCard().
lypinator 0:bb348c97df44 81 This phase allows the card initialization and identification
lypinator 0:bb348c97df44 82 and check the MMC Card type (Standard Capacity or High Capacity)
lypinator 0:bb348c97df44 83 The initialization flow is compatible with MMC standard.
lypinator 0:bb348c97df44 84
lypinator 0:bb348c97df44 85 This API (HAL_MMC_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 MMC 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 MMC Card standard, make sure that the
lypinator 0:bb348c97df44 92 SDMMC_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 SDMMC
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 MMC Card according to the address read with the step 2.
lypinator 0:bb348c97df44 98
lypinator 0:bb348c97df44 99 (#) Configure the MMC Card in wide bus mode: 4-bits data.
lypinator 0:bb348c97df44 100
lypinator 0:bb348c97df44 101 *** MMC Card Read operation ***
lypinator 0:bb348c97df44 102 ==============================
lypinator 0:bb348c97df44 103 [..]
lypinator 0:bb348c97df44 104 (+) You can read from MMC card in polling mode by using function HAL_MMC_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_MMC_GetCardState() function for MMC card state.
lypinator 0:bb348c97df44 110
lypinator 0:bb348c97df44 111 (+) You can read from MMC card in DMA mode by using function HAL_MMC_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_MMC_GetCardState() function for MMC card state.
lypinator 0:bb348c97df44 117 You could also check the DMA transfer process through the MMC Rx interrupt event.
lypinator 0:bb348c97df44 118
lypinator 0:bb348c97df44 119 (+) You can read from MMC card in Interrupt mode by using function HAL_MMC_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_MMC_GetCardState() function for MMC card state.
lypinator 0:bb348c97df44 125 You could also check the IT transfer process through the MMC Rx interrupt event.
lypinator 0:bb348c97df44 126
lypinator 0:bb348c97df44 127 *** MMC Card Write operation ***
lypinator 0:bb348c97df44 128 ===============================
lypinator 0:bb348c97df44 129 [..]
lypinator 0:bb348c97df44 130 (+) You can write to MMC card in polling mode by using function HAL_MMC_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_MMC_GetCardState() function for MMC card state.
lypinator 0:bb348c97df44 136
lypinator 0:bb348c97df44 137 (+) You can write to MMC card in DMA mode by using function HAL_MMC_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_MMC_GetCardState() function for MMC card state.
lypinator 0:bb348c97df44 143 You could also check the DMA transfer process through the MMC Tx interrupt event.
lypinator 0:bb348c97df44 144
lypinator 0:bb348c97df44 145 (+) You can write to MMC card in Interrupt mode by using function HAL_MMC_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_MMC_GetCardState() function for MMC card state.
lypinator 0:bb348c97df44 151 You could also check the IT transfer process through the MMC Tx interrupt event.
lypinator 0:bb348c97df44 152
lypinator 0:bb348c97df44 153 *** MMC card status ***
lypinator 0:bb348c97df44 154 ======================
lypinator 0:bb348c97df44 155 [..]
lypinator 0:bb348c97df44 156 (+) The MMC Status contains status bits that are related to the MMC Memory
lypinator 0:bb348c97df44 157 Card proprietary features. To get MMC card status use the HAL_MMC_GetCardStatus().
lypinator 0:bb348c97df44 158
lypinator 0:bb348c97df44 159 *** MMC card information ***
lypinator 0:bb348c97df44 160 ===========================
lypinator 0:bb348c97df44 161 [..]
lypinator 0:bb348c97df44 162 (+) To get MMC card information, you can use the function HAL_MMC_GetCardInfo().
lypinator 0:bb348c97df44 163 It returns useful information about the MMC card such as block size, card type,
lypinator 0:bb348c97df44 164 block number ...
lypinator 0:bb348c97df44 165
lypinator 0:bb348c97df44 166 *** MMC card CSD register ***
lypinator 0:bb348c97df44 167 ============================
lypinator 0:bb348c97df44 168 [..]
lypinator 0:bb348c97df44 169 (+) The HAL_MMC_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 *** MMC card CID register ***
lypinator 0:bb348c97df44 173 ============================
lypinator 0:bb348c97df44 174 [..]
lypinator 0:bb348c97df44 175 (+) The HAL_MMC_GetCardCID() API allows to get the parameters of the CID register.
lypinator 0:bb348c97df44 176 Some of the CID parameters are useful for card initialization and identification.
lypinator 0:bb348c97df44 177
lypinator 0:bb348c97df44 178 *** MMC HAL driver macros list ***
lypinator 0:bb348c97df44 179 ==================================
lypinator 0:bb348c97df44 180 [..]
lypinator 0:bb348c97df44 181 Below the list of most used macros in MMC HAL driver.
lypinator 0:bb348c97df44 182
lypinator 0:bb348c97df44 183 (+) __HAL_MMC_ENABLE : Enable the MMC device
lypinator 0:bb348c97df44 184 (+) __HAL_MMC_DISABLE : Disable the MMC device
lypinator 0:bb348c97df44 185 (+) __HAL_MMC_DMA_ENABLE: Enable the SDMMC DMA transfer
lypinator 0:bb348c97df44 186 (+) __HAL_MMC_DMA_DISABLE: Disable the SDMMC DMA transfer
lypinator 0:bb348c97df44 187 (+) __HAL_MMC_ENABLE_IT: Enable the MMC device interrupt
lypinator 0:bb348c97df44 188 (+) __HAL_MMC_DISABLE_IT: Disable the MMC device interrupt
lypinator 0:bb348c97df44 189 (+) __HAL_MMC_GET_FLAG:Check whether the specified MMC flag is set or not
lypinator 0:bb348c97df44 190 (+) __HAL_MMC_CLEAR_FLAG: Clear the MMC's pending flags
lypinator 0:bb348c97df44 191
lypinator 0:bb348c97df44 192 [..]
lypinator 0:bb348c97df44 193 (@) You can refer to the MMC 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>&copy; 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 MMC
lypinator 0:bb348c97df44 234 * @{
lypinator 0:bb348c97df44 235 */
lypinator 0:bb348c97df44 236
lypinator 0:bb348c97df44 237 #ifdef HAL_MMC_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 MMC_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 MMC_Private_Functions MMC Private Functions
lypinator 0:bb348c97df44 260 * @{
lypinator 0:bb348c97df44 261 */
lypinator 0:bb348c97df44 262 static uint32_t MMC_InitCard(MMC_HandleTypeDef *hmmc);
lypinator 0:bb348c97df44 263 static uint32_t MMC_PowerON(MMC_HandleTypeDef *hmmc);
lypinator 0:bb348c97df44 264 static uint32_t MMC_SendStatus(MMC_HandleTypeDef *hmmc, uint32_t *pCardStatus);
lypinator 0:bb348c97df44 265 static HAL_StatusTypeDef MMC_PowerOFF(MMC_HandleTypeDef *hmmc);
lypinator 0:bb348c97df44 266 static HAL_StatusTypeDef MMC_Write_IT(MMC_HandleTypeDef *hmmc);
lypinator 0:bb348c97df44 267 static HAL_StatusTypeDef MMC_Read_IT(MMC_HandleTypeDef *hmmc);
lypinator 0:bb348c97df44 268 static void MMC_DMATransmitCplt(DMA_HandleTypeDef *hdma);
lypinator 0:bb348c97df44 269 static void MMC_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
lypinator 0:bb348c97df44 270 static void MMC_DMAError(DMA_HandleTypeDef *hdma);
lypinator 0:bb348c97df44 271 static void MMC_DMATxAbort(DMA_HandleTypeDef *hdma);
lypinator 0:bb348c97df44 272 static void MMC_DMARxAbort(DMA_HandleTypeDef *hdma);
lypinator 0:bb348c97df44 273 /**
lypinator 0:bb348c97df44 274 * @}
lypinator 0:bb348c97df44 275 */
lypinator 0:bb348c97df44 276
lypinator 0:bb348c97df44 277 /* Exported functions --------------------------------------------------------*/
lypinator 0:bb348c97df44 278 /** @addtogroup MMC_Exported_Functions
lypinator 0:bb348c97df44 279 * @{
lypinator 0:bb348c97df44 280 */
lypinator 0:bb348c97df44 281
lypinator 0:bb348c97df44 282 /** @addtogroup MMC_Exported_Functions_Group1
lypinator 0:bb348c97df44 283 * @brief Initialization and de-initialization functions
lypinator 0:bb348c97df44 284 *
lypinator 0:bb348c97df44 285 @verbatim
lypinator 0:bb348c97df44 286 ==============================================================================
lypinator 0:bb348c97df44 287 ##### Initialization and de-initialization functions #####
lypinator 0:bb348c97df44 288 ==============================================================================
lypinator 0:bb348c97df44 289 [..]
lypinator 0:bb348c97df44 290 This section provides functions allowing to initialize/de-initialize the MMC
lypinator 0:bb348c97df44 291 card device to be ready for use.
lypinator 0:bb348c97df44 292
lypinator 0:bb348c97df44 293 @endverbatim
lypinator 0:bb348c97df44 294 * @{
lypinator 0:bb348c97df44 295 */
lypinator 0:bb348c97df44 296
lypinator 0:bb348c97df44 297 /**
lypinator 0:bb348c97df44 298 * @brief Initializes the MMC according to the specified parameters in the
lypinator 0:bb348c97df44 299 MMC_HandleTypeDef and create the associated handle.
lypinator 0:bb348c97df44 300 * @param hmmc Pointer to the MMC handle
lypinator 0:bb348c97df44 301 * @retval HAL status
lypinator 0:bb348c97df44 302 */
lypinator 0:bb348c97df44 303 HAL_StatusTypeDef HAL_MMC_Init(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 304 {
lypinator 0:bb348c97df44 305 /* Check the MMC handle allocation */
lypinator 0:bb348c97df44 306 if(hmmc == NULL)
lypinator 0:bb348c97df44 307 {
lypinator 0:bb348c97df44 308 return HAL_ERROR;
lypinator 0:bb348c97df44 309 }
lypinator 0:bb348c97df44 310
lypinator 0:bb348c97df44 311 /* Check the parameters */
lypinator 0:bb348c97df44 312 assert_param(IS_SDIO_ALL_INSTANCE(hmmc->Instance));
lypinator 0:bb348c97df44 313 assert_param(IS_SDIO_CLOCK_EDGE(hmmc->Init.ClockEdge));
lypinator 0:bb348c97df44 314 assert_param(IS_SDIO_CLOCK_BYPASS(hmmc->Init.ClockBypass));
lypinator 0:bb348c97df44 315 assert_param(IS_SDIO_CLOCK_POWER_SAVE(hmmc->Init.ClockPowerSave));
lypinator 0:bb348c97df44 316 assert_param(IS_SDIO_BUS_WIDE(hmmc->Init.BusWide));
lypinator 0:bb348c97df44 317 assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(hmmc->Init.HardwareFlowControl));
lypinator 0:bb348c97df44 318 assert_param(IS_SDIO_CLKDIV(hmmc->Init.ClockDiv));
lypinator 0:bb348c97df44 319
lypinator 0:bb348c97df44 320 if(hmmc->State == HAL_MMC_STATE_RESET)
lypinator 0:bb348c97df44 321 {
lypinator 0:bb348c97df44 322 /* Allocate lock resource and initialize it */
lypinator 0:bb348c97df44 323 hmmc->Lock = HAL_UNLOCKED;
lypinator 0:bb348c97df44 324 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
lypinator 0:bb348c97df44 325 HAL_MMC_MspInit(hmmc);
lypinator 0:bb348c97df44 326 }
lypinator 0:bb348c97df44 327
lypinator 0:bb348c97df44 328 hmmc->State = HAL_MMC_STATE_BUSY;
lypinator 0:bb348c97df44 329
lypinator 0:bb348c97df44 330 /* Initialize the Card parameters */
lypinator 0:bb348c97df44 331 HAL_MMC_InitCard(hmmc);
lypinator 0:bb348c97df44 332
lypinator 0:bb348c97df44 333 /* Initialize the error code */
lypinator 0:bb348c97df44 334 hmmc->ErrorCode = HAL_DMA_ERROR_NONE;
lypinator 0:bb348c97df44 335
lypinator 0:bb348c97df44 336 /* Initialize the MMC operation */
lypinator 0:bb348c97df44 337 hmmc->Context = MMC_CONTEXT_NONE;
lypinator 0:bb348c97df44 338
lypinator 0:bb348c97df44 339 /* Initialize the MMC state */
lypinator 0:bb348c97df44 340 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 341
lypinator 0:bb348c97df44 342 return HAL_OK;
lypinator 0:bb348c97df44 343 }
lypinator 0:bb348c97df44 344
lypinator 0:bb348c97df44 345 /**
lypinator 0:bb348c97df44 346 * @brief Initializes the MMC Card.
lypinator 0:bb348c97df44 347 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 348 * @note This function initializes the MMC card. It could be used when a card
lypinator 0:bb348c97df44 349 re-initialization is needed.
lypinator 0:bb348c97df44 350 * @retval HAL status
lypinator 0:bb348c97df44 351 */
lypinator 0:bb348c97df44 352 HAL_StatusTypeDef HAL_MMC_InitCard(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 353 {
lypinator 0:bb348c97df44 354 uint32_t errorstate = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 355 MMC_InitTypeDef Init;
lypinator 0:bb348c97df44 356
lypinator 0:bb348c97df44 357 /* Default SDMMC peripheral configuration for MMC card initialization */
lypinator 0:bb348c97df44 358 Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
lypinator 0:bb348c97df44 359 Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
lypinator 0:bb348c97df44 360 Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
lypinator 0:bb348c97df44 361 Init.BusWide = SDIO_BUS_WIDE_1B;
lypinator 0:bb348c97df44 362 Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
lypinator 0:bb348c97df44 363 Init.ClockDiv = SDIO_INIT_CLK_DIV;
lypinator 0:bb348c97df44 364
lypinator 0:bb348c97df44 365 /* Initialize SDMMC peripheral interface with default configuration */
lypinator 0:bb348c97df44 366 SDIO_Init(hmmc->Instance, Init);
lypinator 0:bb348c97df44 367
lypinator 0:bb348c97df44 368 /* Disable SDMMC Clock */
lypinator 0:bb348c97df44 369 __HAL_MMC_DISABLE(hmmc);
lypinator 0:bb348c97df44 370
lypinator 0:bb348c97df44 371 /* Set Power State to ON */
lypinator 0:bb348c97df44 372 SDIO_PowerState_ON(hmmc->Instance);
lypinator 0:bb348c97df44 373
lypinator 0:bb348c97df44 374 /* Enable SDMMC Clock */
lypinator 0:bb348c97df44 375 __HAL_MMC_ENABLE(hmmc);
lypinator 0:bb348c97df44 376
lypinator 0:bb348c97df44 377 /* Required power up waiting time before starting the SD initialization
lypinator 0:bb348c97df44 378 sequence */
lypinator 0:bb348c97df44 379 HAL_Delay(2U);
lypinator 0:bb348c97df44 380
lypinator 0:bb348c97df44 381 /* Identify card operating voltage */
lypinator 0:bb348c97df44 382 errorstate = MMC_PowerON(hmmc);
lypinator 0:bb348c97df44 383 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 384 {
lypinator 0:bb348c97df44 385 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 386 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 387 return HAL_ERROR;
lypinator 0:bb348c97df44 388 }
lypinator 0:bb348c97df44 389
lypinator 0:bb348c97df44 390 /* Card initialization */
lypinator 0:bb348c97df44 391 errorstate = MMC_InitCard(hmmc);
lypinator 0:bb348c97df44 392 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 393 {
lypinator 0:bb348c97df44 394 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 395 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 396 return HAL_ERROR;
lypinator 0:bb348c97df44 397 }
lypinator 0:bb348c97df44 398
lypinator 0:bb348c97df44 399 return HAL_OK;
lypinator 0:bb348c97df44 400 }
lypinator 0:bb348c97df44 401
lypinator 0:bb348c97df44 402 /**
lypinator 0:bb348c97df44 403 * @brief De-Initializes the MMC card.
lypinator 0:bb348c97df44 404 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 405 * @retval HAL status
lypinator 0:bb348c97df44 406 */
lypinator 0:bb348c97df44 407 HAL_StatusTypeDef HAL_MMC_DeInit(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 408 {
lypinator 0:bb348c97df44 409 /* Check the MMC handle allocation */
lypinator 0:bb348c97df44 410 if(hmmc == NULL)
lypinator 0:bb348c97df44 411 {
lypinator 0:bb348c97df44 412 return HAL_ERROR;
lypinator 0:bb348c97df44 413 }
lypinator 0:bb348c97df44 414
lypinator 0:bb348c97df44 415 /* Check the parameters */
lypinator 0:bb348c97df44 416 assert_param(IS_SDIO_ALL_INSTANCE(hmmc->Instance));
lypinator 0:bb348c97df44 417
lypinator 0:bb348c97df44 418 hmmc->State = HAL_MMC_STATE_BUSY;
lypinator 0:bb348c97df44 419
lypinator 0:bb348c97df44 420 /* Set SD power state to off */
lypinator 0:bb348c97df44 421 MMC_PowerOFF(hmmc);
lypinator 0:bb348c97df44 422
lypinator 0:bb348c97df44 423 /* De-Initialize the MSP layer */
lypinator 0:bb348c97df44 424 HAL_MMC_MspDeInit(hmmc);
lypinator 0:bb348c97df44 425
lypinator 0:bb348c97df44 426 hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 427 hmmc->State = HAL_MMC_STATE_RESET;
lypinator 0:bb348c97df44 428
lypinator 0:bb348c97df44 429 return HAL_OK;
lypinator 0:bb348c97df44 430 }
lypinator 0:bb348c97df44 431
lypinator 0:bb348c97df44 432
lypinator 0:bb348c97df44 433 /**
lypinator 0:bb348c97df44 434 * @brief Initializes the MMC MSP.
lypinator 0:bb348c97df44 435 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 436 * @retval None
lypinator 0:bb348c97df44 437 */
lypinator 0:bb348c97df44 438 __weak void HAL_MMC_MspInit(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 439 {
lypinator 0:bb348c97df44 440 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 441 UNUSED(hmmc);
lypinator 0:bb348c97df44 442
lypinator 0:bb348c97df44 443 /* NOTE : This function Should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 444 the HAL_MMC_MspInit could be implemented in the user file
lypinator 0:bb348c97df44 445 */
lypinator 0:bb348c97df44 446 }
lypinator 0:bb348c97df44 447
lypinator 0:bb348c97df44 448 /**
lypinator 0:bb348c97df44 449 * @brief De-Initialize MMC MSP.
lypinator 0:bb348c97df44 450 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 451 * @retval None
lypinator 0:bb348c97df44 452 */
lypinator 0:bb348c97df44 453 __weak void HAL_MMC_MspDeInit(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 454 {
lypinator 0:bb348c97df44 455 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 456 UNUSED(hmmc);
lypinator 0:bb348c97df44 457
lypinator 0:bb348c97df44 458 /* NOTE : This function Should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 459 the HAL_MMC_MspDeInit could be implemented in the user file
lypinator 0:bb348c97df44 460 */
lypinator 0:bb348c97df44 461 }
lypinator 0:bb348c97df44 462
lypinator 0:bb348c97df44 463 /**
lypinator 0:bb348c97df44 464 * @}
lypinator 0:bb348c97df44 465 */
lypinator 0:bb348c97df44 466
lypinator 0:bb348c97df44 467 /** @addtogroup MMC_Exported_Functions_Group2
lypinator 0:bb348c97df44 468 * @brief Data transfer functions
lypinator 0:bb348c97df44 469 *
lypinator 0:bb348c97df44 470 @verbatim
lypinator 0:bb348c97df44 471 ==============================================================================
lypinator 0:bb348c97df44 472 ##### IO operation functions #####
lypinator 0:bb348c97df44 473 ==============================================================================
lypinator 0:bb348c97df44 474 [..]
lypinator 0:bb348c97df44 475 This subsection provides a set of functions allowing to manage the data
lypinator 0:bb348c97df44 476 transfer from/to MMC card.
lypinator 0:bb348c97df44 477
lypinator 0:bb348c97df44 478 @endverbatim
lypinator 0:bb348c97df44 479 * @{
lypinator 0:bb348c97df44 480 */
lypinator 0:bb348c97df44 481
lypinator 0:bb348c97df44 482 /**
lypinator 0:bb348c97df44 483 * @brief Reads block(s) from a specified address in a card. The Data transfer
lypinator 0:bb348c97df44 484 * is managed by polling mode.
lypinator 0:bb348c97df44 485 * @note This API should be followed by a check on the card state through
lypinator 0:bb348c97df44 486 * HAL_MMC_GetCardState().
lypinator 0:bb348c97df44 487 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 488 * @param pData pointer to the buffer that will contain the received data
lypinator 0:bb348c97df44 489 * @param BlockAdd Block Address from where data is to be read
lypinator 0:bb348c97df44 490 * @param NumberOfBlocks Number of MMC blocks to read
lypinator 0:bb348c97df44 491 * @param Timeout Specify timeout value
lypinator 0:bb348c97df44 492 * @retval HAL status
lypinator 0:bb348c97df44 493 */
lypinator 0:bb348c97df44 494 HAL_StatusTypeDef HAL_MMC_ReadBlocks(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
lypinator 0:bb348c97df44 495 {
lypinator 0:bb348c97df44 496 SDIO_DataInitTypeDef config;
lypinator 0:bb348c97df44 497 uint32_t errorstate = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 498 uint32_t tickstart = HAL_GetTick();
lypinator 0:bb348c97df44 499 uint32_t count = 0U, *tempbuff = (uint32_t *)pData;
lypinator 0:bb348c97df44 500
lypinator 0:bb348c97df44 501 if(NULL == pData)
lypinator 0:bb348c97df44 502 {
lypinator 0:bb348c97df44 503 hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
lypinator 0:bb348c97df44 504 return HAL_ERROR;
lypinator 0:bb348c97df44 505 }
lypinator 0:bb348c97df44 506
lypinator 0:bb348c97df44 507 if(hmmc->State == HAL_MMC_STATE_READY)
lypinator 0:bb348c97df44 508 {
lypinator 0:bb348c97df44 509 hmmc->ErrorCode = HAL_DMA_ERROR_NONE;
lypinator 0:bb348c97df44 510
lypinator 0:bb348c97df44 511 if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
lypinator 0:bb348c97df44 512 {
lypinator 0:bb348c97df44 513 hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
lypinator 0:bb348c97df44 514 return HAL_ERROR;
lypinator 0:bb348c97df44 515 }
lypinator 0:bb348c97df44 516
lypinator 0:bb348c97df44 517 hmmc->State = HAL_MMC_STATE_BUSY;
lypinator 0:bb348c97df44 518
lypinator 0:bb348c97df44 519 /* Initialize data control register */
lypinator 0:bb348c97df44 520 hmmc->Instance->DCTRL = 0U;
lypinator 0:bb348c97df44 521
lypinator 0:bb348c97df44 522 /* Check the Card capacity in term of Logical number of blocks */
lypinator 0:bb348c97df44 523 if ((hmmc->MmcCard.LogBlockNbr) < CAPACITY)
lypinator 0:bb348c97df44 524 {
lypinator 0:bb348c97df44 525 BlockAdd *= 512U;
lypinator 0:bb348c97df44 526 }
lypinator 0:bb348c97df44 527
lypinator 0:bb348c97df44 528 /* Set Block Size for Card */
lypinator 0:bb348c97df44 529 errorstate = SDMMC_CmdBlockLength(hmmc->Instance, BLOCKSIZE);
lypinator 0:bb348c97df44 530 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 531 {
lypinator 0:bb348c97df44 532 /* Clear all the static flags */
lypinator 0:bb348c97df44 533 __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
lypinator 0:bb348c97df44 534 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 535 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 536 return HAL_ERROR;
lypinator 0:bb348c97df44 537 }
lypinator 0:bb348c97df44 538
lypinator 0:bb348c97df44 539 /* Configure the MMC DPSM (Data Path State Machine) */
lypinator 0:bb348c97df44 540 config.DataTimeOut = SDMMC_DATATIMEOUT;
lypinator 0:bb348c97df44 541 config.DataLength = NumberOfBlocks * BLOCKSIZE;
lypinator 0:bb348c97df44 542 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
lypinator 0:bb348c97df44 543 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
lypinator 0:bb348c97df44 544 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
lypinator 0:bb348c97df44 545 config.DPSM = SDIO_DPSM_ENABLE;
lypinator 0:bb348c97df44 546 SDIO_ConfigData(hmmc->Instance, &config);
lypinator 0:bb348c97df44 547
lypinator 0:bb348c97df44 548 /* Read block(s) in polling mode */
lypinator 0:bb348c97df44 549 if(NumberOfBlocks > 1U)
lypinator 0:bb348c97df44 550 {
lypinator 0:bb348c97df44 551 hmmc->Context = MMC_CONTEXT_READ_MULTIPLE_BLOCK;
lypinator 0:bb348c97df44 552
lypinator 0:bb348c97df44 553 /* Read Multi Block command */
lypinator 0:bb348c97df44 554 errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, BlockAdd);
lypinator 0:bb348c97df44 555 }
lypinator 0:bb348c97df44 556 else
lypinator 0:bb348c97df44 557 {
lypinator 0:bb348c97df44 558 hmmc->Context = MMC_CONTEXT_READ_SINGLE_BLOCK;
lypinator 0:bb348c97df44 559
lypinator 0:bb348c97df44 560 /* Read Single Block command */
lypinator 0:bb348c97df44 561 errorstate = SDMMC_CmdReadSingleBlock(hmmc->Instance, BlockAdd);
lypinator 0:bb348c97df44 562 }
lypinator 0:bb348c97df44 563 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 564 {
lypinator 0:bb348c97df44 565 /* Clear all the static flags */
lypinator 0:bb348c97df44 566 __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
lypinator 0:bb348c97df44 567 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 568 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 569 return HAL_ERROR;
lypinator 0:bb348c97df44 570 }
lypinator 0:bb348c97df44 571
lypinator 0:bb348c97df44 572 /* Poll on SDMMC flags */
lypinator 0:bb348c97df44 573 #ifdef SDIO_STA_STBITERR
lypinator 0:bb348c97df44 574 while(!__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_STA_STBITERR))
lypinator 0:bb348c97df44 575 #else /* SDIO_STA_STBITERR not defined */
lypinator 0:bb348c97df44 576 while(!__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND))
lypinator 0:bb348c97df44 577 #endif /* SDIO_STA_STBITERR */
lypinator 0:bb348c97df44 578 {
lypinator 0:bb348c97df44 579 if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXFIFOHF))
lypinator 0:bb348c97df44 580 {
lypinator 0:bb348c97df44 581 /* Read data from SDMMC Rx FIFO */
lypinator 0:bb348c97df44 582 for(count = 0U; count < 8U; count++)
lypinator 0:bb348c97df44 583 {
lypinator 0:bb348c97df44 584 *(tempbuff + count) = SDIO_ReadFIFO(hmmc->Instance);
lypinator 0:bb348c97df44 585 }
lypinator 0:bb348c97df44 586 tempbuff += 8U;
lypinator 0:bb348c97df44 587 }
lypinator 0:bb348c97df44 588
lypinator 0:bb348c97df44 589 if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout))
lypinator 0:bb348c97df44 590 {
lypinator 0:bb348c97df44 591 /* Clear all the static flags */
lypinator 0:bb348c97df44 592 __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
lypinator 0:bb348c97df44 593 hmmc->ErrorCode |= HAL_MMC_ERROR_TIMEOUT;
lypinator 0:bb348c97df44 594 hmmc->State= HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 595 return HAL_TIMEOUT;
lypinator 0:bb348c97df44 596 }
lypinator 0:bb348c97df44 597 }
lypinator 0:bb348c97df44 598
lypinator 0:bb348c97df44 599 /* Send stop transmission command in case of multiblock read */
lypinator 0:bb348c97df44 600 if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
lypinator 0:bb348c97df44 601 {
lypinator 0:bb348c97df44 602 /* Send stop transmission command */
lypinator 0:bb348c97df44 603 errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
lypinator 0:bb348c97df44 604 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 605 {
lypinator 0:bb348c97df44 606 /* Clear all the static flags */
lypinator 0:bb348c97df44 607 __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
lypinator 0:bb348c97df44 608 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 609 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 610 return HAL_ERROR;
lypinator 0:bb348c97df44 611 }
lypinator 0:bb348c97df44 612 }
lypinator 0:bb348c97df44 613
lypinator 0:bb348c97df44 614 /* Get error state */
lypinator 0:bb348c97df44 615 if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DTIMEOUT))
lypinator 0:bb348c97df44 616 {
lypinator 0:bb348c97df44 617 /* Clear all the static flags */
lypinator 0:bb348c97df44 618 __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
lypinator 0:bb348c97df44 619 hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
lypinator 0:bb348c97df44 620 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 621 return HAL_ERROR;
lypinator 0:bb348c97df44 622 }
lypinator 0:bb348c97df44 623 else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DCRCFAIL))
lypinator 0:bb348c97df44 624 {
lypinator 0:bb348c97df44 625 /* Clear all the static flags */
lypinator 0:bb348c97df44 626 __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
lypinator 0:bb348c97df44 627 hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
lypinator 0:bb348c97df44 628 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 629 return HAL_ERROR;
lypinator 0:bb348c97df44 630 }
lypinator 0:bb348c97df44 631 else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXOVERR))
lypinator 0:bb348c97df44 632 {
lypinator 0:bb348c97df44 633 /* Clear all the static flags */
lypinator 0:bb348c97df44 634 __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
lypinator 0:bb348c97df44 635 hmmc->ErrorCode |= HAL_MMC_ERROR_RX_OVERRUN;
lypinator 0:bb348c97df44 636 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 637 return HAL_ERROR;
lypinator 0:bb348c97df44 638 }
lypinator 0:bb348c97df44 639
lypinator 0:bb348c97df44 640 /* Empty FIFO if there is still any data */
lypinator 0:bb348c97df44 641 while ((__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_RXDAVL)))
lypinator 0:bb348c97df44 642 {
lypinator 0:bb348c97df44 643 *tempbuff = SDIO_ReadFIFO(hmmc->Instance);
lypinator 0:bb348c97df44 644 tempbuff++;
lypinator 0:bb348c97df44 645
lypinator 0:bb348c97df44 646 if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout))
lypinator 0:bb348c97df44 647 {
lypinator 0:bb348c97df44 648 /* Clear all the static flags */
lypinator 0:bb348c97df44 649 __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
lypinator 0:bb348c97df44 650 hmmc->ErrorCode |= HAL_MMC_ERROR_TIMEOUT;
lypinator 0:bb348c97df44 651 hmmc->State= HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 652 return HAL_ERROR;
lypinator 0:bb348c97df44 653 }
lypinator 0:bb348c97df44 654 }
lypinator 0:bb348c97df44 655
lypinator 0:bb348c97df44 656 /* Clear all the static flags */
lypinator 0:bb348c97df44 657 __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
lypinator 0:bb348c97df44 658
lypinator 0:bb348c97df44 659 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 660
lypinator 0:bb348c97df44 661 return HAL_OK;
lypinator 0:bb348c97df44 662 }
lypinator 0:bb348c97df44 663 else
lypinator 0:bb348c97df44 664 {
lypinator 0:bb348c97df44 665 hmmc->ErrorCode |= HAL_MMC_ERROR_BUSY;
lypinator 0:bb348c97df44 666 return HAL_ERROR;
lypinator 0:bb348c97df44 667 }
lypinator 0:bb348c97df44 668 }
lypinator 0:bb348c97df44 669
lypinator 0:bb348c97df44 670 /**
lypinator 0:bb348c97df44 671 * @brief Allows to write block(s) to a specified address in a card. The Data
lypinator 0:bb348c97df44 672 * transfer is managed by polling mode.
lypinator 0:bb348c97df44 673 * @note This API should be followed by a check on the card state through
lypinator 0:bb348c97df44 674 * HAL_MMC_GetCardState().
lypinator 0:bb348c97df44 675 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 676 * @param pData pointer to the buffer that will contain the data to transmit
lypinator 0:bb348c97df44 677 * @param BlockAdd Block Address where data will be written
lypinator 0:bb348c97df44 678 * @param NumberOfBlocks Number of MMC blocks to write
lypinator 0:bb348c97df44 679 * @param Timeout Specify timeout value
lypinator 0:bb348c97df44 680 * @retval HAL status
lypinator 0:bb348c97df44 681 */
lypinator 0:bb348c97df44 682 HAL_StatusTypeDef HAL_MMC_WriteBlocks(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
lypinator 0:bb348c97df44 683 {
lypinator 0:bb348c97df44 684 SDIO_DataInitTypeDef config;
lypinator 0:bb348c97df44 685 uint32_t errorstate = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 686 uint32_t tickstart = HAL_GetTick();
lypinator 0:bb348c97df44 687 uint32_t count = 0U;
lypinator 0:bb348c97df44 688 uint32_t *tempbuff = (uint32_t *)pData;
lypinator 0:bb348c97df44 689
lypinator 0:bb348c97df44 690 if(NULL == pData)
lypinator 0:bb348c97df44 691 {
lypinator 0:bb348c97df44 692 hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
lypinator 0:bb348c97df44 693 return HAL_ERROR;
lypinator 0:bb348c97df44 694 }
lypinator 0:bb348c97df44 695
lypinator 0:bb348c97df44 696 if(hmmc->State == HAL_MMC_STATE_READY)
lypinator 0:bb348c97df44 697 {
lypinator 0:bb348c97df44 698 hmmc->ErrorCode = HAL_DMA_ERROR_NONE;
lypinator 0:bb348c97df44 699
lypinator 0:bb348c97df44 700 if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
lypinator 0:bb348c97df44 701 {
lypinator 0:bb348c97df44 702 hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
lypinator 0:bb348c97df44 703 return HAL_ERROR;
lypinator 0:bb348c97df44 704 }
lypinator 0:bb348c97df44 705
lypinator 0:bb348c97df44 706 hmmc->State = HAL_MMC_STATE_BUSY;
lypinator 0:bb348c97df44 707
lypinator 0:bb348c97df44 708 /* Initialize data control register */
lypinator 0:bb348c97df44 709 hmmc->Instance->DCTRL = 0U;
lypinator 0:bb348c97df44 710
lypinator 0:bb348c97df44 711 /* Check the Card capacity in term of Logical number of blocks */
lypinator 0:bb348c97df44 712 if ((hmmc->MmcCard.LogBlockNbr) < CAPACITY)
lypinator 0:bb348c97df44 713 {
lypinator 0:bb348c97df44 714 BlockAdd *= 512U;
lypinator 0:bb348c97df44 715 }
lypinator 0:bb348c97df44 716
lypinator 0:bb348c97df44 717 /* Set Block Size for Card */
lypinator 0:bb348c97df44 718 errorstate = SDMMC_CmdBlockLength(hmmc->Instance, BLOCKSIZE);
lypinator 0:bb348c97df44 719 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 720 {
lypinator 0:bb348c97df44 721 /* Clear all the static flags */
lypinator 0:bb348c97df44 722 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 723 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 724 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 725 return HAL_ERROR;
lypinator 0:bb348c97df44 726 }
lypinator 0:bb348c97df44 727
lypinator 0:bb348c97df44 728 /* Write Blocks in Polling mode */
lypinator 0:bb348c97df44 729 if(NumberOfBlocks > 1U)
lypinator 0:bb348c97df44 730 {
lypinator 0:bb348c97df44 731 hmmc->Context = MMC_CONTEXT_WRITE_MULTIPLE_BLOCK;
lypinator 0:bb348c97df44 732
lypinator 0:bb348c97df44 733 /* Write Multi Block command */
lypinator 0:bb348c97df44 734 errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, BlockAdd);
lypinator 0:bb348c97df44 735 }
lypinator 0:bb348c97df44 736 else
lypinator 0:bb348c97df44 737 {
lypinator 0:bb348c97df44 738 hmmc->Context = MMC_CONTEXT_WRITE_SINGLE_BLOCK;
lypinator 0:bb348c97df44 739
lypinator 0:bb348c97df44 740 /* Write Single Block command */
lypinator 0:bb348c97df44 741 errorstate = SDMMC_CmdWriteSingleBlock(hmmc->Instance, BlockAdd);
lypinator 0:bb348c97df44 742 }
lypinator 0:bb348c97df44 743 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 744 {
lypinator 0:bb348c97df44 745 /* Clear all the static flags */
lypinator 0:bb348c97df44 746 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 747 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 748 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 749 return HAL_ERROR;
lypinator 0:bb348c97df44 750 }
lypinator 0:bb348c97df44 751
lypinator 0:bb348c97df44 752 /* Configure the MMC DPSM (Data Path State Machine) */
lypinator 0:bb348c97df44 753 config.DataTimeOut = SDMMC_DATATIMEOUT;
lypinator 0:bb348c97df44 754 config.DataLength = NumberOfBlocks * BLOCKSIZE;
lypinator 0:bb348c97df44 755 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
lypinator 0:bb348c97df44 756 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
lypinator 0:bb348c97df44 757 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
lypinator 0:bb348c97df44 758 config.DPSM = SDIO_DPSM_ENABLE;
lypinator 0:bb348c97df44 759 SDIO_ConfigData(hmmc->Instance, &config);
lypinator 0:bb348c97df44 760
lypinator 0:bb348c97df44 761 /* Write block(s) in polling mode */
lypinator 0:bb348c97df44 762 #ifdef SDIO_STA_STBITERR
lypinator 0:bb348c97df44 763 while(!__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
lypinator 0:bb348c97df44 764 #else /* SDIO_STA_STBITERR not defined */
lypinator 0:bb348c97df44 765 while(!__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND))
lypinator 0:bb348c97df44 766 #endif /* SDIO_STA_STBITERR */
lypinator 0:bb348c97df44 767 {
lypinator 0:bb348c97df44 768 if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_TXFIFOHE))
lypinator 0:bb348c97df44 769 {
lypinator 0:bb348c97df44 770 /* Write data to SDIO Tx FIFO */
lypinator 0:bb348c97df44 771 for(count = 0U; count < 8U; count++)
lypinator 0:bb348c97df44 772 {
lypinator 0:bb348c97df44 773 SDIO_WriteFIFO(hmmc->Instance, (tempbuff + count));
lypinator 0:bb348c97df44 774 }
lypinator 0:bb348c97df44 775 tempbuff += 8U;
lypinator 0:bb348c97df44 776 }
lypinator 0:bb348c97df44 777
lypinator 0:bb348c97df44 778 if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout))
lypinator 0:bb348c97df44 779 {
lypinator 0:bb348c97df44 780 /* Clear all the static flags */
lypinator 0:bb348c97df44 781 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 782 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 783 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 784 return HAL_TIMEOUT;
lypinator 0:bb348c97df44 785 }
lypinator 0:bb348c97df44 786 }
lypinator 0:bb348c97df44 787
lypinator 0:bb348c97df44 788 /* Send stop transmission command in case of multiblock write */
lypinator 0:bb348c97df44 789 if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
lypinator 0:bb348c97df44 790 {
lypinator 0:bb348c97df44 791 /* Send stop transmission command */
lypinator 0:bb348c97df44 792 errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
lypinator 0:bb348c97df44 793 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 794 {
lypinator 0:bb348c97df44 795 /* Clear all the static flags */
lypinator 0:bb348c97df44 796 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 797 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 798 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 799 return HAL_ERROR;
lypinator 0:bb348c97df44 800 }
lypinator 0:bb348c97df44 801 }
lypinator 0:bb348c97df44 802
lypinator 0:bb348c97df44 803 /* Get error state */
lypinator 0:bb348c97df44 804 if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DTIMEOUT))
lypinator 0:bb348c97df44 805 {
lypinator 0:bb348c97df44 806 /* Clear all the static flags */
lypinator 0:bb348c97df44 807 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 808 hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
lypinator 0:bb348c97df44 809 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 810 return HAL_ERROR;
lypinator 0:bb348c97df44 811 }
lypinator 0:bb348c97df44 812 else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_DCRCFAIL))
lypinator 0:bb348c97df44 813 {
lypinator 0:bb348c97df44 814 /* Clear all the static flags */
lypinator 0:bb348c97df44 815 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 816 hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
lypinator 0:bb348c97df44 817 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 818 return HAL_ERROR;
lypinator 0:bb348c97df44 819 }
lypinator 0:bb348c97df44 820 else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_FLAG_TXUNDERR))
lypinator 0:bb348c97df44 821 {
lypinator 0:bb348c97df44 822 /* Clear all the static flags */
lypinator 0:bb348c97df44 823 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 824 hmmc->ErrorCode |= HAL_MMC_ERROR_TX_UNDERRUN;
lypinator 0:bb348c97df44 825 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 826 return HAL_ERROR;
lypinator 0:bb348c97df44 827 }
lypinator 0:bb348c97df44 828
lypinator 0:bb348c97df44 829 /* Clear all the static flags */
lypinator 0:bb348c97df44 830 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 831
lypinator 0:bb348c97df44 832 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 833
lypinator 0:bb348c97df44 834 return HAL_OK;
lypinator 0:bb348c97df44 835 }
lypinator 0:bb348c97df44 836 else
lypinator 0:bb348c97df44 837 {
lypinator 0:bb348c97df44 838 hmmc->ErrorCode |= HAL_MMC_ERROR_BUSY;
lypinator 0:bb348c97df44 839 return HAL_ERROR;
lypinator 0:bb348c97df44 840 }
lypinator 0:bb348c97df44 841 }
lypinator 0:bb348c97df44 842
lypinator 0:bb348c97df44 843 /**
lypinator 0:bb348c97df44 844 * @brief Reads block(s) from a specified address in a card. The Data transfer
lypinator 0:bb348c97df44 845 * is managed in interrupt mode.
lypinator 0:bb348c97df44 846 * @note This API should be followed by a check on the card state through
lypinator 0:bb348c97df44 847 * HAL_MMC_GetCardState().
lypinator 0:bb348c97df44 848 * @note You could also check the IT transfer process through the MMC Rx
lypinator 0:bb348c97df44 849 * interrupt event.
lypinator 0:bb348c97df44 850 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 851 * @param pData Pointer to the buffer that will contain the received data
lypinator 0:bb348c97df44 852 * @param BlockAdd Block Address from where data is to be read
lypinator 0:bb348c97df44 853 * @param NumberOfBlocks Number of blocks to read.
lypinator 0:bb348c97df44 854 * @retval HAL status
lypinator 0:bb348c97df44 855 */
lypinator 0:bb348c97df44 856 HAL_StatusTypeDef HAL_MMC_ReadBlocks_IT(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
lypinator 0:bb348c97df44 857 {
lypinator 0:bb348c97df44 858 SDIO_DataInitTypeDef config;
lypinator 0:bb348c97df44 859 uint32_t errorstate = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 860
lypinator 0:bb348c97df44 861 if(NULL == pData)
lypinator 0:bb348c97df44 862 {
lypinator 0:bb348c97df44 863 hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
lypinator 0:bb348c97df44 864 return HAL_ERROR;
lypinator 0:bb348c97df44 865 }
lypinator 0:bb348c97df44 866
lypinator 0:bb348c97df44 867 if(hmmc->State == HAL_MMC_STATE_READY)
lypinator 0:bb348c97df44 868 {
lypinator 0:bb348c97df44 869 hmmc->ErrorCode = HAL_DMA_ERROR_NONE;
lypinator 0:bb348c97df44 870
lypinator 0:bb348c97df44 871 if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
lypinator 0:bb348c97df44 872 {
lypinator 0:bb348c97df44 873 hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
lypinator 0:bb348c97df44 874 return HAL_ERROR;
lypinator 0:bb348c97df44 875 }
lypinator 0:bb348c97df44 876
lypinator 0:bb348c97df44 877 hmmc->State = HAL_MMC_STATE_BUSY;
lypinator 0:bb348c97df44 878
lypinator 0:bb348c97df44 879 /* Initialize data control register */
lypinator 0:bb348c97df44 880 hmmc->Instance->DCTRL = 0U;
lypinator 0:bb348c97df44 881
lypinator 0:bb348c97df44 882 hmmc->pRxBuffPtr = (uint32_t *)pData;
lypinator 0:bb348c97df44 883 hmmc->RxXferSize = BLOCKSIZE * NumberOfBlocks;
lypinator 0:bb348c97df44 884
lypinator 0:bb348c97df44 885 __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF));
lypinator 0:bb348c97df44 886
lypinator 0:bb348c97df44 887 /* Check the Card capacity in term of Logical number of blocks */
lypinator 0:bb348c97df44 888 if ((hmmc->MmcCard.LogBlockNbr) < CAPACITY)
lypinator 0:bb348c97df44 889 {
lypinator 0:bb348c97df44 890 BlockAdd *= 512U;
lypinator 0:bb348c97df44 891 }
lypinator 0:bb348c97df44 892
lypinator 0:bb348c97df44 893 /* Configure the MMC DPSM (Data Path State Machine) */
lypinator 0:bb348c97df44 894 config.DataTimeOut = SDMMC_DATATIMEOUT;
lypinator 0:bb348c97df44 895 config.DataLength = BLOCKSIZE * NumberOfBlocks;
lypinator 0:bb348c97df44 896 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
lypinator 0:bb348c97df44 897 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
lypinator 0:bb348c97df44 898 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
lypinator 0:bb348c97df44 899 config.DPSM = SDIO_DPSM_ENABLE;
lypinator 0:bb348c97df44 900 SDIO_ConfigData(hmmc->Instance, &config);
lypinator 0:bb348c97df44 901
lypinator 0:bb348c97df44 902 /* Set Block Size for Card */
lypinator 0:bb348c97df44 903 errorstate = SDMMC_CmdBlockLength(hmmc->Instance, BLOCKSIZE);
lypinator 0:bb348c97df44 904 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 905 {
lypinator 0:bb348c97df44 906 /* Clear all the static flags */
lypinator 0:bb348c97df44 907 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 908 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 909 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 910 return HAL_ERROR;
lypinator 0:bb348c97df44 911 }
lypinator 0:bb348c97df44 912
lypinator 0:bb348c97df44 913 /* Read Blocks in IT mode */
lypinator 0:bb348c97df44 914 if(NumberOfBlocks > 1U)
lypinator 0:bb348c97df44 915 {
lypinator 0:bb348c97df44 916 hmmc->Context = (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_IT);
lypinator 0:bb348c97df44 917
lypinator 0:bb348c97df44 918 /* Read Multi Block command */
lypinator 0:bb348c97df44 919 errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, BlockAdd);
lypinator 0:bb348c97df44 920 }
lypinator 0:bb348c97df44 921 else
lypinator 0:bb348c97df44 922 {
lypinator 0:bb348c97df44 923 hmmc->Context = (MMC_CONTEXT_READ_SINGLE_BLOCK | MMC_CONTEXT_IT);
lypinator 0:bb348c97df44 924
lypinator 0:bb348c97df44 925 /* Read Single Block command */
lypinator 0:bb348c97df44 926 errorstate = SDMMC_CmdReadSingleBlock(hmmc->Instance, BlockAdd);
lypinator 0:bb348c97df44 927 }
lypinator 0:bb348c97df44 928 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 929 {
lypinator 0:bb348c97df44 930 /* Clear all the static flags */
lypinator 0:bb348c97df44 931 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 932 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 933 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 934 return HAL_ERROR;
lypinator 0:bb348c97df44 935 }
lypinator 0:bb348c97df44 936
lypinator 0:bb348c97df44 937 return HAL_OK;
lypinator 0:bb348c97df44 938 }
lypinator 0:bb348c97df44 939 else
lypinator 0:bb348c97df44 940 {
lypinator 0:bb348c97df44 941 return HAL_BUSY;
lypinator 0:bb348c97df44 942 }
lypinator 0:bb348c97df44 943 }
lypinator 0:bb348c97df44 944
lypinator 0:bb348c97df44 945 /**
lypinator 0:bb348c97df44 946 * @brief Writes block(s) to a specified address in a card. The Data transfer
lypinator 0:bb348c97df44 947 * is managed in interrupt mode.
lypinator 0:bb348c97df44 948 * @note This API should be followed by a check on the card state through
lypinator 0:bb348c97df44 949 * HAL_MMC_GetCardState().
lypinator 0:bb348c97df44 950 * @note You could also check the IT transfer process through the MMC Tx
lypinator 0:bb348c97df44 951 * interrupt event.
lypinator 0:bb348c97df44 952 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 953 * @param pData Pointer to the buffer that will contain the data to transmit
lypinator 0:bb348c97df44 954 * @param BlockAdd Block Address where data will be written
lypinator 0:bb348c97df44 955 * @param NumberOfBlocks Number of blocks to write
lypinator 0:bb348c97df44 956 * @retval HAL status
lypinator 0:bb348c97df44 957 */
lypinator 0:bb348c97df44 958 HAL_StatusTypeDef HAL_MMC_WriteBlocks_IT(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
lypinator 0:bb348c97df44 959 {
lypinator 0:bb348c97df44 960 SDIO_DataInitTypeDef config;
lypinator 0:bb348c97df44 961 uint32_t errorstate = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 962
lypinator 0:bb348c97df44 963 if(NULL == pData)
lypinator 0:bb348c97df44 964 {
lypinator 0:bb348c97df44 965 hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
lypinator 0:bb348c97df44 966 return HAL_ERROR;
lypinator 0:bb348c97df44 967 }
lypinator 0:bb348c97df44 968
lypinator 0:bb348c97df44 969 if(hmmc->State == HAL_MMC_STATE_READY)
lypinator 0:bb348c97df44 970 {
lypinator 0:bb348c97df44 971 hmmc->ErrorCode = HAL_DMA_ERROR_NONE;
lypinator 0:bb348c97df44 972
lypinator 0:bb348c97df44 973 if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
lypinator 0:bb348c97df44 974 {
lypinator 0:bb348c97df44 975 hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
lypinator 0:bb348c97df44 976 return HAL_ERROR;
lypinator 0:bb348c97df44 977 }
lypinator 0:bb348c97df44 978
lypinator 0:bb348c97df44 979 hmmc->State = HAL_MMC_STATE_BUSY;
lypinator 0:bb348c97df44 980
lypinator 0:bb348c97df44 981 /* Initialize data control register */
lypinator 0:bb348c97df44 982 hmmc->Instance->DCTRL = 0U;
lypinator 0:bb348c97df44 983
lypinator 0:bb348c97df44 984 hmmc->pTxBuffPtr = (uint32_t *)pData;
lypinator 0:bb348c97df44 985 hmmc->TxXferSize = BLOCKSIZE * NumberOfBlocks;
lypinator 0:bb348c97df44 986
lypinator 0:bb348c97df44 987 /* Enable transfer interrupts */
lypinator 0:bb348c97df44 988 __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE));
lypinator 0:bb348c97df44 989
lypinator 0:bb348c97df44 990 /* Check the Card capacity in term of Logical number of blocks */
lypinator 0:bb348c97df44 991 if ((hmmc->MmcCard.LogBlockNbr) < CAPACITY)
lypinator 0:bb348c97df44 992 {
lypinator 0:bb348c97df44 993 BlockAdd *= 512U;
lypinator 0:bb348c97df44 994 }
lypinator 0:bb348c97df44 995
lypinator 0:bb348c97df44 996 /* Set Block Size for Card */
lypinator 0:bb348c97df44 997 errorstate = SDMMC_CmdBlockLength(hmmc->Instance, BLOCKSIZE);
lypinator 0:bb348c97df44 998 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 999 {
lypinator 0:bb348c97df44 1000 /* Clear all the static flags */
lypinator 0:bb348c97df44 1001 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 1002 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 1003 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1004 return HAL_ERROR;
lypinator 0:bb348c97df44 1005 }
lypinator 0:bb348c97df44 1006
lypinator 0:bb348c97df44 1007 /* Write Blocks in Polling mode */
lypinator 0:bb348c97df44 1008 if(NumberOfBlocks > 1U)
lypinator 0:bb348c97df44 1009 {
lypinator 0:bb348c97df44 1010 hmmc->Context = (MMC_CONTEXT_WRITE_MULTIPLE_BLOCK| MMC_CONTEXT_IT);
lypinator 0:bb348c97df44 1011
lypinator 0:bb348c97df44 1012 /* Write Multi Block command */
lypinator 0:bb348c97df44 1013 errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, BlockAdd);
lypinator 0:bb348c97df44 1014 }
lypinator 0:bb348c97df44 1015 else
lypinator 0:bb348c97df44 1016 {
lypinator 0:bb348c97df44 1017 hmmc->Context = (MMC_CONTEXT_WRITE_SINGLE_BLOCK | MMC_CONTEXT_IT);
lypinator 0:bb348c97df44 1018
lypinator 0:bb348c97df44 1019 /* Write Single Block command */
lypinator 0:bb348c97df44 1020 errorstate = SDMMC_CmdWriteSingleBlock(hmmc->Instance, BlockAdd);
lypinator 0:bb348c97df44 1021 }
lypinator 0:bb348c97df44 1022 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 1023 {
lypinator 0:bb348c97df44 1024 /* Clear all the static flags */
lypinator 0:bb348c97df44 1025 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 1026 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 1027 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1028 return HAL_ERROR;
lypinator 0:bb348c97df44 1029 }
lypinator 0:bb348c97df44 1030
lypinator 0:bb348c97df44 1031 /* Configure the MMC DPSM (Data Path State Machine) */
lypinator 0:bb348c97df44 1032 config.DataTimeOut = SDMMC_DATATIMEOUT;
lypinator 0:bb348c97df44 1033 config.DataLength = BLOCKSIZE * NumberOfBlocks;
lypinator 0:bb348c97df44 1034 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
lypinator 0:bb348c97df44 1035 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
lypinator 0:bb348c97df44 1036 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
lypinator 0:bb348c97df44 1037 config.DPSM = SDIO_DPSM_ENABLE;
lypinator 0:bb348c97df44 1038 SDIO_ConfigData(hmmc->Instance, &config);
lypinator 0:bb348c97df44 1039
lypinator 0:bb348c97df44 1040 return HAL_OK;
lypinator 0:bb348c97df44 1041 }
lypinator 0:bb348c97df44 1042 else
lypinator 0:bb348c97df44 1043 {
lypinator 0:bb348c97df44 1044 return HAL_BUSY;
lypinator 0:bb348c97df44 1045 }
lypinator 0:bb348c97df44 1046 }
lypinator 0:bb348c97df44 1047
lypinator 0:bb348c97df44 1048 /**
lypinator 0:bb348c97df44 1049 * @brief Reads block(s) from a specified address in a card. The Data transfer
lypinator 0:bb348c97df44 1050 * is managed by DMA mode.
lypinator 0:bb348c97df44 1051 * @note This API should be followed by a check on the card state through
lypinator 0:bb348c97df44 1052 * HAL_MMC_GetCardState().
lypinator 0:bb348c97df44 1053 * @note You could also check the DMA transfer process through the MMC Rx
lypinator 0:bb348c97df44 1054 * interrupt event.
lypinator 0:bb348c97df44 1055 * @param hmmc Pointer MMC handle
lypinator 0:bb348c97df44 1056 * @param pData Pointer to the buffer that will contain the received data
lypinator 0:bb348c97df44 1057 * @param BlockAdd Block Address from where data is to be read
lypinator 0:bb348c97df44 1058 * @param NumberOfBlocks Number of blocks to read.
lypinator 0:bb348c97df44 1059 * @retval HAL status
lypinator 0:bb348c97df44 1060 */
lypinator 0:bb348c97df44 1061 HAL_StatusTypeDef HAL_MMC_ReadBlocks_DMA(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
lypinator 0:bb348c97df44 1062 {
lypinator 0:bb348c97df44 1063 SDIO_DataInitTypeDef config;
lypinator 0:bb348c97df44 1064 uint32_t errorstate = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 1065
lypinator 0:bb348c97df44 1066 if(NULL == pData)
lypinator 0:bb348c97df44 1067 {
lypinator 0:bb348c97df44 1068 hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
lypinator 0:bb348c97df44 1069 return HAL_ERROR;
lypinator 0:bb348c97df44 1070 }
lypinator 0:bb348c97df44 1071
lypinator 0:bb348c97df44 1072 if(hmmc->State == HAL_MMC_STATE_READY)
lypinator 0:bb348c97df44 1073 {
lypinator 0:bb348c97df44 1074 hmmc->ErrorCode = HAL_DMA_ERROR_NONE;
lypinator 0:bb348c97df44 1075
lypinator 0:bb348c97df44 1076 if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
lypinator 0:bb348c97df44 1077 {
lypinator 0:bb348c97df44 1078 hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
lypinator 0:bb348c97df44 1079 return HAL_ERROR;
lypinator 0:bb348c97df44 1080 }
lypinator 0:bb348c97df44 1081
lypinator 0:bb348c97df44 1082 hmmc->State = HAL_MMC_STATE_BUSY;
lypinator 0:bb348c97df44 1083
lypinator 0:bb348c97df44 1084 /* Initialize data control register */
lypinator 0:bb348c97df44 1085 hmmc->Instance->DCTRL = 0U;
lypinator 0:bb348c97df44 1086
lypinator 0:bb348c97df44 1087 #ifdef SDIO_STA_STBITER
lypinator 0:bb348c97df44 1088 __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_IT_STBITERR));
lypinator 0:bb348c97df44 1089 #else /* SDIO_STA_STBITERR not defined */
lypinator 0:bb348c97df44 1090 __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND));
lypinator 0:bb348c97df44 1091 #endif /* SDIO_STA_STBITERR */
lypinator 0:bb348c97df44 1092
lypinator 0:bb348c97df44 1093 /* Set the DMA transfer complete callback */
lypinator 0:bb348c97df44 1094 hmmc->hdmarx->XferCpltCallback = MMC_DMAReceiveCplt;
lypinator 0:bb348c97df44 1095
lypinator 0:bb348c97df44 1096 /* Set the DMA error callback */
lypinator 0:bb348c97df44 1097 hmmc->hdmarx->XferErrorCallback = MMC_DMAError;
lypinator 0:bb348c97df44 1098
lypinator 0:bb348c97df44 1099 /* Set the DMA Abort callback */
lypinator 0:bb348c97df44 1100 hmmc->hdmarx->XferAbortCallback = NULL;
lypinator 0:bb348c97df44 1101
lypinator 0:bb348c97df44 1102 /* Enable the DMA Channel */
lypinator 0:bb348c97df44 1103 HAL_DMA_Start_IT(hmmc->hdmarx, (uint32_t)&hmmc->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4);
lypinator 0:bb348c97df44 1104
lypinator 0:bb348c97df44 1105 /* Enable MMC DMA transfer */
lypinator 0:bb348c97df44 1106 __HAL_MMC_DMA_ENABLE(hmmc);
lypinator 0:bb348c97df44 1107
lypinator 0:bb348c97df44 1108 /* Check the Card capacity in term of Logical number of blocks */
lypinator 0:bb348c97df44 1109 if ((hmmc->MmcCard.LogBlockNbr) < CAPACITY)
lypinator 0:bb348c97df44 1110 {
lypinator 0:bb348c97df44 1111 BlockAdd *= 512U;
lypinator 0:bb348c97df44 1112 }
lypinator 0:bb348c97df44 1113
lypinator 0:bb348c97df44 1114 /* Configure the MMC DPSM (Data Path State Machine) */
lypinator 0:bb348c97df44 1115 config.DataTimeOut = SDMMC_DATATIMEOUT;
lypinator 0:bb348c97df44 1116 config.DataLength = BLOCKSIZE * NumberOfBlocks;
lypinator 0:bb348c97df44 1117 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
lypinator 0:bb348c97df44 1118 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
lypinator 0:bb348c97df44 1119 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
lypinator 0:bb348c97df44 1120 config.DPSM = SDIO_DPSM_ENABLE;
lypinator 0:bb348c97df44 1121 SDIO_ConfigData(hmmc->Instance, &config);
lypinator 0:bb348c97df44 1122
lypinator 0:bb348c97df44 1123 /* Set Block Size for Card */
lypinator 0:bb348c97df44 1124 errorstate = SDMMC_CmdBlockLength(hmmc->Instance, BLOCKSIZE);
lypinator 0:bb348c97df44 1125 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 1126 {
lypinator 0:bb348c97df44 1127 /* Clear all the static flags */
lypinator 0:bb348c97df44 1128 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 1129 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 1130 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1131 return HAL_ERROR;
lypinator 0:bb348c97df44 1132 }
lypinator 0:bb348c97df44 1133
lypinator 0:bb348c97df44 1134 /* Read Blocks in DMA mode */
lypinator 0:bb348c97df44 1135 if(NumberOfBlocks > 1U)
lypinator 0:bb348c97df44 1136 {
lypinator 0:bb348c97df44 1137 hmmc->Context = (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
lypinator 0:bb348c97df44 1138
lypinator 0:bb348c97df44 1139 /* Read Multi Block command */
lypinator 0:bb348c97df44 1140 errorstate = SDMMC_CmdReadMultiBlock(hmmc->Instance, BlockAdd);
lypinator 0:bb348c97df44 1141 }
lypinator 0:bb348c97df44 1142 else
lypinator 0:bb348c97df44 1143 {
lypinator 0:bb348c97df44 1144 hmmc->Context = (MMC_CONTEXT_READ_SINGLE_BLOCK | MMC_CONTEXT_DMA);
lypinator 0:bb348c97df44 1145
lypinator 0:bb348c97df44 1146 /* Read Single Block command */
lypinator 0:bb348c97df44 1147 errorstate = SDMMC_CmdReadSingleBlock(hmmc->Instance, BlockAdd);
lypinator 0:bb348c97df44 1148 }
lypinator 0:bb348c97df44 1149 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 1150 {
lypinator 0:bb348c97df44 1151 /* Clear all the static flags */
lypinator 0:bb348c97df44 1152 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 1153 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 1154 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1155 return HAL_ERROR;
lypinator 0:bb348c97df44 1156 }
lypinator 0:bb348c97df44 1157
lypinator 0:bb348c97df44 1158 return HAL_OK;
lypinator 0:bb348c97df44 1159 }
lypinator 0:bb348c97df44 1160 else
lypinator 0:bb348c97df44 1161 {
lypinator 0:bb348c97df44 1162 return HAL_BUSY;
lypinator 0:bb348c97df44 1163 }
lypinator 0:bb348c97df44 1164 }
lypinator 0:bb348c97df44 1165
lypinator 0:bb348c97df44 1166 /**
lypinator 0:bb348c97df44 1167 * @brief Writes block(s) to a specified address in a card. The Data transfer
lypinator 0:bb348c97df44 1168 * is managed by DMA mode.
lypinator 0:bb348c97df44 1169 * @note This API should be followed by a check on the card state through
lypinator 0:bb348c97df44 1170 * HAL_MMC_GetCardState().
lypinator 0:bb348c97df44 1171 * @note You could also check the DMA transfer process through the MMC Tx
lypinator 0:bb348c97df44 1172 * interrupt event.
lypinator 0:bb348c97df44 1173 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 1174 * @param pData Pointer to the buffer that will contain the data to transmit
lypinator 0:bb348c97df44 1175 * @param BlockAdd Block Address where data will be written
lypinator 0:bb348c97df44 1176 * @param NumberOfBlocks Number of blocks to write
lypinator 0:bb348c97df44 1177 * @retval HAL status
lypinator 0:bb348c97df44 1178 */
lypinator 0:bb348c97df44 1179 HAL_StatusTypeDef HAL_MMC_WriteBlocks_DMA(MMC_HandleTypeDef *hmmc, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
lypinator 0:bb348c97df44 1180 {
lypinator 0:bb348c97df44 1181 SDIO_DataInitTypeDef config;
lypinator 0:bb348c97df44 1182 uint32_t errorstate = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 1183
lypinator 0:bb348c97df44 1184 if(NULL == pData)
lypinator 0:bb348c97df44 1185 {
lypinator 0:bb348c97df44 1186 hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
lypinator 0:bb348c97df44 1187 return HAL_ERROR;
lypinator 0:bb348c97df44 1188 }
lypinator 0:bb348c97df44 1189
lypinator 0:bb348c97df44 1190 if(hmmc->State == HAL_MMC_STATE_READY)
lypinator 0:bb348c97df44 1191 {
lypinator 0:bb348c97df44 1192 hmmc->ErrorCode = HAL_DMA_ERROR_NONE;
lypinator 0:bb348c97df44 1193
lypinator 0:bb348c97df44 1194 if((BlockAdd + NumberOfBlocks) > (hmmc->MmcCard.LogBlockNbr))
lypinator 0:bb348c97df44 1195 {
lypinator 0:bb348c97df44 1196 hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
lypinator 0:bb348c97df44 1197 return HAL_ERROR;
lypinator 0:bb348c97df44 1198 }
lypinator 0:bb348c97df44 1199
lypinator 0:bb348c97df44 1200 hmmc->State = HAL_MMC_STATE_BUSY;
lypinator 0:bb348c97df44 1201
lypinator 0:bb348c97df44 1202 /* Initialize data control register */
lypinator 0:bb348c97df44 1203 hmmc->Instance->DCTRL = 0U;
lypinator 0:bb348c97df44 1204
lypinator 0:bb348c97df44 1205 /* Enable MMC Error interrupts */
lypinator 0:bb348c97df44 1206 #ifdef SDIO_STA_STBITER
lypinator 0:bb348c97df44 1207 __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR));
lypinator 0:bb348c97df44 1208 #else /* SDIO_STA_STBITERR not defined */
lypinator 0:bb348c97df44 1209 __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR));
lypinator 0:bb348c97df44 1210 #endif /* SDIO_STA_STBITERR */
lypinator 0:bb348c97df44 1211
lypinator 0:bb348c97df44 1212 /* Set the DMA transfer complete callback */
lypinator 0:bb348c97df44 1213 hmmc->hdmatx->XferCpltCallback = MMC_DMATransmitCplt;
lypinator 0:bb348c97df44 1214
lypinator 0:bb348c97df44 1215 /* Set the DMA error callback */
lypinator 0:bb348c97df44 1216 hmmc->hdmatx->XferErrorCallback = MMC_DMAError;
lypinator 0:bb348c97df44 1217
lypinator 0:bb348c97df44 1218 /* Set the DMA Abort callback */
lypinator 0:bb348c97df44 1219 hmmc->hdmatx->XferAbortCallback = NULL;
lypinator 0:bb348c97df44 1220
lypinator 0:bb348c97df44 1221 /* Check the Card capacity in term of Logical number of blocks */
lypinator 0:bb348c97df44 1222 if ((hmmc->MmcCard.LogBlockNbr) < CAPACITY)
lypinator 0:bb348c97df44 1223 {
lypinator 0:bb348c97df44 1224 BlockAdd *= 512U;
lypinator 0:bb348c97df44 1225 }
lypinator 0:bb348c97df44 1226
lypinator 0:bb348c97df44 1227 /* Set Block Size for Card */
lypinator 0:bb348c97df44 1228 errorstate = SDMMC_CmdBlockLength(hmmc->Instance, BLOCKSIZE);
lypinator 0:bb348c97df44 1229 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 1230 {
lypinator 0:bb348c97df44 1231 /* Clear all the static flags */
lypinator 0:bb348c97df44 1232 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 1233 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 1234 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1235 return HAL_ERROR;
lypinator 0:bb348c97df44 1236 }
lypinator 0:bb348c97df44 1237
lypinator 0:bb348c97df44 1238 /* Write Blocks in Polling mode */
lypinator 0:bb348c97df44 1239 if(NumberOfBlocks > 1U)
lypinator 0:bb348c97df44 1240 {
lypinator 0:bb348c97df44 1241 hmmc->Context = (MMC_CONTEXT_WRITE_MULTIPLE_BLOCK | MMC_CONTEXT_DMA);
lypinator 0:bb348c97df44 1242
lypinator 0:bb348c97df44 1243 /* Write Multi Block command */
lypinator 0:bb348c97df44 1244 errorstate = SDMMC_CmdWriteMultiBlock(hmmc->Instance, BlockAdd);
lypinator 0:bb348c97df44 1245 }
lypinator 0:bb348c97df44 1246 else
lypinator 0:bb348c97df44 1247 {
lypinator 0:bb348c97df44 1248 hmmc->Context = (MMC_CONTEXT_WRITE_SINGLE_BLOCK | MMC_CONTEXT_DMA);
lypinator 0:bb348c97df44 1249
lypinator 0:bb348c97df44 1250 /* Write Single Block command */
lypinator 0:bb348c97df44 1251 errorstate = SDMMC_CmdWriteSingleBlock(hmmc->Instance, BlockAdd);
lypinator 0:bb348c97df44 1252 }
lypinator 0:bb348c97df44 1253 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 1254 {
lypinator 0:bb348c97df44 1255 /* Clear all the static flags */
lypinator 0:bb348c97df44 1256 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 1257 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 1258 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1259 return HAL_ERROR;
lypinator 0:bb348c97df44 1260 }
lypinator 0:bb348c97df44 1261
lypinator 0:bb348c97df44 1262 /* Enable SDIO DMA transfer */
lypinator 0:bb348c97df44 1263 __HAL_MMC_DMA_ENABLE(hmmc);
lypinator 0:bb348c97df44 1264
lypinator 0:bb348c97df44 1265 /* Enable the DMA Channel */
lypinator 0:bb348c97df44 1266 HAL_DMA_Start_IT(hmmc->hdmatx, (uint32_t)pData, (uint32_t)&hmmc->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4);
lypinator 0:bb348c97df44 1267
lypinator 0:bb348c97df44 1268 /* Configure the MMC DPSM (Data Path State Machine) */
lypinator 0:bb348c97df44 1269 config.DataTimeOut = SDMMC_DATATIMEOUT;
lypinator 0:bb348c97df44 1270 config.DataLength = BLOCKSIZE * NumberOfBlocks;
lypinator 0:bb348c97df44 1271 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
lypinator 0:bb348c97df44 1272 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
lypinator 0:bb348c97df44 1273 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
lypinator 0:bb348c97df44 1274 config.DPSM = SDIO_DPSM_ENABLE;
lypinator 0:bb348c97df44 1275 SDIO_ConfigData(hmmc->Instance, &config);
lypinator 0:bb348c97df44 1276
lypinator 0:bb348c97df44 1277 return HAL_OK;
lypinator 0:bb348c97df44 1278 }
lypinator 0:bb348c97df44 1279 else
lypinator 0:bb348c97df44 1280 {
lypinator 0:bb348c97df44 1281 return HAL_BUSY;
lypinator 0:bb348c97df44 1282 }
lypinator 0:bb348c97df44 1283 }
lypinator 0:bb348c97df44 1284
lypinator 0:bb348c97df44 1285 /**
lypinator 0:bb348c97df44 1286 * @brief Erases the specified memory area of the given MMC card.
lypinator 0:bb348c97df44 1287 * @note This API should be followed by a check on the card state through
lypinator 0:bb348c97df44 1288 * HAL_MMC_GetCardState().
lypinator 0:bb348c97df44 1289 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 1290 * @param BlockStartAdd Start Block address
lypinator 0:bb348c97df44 1291 * @param BlockEndAdd End Block address
lypinator 0:bb348c97df44 1292 * @retval HAL status
lypinator 0:bb348c97df44 1293 */
lypinator 0:bb348c97df44 1294 HAL_StatusTypeDef HAL_MMC_Erase(MMC_HandleTypeDef *hmmc, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
lypinator 0:bb348c97df44 1295 {
lypinator 0:bb348c97df44 1296 uint32_t errorstate = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 1297
lypinator 0:bb348c97df44 1298 if(hmmc->State == HAL_MMC_STATE_READY)
lypinator 0:bb348c97df44 1299 {
lypinator 0:bb348c97df44 1300 hmmc->ErrorCode = HAL_DMA_ERROR_NONE;
lypinator 0:bb348c97df44 1301
lypinator 0:bb348c97df44 1302 if(BlockEndAdd < BlockStartAdd)
lypinator 0:bb348c97df44 1303 {
lypinator 0:bb348c97df44 1304 hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
lypinator 0:bb348c97df44 1305 return HAL_ERROR;
lypinator 0:bb348c97df44 1306 }
lypinator 0:bb348c97df44 1307
lypinator 0:bb348c97df44 1308 if(BlockEndAdd > (hmmc->MmcCard.LogBlockNbr))
lypinator 0:bb348c97df44 1309 {
lypinator 0:bb348c97df44 1310 hmmc->ErrorCode |= HAL_MMC_ERROR_ADDR_OUT_OF_RANGE;
lypinator 0:bb348c97df44 1311 return HAL_ERROR;
lypinator 0:bb348c97df44 1312 }
lypinator 0:bb348c97df44 1313
lypinator 0:bb348c97df44 1314 hmmc->State = HAL_MMC_STATE_BUSY;
lypinator 0:bb348c97df44 1315
lypinator 0:bb348c97df44 1316 /* Check if the card command class supports erase command */
lypinator 0:bb348c97df44 1317 if(((hmmc->MmcCard.Class) & SDIO_CCCC_ERASE) == 0U)
lypinator 0:bb348c97df44 1318 {
lypinator 0:bb348c97df44 1319 /* Clear all the static flags */
lypinator 0:bb348c97df44 1320 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 1321 hmmc->ErrorCode |= HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
lypinator 0:bb348c97df44 1322 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1323 return HAL_ERROR;
lypinator 0:bb348c97df44 1324 }
lypinator 0:bb348c97df44 1325
lypinator 0:bb348c97df44 1326 if((SDIO_GetResponse(hmmc->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
lypinator 0:bb348c97df44 1327 {
lypinator 0:bb348c97df44 1328 /* Clear all the static flags */
lypinator 0:bb348c97df44 1329 __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
lypinator 0:bb348c97df44 1330 hmmc->ErrorCode |= HAL_MMC_ERROR_LOCK_UNLOCK_FAILED;
lypinator 0:bb348c97df44 1331 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1332 return HAL_ERROR;
lypinator 0:bb348c97df44 1333 }
lypinator 0:bb348c97df44 1334
lypinator 0:bb348c97df44 1335 /* Check the Card capacity in term of Logical number of blocks */
lypinator 0:bb348c97df44 1336 if ((hmmc->MmcCard.LogBlockNbr) < CAPACITY)
lypinator 0:bb348c97df44 1337 {
lypinator 0:bb348c97df44 1338 BlockStartAdd *= 512U;
lypinator 0:bb348c97df44 1339 BlockEndAdd *= 512U;
lypinator 0:bb348c97df44 1340 }
lypinator 0:bb348c97df44 1341
lypinator 0:bb348c97df44 1342 /* Send CMD35 MMC_ERASE_GRP_START with argument as addr */
lypinator 0:bb348c97df44 1343 errorstate = SDMMC_CmdEraseStartAdd(hmmc->Instance, BlockStartAdd);
lypinator 0:bb348c97df44 1344 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 1345 {
lypinator 0:bb348c97df44 1346 /* Clear all the static flags */
lypinator 0:bb348c97df44 1347 __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
lypinator 0:bb348c97df44 1348 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 1349 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1350 return HAL_ERROR;
lypinator 0:bb348c97df44 1351 }
lypinator 0:bb348c97df44 1352
lypinator 0:bb348c97df44 1353 /* Send CMD36 MMC_ERASE_GRP_END with argument as addr */
lypinator 0:bb348c97df44 1354 errorstate = SDMMC_CmdEraseEndAdd(hmmc->Instance, BlockEndAdd);
lypinator 0:bb348c97df44 1355 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 1356 {
lypinator 0:bb348c97df44 1357 /* Clear all the static flags */
lypinator 0:bb348c97df44 1358 __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
lypinator 0:bb348c97df44 1359 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 1360 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1361 return HAL_ERROR;
lypinator 0:bb348c97df44 1362 }
lypinator 0:bb348c97df44 1363
lypinator 0:bb348c97df44 1364 /* Send CMD38 ERASE */
lypinator 0:bb348c97df44 1365 errorstate = SDMMC_CmdErase(hmmc->Instance);
lypinator 0:bb348c97df44 1366 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 1367 {
lypinator 0:bb348c97df44 1368 /* Clear all the static flags */
lypinator 0:bb348c97df44 1369 __HAL_MMC_CLEAR_FLAG(hmmc, SDMMC_STATIC_FLAGS);
lypinator 0:bb348c97df44 1370 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 1371 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1372 return HAL_ERROR;
lypinator 0:bb348c97df44 1373 }
lypinator 0:bb348c97df44 1374
lypinator 0:bb348c97df44 1375 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1376
lypinator 0:bb348c97df44 1377 return HAL_OK;
lypinator 0:bb348c97df44 1378 }
lypinator 0:bb348c97df44 1379 else
lypinator 0:bb348c97df44 1380 {
lypinator 0:bb348c97df44 1381 return HAL_BUSY;
lypinator 0:bb348c97df44 1382 }
lypinator 0:bb348c97df44 1383 }
lypinator 0:bb348c97df44 1384
lypinator 0:bb348c97df44 1385 /**
lypinator 0:bb348c97df44 1386 * @brief This function handles MMC card interrupt request.
lypinator 0:bb348c97df44 1387 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 1388 * @retval None
lypinator 0:bb348c97df44 1389 */
lypinator 0:bb348c97df44 1390 void HAL_MMC_IRQHandler(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 1391 {
lypinator 0:bb348c97df44 1392 uint32_t errorstate = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 1393
lypinator 0:bb348c97df44 1394 /* Check for SDIO interrupt flags */
lypinator 0:bb348c97df44 1395 if(__HAL_MMC_GET_FLAG(hmmc, SDIO_IT_DATAEND) != RESET)
lypinator 0:bb348c97df44 1396 {
lypinator 0:bb348c97df44 1397 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_FLAG_DATAEND);
lypinator 0:bb348c97df44 1398
lypinator 0:bb348c97df44 1399 #ifdef SDIO_STA_STBITERR
lypinator 0:bb348c97df44 1400 __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
lypinator 0:bb348c97df44 1401 SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR | SDIO_IT_STBITERR);
lypinator 0:bb348c97df44 1402 #else /* SDIO_STA_STBITERR not defined */
lypinator 0:bb348c97df44 1403 __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
lypinator 0:bb348c97df44 1404 SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR);
lypinator 0:bb348c97df44 1405 #endif
lypinator 0:bb348c97df44 1406
lypinator 0:bb348c97df44 1407 if((hmmc->Context & MMC_CONTEXT_IT) != RESET)
lypinator 0:bb348c97df44 1408 {
lypinator 0:bb348c97df44 1409 if(((hmmc->Context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != RESET) || ((hmmc->Context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET))
lypinator 0:bb348c97df44 1410 {
lypinator 0:bb348c97df44 1411 errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
lypinator 0:bb348c97df44 1412 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 1413 {
lypinator 0:bb348c97df44 1414 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 1415 HAL_MMC_ErrorCallback(hmmc);
lypinator 0:bb348c97df44 1416 }
lypinator 0:bb348c97df44 1417 }
lypinator 0:bb348c97df44 1418
lypinator 0:bb348c97df44 1419 /* Clear all the static flags */
lypinator 0:bb348c97df44 1420 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 1421
lypinator 0:bb348c97df44 1422 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1423 if(((hmmc->Context & MMC_CONTEXT_READ_SINGLE_BLOCK) != RESET) || ((hmmc->Context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) != RESET))
lypinator 0:bb348c97df44 1424 {
lypinator 0:bb348c97df44 1425 HAL_MMC_RxCpltCallback(hmmc);
lypinator 0:bb348c97df44 1426 }
lypinator 0:bb348c97df44 1427 else
lypinator 0:bb348c97df44 1428 {
lypinator 0:bb348c97df44 1429 HAL_MMC_TxCpltCallback(hmmc);
lypinator 0:bb348c97df44 1430 }
lypinator 0:bb348c97df44 1431 }
lypinator 0:bb348c97df44 1432 else if((hmmc->Context & MMC_CONTEXT_DMA) != RESET)
lypinator 0:bb348c97df44 1433 {
lypinator 0:bb348c97df44 1434 if((hmmc->Context & MMC_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET)
lypinator 0:bb348c97df44 1435 {
lypinator 0:bb348c97df44 1436 errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
lypinator 0:bb348c97df44 1437 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 1438 {
lypinator 0:bb348c97df44 1439 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 1440 HAL_MMC_ErrorCallback(hmmc);
lypinator 0:bb348c97df44 1441 }
lypinator 0:bb348c97df44 1442 }
lypinator 0:bb348c97df44 1443 if(((hmmc->Context & MMC_CONTEXT_READ_SINGLE_BLOCK) == RESET) && ((hmmc->Context & MMC_CONTEXT_READ_MULTIPLE_BLOCK) == RESET))
lypinator 0:bb348c97df44 1444 {
lypinator 0:bb348c97df44 1445 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
lypinator 0:bb348c97df44 1446 in the MMC DCTRL register */
lypinator 0:bb348c97df44 1447 hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
lypinator 0:bb348c97df44 1448
lypinator 0:bb348c97df44 1449 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1450
lypinator 0:bb348c97df44 1451 HAL_MMC_TxCpltCallback(hmmc);
lypinator 0:bb348c97df44 1452 }
lypinator 0:bb348c97df44 1453 }
lypinator 0:bb348c97df44 1454 }
lypinator 0:bb348c97df44 1455
lypinator 0:bb348c97df44 1456 else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_IT_TXFIFOHE) != RESET)
lypinator 0:bb348c97df44 1457 {
lypinator 0:bb348c97df44 1458 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_FLAG_TXFIFOHE);
lypinator 0:bb348c97df44 1459
lypinator 0:bb348c97df44 1460 MMC_Write_IT(hmmc);
lypinator 0:bb348c97df44 1461 }
lypinator 0:bb348c97df44 1462
lypinator 0:bb348c97df44 1463 else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_IT_RXFIFOHF) != RESET)
lypinator 0:bb348c97df44 1464 {
lypinator 0:bb348c97df44 1465 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_FLAG_RXFIFOHF);
lypinator 0:bb348c97df44 1466
lypinator 0:bb348c97df44 1467 MMC_Read_IT(hmmc);
lypinator 0:bb348c97df44 1468 }
lypinator 0:bb348c97df44 1469
lypinator 0:bb348c97df44 1470 #ifdef SDIO_STA_STBITERR
lypinator 0:bb348c97df44 1471 else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR) != RESET)
lypinator 0:bb348c97df44 1472 {
lypinator 0:bb348c97df44 1473 /* Set Error code */
lypinator 0:bb348c97df44 1474 if(__HAL_MMC_GET_FLAG(hmmc, SDIO_IT_DCRCFAIL) != RESET)
lypinator 0:bb348c97df44 1475 {
lypinator 0:bb348c97df44 1476 hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
lypinator 0:bb348c97df44 1477 }
lypinator 0:bb348c97df44 1478 if(__HAL_MMC_GET_FLAG(hmmc, SDIO_IT_DTIMEOUT) != RESET)
lypinator 0:bb348c97df44 1479 {
lypinator 0:bb348c97df44 1480 hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
lypinator 0:bb348c97df44 1481 }
lypinator 0:bb348c97df44 1482 if(__HAL_MMC_GET_FLAG(hmmc, SDIO_IT_RXOVERR) != RESET)
lypinator 0:bb348c97df44 1483 {
lypinator 0:bb348c97df44 1484 hmmc->ErrorCode |= HAL_MMC_ERROR_RX_OVERRUN;
lypinator 0:bb348c97df44 1485 }
lypinator 0:bb348c97df44 1486 if(__HAL_MMC_GET_FLAG(hmmc, SDIO_IT_TXUNDERR) != RESET)
lypinator 0:bb348c97df44 1487 {
lypinator 0:bb348c97df44 1488 hmmc->ErrorCode |= HAL_MMC_ERROR_TX_UNDERRUN;
lypinator 0:bb348c97df44 1489 }
lypinator 0:bb348c97df44 1490 if(__HAL_MMC_GET_FLAG(hmmc, SDIO_IT_STBITERR) != RESET)
lypinator 0:bb348c97df44 1491 {
lypinator 0:bb348c97df44 1492 hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
lypinator 0:bb348c97df44 1493 }
lypinator 0:bb348c97df44 1494
lypinator 0:bb348c97df44 1495 /* Clear All flags */
lypinator 0:bb348c97df44 1496 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS | SDIO_FLAG_STBITERR);
lypinator 0:bb348c97df44 1497
lypinator 0:bb348c97df44 1498 /* Disable all interrupts */
lypinator 0:bb348c97df44 1499 __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
lypinator 0:bb348c97df44 1500 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR |SDIO_IT_STBITERR);
lypinator 0:bb348c97df44 1501
lypinator 0:bb348c97df44 1502 if((hmmc->Context & MMC_CONTEXT_DMA) != RESET)
lypinator 0:bb348c97df44 1503 {
lypinator 0:bb348c97df44 1504 /* Abort the MMC DMA Streams */
lypinator 0:bb348c97df44 1505 if(hmmc->hdmatx != NULL)
lypinator 0:bb348c97df44 1506 {
lypinator 0:bb348c97df44 1507 /* Set the DMA Tx abort callback */
lypinator 0:bb348c97df44 1508 hmmc->hdmatx->XferAbortCallback = MMC_DMATxAbort;
lypinator 0:bb348c97df44 1509 /* Abort DMA in IT mode */
lypinator 0:bb348c97df44 1510 if(HAL_DMA_Abort_IT(hmmc->hdmatx) != HAL_OK)
lypinator 0:bb348c97df44 1511 {
lypinator 0:bb348c97df44 1512 MMC_DMATxAbort(hmmc->hdmatx);
lypinator 0:bb348c97df44 1513 }
lypinator 0:bb348c97df44 1514 }
lypinator 0:bb348c97df44 1515 else if(hmmc->hdmarx != NULL)
lypinator 0:bb348c97df44 1516 {
lypinator 0:bb348c97df44 1517 /* Set the DMA Rx abort callback */
lypinator 0:bb348c97df44 1518 hmmc->hdmarx->XferAbortCallback = MMC_DMARxAbort;
lypinator 0:bb348c97df44 1519 /* Abort DMA in IT mode */
lypinator 0:bb348c97df44 1520 if(HAL_DMA_Abort_IT(hmmc->hdmarx) != HAL_OK)
lypinator 0:bb348c97df44 1521 {
lypinator 0:bb348c97df44 1522 MMC_DMARxAbort(hmmc->hdmarx);
lypinator 0:bb348c97df44 1523 }
lypinator 0:bb348c97df44 1524 }
lypinator 0:bb348c97df44 1525 else
lypinator 0:bb348c97df44 1526 {
lypinator 0:bb348c97df44 1527 hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 1528 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1529 HAL_MMC_AbortCallback(hmmc);
lypinator 0:bb348c97df44 1530 }
lypinator 0:bb348c97df44 1531 }
lypinator 0:bb348c97df44 1532 else if((hmmc->Context & MMC_CONTEXT_IT) != RESET)
lypinator 0:bb348c97df44 1533 {
lypinator 0:bb348c97df44 1534 /* Set the MMC state to ready to be able to start again the process */
lypinator 0:bb348c97df44 1535 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1536 HAL_MMC_ErrorCallback(hmmc);
lypinator 0:bb348c97df44 1537 }
lypinator 0:bb348c97df44 1538 }
lypinator 0:bb348c97df44 1539 #else /* SDIO_STA_STBITERR not defined */
lypinator 0:bb348c97df44 1540 else if(__HAL_MMC_GET_FLAG(hmmc, SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_TXUNDERR) != RESET)
lypinator 0:bb348c97df44 1541 {
lypinator 0:bb348c97df44 1542 /* Set Error code */
lypinator 0:bb348c97df44 1543 if(__HAL_MMC_GET_FLAG(hmmc, SDIO_IT_DCRCFAIL) != RESET)
lypinator 0:bb348c97df44 1544 {
lypinator 0:bb348c97df44 1545 hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_CRC_FAIL;
lypinator 0:bb348c97df44 1546 }
lypinator 0:bb348c97df44 1547 if(__HAL_MMC_GET_FLAG(hmmc, SDIO_IT_DTIMEOUT) != RESET)
lypinator 0:bb348c97df44 1548 {
lypinator 0:bb348c97df44 1549 hmmc->ErrorCode |= HAL_MMC_ERROR_DATA_TIMEOUT;
lypinator 0:bb348c97df44 1550 }
lypinator 0:bb348c97df44 1551 if(__HAL_MMC_GET_FLAG(hmmc, SDIO_IT_RXOVERR) != RESET)
lypinator 0:bb348c97df44 1552 {
lypinator 0:bb348c97df44 1553 hmmc->ErrorCode |= HAL_MMC_ERROR_RX_OVERRUN;
lypinator 0:bb348c97df44 1554 }
lypinator 0:bb348c97df44 1555 if(__HAL_MMC_GET_FLAG(hmmc, SDIO_IT_TXUNDERR) != RESET)
lypinator 0:bb348c97df44 1556 {
lypinator 0:bb348c97df44 1557 hmmc->ErrorCode |= HAL_MMC_ERROR_TX_UNDERRUN;
lypinator 0:bb348c97df44 1558 }
lypinator 0:bb348c97df44 1559
lypinator 0:bb348c97df44 1560 /* Clear All flags */
lypinator 0:bb348c97df44 1561 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 1562
lypinator 0:bb348c97df44 1563 /* Disable all interrupts */
lypinator 0:bb348c97df44 1564 __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
lypinator 0:bb348c97df44 1565 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
lypinator 0:bb348c97df44 1566
lypinator 0:bb348c97df44 1567 if((hmmc->Context & MMC_CONTEXT_DMA) != RESET)
lypinator 0:bb348c97df44 1568 {
lypinator 0:bb348c97df44 1569 /* Abort the MMC DMA Streams */
lypinator 0:bb348c97df44 1570 if(hmmc->hdmatx != NULL)
lypinator 0:bb348c97df44 1571 {
lypinator 0:bb348c97df44 1572 /* Set the DMA Tx abort callback */
lypinator 0:bb348c97df44 1573 hmmc->hdmatx->XferAbortCallback = MMC_DMATxAbort;
lypinator 0:bb348c97df44 1574 /* Abort DMA in IT mode */
lypinator 0:bb348c97df44 1575 if(HAL_DMA_Abort_IT(hmmc->hdmatx) != HAL_OK)
lypinator 0:bb348c97df44 1576 {
lypinator 0:bb348c97df44 1577 MMC_DMATxAbort(hmmc->hdmatx);
lypinator 0:bb348c97df44 1578 }
lypinator 0:bb348c97df44 1579 }
lypinator 0:bb348c97df44 1580 else if(hmmc->hdmarx != NULL)
lypinator 0:bb348c97df44 1581 {
lypinator 0:bb348c97df44 1582 /* Set the DMA Rx abort callback */
lypinator 0:bb348c97df44 1583 hmmc->hdmarx->XferAbortCallback = MMC_DMARxAbort;
lypinator 0:bb348c97df44 1584 /* Abort DMA in IT mode */
lypinator 0:bb348c97df44 1585 if(HAL_DMA_Abort_IT(hmmc->hdmarx) != HAL_OK)
lypinator 0:bb348c97df44 1586 {
lypinator 0:bb348c97df44 1587 MMC_DMARxAbort(hmmc->hdmarx);
lypinator 0:bb348c97df44 1588 }
lypinator 0:bb348c97df44 1589 }
lypinator 0:bb348c97df44 1590 else
lypinator 0:bb348c97df44 1591 {
lypinator 0:bb348c97df44 1592 hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 1593 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1594 HAL_MMC_AbortCallback(hmmc);
lypinator 0:bb348c97df44 1595 }
lypinator 0:bb348c97df44 1596 }
lypinator 0:bb348c97df44 1597 else if((hmmc->Context & MMC_CONTEXT_IT) != RESET)
lypinator 0:bb348c97df44 1598 {
lypinator 0:bb348c97df44 1599 /* Set the MMC state to ready to be able to start again the process */
lypinator 0:bb348c97df44 1600 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1601 HAL_MMC_ErrorCallback(hmmc);
lypinator 0:bb348c97df44 1602 }
lypinator 0:bb348c97df44 1603 }
lypinator 0:bb348c97df44 1604 #endif /* SDIO_STA_STBITERR */
lypinator 0:bb348c97df44 1605 }
lypinator 0:bb348c97df44 1606
lypinator 0:bb348c97df44 1607 /**
lypinator 0:bb348c97df44 1608 * @brief return the MMC state
lypinator 0:bb348c97df44 1609 * @param hmmc Pointer to mmc handle
lypinator 0:bb348c97df44 1610 * @retval HAL state
lypinator 0:bb348c97df44 1611 */
lypinator 0:bb348c97df44 1612 HAL_MMC_StateTypeDef HAL_MMC_GetState(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 1613 {
lypinator 0:bb348c97df44 1614 return hmmc->State;
lypinator 0:bb348c97df44 1615 }
lypinator 0:bb348c97df44 1616
lypinator 0:bb348c97df44 1617 /**
lypinator 0:bb348c97df44 1618 * @brief Return the MMC error code
lypinator 0:bb348c97df44 1619 * @param hmmc Pointer to a MMC_HandleTypeDef structure that contains
lypinator 0:bb348c97df44 1620 * the configuration information.
lypinator 0:bb348c97df44 1621 * @retval MMC Error Code
lypinator 0:bb348c97df44 1622 */
lypinator 0:bb348c97df44 1623 uint32_t HAL_MMC_GetError(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 1624 {
lypinator 0:bb348c97df44 1625 return hmmc->ErrorCode;
lypinator 0:bb348c97df44 1626 }
lypinator 0:bb348c97df44 1627
lypinator 0:bb348c97df44 1628 /**
lypinator 0:bb348c97df44 1629 * @brief Tx Transfer completed callbacks
lypinator 0:bb348c97df44 1630 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 1631 * @retval None
lypinator 0:bb348c97df44 1632 */
lypinator 0:bb348c97df44 1633 __weak void HAL_MMC_TxCpltCallback(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 1634 {
lypinator 0:bb348c97df44 1635 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 1636 UNUSED(hmmc);
lypinator 0:bb348c97df44 1637
lypinator 0:bb348c97df44 1638 /* NOTE : This function should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 1639 the HAL_MMC_TxCpltCallback can be implemented in the user file
lypinator 0:bb348c97df44 1640 */
lypinator 0:bb348c97df44 1641 }
lypinator 0:bb348c97df44 1642
lypinator 0:bb348c97df44 1643 /**
lypinator 0:bb348c97df44 1644 * @brief Rx Transfer completed callbacks
lypinator 0:bb348c97df44 1645 * @param hmmc Pointer MMC handle
lypinator 0:bb348c97df44 1646 * @retval None
lypinator 0:bb348c97df44 1647 */
lypinator 0:bb348c97df44 1648 __weak void HAL_MMC_RxCpltCallback(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 1649 {
lypinator 0:bb348c97df44 1650 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 1651 UNUSED(hmmc);
lypinator 0:bb348c97df44 1652
lypinator 0:bb348c97df44 1653 /* NOTE : This function should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 1654 the HAL_MMC_RxCpltCallback can be implemented in the user file
lypinator 0:bb348c97df44 1655 */
lypinator 0:bb348c97df44 1656 }
lypinator 0:bb348c97df44 1657
lypinator 0:bb348c97df44 1658 /**
lypinator 0:bb348c97df44 1659 * @brief MMC error callbacks
lypinator 0:bb348c97df44 1660 * @param hmmc Pointer MMC handle
lypinator 0:bb348c97df44 1661 * @retval None
lypinator 0:bb348c97df44 1662 */
lypinator 0:bb348c97df44 1663 __weak void HAL_MMC_ErrorCallback(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 1664 {
lypinator 0:bb348c97df44 1665 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 1666 UNUSED(hmmc);
lypinator 0:bb348c97df44 1667
lypinator 0:bb348c97df44 1668 /* NOTE : This function should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 1669 the HAL_MMC_ErrorCallback can be implemented in the user file
lypinator 0:bb348c97df44 1670 */
lypinator 0:bb348c97df44 1671 }
lypinator 0:bb348c97df44 1672
lypinator 0:bb348c97df44 1673 /**
lypinator 0:bb348c97df44 1674 * @brief MMC Abort callbacks
lypinator 0:bb348c97df44 1675 * @param hmmc Pointer MMC handle
lypinator 0:bb348c97df44 1676 * @retval None
lypinator 0:bb348c97df44 1677 */
lypinator 0:bb348c97df44 1678 __weak void HAL_MMC_AbortCallback(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 1679 {
lypinator 0:bb348c97df44 1680 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 1681 UNUSED(hmmc);
lypinator 0:bb348c97df44 1682
lypinator 0:bb348c97df44 1683 /* NOTE : This function should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 1684 the HAL_MMC_ErrorCallback can be implemented in the user file
lypinator 0:bb348c97df44 1685 */
lypinator 0:bb348c97df44 1686 }
lypinator 0:bb348c97df44 1687
lypinator 0:bb348c97df44 1688
lypinator 0:bb348c97df44 1689 /**
lypinator 0:bb348c97df44 1690 * @}
lypinator 0:bb348c97df44 1691 */
lypinator 0:bb348c97df44 1692
lypinator 0:bb348c97df44 1693 /** @addtogroup MMC_Exported_Functions_Group3
lypinator 0:bb348c97df44 1694 * @brief management functions
lypinator 0:bb348c97df44 1695 *
lypinator 0:bb348c97df44 1696 @verbatim
lypinator 0:bb348c97df44 1697 ==============================================================================
lypinator 0:bb348c97df44 1698 ##### Peripheral Control functions #####
lypinator 0:bb348c97df44 1699 ==============================================================================
lypinator 0:bb348c97df44 1700 [..]
lypinator 0:bb348c97df44 1701 This subsection provides a set of functions allowing to control the MMC card
lypinator 0:bb348c97df44 1702 operations and get the related information
lypinator 0:bb348c97df44 1703
lypinator 0:bb348c97df44 1704 @endverbatim
lypinator 0:bb348c97df44 1705 * @{
lypinator 0:bb348c97df44 1706 */
lypinator 0:bb348c97df44 1707
lypinator 0:bb348c97df44 1708 /**
lypinator 0:bb348c97df44 1709 * @brief Returns information the information of the card which are stored on
lypinator 0:bb348c97df44 1710 * the CID register.
lypinator 0:bb348c97df44 1711 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 1712 * @param pCID Pointer to a HAL_MMC_CIDTypedef structure that
lypinator 0:bb348c97df44 1713 * contains all CID register parameters
lypinator 0:bb348c97df44 1714 * @retval HAL status
lypinator 0:bb348c97df44 1715 */
lypinator 0:bb348c97df44 1716 HAL_StatusTypeDef HAL_MMC_GetCardCID(MMC_HandleTypeDef *hmmc, HAL_MMC_CardCIDTypeDef *pCID)
lypinator 0:bb348c97df44 1717 {
lypinator 0:bb348c97df44 1718 uint32_t tmp = 0U;
lypinator 0:bb348c97df44 1719
lypinator 0:bb348c97df44 1720 /* Byte 0 */
lypinator 0:bb348c97df44 1721 tmp = (uint8_t)((hmmc->CID[0U] & 0xFF000000U) >> 24U);
lypinator 0:bb348c97df44 1722 pCID->ManufacturerID = tmp;
lypinator 0:bb348c97df44 1723
lypinator 0:bb348c97df44 1724 /* Byte 1 */
lypinator 0:bb348c97df44 1725 tmp = (uint8_t)((hmmc->CID[0U] & 0x00FF0000U) >> 16U);
lypinator 0:bb348c97df44 1726 pCID->OEM_AppliID = tmp << 8U;
lypinator 0:bb348c97df44 1727
lypinator 0:bb348c97df44 1728 /* Byte 2 */
lypinator 0:bb348c97df44 1729 tmp = (uint8_t)((hmmc->CID[0U] & 0x000000FF00U) >> 8U);
lypinator 0:bb348c97df44 1730 pCID->OEM_AppliID |= tmp;
lypinator 0:bb348c97df44 1731
lypinator 0:bb348c97df44 1732 /* Byte 3 */
lypinator 0:bb348c97df44 1733 tmp = (uint8_t)(hmmc->CID[0U] & 0x000000FFU);
lypinator 0:bb348c97df44 1734 pCID->ProdName1 = tmp << 24U;
lypinator 0:bb348c97df44 1735
lypinator 0:bb348c97df44 1736 /* Byte 4 */
lypinator 0:bb348c97df44 1737 tmp = (uint8_t)((hmmc->CID[1U] & 0xFF000000U) >> 24U);
lypinator 0:bb348c97df44 1738 pCID->ProdName1 |= tmp << 16U;
lypinator 0:bb348c97df44 1739
lypinator 0:bb348c97df44 1740 /* Byte 5 */
lypinator 0:bb348c97df44 1741 tmp = (uint8_t)((hmmc->CID[1U] & 0x00FF0000U) >> 16U);
lypinator 0:bb348c97df44 1742 pCID->ProdName1 |= tmp << 8U;
lypinator 0:bb348c97df44 1743
lypinator 0:bb348c97df44 1744 /* Byte 6 */
lypinator 0:bb348c97df44 1745 tmp = (uint8_t)((hmmc->CID[1U] & 0x0000FF00U) >> 8U);
lypinator 0:bb348c97df44 1746 pCID->ProdName1 |= tmp;
lypinator 0:bb348c97df44 1747
lypinator 0:bb348c97df44 1748 /* Byte 7 */
lypinator 0:bb348c97df44 1749 tmp = (uint8_t)(hmmc->CID[1U] & 0x000000FFU);
lypinator 0:bb348c97df44 1750 pCID->ProdName2 = tmp;
lypinator 0:bb348c97df44 1751
lypinator 0:bb348c97df44 1752 /* Byte 8 */
lypinator 0:bb348c97df44 1753 tmp = (uint8_t)((hmmc->CID[2U] & 0xFF000000U) >> 24U);
lypinator 0:bb348c97df44 1754 pCID->ProdRev = tmp;
lypinator 0:bb348c97df44 1755
lypinator 0:bb348c97df44 1756 /* Byte 9 */
lypinator 0:bb348c97df44 1757 tmp = (uint8_t)((hmmc->CID[2U] & 0x00FF0000U) >> 16U);
lypinator 0:bb348c97df44 1758 pCID->ProdSN = tmp << 24U;
lypinator 0:bb348c97df44 1759
lypinator 0:bb348c97df44 1760 /* Byte 10 */
lypinator 0:bb348c97df44 1761 tmp = (uint8_t)((hmmc->CID[2U] & 0x0000FF00U) >> 8U);
lypinator 0:bb348c97df44 1762 pCID->ProdSN |= tmp << 16U;
lypinator 0:bb348c97df44 1763
lypinator 0:bb348c97df44 1764 /* Byte 11 */
lypinator 0:bb348c97df44 1765 tmp = (uint8_t)(hmmc->CID[2U] & 0x000000FFU);
lypinator 0:bb348c97df44 1766 pCID->ProdSN |= tmp << 8U;
lypinator 0:bb348c97df44 1767
lypinator 0:bb348c97df44 1768 /* Byte 12 */
lypinator 0:bb348c97df44 1769 tmp = (uint8_t)((hmmc->CID[3U] & 0xFF000000U) >> 24U);
lypinator 0:bb348c97df44 1770 pCID->ProdSN |= tmp;
lypinator 0:bb348c97df44 1771
lypinator 0:bb348c97df44 1772 /* Byte 13 */
lypinator 0:bb348c97df44 1773 tmp = (uint8_t)((hmmc->CID[3U] & 0x00FF0000U) >> 16U);
lypinator 0:bb348c97df44 1774 pCID->Reserved1 |= (tmp & 0xF0U) >> 4U;
lypinator 0:bb348c97df44 1775 pCID->ManufactDate = (tmp & 0x0FU) << 8U;
lypinator 0:bb348c97df44 1776
lypinator 0:bb348c97df44 1777 /* Byte 14 */
lypinator 0:bb348c97df44 1778 tmp = (uint8_t)((hmmc->CID[3U] & 0x0000FF00U) >> 8U);
lypinator 0:bb348c97df44 1779 pCID->ManufactDate |= tmp;
lypinator 0:bb348c97df44 1780
lypinator 0:bb348c97df44 1781 /* Byte 15 */
lypinator 0:bb348c97df44 1782 tmp = (uint8_t)(hmmc->CID[3U] & 0x000000FFU);
lypinator 0:bb348c97df44 1783 pCID->CID_CRC = (tmp & 0xFEU) >> 1U;
lypinator 0:bb348c97df44 1784 pCID->Reserved2 = 1U;
lypinator 0:bb348c97df44 1785
lypinator 0:bb348c97df44 1786 return HAL_OK;
lypinator 0:bb348c97df44 1787 }
lypinator 0:bb348c97df44 1788
lypinator 0:bb348c97df44 1789 /**
lypinator 0:bb348c97df44 1790 * @brief Returns information the information of the card which are stored on
lypinator 0:bb348c97df44 1791 * the CSD register.
lypinator 0:bb348c97df44 1792 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 1793 * @param pCSD Pointer to a HAL_MMC_CardInfoTypeDef structure that
lypinator 0:bb348c97df44 1794 * contains all CSD register parameters
lypinator 0:bb348c97df44 1795 * @retval HAL status
lypinator 0:bb348c97df44 1796 */
lypinator 0:bb348c97df44 1797 HAL_StatusTypeDef HAL_MMC_GetCardCSD(MMC_HandleTypeDef *hmmc, HAL_MMC_CardCSDTypeDef *pCSD)
lypinator 0:bb348c97df44 1798 {
lypinator 0:bb348c97df44 1799 uint32_t tmp = 0U;
lypinator 0:bb348c97df44 1800
lypinator 0:bb348c97df44 1801 /* Byte 0 */
lypinator 0:bb348c97df44 1802 tmp = (hmmc->CSD[0U] & 0xFF000000U) >> 24U;
lypinator 0:bb348c97df44 1803 pCSD->CSDStruct = (uint8_t)((tmp & 0xC0U) >> 6U);
lypinator 0:bb348c97df44 1804 pCSD->SysSpecVersion = (uint8_t)((tmp & 0x3CU) >> 2U);
lypinator 0:bb348c97df44 1805 pCSD->Reserved1 = tmp & 0x03U;
lypinator 0:bb348c97df44 1806
lypinator 0:bb348c97df44 1807 /* Byte 1 */
lypinator 0:bb348c97df44 1808 tmp = (hmmc->CSD[0U] & 0x00FF0000U) >> 16U;
lypinator 0:bb348c97df44 1809 pCSD->TAAC = (uint8_t)tmp;
lypinator 0:bb348c97df44 1810
lypinator 0:bb348c97df44 1811 /* Byte 2 */
lypinator 0:bb348c97df44 1812 tmp = (hmmc->CSD[0U] & 0x0000FF00U) >> 8U;
lypinator 0:bb348c97df44 1813 pCSD->NSAC = (uint8_t)tmp;
lypinator 0:bb348c97df44 1814
lypinator 0:bb348c97df44 1815 /* Byte 3 */
lypinator 0:bb348c97df44 1816 tmp = hmmc->CSD[0U] & 0x000000FFU;
lypinator 0:bb348c97df44 1817 pCSD->MaxBusClkFrec = (uint8_t)tmp;
lypinator 0:bb348c97df44 1818
lypinator 0:bb348c97df44 1819 /* Byte 4 */
lypinator 0:bb348c97df44 1820 tmp = (hmmc->CSD[1U] & 0xFF000000U) >> 24U;
lypinator 0:bb348c97df44 1821 pCSD->CardComdClasses = (uint16_t)(tmp << 4U);
lypinator 0:bb348c97df44 1822
lypinator 0:bb348c97df44 1823 /* Byte 5 */
lypinator 0:bb348c97df44 1824 tmp = (hmmc->CSD[1U] & 0x00FF0000U) >> 16U;
lypinator 0:bb348c97df44 1825 pCSD->CardComdClasses |= (uint16_t)((tmp & 0xF0U) >> 4U);
lypinator 0:bb348c97df44 1826 pCSD->RdBlockLen = (uint8_t)(tmp & 0x0FU);
lypinator 0:bb348c97df44 1827
lypinator 0:bb348c97df44 1828 /* Byte 6 */
lypinator 0:bb348c97df44 1829 tmp = (hmmc->CSD[1U] & 0x0000FF00U) >> 8U;
lypinator 0:bb348c97df44 1830 pCSD->PartBlockRead = (uint8_t)((tmp & 0x80U) >> 7U);
lypinator 0:bb348c97df44 1831 pCSD->WrBlockMisalign = (uint8_t)((tmp & 0x40U) >> 6U);
lypinator 0:bb348c97df44 1832 pCSD->RdBlockMisalign = (uint8_t)((tmp & 0x20U) >> 5U);
lypinator 0:bb348c97df44 1833 pCSD->DSRImpl = (uint8_t)((tmp & 0x10U) >> 4U);
lypinator 0:bb348c97df44 1834 pCSD->Reserved2 = 0; /*!< Reserved */
lypinator 0:bb348c97df44 1835
lypinator 0:bb348c97df44 1836 pCSD->DeviceSize = (tmp & 0x03U) << 10U;
lypinator 0:bb348c97df44 1837
lypinator 0:bb348c97df44 1838 /* Byte 7 */
lypinator 0:bb348c97df44 1839 tmp = (uint8_t)(hmmc->CSD[1U] & 0x000000FFU);
lypinator 0:bb348c97df44 1840 pCSD->DeviceSize |= (tmp) << 2U;
lypinator 0:bb348c97df44 1841
lypinator 0:bb348c97df44 1842 /* Byte 8 */
lypinator 0:bb348c97df44 1843 tmp = (uint8_t)((hmmc->CSD[2U] & 0xFF000000U) >> 24U);
lypinator 0:bb348c97df44 1844 pCSD->DeviceSize |= (tmp & 0xC0U) >> 6U;
lypinator 0:bb348c97df44 1845
lypinator 0:bb348c97df44 1846 pCSD->MaxRdCurrentVDDMin = (tmp & 0x38U) >> 3U;
lypinator 0:bb348c97df44 1847 pCSD->MaxRdCurrentVDDMax = (tmp & 0x07U);
lypinator 0:bb348c97df44 1848
lypinator 0:bb348c97df44 1849 /* Byte 9 */
lypinator 0:bb348c97df44 1850 tmp = (uint8_t)((hmmc->CSD[2U] & 0x00FF0000U) >> 16U);
lypinator 0:bb348c97df44 1851 pCSD->MaxWrCurrentVDDMin = (tmp & 0xE0U) >> 5U;
lypinator 0:bb348c97df44 1852 pCSD->MaxWrCurrentVDDMax = (tmp & 0x1CU) >> 2U;
lypinator 0:bb348c97df44 1853 pCSD->DeviceSizeMul = (tmp & 0x03U) << 1U;
lypinator 0:bb348c97df44 1854 /* Byte 10 */
lypinator 0:bb348c97df44 1855 tmp = (uint8_t)((hmmc->CSD[2] & 0x0000FF00U) >> 8U);
lypinator 0:bb348c97df44 1856 pCSD->DeviceSizeMul |= (tmp & 0x80U) >> 7U;
lypinator 0:bb348c97df44 1857
lypinator 0:bb348c97df44 1858 hmmc->MmcCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
lypinator 0:bb348c97df44 1859 hmmc->MmcCard.BlockNbr *= (1U << (pCSD->DeviceSizeMul + 2U));
lypinator 0:bb348c97df44 1860 hmmc->MmcCard.BlockSize = 1U << (pCSD->RdBlockLen);
lypinator 0:bb348c97df44 1861
lypinator 0:bb348c97df44 1862 hmmc->MmcCard.LogBlockNbr = (hmmc->MmcCard.BlockNbr) * ((hmmc->MmcCard.BlockSize) / 512U);
lypinator 0:bb348c97df44 1863 hmmc->MmcCard.LogBlockSize = 512U;
lypinator 0:bb348c97df44 1864
lypinator 0:bb348c97df44 1865 pCSD->EraseGrSize = (tmp & 0x40U) >> 6U;
lypinator 0:bb348c97df44 1866 pCSD->EraseGrMul = (tmp & 0x3FU) << 1U;
lypinator 0:bb348c97df44 1867
lypinator 0:bb348c97df44 1868 /* Byte 11 */
lypinator 0:bb348c97df44 1869 tmp = (uint8_t)(hmmc->CSD[2U] & 0x000000FFU);
lypinator 0:bb348c97df44 1870 pCSD->EraseGrMul |= (tmp & 0x80U) >> 7U;
lypinator 0:bb348c97df44 1871 pCSD->WrProtectGrSize = (tmp & 0x7FU);
lypinator 0:bb348c97df44 1872
lypinator 0:bb348c97df44 1873 /* Byte 12 */
lypinator 0:bb348c97df44 1874 tmp = (uint8_t)((hmmc->CSD[3U] & 0xFF000000U) >> 24U);
lypinator 0:bb348c97df44 1875 pCSD->WrProtectGrEnable = (tmp & 0x80U) >> 7U;
lypinator 0:bb348c97df44 1876 pCSD->ManDeflECC = (tmp & 0x60U) >> 5U;
lypinator 0:bb348c97df44 1877 pCSD->WrSpeedFact = (tmp & 0x1CU) >> 2U;
lypinator 0:bb348c97df44 1878 pCSD->MaxWrBlockLen = (tmp & 0x03U) << 2U;
lypinator 0:bb348c97df44 1879
lypinator 0:bb348c97df44 1880 /* Byte 13 */
lypinator 0:bb348c97df44 1881 tmp = (uint8_t)((hmmc->CSD[3U] & 0x00FF0000U) >> 16U);
lypinator 0:bb348c97df44 1882 pCSD->MaxWrBlockLen |= (tmp & 0xC0U) >> 6U;
lypinator 0:bb348c97df44 1883 pCSD->WriteBlockPaPartial = (tmp & 0x20U) >> 5U;
lypinator 0:bb348c97df44 1884 pCSD->Reserved3 = 0U;
lypinator 0:bb348c97df44 1885 pCSD->ContentProtectAppli = (tmp & 0x01U);
lypinator 0:bb348c97df44 1886
lypinator 0:bb348c97df44 1887 /* Byte 14 */
lypinator 0:bb348c97df44 1888 tmp = (uint8_t)((hmmc->CSD[3U] & 0x0000FF00U) >> 8U);
lypinator 0:bb348c97df44 1889 pCSD->FileFormatGrouop = (tmp & 0x80U) >> 7U;
lypinator 0:bb348c97df44 1890 pCSD->CopyFlag = (tmp & 0x40U) >> 6U;
lypinator 0:bb348c97df44 1891 pCSD->PermWrProtect = (tmp & 0x20U) >> 5U;
lypinator 0:bb348c97df44 1892 pCSD->TempWrProtect = (tmp & 0x10U) >> 4U;
lypinator 0:bb348c97df44 1893 pCSD->FileFormat = (tmp & 0x0CU) >> 2U;
lypinator 0:bb348c97df44 1894 pCSD->ECC = (tmp & 0x03U);
lypinator 0:bb348c97df44 1895
lypinator 0:bb348c97df44 1896 /* Byte 15 */
lypinator 0:bb348c97df44 1897 tmp = (uint8_t)(hmmc->CSD[3U] & 0x000000FFU);
lypinator 0:bb348c97df44 1898 pCSD->CSD_CRC = (tmp & 0xFEU) >> 1U;
lypinator 0:bb348c97df44 1899 pCSD->Reserved4 = 1U;
lypinator 0:bb348c97df44 1900
lypinator 0:bb348c97df44 1901 return HAL_OK;
lypinator 0:bb348c97df44 1902 }
lypinator 0:bb348c97df44 1903
lypinator 0:bb348c97df44 1904 /**
lypinator 0:bb348c97df44 1905 * @brief Gets the MMC card info.
lypinator 0:bb348c97df44 1906 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 1907 * @param pCardInfo Pointer to the HAL_MMC_CardInfoTypeDef structure that
lypinator 0:bb348c97df44 1908 * will contain the MMC card status information
lypinator 0:bb348c97df44 1909 * @retval HAL status
lypinator 0:bb348c97df44 1910 */
lypinator 0:bb348c97df44 1911 HAL_StatusTypeDef HAL_MMC_GetCardInfo(MMC_HandleTypeDef *hmmc, HAL_MMC_CardInfoTypeDef *pCardInfo)
lypinator 0:bb348c97df44 1912 {
lypinator 0:bb348c97df44 1913 pCardInfo->CardType = (uint32_t)(hmmc->MmcCard.CardType);
lypinator 0:bb348c97df44 1914 pCardInfo->Class = (uint32_t)(hmmc->MmcCard.Class);
lypinator 0:bb348c97df44 1915 pCardInfo->RelCardAdd = (uint32_t)(hmmc->MmcCard.RelCardAdd);
lypinator 0:bb348c97df44 1916 pCardInfo->BlockNbr = (uint32_t)(hmmc->MmcCard.BlockNbr);
lypinator 0:bb348c97df44 1917 pCardInfo->BlockSize = (uint32_t)(hmmc->MmcCard.BlockSize);
lypinator 0:bb348c97df44 1918 pCardInfo->LogBlockNbr = (uint32_t)(hmmc->MmcCard.LogBlockNbr);
lypinator 0:bb348c97df44 1919 pCardInfo->LogBlockSize = (uint32_t)(hmmc->MmcCard.LogBlockSize);
lypinator 0:bb348c97df44 1920
lypinator 0:bb348c97df44 1921 return HAL_OK;
lypinator 0:bb348c97df44 1922 }
lypinator 0:bb348c97df44 1923
lypinator 0:bb348c97df44 1924 /**
lypinator 0:bb348c97df44 1925 * @brief Enables wide bus operation for the requested card if supported by
lypinator 0:bb348c97df44 1926 * card.
lypinator 0:bb348c97df44 1927 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 1928 * @param WideMode Specifies the MMC card wide bus mode
lypinator 0:bb348c97df44 1929 * This parameter can be one of the following values:
lypinator 0:bb348c97df44 1930 * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer
lypinator 0:bb348c97df44 1931 * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
lypinator 0:bb348c97df44 1932 * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
lypinator 0:bb348c97df44 1933 * @retval HAL status
lypinator 0:bb348c97df44 1934 */
lypinator 0:bb348c97df44 1935 HAL_StatusTypeDef HAL_MMC_ConfigWideBusOperation(MMC_HandleTypeDef *hmmc, uint32_t WideMode)
lypinator 0:bb348c97df44 1936 {
lypinator 0:bb348c97df44 1937 __IO uint32_t count = 0U;
lypinator 0:bb348c97df44 1938 SDIO_InitTypeDef Init;
lypinator 0:bb348c97df44 1939 uint32_t errorstate = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 1940 uint32_t response = 0U, busy = 0U;
lypinator 0:bb348c97df44 1941
lypinator 0:bb348c97df44 1942 /* Check the parameters */
lypinator 0:bb348c97df44 1943 assert_param(IS_SDIO_BUS_WIDE(WideMode));
lypinator 0:bb348c97df44 1944
lypinator 0:bb348c97df44 1945 /* Chnage Satte */
lypinator 0:bb348c97df44 1946 hmmc->State = HAL_MMC_STATE_BUSY;
lypinator 0:bb348c97df44 1947
lypinator 0:bb348c97df44 1948 /* Update Clock for Bus mode update */
lypinator 0:bb348c97df44 1949 Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
lypinator 0:bb348c97df44 1950 Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
lypinator 0:bb348c97df44 1951 Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
lypinator 0:bb348c97df44 1952 Init.BusWide = WideMode;
lypinator 0:bb348c97df44 1953 Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
lypinator 0:bb348c97df44 1954 Init.ClockDiv = SDIO_INIT_CLK_DIV;
lypinator 0:bb348c97df44 1955 /* Initialize SDIO*/
lypinator 0:bb348c97df44 1956 SDIO_Init(hmmc->Instance, Init);
lypinator 0:bb348c97df44 1957
lypinator 0:bb348c97df44 1958 if(WideMode == SDIO_BUS_WIDE_8B)
lypinator 0:bb348c97df44 1959 {
lypinator 0:bb348c97df44 1960 errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70200U);
lypinator 0:bb348c97df44 1961 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 1962 {
lypinator 0:bb348c97df44 1963 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 1964 }
lypinator 0:bb348c97df44 1965 }
lypinator 0:bb348c97df44 1966 else if(WideMode == SDIO_BUS_WIDE_4B)
lypinator 0:bb348c97df44 1967 {
lypinator 0:bb348c97df44 1968 errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70100U);
lypinator 0:bb348c97df44 1969 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 1970 {
lypinator 0:bb348c97df44 1971 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 1972 }
lypinator 0:bb348c97df44 1973 }
lypinator 0:bb348c97df44 1974 else if(WideMode == SDIO_BUS_WIDE_1B)
lypinator 0:bb348c97df44 1975 {
lypinator 0:bb348c97df44 1976 errorstate = SDMMC_CmdSwitch(hmmc->Instance, 0x03B70000U);
lypinator 0:bb348c97df44 1977 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 1978 {
lypinator 0:bb348c97df44 1979 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 1980 }
lypinator 0:bb348c97df44 1981 }
lypinator 0:bb348c97df44 1982 else
lypinator 0:bb348c97df44 1983 {
lypinator 0:bb348c97df44 1984 /* WideMode is not a valid argument*/
lypinator 0:bb348c97df44 1985 hmmc->ErrorCode |= HAL_MMC_ERROR_PARAM;
lypinator 0:bb348c97df44 1986 }
lypinator 0:bb348c97df44 1987
lypinator 0:bb348c97df44 1988 /* Check for switch error and violation of the trial number of sending CMD 13 */
lypinator 0:bb348c97df44 1989 while(busy == 0U)
lypinator 0:bb348c97df44 1990 {
lypinator 0:bb348c97df44 1991 if(count++ == SDMMC_MAX_TRIAL)
lypinator 0:bb348c97df44 1992 {
lypinator 0:bb348c97df44 1993 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 1994 hmmc->ErrorCode |= HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
lypinator 0:bb348c97df44 1995 return HAL_ERROR;
lypinator 0:bb348c97df44 1996 }
lypinator 0:bb348c97df44 1997
lypinator 0:bb348c97df44 1998 /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
lypinator 0:bb348c97df44 1999 errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
lypinator 0:bb348c97df44 2000 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 2001 {
lypinator 0:bb348c97df44 2002 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 2003 }
lypinator 0:bb348c97df44 2004
lypinator 0:bb348c97df44 2005 /* Get command response */
lypinator 0:bb348c97df44 2006 response = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
lypinator 0:bb348c97df44 2007
lypinator 0:bb348c97df44 2008 /* Get operating voltage*/
lypinator 0:bb348c97df44 2009 busy = (((response >> 7U) == 1U) ? 0U : 1U);
lypinator 0:bb348c97df44 2010 }
lypinator 0:bb348c97df44 2011
lypinator 0:bb348c97df44 2012 /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
lypinator 0:bb348c97df44 2013 count = SDMMC_DATATIMEOUT;
lypinator 0:bb348c97df44 2014 while((response & 0x00000100U) == 0U)
lypinator 0:bb348c97df44 2015 {
lypinator 0:bb348c97df44 2016 if(count-- == 0U)
lypinator 0:bb348c97df44 2017 {
lypinator 0:bb348c97df44 2018 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 2019 hmmc->ErrorCode |= HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
lypinator 0:bb348c97df44 2020 return HAL_ERROR;
lypinator 0:bb348c97df44 2021 }
lypinator 0:bb348c97df44 2022
lypinator 0:bb348c97df44 2023 /* While card is not ready for data and trial number for sending CMD13 is not exceeded */
lypinator 0:bb348c97df44 2024 errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
lypinator 0:bb348c97df44 2025 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 2026 {
lypinator 0:bb348c97df44 2027 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 2028 }
lypinator 0:bb348c97df44 2029
lypinator 0:bb348c97df44 2030 /* Get command response */
lypinator 0:bb348c97df44 2031 response = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
lypinator 0:bb348c97df44 2032 }
lypinator 0:bb348c97df44 2033
lypinator 0:bb348c97df44 2034 if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 2035 {
lypinator 0:bb348c97df44 2036 /* Clear all the static flags */
lypinator 0:bb348c97df44 2037 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 2038 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 2039 return HAL_ERROR;
lypinator 0:bb348c97df44 2040 }
lypinator 0:bb348c97df44 2041 else
lypinator 0:bb348c97df44 2042 {
lypinator 0:bb348c97df44 2043 /* Configure the SDIO peripheral */
lypinator 0:bb348c97df44 2044 Init.ClockEdge = hmmc->Init.ClockEdge;
lypinator 0:bb348c97df44 2045 Init.ClockBypass = hmmc->Init.ClockBypass;
lypinator 0:bb348c97df44 2046 Init.ClockPowerSave = hmmc->Init.ClockPowerSave;
lypinator 0:bb348c97df44 2047 Init.BusWide = WideMode;
lypinator 0:bb348c97df44 2048 Init.HardwareFlowControl = hmmc->Init.HardwareFlowControl;
lypinator 0:bb348c97df44 2049 Init.ClockDiv = hmmc->Init.ClockDiv;
lypinator 0:bb348c97df44 2050 SDIO_Init(hmmc->Instance, Init);
lypinator 0:bb348c97df44 2051 }
lypinator 0:bb348c97df44 2052
lypinator 0:bb348c97df44 2053 /* Change State */
lypinator 0:bb348c97df44 2054 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 2055
lypinator 0:bb348c97df44 2056 return HAL_OK;
lypinator 0:bb348c97df44 2057 }
lypinator 0:bb348c97df44 2058
lypinator 0:bb348c97df44 2059
lypinator 0:bb348c97df44 2060 /**
lypinator 0:bb348c97df44 2061 * @brief Gets the current mmc card data state.
lypinator 0:bb348c97df44 2062 * @param hmmc pointer to MMC handle
lypinator 0:bb348c97df44 2063 * @retval Card state
lypinator 0:bb348c97df44 2064 */
lypinator 0:bb348c97df44 2065 HAL_MMC_CardStateTypeDef HAL_MMC_GetCardState(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 2066 {
lypinator 0:bb348c97df44 2067 HAL_MMC_CardStateTypeDef cardstate = HAL_MMC_CARD_TRANSFER;
lypinator 0:bb348c97df44 2068 uint32_t errorstate = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 2069 uint32_t resp1 = 0U;
lypinator 0:bb348c97df44 2070
lypinator 0:bb348c97df44 2071 errorstate = MMC_SendStatus(hmmc, &resp1);
lypinator 0:bb348c97df44 2072 if(errorstate != HAL_OK)
lypinator 0:bb348c97df44 2073 {
lypinator 0:bb348c97df44 2074 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 2075 }
lypinator 0:bb348c97df44 2076
lypinator 0:bb348c97df44 2077 cardstate = (HAL_MMC_CardStateTypeDef)((resp1 >> 9U) & 0x0FU);
lypinator 0:bb348c97df44 2078
lypinator 0:bb348c97df44 2079 return cardstate;
lypinator 0:bb348c97df44 2080 }
lypinator 0:bb348c97df44 2081
lypinator 0:bb348c97df44 2082 /**
lypinator 0:bb348c97df44 2083 * @brief Abort the current transfer and disable the MMC.
lypinator 0:bb348c97df44 2084 * @param hmmc pointer to a MMC_HandleTypeDef structure that contains
lypinator 0:bb348c97df44 2085 * the configuration information for MMC module.
lypinator 0:bb348c97df44 2086 * @retval HAL status
lypinator 0:bb348c97df44 2087 */
lypinator 0:bb348c97df44 2088 HAL_StatusTypeDef HAL_MMC_Abort(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 2089 {
lypinator 0:bb348c97df44 2090 HAL_MMC_CardStateTypeDef CardState;
lypinator 0:bb348c97df44 2091
lypinator 0:bb348c97df44 2092 /* DIsable All interrupts */
lypinator 0:bb348c97df44 2093 __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
lypinator 0:bb348c97df44 2094 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
lypinator 0:bb348c97df44 2095
lypinator 0:bb348c97df44 2096 /* Clear All flags */
lypinator 0:bb348c97df44 2097 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 2098
lypinator 0:bb348c97df44 2099 if((hmmc->hdmatx != NULL) || (hmmc->hdmarx != NULL))
lypinator 0:bb348c97df44 2100 {
lypinator 0:bb348c97df44 2101 /* Disable the MMC DMA request */
lypinator 0:bb348c97df44 2102 hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
lypinator 0:bb348c97df44 2103
lypinator 0:bb348c97df44 2104 /* Abort the MMC DMA Tx Stream */
lypinator 0:bb348c97df44 2105 if(hmmc->hdmatx != NULL)
lypinator 0:bb348c97df44 2106 {
lypinator 0:bb348c97df44 2107 HAL_DMA_Abort(hmmc->hdmatx);
lypinator 0:bb348c97df44 2108 }
lypinator 0:bb348c97df44 2109 /* Abort the MMC DMA Rx Stream */
lypinator 0:bb348c97df44 2110 if(hmmc->hdmarx != NULL)
lypinator 0:bb348c97df44 2111 {
lypinator 0:bb348c97df44 2112 HAL_DMA_Abort(hmmc->hdmarx);
lypinator 0:bb348c97df44 2113 }
lypinator 0:bb348c97df44 2114 }
lypinator 0:bb348c97df44 2115
lypinator 0:bb348c97df44 2116 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 2117 CardState = HAL_MMC_GetCardState(hmmc);
lypinator 0:bb348c97df44 2118 if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
lypinator 0:bb348c97df44 2119 {
lypinator 0:bb348c97df44 2120 hmmc->ErrorCode = SDMMC_CmdStopTransfer(hmmc->Instance);
lypinator 0:bb348c97df44 2121 }
lypinator 0:bb348c97df44 2122 if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 2123 {
lypinator 0:bb348c97df44 2124 return HAL_ERROR;
lypinator 0:bb348c97df44 2125 }
lypinator 0:bb348c97df44 2126 return HAL_OK;
lypinator 0:bb348c97df44 2127 }
lypinator 0:bb348c97df44 2128
lypinator 0:bb348c97df44 2129 /**
lypinator 0:bb348c97df44 2130 * @brief Abort the current transfer and disable the MMC (IT mode).
lypinator 0:bb348c97df44 2131 * @param hmmc pointer to a MMC_HandleTypeDef structure that contains
lypinator 0:bb348c97df44 2132 * the configuration information for MMC module.
lypinator 0:bb348c97df44 2133 * @retval HAL status
lypinator 0:bb348c97df44 2134 */
lypinator 0:bb348c97df44 2135 HAL_StatusTypeDef HAL_MMC_Abort_IT(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 2136 {
lypinator 0:bb348c97df44 2137 HAL_MMC_CardStateTypeDef CardState;
lypinator 0:bb348c97df44 2138
lypinator 0:bb348c97df44 2139 /* DIsable All interrupts */
lypinator 0:bb348c97df44 2140 __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
lypinator 0:bb348c97df44 2141 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
lypinator 0:bb348c97df44 2142
lypinator 0:bb348c97df44 2143 /* Clear All flags */
lypinator 0:bb348c97df44 2144 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 2145
lypinator 0:bb348c97df44 2146 if((hmmc->hdmatx != NULL) || (hmmc->hdmarx != NULL))
lypinator 0:bb348c97df44 2147 {
lypinator 0:bb348c97df44 2148 /* Disable the MMC DMA request */
lypinator 0:bb348c97df44 2149 hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
lypinator 0:bb348c97df44 2150
lypinator 0:bb348c97df44 2151 /* Abort the MMC DMA Tx Stream */
lypinator 0:bb348c97df44 2152 if(hmmc->hdmatx != NULL)
lypinator 0:bb348c97df44 2153 {
lypinator 0:bb348c97df44 2154 hmmc->hdmatx->XferAbortCallback = MMC_DMATxAbort;
lypinator 0:bb348c97df44 2155 if(HAL_DMA_Abort_IT(hmmc->hdmatx) != HAL_OK)
lypinator 0:bb348c97df44 2156 {
lypinator 0:bb348c97df44 2157 hmmc->hdmatx = NULL;
lypinator 0:bb348c97df44 2158 }
lypinator 0:bb348c97df44 2159 }
lypinator 0:bb348c97df44 2160 /* Abort the MMC DMA Rx Stream */
lypinator 0:bb348c97df44 2161 if(hmmc->hdmarx != NULL)
lypinator 0:bb348c97df44 2162 {
lypinator 0:bb348c97df44 2163 hmmc->hdmarx->XferAbortCallback = MMC_DMARxAbort;
lypinator 0:bb348c97df44 2164 if(HAL_DMA_Abort_IT(hmmc->hdmarx) != HAL_OK)
lypinator 0:bb348c97df44 2165 {
lypinator 0:bb348c97df44 2166 hmmc->hdmarx = NULL;
lypinator 0:bb348c97df44 2167 }
lypinator 0:bb348c97df44 2168 }
lypinator 0:bb348c97df44 2169 }
lypinator 0:bb348c97df44 2170
lypinator 0:bb348c97df44 2171 /* No transfer ongoing on both DMA channels*/
lypinator 0:bb348c97df44 2172 if((hmmc->hdmatx == NULL) && (hmmc->hdmarx == NULL))
lypinator 0:bb348c97df44 2173 {
lypinator 0:bb348c97df44 2174 CardState = HAL_MMC_GetCardState(hmmc);
lypinator 0:bb348c97df44 2175 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 2176 if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
lypinator 0:bb348c97df44 2177 {
lypinator 0:bb348c97df44 2178 hmmc->ErrorCode = SDMMC_CmdStopTransfer(hmmc->Instance);
lypinator 0:bb348c97df44 2179 }
lypinator 0:bb348c97df44 2180 if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 2181 {
lypinator 0:bb348c97df44 2182 return HAL_ERROR;
lypinator 0:bb348c97df44 2183 }
lypinator 0:bb348c97df44 2184 else
lypinator 0:bb348c97df44 2185 {
lypinator 0:bb348c97df44 2186 HAL_MMC_AbortCallback(hmmc);
lypinator 0:bb348c97df44 2187 }
lypinator 0:bb348c97df44 2188 }
lypinator 0:bb348c97df44 2189
lypinator 0:bb348c97df44 2190 return HAL_OK;
lypinator 0:bb348c97df44 2191 }
lypinator 0:bb348c97df44 2192
lypinator 0:bb348c97df44 2193 /**
lypinator 0:bb348c97df44 2194 * @}
lypinator 0:bb348c97df44 2195 */
lypinator 0:bb348c97df44 2196
lypinator 0:bb348c97df44 2197 /**
lypinator 0:bb348c97df44 2198 * @}
lypinator 0:bb348c97df44 2199 */
lypinator 0:bb348c97df44 2200
lypinator 0:bb348c97df44 2201 /* Private function ----------------------------------------------------------*/
lypinator 0:bb348c97df44 2202 /** @addtogroup MMC_Private_Functions
lypinator 0:bb348c97df44 2203 * @{
lypinator 0:bb348c97df44 2204 */
lypinator 0:bb348c97df44 2205
lypinator 0:bb348c97df44 2206 /**
lypinator 0:bb348c97df44 2207 * @brief DMA MMC transmit process complete callback
lypinator 0:bb348c97df44 2208 * @param hdma DMA handle
lypinator 0:bb348c97df44 2209 * @retval None
lypinator 0:bb348c97df44 2210 */
lypinator 0:bb348c97df44 2211 static void MMC_DMATransmitCplt(DMA_HandleTypeDef *hdma)
lypinator 0:bb348c97df44 2212 {
lypinator 0:bb348c97df44 2213 MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
lypinator 0:bb348c97df44 2214
lypinator 0:bb348c97df44 2215 /* Enable DATAEND Interrupt */
lypinator 0:bb348c97df44 2216 __HAL_MMC_ENABLE_IT(hmmc, (SDIO_IT_DATAEND));
lypinator 0:bb348c97df44 2217 }
lypinator 0:bb348c97df44 2218
lypinator 0:bb348c97df44 2219 /**
lypinator 0:bb348c97df44 2220 * @brief DMA MMC receive process complete callback
lypinator 0:bb348c97df44 2221 * @param hdma DMA handle
lypinator 0:bb348c97df44 2222 * @retval None
lypinator 0:bb348c97df44 2223 */
lypinator 0:bb348c97df44 2224 static void MMC_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
lypinator 0:bb348c97df44 2225 {
lypinator 0:bb348c97df44 2226 MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
lypinator 0:bb348c97df44 2227 uint32_t errorstate = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 2228
lypinator 0:bb348c97df44 2229 /* Send stop command in multiblock write */
lypinator 0:bb348c97df44 2230 if(hmmc->Context == (MMC_CONTEXT_READ_MULTIPLE_BLOCK | MMC_CONTEXT_DMA))
lypinator 0:bb348c97df44 2231 {
lypinator 0:bb348c97df44 2232 errorstate = SDMMC_CmdStopTransfer(hmmc->Instance);
lypinator 0:bb348c97df44 2233 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 2234 {
lypinator 0:bb348c97df44 2235 hmmc->ErrorCode |= errorstate;
lypinator 0:bb348c97df44 2236 HAL_MMC_ErrorCallback(hmmc);
lypinator 0:bb348c97df44 2237 }
lypinator 0:bb348c97df44 2238 }
lypinator 0:bb348c97df44 2239
lypinator 0:bb348c97df44 2240 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
lypinator 0:bb348c97df44 2241 in the MMC DCTRL register */
lypinator 0:bb348c97df44 2242 hmmc->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
lypinator 0:bb348c97df44 2243
lypinator 0:bb348c97df44 2244 /* Clear all the static flags */
lypinator 0:bb348c97df44 2245 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 2246
lypinator 0:bb348c97df44 2247 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 2248
lypinator 0:bb348c97df44 2249 HAL_MMC_RxCpltCallback(hmmc);
lypinator 0:bb348c97df44 2250 }
lypinator 0:bb348c97df44 2251
lypinator 0:bb348c97df44 2252 /**
lypinator 0:bb348c97df44 2253 * @brief DMA MMC communication error callback
lypinator 0:bb348c97df44 2254 * @param hdma DMA handle
lypinator 0:bb348c97df44 2255 * @retval None
lypinator 0:bb348c97df44 2256 */
lypinator 0:bb348c97df44 2257 static void MMC_DMAError(DMA_HandleTypeDef *hdma)
lypinator 0:bb348c97df44 2258 {
lypinator 0:bb348c97df44 2259 MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
lypinator 0:bb348c97df44 2260 HAL_MMC_CardStateTypeDef CardState;
lypinator 0:bb348c97df44 2261
lypinator 0:bb348c97df44 2262 if((hmmc->hdmarx->ErrorCode == HAL_DMA_ERROR_TE) || (hmmc->hdmatx->ErrorCode == HAL_DMA_ERROR_TE))
lypinator 0:bb348c97df44 2263 {
lypinator 0:bb348c97df44 2264 /* Clear All flags */
lypinator 0:bb348c97df44 2265 __HAL_MMC_CLEAR_FLAG(hmmc, SDIO_STATIC_FLAGS);
lypinator 0:bb348c97df44 2266
lypinator 0:bb348c97df44 2267 /* Disable All interrupts */
lypinator 0:bb348c97df44 2268 __HAL_MMC_DISABLE_IT(hmmc, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
lypinator 0:bb348c97df44 2269 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
lypinator 0:bb348c97df44 2270
lypinator 0:bb348c97df44 2271 hmmc->ErrorCode |= HAL_MMC_ERROR_DMA;
lypinator 0:bb348c97df44 2272 CardState = HAL_MMC_GetCardState(hmmc);
lypinator 0:bb348c97df44 2273 if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
lypinator 0:bb348c97df44 2274 {
lypinator 0:bb348c97df44 2275 hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
lypinator 0:bb348c97df44 2276 }
lypinator 0:bb348c97df44 2277
lypinator 0:bb348c97df44 2278 hmmc->State= HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 2279 }
lypinator 0:bb348c97df44 2280
lypinator 0:bb348c97df44 2281 HAL_MMC_ErrorCallback(hmmc);
lypinator 0:bb348c97df44 2282 }
lypinator 0:bb348c97df44 2283
lypinator 0:bb348c97df44 2284 /**
lypinator 0:bb348c97df44 2285 * @brief DMA MMC Tx Abort callback
lypinator 0:bb348c97df44 2286 * @param hdma DMA handle
lypinator 0:bb348c97df44 2287 * @retval None
lypinator 0:bb348c97df44 2288 */
lypinator 0:bb348c97df44 2289 static void MMC_DMATxAbort(DMA_HandleTypeDef *hdma)
lypinator 0:bb348c97df44 2290 {
lypinator 0:bb348c97df44 2291 MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
lypinator 0:bb348c97df44 2292 HAL_MMC_CardStateTypeDef CardState;
lypinator 0:bb348c97df44 2293
lypinator 0:bb348c97df44 2294 if(hmmc->hdmatx != NULL)
lypinator 0:bb348c97df44 2295 {
lypinator 0:bb348c97df44 2296 hmmc->hdmatx = NULL;
lypinator 0:bb348c97df44 2297 }
lypinator 0:bb348c97df44 2298
lypinator 0:bb348c97df44 2299 /* All DMA channels are aborted */
lypinator 0:bb348c97df44 2300 if(hmmc->hdmarx == NULL)
lypinator 0:bb348c97df44 2301 {
lypinator 0:bb348c97df44 2302 CardState = HAL_MMC_GetCardState(hmmc);
lypinator 0:bb348c97df44 2303 hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 2304 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 2305 if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
lypinator 0:bb348c97df44 2306 {
lypinator 0:bb348c97df44 2307 hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
lypinator 0:bb348c97df44 2308
lypinator 0:bb348c97df44 2309 if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 2310 {
lypinator 0:bb348c97df44 2311 HAL_MMC_AbortCallback(hmmc);
lypinator 0:bb348c97df44 2312 }
lypinator 0:bb348c97df44 2313 else
lypinator 0:bb348c97df44 2314 {
lypinator 0:bb348c97df44 2315 HAL_MMC_ErrorCallback(hmmc);
lypinator 0:bb348c97df44 2316 }
lypinator 0:bb348c97df44 2317 }
lypinator 0:bb348c97df44 2318 }
lypinator 0:bb348c97df44 2319 }
lypinator 0:bb348c97df44 2320
lypinator 0:bb348c97df44 2321 /**
lypinator 0:bb348c97df44 2322 * @brief DMA MMC Rx Abort callback
lypinator 0:bb348c97df44 2323 * @param hdma DMA handle
lypinator 0:bb348c97df44 2324 * @retval None
lypinator 0:bb348c97df44 2325 */
lypinator 0:bb348c97df44 2326 static void MMC_DMARxAbort(DMA_HandleTypeDef *hdma)
lypinator 0:bb348c97df44 2327 {
lypinator 0:bb348c97df44 2328 MMC_HandleTypeDef* hmmc = (MMC_HandleTypeDef* )(hdma->Parent);
lypinator 0:bb348c97df44 2329 HAL_MMC_CardStateTypeDef CardState;
lypinator 0:bb348c97df44 2330
lypinator 0:bb348c97df44 2331 if(hmmc->hdmarx != NULL)
lypinator 0:bb348c97df44 2332 {
lypinator 0:bb348c97df44 2333 hmmc->hdmarx = NULL;
lypinator 0:bb348c97df44 2334 }
lypinator 0:bb348c97df44 2335
lypinator 0:bb348c97df44 2336 /* All DMA channels are aborted */
lypinator 0:bb348c97df44 2337 if(hmmc->hdmatx == NULL)
lypinator 0:bb348c97df44 2338 {
lypinator 0:bb348c97df44 2339 CardState = HAL_MMC_GetCardState(hmmc);
lypinator 0:bb348c97df44 2340 hmmc->ErrorCode = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 2341 hmmc->State = HAL_MMC_STATE_READY;
lypinator 0:bb348c97df44 2342 if((CardState == HAL_MMC_CARD_RECEIVING) || (CardState == HAL_MMC_CARD_SENDING))
lypinator 0:bb348c97df44 2343 {
lypinator 0:bb348c97df44 2344 hmmc->ErrorCode |= SDMMC_CmdStopTransfer(hmmc->Instance);
lypinator 0:bb348c97df44 2345
lypinator 0:bb348c97df44 2346 if(hmmc->ErrorCode != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 2347 {
lypinator 0:bb348c97df44 2348 HAL_MMC_AbortCallback(hmmc);
lypinator 0:bb348c97df44 2349 }
lypinator 0:bb348c97df44 2350 else
lypinator 0:bb348c97df44 2351 {
lypinator 0:bb348c97df44 2352 HAL_MMC_ErrorCallback(hmmc);
lypinator 0:bb348c97df44 2353 }
lypinator 0:bb348c97df44 2354 }
lypinator 0:bb348c97df44 2355 }
lypinator 0:bb348c97df44 2356 }
lypinator 0:bb348c97df44 2357
lypinator 0:bb348c97df44 2358
lypinator 0:bb348c97df44 2359 /**
lypinator 0:bb348c97df44 2360 * @brief Initializes the mmc card.
lypinator 0:bb348c97df44 2361 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 2362 * @retval MMC Card error state
lypinator 0:bb348c97df44 2363 */
lypinator 0:bb348c97df44 2364 static uint32_t MMC_InitCard(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 2365 {
lypinator 0:bb348c97df44 2366 HAL_MMC_CardCSDTypeDef CSD;
lypinator 0:bb348c97df44 2367 uint32_t errorstate = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 2368 uint16_t mmc_rca = 1;
lypinator 0:bb348c97df44 2369
lypinator 0:bb348c97df44 2370 /* Check the power State */
lypinator 0:bb348c97df44 2371 if(SDIO_GetPowerState(hmmc->Instance) == 0U)
lypinator 0:bb348c97df44 2372 {
lypinator 0:bb348c97df44 2373 /* Power off */
lypinator 0:bb348c97df44 2374 return HAL_MMC_ERROR_REQUEST_NOT_APPLICABLE;
lypinator 0:bb348c97df44 2375 }
lypinator 0:bb348c97df44 2376
lypinator 0:bb348c97df44 2377 /* Send CMD2 ALL_SEND_CID */
lypinator 0:bb348c97df44 2378 errorstate = SDMMC_CmdSendCID(hmmc->Instance);
lypinator 0:bb348c97df44 2379 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 2380 {
lypinator 0:bb348c97df44 2381 return errorstate;
lypinator 0:bb348c97df44 2382 }
lypinator 0:bb348c97df44 2383 else
lypinator 0:bb348c97df44 2384 {
lypinator 0:bb348c97df44 2385 /* Get Card identification number data */
lypinator 0:bb348c97df44 2386 hmmc->CID[0U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
lypinator 0:bb348c97df44 2387 hmmc->CID[1U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP2);
lypinator 0:bb348c97df44 2388 hmmc->CID[2U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP3);
lypinator 0:bb348c97df44 2389 hmmc->CID[3U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP4);
lypinator 0:bb348c97df44 2390 }
lypinator 0:bb348c97df44 2391
lypinator 0:bb348c97df44 2392 /* Send CMD3 SET_REL_ADDR with argument 0 */
lypinator 0:bb348c97df44 2393 /* MMC Card publishes its RCA. */
lypinator 0:bb348c97df44 2394 errorstate = SDMMC_CmdSetRelAdd(hmmc->Instance, &mmc_rca);
lypinator 0:bb348c97df44 2395 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 2396 {
lypinator 0:bb348c97df44 2397 return errorstate;
lypinator 0:bb348c97df44 2398 }
lypinator 0:bb348c97df44 2399
lypinator 0:bb348c97df44 2400 /* Get the MMC card RCA */
lypinator 0:bb348c97df44 2401 hmmc->MmcCard.RelCardAdd = mmc_rca;
lypinator 0:bb348c97df44 2402
lypinator 0:bb348c97df44 2403 /* Send CMD9 SEND_CSD with argument as card's RCA */
lypinator 0:bb348c97df44 2404 errorstate = SDMMC_CmdSendCSD(hmmc->Instance, (uint32_t)(hmmc->MmcCard.RelCardAdd << 16U));
lypinator 0:bb348c97df44 2405 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 2406 {
lypinator 0:bb348c97df44 2407 return errorstate;
lypinator 0:bb348c97df44 2408 }
lypinator 0:bb348c97df44 2409 else
lypinator 0:bb348c97df44 2410 {
lypinator 0:bb348c97df44 2411 /* Get Card Specific Data */
lypinator 0:bb348c97df44 2412 hmmc->CSD[0U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
lypinator 0:bb348c97df44 2413 hmmc->CSD[1U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP2);
lypinator 0:bb348c97df44 2414 hmmc->CSD[2U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP3);
lypinator 0:bb348c97df44 2415 hmmc->CSD[3U] = SDIO_GetResponse(hmmc->Instance, SDIO_RESP4);
lypinator 0:bb348c97df44 2416 }
lypinator 0:bb348c97df44 2417
lypinator 0:bb348c97df44 2418 /* Get the Card Class */
lypinator 0:bb348c97df44 2419 hmmc->MmcCard.Class = (SDIO_GetResponse(hmmc->Instance, SDIO_RESP2) >> 20U);
lypinator 0:bb348c97df44 2420
lypinator 0:bb348c97df44 2421 /* Get CSD parameters */
lypinator 0:bb348c97df44 2422 HAL_MMC_GetCardCSD(hmmc, &CSD);
lypinator 0:bb348c97df44 2423
lypinator 0:bb348c97df44 2424 /* Select the Card */
lypinator 0:bb348c97df44 2425 errorstate = SDMMC_CmdSelDesel(hmmc->Instance, (uint32_t)(((uint32_t)hmmc->MmcCard.RelCardAdd) << 16U));
lypinator 0:bb348c97df44 2426 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 2427 {
lypinator 0:bb348c97df44 2428 return errorstate;
lypinator 0:bb348c97df44 2429 }
lypinator 0:bb348c97df44 2430
lypinator 0:bb348c97df44 2431 /* Configure SDIO peripheral interface */
lypinator 0:bb348c97df44 2432 SDIO_Init(hmmc->Instance, hmmc->Init);
lypinator 0:bb348c97df44 2433
lypinator 0:bb348c97df44 2434 /* All cards are initialized */
lypinator 0:bb348c97df44 2435 return HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 2436 }
lypinator 0:bb348c97df44 2437
lypinator 0:bb348c97df44 2438 /**
lypinator 0:bb348c97df44 2439 * @brief Enquires cards about their operating voltage and configures clock
lypinator 0:bb348c97df44 2440 * controls and stores MMC information that will be needed in future
lypinator 0:bb348c97df44 2441 * in the MMC handle.
lypinator 0:bb348c97df44 2442 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 2443 * @retval error state
lypinator 0:bb348c97df44 2444 */
lypinator 0:bb348c97df44 2445 static uint32_t MMC_PowerON(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 2446 {
lypinator 0:bb348c97df44 2447 __IO uint32_t count = 0U;
lypinator 0:bb348c97df44 2448 uint32_t response = 0U, validvoltage = 0U;
lypinator 0:bb348c97df44 2449 uint32_t errorstate = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 2450
lypinator 0:bb348c97df44 2451 /* CMD0: GO_IDLE_STATE */
lypinator 0:bb348c97df44 2452 errorstate = SDMMC_CmdGoIdleState(hmmc->Instance);
lypinator 0:bb348c97df44 2453 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 2454 {
lypinator 0:bb348c97df44 2455 return errorstate;
lypinator 0:bb348c97df44 2456 }
lypinator 0:bb348c97df44 2457
lypinator 0:bb348c97df44 2458 while(validvoltage == 0U)
lypinator 0:bb348c97df44 2459 {
lypinator 0:bb348c97df44 2460 if(count++ == SDMMC_MAX_VOLT_TRIAL)
lypinator 0:bb348c97df44 2461 {
lypinator 0:bb348c97df44 2462 return HAL_MMC_ERROR_INVALID_VOLTRANGE;
lypinator 0:bb348c97df44 2463 }
lypinator 0:bb348c97df44 2464
lypinator 0:bb348c97df44 2465 /* SEND CMD1 APP_CMD with MMC_HIGH_VOLTAGE_RANGE(0xC0FF8000) as argument */
lypinator 0:bb348c97df44 2466 errorstate = SDMMC_CmdOpCondition(hmmc->Instance, eMMC_HIGH_VOLTAGE_RANGE);
lypinator 0:bb348c97df44 2467 if(errorstate != HAL_MMC_ERROR_NONE)
lypinator 0:bb348c97df44 2468 {
lypinator 0:bb348c97df44 2469 return HAL_MMC_ERROR_UNSUPPORTED_FEATURE;
lypinator 0:bb348c97df44 2470 }
lypinator 0:bb348c97df44 2471
lypinator 0:bb348c97df44 2472 /* Get command response */
lypinator 0:bb348c97df44 2473 response = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
lypinator 0:bb348c97df44 2474
lypinator 0:bb348c97df44 2475 /* Get operating voltage*/
lypinator 0:bb348c97df44 2476 validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
lypinator 0:bb348c97df44 2477 }
lypinator 0:bb348c97df44 2478
lypinator 0:bb348c97df44 2479 /* When power routine is finished and command returns valid voltage */
lypinator 0:bb348c97df44 2480 if ((response & eMMC_HIGH_VOLTAGE_RANGE) == MMC_HIGH_VOLTAGE_RANGE)
lypinator 0:bb348c97df44 2481 {
lypinator 0:bb348c97df44 2482 /* When voltage range of the card is within 2.7V and 3.6V */
lypinator 0:bb348c97df44 2483 hmmc->MmcCard.CardType = MMC_HIGH_VOLTAGE_CARD;
lypinator 0:bb348c97df44 2484 }
lypinator 0:bb348c97df44 2485 else
lypinator 0:bb348c97df44 2486 {
lypinator 0:bb348c97df44 2487 /* When voltage range of the card is within 1.65V and 1.95V or 2.7V and 3.6V */
lypinator 0:bb348c97df44 2488 hmmc->MmcCard.CardType = MMC_DUAL_VOLTAGE_CARD;
lypinator 0:bb348c97df44 2489 }
lypinator 0:bb348c97df44 2490
lypinator 0:bb348c97df44 2491 return HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 2492 }
lypinator 0:bb348c97df44 2493
lypinator 0:bb348c97df44 2494 /**
lypinator 0:bb348c97df44 2495 * @brief Turns the SDIO output signals off.
lypinator 0:bb348c97df44 2496 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 2497 * @retval HAL status
lypinator 0:bb348c97df44 2498 */
lypinator 0:bb348c97df44 2499 static HAL_StatusTypeDef MMC_PowerOFF(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 2500 {
lypinator 0:bb348c97df44 2501 /* Set Power State to OFF */
lypinator 0:bb348c97df44 2502 SDIO_PowerState_OFF(hmmc->Instance);
lypinator 0:bb348c97df44 2503
lypinator 0:bb348c97df44 2504 return HAL_OK;
lypinator 0:bb348c97df44 2505 }
lypinator 0:bb348c97df44 2506
lypinator 0:bb348c97df44 2507 /**
lypinator 0:bb348c97df44 2508 * @brief Returns the current card's status.
lypinator 0:bb348c97df44 2509 * @param hmmc Pointer to MMC handle
lypinator 0:bb348c97df44 2510 * @param pCardStatus pointer to the buffer that will contain the MMC card
lypinator 0:bb348c97df44 2511 * status (Card Status register)
lypinator 0:bb348c97df44 2512 * @retval error state
lypinator 0:bb348c97df44 2513 */
lypinator 0:bb348c97df44 2514 static uint32_t MMC_SendStatus(MMC_HandleTypeDef *hmmc, uint32_t *pCardStatus)
lypinator 0:bb348c97df44 2515 {
lypinator 0:bb348c97df44 2516 uint32_t errorstate = HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 2517
lypinator 0:bb348c97df44 2518 if(pCardStatus == NULL)
lypinator 0:bb348c97df44 2519 {
lypinator 0:bb348c97df44 2520 return HAL_MMC_ERROR_PARAM;
lypinator 0:bb348c97df44 2521 }
lypinator 0:bb348c97df44 2522
lypinator 0:bb348c97df44 2523 /* Send Status command */
lypinator 0:bb348c97df44 2524 errorstate = SDMMC_CmdSendStatus(hmmc->Instance, (uint32_t)(hmmc->MmcCard.RelCardAdd << 16U));
lypinator 0:bb348c97df44 2525 if(errorstate != HAL_OK)
lypinator 0:bb348c97df44 2526 {
lypinator 0:bb348c97df44 2527 return errorstate;
lypinator 0:bb348c97df44 2528 }
lypinator 0:bb348c97df44 2529
lypinator 0:bb348c97df44 2530 /* Get MMC card status */
lypinator 0:bb348c97df44 2531 *pCardStatus = SDIO_GetResponse(hmmc->Instance, SDIO_RESP1);
lypinator 0:bb348c97df44 2532
lypinator 0:bb348c97df44 2533 return HAL_MMC_ERROR_NONE;
lypinator 0:bb348c97df44 2534 }
lypinator 0:bb348c97df44 2535
lypinator 0:bb348c97df44 2536 /**
lypinator 0:bb348c97df44 2537 * @brief Wrap up reading in non-blocking mode.
lypinator 0:bb348c97df44 2538 * @param hmmc pointer to a MMC_HandleTypeDef structure that contains
lypinator 0:bb348c97df44 2539 * the configuration information.
lypinator 0:bb348c97df44 2540 * @retval HAL status
lypinator 0:bb348c97df44 2541 */
lypinator 0:bb348c97df44 2542 static HAL_StatusTypeDef MMC_Read_IT(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 2543 {
lypinator 0:bb348c97df44 2544 uint32_t count = 0U;
lypinator 0:bb348c97df44 2545 uint32_t* tmp;
lypinator 0:bb348c97df44 2546
lypinator 0:bb348c97df44 2547 tmp = (uint32_t*)hmmc->pRxBuffPtr;
lypinator 0:bb348c97df44 2548
lypinator 0:bb348c97df44 2549 /* Read data from SDMMC Rx FIFO */
lypinator 0:bb348c97df44 2550 for(count = 0U; count < 8U; count++)
lypinator 0:bb348c97df44 2551 {
lypinator 0:bb348c97df44 2552 *(tmp + count) = SDIO_ReadFIFO(hmmc->Instance);
lypinator 0:bb348c97df44 2553 }
lypinator 0:bb348c97df44 2554
lypinator 0:bb348c97df44 2555 hmmc->pRxBuffPtr += 8U;
lypinator 0:bb348c97df44 2556
lypinator 0:bb348c97df44 2557 return HAL_OK;
lypinator 0:bb348c97df44 2558 }
lypinator 0:bb348c97df44 2559
lypinator 0:bb348c97df44 2560 /**
lypinator 0:bb348c97df44 2561 * @brief Wrap up writing in non-blocking mode.
lypinator 0:bb348c97df44 2562 * @param hmmc pointer to a MMC_HandleTypeDef structure that contains
lypinator 0:bb348c97df44 2563 * the configuration information.
lypinator 0:bb348c97df44 2564 * @retval HAL status
lypinator 0:bb348c97df44 2565 */
lypinator 0:bb348c97df44 2566 static HAL_StatusTypeDef MMC_Write_IT(MMC_HandleTypeDef *hmmc)
lypinator 0:bb348c97df44 2567 {
lypinator 0:bb348c97df44 2568 uint32_t count = 0U;
lypinator 0:bb348c97df44 2569 uint32_t* tmp;
lypinator 0:bb348c97df44 2570
lypinator 0:bb348c97df44 2571 tmp = (uint32_t*)hmmc->pTxBuffPtr;
lypinator 0:bb348c97df44 2572
lypinator 0:bb348c97df44 2573 /* Write data to SDMMC Tx FIFO */
lypinator 0:bb348c97df44 2574 for(count = 0U; count < 8U; count++)
lypinator 0:bb348c97df44 2575 {
lypinator 0:bb348c97df44 2576 SDIO_WriteFIFO(hmmc->Instance, (tmp + count));
lypinator 0:bb348c97df44 2577 }
lypinator 0:bb348c97df44 2578
lypinator 0:bb348c97df44 2579 hmmc->pTxBuffPtr += 8U;
lypinator 0:bb348c97df44 2580
lypinator 0:bb348c97df44 2581 return HAL_OK;
lypinator 0:bb348c97df44 2582 }
lypinator 0:bb348c97df44 2583
lypinator 0:bb348c97df44 2584 /**
lypinator 0:bb348c97df44 2585 * @}
lypinator 0:bb348c97df44 2586 */
lypinator 0:bb348c97df44 2587
lypinator 0:bb348c97df44 2588 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||
lypinator 0:bb348c97df44 2589 STM32F401xC || STM32F401xE || STM32F411xE || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx ||
lypinator 0:bb348c97df44 2590 STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */
lypinator 0:bb348c97df44 2591
lypinator 0:bb348c97df44 2592 #endif /* HAL_MMC_MODULE_ENABLED */
lypinator 0:bb348c97df44 2593
lypinator 0:bb348c97df44 2594 /**
lypinator 0:bb348c97df44 2595 * @}
lypinator 0:bb348c97df44 2596 */
lypinator 0:bb348c97df44 2597
lypinator 0:bb348c97df44 2598 /**
lypinator 0:bb348c97df44 2599 * @}
lypinator 0:bb348c97df44 2600 */
lypinator 0:bb348c97df44 2601
lypinator 0:bb348c97df44 2602 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/