stm32h7xx_hal_sdram.c 38.1 KB
Newer Older
whj123999's avatar
whj123999 已提交
1 2 3 4 5
/**
  ******************************************************************************
  * @file    stm32h7xx_hal_sdram.c
  * @author  MCD Application Team
  * @brief   SDRAM HAL module driver.
6
  *          This file provides a generic firmware to drive SDRAM memories mounted
whj123999's avatar
whj123999 已提交
7
  *          as external device.
8
  *
whj123999's avatar
whj123999 已提交
9 10 11
  @verbatim
  ==============================================================================
                       ##### How to use this driver #####
12
  ==============================================================================
whj123999's avatar
whj123999 已提交
13
  [..]
14 15 16
    This driver is a generic layered driver which contains a set of APIs used to
    control SDRAM memories. It uses the FMC layer functions to interface
    with SDRAM devices.
whj123999's avatar
whj123999 已提交
17
    The following sequence should be followed to configure the FMC to interface
18 19
    with SDRAM memories:

whj123999's avatar
whj123999 已提交
20
   (#) Declare a SDRAM_HandleTypeDef handle structure, for example:
21 22 23
          SDRAM_HandleTypeDef  hsdram

       (++) Fill the SDRAM_HandleTypeDef handle "Init" field with the allowed
whj123999's avatar
whj123999 已提交
24
            values of the structure member.
25 26 27 28

       (++) Fill the SDRAM_HandleTypeDef handle "Instance" field with a predefined
            base register instance for NOR or SDRAM device

whj123999's avatar
whj123999 已提交
29 30 31
   (#) Declare a FMC_SDRAM_TimingTypeDef structure; for example:
          FMC_SDRAM_TimingTypeDef  Timing;
      and fill its fields with the allowed values of the structure member.
32

whj123999's avatar
whj123999 已提交
33 34
   (#) Initialize the SDRAM Controller by calling the function HAL_SDRAM_Init(). This function
       performs the following sequence:
35

whj123999's avatar
whj123999 已提交
36
       (##) MSP hardware layer configuration using the function HAL_SDRAM_MspInit()
37
       (##) Control register configuration using the FMC SDRAM interface function
whj123999's avatar
whj123999 已提交
38
            FMC_SDRAM_Init()
39
       (##) Timing register configuration using the FMC SDRAM interface function
whj123999's avatar
whj123999 已提交
40 41 42
            FMC_SDRAM_Timing_Init()
       (##) Program the SDRAM external device by applying its initialization sequence
            according to the device plugged in your hardware. This step is mandatory
43
            for accessing the SDRAM device.
whj123999's avatar
whj123999 已提交
44

45
   (#) At this stage you can perform read/write accesses from/to the memory connected
whj123999's avatar
whj123999 已提交
46 47 48 49
       to the SDRAM Bank. You can perform either polling or DMA transfer using the
       following APIs:
       (++) HAL_SDRAM_Read()/HAL_SDRAM_Write() for polling read/write access
       (++) HAL_SDRAM_Read_DMA()/HAL_SDRAM_Write_DMA() for DMA read/write transfer
50

whj123999's avatar
whj123999 已提交
51
   (#) You can also control the SDRAM device by calling the control APIs HAL_SDRAM_WriteOperation_Enable()/
52
       HAL_SDRAM_WriteOperation_Disable() to respectively enable/disable the SDRAM write operation or
whj123999's avatar
whj123999 已提交
53
       the function HAL_SDRAM_SendCommand() to send a specified command to the SDRAM
54 55 56
       device. The command to be sent must be configured with the FMC_SDRAM_CommandTypeDef
       structure.

whj123999's avatar
whj123999 已提交
57
   (#) You can continuously monitor the SDRAM device HAL state by calling the function
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
       HAL_SDRAM_GetState()

   *** Callback registration ***
    =============================================
    [..]
      The compilation define  USE_HAL_SDRAM_REGISTER_CALLBACKS when set to 1
      allows the user to configure dynamically the driver callbacks.

      Use Functions @ref HAL_SDRAM_RegisterCallback() to register a user callback,
      it allows to register following callbacks:
        (+) MspInitCallback    : SDRAM MspInit.
        (+) MspDeInitCallback  : SDRAM MspDeInit.
      This function takes as parameters the HAL peripheral handle, the Callback ID
      and a pointer to the user callback function.

      Use function @ref HAL_SDRAM_UnRegisterCallback() to reset a callback to the default
      weak (surcharged) function. It allows to reset following callbacks:
        (+) MspInitCallback    : SDRAM MspInit.
        (+) MspDeInitCallback  : SDRAM MspDeInit.
      This function) takes as parameters the HAL peripheral handle and the Callback ID.

      By default, after the @ref HAL_SDRAM_Init and if the state is HAL_SDRAM_STATE_RESET
      all callbacks are reset to the corresponding legacy weak (surcharged) functions.
      Exception done for MspInit and MspDeInit callbacks that are respectively
      reset to the legacy weak (surcharged) functions in the @ref HAL_SDRAM_Init
      and @ref  HAL_SDRAM_DeInit only when these callbacks are null (not registered beforehand).
      If not, MspInit or MspDeInit are not null, the @ref HAL_SDRAM_Init and @ref HAL_SDRAM_DeInit
      keep and use the user MspInit/MspDeInit callbacks (registered beforehand)

      Callbacks can be registered/unregistered in READY state only.
      Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
      in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
      during the Init/DeInit.
      In that case first register the MspInit/MspDeInit user callbacks
      using @ref HAL_SDRAM_RegisterCallback before calling @ref HAL_SDRAM_DeInit
      or @ref HAL_SDRAM_Init function.

      When The compilation define USE_HAL_SDRAM_REGISTER_CALLBACKS is set to 0 or
      not defined, the callback registering feature is not available
      and weak (surcharged) callbacks are used.

whj123999's avatar
whj123999 已提交
99 100 101 102
  @endverbatim
  ******************************************************************************
  * @attention
  *
M
Mr.Tiger 已提交
103 104
  * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  * All rights reserved.</center></h2>
whj123999's avatar
whj123999 已提交
105
  *
106 107 108 109
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                       opensource.org/licenses/BSD-3-Clause
whj123999's avatar
whj123999 已提交
110 111
  *
  ******************************************************************************
112
  */
whj123999's avatar
whj123999 已提交
113 114 115 116

/* Includes ------------------------------------------------------------------*/
#include "stm32h7xx_hal.h"

117

whj123999's avatar
whj123999 已提交
118 119 120 121
/** @addtogroup STM32H7xx_HAL_Driver
  * @{
  */

