Nam

Dependencies:   mbed

Dependents:   uSD LCD

Committer:
bcostm
Date:
Thu Nov 10 09:57:11 2016 +0100
Revision:
6:e1d9da7fe856
Child:
8:56384bddaba5
Update all files present in STM32CubeF7 V1.5.1. Keep original folders structure.

Who changed what in which revision?

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