code

Fork of BSP_DISCO_F746NG by ST

Committer:
shoaib_ahmed
Date:
Sun Jul 30 16:14:12 2017 +0000
Revision:
10:040ff14a2358
Parent:
1:ee089790cdbb
mbed core for link sprite camera

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 1:ee089790cdbb 1 /**
bcostm 1:ee089790cdbb 2 ******************************************************************************
bcostm 1:ee089790cdbb 3 * @file stm32746g_discovery_sd.c
bcostm 1:ee089790cdbb 4 * @author MCD Application Team
bcostm 1:ee089790cdbb 5 * @version V1.0.0
bcostm 1:ee089790cdbb 6 * @date 25-June-2015
bcostm 1:ee089790cdbb 7 * @brief This file includes the uSD card driver mounted on STM32746G-Discovery
bcostm 1:ee089790cdbb 8 * board.
bcostm 1:ee089790cdbb 9 @verbatim
bcostm 1:ee089790cdbb 10 1. How To use this driver:
bcostm 1:ee089790cdbb 11 --------------------------
bcostm 1:ee089790cdbb 12 - This driver is used to drive the micro SD external card mounted on STM32746G-Discovery
bcostm 1:ee089790cdbb 13 board.
bcostm 1:ee089790cdbb 14 - This driver does not need a specific component driver for the micro SD device
bcostm 1:ee089790cdbb 15 to be included with.
bcostm 1:ee089790cdbb 16
bcostm 1:ee089790cdbb 17 2. Driver description:
bcostm 1:ee089790cdbb 18 ---------------------
bcostm 1:ee089790cdbb 19 + Initialization steps:
bcostm 1:ee089790cdbb 20 o Initialize the micro SD card using the BSP_SD_Init() function. This
bcostm 1:ee089790cdbb 21 function includes the MSP layer hardware resources initialization and the
bcostm 1:ee089790cdbb 22 SDIO interface configuration to interface with the external micro SD. It
bcostm 1:ee089790cdbb 23 also includes the micro SD initialization sequence.
bcostm 1:ee089790cdbb 24 o To check the SD card presence you can use the function BSP_SD_IsDetected() which
bcostm 1:ee089790cdbb 25 returns the detection status
bcostm 1:ee089790cdbb 26 o If SD presence detection interrupt mode is desired, you must configure the
bcostm 1:ee089790cdbb 27 SD detection interrupt mode by calling the function BSP_SD_ITConfig(). The interrupt
bcostm 1:ee089790cdbb 28 is generated as an external interrupt whenever the micro SD card is
bcostm 1:ee089790cdbb 29 plugged/unplugged in/from the board.
bcostm 1:ee089790cdbb 30 o The function BSP_SD_GetCardInfo() is used to get the micro SD card information
bcostm 1:ee089790cdbb 31 which is stored in the structure "HAL_SD_CardInfoTypedef".
bcostm 1:ee089790cdbb 32
bcostm 1:ee089790cdbb 33 + Micro SD card operations
bcostm 1:ee089790cdbb 34 o The micro SD card can be accessed with read/write block(s) operations once
bcostm 1:ee089790cdbb 35 it is ready for access. The access can be performed whether using the polling
bcostm 1:ee089790cdbb 36 mode by calling the functions BSP_SD_ReadBlocks()/BSP_SD_WriteBlocks(), or by DMA
bcostm 1:ee089790cdbb 37 transfer using the functions BSP_SD_ReadBlocks_DMA()/BSP_SD_WriteBlocks_DMA()
bcostm 1:ee089790cdbb 38 o The DMA transfer complete is used with interrupt mode. Once the SD transfer
bcostm 1:ee089790cdbb 39 is complete, the SD interrupt is handled using the function BSP_SD_IRQHandler(),
bcostm 1:ee089790cdbb 40 the DMA Tx/Rx transfer complete are handled using the functions
bcostm 1:ee089790cdbb 41 BSP_SD_DMA_Tx_IRQHandler()/BSP_SD_DMA_Rx_IRQHandler(). The corresponding user callbacks
bcostm 1:ee089790cdbb 42 are implemented by the user at application level.
bcostm 1:ee089790cdbb 43 o The SD erase block(s) is performed using the function BSP_SD_Erase() with specifying
bcostm 1:ee089790cdbb 44 the number of blocks to erase.
bcostm 1:ee089790cdbb 45 o The SD runtime status is returned when calling the function BSP_SD_GetStatus().
bcostm 1:ee089790cdbb 46 @endverbatim
bcostm 1:ee089790cdbb 47 ******************************************************************************
bcostm 1:ee089790cdbb 48 * @attention
bcostm 1:ee089790cdbb 49 *
bcostm 1:ee089790cdbb 50 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
bcostm 1:ee089790cdbb 51 *
bcostm 1:ee089790cdbb 52 * Redistribution and use in source and binary forms, with or without modification,
bcostm 1:ee089790cdbb 53 * are permitted provided that the following conditions are met:
bcostm 1:ee089790cdbb 54 * 1. Redistributions of source code must retain the above copyright notice,
bcostm 1:ee089790cdbb 55 * this list of conditions and the following disclaimer.
bcostm 1:ee089790cdbb 56 * 2. Redistributions in binary form must reproduce the above copyright notice,
bcostm 1:ee089790cdbb 57 * this list of conditions and the following disclaimer in the documentation
bcostm 1:ee089790cdbb 58 * and/or other materials provided with the distribution.
bcostm 1:ee089790cdbb 59 * 3. Neither the name of STMicroelectronics nor the names of its contributors
bcostm 1:ee089790cdbb 60 * may be used to endorse or promote products derived from this software
bcostm 1:ee089790cdbb 61 * without specific prior written permission.
bcostm 1:ee089790cdbb 62 *
bcostm 1:ee089790cdbb 63 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
bcostm 1:ee089790cdbb 64 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
bcostm 1:ee089790cdbb 65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
bcostm 1:ee089790cdbb 66 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
bcostm 1:ee089790cdbb 67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
bcostm 1:ee089790cdbb 68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
bcostm 1:ee089790cdbb 69 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
bcostm 1:ee089790cdbb 70 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
bcostm 1:ee089790cdbb 71 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
bcostm 1:ee089790cdbb 72 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bcostm 1:ee089790cdbb 73 *
bcostm 1:ee089790cdbb 74 ******************************************************************************
bcostm 1:ee089790cdbb 75 */
bcostm 1:ee089790cdbb 76
bcostm 1:ee089790cdbb 77 /* Includes ------------------------------------------------------------------*/
bcostm 1:ee089790cdbb 78 #include "stm32746g_discovery_sd.h"
bcostm 1:ee089790cdbb 79
bcostm 1:ee089790cdbb 80 /** @addtogroup BSP
bcostm 1:ee089790cdbb 81 * @{
bcostm 1:ee089790cdbb 82 */
bcostm 1:ee089790cdbb 83
bcostm 1:ee089790cdbb 84 /** @addtogroup STM32746G_DISCOVERY
bcostm 1:ee089790cdbb 85 * @{
bcostm 1:ee089790cdbb 86 */
bcostm 1:ee089790cdbb 87
bcostm 1:ee089790cdbb 88 /** @defgroup STM32746G_DISCOVERY_SD STM32746G_DISCOVERY_SD
bcostm 1:ee089790cdbb 89 * @{
bcostm 1:ee089790cdbb 90 */
bcostm 1:ee089790cdbb 91
bcostm 1:ee089790cdbb 92
bcostm 1:ee089790cdbb 93 /** @defgroup STM32746G_DISCOVERY_SD_Private_TypesDefinitions STM32746G_DISCOVERY_SD Private Types Definitions
bcostm 1:ee089790cdbb 94 * @{
bcostm 1:ee089790cdbb 95 */
bcostm 1:ee089790cdbb 96 /**
bcostm 1:ee089790cdbb 97 * @}
bcostm 1:ee089790cdbb 98 */
bcostm 1:ee089790cdbb 99
bcostm 1:ee089790cdbb 100 /** @defgroup STM32746G_DISCOVERY_SD_Private_Defines STM32746G_DISCOVERY_SD Private Defines
bcostm 1:ee089790cdbb 101 * @{
bcostm 1:ee089790cdbb 102 */
bcostm 1:ee089790cdbb 103 /**
bcostm 1:ee089790cdbb 104 * @}
bcostm 1:ee089790cdbb 105 */
bcostm 1:ee089790cdbb 106
bcostm 1:ee089790cdbb 107 /** @defgroup STM32746G_DISCOVERY_SD_Private_Macros STM32746G_DISCOVERY_SD Private Macros
bcostm 1:ee089790cdbb 108 * @{
bcostm 1:ee089790cdbb 109 */
bcostm 1:ee089790cdbb 110 /**
bcostm 1:ee089790cdbb 111 * @}
bcostm 1:ee089790cdbb 112 */
bcostm 1:ee089790cdbb 113
bcostm 1:ee089790cdbb 114 /** @defgroup STM32746G_DISCOVERY_SD_Private_Variables STM32746G_DISCOVERY_SD Private Variables
bcostm 1:ee089790cdbb 115 * @{
bcostm 1:ee089790cdbb 116 */
bcostm 1:ee089790cdbb 117 static SD_HandleTypeDef uSdHandle;
bcostm 1:ee089790cdbb 118 static SD_CardInfo uSdCardInfo;
bcostm 1:ee089790cdbb 119
bcostm 1:ee089790cdbb 120 /**
bcostm 1:ee089790cdbb 121 * @}
bcostm 1:ee089790cdbb 122 */
bcostm 1:ee089790cdbb 123
bcostm 1:ee089790cdbb 124 /** @defgroup STM32746G_DISCOVERY_SD_Private_FunctionPrototypes STM32746G_DISCOVERY_SD Private Function Prototypes
bcostm 1:ee089790cdbb 125 * @{
bcostm 1:ee089790cdbb 126 */
bcostm 1:ee089790cdbb 127 /**
bcostm 1:ee089790cdbb 128 * @}
bcostm 1:ee089790cdbb 129 */
bcostm 1:ee089790cdbb 130
bcostm 1:ee089790cdbb 131 /** @defgroup STM32746G_DISCOVERY_SD_Exported_Functions STM32746G_DISCOVERY_SD Exported Functions
bcostm 1:ee089790cdbb 132 * @{
bcostm 1:ee089790cdbb 133 */
bcostm 1:ee089790cdbb 134
bcostm 1:ee089790cdbb 135 /**
bcostm 1:ee089790cdbb 136 * @brief Initializes the SD card device.
bcostm 1:ee089790cdbb 137 * @retval SD status
bcostm 1:ee089790cdbb 138 */
bcostm 1:ee089790cdbb 139 uint8_t BSP_SD_Init(void)
bcostm 1:ee089790cdbb 140 {
bcostm 1:ee089790cdbb 141 uint8_t sd_state = MSD_OK;
bcostm 1:ee089790cdbb 142
bcostm 1:ee089790cdbb 143 /* uSD device interface configuration */
bcostm 1:ee089790cdbb 144 uSdHandle.Instance = SDMMC1;
bcostm 1:ee089790cdbb 145
bcostm 1:ee089790cdbb 146 uSdHandle.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
bcostm 1:ee089790cdbb 147 uSdHandle.Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
bcostm 1:ee089790cdbb 148 uSdHandle.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
bcostm 1:ee089790cdbb 149 uSdHandle.Init.BusWide = SDMMC_BUS_WIDE_1B;
bcostm 1:ee089790cdbb 150 uSdHandle.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
bcostm 1:ee089790cdbb 151 uSdHandle.Init.ClockDiv = SDMMC_TRANSFER_CLK_DIV;
bcostm 1:ee089790cdbb 152
bcostm 1:ee089790cdbb 153 /* Msp SD Detect pin initialization */
bcostm 1:ee089790cdbb 154 BSP_SD_Detect_MspInit(&uSdHandle, NULL);
bcostm 1:ee089790cdbb 155 if(BSP_SD_IsDetected() != SD_PRESENT) /* Check if SD card is present */
bcostm 1:ee089790cdbb 156 {
bcostm 1:ee089790cdbb 157 return MSD_ERROR_SD_NOT_PRESENT;
bcostm 1:ee089790cdbb 158 }
bcostm 1:ee089790cdbb 159
bcostm 1:ee089790cdbb 160 /* Msp SD initialization */
bcostm 1:ee089790cdbb 161 BSP_SD_MspInit(&uSdHandle, NULL);
bcostm 1:ee089790cdbb 162
bcostm 1:ee089790cdbb 163 /* HAL SD initialization */
bcostm 1:ee089790cdbb 164 if(HAL_SD_Init(&uSdHandle, &uSdCardInfo) != SD_OK)
bcostm 1:ee089790cdbb 165 {
bcostm 1:ee089790cdbb 166 sd_state = MSD_ERROR;
bcostm 1:ee089790cdbb 167 }
bcostm 1:ee089790cdbb 168
bcostm 1:ee089790cdbb 169 /* Configure SD Bus width */
bcostm 1:ee089790cdbb 170 if(sd_state == MSD_OK)
bcostm 1:ee089790cdbb 171 {
bcostm 1:ee089790cdbb 172 /* Enable wide operation */
bcostm 1:ee089790cdbb 173 if(HAL_SD_WideBusOperation_Config(&uSdHandle, SDMMC_BUS_WIDE_4B) != SD_OK)
bcostm 1:ee089790cdbb 174 {
bcostm 1:ee089790cdbb 175 sd_state = MSD_ERROR;
bcostm 1:ee089790cdbb 176 }
bcostm 1:ee089790cdbb 177 else
bcostm 1:ee089790cdbb 178 {
bcostm 1:ee089790cdbb 179 sd_state = MSD_OK;
bcostm 1:ee089790cdbb 180 }
bcostm 1:ee089790cdbb 181 }
bcostm 1:ee089790cdbb 182
bcostm 1:ee089790cdbb 183 return sd_state;
bcostm 1:ee089790cdbb 184 }
bcostm 1:ee089790cdbb 185
bcostm 1:ee089790cdbb 186 /**
bcostm 1:ee089790cdbb 187 * @brief DeInitializes the SD card device.
bcostm 1:ee089790cdbb 188 * @retval SD status
bcostm 1:ee089790cdbb 189 */
bcostm 1:ee089790cdbb 190 uint8_t BSP_SD_DeInit(void)
bcostm 1:ee089790cdbb 191 {
bcostm 1:ee089790cdbb 192 uint8_t sd_state = MSD_OK;
bcostm 1:ee089790cdbb 193
bcostm 1:ee089790cdbb 194 uSdHandle.Instance = SDMMC1;
bcostm 1:ee089790cdbb 195
bcostm 1:ee089790cdbb 196 /* HAL SD deinitialization */
bcostm 1:ee089790cdbb 197 if(HAL_SD_DeInit(&uSdHandle) != HAL_OK)
bcostm 1:ee089790cdbb 198 {
bcostm 1:ee089790cdbb 199 sd_state = MSD_ERROR;
bcostm 1:ee089790cdbb 200 }
bcostm 1:ee089790cdbb 201
bcostm 1:ee089790cdbb 202 /* Msp SD deinitialization */
bcostm 1:ee089790cdbb 203 uSdHandle.Instance = SDMMC1;
bcostm 1:ee089790cdbb 204 BSP_SD_MspDeInit(&uSdHandle, NULL);
bcostm 1:ee089790cdbb 205
bcostm 1:ee089790cdbb 206 return sd_state;
bcostm 1:ee089790cdbb 207 }
bcostm 1:ee089790cdbb 208
bcostm 1:ee089790cdbb 209 /**
bcostm 1:ee089790cdbb 210 * @brief Configures Interrupt mode for SD detection pin.
bcostm 1:ee089790cdbb 211 * @retval Returns MSD_OK
bcostm 1:ee089790cdbb 212 */
bcostm 1:ee089790cdbb 213 uint8_t BSP_SD_ITConfig(void)
bcostm 1:ee089790cdbb 214 {
bcostm 1:ee089790cdbb 215 GPIO_InitTypeDef gpio_init_structure;
bcostm 1:ee089790cdbb 216
bcostm 1:ee089790cdbb 217 /* Configure Interrupt mode for SD detection pin */
bcostm 1:ee089790cdbb 218 gpio_init_structure.Pin = SD_DETECT_PIN;
bcostm 1:ee089790cdbb 219 gpio_init_structure.Pull = GPIO_PULLUP;
bcostm 1:ee089790cdbb 220 gpio_init_structure.Speed = GPIO_SPEED_FAST;
bcostm 1:ee089790cdbb 221 gpio_init_structure.Mode = GPIO_MODE_IT_RISING_FALLING;
bcostm 1:ee089790cdbb 222 HAL_GPIO_Init(SD_DETECT_GPIO_PORT, &gpio_init_structure);
bcostm 1:ee089790cdbb 223
bcostm 1:ee089790cdbb 224 /* Enable and set SD detect EXTI Interrupt to the lowest priority */
bcostm 1:ee089790cdbb 225 HAL_NVIC_SetPriority((IRQn_Type)(SD_DETECT_EXTI_IRQn), 0x0F, 0x00);
bcostm 1:ee089790cdbb 226 HAL_NVIC_EnableIRQ((IRQn_Type)(SD_DETECT_EXTI_IRQn));
bcostm 1:ee089790cdbb 227
bcostm 1:ee089790cdbb 228 return MSD_OK;
bcostm 1:ee089790cdbb 229 }
bcostm 1:ee089790cdbb 230
bcostm 1:ee089790cdbb 231 /**
bcostm 1:ee089790cdbb 232 * @brief Detects if SD card is correctly plugged in the memory slot or not.
bcostm 1:ee089790cdbb 233 * @retval Returns if SD is detected or not
bcostm 1:ee089790cdbb 234 */
bcostm 1:ee089790cdbb 235 uint8_t BSP_SD_IsDetected(void)
bcostm 1:ee089790cdbb 236 {
bcostm 1:ee089790cdbb 237 __IO uint8_t status = SD_PRESENT;
bcostm 1:ee089790cdbb 238
bcostm 1:ee089790cdbb 239 /* Check SD card detect pin */
bcostm 1:ee089790cdbb 240 if (HAL_GPIO_ReadPin(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) == GPIO_PIN_SET)
bcostm 1:ee089790cdbb 241 {
bcostm 1:ee089790cdbb 242 status = SD_NOT_PRESENT;
bcostm 1:ee089790cdbb 243 }
bcostm 1:ee089790cdbb 244
bcostm 1:ee089790cdbb 245 return status;
bcostm 1:ee089790cdbb 246 }
bcostm 1:ee089790cdbb 247
bcostm 1:ee089790cdbb 248 /**
bcostm 1:ee089790cdbb 249 * @brief Reads block(s) from a specified address in an SD card, in polling mode.
bcostm 1:ee089790cdbb 250 * @param pData: Pointer to the buffer that will contain the data to transmit
bcostm 1:ee089790cdbb 251 * @param ReadAddr: Address from where data is to be read
bcostm 1:ee089790cdbb 252 * @param BlockSize: SD card data block size, that should be 512
bcostm 1:ee089790cdbb 253 * @param NumOfBlocks: Number of SD blocks to read
bcostm 1:ee089790cdbb 254 * @retval SD status
bcostm 1:ee089790cdbb 255 */
bcostm 1:ee089790cdbb 256 uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumOfBlocks)
bcostm 1:ee089790cdbb 257 {
bcostm 1:ee089790cdbb 258 if(HAL_SD_ReadBlocks(&uSdHandle, pData, ReadAddr, BlockSize, NumOfBlocks) != SD_OK)
bcostm 1:ee089790cdbb 259 {
bcostm 1:ee089790cdbb 260 return MSD_ERROR;
bcostm 1:ee089790cdbb 261 }
bcostm 1:ee089790cdbb 262 else
bcostm 1:ee089790cdbb 263 {
bcostm 1:ee089790cdbb 264 return MSD_OK;
bcostm 1:ee089790cdbb 265 }
bcostm 1:ee089790cdbb 266 }
bcostm 1:ee089790cdbb 267
bcostm 1:ee089790cdbb 268 /**
bcostm 1:ee089790cdbb 269 * @brief Writes block(s) to a specified address in an SD card, in polling mode.
bcostm 1:ee089790cdbb 270 * @param pData: Pointer to the buffer that will contain the data to transmit
bcostm 1:ee089790cdbb 271 * @param WriteAddr: Address from where data is to be written
bcostm 1:ee089790cdbb 272 * @param BlockSize: SD card data block size, that should be 512
bcostm 1:ee089790cdbb 273 * @param NumOfBlocks: Number of SD blocks to write
bcostm 1:ee089790cdbb 274 * @retval SD status
bcostm 1:ee089790cdbb 275 */
bcostm 1:ee089790cdbb 276 uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumOfBlocks)
bcostm 1:ee089790cdbb 277 {
bcostm 1:ee089790cdbb 278 if(HAL_SD_WriteBlocks(&uSdHandle, pData, WriteAddr, BlockSize, NumOfBlocks) != SD_OK)
bcostm 1:ee089790cdbb 279 {
bcostm 1:ee089790cdbb 280 return MSD_ERROR;
bcostm 1:ee089790cdbb 281 }
bcostm 1:ee089790cdbb 282 else
bcostm 1:ee089790cdbb 283 {
bcostm 1:ee089790cdbb 284 return MSD_OK;
bcostm 1:ee089790cdbb 285 }
bcostm 1:ee089790cdbb 286 }
bcostm 1:ee089790cdbb 287
bcostm 1:ee089790cdbb 288 /**
bcostm 1:ee089790cdbb 289 * @brief Reads block(s) from a specified address in an SD card, in DMA mode.
bcostm 1:ee089790cdbb 290 * @param pData: Pointer to the buffer that will contain the data to transmit
bcostm 1:ee089790cdbb 291 * @param ReadAddr: Address from where data is to be read
bcostm 1:ee089790cdbb 292 * @param BlockSize: SD card data block size, that should be 512
bcostm 1:ee089790cdbb 293 * @param NumOfBlocks: Number of SD blocks to read
bcostm 1:ee089790cdbb 294 * @retval SD status
bcostm 1:ee089790cdbb 295 */
bcostm 1:ee089790cdbb 296 uint8_t BSP_SD_ReadBlocks_DMA(uint32_t *pData, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumOfBlocks)
bcostm 1:ee089790cdbb 297 {
bcostm 1:ee089790cdbb 298 uint8_t sd_state = MSD_OK;
bcostm 1:ee089790cdbb 299
bcostm 1:ee089790cdbb 300 /* Read block(s) in DMA transfer mode */
bcostm 1:ee089790cdbb 301 if(HAL_SD_ReadBlocks_DMA(&uSdHandle, pData, ReadAddr, BlockSize, NumOfBlocks) != SD_OK)
bcostm 1:ee089790cdbb 302 {
bcostm 1:ee089790cdbb 303 sd_state = MSD_ERROR;
bcostm 1:ee089790cdbb 304 }
bcostm 1:ee089790cdbb 305
bcostm 1:ee089790cdbb 306 /* Wait until transfer is complete */
bcostm 1:ee089790cdbb 307 if(sd_state == MSD_OK)
bcostm 1:ee089790cdbb 308 {
bcostm 1:ee089790cdbb 309 if(HAL_SD_CheckReadOperation(&uSdHandle, (uint32_t)SD_DATATIMEOUT) != SD_OK)
bcostm 1:ee089790cdbb 310 {
bcostm 1:ee089790cdbb 311 sd_state = MSD_ERROR;
bcostm 1:ee089790cdbb 312 }
bcostm 1:ee089790cdbb 313 else
bcostm 1:ee089790cdbb 314 {
bcostm 1:ee089790cdbb 315 sd_state = MSD_OK;
bcostm 1:ee089790cdbb 316 }
bcostm 1:ee089790cdbb 317 }
bcostm 1:ee089790cdbb 318
bcostm 1:ee089790cdbb 319 return sd_state;
bcostm 1:ee089790cdbb 320 }
bcostm 1:ee089790cdbb 321
bcostm 1:ee089790cdbb 322 /**
bcostm 1:ee089790cdbb 323 * @brief Writes block(s) to a specified address in an SD card, in DMA mode.
bcostm 1:ee089790cdbb 324 * @param pData: Pointer to the buffer that will contain the data to transmit
bcostm 1:ee089790cdbb 325 * @param WriteAddr: Address from where data is to be written
bcostm 1:ee089790cdbb 326 * @param BlockSize: SD card data block size, that should be 512
bcostm 1:ee089790cdbb 327 * @param NumOfBlocks: Number of SD blocks to write
bcostm 1:ee089790cdbb 328 * @retval SD status
bcostm 1:ee089790cdbb 329 */
bcostm 1:ee089790cdbb 330 uint8_t BSP_SD_WriteBlocks_DMA(uint32_t *pData, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumOfBlocks)
bcostm 1:ee089790cdbb 331 {
bcostm 1:ee089790cdbb 332 uint8_t sd_state = MSD_OK;
bcostm 1:ee089790cdbb 333
bcostm 1:ee089790cdbb 334 /* Write block(s) in DMA transfer mode */
bcostm 1:ee089790cdbb 335 if(HAL_SD_WriteBlocks_DMA(&uSdHandle, pData, WriteAddr, BlockSize, NumOfBlocks) != SD_OK)
bcostm 1:ee089790cdbb 336 {
bcostm 1:ee089790cdbb 337 sd_state = MSD_ERROR;
bcostm 1:ee089790cdbb 338 }
bcostm 1:ee089790cdbb 339
bcostm 1:ee089790cdbb 340 /* Wait until transfer is complete */
bcostm 1:ee089790cdbb 341 if(sd_state == MSD_OK)
bcostm 1:ee089790cdbb 342 {
bcostm 1:ee089790cdbb 343 if(HAL_SD_CheckWriteOperation(&uSdHandle, (uint32_t)SD_DATATIMEOUT) != SD_OK)
bcostm 1:ee089790cdbb 344 {
bcostm 1:ee089790cdbb 345 sd_state = MSD_ERROR;
bcostm 1:ee089790cdbb 346 }
bcostm 1:ee089790cdbb 347 else
bcostm 1:ee089790cdbb 348 {
bcostm 1:ee089790cdbb 349 sd_state = MSD_OK;
bcostm 1:ee089790cdbb 350 }
bcostm 1:ee089790cdbb 351 }
bcostm 1:ee089790cdbb 352
bcostm 1:ee089790cdbb 353 return sd_state;
bcostm 1:ee089790cdbb 354 }
bcostm 1:ee089790cdbb 355
bcostm 1:ee089790cdbb 356 /**
bcostm 1:ee089790cdbb 357 * @brief Erases the specified memory area of the given SD card.
bcostm 1:ee089790cdbb 358 * @param StartAddr: Start byte address
bcostm 1:ee089790cdbb 359 * @param EndAddr: End byte address
bcostm 1:ee089790cdbb 360 * @retval SD status
bcostm 1:ee089790cdbb 361 */
bcostm 1:ee089790cdbb 362 uint8_t BSP_SD_Erase(uint64_t StartAddr, uint64_t EndAddr)
bcostm 1:ee089790cdbb 363 {
bcostm 1:ee089790cdbb 364 if(HAL_SD_Erase(&uSdHandle, StartAddr, EndAddr) != SD_OK)
bcostm 1:ee089790cdbb 365 {
bcostm 1:ee089790cdbb 366 return MSD_ERROR;
bcostm 1:ee089790cdbb 367 }
bcostm 1:ee089790cdbb 368 else
bcostm 1:ee089790cdbb 369 {
bcostm 1:ee089790cdbb 370 return MSD_OK;
bcostm 1:ee089790cdbb 371 }
bcostm 1:ee089790cdbb 372 }
bcostm 1:ee089790cdbb 373
bcostm 1:ee089790cdbb 374 /**
bcostm 1:ee089790cdbb 375 * @brief Initializes the SD MSP.
bcostm 1:ee089790cdbb 376 * @param hsd: SD handle
bcostm 1:ee089790cdbb 377 * @param Params
bcostm 1:ee089790cdbb 378 * @retval None
bcostm 1:ee089790cdbb 379 */
bcostm 1:ee089790cdbb 380 __weak void BSP_SD_MspInit(SD_HandleTypeDef *hsd, void *Params)
bcostm 1:ee089790cdbb 381 {
bcostm 1:ee089790cdbb 382 static DMA_HandleTypeDef dma_rx_handle;
bcostm 1:ee089790cdbb 383 static DMA_HandleTypeDef dma_tx_handle;
bcostm 1:ee089790cdbb 384 GPIO_InitTypeDef gpio_init_structure;
bcostm 1:ee089790cdbb 385
bcostm 1:ee089790cdbb 386 /* Enable SDIO clock */
bcostm 1:ee089790cdbb 387 __HAL_RCC_SDMMC1_CLK_ENABLE();
bcostm 1:ee089790cdbb 388
bcostm 1:ee089790cdbb 389 /* Enable DMA2 clocks */
bcostm 1:ee089790cdbb 390 __DMAx_TxRx_CLK_ENABLE();
bcostm 1:ee089790cdbb 391
bcostm 1:ee089790cdbb 392 /* Enable GPIOs clock */
bcostm 1:ee089790cdbb 393 __HAL_RCC_GPIOC_CLK_ENABLE();
bcostm 1:ee089790cdbb 394 __HAL_RCC_GPIOD_CLK_ENABLE();
bcostm 1:ee089790cdbb 395
bcostm 1:ee089790cdbb 396 /* Common GPIO configuration */
bcostm 1:ee089790cdbb 397 gpio_init_structure.Mode = GPIO_MODE_AF_PP;
bcostm 1:ee089790cdbb 398 gpio_init_structure.Pull = GPIO_PULLUP;
bcostm 1:ee089790cdbb 399 gpio_init_structure.Speed = GPIO_SPEED_HIGH;
bcostm 1:ee089790cdbb 400 gpio_init_structure.Alternate = GPIO_AF12_SDMMC1;
bcostm 1:ee089790cdbb 401
bcostm 1:ee089790cdbb 402 /* GPIOC configuration */
bcostm 1:ee089790cdbb 403 gpio_init_structure.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
bcostm 1:ee089790cdbb 404 HAL_GPIO_Init(GPIOC, &gpio_init_structure);
bcostm 1:ee089790cdbb 405
bcostm 1:ee089790cdbb 406 /* GPIOD configuration */
bcostm 1:ee089790cdbb 407 gpio_init_structure.Pin = GPIO_PIN_2;
bcostm 1:ee089790cdbb 408 HAL_GPIO_Init(GPIOD, &gpio_init_structure);
bcostm 1:ee089790cdbb 409
bcostm 1:ee089790cdbb 410 /* NVIC configuration for SDIO interrupts */
bcostm 1:ee089790cdbb 411 HAL_NVIC_SetPriority(SDMMC1_IRQn, 5, 0);
bcostm 1:ee089790cdbb 412 HAL_NVIC_EnableIRQ(SDMMC1_IRQn);
bcostm 1:ee089790cdbb 413
bcostm 1:ee089790cdbb 414 /* Configure DMA Rx parameters */
bcostm 1:ee089790cdbb 415 dma_rx_handle.Init.Channel = SD_DMAx_Rx_CHANNEL;
bcostm 1:ee089790cdbb 416 dma_rx_handle.Init.Direction = DMA_PERIPH_TO_MEMORY;
bcostm 1:ee089790cdbb 417 dma_rx_handle.Init.PeriphInc = DMA_PINC_DISABLE;
bcostm 1:ee089790cdbb 418 dma_rx_handle.Init.MemInc = DMA_MINC_ENABLE;
bcostm 1:ee089790cdbb 419 dma_rx_handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
bcostm 1:ee089790cdbb 420 dma_rx_handle.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
bcostm 1:ee089790cdbb 421 dma_rx_handle.Init.Mode = DMA_PFCTRL;
bcostm 1:ee089790cdbb 422 dma_rx_handle.Init.Priority = DMA_PRIORITY_VERY_HIGH;
bcostm 1:ee089790cdbb 423 dma_rx_handle.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
bcostm 1:ee089790cdbb 424 dma_rx_handle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
bcostm 1:ee089790cdbb 425 dma_rx_handle.Init.MemBurst = DMA_MBURST_INC4;
bcostm 1:ee089790cdbb 426 dma_rx_handle.Init.PeriphBurst = DMA_PBURST_INC4;
bcostm 1:ee089790cdbb 427
bcostm 1:ee089790cdbb 428 dma_rx_handle.Instance = SD_DMAx_Rx_STREAM;
bcostm 1:ee089790cdbb 429
bcostm 1:ee089790cdbb 430 /* Associate the DMA handle */
bcostm 1:ee089790cdbb 431 __HAL_LINKDMA(hsd, hdmarx, dma_rx_handle);
bcostm 1:ee089790cdbb 432
bcostm 1:ee089790cdbb 433 /* Deinitialize the stream for new transfer */
bcostm 1:ee089790cdbb 434 HAL_DMA_DeInit(&dma_rx_handle);
bcostm 1:ee089790cdbb 435
bcostm 1:ee089790cdbb 436 /* Configure the DMA stream */
bcostm 1:ee089790cdbb 437 HAL_DMA_Init(&dma_rx_handle);
bcostm 1:ee089790cdbb 438
bcostm 1:ee089790cdbb 439 /* Configure DMA Tx parameters */
bcostm 1:ee089790cdbb 440 dma_tx_handle.Init.Channel = SD_DMAx_Tx_CHANNEL;
bcostm 1:ee089790cdbb 441 dma_tx_handle.Init.Direction = DMA_MEMORY_TO_PERIPH;
bcostm 1:ee089790cdbb 442 dma_tx_handle.Init.PeriphInc = DMA_PINC_DISABLE;
bcostm 1:ee089790cdbb 443 dma_tx_handle.Init.MemInc = DMA_MINC_ENABLE;
bcostm 1:ee089790cdbb 444 dma_tx_handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
bcostm 1:ee089790cdbb 445 dma_tx_handle.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
bcostm 1:ee089790cdbb 446 dma_tx_handle.Init.Mode = DMA_PFCTRL;
bcostm 1:ee089790cdbb 447 dma_tx_handle.Init.Priority = DMA_PRIORITY_VERY_HIGH;
bcostm 1:ee089790cdbb 448 dma_tx_handle.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
bcostm 1:ee089790cdbb 449 dma_tx_handle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
bcostm 1:ee089790cdbb 450 dma_tx_handle.Init.MemBurst = DMA_MBURST_INC4;
bcostm 1:ee089790cdbb 451 dma_tx_handle.Init.PeriphBurst = DMA_PBURST_INC4;
bcostm 1:ee089790cdbb 452
bcostm 1:ee089790cdbb 453 dma_tx_handle.Instance = SD_DMAx_Tx_STREAM;
bcostm 1:ee089790cdbb 454
bcostm 1:ee089790cdbb 455 /* Associate the DMA handle */
bcostm 1:ee089790cdbb 456 __HAL_LINKDMA(hsd, hdmatx, dma_tx_handle);
bcostm 1:ee089790cdbb 457
bcostm 1:ee089790cdbb 458 /* Deinitialize the stream for new transfer */
bcostm 1:ee089790cdbb 459 HAL_DMA_DeInit(&dma_tx_handle);
bcostm 1:ee089790cdbb 460
bcostm 1:ee089790cdbb 461 /* Configure the DMA stream */
bcostm 1:ee089790cdbb 462 HAL_DMA_Init(&dma_tx_handle);
bcostm 1:ee089790cdbb 463
bcostm 1:ee089790cdbb 464 /* NVIC configuration for DMA transfer complete interrupt */
bcostm 1:ee089790cdbb 465 HAL_NVIC_SetPriority(SD_DMAx_Rx_IRQn, 6, 0);
bcostm 1:ee089790cdbb 466 HAL_NVIC_EnableIRQ(SD_DMAx_Rx_IRQn);
bcostm 1:ee089790cdbb 467
bcostm 1:ee089790cdbb 468 /* NVIC configuration for DMA transfer complete interrupt */
bcostm 1:ee089790cdbb 469 HAL_NVIC_SetPriority(SD_DMAx_Tx_IRQn, 6, 0);
bcostm 1:ee089790cdbb 470 HAL_NVIC_EnableIRQ(SD_DMAx_Tx_IRQn);
bcostm 1:ee089790cdbb 471 }
bcostm 1:ee089790cdbb 472
bcostm 1:ee089790cdbb 473 /**
bcostm 1:ee089790cdbb 474 * @brief Initializes the SD Detect pin MSP.
bcostm 1:ee089790cdbb 475 * @param hsd: SD handle
bcostm 1:ee089790cdbb 476 * @param Params
bcostm 1:ee089790cdbb 477 * @retval None
bcostm 1:ee089790cdbb 478 */
bcostm 1:ee089790cdbb 479 __weak void BSP_SD_Detect_MspInit(SD_HandleTypeDef *hsd, void *Params)
bcostm 1:ee089790cdbb 480 {
bcostm 1:ee089790cdbb 481 GPIO_InitTypeDef gpio_init_structure;
bcostm 1:ee089790cdbb 482
bcostm 1:ee089790cdbb 483 SD_DETECT_GPIO_CLK_ENABLE();
bcostm 1:ee089790cdbb 484
bcostm 1:ee089790cdbb 485 /* GPIO configuration in input for uSD_Detect signal */
bcostm 1:ee089790cdbb 486 gpio_init_structure.Pin = SD_DETECT_PIN;
bcostm 1:ee089790cdbb 487 gpio_init_structure.Mode = GPIO_MODE_INPUT;
bcostm 1:ee089790cdbb 488 gpio_init_structure.Pull = GPIO_PULLUP;
bcostm 1:ee089790cdbb 489 gpio_init_structure.Speed = GPIO_SPEED_HIGH;
bcostm 1:ee089790cdbb 490 HAL_GPIO_Init(SD_DETECT_GPIO_PORT, &gpio_init_structure);
bcostm 1:ee089790cdbb 491 }
bcostm 1:ee089790cdbb 492
bcostm 1:ee089790cdbb 493 /**
bcostm 1:ee089790cdbb 494 * @brief DeInitializes the SD MSP.
bcostm 1:ee089790cdbb 495 * @param hsd: SD handle
bcostm 1:ee089790cdbb 496 * @param Params
bcostm 1:ee089790cdbb 497 * @retval None
bcostm 1:ee089790cdbb 498 */
bcostm 1:ee089790cdbb 499 __weak void BSP_SD_MspDeInit(SD_HandleTypeDef *hsd, void *Params)
bcostm 1:ee089790cdbb 500 {
bcostm 1:ee089790cdbb 501 static DMA_HandleTypeDef dma_rx_handle;
bcostm 1:ee089790cdbb 502 static DMA_HandleTypeDef dma_tx_handle;
bcostm 1:ee089790cdbb 503
bcostm 1:ee089790cdbb 504 /* Disable NVIC for DMA transfer complete interrupts */
bcostm 1:ee089790cdbb 505 HAL_NVIC_DisableIRQ(SD_DMAx_Rx_IRQn);
bcostm 1:ee089790cdbb 506 HAL_NVIC_DisableIRQ(SD_DMAx_Tx_IRQn);
bcostm 1:ee089790cdbb 507
bcostm 1:ee089790cdbb 508 /* Deinitialize the stream for new transfer */
bcostm 1:ee089790cdbb 509 dma_rx_handle.Instance = SD_DMAx_Rx_STREAM;
bcostm 1:ee089790cdbb 510 HAL_DMA_DeInit(&dma_rx_handle);
bcostm 1:ee089790cdbb 511
bcostm 1:ee089790cdbb 512 /* Deinitialize the stream for new transfer */
bcostm 1:ee089790cdbb 513 dma_tx_handle.Instance = SD_DMAx_Tx_STREAM;
bcostm 1:ee089790cdbb 514 HAL_DMA_DeInit(&dma_tx_handle);
bcostm 1:ee089790cdbb 515
bcostm 1:ee089790cdbb 516 /* Disable NVIC for SDIO interrupts */
bcostm 1:ee089790cdbb 517 HAL_NVIC_DisableIRQ(SDMMC1_IRQn);
bcostm 1:ee089790cdbb 518
bcostm 1:ee089790cdbb 519 /* DeInit GPIO pins can be done in the application
bcostm 1:ee089790cdbb 520 (by surcharging this __weak function) */
bcostm 1:ee089790cdbb 521
bcostm 1:ee089790cdbb 522 /* Disable SDMMC1 clock */
bcostm 1:ee089790cdbb 523 __HAL_RCC_SDMMC1_CLK_DISABLE();
bcostm 1:ee089790cdbb 524
bcostm 1:ee089790cdbb 525 /* GPIO pins clock and DMA clocks can be shut down in the application
bcostm 1:ee089790cdbb 526 by surcharging this __weak function */
bcostm 1:ee089790cdbb 527 }
bcostm 1:ee089790cdbb 528
bcostm 1:ee089790cdbb 529 /**
bcostm 1:ee089790cdbb 530 * @brief Handles SD card interrupt request.
bcostm 1:ee089790cdbb 531 * @retval None
bcostm 1:ee089790cdbb 532 */
bcostm 1:ee089790cdbb 533 void BSP_SD_IRQHandler(void)
bcostm 1:ee089790cdbb 534 {
bcostm 1:ee089790cdbb 535 HAL_SD_IRQHandler(&uSdHandle);
bcostm 1:ee089790cdbb 536 }
bcostm 1:ee089790cdbb 537
bcostm 1:ee089790cdbb 538 /**
bcostm 1:ee089790cdbb 539 * @brief Handles SD DMA Tx transfer interrupt request.
bcostm 1:ee089790cdbb 540 * @retval None
bcostm 1:ee089790cdbb 541 */
bcostm 1:ee089790cdbb 542 void BSP_SD_DMA_Tx_IRQHandler(void)
bcostm 1:ee089790cdbb 543 {
bcostm 1:ee089790cdbb 544 HAL_DMA_IRQHandler(uSdHandle.hdmatx);
bcostm 1:ee089790cdbb 545 }
bcostm 1:ee089790cdbb 546
bcostm 1:ee089790cdbb 547 /**
bcostm 1:ee089790cdbb 548 * @brief Handles SD DMA Rx transfer interrupt request.
bcostm 1:ee089790cdbb 549 * @retval None
bcostm 1:ee089790cdbb 550 */
bcostm 1:ee089790cdbb 551 void BSP_SD_DMA_Rx_IRQHandler(void)
bcostm 1:ee089790cdbb 552 {
bcostm 1:ee089790cdbb 553 HAL_DMA_IRQHandler(uSdHandle.hdmarx);
bcostm 1:ee089790cdbb 554 }
bcostm 1:ee089790cdbb 555
bcostm 1:ee089790cdbb 556 /**
bcostm 1:ee089790cdbb 557 * @brief Gets the current SD card data status.
bcostm 1:ee089790cdbb 558 * @retval Data transfer state.
bcostm 1:ee089790cdbb 559 * This value can be one of the following values:
bcostm 1:ee089790cdbb 560 * @arg SD_TRANSFER_OK: No data transfer is acting
bcostm 1:ee089790cdbb 561 * @arg SD_TRANSFER_BUSY: Data transfer is acting
bcostm 1:ee089790cdbb 562 * @arg SD_TRANSFER_ERROR: Data transfer error
bcostm 1:ee089790cdbb 563 */
bcostm 1:ee089790cdbb 564 HAL_SD_TransferStateTypedef BSP_SD_GetStatus(void)
bcostm 1:ee089790cdbb 565 {
bcostm 1:ee089790cdbb 566 return(HAL_SD_GetStatus(&uSdHandle));
bcostm 1:ee089790cdbb 567 }
bcostm 1:ee089790cdbb 568
bcostm 1:ee089790cdbb 569 /**
bcostm 1:ee089790cdbb 570 * @brief Get SD information about specific SD card.
bcostm 1:ee089790cdbb 571 * @param CardInfo: Pointer to HAL_SD_CardInfoTypedef structure
bcostm 1:ee089790cdbb 572 * @retval None
bcostm 1:ee089790cdbb 573 */
bcostm 1:ee089790cdbb 574 void BSP_SD_GetCardInfo(HAL_SD_CardInfoTypedef *CardInfo)
bcostm 1:ee089790cdbb 575 {
bcostm 1:ee089790cdbb 576 /* Get SD card Information */
bcostm 1:ee089790cdbb 577 HAL_SD_Get_CardInfo(&uSdHandle, CardInfo);
bcostm 1:ee089790cdbb 578 }
bcostm 1:ee089790cdbb 579
bcostm 1:ee089790cdbb 580 /**
bcostm 1:ee089790cdbb 581 * @}
bcostm 1:ee089790cdbb 582 */
bcostm 1:ee089790cdbb 583
bcostm 1:ee089790cdbb 584 /**
bcostm 1:ee089790cdbb 585 * @}
bcostm 1:ee089790cdbb 586 */
bcostm 1:ee089790cdbb 587
bcostm 1:ee089790cdbb 588 /**
bcostm 1:ee089790cdbb 589 * @}
bcostm 1:ee089790cdbb 590 */
bcostm 1:ee089790cdbb 591
bcostm 1:ee089790cdbb 592 /**
bcostm 1:ee089790cdbb 593 * @}
bcostm 1:ee089790cdbb 594 */
bcostm 1:ee089790cdbb 595
bcostm 1:ee089790cdbb 596 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/