122 123
#ifdef HAL_SDRAM_MODULE_ENABLED

whj123999's avatar
whj123999 已提交
124 125 126 127 128
/** @defgroup SDRAM SDRAM
  * @brief SDRAM driver modules
  * @{
  */

129 130 131
/**
  @cond 0
  */
whj123999's avatar
whj123999 已提交
132 133
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
134
/* Private macro -------------------------------------------------------------*/
whj123999's avatar
whj123999 已提交
135
/* Private variables ---------------------------------------------------------*/
136 137 138 139 140 141 142 143
/* Private function prototypes -----------------------------------------------*/
static void SDRAM_DMACplt    (MDMA_HandleTypeDef *hmdma);
static void SDRAM_DMACpltProt(MDMA_HandleTypeDef *hmdma);
static void SDRAM_DMAError   (MDMA_HandleTypeDef *hmdma);
/**
  @endcond
  */

whj123999's avatar
whj123999 已提交
144 145 146 147 148
/* Exported functions --------------------------------------------------------*/
/** @defgroup SDRAM_Exported_Functions SDRAM Exported Functions
  * @{
  */

149 150
/** @defgroup SDRAM_Exported_Functions_Group1 Initialization and de-initialization functions
  * @brief    Initialization and Configuration functions
whj123999's avatar
whj123999 已提交
151
  *
152
  @verbatim
whj123999's avatar
whj123999 已提交
153 154 155
  ==============================================================================
           ##### SDRAM Initialization and de_initialization functions #####
  ==============================================================================
156
  [..]
whj123999's avatar
whj123999 已提交
157 158
    This section provides functions allowing to initialize/de-initialize
    the SDRAM memory
159

whj123999's avatar
whj123999 已提交
160 161 162
@endverbatim
  * @{
  */
163

whj123999's avatar
whj123999 已提交
164 165
/**
  * @brief  Performs the SDRAM device initialization sequence.
166
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
167
  *                the configuration information for SDRAM module.
168
  * @param  Timing Pointer to SDRAM control timing structure
whj123999's avatar
whj123999 已提交
169 170 171
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_SDRAM_Init(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_TimingTypeDef *Timing)
172
{
whj123999's avatar
whj123999 已提交
173
  /* Check the SDRAM handle parameter */
174
  if (hsdram == NULL)
whj123999's avatar
whj123999 已提交
175 176 177
  {
    return HAL_ERROR;
  }
178 179 180

  if (hsdram->State == HAL_SDRAM_STATE_RESET)
  {
whj123999's avatar
whj123999 已提交
181 182
    /* Allocate lock resource and initialize it */
    hsdram->Lock = HAL_UNLOCKED;
183 184 185 186 187 188 189 190 191 192 193 194
#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
    if(hsdram->MspInitCallback == NULL)
    {
      hsdram->MspInitCallback = HAL_SDRAM_MspInit;
    }
    hsdram->RefreshErrorCallback = HAL_SDRAM_RefreshErrorCallback;
    hsdram->DmaXferCpltCallback = HAL_SDRAM_DMA_XferCpltCallback;
    hsdram->DmaXferErrorCallback = HAL_SDRAM_DMA_XferErrorCallback;

    /* Init the low level hardware */
    hsdram->MspInitCallback(hsdram);
#else
whj123999's avatar
whj123999 已提交
195 196
    /* Initialize the low level hardware (MSP) */
    HAL_SDRAM_MspInit(hsdram);
197
#endif
whj123999's avatar
whj123999 已提交
198
  }
199

whj123999's avatar
whj123999 已提交
200 201
  /* Initialize the SDRAM controller state */
  hsdram->State = HAL_SDRAM_STATE_BUSY;
202

whj123999's avatar
whj123999 已提交
203
  /* Initialize SDRAM control Interface */
204 205
  (void)FMC_SDRAM_Init(hsdram->Instance, &(hsdram->Init));

whj123999's avatar
whj123999 已提交
206
  /* Initialize SDRAM timing Interface */
207 208 209
  (void)FMC_SDRAM_Timing_Init(hsdram->Instance, Timing, hsdram->Init.SDBank);

  /* Enable FMC Peripheral */
whj123999's avatar
whj123999 已提交
210 211 212
  __FMC_ENABLE();
  /* Update the SDRAM controller state */
  hsdram->State = HAL_SDRAM_STATE_READY;
213

whj123999's avatar
whj123999 已提交
214 215 216 217 218
  return HAL_OK;
}

/**
  * @brief  Perform the SDRAM device initialization sequence.
219
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
220 221 222 223 224
  *                the configuration information for SDRAM module.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_SDRAM_DeInit(SDRAM_HandleTypeDef *hsdram)
{
225 226 227 228 229 230 231 232 233
#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
  if(hsdram->MspDeInitCallback == NULL)
  {
    hsdram->MspDeInitCallback = HAL_SDRAM_MspDeInit;
  }

  /* DeInit the low level hardware */
  hsdram->MspDeInitCallback(hsdram);
#else
whj123999's avatar
whj123999 已提交
234 235
  /* Initialize the low level hardware (MSP) */
  HAL_SDRAM_MspDeInit(hsdram);
236
#endif
whj123999's avatar
whj123999 已提交
237 238

  /* Configure the SDRAM registers with their reset values */
239
  (void)FMC_SDRAM_DeInit(hsdram->Instance, hsdram->Init.SDBank);
whj123999's avatar
whj123999 已提交
240 241 242 243 244 245 246 247 248 249 250 251

  /* Reset the SDRAM controller state */
  hsdram->State = HAL_SDRAM_STATE_RESET;

  /* Release Lock */
  __HAL_UNLOCK(hsdram);

  return HAL_OK;
}

/**
  * @brief  SDRAM MSP Init.
252
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
253 254 255 256 257 258 259
  *                the configuration information for SDRAM module.
  * @retval None
  */
__weak void HAL_SDRAM_MspInit(SDRAM_HandleTypeDef *hsdram)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hsdram);
260

whj123999's avatar
whj123999 已提交
261 262
  /* NOTE: This function Should not be modified, when the callback is needed,
            the HAL_SDRAM_MspInit could be implemented in the user file
263
   */
whj123999's avatar
whj123999 已提交
264 265 266 267
}

/**
  * @brief  SDRAM MSP DeInit.
268
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
269 270 271 272 273 274 275
  *                the configuration information for SDRAM module.
  * @retval None
  */
__weak void HAL_SDRAM_MspDeInit(SDRAM_HandleTypeDef *hsdram)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hsdram);
276

