BSP driver for DISCO_L496AG

Dependents:   DISCO_L496AG-LCD-prova_1 DISCO_L496AG-LCD-prova_2 DISCO_L496AG-LCD-demo DISCO_L496AG-SRAM-demo

Committer:
Jerome Coutant
Date:
Wed Nov 20 16:48:24 2019 +0100
Revision:
2:106c7b82e064
Parent:
0:d83f1c8ca282
Update BSP files with CubeL4 V1.14.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 0:d83f1c8ca282 1 /**
bcostm 0:d83f1c8ca282 2 ******************************************************************************
bcostm 0:d83f1c8ca282 3 * @file stm32l496g_discovery_sd.c
bcostm 0:d83f1c8ca282 4 * @author MCD Application Team
bcostm 0:d83f1c8ca282 5 * @brief This file includes the uSD card driver.
bcostm 0:d83f1c8ca282 6 @verbatim
bcostm 0:d83f1c8ca282 7 ==============================================================================
bcostm 0:d83f1c8ca282 8 ##### How to use this driver #####
bcostm 0:d83f1c8ca282 9 ==============================================================================
bcostm 0:d83f1c8ca282 10 (#) This driver is used to drive the micro SD external card mounted on STM32L496G-DISCOVERY
bcostm 0:d83f1c8ca282 11 evaluation board.
bcostm 0:d83f1c8ca282 12
bcostm 0:d83f1c8ca282 13 (#) This driver does not need a specific component driver for the micro SD device
bcostm 0:d83f1c8ca282 14 to be included with.
bcostm 0:d83f1c8ca282 15
bcostm 0:d83f1c8ca282 16 (#) Initialization steps:
bcostm 0:d83f1c8ca282 17 (++) Initialize the micro SD card using the BSP_SD_Init() function. This
bcostm 0:d83f1c8ca282 18 function includes the MSP layer hardware resources initialization and the
bcostm 0:d83f1c8ca282 19 SDMMC1 interface configuration to interface with the external micro SD. It
bcostm 0:d83f1c8ca282 20 also includes the micro SD initialization sequence.
bcostm 0:d83f1c8ca282 21 (++) To check the SD card presence you can use the function BSP_SD_IsDetected() which
bcostm 0:d83f1c8ca282 22 returns the detection status.
bcostm 0:d83f1c8ca282 23 (++) The function BSP_SD_GetCardInfo() is used to get the micro SD card information
bcostm 0:d83f1c8ca282 24 which is stored in the structure "HAL_SD_CardInfoTypedef".
bcostm 0:d83f1c8ca282 25
bcostm 0:d83f1c8ca282 26 (#) Micro SD card operations
bcostm 0:d83f1c8ca282 27 (++) The micro SD card can be accessed with read/write block(s) operations once
bcostm 0:d83f1c8ca282 28 it is reay for access. The access cand be performed whether using the polling
bcostm 0:d83f1c8ca282 29 mode by calling the functions BSP_SD_ReadBlocks()/BSP_SD_WriteBlocks(), or by DMA
bcostm 0:d83f1c8ca282 30 transfer using the functions BSP_SD_ReadBlocks_DMA()/BSP_SD_WriteBlocks_DMA()
bcostm 0:d83f1c8ca282 31 (++) The DMA transfer complete is used with interrupt mode. Once the SD transfer
bcostm 0:d83f1c8ca282 32 is complete, the SD interrupt is handeled using the function BSP_SD_IRQHandler(),
bcostm 0:d83f1c8ca282 33 the DMA Tx/Rx transfer complete are handeled using the functions
bcostm 0:d83f1c8ca282 34 BSP_SD_DMA_Tx_IRQHandler()/BSP_SD_DMA_Rx_IRQHandler(). The corresponding user callbacks
bcostm 0:d83f1c8ca282 35 are implemented by the user at application level.
bcostm 0:d83f1c8ca282 36 (++) The SD erase block(s) is performed using the function BSP_SD_Erase() with specifying
bcostm 0:d83f1c8ca282 37 the number of blocks to erase.
bcostm 0:d83f1c8ca282 38 (++) The SD runtime status is returned when calling the function BSP_SD_GetStatus().
bcostm 0:d83f1c8ca282 39 [..]
bcostm 0:d83f1c8ca282 40 @endverbatim
bcostm 0:d83f1c8ca282 41 ******************************************************************************
bcostm 0:d83f1c8ca282 42 * @attention
bcostm 0:d83f1c8ca282 43 *
Jerome Coutant 2:106c7b82e064 44 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
Jerome Coutant 2:106c7b82e064 45 * All rights reserved.</center></h2>
bcostm 0:d83f1c8ca282 46 *
Jerome Coutant 2:106c7b82e064 47 * This software component is licensed by ST under BSD 3-Clause license,
Jerome Coutant 2:106c7b82e064 48 * the "License"; You may not use this file except in compliance with the
Jerome Coutant 2:106c7b82e064 49 * License. You may obtain a copy of the License at:
Jerome Coutant 2:106c7b82e064 50 * opensource.org/licenses/BSD-3-Clause
bcostm 0:d83f1c8ca282 51 *
bcostm 0:d83f1c8ca282 52 ******************************************************************************
bcostm 0:d83f1c8ca282 53 */
bcostm 0:d83f1c8ca282 54
bcostm 0:d83f1c8ca282 55 /* Includes ------------------------------------------------------------------*/
bcostm 0:d83f1c8ca282 56 #include "stm32l496g_discovery_io.h"
bcostm 0:d83f1c8ca282 57 #include "stm32l496g_discovery_sd.h"
bcostm 0:d83f1c8ca282 58
bcostm 0:d83f1c8ca282 59 /** @addtogroup BSP
bcostm 0:d83f1c8ca282 60 * @{
bcostm 0:d83f1c8ca282 61 */
bcostm 0:d83f1c8ca282 62
bcostm 0:d83f1c8ca282 63 /** @addtogroup STM32L496G_DISCOVERY
bcostm 0:d83f1c8ca282 64 * @{
bcostm 0:d83f1c8ca282 65 */
bcostm 0:d83f1c8ca282 66
bcostm 0:d83f1c8ca282 67 /** @defgroup STM32L496G_DISCOVERY_SD STM32L496G_DISCOVERY SD
bcostm 0:d83f1c8ca282 68 * @{
bcostm 0:d83f1c8ca282 69 */
bcostm 0:d83f1c8ca282 70
bcostm 0:d83f1c8ca282 71 /* Private variables ---------------------------------------------------------*/
bcostm 0:d83f1c8ca282 72
bcostm 0:d83f1c8ca282 73 /** @defgroup STM32L496G_SD_Private_Variables Private Variables
bcostm 0:d83f1c8ca282 74 * @{
bcostm 0:d83f1c8ca282 75 */
bcostm 0:d83f1c8ca282 76 SD_HandleTypeDef uSdHandle;
bcostm 0:d83f1c8ca282 77 /**
bcostm 0:d83f1c8ca282 78 * @}
bcostm 0:d83f1c8ca282 79 */
bcostm 0:d83f1c8ca282 80
bcostm 0:d83f1c8ca282 81 /* Private function prototypes -----------------------------------------------*/
bcostm 0:d83f1c8ca282 82
bcostm 0:d83f1c8ca282 83 /** @defgroup STM32L496G_DISCOVERY_SD_Private_Functions Private Functions
bcostm 0:d83f1c8ca282 84 * @{
bcostm 0:d83f1c8ca282 85 */
bcostm 0:d83f1c8ca282 86 static void SD_Detect_MspInit(void);
bcostm 0:d83f1c8ca282 87 static void SD_Detect_MspDeInit(void);
bcostm 0:d83f1c8ca282 88 static HAL_StatusTypeDef SD_DMAConfigRx(SD_HandleTypeDef *hsd);
bcostm 0:d83f1c8ca282 89 static HAL_StatusTypeDef SD_DMAConfigTx(SD_HandleTypeDef *hsd);
bcostm 0:d83f1c8ca282 90
bcostm 0:d83f1c8ca282 91 /**
bcostm 0:d83f1c8ca282 92 * @}
bcostm 0:d83f1c8ca282 93 */
bcostm 0:d83f1c8ca282 94
bcostm 0:d83f1c8ca282 95 /* Exported functions ---------------------------------------------------------*/
bcostm 0:d83f1c8ca282 96
bcostm 0:d83f1c8ca282 97 /** @addtogroup STM32L496G_DISCOVERY_SD_Exported_Functions
bcostm 0:d83f1c8ca282 98 * @{
bcostm 0:d83f1c8ca282 99 */
bcostm 0:d83f1c8ca282 100
bcostm 0:d83f1c8ca282 101 /**
bcostm 0:d83f1c8ca282 102 * @brief Initializes the SD card device.
bcostm 0:d83f1c8ca282 103 * @param None
bcostm 0:d83f1c8ca282 104 * @retval SD status
bcostm 0:d83f1c8ca282 105 */
bcostm 0:d83f1c8ca282 106 uint8_t BSP_SD_Init(void)
bcostm 0:d83f1c8ca282 107 {
bcostm 0:d83f1c8ca282 108 uint8_t sd_state = MSD_OK;
bcostm 0:d83f1c8ca282 109
bcostm 0:d83f1c8ca282 110 /* uSD device interface configuration */
bcostm 0:d83f1c8ca282 111 uSdHandle.Instance = SDMMC1;
bcostm 0:d83f1c8ca282 112 uSdHandle.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
bcostm 0:d83f1c8ca282 113 uSdHandle.Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
bcostm 0:d83f1c8ca282 114 uSdHandle.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
bcostm 0:d83f1c8ca282 115 uSdHandle.Init.BusWide = SDMMC_BUS_WIDE_1B;
bcostm 0:d83f1c8ca282 116 uSdHandle.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_ENABLE;
bcostm 0:d83f1c8ca282 117 uSdHandle.Init.ClockDiv = SDMMC_TRANSFER_CLK_DIV;
bcostm 0:d83f1c8ca282 118
bcostm 0:d83f1c8ca282 119 /* Msp SD Detect pin initialization */
bcostm 0:d83f1c8ca282 120 SD_Detect_MspInit();
bcostm 0:d83f1c8ca282 121
bcostm 0:d83f1c8ca282 122 /* Check if the SD card is plugged in the slot */
bcostm 0:d83f1c8ca282 123 if (BSP_SD_IsDetected() != SD_PRESENT)
bcostm 0:d83f1c8ca282 124 {
bcostm 0:d83f1c8ca282 125 return MSD_ERROR_SD_NOT_PRESENT;
bcostm 0:d83f1c8ca282 126 }
bcostm 0:d83f1c8ca282 127
bcostm 0:d83f1c8ca282 128 /* Msp SD initialization */
bcostm 0:d83f1c8ca282 129 BSP_SD_MspInit(&uSdHandle, NULL);
bcostm 0:d83f1c8ca282 130
bcostm 0:d83f1c8ca282 131 /* HAL SD initialization */
bcostm 0:d83f1c8ca282 132 if (HAL_SD_Init(&uSdHandle) != HAL_OK)
bcostm 0:d83f1c8ca282 133 {
bcostm 0:d83f1c8ca282 134 sd_state = MSD_ERROR;
bcostm 0:d83f1c8ca282 135 }
bcostm 0:d83f1c8ca282 136
bcostm 0:d83f1c8ca282 137 /* Configure SD Bus width */
bcostm 0:d83f1c8ca282 138 if (sd_state == MSD_OK)
bcostm 0:d83f1c8ca282 139 {
bcostm 0:d83f1c8ca282 140 /* Enable wide operation */
bcostm 0:d83f1c8ca282 141 if (HAL_SD_ConfigWideBusOperation(&uSdHandle, SDMMC_BUS_WIDE_4B) != HAL_OK)
bcostm 0:d83f1c8ca282 142 {
bcostm 0:d83f1c8ca282 143 sd_state = MSD_ERROR;
bcostm 0:d83f1c8ca282 144 }
bcostm 0:d83f1c8ca282 145 else
bcostm 0:d83f1c8ca282 146 {
bcostm 0:d83f1c8ca282 147 sd_state = MSD_OK;
bcostm 0:d83f1c8ca282 148 }
bcostm 0:d83f1c8ca282 149 }
bcostm 0:d83f1c8ca282 150
bcostm 0:d83f1c8ca282 151 return sd_state;
bcostm 0:d83f1c8ca282 152 }
bcostm 0:d83f1c8ca282 153
bcostm 0:d83f1c8ca282 154 /**
bcostm 0:d83f1c8ca282 155 * @brief DeInitializes the SD card device.
bcostm 0:d83f1c8ca282 156 * @param None
bcostm 0:d83f1c8ca282 157 * @retval SD status
bcostm 0:d83f1c8ca282 158 */
bcostm 0:d83f1c8ca282 159 uint8_t BSP_SD_DeInit(void)
bcostm 0:d83f1c8ca282 160 {
bcostm 0:d83f1c8ca282 161 uint8_t sd_state = MSD_OK;
bcostm 0:d83f1c8ca282 162
bcostm 0:d83f1c8ca282 163 uSdHandle.Instance = SDMMC1;
bcostm 0:d83f1c8ca282 164 /* HAL SD deinitialization */
bcostm 0:d83f1c8ca282 165 if (HAL_SD_DeInit(&uSdHandle) != HAL_OK)
bcostm 0:d83f1c8ca282 166 {
bcostm 0:d83f1c8ca282 167 sd_state = MSD_ERROR;
bcostm 0:d83f1c8ca282 168 }
bcostm 0:d83f1c8ca282 169
bcostm 0:d83f1c8ca282 170 /* Msp SD deinitialization */
bcostm 0:d83f1c8ca282 171 BSP_SD_MspDeInit(&uSdHandle, NULL);
bcostm 0:d83f1c8ca282 172
bcostm 0:d83f1c8ca282 173 SD_Detect_MspDeInit();
bcostm 0:d83f1c8ca282 174
bcostm 0:d83f1c8ca282 175 return sd_state;
bcostm 0:d83f1c8ca282 176 }
bcostm 0:d83f1c8ca282 177
bcostm 0:d83f1c8ca282 178 /**
bcostm 0:d83f1c8ca282 179 * @brief Configures Interrupt mode for SD detection pin.
bcostm 0:d83f1c8ca282 180 * @param None
bcostm 0:d83f1c8ca282 181 * @retval IO_OK: if all initializations are OK. Other value if error.
bcostm 0:d83f1c8ca282 182 */
bcostm 0:d83f1c8ca282 183 uint8_t BSP_SD_ITConfig(void)
bcostm 0:d83f1c8ca282 184 {
bcostm 0:d83f1c8ca282 185 /* Check SD card detect pin */
bcostm 0:d83f1c8ca282 186 if (BSP_IO_ReadPin(SD_DETECT_PIN) != SD_DETECT_PIN)
bcostm 0:d83f1c8ca282 187 {
bcostm 0:d83f1c8ca282 188 return BSP_IO_ConfigPin(SD_DETECT_PIN, IO_MODE_IT_RISING_EDGE_PU);
bcostm 0:d83f1c8ca282 189 }
bcostm 0:d83f1c8ca282 190 else
bcostm 0:d83f1c8ca282 191 {
bcostm 0:d83f1c8ca282 192 return BSP_IO_ConfigPin(SD_DETECT_PIN, IO_MODE_IT_FALLING_EDGE_PU);
bcostm 0:d83f1c8ca282 193 }
bcostm 0:d83f1c8ca282 194 }
bcostm 0:d83f1c8ca282 195
bcostm 0:d83f1c8ca282 196
bcostm 0:d83f1c8ca282 197
bcostm 0:d83f1c8ca282 198 /**
bcostm 0:d83f1c8ca282 199 * @brief Detects if SD card is correctly plugged in the memory slot or not.
bcostm 0:d83f1c8ca282 200 * @param None
bcostm 0:d83f1c8ca282 201 * @retval Returns if SD is detected or not
bcostm 0:d83f1c8ca282 202 */
bcostm 0:d83f1c8ca282 203 uint8_t BSP_SD_IsDetected(void)
bcostm 0:d83f1c8ca282 204 {
bcostm 0:d83f1c8ca282 205 __IO uint8_t status = SD_PRESENT;
bcostm 0:d83f1c8ca282 206
bcostm 0:d83f1c8ca282 207 /* Check SD card detect pin */
bcostm 0:d83f1c8ca282 208 if (BSP_IO_ReadPin(SD_DETECT_PIN) != GPIO_PIN_RESET)
bcostm 0:d83f1c8ca282 209 {
bcostm 0:d83f1c8ca282 210 status = SD_NOT_PRESENT;
bcostm 0:d83f1c8ca282 211 }
bcostm 0:d83f1c8ca282 212
bcostm 0:d83f1c8ca282 213 return status;
bcostm 0:d83f1c8ca282 214 }
bcostm 0:d83f1c8ca282 215
bcostm 0:d83f1c8ca282 216 /**
bcostm 0:d83f1c8ca282 217 * @brief Reads block(s) from a specified address in an SD card, in polling mode.
bcostm 0:d83f1c8ca282 218 * @param pData: Pointer to the buffer that will contain the data to transmit
bcostm 0:d83f1c8ca282 219 * @param ReadAddr: Address from where data is to be read
bcostm 0:d83f1c8ca282 220 * @param NumOfBlocks: Number of SD blocks to read
bcostm 0:d83f1c8ca282 221 * @param Timeout: Timeout for read operation
bcostm 0:d83f1c8ca282 222 * @retval SD status
bcostm 0:d83f1c8ca282 223 */
bcostm 0:d83f1c8ca282 224 uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout)
bcostm 0:d83f1c8ca282 225 {
bcostm 0:d83f1c8ca282 226 HAL_StatusTypeDef sd_state = HAL_OK;
bcostm 0:d83f1c8ca282 227
bcostm 0:d83f1c8ca282 228 sd_state = HAL_SD_ReadBlocks(&uSdHandle, (uint8_t *)pData, ReadAddr, NumOfBlocks, Timeout);
bcostm 0:d83f1c8ca282 229
bcostm 0:d83f1c8ca282 230 if (sd_state == HAL_OK)
bcostm 0:d83f1c8ca282 231 {
bcostm 0:d83f1c8ca282 232 return MSD_OK;
bcostm 0:d83f1c8ca282 233 }
bcostm 0:d83f1c8ca282 234 else
bcostm 0:d83f1c8ca282 235 {
bcostm 0:d83f1c8ca282 236 return MSD_ERROR;
bcostm 0:d83f1c8ca282 237 }
bcostm 0:d83f1c8ca282 238 }
bcostm 0:d83f1c8ca282 239
bcostm 0:d83f1c8ca282 240 /**
bcostm 0:d83f1c8ca282 241 * @brief Writes block(s) to a specified address in an SD card, in polling mode.
bcostm 0:d83f1c8ca282 242 * @param pData: Pointer to the buffer that will contain the data to transmit
bcostm 0:d83f1c8ca282 243 * @param WriteAddr: Address from where data is to be written
bcostm 0:d83f1c8ca282 244 * @param NumOfBlocks: Number of SD blocks to write
bcostm 0:d83f1c8ca282 245 * @param Timeout: Timeout for write operation
bcostm 0:d83f1c8ca282 246 * @retval SD status
bcostm 0:d83f1c8ca282 247 */
bcostm 0:d83f1c8ca282 248 uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout)
bcostm 0:d83f1c8ca282 249 {
bcostm 0:d83f1c8ca282 250 HAL_StatusTypeDef sd_state = HAL_OK;
bcostm 0:d83f1c8ca282 251
bcostm 0:d83f1c8ca282 252 sd_state = HAL_SD_WriteBlocks(&uSdHandle, (uint8_t *)pData, WriteAddr, NumOfBlocks, Timeout);
bcostm 0:d83f1c8ca282 253
bcostm 0:d83f1c8ca282 254 if (sd_state == HAL_OK)
bcostm 0:d83f1c8ca282 255 {
bcostm 0:d83f1c8ca282 256 return MSD_OK;
bcostm 0:d83f1c8ca282 257 }
bcostm 0:d83f1c8ca282 258 else
bcostm 0:d83f1c8ca282 259 {
bcostm 0:d83f1c8ca282 260 return MSD_ERROR;
bcostm 0:d83f1c8ca282 261 }
bcostm 0:d83f1c8ca282 262 }
bcostm 0:d83f1c8ca282 263
bcostm 0:d83f1c8ca282 264 /**
bcostm 0:d83f1c8ca282 265 * @brief Reads block(s) from a specified address in an SD card, in DMA mode.
bcostm 0:d83f1c8ca282 266 * @param pData: Pointer to the buffer that will contain the data to transmit
bcostm 0:d83f1c8ca282 267 * @param ReadAddr: Address from where data is to be read
bcostm 0:d83f1c8ca282 268 * @param NumOfBlocks: Number of SD blocks to read
bcostm 0:d83f1c8ca282 269 * @retval SD status
bcostm 0:d83f1c8ca282 270 */
bcostm 0:d83f1c8ca282 271 uint8_t BSP_SD_ReadBlocks_DMA(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks)
bcostm 0:d83f1c8ca282 272 {
bcostm 0:d83f1c8ca282 273 HAL_StatusTypeDef sd_state = HAL_OK;
bcostm 0:d83f1c8ca282 274
bcostm 0:d83f1c8ca282 275 /* Invalidate the dma tx handle*/
bcostm 0:d83f1c8ca282 276 uSdHandle.hdmatx = NULL;
bcostm 0:d83f1c8ca282 277
bcostm 0:d83f1c8ca282 278 /* Prepare the dma channel for a read operation */
bcostm 0:d83f1c8ca282 279 sd_state = SD_DMAConfigRx(&uSdHandle);
bcostm 0:d83f1c8ca282 280
bcostm 0:d83f1c8ca282 281 if (sd_state == HAL_OK)
bcostm 0:d83f1c8ca282 282 {
bcostm 0:d83f1c8ca282 283 /* Read block(s) in DMA transfer mode */
bcostm 0:d83f1c8ca282 284 sd_state = HAL_SD_ReadBlocks_DMA(&uSdHandle, (uint8_t *)pData, ReadAddr, NumOfBlocks);
bcostm 0:d83f1c8ca282 285 }
bcostm 0:d83f1c8ca282 286
bcostm 0:d83f1c8ca282 287 if (sd_state == HAL_OK)
bcostm 0:d83f1c8ca282 288 {
bcostm 0:d83f1c8ca282 289 return MSD_OK;
bcostm 0:d83f1c8ca282 290 }
bcostm 0:d83f1c8ca282 291 else
bcostm 0:d83f1c8ca282 292 {
bcostm 0:d83f1c8ca282 293 return MSD_ERROR;
bcostm 0:d83f1c8ca282 294 }
bcostm 0:d83f1c8ca282 295 }
bcostm 0:d83f1c8ca282 296
bcostm 0:d83f1c8ca282 297 /**
bcostm 0:d83f1c8ca282 298 * @brief Writes block(s) to a specified address in an SD card, in DMA mode.
bcostm 0:d83f1c8ca282 299 * @param pData: Pointer to the buffer that will contain the data to transmit
bcostm 0:d83f1c8ca282 300 * @param WriteAddr: Address from where data is to be written
bcostm 0:d83f1c8ca282 301 * @param NumOfBlocks: Number of SD blocks to write
bcostm 0:d83f1c8ca282 302 * @retval SD status
bcostm 0:d83f1c8ca282 303 */
bcostm 0:d83f1c8ca282 304 uint8_t BSP_SD_WriteBlocks_DMA(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks)
bcostm 0:d83f1c8ca282 305 {
bcostm 0:d83f1c8ca282 306 HAL_StatusTypeDef sd_state = HAL_OK;
bcostm 0:d83f1c8ca282 307
bcostm 0:d83f1c8ca282 308 /* Invalidate the dma rx handle*/
bcostm 0:d83f1c8ca282 309 uSdHandle.hdmarx = NULL;
bcostm 0:d83f1c8ca282 310
bcostm 0:d83f1c8ca282 311 /* Prepare the dma channel for a read operation */
bcostm 0:d83f1c8ca282 312 sd_state = SD_DMAConfigTx(&uSdHandle);
bcostm 0:d83f1c8ca282 313
bcostm 0:d83f1c8ca282 314 if (sd_state == HAL_OK)
bcostm 0:d83f1c8ca282 315 {
bcostm 0:d83f1c8ca282 316 /* Write block(s) in DMA transfer mode */
bcostm 0:d83f1c8ca282 317 sd_state = HAL_SD_WriteBlocks_DMA(&uSdHandle, (uint8_t *)pData, WriteAddr, NumOfBlocks);
bcostm 0:d83f1c8ca282 318 }
bcostm 0:d83f1c8ca282 319
bcostm 0:d83f1c8ca282 320 if (sd_state == HAL_OK)
bcostm 0:d83f1c8ca282 321 {
bcostm 0:d83f1c8ca282 322 return MSD_OK;
bcostm 0:d83f1c8ca282 323 }
bcostm 0:d83f1c8ca282 324 else
bcostm 0:d83f1c8ca282 325 {
bcostm 0:d83f1c8ca282 326 return MSD_ERROR;
bcostm 0:d83f1c8ca282 327 }
bcostm 0:d83f1c8ca282 328 }
bcostm 0:d83f1c8ca282 329
bcostm 0:d83f1c8ca282 330 /**
bcostm 0:d83f1c8ca282 331 * @brief Erases the specified memory area of the given SD card.
bcostm 0:d83f1c8ca282 332 * @param StartAddr: Start byte address
bcostm 0:d83f1c8ca282 333 * @param EndAddr: End byte address
bcostm 0:d83f1c8ca282 334 * @retval SD status
bcostm 0:d83f1c8ca282 335 */
bcostm 0:d83f1c8ca282 336 uint8_t BSP_SD_Erase(uint32_t StartAddr, uint32_t EndAddr)
bcostm 0:d83f1c8ca282 337 {
bcostm 0:d83f1c8ca282 338 HAL_StatusTypeDef sd_state = HAL_OK;
bcostm 0:d83f1c8ca282 339
bcostm 0:d83f1c8ca282 340 sd_state = HAL_SD_Erase(&uSdHandle, StartAddr, EndAddr);
bcostm 0:d83f1c8ca282 341
bcostm 0:d83f1c8ca282 342 if (sd_state == HAL_OK)
bcostm 0:d83f1c8ca282 343 {
bcostm 0:d83f1c8ca282 344 return MSD_OK;
bcostm 0:d83f1c8ca282 345 }
bcostm 0:d83f1c8ca282 346 else
bcostm 0:d83f1c8ca282 347 {
bcostm 0:d83f1c8ca282 348 return MSD_ERROR;
bcostm 0:d83f1c8ca282 349 }
bcostm 0:d83f1c8ca282 350 }
bcostm 0:d83f1c8ca282 351
bcostm 0:d83f1c8ca282 352 /**
bcostm 0:d83f1c8ca282 353 * @brief Handles SD card interrupt request.
bcostm 0:d83f1c8ca282 354 * @retval None
bcostm 0:d83f1c8ca282 355 */
bcostm 0:d83f1c8ca282 356 void BSP_SD_IRQHandler(void)
bcostm 0:d83f1c8ca282 357 {
bcostm 0:d83f1c8ca282 358 HAL_SD_IRQHandler(&uSdHandle);
bcostm 0:d83f1c8ca282 359 }
bcostm 0:d83f1c8ca282 360
bcostm 0:d83f1c8ca282 361 /**
bcostm 0:d83f1c8ca282 362 * @brief Handles SD DMA Tx transfer interrupt request.
bcostm 0:d83f1c8ca282 363 * @retval None
bcostm 0:d83f1c8ca282 364 */
bcostm 0:d83f1c8ca282 365 void BSP_SD_DMA_Tx_IRQHandler(void)
bcostm 0:d83f1c8ca282 366 {
bcostm 0:d83f1c8ca282 367 HAL_DMA_IRQHandler(uSdHandle.hdmatx);
bcostm 0:d83f1c8ca282 368 }
bcostm 0:d83f1c8ca282 369
bcostm 0:d83f1c8ca282 370 /**
bcostm 0:d83f1c8ca282 371 * @brief Handles SD DMA Rx transfer interrupt request.
bcostm 0:d83f1c8ca282 372 * @retval None
bcostm 0:d83f1c8ca282 373 */
bcostm 0:d83f1c8ca282 374 void BSP_SD_DMA_Rx_IRQHandler(void)
bcostm 0:d83f1c8ca282 375 {
bcostm 0:d83f1c8ca282 376 HAL_DMA_IRQHandler(uSdHandle.hdmarx);
bcostm 0:d83f1c8ca282 377 }
bcostm 0:d83f1c8ca282 378
bcostm 0:d83f1c8ca282 379 /**
bcostm 0:d83f1c8ca282 380 * @brief Gets the current SD card data status.
bcostm 0:d83f1c8ca282 381 * @param None
bcostm 0:d83f1c8ca282 382 * @retval Data transfer state.
bcostm 0:d83f1c8ca282 383 */
bcostm 0:d83f1c8ca282 384 uint8_t BSP_SD_GetCardState(void)
bcostm 0:d83f1c8ca282 385 {
bcostm 0:d83f1c8ca282 386 HAL_SD_CardStateTypedef card_state;
bcostm 0:d83f1c8ca282 387 card_state = HAL_SD_GetCardState(&uSdHandle);
bcostm 0:d83f1c8ca282 388
bcostm 0:d83f1c8ca282 389 if (card_state == HAL_SD_CARD_TRANSFER)
bcostm 0:d83f1c8ca282 390 {
bcostm 0:d83f1c8ca282 391 return (SD_TRANSFER_OK);
bcostm 0:d83f1c8ca282 392 }
bcostm 0:d83f1c8ca282 393 else if ((card_state == HAL_SD_CARD_SENDING) ||
bcostm 0:d83f1c8ca282 394 (card_state == HAL_SD_CARD_RECEIVING) ||
bcostm 0:d83f1c8ca282 395 (card_state == HAL_SD_CARD_PROGRAMMING))
bcostm 0:d83f1c8ca282 396 {
bcostm 0:d83f1c8ca282 397 return (SD_TRANSFER_BUSY);
bcostm 0:d83f1c8ca282 398 }
bcostm 0:d83f1c8ca282 399 else
bcostm 0:d83f1c8ca282 400 {
bcostm 0:d83f1c8ca282 401 return (SD_TRANSFER_ERROR);
bcostm 0:d83f1c8ca282 402 }
bcostm 0:d83f1c8ca282 403 }
bcostm 0:d83f1c8ca282 404
bcostm 0:d83f1c8ca282 405 /**
bcostm 0:d83f1c8ca282 406 * @brief Get SD information about specific SD card.
bcostm 0:d83f1c8ca282 407 * @param CardInfo: Pointer to HAL_SD_CardInfoTypedef structure
bcostm 0:d83f1c8ca282 408 * @retval None
bcostm 0:d83f1c8ca282 409 */
bcostm 0:d83f1c8ca282 410 void BSP_SD_GetCardInfo(BSP_SD_CardInfo *CardInfo)
bcostm 0:d83f1c8ca282 411 {
bcostm 0:d83f1c8ca282 412 /* Get SD card Information */
bcostm 0:d83f1c8ca282 413 HAL_SD_GetCardInfo(&uSdHandle, CardInfo);
bcostm 0:d83f1c8ca282 414 }
bcostm 0:d83f1c8ca282 415
bcostm 0:d83f1c8ca282 416 /**
bcostm 0:d83f1c8ca282 417 * @brief Initializes the SD MSP.
bcostm 0:d83f1c8ca282 418 * @note The SDMMC clock configuration done within this function assumes that
bcostm 0:d83f1c8ca282 419 * the PLLSAI1 input clock runs at 8 MHz.
bcostm 0:d83f1c8ca282 420 * @param hsd: SD handle
bcostm 0:d83f1c8ca282 421 * @param Params: Additional parameters
bcostm 0:d83f1c8ca282 422 * @retval None
bcostm 0:d83f1c8ca282 423 */
bcostm 0:d83f1c8ca282 424 __weak void BSP_SD_MspInit(SD_HandleTypeDef *hsd, void *Params)
bcostm 0:d83f1c8ca282 425 {
bcostm 0:d83f1c8ca282 426 GPIO_InitTypeDef gpioinitstruct = {0};
bcostm 0:d83f1c8ca282 427 RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
bcostm 0:d83f1c8ca282 428
bcostm 0:d83f1c8ca282 429 /* Prevent unused argument(s) compilation warning */
bcostm 0:d83f1c8ca282 430 UNUSED(Params);
bcostm 0:d83f1c8ca282 431
bcostm 0:d83f1c8ca282 432 HAL_RCCEx_GetPeriphCLKConfig(&RCC_PeriphClkInit);
bcostm 0:d83f1c8ca282 433
bcostm 0:d83f1c8ca282 434 /* Configure the SDMMC1 clock source. The clock is derived from the PLLSAI1 */
bcostm 0:d83f1c8ca282 435 /* Hypothesis is that PLLSAI1 VCO input is 8Mhz */
bcostm 0:d83f1c8ca282 436 RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_SDMMC1;
bcostm 0:d83f1c8ca282 437 RCC_PeriphClkInit.PLLSAI1.PLLSAI1N = 24;
bcostm 0:d83f1c8ca282 438 RCC_PeriphClkInit.PLLSAI1.PLLSAI1Q = 4;
bcostm 0:d83f1c8ca282 439 RCC_PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_48M2CLK;
bcostm 0:d83f1c8ca282 440 RCC_PeriphClkInit.Sdmmc1ClockSelection = RCC_SDMMC1CLKSOURCE_PLLSAI1;
bcostm 0:d83f1c8ca282 441 if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit) != HAL_OK)
bcostm 0:d83f1c8ca282 442 {
bcostm 0:d83f1c8ca282 443 while (1) {}
bcostm 0:d83f1c8ca282 444 }
bcostm 0:d83f1c8ca282 445
bcostm 0:d83f1c8ca282 446 /* Enable SDMMC1 clock */
bcostm 0:d83f1c8ca282 447 __HAL_RCC_SDMMC1_CLK_ENABLE();
bcostm 0:d83f1c8ca282 448
bcostm 0:d83f1c8ca282 449 /* Enable DMA2 clocks */
bcostm 0:d83f1c8ca282 450 SD_DMAx_CLK_ENABLE();
bcostm 0:d83f1c8ca282 451
bcostm 0:d83f1c8ca282 452 /* Enable GPIOs clock */
bcostm 0:d83f1c8ca282 453 __HAL_RCC_GPIOC_CLK_ENABLE();
bcostm 0:d83f1c8ca282 454 __HAL_RCC_GPIOD_CLK_ENABLE();
bcostm 0:d83f1c8ca282 455
bcostm 0:d83f1c8ca282 456 /* Common GPIO configuration */
bcostm 0:d83f1c8ca282 457 gpioinitstruct.Mode = GPIO_MODE_AF_PP;
bcostm 0:d83f1c8ca282 458 gpioinitstruct.Pull = GPIO_PULLUP;
bcostm 0:d83f1c8ca282 459 gpioinitstruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
bcostm 0:d83f1c8ca282 460 gpioinitstruct.Alternate = GPIO_AF12_SDMMC1;
bcostm 0:d83f1c8ca282 461
bcostm 0:d83f1c8ca282 462 /* GPIOC configuration */
bcostm 0:d83f1c8ca282 463 gpioinitstruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
bcostm 0:d83f1c8ca282 464
bcostm 0:d83f1c8ca282 465 HAL_GPIO_Init(GPIOC, &gpioinitstruct);
bcostm 0:d83f1c8ca282 466
bcostm 0:d83f1c8ca282 467 /* GPIOD configuration */
bcostm 0:d83f1c8ca282 468 gpioinitstruct.Pin = GPIO_PIN_2;
bcostm 0:d83f1c8ca282 469 HAL_GPIO_Init(GPIOD, &gpioinitstruct);
bcostm 0:d83f1c8ca282 470
bcostm 0:d83f1c8ca282 471 /* NVIC configuration for SDMMC1 interrupts */
bcostm 0:d83f1c8ca282 472 HAL_NVIC_SetPriority(SDMMCx_IRQn, 5, 0);
bcostm 0:d83f1c8ca282 473 HAL_NVIC_EnableIRQ(SDMMCx_IRQn);
bcostm 0:d83f1c8ca282 474
bcostm 0:d83f1c8ca282 475 /* DMA initialization should be done here but , as there is only one channel for RX and TX it is configured and done directly when required*/
bcostm 0:d83f1c8ca282 476 }
bcostm 0:d83f1c8ca282 477
bcostm 0:d83f1c8ca282 478 /**
bcostm 0:d83f1c8ca282 479 * @brief De-Initializes the SD MSP.
bcostm 0:d83f1c8ca282 480 * @param hsd: SD handle
bcostm 0:d83f1c8ca282 481 * @param Params: Additional parameters
bcostm 0:d83f1c8ca282 482 * @retval None
bcostm 0:d83f1c8ca282 483 */
bcostm 0:d83f1c8ca282 484 __weak void BSP_SD_MspDeInit(SD_HandleTypeDef *hsd, void *Params)
bcostm 0:d83f1c8ca282 485 {
bcostm 0:d83f1c8ca282 486 GPIO_InitTypeDef gpioinitstruct = {0};
bcostm 0:d83f1c8ca282 487
bcostm 0:d83f1c8ca282 488 /* Prevent unused argument(s) compilation warning */
bcostm 0:d83f1c8ca282 489 UNUSED(Params);
bcostm 0:d83f1c8ca282 490
bcostm 0:d83f1c8ca282 491 /* Enable SDMMC1 clock */
bcostm 0:d83f1c8ca282 492 __HAL_RCC_SDMMC1_CLK_DISABLE();
bcostm 0:d83f1c8ca282 493
bcostm 0:d83f1c8ca282 494 /* Enable DMA2 clocks */
bcostm 0:d83f1c8ca282 495 SD_DMAx_CLK_DISABLE();
bcostm 0:d83f1c8ca282 496
bcostm 0:d83f1c8ca282 497 /* Enable GPIOs clock */
bcostm 0:d83f1c8ca282 498 __HAL_RCC_GPIOC_CLK_ENABLE();
bcostm 0:d83f1c8ca282 499 __HAL_RCC_GPIOD_CLK_ENABLE();
bcostm 0:d83f1c8ca282 500
bcostm 0:d83f1c8ca282 501 /* Common GPIO configuration */
bcostm 0:d83f1c8ca282 502 gpioinitstruct.Mode = GPIO_MODE_ANALOG;
bcostm 0:d83f1c8ca282 503 gpioinitstruct.Pull = GPIO_NOPULL;
bcostm 0:d83f1c8ca282 504 gpioinitstruct.Speed = GPIO_SPEED_FREQ_LOW;
bcostm 0:d83f1c8ca282 505 gpioinitstruct.Alternate = 0;
bcostm 0:d83f1c8ca282 506
bcostm 0:d83f1c8ca282 507 /* GPIOC configuration */
bcostm 0:d83f1c8ca282 508 gpioinitstruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
bcostm 0:d83f1c8ca282 509
bcostm 0:d83f1c8ca282 510 HAL_GPIO_Init(GPIOC, &gpioinitstruct);
bcostm 0:d83f1c8ca282 511
bcostm 0:d83f1c8ca282 512 /* GPIOD configuration */
bcostm 0:d83f1c8ca282 513 gpioinitstruct.Pin = GPIO_PIN_2;
bcostm 0:d83f1c8ca282 514 HAL_GPIO_Init(GPIOD, &gpioinitstruct);
bcostm 0:d83f1c8ca282 515
bcostm 0:d83f1c8ca282 516 /* NVIC configuration for SDMMC1 interrupts */
bcostm 0:d83f1c8ca282 517 HAL_NVIC_DisableIRQ(SDMMCx_IRQn);
bcostm 0:d83f1c8ca282 518
bcostm 0:d83f1c8ca282 519 }
bcostm 0:d83f1c8ca282 520
bcostm 0:d83f1c8ca282 521 /**
bcostm 0:d83f1c8ca282 522 * @brief BSP SD Abort callback
bcostm 0:d83f1c8ca282 523 * @retval None
bcostm 0:d83f1c8ca282 524 */
bcostm 0:d83f1c8ca282 525 __weak void BSP_SD_AbortCallback(void)
bcostm 0:d83f1c8ca282 526 {
bcostm 0:d83f1c8ca282 527
bcostm 0:d83f1c8ca282 528 }
bcostm 0:d83f1c8ca282 529
bcostm 0:d83f1c8ca282 530 /**
bcostm 0:d83f1c8ca282 531 * @brief BSP Tx Transfer completed callback
bcostm 0:d83f1c8ca282 532 * @retval None
bcostm 0:d83f1c8ca282 533 */
bcostm 0:d83f1c8ca282 534 __weak void BSP_SD_WriteCpltCallback(void)
bcostm 0:d83f1c8ca282 535 {
bcostm 0:d83f1c8ca282 536
bcostm 0:d83f1c8ca282 537 }
bcostm 0:d83f1c8ca282 538
bcostm 0:d83f1c8ca282 539 /**
bcostm 0:d83f1c8ca282 540 * @brief BSP Rx Transfer completed callback
bcostm 0:d83f1c8ca282 541 * @retval None
bcostm 0:d83f1c8ca282 542 */
bcostm 0:d83f1c8ca282 543 __weak void BSP_SD_ReadCpltCallback(void)
bcostm 0:d83f1c8ca282 544 {
bcostm 0:d83f1c8ca282 545
bcostm 0:d83f1c8ca282 546 }
bcostm 0:d83f1c8ca282 547
bcostm 0:d83f1c8ca282 548 /**
bcostm 0:d83f1c8ca282 549 * @brief SD Abort callback
bcostm 0:d83f1c8ca282 550 * @param hsd: SD handle
bcostm 0:d83f1c8ca282 551 * @retval None
bcostm 0:d83f1c8ca282 552 */
bcostm 0:d83f1c8ca282 553 void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
bcostm 0:d83f1c8ca282 554 {
bcostm 0:d83f1c8ca282 555 BSP_SD_AbortCallback();
bcostm 0:d83f1c8ca282 556 }
bcostm 0:d83f1c8ca282 557
bcostm 0:d83f1c8ca282 558 /**
bcostm 0:d83f1c8ca282 559 * @brief Tx Transfer completed callback
bcostm 0:d83f1c8ca282 560 * @param hsd: SD handle
bcostm 0:d83f1c8ca282 561 * @retval None
bcostm 0:d83f1c8ca282 562 */
bcostm 0:d83f1c8ca282 563 void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
bcostm 0:d83f1c8ca282 564 {
bcostm 0:d83f1c8ca282 565 BSP_SD_WriteCpltCallback();
bcostm 0:d83f1c8ca282 566 }
bcostm 0:d83f1c8ca282 567
bcostm 0:d83f1c8ca282 568 /**
bcostm 0:d83f1c8ca282 569 * @brief Rx Transfer completed callback
bcostm 0:d83f1c8ca282 570 * @param hsd: SD handle
bcostm 0:d83f1c8ca282 571 * @retval None
bcostm 0:d83f1c8ca282 572 */
bcostm 0:d83f1c8ca282 573 void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
bcostm 0:d83f1c8ca282 574 {
bcostm 0:d83f1c8ca282 575 BSP_SD_ReadCpltCallback();
bcostm 0:d83f1c8ca282 576 }
bcostm 0:d83f1c8ca282 577
bcostm 0:d83f1c8ca282 578 /**
bcostm 0:d83f1c8ca282 579 * @}
bcostm 0:d83f1c8ca282 580 */
bcostm 0:d83f1c8ca282 581
bcostm 0:d83f1c8ca282 582
bcostm 0:d83f1c8ca282 583 /** @addtogroup STM32L496G_DISCOVERY_SD_Private_Functions
bcostm 0:d83f1c8ca282 584 * @{
bcostm 0:d83f1c8ca282 585 */
bcostm 0:d83f1c8ca282 586
bcostm 0:d83f1c8ca282 587 /**
bcostm 0:d83f1c8ca282 588 * @brief Initializes the SD Detect pin MSP.
bcostm 0:d83f1c8ca282 589 * @param hsd: SD handle
bcostm 0:d83f1c8ca282 590 * @param Params
bcostm 0:d83f1c8ca282 591 * @retval None
bcostm 0:d83f1c8ca282 592 */
bcostm 0:d83f1c8ca282 593 static void SD_Detect_MspInit(void)
bcostm 0:d83f1c8ca282 594 {
bcostm 0:d83f1c8ca282 595 if (BSP_IO_Init() == IO_ERROR)
bcostm 0:d83f1c8ca282 596 {
bcostm 0:d83f1c8ca282 597 BSP_ErrorHandler();
bcostm 0:d83f1c8ca282 598 }
bcostm 0:d83f1c8ca282 599 BSP_IO_ConfigPin(SD_DETECT_PIN, IO_MODE_INPUT_PU);
bcostm 0:d83f1c8ca282 600 }
bcostm 0:d83f1c8ca282 601
bcostm 0:d83f1c8ca282 602 /**
bcostm 0:d83f1c8ca282 603 * @brief Initializes the SD Detect pin MSP.
bcostm 0:d83f1c8ca282 604 * @param hsd: SD handle
bcostm 0:d83f1c8ca282 605 * @param Params
bcostm 0:d83f1c8ca282 606 * @retval None
bcostm 0:d83f1c8ca282 607 */
bcostm 0:d83f1c8ca282 608 static void SD_Detect_MspDeInit(void)
bcostm 0:d83f1c8ca282 609 {
bcostm 0:d83f1c8ca282 610 /* Disable all interrupts */
bcostm 0:d83f1c8ca282 611 /*HAL_NVIC_DisableIRQ(MFX_INT_EXTI_IRQn);*/
bcostm 0:d83f1c8ca282 612
bcostm 0:d83f1c8ca282 613 if (BSP_IO_Init() == IO_ERROR)
bcostm 0:d83f1c8ca282 614 {
bcostm 0:d83f1c8ca282 615 BSP_ErrorHandler();
bcostm 0:d83f1c8ca282 616 }
bcostm 0:d83f1c8ca282 617 BSP_IO_ConfigPin(SD_DETECT_PIN, IO_MODE_ANALOG);
bcostm 0:d83f1c8ca282 618 }
bcostm 0:d83f1c8ca282 619
bcostm 0:d83f1c8ca282 620 /**
bcostm 0:d83f1c8ca282 621 * @brief Configure the DMA to receive data from the SD card
bcostm 0:d83f1c8ca282 622 * @retval
bcostm 0:d83f1c8ca282 623 * HAL_ERROR or HAL_OK
bcostm 0:d83f1c8ca282 624 */
bcostm 0:d83f1c8ca282 625 static HAL_StatusTypeDef SD_DMAConfigRx(SD_HandleTypeDef *hsd)
bcostm 0:d83f1c8ca282 626 {
bcostm 0:d83f1c8ca282 627 static DMA_HandleTypeDef hdma_rx;
bcostm 0:d83f1c8ca282 628 HAL_StatusTypeDef status = HAL_ERROR;
bcostm 0:d83f1c8ca282 629
bcostm 0:d83f1c8ca282 630 /* Configure DMA Rx parameters */
bcostm 0:d83f1c8ca282 631 hdma_rx.Init.Request = DMA_REQUEST_7;
bcostm 0:d83f1c8ca282 632 hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
bcostm 0:d83f1c8ca282 633 hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
bcostm 0:d83f1c8ca282 634 hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
bcostm 0:d83f1c8ca282 635 hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
bcostm 0:d83f1c8ca282 636 hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
bcostm 0:d83f1c8ca282 637 hdma_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
bcostm 0:d83f1c8ca282 638
bcostm 0:d83f1c8ca282 639 hdma_rx.Instance = SD_DMAx_STREAM;
bcostm 0:d83f1c8ca282 640
bcostm 0:d83f1c8ca282 641 /* Associate the DMA handle */
bcostm 0:d83f1c8ca282 642 __HAL_LINKDMA(hsd, hdmarx, hdma_rx);
bcostm 0:d83f1c8ca282 643
bcostm 0:d83f1c8ca282 644 /* Stop any ongoing transfer and reset the state*/
bcostm 0:d83f1c8ca282 645 HAL_DMA_Abort(&hdma_rx);
bcostm 0:d83f1c8ca282 646
bcostm 0:d83f1c8ca282 647 /* Deinitialize the Channel for new transfer */
bcostm 0:d83f1c8ca282 648 HAL_DMA_DeInit(&hdma_rx);
bcostm 0:d83f1c8ca282 649
bcostm 0:d83f1c8ca282 650 /* Configure the DMA Channel */
bcostm 0:d83f1c8ca282 651 status = HAL_DMA_Init(&hdma_rx);
bcostm 0:d83f1c8ca282 652
bcostm 0:d83f1c8ca282 653 /* NVIC configuration for DMA transfer complete interrupt */
bcostm 0:d83f1c8ca282 654 HAL_NVIC_SetPriority(SD_DMAx_IRQn, 6, 0);
bcostm 0:d83f1c8ca282 655 HAL_NVIC_EnableIRQ(SD_DMAx_IRQn);
bcostm 0:d83f1c8ca282 656
bcostm 0:d83f1c8ca282 657 return (status);
bcostm 0:d83f1c8ca282 658 }
bcostm 0:d83f1c8ca282 659
bcostm 0:d83f1c8ca282 660 /**
bcostm 0:d83f1c8ca282 661 * @brief Configure the DMA to transmit data to the SD card
bcostm 0:d83f1c8ca282 662 * @retval
bcostm 0:d83f1c8ca282 663 * HAL_ERROR or HAL_OK
bcostm 0:d83f1c8ca282 664 */
bcostm 0:d83f1c8ca282 665 static HAL_StatusTypeDef SD_DMAConfigTx(SD_HandleTypeDef *hsd)
bcostm 0:d83f1c8ca282 666 {
bcostm 0:d83f1c8ca282 667 static DMA_HandleTypeDef hdma_tx;
bcostm 0:d83f1c8ca282 668 HAL_StatusTypeDef status;
bcostm 0:d83f1c8ca282 669
bcostm 0:d83f1c8ca282 670 /* Configure DMA Tx parameters */
bcostm 0:d83f1c8ca282 671 hdma_tx.Init.Request = DMA_REQUEST_7;
bcostm 0:d83f1c8ca282 672 hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
bcostm 0:d83f1c8ca282 673 hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
bcostm 0:d83f1c8ca282 674 hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
bcostm 0:d83f1c8ca282 675 hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
bcostm 0:d83f1c8ca282 676 hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
bcostm 0:d83f1c8ca282 677 hdma_tx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
bcostm 0:d83f1c8ca282 678
bcostm 0:d83f1c8ca282 679 hdma_tx.Instance = SD_DMAx_STREAM;
bcostm 0:d83f1c8ca282 680
bcostm 0:d83f1c8ca282 681 /* Associate the DMA handle */
bcostm 0:d83f1c8ca282 682 __HAL_LINKDMA(hsd, hdmatx, hdma_tx);
bcostm 0:d83f1c8ca282 683
bcostm 0:d83f1c8ca282 684 /* Stop any ongoing transfer and reset the state*/
bcostm 0:d83f1c8ca282 685 HAL_DMA_Abort(&hdma_tx);
bcostm 0:d83f1c8ca282 686
bcostm 0:d83f1c8ca282 687 /* Deinitialize the Channel for new transfer */
bcostm 0:d83f1c8ca282 688 HAL_DMA_DeInit(&hdma_tx);
bcostm 0:d83f1c8ca282 689
bcostm 0:d83f1c8ca282 690 /* Configure the DMA Channel */
bcostm 0:d83f1c8ca282 691 status = HAL_DMA_Init(&hdma_tx);
bcostm 0:d83f1c8ca282 692
bcostm 0:d83f1c8ca282 693 /* NVIC configuration for DMA transfer complete interrupt */
bcostm 0:d83f1c8ca282 694 HAL_NVIC_SetPriority(SD_DMAx_IRQn, 6, 0);
bcostm 0:d83f1c8ca282 695 HAL_NVIC_EnableIRQ(SD_DMAx_IRQn);
bcostm 0:d83f1c8ca282 696
bcostm 0:d83f1c8ca282 697 return (status);
bcostm 0:d83f1c8ca282 698 }
bcostm 0:d83f1c8ca282 699
bcostm 0:d83f1c8ca282 700 /**
bcostm 0:d83f1c8ca282 701 * @}
bcostm 0:d83f1c8ca282 702 */
bcostm 0:d83f1c8ca282 703
bcostm 0:d83f1c8ca282 704 /**
bcostm 0:d83f1c8ca282 705 * @}
bcostm 0:d83f1c8ca282 706 */
bcostm 0:d83f1c8ca282 707
bcostm 0:d83f1c8ca282 708 /**
bcostm 0:d83f1c8ca282 709 * @}
bcostm 0:d83f1c8ca282 710 */
bcostm 0:d83f1c8ca282 711
bcostm 0:d83f1c8ca282 712 /**
bcostm 0:d83f1c8ca282 713 * @}
bcostm 0:d83f1c8ca282 714 */
bcostm 0:d83f1c8ca282 715
bcostm 0:d83f1c8ca282 716 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/