BSP files for STM32H747I-Discovery Copy from ST Cube delivery

Dependents:   DISCO_H747I_LCD_demo DISCO_H747I_AUDIO_demo

Committer:
Jerome Coutant
Date:
Wed Nov 06 11:32:01 2019 +0100
Revision:
3:bc403474b366
Parent:
0:146cf26a9bbb
Add PDM lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jerome Coutant 0:146cf26a9bbb 1 /**
Jerome Coutant 0:146cf26a9bbb 2 ******************************************************************************
Jerome Coutant 0:146cf26a9bbb 3 * @file stm32h747i_discovery_sd.c
Jerome Coutant 0:146cf26a9bbb 4 * @author MCD Application Team
Jerome Coutant 0:146cf26a9bbb 5 * @brief This file includes the uSD card driver mounted on STM32H747I-DISCOVERY
Jerome Coutant 0:146cf26a9bbb 6 * board.
Jerome Coutant 0:146cf26a9bbb 7 ******************************************************************************
Jerome Coutant 0:146cf26a9bbb 8 * @attention
Jerome Coutant 0:146cf26a9bbb 9 *
Jerome Coutant 0:146cf26a9bbb 10 * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
Jerome Coutant 0:146cf26a9bbb 11 * All rights reserved.</center></h2>
Jerome Coutant 0:146cf26a9bbb 12 *
Jerome Coutant 0:146cf26a9bbb 13 * This software component is licensed by ST under BSD 3-Clause license,
Jerome Coutant 0:146cf26a9bbb 14 * the "License"; You may not use this file except in compliance with the
Jerome Coutant 0:146cf26a9bbb 15 * License. You may obtain a copy of the License at:
Jerome Coutant 0:146cf26a9bbb 16 * opensource.org/licenses/BSD-3-Clause
Jerome Coutant 0:146cf26a9bbb 17 *
Jerome Coutant 0:146cf26a9bbb 18 ******************************************************************************
Jerome Coutant 0:146cf26a9bbb 19 */
Jerome Coutant 0:146cf26a9bbb 20
Jerome Coutant 0:146cf26a9bbb 21 /* File Info : -----------------------------------------------------------------
Jerome Coutant 0:146cf26a9bbb 22 User NOTES
Jerome Coutant 0:146cf26a9bbb 23 1. How To use this driver:
Jerome Coutant 0:146cf26a9bbb 24 --------------------------
Jerome Coutant 0:146cf26a9bbb 25 - This driver is used to drive the micro SD external card mounted on STM32412G-DISCOVERY
Jerome Coutant 0:146cf26a9bbb 26 board.
Jerome Coutant 0:146cf26a9bbb 27 - This driver does not need a specific component driver for the micro SD device
Jerome Coutant 0:146cf26a9bbb 28 to be included with.
Jerome Coutant 0:146cf26a9bbb 29
Jerome Coutant 0:146cf26a9bbb 30 2. Driver description:
Jerome Coutant 0:146cf26a9bbb 31 ---------------------
Jerome Coutant 0:146cf26a9bbb 32 + Initialization steps:
Jerome Coutant 0:146cf26a9bbb 33 o Initialize the micro SD card using the BSP_SD_Init() function. This
Jerome Coutant 0:146cf26a9bbb 34 function includes the MSP layer hardware resources initialization and the
Jerome Coutant 0:146cf26a9bbb 35 SDIO interface configuration to interface with the external micro SD. It
Jerome Coutant 0:146cf26a9bbb 36 also includes the micro SD initialization sequence.
Jerome Coutant 0:146cf26a9bbb 37 o To check the SD card presence you can use the function BSP_SD_IsDetected() which
Jerome Coutant 0:146cf26a9bbb 38 returns the detection status
Jerome Coutant 0:146cf26a9bbb 39 o If SD presence detection interrupt mode is desired, you must configure the
Jerome Coutant 0:146cf26a9bbb 40 SD detection interrupt mode by calling the function BSP_SD_ITConfig(). The interrupt
Jerome Coutant 0:146cf26a9bbb 41 is generated as an external interrupt whenever the micro SD card is
Jerome Coutant 0:146cf26a9bbb 42 plugged/unplugged in/from the board.
Jerome Coutant 0:146cf26a9bbb 43 o The function BSP_SD_GetCardInfo() is used to get the micro SD card information
Jerome Coutant 0:146cf26a9bbb 44 which is stored in the structure "HAL_SD_CardInfoTypedef".
Jerome Coutant 0:146cf26a9bbb 45
Jerome Coutant 0:146cf26a9bbb 46 + Micro SD card operations
Jerome Coutant 0:146cf26a9bbb 47 o The micro SD card can be accessed with read/write block(s) operations once
Jerome Coutant 0:146cf26a9bbb 48 it is ready for access. The access can be performed whether using the polling
Jerome Coutant 0:146cf26a9bbb 49 mode by calling the functions BSP_SD_ReadBlocks()/BSP_SD_WriteBlocks(), or by DMA
Jerome Coutant 0:146cf26a9bbb 50 transfer using the functions BSP_SD_ReadBlocks_DMA()/BSP_SD_WriteBlocks_DMA()
Jerome Coutant 0:146cf26a9bbb 51 o The DMA transfer complete is used with interrupt mode. Once the SD transfer
Jerome Coutant 0:146cf26a9bbb 52 is complete, the SD interrupt is handled using the function BSP_SD_IRQHandler(),
Jerome Coutant 0:146cf26a9bbb 53 the DMA Tx/Rx transfer complete are handled using the functions
Jerome Coutant 0:146cf26a9bbb 54 SD_DMA_Tx_IRQHandler()/SD_DMA_Rx_IRQHandler() that should be defined by user.
Jerome Coutant 0:146cf26a9bbb 55 The corresponding user callbacks are implemented by the user at application level.
Jerome Coutant 0:146cf26a9bbb 56 o The SD erase block(s) is performed using the function BSP_SD_Erase() with specifying
Jerome Coutant 0:146cf26a9bbb 57 the number of blocks to erase.
Jerome Coutant 0:146cf26a9bbb 58 o The SD runtime status is returned when calling the function BSP_SD_GetStatus().
Jerome Coutant 0:146cf26a9bbb 59
Jerome Coutant 0:146cf26a9bbb 60 ------------------------------------------------------------------------------*/
Jerome Coutant 0:146cf26a9bbb 61
Jerome Coutant 0:146cf26a9bbb 62 /* Includes ------------------------------------------------------------------*/
Jerome Coutant 0:146cf26a9bbb 63 #include "stm32h747i_discovery_sd.h"
Jerome Coutant 0:146cf26a9bbb 64
Jerome Coutant 0:146cf26a9bbb 65 /** @addtogroup BSP
Jerome Coutant 0:146cf26a9bbb 66 * @{
Jerome Coutant 0:146cf26a9bbb 67 */
Jerome Coutant 0:146cf26a9bbb 68
Jerome Coutant 0:146cf26a9bbb 69 /** @addtogroup STM32H747I_DISCOVERY
Jerome Coutant 0:146cf26a9bbb 70 * @{
Jerome Coutant 0:146cf26a9bbb 71 */
Jerome Coutant 0:146cf26a9bbb 72
Jerome Coutant 0:146cf26a9bbb 73 /** @defgroup STM32H747I_DISCOVERY_SD STM32H747I_DISCOVERY_SD
Jerome Coutant 0:146cf26a9bbb 74 * @{
Jerome Coutant 0:146cf26a9bbb 75 */
Jerome Coutant 0:146cf26a9bbb 76
Jerome Coutant 0:146cf26a9bbb 77 /** @defgroup STM32H747I_DISCOVERY_SD_Exported_Variables Exported Variables
Jerome Coutant 0:146cf26a9bbb 78 * @{
Jerome Coutant 0:146cf26a9bbb 79 */
Jerome Coutant 0:146cf26a9bbb 80 SD_HandleTypeDef uSdHandle;
Jerome Coutant 0:146cf26a9bbb 81 /**
Jerome Coutant 0:146cf26a9bbb 82 * @}
Jerome Coutant 0:146cf26a9bbb 83 */
Jerome Coutant 0:146cf26a9bbb 84
Jerome Coutant 0:146cf26a9bbb 85 /** @defgroup STM32H747I_DISCOVERY_SD_Exported_Functions Exported Functions
Jerome Coutant 0:146cf26a9bbb 86 * @{
Jerome Coutant 0:146cf26a9bbb 87 */
Jerome Coutant 0:146cf26a9bbb 88
Jerome Coutant 0:146cf26a9bbb 89 /**
Jerome Coutant 0:146cf26a9bbb 90 * @brief Initializes the SD card device.
Jerome Coutant 0:146cf26a9bbb 91 * @retval SD status
Jerome Coutant 0:146cf26a9bbb 92 */
Jerome Coutant 0:146cf26a9bbb 93 uint8_t BSP_SD_Init(void)
Jerome Coutant 0:146cf26a9bbb 94 {
Jerome Coutant 0:146cf26a9bbb 95 uint8_t sd_state = MSD_OK;
Jerome Coutant 0:146cf26a9bbb 96
Jerome Coutant 0:146cf26a9bbb 97 /* uSD device interface configuration */
Jerome Coutant 0:146cf26a9bbb 98 uSdHandle.Instance = SDMMC1;
Jerome Coutant 0:146cf26a9bbb 99
Jerome Coutant 0:146cf26a9bbb 100 /* if CLKDIV = 0 then SDMMC Clock frequency = SDMMC Kernel Clock
Jerome Coutant 0:146cf26a9bbb 101 else SDMMC Clock frequency = SDMMC Kernel Clock / [2 * CLKDIV].
Jerome Coutant 0:146cf26a9bbb 102 */
Jerome Coutant 0:146cf26a9bbb 103 #if ! defined (BSP_SD_HIGH_PERFORMANCE_CONFIG)
Jerome Coutant 0:146cf26a9bbb 104 uSdHandle.Init.ClockDiv = 4;
Jerome Coutant 0:146cf26a9bbb 105 #else
Jerome Coutant 0:146cf26a9bbb 106 /* Code for high performance */
Jerome Coutant 0:146cf26a9bbb 107 uSdHandle.Init.ClockDiv = 2;
Jerome Coutant 0:146cf26a9bbb 108 #endif /* BSP_SD_HIGH_PERFORMANCE_CONFIG */
Jerome Coutant 0:146cf26a9bbb 109 uSdHandle.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
Jerome Coutant 0:146cf26a9bbb 110 uSdHandle.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
Jerome Coutant 0:146cf26a9bbb 111 uSdHandle.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
Jerome Coutant 0:146cf26a9bbb 112 uSdHandle.Init.BusWide = SDMMC_BUS_WIDE_4B;
Jerome Coutant 0:146cf26a9bbb 113
Jerome Coutant 0:146cf26a9bbb 114 /* Msp SD initialization */
Jerome Coutant 0:146cf26a9bbb 115 BSP_SD_MspInit(&uSdHandle, NULL);
Jerome Coutant 0:146cf26a9bbb 116
Jerome Coutant 0:146cf26a9bbb 117 /* Check if SD card is present */
Jerome Coutant 0:146cf26a9bbb 118 if(BSP_SD_IsDetected() != SD_PRESENT)
Jerome Coutant 0:146cf26a9bbb 119 {
Jerome Coutant 0:146cf26a9bbb 120 BSP_SD_MspDeInit(&uSdHandle, NULL);
Jerome Coutant 0:146cf26a9bbb 121 return MSD_ERROR_SD_NOT_PRESENT;
Jerome Coutant 0:146cf26a9bbb 122 }
Jerome Coutant 0:146cf26a9bbb 123
Jerome Coutant 0:146cf26a9bbb 124 /* HAL SD initialization */
Jerome Coutant 0:146cf26a9bbb 125 if(HAL_SD_Init(&uSdHandle) != HAL_OK)
Jerome Coutant 0:146cf26a9bbb 126 {
Jerome Coutant 0:146cf26a9bbb 127 sd_state = MSD_ERROR;
Jerome Coutant 0:146cf26a9bbb 128 }
Jerome Coutant 0:146cf26a9bbb 129
Jerome Coutant 0:146cf26a9bbb 130 return sd_state;
Jerome Coutant 0:146cf26a9bbb 131 }
Jerome Coutant 0:146cf26a9bbb 132
Jerome Coutant 0:146cf26a9bbb 133 /**
Jerome Coutant 0:146cf26a9bbb 134 * @brief DeInitializes the SD card device.
Jerome Coutant 0:146cf26a9bbb 135 * @retval SD status
Jerome Coutant 0:146cf26a9bbb 136 */
Jerome Coutant 0:146cf26a9bbb 137 uint8_t BSP_SD_DeInit(void)
Jerome Coutant 0:146cf26a9bbb 138 {
Jerome Coutant 0:146cf26a9bbb 139 uint8_t sd_state = MSD_OK;
Jerome Coutant 0:146cf26a9bbb 140
Jerome Coutant 0:146cf26a9bbb 141 uSdHandle.Instance = SDMMC1;
Jerome Coutant 0:146cf26a9bbb 142
Jerome Coutant 0:146cf26a9bbb 143 /* HAL SD deinitialization */
Jerome Coutant 0:146cf26a9bbb 144 if(HAL_SD_DeInit(&uSdHandle) != HAL_OK)
Jerome Coutant 0:146cf26a9bbb 145 {
Jerome Coutant 0:146cf26a9bbb 146 sd_state = MSD_ERROR;
Jerome Coutant 0:146cf26a9bbb 147 }
Jerome Coutant 0:146cf26a9bbb 148
Jerome Coutant 0:146cf26a9bbb 149 /* Msp SD deinitialization */
Jerome Coutant 0:146cf26a9bbb 150 uSdHandle.Instance = SDMMC1;
Jerome Coutant 0:146cf26a9bbb 151 BSP_SD_MspDeInit(&uSdHandle, NULL);
Jerome Coutant 0:146cf26a9bbb 152
Jerome Coutant 0:146cf26a9bbb 153 return sd_state;
Jerome Coutant 0:146cf26a9bbb 154 }
Jerome Coutant 0:146cf26a9bbb 155
Jerome Coutant 0:146cf26a9bbb 156 /**
Jerome Coutant 0:146cf26a9bbb 157 * @brief Configures Interrupt mode for SD1 detection pin.
Jerome Coutant 0:146cf26a9bbb 158 * @retval Returns 0
Jerome Coutant 0:146cf26a9bbb 159 */
Jerome Coutant 0:146cf26a9bbb 160 uint8_t BSP_SD_ITConfig(void)
Jerome Coutant 0:146cf26a9bbb 161 {
Jerome Coutant 0:146cf26a9bbb 162 GPIO_InitTypeDef gpio_init_structure;
Jerome Coutant 0:146cf26a9bbb 163
Jerome Coutant 0:146cf26a9bbb 164 /* Configure Interrupt mode for SD detection pin */
Jerome Coutant 0:146cf26a9bbb 165 gpio_init_structure.Pin = SD_DETECT_PIN;
Jerome Coutant 0:146cf26a9bbb 166 gpio_init_structure.Pull = GPIO_PULLUP;
Jerome Coutant 0:146cf26a9bbb 167 gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
Jerome Coutant 0:146cf26a9bbb 168 gpio_init_structure.Mode = GPIO_MODE_IT_RISING_FALLING;
Jerome Coutant 0:146cf26a9bbb 169 HAL_GPIO_Init(SD_DETECT_GPIO_PORT, &gpio_init_structure);
Jerome Coutant 0:146cf26a9bbb 170
Jerome Coutant 0:146cf26a9bbb 171 /* Enable and set SD detect EXTI Interrupt to the lowest priority */
Jerome Coutant 0:146cf26a9bbb 172 HAL_NVIC_SetPriority((IRQn_Type)(SD_DETECT_EXTI_IRQn), 0x0F, 0x00);
Jerome Coutant 0:146cf26a9bbb 173 HAL_NVIC_EnableIRQ((IRQn_Type)(SD_DETECT_EXTI_IRQn));
Jerome Coutant 0:146cf26a9bbb 174
Jerome Coutant 0:146cf26a9bbb 175 return MSD_OK;
Jerome Coutant 0:146cf26a9bbb 176 }
Jerome Coutant 0:146cf26a9bbb 177
Jerome Coutant 0:146cf26a9bbb 178 /**
Jerome Coutant 0:146cf26a9bbb 179 * @brief Detects if SD card is correctly plugged in the memory slot or not.
Jerome Coutant 0:146cf26a9bbb 180 * @retval Returns if SD is detected or not
Jerome Coutant 0:146cf26a9bbb 181 */
Jerome Coutant 0:146cf26a9bbb 182 uint8_t BSP_SD_IsDetected(void)
Jerome Coutant 0:146cf26a9bbb 183 {
Jerome Coutant 0:146cf26a9bbb 184 __IO uint8_t status = SD_PRESENT;
Jerome Coutant 0:146cf26a9bbb 185
Jerome Coutant 0:146cf26a9bbb 186
Jerome Coutant 0:146cf26a9bbb 187 /* Check SD card detect pin */
Jerome Coutant 0:146cf26a9bbb 188 if (HAL_GPIO_ReadPin(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) == GPIO_PIN_SET)
Jerome Coutant 0:146cf26a9bbb 189 {
Jerome Coutant 0:146cf26a9bbb 190 status = SD_NOT_PRESENT;
Jerome Coutant 0:146cf26a9bbb 191 }
Jerome Coutant 0:146cf26a9bbb 192
Jerome Coutant 0:146cf26a9bbb 193 return status;
Jerome Coutant 0:146cf26a9bbb 194 }
Jerome Coutant 0:146cf26a9bbb 195
Jerome Coutant 0:146cf26a9bbb 196 /**
Jerome Coutant 0:146cf26a9bbb 197 * @brief Reads block(s) from a specified address in an SD card, in polling mode.
Jerome Coutant 0:146cf26a9bbb 198 * @param pData: Pointer to the buffer that will contain the data to transmit
Jerome Coutant 0:146cf26a9bbb 199 * @param ReadAddr: Address from where data is to be read
Jerome Coutant 0:146cf26a9bbb 200 * @param NumOfBlocks: Number of SD blocks to read
Jerome Coutant 0:146cf26a9bbb 201 * @param Timeout: Timeout for read operation
Jerome Coutant 0:146cf26a9bbb 202 * @retval SD status
Jerome Coutant 0:146cf26a9bbb 203 */
Jerome Coutant 0:146cf26a9bbb 204 uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout)
Jerome Coutant 0:146cf26a9bbb 205 {
Jerome Coutant 0:146cf26a9bbb 206
Jerome Coutant 0:146cf26a9bbb 207 if( HAL_SD_ReadBlocks(&uSdHandle, (uint8_t *)pData, ReadAddr, NumOfBlocks, Timeout) == HAL_OK)
Jerome Coutant 0:146cf26a9bbb 208 {
Jerome Coutant 0:146cf26a9bbb 209 return MSD_OK;
Jerome Coutant 0:146cf26a9bbb 210 }
Jerome Coutant 0:146cf26a9bbb 211 else
Jerome Coutant 0:146cf26a9bbb 212 {
Jerome Coutant 0:146cf26a9bbb 213 return MSD_ERROR;
Jerome Coutant 0:146cf26a9bbb 214 }
Jerome Coutant 0:146cf26a9bbb 215
Jerome Coutant 0:146cf26a9bbb 216 }
Jerome Coutant 0:146cf26a9bbb 217
Jerome Coutant 0:146cf26a9bbb 218 /**
Jerome Coutant 0:146cf26a9bbb 219 * @brief Writes block(s) to a specified address in an SD card, in polling mode.
Jerome Coutant 0:146cf26a9bbb 220 * @param pData: Pointer to the buffer that will contain the data to transmit
Jerome Coutant 0:146cf26a9bbb 221 * @param WriteAddr: Address from where data is to be written
Jerome Coutant 0:146cf26a9bbb 222 * @param NumOfBlocks: Number of SD blocks to write
Jerome Coutant 0:146cf26a9bbb 223 * @param Timeout: Timeout for write operation
Jerome Coutant 0:146cf26a9bbb 224 * @retval SD status
Jerome Coutant 0:146cf26a9bbb 225 */
Jerome Coutant 0:146cf26a9bbb 226 uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout)
Jerome Coutant 0:146cf26a9bbb 227 {
Jerome Coutant 0:146cf26a9bbb 228
Jerome Coutant 0:146cf26a9bbb 229 if( HAL_SD_WriteBlocks(&uSdHandle, (uint8_t *)pData, WriteAddr, NumOfBlocks, Timeout) == HAL_OK)
Jerome Coutant 0:146cf26a9bbb 230 {
Jerome Coutant 0:146cf26a9bbb 231 return MSD_OK;
Jerome Coutant 0:146cf26a9bbb 232 }
Jerome Coutant 0:146cf26a9bbb 233 else
Jerome Coutant 0:146cf26a9bbb 234 {
Jerome Coutant 0:146cf26a9bbb 235 return MSD_ERROR;
Jerome Coutant 0:146cf26a9bbb 236 }
Jerome Coutant 0:146cf26a9bbb 237 }
Jerome Coutant 0:146cf26a9bbb 238
Jerome Coutant 0:146cf26a9bbb 239 /**
Jerome Coutant 0:146cf26a9bbb 240 * @brief Reads block(s) from a specified address in an SD card, in DMA mode.
Jerome Coutant 0:146cf26a9bbb 241 * @param pData: Pointer to the buffer that will contain the data to transmit
Jerome Coutant 0:146cf26a9bbb 242 * @param ReadAddr: Address from where data is to be read
Jerome Coutant 0:146cf26a9bbb 243 * @param NumOfBlocks: Number of SD blocks to read
Jerome Coutant 0:146cf26a9bbb 244 * @retval SD status
Jerome Coutant 0:146cf26a9bbb 245 */
Jerome Coutant 0:146cf26a9bbb 246 uint8_t BSP_SD_ReadBlocks_DMA(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks)
Jerome Coutant 0:146cf26a9bbb 247 {
Jerome Coutant 0:146cf26a9bbb 248
Jerome Coutant 0:146cf26a9bbb 249 if( HAL_SD_ReadBlocks_DMA(&uSdHandle, (uint8_t *)pData, ReadAddr, NumOfBlocks) == HAL_OK)
Jerome Coutant 0:146cf26a9bbb 250 {
Jerome Coutant 0:146cf26a9bbb 251 return MSD_OK;
Jerome Coutant 0:146cf26a9bbb 252 }
Jerome Coutant 0:146cf26a9bbb 253 else
Jerome Coutant 0:146cf26a9bbb 254 {
Jerome Coutant 0:146cf26a9bbb 255 return MSD_ERROR;
Jerome Coutant 0:146cf26a9bbb 256 }
Jerome Coutant 0:146cf26a9bbb 257 }
Jerome Coutant 0:146cf26a9bbb 258
Jerome Coutant 0:146cf26a9bbb 259 /**
Jerome Coutant 0:146cf26a9bbb 260 * @brief Writes block(s) to a specified address in an SD card, in DMA mode.
Jerome Coutant 0:146cf26a9bbb 261 * @param pData: Pointer to the buffer that will contain the data to transmit
Jerome Coutant 0:146cf26a9bbb 262 * @param WriteAddr: Address from where data is to be written
Jerome Coutant 0:146cf26a9bbb 263 * @param NumOfBlocks: Number of SD blocks to write
Jerome Coutant 0:146cf26a9bbb 264 * @retval SD status
Jerome Coutant 0:146cf26a9bbb 265 */
Jerome Coutant 0:146cf26a9bbb 266 uint8_t BSP_SD_WriteBlocks_DMA(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks)
Jerome Coutant 0:146cf26a9bbb 267 {
Jerome Coutant 0:146cf26a9bbb 268
Jerome Coutant 0:146cf26a9bbb 269 if( HAL_SD_WriteBlocks_DMA(&uSdHandle, (uint8_t *)pData, WriteAddr, NumOfBlocks) == HAL_OK)
Jerome Coutant 0:146cf26a9bbb 270 {
Jerome Coutant 0:146cf26a9bbb 271 return MSD_OK;
Jerome Coutant 0:146cf26a9bbb 272 }
Jerome Coutant 0:146cf26a9bbb 273 else
Jerome Coutant 0:146cf26a9bbb 274 {
Jerome Coutant 0:146cf26a9bbb 275 return MSD_ERROR;
Jerome Coutant 0:146cf26a9bbb 276 }
Jerome Coutant 0:146cf26a9bbb 277
Jerome Coutant 0:146cf26a9bbb 278 }
Jerome Coutant 0:146cf26a9bbb 279
Jerome Coutant 0:146cf26a9bbb 280 /**
Jerome Coutant 0:146cf26a9bbb 281 * @brief Erases the specified memory area of the given SD card.
Jerome Coutant 0:146cf26a9bbb 282 * @param StartAddr: Start byte address
Jerome Coutant 0:146cf26a9bbb 283 * @param EndAddr: End byte address
Jerome Coutant 0:146cf26a9bbb 284 * @retval SD status
Jerome Coutant 0:146cf26a9bbb 285 */
Jerome Coutant 0:146cf26a9bbb 286 uint8_t BSP_SD_Erase(uint32_t StartAddr, uint32_t EndAddr)
Jerome Coutant 0:146cf26a9bbb 287 {
Jerome Coutant 0:146cf26a9bbb 288
Jerome Coutant 0:146cf26a9bbb 289 if( HAL_SD_Erase(&uSdHandle, StartAddr, EndAddr) == HAL_OK)
Jerome Coutant 0:146cf26a9bbb 290 {
Jerome Coutant 0:146cf26a9bbb 291 return MSD_OK;
Jerome Coutant 0:146cf26a9bbb 292 }
Jerome Coutant 0:146cf26a9bbb 293 else
Jerome Coutant 0:146cf26a9bbb 294 {
Jerome Coutant 0:146cf26a9bbb 295 return MSD_ERROR;
Jerome Coutant 0:146cf26a9bbb 296 }
Jerome Coutant 0:146cf26a9bbb 297 }
Jerome Coutant 0:146cf26a9bbb 298
Jerome Coutant 0:146cf26a9bbb 299 /**
Jerome Coutant 0:146cf26a9bbb 300 * @brief Initializes the SD MSP.
Jerome Coutant 0:146cf26a9bbb 301 * @param hsd SD handle
Jerome Coutant 0:146cf26a9bbb 302 * @param Params User parameters
Jerome Coutant 0:146cf26a9bbb 303 * @retval None
Jerome Coutant 0:146cf26a9bbb 304 */
Jerome Coutant 0:146cf26a9bbb 305 __weak void BSP_SD_MspInit(SD_HandleTypeDef *hsd, void *Params)
Jerome Coutant 0:146cf26a9bbb 306 {
Jerome Coutant 0:146cf26a9bbb 307 /* __weak function can be modified by the application */
Jerome Coutant 0:146cf26a9bbb 308
Jerome Coutant 0:146cf26a9bbb 309 GPIO_InitTypeDef gpio_init_structure;
Jerome Coutant 0:146cf26a9bbb 310
Jerome Coutant 0:146cf26a9bbb 311 /* SD pins are in conflict with Camera pins on the Disco board
Jerome Coutant 0:146cf26a9bbb 312 therefore Camera must be power down before using the BSP SD
Jerome Coutant 0:146cf26a9bbb 313 To power down the camera , Set GPIOJ pin 14 to high
Jerome Coutant 0:146cf26a9bbb 314 */
Jerome Coutant 0:146cf26a9bbb 315
Jerome Coutant 0:146cf26a9bbb 316 /* Enable GPIO J clock */
Jerome Coutant 0:146cf26a9bbb 317 __HAL_RCC_GPIOJ_CLK_ENABLE();
Jerome Coutant 0:146cf26a9bbb 318
Jerome Coutant 0:146cf26a9bbb 319 gpio_init_structure.Pin = GPIO_PIN_14;
Jerome Coutant 0:146cf26a9bbb 320 gpio_init_structure.Mode = GPIO_MODE_OUTPUT_PP;
Jerome Coutant 0:146cf26a9bbb 321 gpio_init_structure.Pull = GPIO_NOPULL;
Jerome Coutant 0:146cf26a9bbb 322 gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
Jerome Coutant 0:146cf26a9bbb 323 HAL_GPIO_Init(GPIOJ, &gpio_init_structure);
Jerome Coutant 0:146cf26a9bbb 324
Jerome Coutant 0:146cf26a9bbb 325 /* Set the camera POWER_DOWN pin (active high) */
Jerome Coutant 0:146cf26a9bbb 326 HAL_GPIO_WritePin(GPIOJ, GPIO_PIN_14, GPIO_PIN_SET);
Jerome Coutant 0:146cf26a9bbb 327
Jerome Coutant 0:146cf26a9bbb 328 /* Enable SDIO clock */
Jerome Coutant 0:146cf26a9bbb 329 __HAL_RCC_SDMMC1_CLK_ENABLE();
Jerome Coutant 0:146cf26a9bbb 330
Jerome Coutant 0:146cf26a9bbb 331
Jerome Coutant 0:146cf26a9bbb 332 /* Enable GPIOs clock */
Jerome Coutant 0:146cf26a9bbb 333 __HAL_RCC_GPIOB_CLK_ENABLE();
Jerome Coutant 0:146cf26a9bbb 334 __HAL_RCC_GPIOC_CLK_ENABLE();
Jerome Coutant 0:146cf26a9bbb 335 __HAL_RCC_GPIOD_CLK_ENABLE();
Jerome Coutant 0:146cf26a9bbb 336
Jerome Coutant 0:146cf26a9bbb 337
Jerome Coutant 0:146cf26a9bbb 338 /* Common GPIO configuration */
Jerome Coutant 0:146cf26a9bbb 339 gpio_init_structure.Mode = GPIO_MODE_AF_PP;
Jerome Coutant 0:146cf26a9bbb 340 gpio_init_structure.Pull = GPIO_PULLUP;
Jerome Coutant 0:146cf26a9bbb 341 gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
Jerome Coutant 0:146cf26a9bbb 342 gpio_init_structure.Alternate = GPIO_AF12_SDIO1;
Jerome Coutant 0:146cf26a9bbb 343
Jerome Coutant 0:146cf26a9bbb 344 /* SDMMC GPIO CLKIN PB8, D0 PC8, D1 PC9, D2 PC10, D3 PC11, CK PC12, CMD PD2 */
Jerome Coutant 0:146cf26a9bbb 345 /* GPIOC configuration */
Jerome Coutant 0:146cf26a9bbb 346 gpio_init_structure.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
Jerome Coutant 0:146cf26a9bbb 347
Jerome Coutant 0:146cf26a9bbb 348 HAL_GPIO_Init(GPIOC, &gpio_init_structure);
Jerome Coutant 0:146cf26a9bbb 349
Jerome Coutant 0:146cf26a9bbb 350 /* GPIOD configuration */
Jerome Coutant 0:146cf26a9bbb 351 gpio_init_structure.Pin = GPIO_PIN_2;
Jerome Coutant 0:146cf26a9bbb 352 HAL_GPIO_Init(GPIOD, &gpio_init_structure);
Jerome Coutant 0:146cf26a9bbb 353
Jerome Coutant 0:146cf26a9bbb 354 /* Configure Input mode for SD detection pin */
Jerome Coutant 0:146cf26a9bbb 355 SD_DETECT_GPIO_CLK_ENABLE();
Jerome Coutant 0:146cf26a9bbb 356 gpio_init_structure.Pin = SD_DETECT_PIN;
Jerome Coutant 0:146cf26a9bbb 357 gpio_init_structure.Pull = GPIO_PULLUP;
Jerome Coutant 0:146cf26a9bbb 358 gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
Jerome Coutant 0:146cf26a9bbb 359 gpio_init_structure.Mode = GPIO_MODE_INPUT;
Jerome Coutant 0:146cf26a9bbb 360 HAL_GPIO_Init(SD_DETECT_GPIO_PORT, &gpio_init_structure);
Jerome Coutant 0:146cf26a9bbb 361
Jerome Coutant 0:146cf26a9bbb 362 /* NVIC configuration for SDIO interrupts */
Jerome Coutant 0:146cf26a9bbb 363 HAL_NVIC_SetPriority(SDMMC1_IRQn, 5, 0);
Jerome Coutant 0:146cf26a9bbb 364 HAL_NVIC_EnableIRQ(SDMMC1_IRQn);
Jerome Coutant 0:146cf26a9bbb 365
Jerome Coutant 0:146cf26a9bbb 366 }
Jerome Coutant 0:146cf26a9bbb 367
Jerome Coutant 0:146cf26a9bbb 368 /**
Jerome Coutant 0:146cf26a9bbb 369 * @brief DeInitializes the SD MSP.
Jerome Coutant 0:146cf26a9bbb 370 * @param hsd SD handle
Jerome Coutant 0:146cf26a9bbb 371 * @param Params User parameters
Jerome Coutant 0:146cf26a9bbb 372 * @retval None
Jerome Coutant 0:146cf26a9bbb 373 */
Jerome Coutant 0:146cf26a9bbb 374 __weak void BSP_SD_MspDeInit(SD_HandleTypeDef *hsd, void *Params)
Jerome Coutant 0:146cf26a9bbb 375 {
Jerome Coutant 0:146cf26a9bbb 376 /* Disable NVIC for SDIO interrupts */
Jerome Coutant 0:146cf26a9bbb 377 HAL_NVIC_DisableIRQ(SDMMC1_IRQn);
Jerome Coutant 0:146cf26a9bbb 378
Jerome Coutant 0:146cf26a9bbb 379 /* DeInit GPIO pins can be done in the application
Jerome Coutant 0:146cf26a9bbb 380 (by surcharging this __weak function) */
Jerome Coutant 0:146cf26a9bbb 381
Jerome Coutant 0:146cf26a9bbb 382 /* Disable SDMMC1 clock */
Jerome Coutant 0:146cf26a9bbb 383 __HAL_RCC_SDMMC1_CLK_DISABLE();
Jerome Coutant 0:146cf26a9bbb 384
Jerome Coutant 0:146cf26a9bbb 385 /* GPIO pins clock and DMA clocks can be shut down in the application
Jerome Coutant 0:146cf26a9bbb 386 by surcharging this __weak function */
Jerome Coutant 0:146cf26a9bbb 387 }
Jerome Coutant 0:146cf26a9bbb 388
Jerome Coutant 0:146cf26a9bbb 389 /**
Jerome Coutant 0:146cf26a9bbb 390 * @brief Handles SD card interrupt request.
Jerome Coutant 0:146cf26a9bbb 391 * @retval None
Jerome Coutant 0:146cf26a9bbb 392 */
Jerome Coutant 0:146cf26a9bbb 393 void BSP_SD_IRQHandler(void)
Jerome Coutant 0:146cf26a9bbb 394 {
Jerome Coutant 0:146cf26a9bbb 395 HAL_SD_IRQHandler(&uSdHandle);
Jerome Coutant 0:146cf26a9bbb 396 }
Jerome Coutant 0:146cf26a9bbb 397
Jerome Coutant 0:146cf26a9bbb 398 /**
Jerome Coutant 0:146cf26a9bbb 399 * @brief Gets the current SD card data status.
Jerome Coutant 0:146cf26a9bbb 400 * @retval Data transfer state.
Jerome Coutant 0:146cf26a9bbb 401 * This value can be one of the following values:
Jerome Coutant 0:146cf26a9bbb 402 * @arg SD_TRANSFER_OK: No data transfer is acting
Jerome Coutant 0:146cf26a9bbb 403 * @arg SD_TRANSFER_BUSY: Data transfer is acting
Jerome Coutant 0:146cf26a9bbb 404 * @arg SD_TRANSFER_ERROR: Data transfer error
Jerome Coutant 0:146cf26a9bbb 405 */
Jerome Coutant 0:146cf26a9bbb 406 uint8_t BSP_SD_GetCardState(void)
Jerome Coutant 0:146cf26a9bbb 407 {
Jerome Coutant 0:146cf26a9bbb 408 return((HAL_SD_GetCardState(&uSdHandle) == HAL_SD_CARD_TRANSFER ) ? SD_TRANSFER_OK : SD_TRANSFER_BUSY);
Jerome Coutant 0:146cf26a9bbb 409 }
Jerome Coutant 0:146cf26a9bbb 410
Jerome Coutant 0:146cf26a9bbb 411 /**
Jerome Coutant 0:146cf26a9bbb 412 * @brief Get SD information about specific SD card.
Jerome Coutant 0:146cf26a9bbb 413 * @param CardInfo: Pointer to HAL_SD_CardInfoTypedef structure
Jerome Coutant 0:146cf26a9bbb 414 * @retval None
Jerome Coutant 0:146cf26a9bbb 415 */
Jerome Coutant 0:146cf26a9bbb 416 void BSP_SD_GetCardInfo(BSP_SD_CardInfo *CardInfo)
Jerome Coutant 0:146cf26a9bbb 417 {
Jerome Coutant 0:146cf26a9bbb 418 HAL_SD_GetCardInfo(&uSdHandle, CardInfo);
Jerome Coutant 0:146cf26a9bbb 419 }
Jerome Coutant 0:146cf26a9bbb 420
Jerome Coutant 0:146cf26a9bbb 421 /**
Jerome Coutant 0:146cf26a9bbb 422 * @brief SD Abort callbacks
Jerome Coutant 0:146cf26a9bbb 423 * @param hsd SD handle
Jerome Coutant 0:146cf26a9bbb 424 * @retval None
Jerome Coutant 0:146cf26a9bbb 425 */
Jerome Coutant 0:146cf26a9bbb 426 void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
Jerome Coutant 0:146cf26a9bbb 427 {
Jerome Coutant 0:146cf26a9bbb 428 BSP_SD_AbortCallback();
Jerome Coutant 0:146cf26a9bbb 429 }
Jerome Coutant 0:146cf26a9bbb 430
Jerome Coutant 0:146cf26a9bbb 431
Jerome Coutant 0:146cf26a9bbb 432 /**
Jerome Coutant 0:146cf26a9bbb 433 * @brief Tx Transfer completed callbacks
Jerome Coutant 0:146cf26a9bbb 434 * @param hsd SD handle
Jerome Coutant 0:146cf26a9bbb 435 * @retval None
Jerome Coutant 0:146cf26a9bbb 436 */
Jerome Coutant 0:146cf26a9bbb 437 void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
Jerome Coutant 0:146cf26a9bbb 438 {
Jerome Coutant 0:146cf26a9bbb 439 BSP_SD_WriteCpltCallback();
Jerome Coutant 0:146cf26a9bbb 440 }
Jerome Coutant 0:146cf26a9bbb 441
Jerome Coutant 0:146cf26a9bbb 442 /**
Jerome Coutant 0:146cf26a9bbb 443 * @brief Rx Transfer completed callbacks
Jerome Coutant 0:146cf26a9bbb 444 * @param hsd SD handle
Jerome Coutant 0:146cf26a9bbb 445 * @retval None
Jerome Coutant 0:146cf26a9bbb 446 */
Jerome Coutant 0:146cf26a9bbb 447 void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
Jerome Coutant 0:146cf26a9bbb 448 {
Jerome Coutant 0:146cf26a9bbb 449 BSP_SD_ReadCpltCallback();
Jerome Coutant 0:146cf26a9bbb 450 }
Jerome Coutant 0:146cf26a9bbb 451
Jerome Coutant 0:146cf26a9bbb 452 /**
Jerome Coutant 0:146cf26a9bbb 453 * @brief BSP SD Abort callbacks
Jerome Coutant 0:146cf26a9bbb 454 * @retval None
Jerome Coutant 0:146cf26a9bbb 455 */
Jerome Coutant 0:146cf26a9bbb 456 __weak void BSP_SD_AbortCallback(void)
Jerome Coutant 0:146cf26a9bbb 457 {
Jerome Coutant 0:146cf26a9bbb 458
Jerome Coutant 0:146cf26a9bbb 459 }
Jerome Coutant 0:146cf26a9bbb 460
Jerome Coutant 0:146cf26a9bbb 461 /**
Jerome Coutant 0:146cf26a9bbb 462 * @brief BSP Tx Transfer completed callbacks
Jerome Coutant 0:146cf26a9bbb 463 * @retval None
Jerome Coutant 0:146cf26a9bbb 464 */
Jerome Coutant 0:146cf26a9bbb 465 __weak void BSP_SD_WriteCpltCallback(void)
Jerome Coutant 0:146cf26a9bbb 466 {
Jerome Coutant 0:146cf26a9bbb 467
Jerome Coutant 0:146cf26a9bbb 468 }
Jerome Coutant 0:146cf26a9bbb 469
Jerome Coutant 0:146cf26a9bbb 470 /**
Jerome Coutant 0:146cf26a9bbb 471 * @brief BSP Rx Transfer completed callbacks
Jerome Coutant 0:146cf26a9bbb 472 * @retval None
Jerome Coutant 0:146cf26a9bbb 473 */
Jerome Coutant 0:146cf26a9bbb 474 __weak void BSP_SD_ReadCpltCallback(void)
Jerome Coutant 0:146cf26a9bbb 475 {
Jerome Coutant 0:146cf26a9bbb 476
Jerome Coutant 0:146cf26a9bbb 477 }
Jerome Coutant 0:146cf26a9bbb 478 /**
Jerome Coutant 0:146cf26a9bbb 479 * @}
Jerome Coutant 0:146cf26a9bbb 480 */
Jerome Coutant 0:146cf26a9bbb 481
Jerome Coutant 0:146cf26a9bbb 482 /**
Jerome Coutant 0:146cf26a9bbb 483 * @}
Jerome Coutant 0:146cf26a9bbb 484 */
Jerome Coutant 0:146cf26a9bbb 485
Jerome Coutant 0:146cf26a9bbb 486 /**
Jerome Coutant 0:146cf26a9bbb 487 * @}
Jerome Coutant 0:146cf26a9bbb 488 */
Jerome Coutant 0:146cf26a9bbb 489
Jerome Coutant 0:146cf26a9bbb 490 /**
Jerome Coutant 0:146cf26a9bbb 491 * @}
Jerome Coutant 0:146cf26a9bbb 492 */
Jerome Coutant 0:146cf26a9bbb 493
Jerome Coutant 0:146cf26a9bbb 494 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/