whj123999's avatar
whj123999 已提交
277 278
  /* NOTE: This function Should not be modified, when the callback is needed,
            the HAL_SDRAM_MspDeInit could be implemented in the user file
279
   */
whj123999's avatar
whj123999 已提交
280 281 282 283
}

/**
  * @brief  This function handles SDRAM refresh error interrupt request.
284
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
285 286 287 288 289 290
  *                the configuration information for SDRAM module.
  * @retval HAL status
*/
void HAL_SDRAM_IRQHandler(SDRAM_HandleTypeDef *hsdram)
{
  /* Check SDRAM interrupt Rising edge flag */
291
  if (__FMC_SDRAM_GET_FLAG(hsdram->Instance, FMC_SDRAM_FLAG_REFRESH_IT))
whj123999's avatar
whj123999 已提交
292 293
  {
    /* SDRAM refresh error interrupt callback */
294 295 296
#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
    hsdram->RefreshErrorCallback(hsdram);
#else
whj123999's avatar
whj123999 已提交
297
    HAL_SDRAM_RefreshErrorCallback(hsdram);
298 299
#endif

whj123999's avatar
whj123999 已提交
300 301 302 303 304 305 306
    /* Clear SDRAM refresh error interrupt pending bit */
    __FMC_SDRAM_CLEAR_FLAG(hsdram->Instance, FMC_SDRAM_FLAG_REFRESH_ERROR);
  }
}

/**
  * @brief  SDRAM Refresh error callback.
307 308
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  *                the configuration information for SDRAM module.
whj123999's avatar
whj123999 已提交
309 310 311 312 313 314
  * @retval None
  */
__weak void HAL_SDRAM_RefreshErrorCallback(SDRAM_HandleTypeDef *hsdram)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hsdram);
315

whj123999's avatar
whj123999 已提交
316 317
  /* NOTE: This function Should not be modified, when the callback is needed,
            the HAL_SDRAM_RefreshErrorCallback could be implemented in the user file
318
   */
whj123999's avatar
whj123999 已提交
319 320 321 322
}

/**
  * @brief  DMA transfer complete callback.
323
  * @param  hmdma pointer to a DMA_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
324 325 326 327 328 329 330
  *                the configuration information for the specified DMA module.
  * @retval None
  */
__weak void HAL_SDRAM_DMA_XferCpltCallback(MDMA_HandleTypeDef *hmdma)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hmdma);
331

whj123999's avatar
whj123999 已提交
332 333
  /* NOTE: This function Should not be modified, when the callback is needed,
            the HAL_SDRAM_DMA_XferCpltCallback could be implemented in the user file
334
   */
whj123999's avatar
whj123999 已提交
335 336 337 338
}

/**
  * @brief  DMA transfer complete error callback.
339
  * @param  hmdma DMA handle
whj123999's avatar
whj123999 已提交
340 341 342 343 344 345
  * @retval None
  */
__weak void HAL_SDRAM_DMA_XferErrorCallback(MDMA_HandleTypeDef *hmdma)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hmdma);
346

whj123999's avatar
whj123999 已提交
347 348
  /* NOTE: This function Should not be modified, when the callback is needed,
            the HAL_SDRAM_DMA_XferErrorCallback could be implemented in the user file
349
   */
whj123999's avatar
whj123999 已提交
350 351 352 353 354 355
}

/**
  * @}
  */

356 357
/** @defgroup SDRAM_Exported_Functions_Group2 Input and Output functions
  * @brief    Input Output and memory control functions
whj123999's avatar
whj123999 已提交
358
  *
359
  @verbatim
whj123999's avatar
whj123999 已提交
360 361 362
  ==============================================================================
                    ##### SDRAM Input and Output functions #####
  ==============================================================================
363
  [..]
whj123999's avatar
whj123999 已提交
364
    This section provides functions allowing to use and control the SDRAM memory
365

whj123999's avatar
whj123999 已提交
366 367 368 369 370 371
@endverbatim
  * @{
  */

/**
  * @brief  Reads 8-bit data buffer from the SDRAM memory.
372
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
373
  *                the configuration information for SDRAM module.
374 375 376
  * @param  pAddress Pointer to read start address
  * @param  pDstBuffer Pointer to destination buffer
  * @param  BufferSize Size of the buffer to read from memory
whj123999's avatar
whj123999 已提交
377 378 379 380
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_SDRAM_Read_8b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint8_t *pDstBuffer, uint32_t BufferSize)
{
381
  uint32_t size;
whj123999's avatar
whj123999 已提交
382
  __IO uint8_t *pSdramAddress = (uint8_t *)pAddress;
383 384 385
  uint8_t * pdestbuff = pDstBuffer;
  HAL_SDRAM_StateTypeDef state = hsdram->State;

whj123999's avatar
whj123999 已提交
386
  /* Check the SDRAM controller state */
387
  if (state == HAL_SDRAM_STATE_BUSY)
whj123999's avatar
whj123999 已提交
388 389 390
  {
    return HAL_BUSY;
  }
391
  else if ((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
whj123999's avatar
whj123999 已提交
392
  {
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
    /* Process Locked */
    __HAL_LOCK(hsdram);

    /* Update the SDRAM controller state */
    hsdram->State = HAL_SDRAM_STATE_BUSY;

    /* Read data from source */
    for (size = BufferSize; size != 0U; size--)
    {
      *pdestbuff = *(__IO uint8_t *)pSdramAddress;
      pdestbuff++;
      pSdramAddress++;
    }

    /* Update the SDRAM controller state */
    hsdram->State = state;

    /* Process Unlocked */
    __HAL_UNLOCK(hsdram);
  }
  else
whj123999's avatar
whj123999 已提交
414
  {
415
    return  HAL_ERROR;
whj123999's avatar
whj123999 已提交
416 417
  }

418 419
  return HAL_OK;
}
whj123999's avatar
whj123999 已提交
420 421 422

/**
  * @brief  Writes 8-bit data buffer to SDRAM memory.
423
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
424
  *                the configuration information for SDRAM module.
425 426 427
  * @param  pAddress Pointer to write start address
  * @param  pSrcBuffer Pointer to source buffer to write
  * @param  BufferSize Size of the buffer to write to memory
whj123999's avatar
whj123999 已提交
428 429 430 431
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_SDRAM_Write_8b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint8_t *pSrcBuffer, uint32_t BufferSize)
{
432
  uint32_t size;
whj123999's avatar
whj123999 已提交
433
  __IO uint8_t *pSdramAddress = (uint8_t *)pAddress;
434
  uint8_t * psrcbuff = pSrcBuffer;
whj123999's avatar
whj123999 已提交
435 436
  
  /* Check the SDRAM controller state */
437
  if (hsdram->State == HAL_SDRAM_STATE_BUSY)
whj123999's avatar
whj123999 已提交
438 439 440
  {
    return HAL_BUSY;
  }
441
  else if (hsdram->State == HAL_SDRAM_STATE_READY)
whj123999's avatar
whj123999 已提交
442
  {
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    /* Process Locked */
    __HAL_LOCK(hsdram);

    /* Update the SDRAM controller state */
    hsdram->State = HAL_SDRAM_STATE_BUSY;

    /* Write data to memory */
    for (size = BufferSize; size != 0U; size--)
    {
      *(__IO uint8_t *)pSdramAddress = *psrcbuff;
      psrcbuff++;
      pSdramAddress++;
    }

    /* Update the SDRAM controller state */
    hsdram->State = HAL_SDRAM_STATE_READY;
	
    /* Process Unlocked */
    __HAL_UNLOCK(hsdram);
whj123999's avatar
whj123999 已提交
462
  }
463
  else
whj123999's avatar
whj123999 已提交
464
  {
465
    return  HAL_ERROR;
whj123999's avatar
whj123999 已提交
466 467
  }

468 469
  return HAL_OK;
}
whj123999's avatar
whj123999 已提交
470 471

