]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil/ST_Library/stm32f7xx_hal_sdram.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil / ST_Library / stm32f7xx_hal_sdram.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f7xx_hal_sdram.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0RC1\r
6   * @date    24-March-2015\r
7   * @brief   SDRAM HAL module driver.\r
8   *          This file provides a generic firmware to drive SDRAM memories mounted \r
9   *          as external device.\r
10   *         \r
11   @verbatim\r
12   ==============================================================================\r
13                        ##### How to use this driver #####\r
14   ============================================================================== \r
15   [..]\r
16     This driver is a generic layered driver which contains a set of APIs used to \r
17     control SDRAM memories. It uses the FMC layer functions to interface \r
18     with SDRAM devices.  \r
19     The following sequence should be followed to configure the FMC to interface\r
20     with SDRAM memories: \r
21       \r
22    (#) Declare a SDRAM_HandleTypeDef handle structure, for example:\r
23           SDRAM_HandleTypeDef  hdsram \r
24           \r
25        (++) Fill the SDRAM_HandleTypeDef handle "Init" field with the allowed \r
26             values of the structure member.\r
27             \r
28        (++) Fill the SDRAM_HandleTypeDef handle "Instance" field with a predefined \r
29             base register instance for NOR or SDRAM device \r
30              \r
31    (#) Declare a FMC_SDRAM_TimingTypeDef structure; for example:\r
32           FMC_SDRAM_TimingTypeDef  Timing;\r
33       and fill its fields with the allowed values of the structure member.\r
34       \r
35    (#) Initialize the SDRAM Controller by calling the function HAL_SDRAM_Init(). This function\r
36        performs the following sequence:\r
37           \r
38        (##) MSP hardware layer configuration using the function HAL_SDRAM_MspInit()\r
39        (##) Control register configuration using the FMC SDRAM interface function \r
40             FMC_SDRAM_Init()\r
41        (##) Timing register configuration using the FMC SDRAM interface function \r
42             FMC_SDRAM_Timing_Init()\r
43        (##) Program the SDRAM external device by applying its initialization sequence\r
44             according to the device plugged in your hardware. This step is mandatory\r
45             for accessing the SDRAM device.   \r
46 \r
47    (#) At this stage you can perform read/write accesses from/to the memory connected \r
48        to the SDRAM Bank. You can perform either polling or DMA transfer using the\r
49        following APIs:\r
50        (++) HAL_SDRAM_Read()/HAL_SDRAM_Write() for polling read/write access\r
51        (++) HAL_SDRAM_Read_DMA()/HAL_SDRAM_Write_DMA() for DMA read/write transfer\r
52        \r
53    (#) You can also control the SDRAM device by calling the control APIs HAL_SDRAM_WriteOperation_Enable()/\r
54        HAL_SDRAM_WriteOperation_Disable() to respectively enable/disable the SDRAM write operation or \r
55        the function HAL_SDRAM_SendCommand() to send a specified command to the SDRAM\r
56        device. The command to be sent must be configured with the FMC_SDRAM_CommandTypeDef \r
57        structure.   \r
58        \r
59    (#) You can continuously monitor the SDRAM device HAL state by calling the function\r
60        HAL_SDRAM_GetState()         \r
61       \r
62   @endverbatim\r
63   ******************************************************************************\r
64   * @attention\r
65   *\r
66   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>\r
67   *\r
68   * Redistribution and use in source and binary forms, with or without modification,\r
69   * are permitted provided that the following conditions are met:\r
70   *   1. Redistributions of source code must retain the above copyright notice,\r
71   *      this list of conditions and the following disclaimer.\r
72   *   2. Redistributions in binary form must reproduce the above copyright notice,\r
73   *      this list of conditions and the following disclaimer in the documentation\r
74   *      and/or other materials provided with the distribution.\r
75   *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
76   *      may be used to endorse or promote products derived from this software\r
77   *      without specific prior written permission.\r
78   *\r
79   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
80   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
81   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
82   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
83   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
84   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
85   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
86   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
87   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
88   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
89   *\r
90   ******************************************************************************\r
91   */ \r
92 \r
93 /* Includes ------------------------------------------------------------------*/\r
94 #include "stm32f7xx_hal.h"\r
95 \r
96 /** @addtogroup STM32F7xx_HAL_Driver\r
97   * @{\r
98   */\r
99 \r
100 /** @defgroup SDRAM SDRAM\r
101   * @brief SDRAM driver modules\r
102   * @{\r
103   */\r
104 #ifdef HAL_SDRAM_MODULE_ENABLED\r
105 \r
106 /* Private typedef -----------------------------------------------------------*/\r
107 /* Private define ------------------------------------------------------------*/\r
108 /* Private macro -------------------------------------------------------------*/    \r
109 /* Private variables ---------------------------------------------------------*/\r
110 /* Private functions ---------------------------------------------------------*/\r
111 /* Exported functions --------------------------------------------------------*/\r
112 /** @defgroup SDRAM_Exported_Functions SDRAM Exported Functions\r
113   * @{\r
114   */\r
115 \r
116 /** @defgroup SDRAM_Exported_Functions_Group1 Initialization and de-initialization functions \r
117   * @brief    Initialization and Configuration functions \r
118   *\r
119   @verbatim    \r
120   ==============================================================================\r
121            ##### SDRAM Initialization and de_initialization functions #####\r
122   ==============================================================================\r
123   [..]  \r
124     This section provides functions allowing to initialize/de-initialize\r
125     the SDRAM memory\r
126   \r
127 @endverbatim\r
128   * @{\r
129   */\r
130     \r
131 /**\r
132   * @brief  Performs the SDRAM device initialization sequence.\r
133   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
134   *                the configuration information for SDRAM module.\r
135   * @param  Timing: Pointer to SDRAM control timing structure \r
136   * @retval HAL status\r
137   */\r
138 HAL_StatusTypeDef HAL_SDRAM_Init(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_TimingTypeDef *Timing)\r
139 {   \r
140   /* Check the SDRAM handle parameter */\r
141   if(hsdram == NULL)\r
142   {\r
143     return HAL_ERROR;\r
144   }\r
145   \r
146   if(hsdram->State == HAL_SDRAM_STATE_RESET)\r
147   {  \r
148     /* Initialize the low level hardware (MSP) */\r
149     HAL_SDRAM_MspInit(hsdram);\r
150   }\r
151   \r
152   /* Initialize the SDRAM controller state */\r
153   hsdram->State = HAL_SDRAM_STATE_BUSY;\r
154   \r
155   /* Initialize SDRAM control Interface */\r
156   FMC_SDRAM_Init(hsdram->Instance, &(hsdram->Init));\r
157   \r
158   /* Initialize SDRAM timing Interface */\r
159   FMC_SDRAM_Timing_Init(hsdram->Instance, Timing, hsdram->Init.SDBank); \r
160   \r
161   /* Update the SDRAM controller state */\r
162   hsdram->State = HAL_SDRAM_STATE_READY;\r
163   \r
164   return HAL_OK;\r
165 }\r
166 \r
167 /**\r
168   * @brief  Perform the SDRAM device initialization sequence.\r
169   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
170   *                the configuration information for SDRAM module.\r
171   * @retval HAL status\r
172   */\r
173 HAL_StatusTypeDef HAL_SDRAM_DeInit(SDRAM_HandleTypeDef *hsdram)\r
174 {\r
175   /* Initialize the low level hardware (MSP) */\r
176   HAL_SDRAM_MspDeInit(hsdram);\r
177 \r
178   /* Configure the SDRAM registers with their reset values */\r
179   FMC_SDRAM_DeInit(hsdram->Instance, hsdram->Init.SDBank);\r
180 \r
181   /* Reset the SDRAM controller state */\r
182   hsdram->State = HAL_SDRAM_STATE_RESET;\r
183 \r
184   /* Release Lock */\r
185   __HAL_UNLOCK(hsdram);\r
186 \r
187   return HAL_OK;\r
188 }\r
189 \r
190 /**\r
191   * @brief  SDRAM MSP Init.\r
192   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
193   *                the configuration information for SDRAM module.\r
194   * @retval None\r
195   */\r
196 __weak void HAL_SDRAM_MspInit(SDRAM_HandleTypeDef *hsdram)\r
197 {\r
198   /* NOTE: This function Should not be modified, when the callback is needed,\r
199             the HAL_SDRAM_MspInit could be implemented in the user file\r
200    */ \r
201 }\r
202 \r
203 /**\r
204   * @brief  SDRAM MSP DeInit.\r
205   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
206   *                the configuration information for SDRAM module.\r
207   * @retval None\r
208   */\r
209 __weak void HAL_SDRAM_MspDeInit(SDRAM_HandleTypeDef *hsdram)\r
210 {\r
211   /* NOTE: This function Should not be modified, when the callback is needed,\r
212             the HAL_SDRAM_MspDeInit could be implemented in the user file\r
213    */ \r
214 }\r
215 \r
216 /**\r
217   * @brief  This function handles SDRAM refresh error interrupt request.\r
218   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
219   *                the configuration information for SDRAM module.\r
220   * @retval HAL status\r
221 */\r
222 void HAL_SDRAM_IRQHandler(SDRAM_HandleTypeDef *hsdram)\r
223 {\r
224   /* Check SDRAM interrupt Rising edge flag */\r
225   if(__FMC_SDRAM_GET_FLAG(hsdram->Instance, FMC_SDRAM_FLAG_REFRESH_IT))\r
226   {\r
227     /* SDRAM refresh error interrupt callback */\r
228     HAL_SDRAM_RefreshErrorCallback(hsdram);\r
229     \r
230     /* Clear SDRAM refresh error interrupt pending bit */\r
231     __FMC_SDRAM_CLEAR_FLAG(hsdram->Instance, FMC_SDRAM_FLAG_REFRESH_ERROR);\r
232   }\r
233 }\r
234 \r
235 /**\r
236   * @brief  SDRAM Refresh error callback.\r
237   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
238   *                the configuration information for SDRAM module. \r
239   * @retval None\r
240   */\r
241 __weak void HAL_SDRAM_RefreshErrorCallback(SDRAM_HandleTypeDef *hsdram)\r
242 {\r
243   /* NOTE: This function Should not be modified, when the callback is needed,\r
244             the HAL_SDRAM_RefreshErrorCallback could be implemented in the user file\r
245    */ \r
246 }\r
247 \r
248 /**\r
249   * @brief  DMA transfer complete callback.\r
250   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
251   *                the configuration information for the specified DMA module.\r
252   * @retval None\r
253   */\r
254 __weak void HAL_SDRAM_DMA_XferCpltCallback(DMA_HandleTypeDef *hdma)\r
255 {\r
256   /* NOTE: This function Should not be modified, when the callback is needed,\r
257             the HAL_SDRAM_DMA_XferCpltCallback could be implemented in the user file\r
258    */ \r
259 }\r
260 \r
261 /**\r
262   * @brief  DMA transfer complete error callback.\r
263   * @param  hdma: DMA handle\r
264   * @retval None\r
265   */\r
266 __weak void HAL_SDRAM_DMA_XferErrorCallback(DMA_HandleTypeDef *hdma)\r
267 {\r
268   /* NOTE: This function Should not be modified, when the callback is needed,\r
269             the HAL_SDRAM_DMA_XferErrorCallback could be implemented in the user file\r
270    */ \r
271 }\r
272 \r
273 /**\r
274   * @}\r
275   */\r
276 \r
277 /** @defgroup SDRAM_Exported_Functions_Group2 Input and Output functions \r
278   * @brief    Input Output and memory control functions \r
279   *\r
280   @verbatim    \r
281   ==============================================================================\r
282                     ##### SDRAM Input and Output functions #####\r
283   ==============================================================================\r
284   [..]  \r
285     This section provides functions allowing to use and control the SDRAM memory\r
286   \r
287 @endverbatim\r
288   * @{\r
289   */\r
290 \r
291 /**\r
292   * @brief  Reads 8-bit data buffer from the SDRAM memory.\r
293   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
294   *                the configuration information for SDRAM module.\r
295   * @param  pAddress: Pointer to read start address\r
296   * @param  pDstBuffer: Pointer to destination buffer  \r
297   * @param  BufferSize: Size of the buffer to read from memory\r
298   * @retval HAL status\r
299   */\r
300 HAL_StatusTypeDef HAL_SDRAM_Read_8b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint8_t *pDstBuffer, uint32_t BufferSize)\r
301 {\r
302   __IO uint8_t *pSdramAddress = (uint8_t *)pAddress;\r
303   \r
304   /* Process Locked */\r
305   __HAL_LOCK(hsdram);\r
306   \r
307   /* Check the SDRAM controller state */\r
308   if(hsdram->State == HAL_SDRAM_STATE_BUSY)\r
309   {\r
310     return HAL_BUSY;\r
311   }\r
312   else if(hsdram->State == HAL_SDRAM_STATE_PRECHARGED)\r
313   {\r
314     return  HAL_ERROR; \r
315   }  \r
316   \r
317   /* Read data from source */\r
318   for(; BufferSize != 0; BufferSize--)\r
319   {\r
320     *pDstBuffer = *(__IO uint8_t *)pSdramAddress;  \r
321     pDstBuffer++;\r
322     pSdramAddress++;\r
323   }\r
324   \r
325   /* Process Unlocked */\r
326   __HAL_UNLOCK(hsdram);\r
327   \r
328   return HAL_OK; \r
329 }\r
330 \r
331 \r
332 /**\r
333   * @brief  Writes 8-bit data buffer to SDRAM memory.\r
334   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
335   *                the configuration information for SDRAM module.\r
336   * @param  pAddress: Pointer to write start address\r
337   * @param  pSrcBuffer: Pointer to source buffer to write  \r
338   * @param  BufferSize: Size of the buffer to write to memory\r
339   * @retval HAL status\r
340   */\r
341 HAL_StatusTypeDef HAL_SDRAM_Write_8b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint8_t *pSrcBuffer, uint32_t BufferSize)\r
342 {\r
343   __IO uint8_t *pSdramAddress = (uint8_t *)pAddress;\r
344   uint32_t tmp = 0;\r
345   \r
346   /* Process Locked */\r
347   __HAL_LOCK(hsdram);\r
348   \r
349   /* Check the SDRAM controller state */\r
350   tmp = hsdram->State;\r
351   \r
352   if(tmp == HAL_SDRAM_STATE_BUSY)\r
353   {\r
354     return HAL_BUSY;\r
355   }\r
356   else if((tmp == HAL_SDRAM_STATE_PRECHARGED) || (tmp == HAL_SDRAM_STATE_WRITE_PROTECTED))\r
357   {\r
358     return  HAL_ERROR; \r
359   }\r
360   \r
361   /* Write data to memory */\r
362   for(; BufferSize != 0; BufferSize--)\r
363   {\r
364     *(__IO uint8_t *)pSdramAddress = *pSrcBuffer;\r
365     pSrcBuffer++;\r
366     pSdramAddress++;\r
367   }\r
368   \r
369   /* Process Unlocked */\r
370   __HAL_UNLOCK(hsdram);    \r
371   \r
372   return HAL_OK;   \r
373 }\r
374 \r
375 \r
376 /**\r
377   * @brief  Reads 16-bit data buffer from the SDRAM memory. \r
378   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
379   *                the configuration information for SDRAM module.\r
380   * @param  pAddress: Pointer to read start address\r
381   * @param  pDstBuffer: Pointer to destination buffer  \r
382   * @param  BufferSize: Size of the buffer to read from memory\r
383   * @retval HAL status\r
384   */\r
385 HAL_StatusTypeDef HAL_SDRAM_Read_16b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint16_t *pDstBuffer, uint32_t BufferSize)\r
386 {\r
387   __IO uint16_t *pSdramAddress = (uint16_t *)pAddress;\r
388   \r
389   /* Process Locked */\r
390   __HAL_LOCK(hsdram);\r
391   \r
392   /* Check the SDRAM controller state */\r
393   if(hsdram->State == HAL_SDRAM_STATE_BUSY)\r
394   {\r
395     return HAL_BUSY;\r
396   }\r
397   else if(hsdram->State == HAL_SDRAM_STATE_PRECHARGED)\r
398   {\r
399     return  HAL_ERROR; \r
400   }  \r
401   \r
402   /* Read data from source */\r
403   for(; BufferSize != 0; BufferSize--)\r
404   {\r
405     *pDstBuffer = *(__IO uint16_t *)pSdramAddress;  \r
406     pDstBuffer++;\r
407     pSdramAddress++;               \r
408   }\r
409   \r
410   /* Process Unlocked */\r
411   __HAL_UNLOCK(hsdram);       \r
412   \r
413   return HAL_OK; \r
414 }\r
415 \r
416 /**\r
417   * @brief  Writes 16-bit data buffer to SDRAM memory. \r
418   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
419   *                the configuration information for SDRAM module.\r
420   * @param  pAddress: Pointer to write start address\r
421   * @param  pSrcBuffer: Pointer to source buffer to write  \r
422   * @param  BufferSize: Size of the buffer to write to memory\r
423   * @retval HAL status\r
424   */\r
425 HAL_StatusTypeDef HAL_SDRAM_Write_16b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint16_t *pSrcBuffer, uint32_t BufferSize)\r
426 {\r
427   __IO uint16_t *pSdramAddress = (uint16_t *)pAddress;\r
428   uint32_t tmp = 0;\r
429   \r
430   /* Process Locked */\r
431   __HAL_LOCK(hsdram);\r
432   \r
433   /* Check the SDRAM controller state */\r
434   tmp = hsdram->State;\r
435   \r
436   if(tmp == HAL_SDRAM_STATE_BUSY)\r
437   {\r
438     return HAL_BUSY;\r
439   }\r
440   else if((tmp == HAL_SDRAM_STATE_PRECHARGED) || (tmp == HAL_SDRAM_STATE_WRITE_PROTECTED))\r
441   {\r
442     return  HAL_ERROR; \r
443   }\r
444   \r
445   /* Write data to memory */\r
446   for(; BufferSize != 0; BufferSize--)\r
447   {\r
448     *(__IO uint16_t *)pSdramAddress = *pSrcBuffer;\r
449     pSrcBuffer++;\r
450     pSdramAddress++;            \r
451   }\r
452   \r
453   /* Process Unlocked */\r
454   __HAL_UNLOCK(hsdram);    \r
455   \r
456   return HAL_OK;   \r
457 }\r
458 \r
459 /**\r
460   * @brief  Reads 32-bit data buffer from the SDRAM memory. \r
461   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
462   *                the configuration information for SDRAM module.\r
463   * @param  pAddress: Pointer to read start address\r
464   * @param  pDstBuffer: Pointer to destination buffer  \r
465   * @param  BufferSize: Size of the buffer to read from memory\r
466   * @retval HAL status\r
467   */\r
468 HAL_StatusTypeDef HAL_SDRAM_Read_32b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)\r
469 {\r
470   __IO uint32_t *pSdramAddress = (uint32_t *)pAddress;\r
471   \r
472   /* Process Locked */\r
473   __HAL_LOCK(hsdram);\r
474   \r
475   /* Check the SDRAM controller state */\r
476   if(hsdram->State == HAL_SDRAM_STATE_BUSY)\r
477   {\r
478     return HAL_BUSY;\r
479   }\r
480   else if(hsdram->State == HAL_SDRAM_STATE_PRECHARGED)\r
481   {\r
482     return  HAL_ERROR; \r
483   }  \r
484   \r
485   /* Read data from source */\r
486   for(; BufferSize != 0; BufferSize--)\r
487   {\r
488     *pDstBuffer = *(__IO uint32_t *)pSdramAddress;  \r
489     pDstBuffer++;\r
490     pSdramAddress++;               \r
491   }\r
492   \r
493   /* Process Unlocked */\r
494   __HAL_UNLOCK(hsdram);       \r
495   \r
496   return HAL_OK; \r
497 }\r
498 \r
499 /**\r
500   * @brief  Writes 32-bit data buffer to SDRAM memory. \r
501   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
502   *                the configuration information for SDRAM module.\r
503   * @param  pAddress: Pointer to write start address\r
504   * @param  pSrcBuffer: Pointer to source buffer to write  \r
505   * @param  BufferSize: Size of the buffer to write to memory\r
506   * @retval HAL status\r
507   */\r
508 HAL_StatusTypeDef HAL_SDRAM_Write_32b(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)\r
509 {\r
510   __IO uint32_t *pSdramAddress = (uint32_t *)pAddress;\r
511   uint32_t tmp = 0;\r
512   \r
513   /* Process Locked */\r
514   __HAL_LOCK(hsdram);\r
515   \r
516   /* Check the SDRAM controller state */\r
517   tmp = hsdram->State;\r
518   \r
519   if(tmp == HAL_SDRAM_STATE_BUSY)\r
520   {\r
521     return HAL_BUSY;\r
522   }\r
523   else if((tmp == HAL_SDRAM_STATE_PRECHARGED) || (tmp == HAL_SDRAM_STATE_WRITE_PROTECTED))\r
524   {\r
525     return  HAL_ERROR; \r
526   }\r
527   \r
528   /* Write data to memory */\r
529   for(; BufferSize != 0; BufferSize--)\r
530   {\r
531     *(__IO uint32_t *)pSdramAddress = *pSrcBuffer;\r
532     pSrcBuffer++;\r
533     pSdramAddress++;          \r
534   }\r
535   \r
536   /* Process Unlocked */\r
537   __HAL_UNLOCK(hsdram);    \r
538   \r
539   return HAL_OK;  \r
540 }\r
541 \r
542 /**\r
543   * @brief  Reads a Words data from the SDRAM memory using DMA transfer. \r
544   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
545   *                the configuration information for SDRAM module.\r
546   * @param  pAddress: Pointer to read start address\r
547   * @param  pDstBuffer: Pointer to destination buffer  \r
548   * @param  BufferSize: Size of the buffer to read from memory\r
549   * @retval HAL status\r
550   */\r
551 HAL_StatusTypeDef HAL_SDRAM_Read_DMA(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)\r
552 {\r
553   uint32_t tmp = 0;\r
554     \r
555   /* Process Locked */\r
556   __HAL_LOCK(hsdram);\r
557   \r
558   /* Check the SDRAM controller state */  \r
559   tmp = hsdram->State;\r
560   \r
561   if(tmp == HAL_SDRAM_STATE_BUSY)\r
562   {\r
563     return HAL_BUSY;\r
564   }\r
565   else if(tmp == HAL_SDRAM_STATE_PRECHARGED)\r
566   {\r
567     return  HAL_ERROR; \r
568   }  \r
569   \r
570   /* Configure DMA user callbacks */\r
571   hsdram->hdma->XferCpltCallback  = HAL_SDRAM_DMA_XferCpltCallback;\r
572   hsdram->hdma->XferErrorCallback = HAL_SDRAM_DMA_XferErrorCallback;\r
573   \r
574   /* Enable the DMA Stream */\r
575   HAL_DMA_Start_IT(hsdram->hdma, (uint32_t)pAddress, (uint32_t)pDstBuffer, (uint32_t)BufferSize);\r
576   \r
577   /* Process Unlocked */\r
578   __HAL_UNLOCK(hsdram);  \r
579   \r
580   return HAL_OK; \r
581 }\r
582 \r
583 /**\r
584   * @brief  Writes a Words data buffer to SDRAM memory using DMA transfer.\r
585   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
586   *                the configuration information for SDRAM module.\r
587   * @param  pAddress: Pointer to write start address\r
588   * @param  pSrcBuffer: Pointer to source buffer to write  \r
589   * @param  BufferSize: Size of the buffer to write to memory\r
590   * @retval HAL status\r
591   */\r
592 HAL_StatusTypeDef HAL_SDRAM_Write_DMA(SDRAM_HandleTypeDef *hsdram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)\r
593 {\r
594   uint32_t tmp = 0;\r
595   \r
596   /* Process Locked */\r
597   __HAL_LOCK(hsdram);\r
598   \r
599   /* Check the SDRAM controller state */  \r
600   tmp = hsdram->State;\r
601   \r
602   if(tmp == HAL_SDRAM_STATE_BUSY)\r
603   {\r
604     return HAL_BUSY;\r
605   }\r
606   else if((tmp == HAL_SDRAM_STATE_PRECHARGED) || (tmp == HAL_SDRAM_STATE_WRITE_PROTECTED))\r
607   {\r
608     return  HAL_ERROR; \r
609   }  \r
610   \r
611   /* Configure DMA user callbacks */\r
612   hsdram->hdma->XferCpltCallback  = HAL_SDRAM_DMA_XferCpltCallback;\r
613   hsdram->hdma->XferErrorCallback = HAL_SDRAM_DMA_XferErrorCallback;\r
614   \r
615   /* Enable the DMA Stream */\r
616   HAL_DMA_Start_IT(hsdram->hdma, (uint32_t)pSrcBuffer, (uint32_t)pAddress, (uint32_t)BufferSize);\r
617   \r
618   /* Process Unlocked */\r
619   __HAL_UNLOCK(hsdram);\r
620   \r
621   return HAL_OK;\r
622 }\r
623 \r
624 /**\r
625   * @}\r
626   */\r
627   \r
628 /** @defgroup SDRAM_Exported_Functions_Group3 Control functions \r
629  *  @brief   management functions \r
630  *\r
631 @verbatim   \r
632   ==============================================================================\r
633                          ##### SDRAM Control functions #####\r
634   ==============================================================================  \r
635   [..]\r
636     This subsection provides a set of functions allowing to control dynamically\r
637     the SDRAM interface.\r
638 \r
639 @endverbatim\r
640   * @{\r
641   */\r
642 \r
643 /**\r
644   * @brief  Enables dynamically SDRAM write protection.\r
645   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
646   *                the configuration information for SDRAM module.\r
647   * @retval HAL status\r
648   */\r
649 HAL_StatusTypeDef HAL_SDRAM_WriteProtection_Enable(SDRAM_HandleTypeDef *hsdram)\r
650\r
651   /* Check the SDRAM controller state */ \r
652   if(hsdram->State == HAL_SDRAM_STATE_BUSY)\r
653   {\r
654     return HAL_BUSY;\r
655   }\r
656   \r
657   /* Update the SDRAM state */\r
658   hsdram->State = HAL_SDRAM_STATE_BUSY;\r
659   \r
660   /* Enable write protection */\r
661   FMC_SDRAM_WriteProtection_Enable(hsdram->Instance, hsdram->Init.SDBank);\r
662   \r
663   /* Update the SDRAM state */\r
664   hsdram->State = HAL_SDRAM_STATE_WRITE_PROTECTED;\r
665   \r
666   return HAL_OK;  \r
667 }\r
668 \r
669 /**\r
670   * @brief  Disables dynamically SDRAM write protection.\r
671   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
672   *                the configuration information for SDRAM module.\r
673   * @retval HAL status\r
674   */\r
675 HAL_StatusTypeDef HAL_SDRAM_WriteProtection_Disable(SDRAM_HandleTypeDef *hsdram)\r
676 {\r
677   /* Check the SDRAM controller state */\r
678   if(hsdram->State == HAL_SDRAM_STATE_BUSY)\r
679   {\r
680     return HAL_BUSY;\r
681   }\r
682   \r
683   /* Update the SDRAM state */\r
684   hsdram->State = HAL_SDRAM_STATE_BUSY;\r
685   \r
686   /* Disable write protection */\r
687   FMC_SDRAM_WriteProtection_Disable(hsdram->Instance, hsdram->Init.SDBank);\r
688   \r
689   /* Update the SDRAM state */\r
690   hsdram->State = HAL_SDRAM_STATE_READY;\r
691   \r
692   return HAL_OK;\r
693 }\r
694 \r
695 /**\r
696   * @brief  Sends Command to the SDRAM bank.\r
697   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
698   *                the configuration information for SDRAM module.\r
699   * @param  Command: SDRAM command structure\r
700   * @param  Timeout: Timeout duration\r
701   * @retval HAL status\r
702   */  \r
703 HAL_StatusTypeDef HAL_SDRAM_SendCommand(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_CommandTypeDef *Command, uint32_t Timeout)\r
704 {\r
705   /* Check the SDRAM controller state */\r
706   if(hsdram->State == HAL_SDRAM_STATE_BUSY)\r
707   {\r
708     return HAL_BUSY;\r
709   }\r
710   \r
711   /* Update the SDRAM state */\r
712   hsdram->State = HAL_SDRAM_STATE_BUSY;\r
713   \r
714   /* Send SDRAM command */\r
715   FMC_SDRAM_SendCommand(hsdram->Instance, Command, Timeout);\r
716   \r
717   /* Update the SDRAM controller state state */\r
718   if(Command->CommandMode == FMC_SDRAM_CMD_PALL)\r
719   {\r
720     hsdram->State = HAL_SDRAM_STATE_PRECHARGED;\r
721   }\r
722   else\r
723   {\r
724     hsdram->State = HAL_SDRAM_STATE_READY;\r
725   }\r
726   \r
727   return HAL_OK;  \r
728 }\r
729 \r
730 /**\r
731   * @brief  Programs the SDRAM Memory Refresh rate.\r
732   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
733   *                the configuration information for SDRAM module.  \r
734   * @param  RefreshRate: The SDRAM refresh rate value       \r
735   * @retval HAL status\r
736   */\r
737 HAL_StatusTypeDef HAL_SDRAM_ProgramRefreshRate(SDRAM_HandleTypeDef *hsdram, uint32_t RefreshRate)\r
738 {\r
739   /* Check the SDRAM controller state */\r
740   if(hsdram->State == HAL_SDRAM_STATE_BUSY)\r
741   {\r
742     return HAL_BUSY;\r
743   } \r
744   \r
745   /* Update the SDRAM state */\r
746   hsdram->State = HAL_SDRAM_STATE_BUSY;\r
747   \r
748   /* Program the refresh rate */\r
749   FMC_SDRAM_ProgramRefreshRate(hsdram->Instance ,RefreshRate);\r
750   \r
751   /* Update the SDRAM state */\r
752   hsdram->State = HAL_SDRAM_STATE_READY;\r
753   \r
754   return HAL_OK;   \r
755 }\r
756 \r
757 /**\r
758   * @brief  Sets the Number of consecutive SDRAM Memory auto Refresh commands.\r
759   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
760   *                the configuration information for SDRAM module.  \r
761   * @param  AutoRefreshNumber: The SDRAM auto Refresh number       \r
762   * @retval HAL status\r
763   */\r
764 HAL_StatusTypeDef HAL_SDRAM_SetAutoRefreshNumber(SDRAM_HandleTypeDef *hsdram, uint32_t AutoRefreshNumber)\r
765 {\r
766   /* Check the SDRAM controller state */\r
767   if(hsdram->State == HAL_SDRAM_STATE_BUSY)\r
768   {\r
769     return HAL_BUSY;\r
770   } \r
771   \r
772   /* Update the SDRAM state */\r
773   hsdram->State = HAL_SDRAM_STATE_BUSY;\r
774   \r
775   /* Set the Auto-Refresh number */\r
776   FMC_SDRAM_SetAutoRefreshNumber(hsdram->Instance ,AutoRefreshNumber);\r
777   \r
778   /* Update the SDRAM state */\r
779   hsdram->State = HAL_SDRAM_STATE_READY;\r
780   \r
781   return HAL_OK;\r
782 }\r
783 \r
784 /**\r
785   * @brief  Returns the SDRAM memory current mode.\r
786   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
787   *                the configuration information for SDRAM module.\r
788   * @retval The SDRAM memory mode.        \r
789   */\r
790 uint32_t HAL_SDRAM_GetModeStatus(SDRAM_HandleTypeDef *hsdram)\r
791 {\r
792   /* Return the SDRAM memory current mode */\r
793   return(FMC_SDRAM_GetModeStatus(hsdram->Instance, hsdram->Init.SDBank));\r
794 }\r
795 \r
796 /**\r
797   * @}\r
798   */\r
799   \r
800 /** @defgroup SDRAM_Exported_Functions_Group4 State functions \r
801  *  @brief   Peripheral State functions \r
802  *\r
803 @verbatim   \r
804   ==============================================================================\r
805                       ##### SDRAM State functions #####\r
806   ==============================================================================  \r
807   [..]\r
808     This subsection permits to get in run-time the status of the SDRAM controller \r
809     and the data flow.\r
810 \r
811 @endverbatim\r
812   * @{\r
813   */\r
814 \r
815 /**\r
816   * @brief  Returns the SDRAM state.\r
817   * @param  hsdram: pointer to a SDRAM_HandleTypeDef structure that contains\r
818   *                the configuration information for SDRAM module.\r
819   * @retval HAL state\r
820   */\r
821 HAL_SDRAM_StateTypeDef HAL_SDRAM_GetState(SDRAM_HandleTypeDef *hsdram)\r
822 {\r
823   return hsdram->State;\r
824 }\r
825 \r
826 /**\r
827   * @}\r
828   */    \r
829 \r
830 /**\r
831   * @}\r
832   */\r
833 #endif /* HAL_SDRAM_MODULE_ENABLED */\r
834 /**\r
835   * @}\r
836   */\r
837 \r
838 /**\r
839   * @}\r
840   */\r
841 \r
842 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r