/**
472 473
  * @brief  Reads 16-bit data buffer from the SDRAM memory.
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
474
  *                the configuration information for SDRAM module.
475 476 477
  * @param  pAddress Pointer to read start address
  * @param  pDstBuffer Pointer to destination buffer
  * @param  BufferSize Size of the buffer to read from memory
whj123999's avatar
whj123999 已提交
478 479 480 481
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_SDRAM_Read_16b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint16_t *pDstBuffer, uint32_t BufferSize)
{
482 483 484 485 486
  uint32_t size;
  __IO uint32_t *pSdramAddress = pAddress;
  uint16_t *pdestbuff = pDstBuffer;
  HAL_SDRAM_StateTypeDef state = hsdram->State;

whj123999's avatar
whj123999 已提交
487
  /* Check the SDRAM controller state */
488
  if (state == HAL_SDRAM_STATE_BUSY)
whj123999's avatar
whj123999 已提交
489 490 491
  {
    return HAL_BUSY;
  }
492
  else if ((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
whj123999's avatar
whj123999 已提交
493
  {
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
    /* Process Locked */
    __HAL_LOCK(hsdram);

    /* Update the SDRAM controller state */
    hsdram->State = HAL_SDRAM_STATE_BUSY;

    /* Read data from memory */
    for (size = BufferSize; size >= 2U ; size-=2U)
    {
      *pdestbuff = (uint16_t)((*pSdramAddress) & 0x0000FFFFU);
      pdestbuff++;
      *pdestbuff = (uint16_t)(((*pSdramAddress) & 0xFFFF0000U) >> 16U);
      pdestbuff++;
      pSdramAddress++;
    }

    /* Read last 16-bits if size is not 32-bits multiple */
    if ((BufferSize % 2U)!= 0U)
    {
      *pdestbuff = (uint16_t)((*pSdramAddress) & 0x0000FFFFU);
    }

    /* Update the SDRAM controller state */
    hsdram->State = state;

    /* Process Unlocked */
    __HAL_UNLOCK(hsdram);
  }
  else
whj123999's avatar
whj123999 已提交
523
  {
524
    return  HAL_ERROR;
whj123999's avatar
whj123999 已提交
525
  }
526 527

  return HAL_OK;
whj123999's avatar
whj123999 已提交
528 529 530
}

/**
531 532
  * @brief  Writes 16-bit data buffer to SDRAM memory.
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
533
  *                the configuration information for SDRAM module.
534 535 536
  * @param  pAddress Pointer to write start address
  * @param  pSrcBuffer Pointer to source buffer to write
  * @param  BufferSize Size of the buffer to write to memory
whj123999's avatar
whj123999 已提交
537 538 539 540
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_SDRAM_Write_16b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint16_t *pSrcBuffer, uint32_t BufferSize)
{
541 542 543
  uint32_t size;
  __IO uint32_t *psdramaddress = pAddress;
  uint16_t * psrcbuff = pSrcBuffer;
whj123999's avatar
whj123999 已提交
544 545
  
  /* Check the SDRAM controller state */
546
  if (hsdram->State == HAL_SDRAM_STATE_BUSY)
whj123999's avatar
whj123999 已提交
547 548 549
  {
    return HAL_BUSY;
  }
550
  else if (hsdram->State == HAL_SDRAM_STATE_READY)
whj123999's avatar
whj123999 已提交
551
  {
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
    /* Process Locked */
    __HAL_LOCK(hsdram);

    /* Update the SDRAM controller state */
    hsdram->State = HAL_SDRAM_STATE_BUSY;
    
    /* Write data to memory */
    for (size = BufferSize; size >= 2U ; size-=2U)
    {
      *psdramaddress = (uint32_t)(*psrcbuff);
      psrcbuff++;
      *psdramaddress |= ((uint32_t)(*psrcbuff) << 16U);
      psrcbuff++;
      psdramaddress++;
    }

    /* Write last 16-bits if size is not 32-bits multiple */
    if ((BufferSize % 2U)!= 0U)
    {
      *psdramaddress = ((uint32_t)(*psrcbuff) & 0x0000FFFFU) | ((*psdramaddress) & 0xFFFF0000U);
    }

    /* Update the SDRAM controller state */
    hsdram->State = HAL_SDRAM_STATE_READY;
	
    /* Process Unlocked */
    __HAL_UNLOCK(hsdram);
whj123999's avatar
whj123999 已提交
579
  }
580
  else
whj123999's avatar
whj123999 已提交
581
  {
582
    return  HAL_ERROR;
whj123999's avatar
whj123999 已提交
583
  }
584 585

  return HAL_OK;
whj123999's avatar
whj123999 已提交
586 587 588
}

/**
589 590
  * @brief  Reads 32-bit data buffer from the SDRAM memory.
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
591
  *                the configuration information for SDRAM module.
592 593 594
  * @param  pAddress Pointer to read start address
  * @param  pDstBuffer Pointer to destination buffer
  * @param  BufferSize Size of the buffer to read from memory
whj123999's avatar
whj123999 已提交
595 596 597 598
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_SDRAM_Read_32b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
{
599
  uint32_t size;
whj123999's avatar
whj123999 已提交
600
  __IO uint32_t *pSdramAddress = (uint32_t *)pAddress;
601 602 603
  uint32_t * pdestbuff = pDstBuffer;
  HAL_SDRAM_StateTypeDef state = hsdram->State;

whj123999's avatar
whj123999 已提交
604
  /* Check the SDRAM controller state */
605
  if (state == HAL_SDRAM_STATE_BUSY)
whj123999's avatar
whj123999 已提交
606 607 608
  {
    return HAL_BUSY;
  }
609
  else if ((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
whj123999's avatar
whj123999 已提交
610
  {
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
    /* Process Locked */
    __HAL_LOCK(hsdram);

    /* Update the SDRAM controller state */
    hsdram->State = HAL_SDRAM_STATE_BUSY;

    /* Read data from source */
    for (size = BufferSize; size != 0U; size--)
    {
      *pdestbuff = *(__IO uint32_t *)pSdramAddress;
      pdestbuff++;
      pSdramAddress++;
    }

    /* Update the SDRAM controller state */
    hsdram->State = state;

    /* Process Unlocked */
    __HAL_UNLOCK(hsdram);
  }
  else
whj123999's avatar
whj123999 已提交
632
  {
633
    return  HAL_ERROR;
whj123999's avatar
whj123999 已提交
634
  }
635 636

  return HAL_OK;
whj123999's avatar
whj123999 已提交
637 638 639
}

/**
640 641
  * @brief  Writes 32-bit data buffer to SDRAM memory.
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
642
  *                the configuration information for SDRAM module.
643 644 645
  * @param  pAddress Pointer to write start address
  * @param  pSrcBuffer Pointer to source buffer to write
  * @param  BufferSize Size of the buffer to write to memory
whj123999's avatar
whj123999 已提交
646 647 648 649
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_SDRAM_Write_32b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)
{
650 651 652 653
  uint32_t size;
  __IO uint32_t *pSdramAddress = pAddress;
  uint32_t * psrcbuff = pSrcBuffer;

whj123999's avatar
whj123999 已提交
654
  /* Check the SDRAM controller state */
655
  if (hsdram->State == HAL_SDRAM_STATE_BUSY)
whj123999's avatar
whj123999 已提交
656 657 658
  {
    return HAL_BUSY;
  }
659
  else if (hsdram->State == HAL_SDRAM_STATE_READY)
whj123999's avatar
whj123999 已提交
660
  {
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
    /* Process Locked */
    __HAL_LOCK(hsdram);

    /* Update the SDRAM controller state */
    hsdram->State = HAL_SDRAM_STATE_BUSY;

    /* Write data to memory */
    for (size = BufferSize; size != 0U; size--)
    {
      *pSdramAddress = *psrcbuff;
      psrcbuff++;
      pSdramAddress++;
    }

    /* Update the SDRAM controller state */
    hsdram->State = HAL_SDRAM_STATE_READY;
	
    /* Process Unlocked */
    __HAL_UNLOCK(hsdram);
whj123999's avatar
whj123999 已提交
680
  }
681
  else
whj123999's avatar
whj123999 已提交
682
  {
683
    return  HAL_ERROR;
whj123999's avatar
whj123999 已提交
684
  }
685 686

  return HAL_OK;
whj123999's avatar
whj123999 已提交
687 688 689
}

/**
690 691
  * @brief  Reads a Words data from the SDRAM memory using DMA transfer.
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
692
  *                the configuration information for SDRAM module.
693 694 695
  * @param  pAddress Pointer to read start address
  * @param  pDstBuffer Pointer to destination buffer
  * @param  BufferSize Size of the buffer to read from memory
whj123999's avatar
whj123999 已提交
696 697 698 699
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_SDRAM_Read_DMA(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
{
700 701 702 703 704
  HAL_StatusTypeDef status;
  HAL_SDRAM_StateTypeDef state = hsdram->State;

  /* Check the SDRAM controller state */
  if (state == HAL_SDRAM_STATE_BUSY)
whj123999's avatar
whj123999 已提交
705 706 707
  {
    return HAL_BUSY;
  }
708
  else if ((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
whj123999's avatar
whj123999 已提交
709
  {
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
    /* Process Locked */
    __HAL_LOCK(hsdram);

    /* Update the SDRAM controller state */
    hsdram->State = HAL_SDRAM_STATE_BUSY;

    /* Configure DMA user callbacks */
    if (state == HAL_SDRAM_STATE_READY)
    {
      hsdram->hmdma->XferCpltCallback = SDRAM_DMACplt;
    }
    else
    {
      hsdram->hmdma->XferCpltCallback = SDRAM_DMACpltProt;
    }
    hsdram->hmdma->XferErrorCallback = SDRAM_DMAError;

	/* Enable the DMA Stream */
    status = HAL_MDMA_Start_IT(hsdram->hmdma, (uint32_t)pAddress, (uint32_t)pDstBuffer, (uint32_t)(BufferSize * 4U), 1);	

    /* Process Unlocked */
    __HAL_UNLOCK(hsdram);
  }
  else
  {
    return HAL_ERROR;
  }

  return status;
whj123999's avatar
whj123999 已提交
739 740 741 742
}

/**
  * @brief  Writes a Words data buffer to SDRAM memory using DMA transfer.
743
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
744
  *                the configuration information for SDRAM module.
745 746 747
  * @param  pAddress Pointer to write start address
  * @param  pSrcBuffer Pointer to source buffer to write
  * @param  BufferSize Size of the buffer to write to memory
whj123999's avatar
whj123999 已提交
748 749 750 751
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_SDRAM_Write_DMA(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)
{
752 753 754 755
  HAL_StatusTypeDef status;

  /* Check the SDRAM controller state */
  if (hsdram->State == HAL_SDRAM_STATE_BUSY)
whj123999's avatar
whj123999 已提交
756 757 758
  {
    return HAL_BUSY;
  }
759
  else if (hsdram->State == HAL_SDRAM_STATE_READY)
whj123999's avatar
whj123999 已提交
760
  {
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
    /* Process Locked */
    __HAL_LOCK(hsdram);

    /* Update the SDRAM controller state */
    hsdram->State = HAL_SDRAM_STATE_BUSY;

    /* Configure DMA user callbacks */
    hsdram->hmdma->XferCpltCallback = SDRAM_DMACplt;
    hsdram->hmdma->XferErrorCallback = SDRAM_DMAError;

	/* Enable the DMA Stream */
  status = HAL_MDMA_Start_IT(hsdram->hmdma, (uint32_t)pSrcBuffer, (uint32_t)pAddress, (uint32_t)(BufferSize * 4U), 1);

    /* Process Unlocked */
    __HAL_UNLOCK(hsdram);
  }
  else
  {
    return HAL_ERROR;
  }

  return status;
}

#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
/**
  * @brief  Register a User SDRAM Callback
  *         To be used instead of the weak (surcharged) predefined callback
  * @param hsdram : SDRAM handle
  * @param CallbackId : ID of the callback to be registered
  *        This parameter can be one of the following values:
  *          @arg @ref HAL_SDRAM_MSP_INIT_CB_ID       SDRAM MspInit callback ID
  *          @arg @ref HAL_SDRAM_MSP_DEINIT_CB_ID     SDRAM MspDeInit callback ID
  *          @arg @ref HAL_SDRAM_REFRESH_ERR_CB_ID    SDRAM Refresh Error callback ID
  * @param pCallback : pointer to the Callback function
  * @retval status
  */
HAL_StatusTypeDef HAL_SDRAM_RegisterCallback (SDRAM_HandleTypeDef *hsdram, HAL_SDRAM_CallbackIDTypeDef CallbackId, pSDRAM_CallbackTypeDef pCallback)
{
  HAL_StatusTypeDef status = HAL_OK;
  HAL_SDRAM_StateTypeDef state;
whj123999's avatar
whj123999 已提交
802
  
803 804 805 806 807 808 809
  if(pCallback == NULL)
  {
    return HAL_ERROR;
  }

  /* Process locked */
  __HAL_LOCK(hsdram);
whj123999's avatar
whj123999 已提交
810
  
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
  state = hsdram->State;
  if((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
  {
    switch (CallbackId)
    {
    case HAL_SDRAM_MSP_INIT_CB_ID :
      hsdram->MspInitCallback = pCallback;
      break;
    case HAL_SDRAM_MSP_DEINIT_CB_ID :
      hsdram->MspDeInitCallback = pCallback;
      break;
    case HAL_SDRAM_REFRESH_ERR_CB_ID :
      hsdram->RefreshErrorCallback = pCallback;
      break;
    default :
      /* update return status */
      status =  HAL_ERROR;
      break;
    }
  }
  else if(hsdram->State == HAL_SDRAM_STATE_RESET)
  {
    switch (CallbackId)
    {
    case HAL_SDRAM_MSP_INIT_CB_ID :
      hsdram->MspInitCallback = pCallback;
      break;
    case HAL_SDRAM_MSP_DEINIT_CB_ID :
      hsdram->MspDeInitCallback = pCallback;
      break;
    default :
      /* update return status */
      status =  HAL_ERROR;
      break;
    }
  }
  else
  {
    /* update return status */
    status =  HAL_ERROR;
  }

  /* Release Lock */
  __HAL_UNLOCK(hsdram);
  return status;
}

/**
  * @brief  Unregister a User SDRAM Callback
  *         SDRAM Callback is redirected to the weak (surcharged) predefined callback
  * @param hsdram : SDRAM handle
  * @param CallbackId : ID of the callback to be unregistered
  *        This parameter can be one of the following values:
  *          @arg @ref HAL_SDRAM_MSP_INIT_CB_ID       SDRAM MspInit callback ID
  *          @arg @ref HAL_SDRAM_MSP_DEINIT_CB_ID     SDRAM MspDeInit callback ID
  *          @arg @ref HAL_SDRAM_REFRESH_ERR_CB_ID    SDRAM Refresh Error callback ID
  *          @arg @ref HAL_SDRAM_DMA_XFER_CPLT_CB_ID  SDRAM DMA Xfer Complete callback ID
  *          @arg @ref HAL_SDRAM_DMA_XFER_ERR_CB_ID   SDRAM DMA Xfer Error callback ID
  * @retval status
  */
HAL_StatusTypeDef HAL_SDRAM_UnRegisterCallback (SDRAM_HandleTypeDef *hsdram, HAL_SDRAM_CallbackIDTypeDef CallbackId)
{
  HAL_StatusTypeDef status = HAL_OK;
  HAL_SDRAM_StateTypeDef state;
whj123999's avatar
whj123999 已提交
875
  
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
  /* Process locked */
  __HAL_LOCK(hsdram);

  state = hsdram->State;
  if((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
  {
    switch (CallbackId)
    {
    case HAL_SDRAM_MSP_INIT_CB_ID :
      hsdram->MspInitCallback = HAL_SDRAM_MspInit;
      break;
    case HAL_SDRAM_MSP_DEINIT_CB_ID :
      hsdram->MspDeInitCallback = HAL_SDRAM_MspDeInit;
      break;
    case HAL_SDRAM_REFRESH_ERR_CB_ID :
      hsdram->RefreshErrorCallback = HAL_SDRAM_RefreshErrorCallback;
      break;
    case HAL_SDRAM_DMA_XFER_CPLT_CB_ID :
      hsdram->DmaXferCpltCallback = HAL_SDRAM_DMA_XferCpltCallback;
      break;
    case HAL_SDRAM_DMA_XFER_ERR_CB_ID :
      hsdram->DmaXferErrorCallback = HAL_SDRAM_DMA_XferErrorCallback;
      break;
    default :
      /* update return status */
      status =  HAL_ERROR;
      break;
    }
  }
  else if(hsdram->State == HAL_SDRAM_STATE_RESET)
  {
    switch (CallbackId)
    {
    case HAL_SDRAM_MSP_INIT_CB_ID :
      hsdram->MspInitCallback = HAL_SDRAM_MspInit;
      break;
    case HAL_SDRAM_MSP_DEINIT_CB_ID :
      hsdram->MspDeInitCallback = HAL_SDRAM_MspDeInit;
      break;
    default :
      /* update return status */
      status =  HAL_ERROR;
      break;
    }
  }
  else
  {
    /* update return status */
    status =  HAL_ERROR;
  }

  /* Release Lock */
whj123999's avatar
whj123999 已提交
928
  __HAL_UNLOCK(hsdram);
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
  return status;
}

/**
  * @brief  Register a User SDRAM Callback for DMA transfers
  *         To be used instead of the weak (surcharged) predefined callback
  * @param hsdram : SDRAM handle
  * @param CallbackId : ID of the callback to be registered
  *        This parameter can be one of the following values:
  *          @arg @ref HAL_SDRAM_DMA_XFER_CPLT_CB_ID  SDRAM DMA Xfer Complete callback ID
  *          @arg @ref HAL_SDRAM_DMA_XFER_ERR_CB_ID   SDRAM DMA Xfer Error callback ID
  * @param pCallback : pointer to the Callback function
  * @retval status
  */
HAL_StatusTypeDef HAL_SDRAM_RegisterDmaCallback(SDRAM_HandleTypeDef *hsdram, HAL_SDRAM_CallbackIDTypeDef CallbackId, pSDRAM_DmaCallbackTypeDef pCallback)
{
  HAL_StatusTypeDef status = HAL_OK;
  HAL_SDRAM_StateTypeDef state;
whj123999's avatar
whj123999 已提交
947
  
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
  if(pCallback == NULL)
  {
    return HAL_ERROR;
  }

  /* Process locked */
  __HAL_LOCK(hsdram);

  state = hsdram->State;
  if((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_WRITE_PROTECTED))
  {
    switch (CallbackId)
    {
    case HAL_SDRAM_DMA_XFER_CPLT_CB_ID :
      hsdram->DmaXferCpltCallback = pCallback;
      break;
    case HAL_SDRAM_DMA_XFER_ERR_CB_ID :
      hsdram->DmaXferErrorCallback = pCallback;
      break;
    default :
      /* update return status */
      status =  HAL_ERROR;
      break;
    }
  }
  else
  {
    /* update return status */
    status =  HAL_ERROR;
  }

  /* Release Lock */
  __HAL_UNLOCK(hsdram);
  return status;
whj123999's avatar
whj123999 已提交
982
}
983
#endif
whj123999's avatar
whj123999 已提交
984 985 986 987

/**
  * @}
  */
988 989 990

/** @defgroup SDRAM_Exported_Functions_Group3 Control functions
 *  @brief   management functions
whj123999's avatar
whj123999 已提交
991
 *
992
@verbatim
whj123999's avatar
whj123999 已提交
993 994
  ==============================================================================
                         ##### SDRAM Control functions #####
995
  ==============================================================================
whj123999's avatar
whj123999 已提交
996 997 998 999 1000 1001 1002 1003 1004 1005
  [..]
    This subsection provides a set of functions allowing to control dynamically
    the SDRAM interface.

@endverbatim
  * @{
  */

/**
  * @brief  Enables dynamically SDRAM write protection.
1006
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
1007 1008 1009 1010
  *                the configuration information for SDRAM module.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_SDRAM_WriteProtection_Enable(SDRAM_HandleTypeDef *hsdram)
1011 1012 1013
{
  /* Check the SDRAM controller state */
  if (hsdram->State == HAL_SDRAM_STATE_BUSY)
whj123999's avatar
whj123999 已提交
1014 1015 1016
  {
    return HAL_BUSY;
  }
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
  else if (hsdram->State == HAL_SDRAM_STATE_READY)
  {
    /* Update the SDRAM state */
    hsdram->State = HAL_SDRAM_STATE_BUSY;

    /* Enable write protection */
    (void)FMC_SDRAM_WriteProtection_Enable(hsdram->Instance, hsdram->Init.SDBank);

    /* Update the SDRAM state */
    hsdram->State = HAL_SDRAM_STATE_WRITE_PROTECTED;
  }
  else
  {
    return HAL_ERROR;
  }

  return HAL_OK;
whj123999's avatar
whj123999 已提交
1034 1035 1036 1037
}

/**
  * @brief  Disables dynamically SDRAM write protection.
1038
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
1039 1040 1041 1042 1043
  *                the configuration information for SDRAM module.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_SDRAM_WriteProtection_Disable(SDRAM_HandleTypeDef *hsdram)
{
1044 1045
  HAL_SDRAM_StateTypeDef state = hsdram->State;
  
whj123999's avatar
whj123999 已提交
1046
  /* Check the SDRAM controller state */
1047
  if (state == HAL_SDRAM_STATE_BUSY)
whj123999's avatar
whj123999 已提交
1048 1049 1050
  {
    return HAL_BUSY;
  }
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
  else if (state == HAL_SDRAM_STATE_WRITE_PROTECTED)
  {
    /* Update the SDRAM state */
    hsdram->State = HAL_SDRAM_STATE_BUSY;

    /* Disable write protection */
    (void)FMC_SDRAM_WriteProtection_Disable(hsdram->Instance, hsdram->Init.SDBank);

    /* Update the SDRAM state */
    hsdram->State = HAL_SDRAM_STATE_READY;
  }
  else
  {
    return HAL_ERROR;
  }

whj123999's avatar
whj123999 已提交
1067 1068 1069 1070 1071
  return HAL_OK;
}

/**
  * @brief  Sends Command to the SDRAM bank.
1072
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
1073
  *                the configuration information for SDRAM module.
1074 1075
  * @param  Command SDRAM command structure
  * @param  Timeout Timeout duration
whj123999's avatar
whj123999 已提交
1076
  * @retval HAL status
1077
  */
whj123999's avatar
whj123999 已提交
1078 1079
HAL_StatusTypeDef HAL_SDRAM_SendCommand(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_CommandTypeDef *Command, uint32_t Timeout)
{
1080 1081
  HAL_SDRAM_StateTypeDef state = hsdram->State;
  
whj123999's avatar
whj123999 已提交
1082
  /* Check the SDRAM controller state */
1083
  if (state == HAL_SDRAM_STATE_BUSY)
whj123999's avatar
whj123999 已提交
1084 1085 1086
  {
    return HAL_BUSY;
  }
1087
  else if((state == HAL_SDRAM_STATE_READY) || (state == HAL_SDRAM_STATE_PRECHARGED))
whj123999's avatar
whj123999 已提交
1088
  {
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    /* Update the SDRAM state */
    hsdram->State = HAL_SDRAM_STATE_BUSY;

    /* Send SDRAM command */
    (void)FMC_SDRAM_SendCommand(hsdram->Instance, Command, Timeout);

    /* Update the SDRAM controller state state */
    if (Command->CommandMode == FMC_SDRAM_CMD_PALL)
    {
      hsdram->State = HAL_SDRAM_STATE_PRECHARGED;
    }
    else
    {
      hsdram->State = HAL_SDRAM_STATE_READY;
    }
whj123999's avatar
whj123999 已提交
1104 1105 1106
  }
  else
  {
1107
    return HAL_ERROR;
whj123999's avatar
whj123999 已提交
1108
  }
1109 1110

  return HAL_OK;
whj123999's avatar
whj123999 已提交
1111 1112 1113 1114
}

/**
  * @brief  Programs the SDRAM Memory Refresh rate.
1115 1116 1117
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  *                the configuration information for SDRAM module.
  * @param  RefreshRate The SDRAM refresh rate value
whj123999's avatar
whj123999 已提交
1118 1119 1120 1121 1122
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_SDRAM_ProgramRefreshRate(SDRAM_HandleTypeDef *hsdram, uint32_t RefreshRate)
{
  /* Check the SDRAM controller state */
1123
  if (hsdram->State == HAL_SDRAM_STATE_BUSY)
whj123999's avatar
whj123999 已提交
1124 1125
  {
    return HAL_BUSY;
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
  }
  else if (hsdram->State == HAL_SDRAM_STATE_READY)
  {
    /* Update the SDRAM state */
    hsdram->State = HAL_SDRAM_STATE_BUSY;

    /* Program the refresh rate */
    (void)FMC_SDRAM_ProgramRefreshRate(hsdram->Instance, RefreshRate);

    /* Update the SDRAM state */
    hsdram->State = HAL_SDRAM_STATE_READY;
  }
  else
  {
    return HAL_ERROR;
  }

  return HAL_OK;
whj123999's avatar
whj123999 已提交
1144 1145 1146 1147
}

/**
  * @brief  Sets the Number of consecutive SDRAM Memory auto Refresh commands.
1148 1149 1150
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
  *                the configuration information for SDRAM module.
  * @param  AutoRefreshNumber The SDRAM auto Refresh number
whj123999's avatar
whj123999 已提交
1151 1152 1153 1154 1155
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_SDRAM_SetAutoRefreshNumber(SDRAM_HandleTypeDef *hsdram, uint32_t AutoRefreshNumber)
{
  /* Check the SDRAM controller state */
1156
  if (hsdram->State == HAL_SDRAM_STATE_BUSY)
whj123999's avatar
whj123999 已提交
1157 1158
  {
    return HAL_BUSY;
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
  }
  else if (hsdram->State == HAL_SDRAM_STATE_READY)
  {
    /* Update the SDRAM state */
    hsdram->State = HAL_SDRAM_STATE_BUSY;

    /* Set the Auto-Refresh number */
    (void)FMC_SDRAM_SetAutoRefreshNumber(hsdram->Instance, AutoRefreshNumber);

    /* Update the SDRAM state */
    hsdram->State = HAL_SDRAM_STATE_READY;
  }
  else
  {
    return HAL_ERROR;
  }

whj123999's avatar
whj123999 已提交
1176 1177 1178 1179 1180
  return HAL_OK;
}

/**
  * @brief  Returns the SDRAM memory current mode.
1181
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
1182
  *                the configuration information for SDRAM module.
1183
  * @retval The SDRAM memory mode.
whj123999's avatar
whj123999 已提交
1184 1185 1186 1187
  */
uint32_t HAL_SDRAM_GetModeStatus(SDRAM_HandleTypeDef *hsdram)
{
  /* Return the SDRAM memory current mode */
1188
  return (FMC_SDRAM_GetModeStatus(hsdram->Instance, hsdram->Init.SDBank));
whj123999's avatar
whj123999 已提交
1189 1190 1191 1192 1193
}

/**
  * @}
  */
1194 1195 1196

/** @defgroup SDRAM_Exported_Functions_Group4 State functions
 *  @brief   Peripheral State functions
whj123999's avatar
whj123999 已提交
1197
 *
1198
@verbatim
whj123999's avatar
whj123999 已提交
1199 1200
  ==============================================================================
                      ##### SDRAM State functions #####
1201
  ==============================================================================
whj123999's avatar
whj123999 已提交
1202
  [..]
1203
    This subsection permits to get in run-time the status of the SDRAM controller
whj123999's avatar
whj123999 已提交
1204 1205 1206 1207 1208 1209 1210 1211
    and the data flow.

@endverbatim
  * @{
  */

/**
  * @brief  Returns the SDRAM state.
1212
  * @param  hsdram pointer to a SDRAM_HandleTypeDef structure that contains
whj123999's avatar
whj123999 已提交
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
  *                the configuration information for SDRAM module.
  * @retval HAL state
  */
HAL_SDRAM_StateTypeDef HAL_SDRAM_GetState(SDRAM_HandleTypeDef *hsdram)
{
  return hsdram->State;
}

/**
  * @}
1223
  */
whj123999's avatar
whj123999 已提交
1224 1225 1226 1227

/**
  * @}
  */
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300

/**
  @cond 0
  */
/**
  * @brief  MDMA SDRAM process complete callback.
  * @param  hmdma : MDMA handle
  * @retval None
  */
static void SDRAM_DMACplt(MDMA_HandleTypeDef *hmdma)
{
  SDRAM_HandleTypeDef* hsdram = ( SDRAM_HandleTypeDef* )(hmdma->Parent);

  /* Disable the MDMA channel */
  __HAL_MDMA_DISABLE(hmdma);

  /* Update the SDRAM controller state */
  hsdram->State = HAL_SDRAM_STATE_READY;

#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
  hsdram->DmaXferCpltCallback(hmdma);
#else
  HAL_SDRAM_DMA_XferCpltCallback(hmdma);
#endif
}

/**
  * @brief  MDMA SRAM process complete callback.
  * @param  hmdma : MDMA handle
  * @retval None
  */
static void SDRAM_DMACpltProt(MDMA_HandleTypeDef *hmdma)
{
  SDRAM_HandleTypeDef* hsdram = ( SDRAM_HandleTypeDef* )(hmdma->Parent);

  /* Disable the MDMA channel */
  __HAL_MDMA_DISABLE(hmdma);

  /* Update the SDRAM controller state */
  hsdram->State = HAL_SDRAM_STATE_WRITE_PROTECTED;

#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
  hsdram->DmaXferCpltCallback(hmdma);
#else
  HAL_SDRAM_DMA_XferCpltCallback(hmdma);
#endif
}

/**
  * @brief  MDMA SDRAM error callback.
  * @param  hmdma : MDMA handle
  * @retval None
  */
static void SDRAM_DMAError(MDMA_HandleTypeDef *hmdma)
{
  SDRAM_HandleTypeDef* hsdram = ( SDRAM_HandleTypeDef* )(hmdma->Parent);

  /* Disable the MDMA channel */
  __HAL_MDMA_DISABLE(hmdma);

  /* Update the SDRAM controller state */
  hsdram->State = HAL_SDRAM_STATE_ERROR;

#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
  hsdram->DmaXferErrorCallback(hmdma);
#else
  HAL_SDRAM_DMA_XferErrorCallback(hmdma);
#endif
}
/**
  @endcond
  */

whj123999's avatar
whj123999 已提交
1301 1302 1303 1304
/**
  * @}
  */

1305 1306
#endif /* HAL_SDRAM_MODULE_ENABLED */

whj123999's avatar
whj123999 已提交
1307 1308 1309 1310
/**
  * @}
  */

1311

whj123999's avatar
whj123999 已提交
1312
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/