2 ******************************************************************************
3 * @file stm32f7xx_hal_sram.c
4 * @author MCD Application Team
7 * @brief SRAM HAL module driver.
8 * This file provides a generic firmware to drive SRAM memories
9 * mounted as external device.
12 ==============================================================================
13 ##### How to use this driver #####
14 ==============================================================================
16 This driver is a generic layered driver which contains a set of APIs used to
17 control SRAM memories. It uses the FMC layer functions to interface
19 The following sequence should be followed to configure the FMC to interface
20 with SRAM/PSRAM memories:
22 (#) Declare a SRAM_HandleTypeDef handle structure, for example:
23 SRAM_HandleTypeDef hsram; and:
25 (++) Fill the SRAM_HandleTypeDef handle "Init" field with the allowed
26 values of the structure member.
28 (++) Fill the SRAM_HandleTypeDef handle "Instance" field with a predefined
29 base register instance for NOR or SRAM device
31 (++) Fill the SRAM_HandleTypeDef handle "Extended" field with a predefined
32 base register instance for NOR or SRAM extended mode
34 (#) Declare two FMC_NORSRAM_TimingTypeDef structures, for both normal and extended
35 mode timings; for example:
36 FMC_NORSRAM_TimingTypeDef Timing and FMC_NORSRAM_TimingTypeDef ExTiming;
37 and fill its fields with the allowed values of the structure member.
39 (#) Initialize the SRAM Controller by calling the function HAL_SRAM_Init(). This function
40 performs the following sequence:
42 (##) MSP hardware layer configuration using the function HAL_SRAM_MspInit()
43 (##) Control register configuration using the FMC NORSRAM interface function
45 (##) Timing register configuration using the FMC NORSRAM interface function
46 FMC_NORSRAM_Timing_Init()
47 (##) Extended mode Timing register configuration using the FMC NORSRAM interface function
48 FMC_NORSRAM_Extended_Timing_Init()
49 (##) Enable the SRAM device using the macro __FMC_NORSRAM_ENABLE()
51 (#) At this stage you can perform read/write accesses from/to the memory connected
52 to the NOR/SRAM Bank. You can perform either polling or DMA transfer using the
54 (++) HAL_SRAM_Read()/HAL_SRAM_Write() for polling read/write access
55 (++) HAL_SRAM_Read_DMA()/HAL_SRAM_Write_DMA() for DMA read/write transfer
57 (#) You can also control the SRAM device by calling the control APIs HAL_SRAM_WriteOperation_Enable()/
58 HAL_SRAM_WriteOperation_Disable() to respectively enable/disable the SRAM write operation
60 (#) You can continuously monitor the SRAM device HAL state by calling the function
64 ******************************************************************************
67 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
69 * Redistribution and use in source and binary forms, with or without modification,
70 * are permitted provided that the following conditions are met:
71 * 1. Redistributions of source code must retain the above copyright notice,
72 * this list of conditions and the following disclaimer.
73 * 2. Redistributions in binary form must reproduce the above copyright notice,
74 * this list of conditions and the following disclaimer in the documentation
75 * and/or other materials provided with the distribution.
76 * 3. Neither the name of STMicroelectronics nor the names of its contributors
77 * may be used to endorse or promote products derived from this software
78 * without specific prior written permission.
80 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
81 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
82 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
83 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
84 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
85 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
86 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
87 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
88 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
89 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
91 ******************************************************************************
94 /* Includes ------------------------------------------------------------------*/
95 #include "stm32f7xx_hal.h"
97 /** @addtogroup STM32F7xx_HAL_Driver
101 /** @defgroup SRAM SRAM
102 * @brief SRAM driver modules
105 #ifdef HAL_SRAM_MODULE_ENABLED
106 /* Private typedef -----------------------------------------------------------*/
107 /* Private define ------------------------------------------------------------*/
108 /* Private macro -------------------------------------------------------------*/
109 /* Private variables ---------------------------------------------------------*/
110 /* Private function prototypes -----------------------------------------------*/
111 /* Exported functions --------------------------------------------------------*/
113 /** @defgroup SRAM_Exported_Functions SRAM Exported Functions
117 /** @defgroup SRAM_Exported_Functions_Group1 Initialization and de-initialization functions
118 * @brief Initialization and Configuration functions.
121 ==============================================================================
122 ##### SRAM Initialization and de_initialization functions #####
123 ==============================================================================
124 [..] This section provides functions allowing to initialize/de-initialize
132 * @brief Performs the SRAM device initialization sequence
133 * @param hsram: pointer to a SRAM_HandleTypeDef structure that contains
134 * the configuration information for SRAM module.
135 * @param Timing: Pointer to SRAM control timing structure
136 * @param ExtTiming: Pointer to SRAM extended mode timing structure
139 HAL_StatusTypeDef
HAL_SRAM_Init(SRAM_HandleTypeDef
*hsram
, FMC_NORSRAM_TimingTypeDef
*Timing
, FMC_NORSRAM_TimingTypeDef
*ExtTiming
)
141 /* Check the SRAM handle parameter */
147 if(hsram
->State
== HAL_SRAM_STATE_RESET
)
149 /* Allocate lock resource and initialize it */
150 hsram
->Lock
= HAL_UNLOCKED
;
151 /* Initialize the low level hardware (MSP) */
152 HAL_SRAM_MspInit(hsram
);
155 /* Initialize SRAM control Interface */
156 FMC_NORSRAM_Init(hsram
->Instance
, &(hsram
->Init
));
158 /* Initialize SRAM timing Interface */
159 FMC_NORSRAM_Timing_Init(hsram
->Instance
, Timing
, hsram
->Init
.NSBank
);
161 /* Initialize SRAM extended mode timing Interface */
162 FMC_NORSRAM_Extended_Timing_Init(hsram
->Extended
, ExtTiming
, hsram
->Init
.NSBank
, hsram
->Init
.ExtendedMode
);
164 /* Enable the NORSRAM device */
165 __FMC_NORSRAM_ENABLE(hsram
->Instance
, hsram
->Init
.NSBank
);
171 * @brief Performs the SRAM device De-initialization sequence.
172 * @param hsram: pointer to a SRAM_HandleTypeDef structure that contains
173 * the configuration information for SRAM module.
176 HAL_StatusTypeDef
HAL_SRAM_DeInit(SRAM_HandleTypeDef
*hsram
)
178 /* De-Initialize the low level hardware (MSP) */
179 HAL_SRAM_MspDeInit(hsram
);
181 /* Configure the SRAM registers with their reset values */
182 FMC_NORSRAM_DeInit(hsram
->Instance
, hsram
->Extended
, hsram
->Init
.NSBank
);
184 hsram
->State
= HAL_SRAM_STATE_RESET
;
193 * @brief SRAM MSP Init.
194 * @param hsram: pointer to a SRAM_HandleTypeDef structure that contains
195 * the configuration information for SRAM module.
198 __weak
void HAL_SRAM_MspInit(SRAM_HandleTypeDef
*hsram
)
200 /* Prevent unused argument(s) compilation warning */
203 /* NOTE : This function Should not be modified, when the callback is needed,
204 the HAL_SRAM_MspInit could be implemented in the user file
209 * @brief SRAM MSP DeInit.
210 * @param hsram: pointer to a SRAM_HandleTypeDef structure that contains
211 * the configuration information for SRAM module.
214 __weak
void HAL_SRAM_MspDeInit(SRAM_HandleTypeDef
*hsram
)
216 /* Prevent unused argument(s) compilation warning */
219 /* NOTE : This function Should not be modified, when the callback is needed,
220 the HAL_SRAM_MspDeInit could be implemented in the user file
225 * @brief DMA transfer complete callback.
226 * @param hdma: pointer to a SRAM_HandleTypeDef structure that contains
227 * the configuration information for SRAM module.
230 __weak
void HAL_SRAM_DMA_XferCpltCallback(DMA_HandleTypeDef
*hdma
)
232 /* Prevent unused argument(s) compilation warning */
235 /* NOTE : This function Should not be modified, when the callback is needed,
236 the HAL_SRAM_DMA_XferCpltCallback could be implemented in the user file
241 * @brief DMA transfer complete error callback.
242 * @param hdma: pointer to a SRAM_HandleTypeDef structure that contains
243 * the configuration information for SRAM module.
246 __weak
void HAL_SRAM_DMA_XferErrorCallback(DMA_HandleTypeDef
*hdma
)
248 /* Prevent unused argument(s) compilation warning */
251 /* NOTE : This function Should not be modified, when the callback is needed,
252 the HAL_SRAM_DMA_XferErrorCallback could be implemented in the user file
260 /** @defgroup SRAM_Exported_Functions_Group2 Input Output and memory control functions
261 * @brief Input Output and memory control functions
264 ==============================================================================
265 ##### SRAM Input and Output functions #####
266 ==============================================================================
268 This section provides functions allowing to use and control the SRAM memory
275 * @brief Reads 8-bit buffer from SRAM memory.
276 * @param hsram: pointer to a SRAM_HandleTypeDef structure that contains
277 * the configuration information for SRAM module.
278 * @param pAddress: Pointer to read start address
279 * @param pDstBuffer: Pointer to destination buffer
280 * @param BufferSize: Size of the buffer to read from memory
283 HAL_StatusTypeDef
HAL_SRAM_Read_8b(SRAM_HandleTypeDef
*hsram
, uint32_t *pAddress
, uint8_t *pDstBuffer
, uint32_t BufferSize
)
285 __IO
uint8_t * psramaddress
= (uint8_t *)pAddress
;
290 /* Update the SRAM controller state */
291 hsram
->State
= HAL_SRAM_STATE_BUSY
;
293 /* Read data from memory */
294 for(; BufferSize
!= 0; BufferSize
--)
296 *pDstBuffer
= *(__IO
uint8_t *)psramaddress
;
301 /* Update the SRAM controller state */
302 hsram
->State
= HAL_SRAM_STATE_READY
;
304 /* Process unlocked */
311 * @brief Writes 8-bit buffer to SRAM memory.
312 * @param hsram: pointer to a SRAM_HandleTypeDef structure that contains
313 * the configuration information for SRAM module.
314 * @param pAddress: Pointer to write start address
315 * @param pSrcBuffer: Pointer to source buffer to write
316 * @param BufferSize: Size of the buffer to write to memory
319 HAL_StatusTypeDef
HAL_SRAM_Write_8b(SRAM_HandleTypeDef
*hsram
, uint32_t *pAddress
, uint8_t *pSrcBuffer
, uint32_t BufferSize
)
321 __IO
uint8_t * psramaddress
= (uint8_t *)pAddress
;
323 /* Check the SRAM controller state */
324 if(hsram
->State
== HAL_SRAM_STATE_PROTECTED
)
332 /* Update the SRAM controller state */
333 hsram
->State
= HAL_SRAM_STATE_BUSY
;
335 /* Write data to memory */
336 for(; BufferSize
!= 0; BufferSize
--)
338 *(__IO
uint8_t *)psramaddress
= *pSrcBuffer
;
343 /* Update the SRAM controller state */
344 hsram
->State
= HAL_SRAM_STATE_READY
;
346 /* Process unlocked */
353 * @brief Reads 16-bit buffer from SRAM memory.
354 * @param hsram: pointer to a SRAM_HandleTypeDef structure that contains
355 * the configuration information for SRAM module.
356 * @param pAddress: Pointer to read start address
357 * @param pDstBuffer: Pointer to destination buffer
358 * @param BufferSize: Size of the buffer to read from memory
361 HAL_StatusTypeDef
HAL_SRAM_Read_16b(SRAM_HandleTypeDef
*hsram
, uint32_t *pAddress
, uint16_t *pDstBuffer
, uint32_t BufferSize
)
363 __IO
uint16_t * psramaddress
= (uint16_t *)pAddress
;
368 /* Update the SRAM controller state */
369 hsram
->State
= HAL_SRAM_STATE_BUSY
;
371 /* Read data from memory */
372 for(; BufferSize
!= 0; BufferSize
--)
374 *pDstBuffer
= *(__IO
uint16_t *)psramaddress
;
379 /* Update the SRAM controller state */
380 hsram
->State
= HAL_SRAM_STATE_READY
;
382 /* Process unlocked */
389 * @brief Writes 16-bit buffer to SRAM memory.
390 * @param hsram: pointer to a SRAM_HandleTypeDef structure that contains
391 * the configuration information for SRAM module.
392 * @param pAddress: Pointer to write start address
393 * @param pSrcBuffer: Pointer to source buffer to write
394 * @param BufferSize: Size of the buffer to write to memory
397 HAL_StatusTypeDef
HAL_SRAM_Write_16b(SRAM_HandleTypeDef
*hsram
, uint32_t *pAddress
, uint16_t *pSrcBuffer
, uint32_t BufferSize
)
399 __IO
uint16_t * psramaddress
= (uint16_t *)pAddress
;
401 /* Check the SRAM controller state */
402 if(hsram
->State
== HAL_SRAM_STATE_PROTECTED
)
410 /* Update the SRAM controller state */
411 hsram
->State
= HAL_SRAM_STATE_BUSY
;
413 /* Write data to memory */
414 for(; BufferSize
!= 0; BufferSize
--)
416 *(__IO
uint16_t *)psramaddress
= *pSrcBuffer
;
421 /* Update the SRAM controller state */
422 hsram
->State
= HAL_SRAM_STATE_READY
;
424 /* Process unlocked */
431 * @brief Reads 32-bit buffer from SRAM memory.
432 * @param hsram: pointer to a SRAM_HandleTypeDef structure that contains
433 * the configuration information for SRAM module.
434 * @param pAddress: Pointer to read start address
435 * @param pDstBuffer: Pointer to destination buffer
436 * @param BufferSize: Size of the buffer to read from memory
439 HAL_StatusTypeDef
HAL_SRAM_Read_32b(SRAM_HandleTypeDef
*hsram
, uint32_t *pAddress
, uint32_t *pDstBuffer
, uint32_t BufferSize
)
444 /* Update the SRAM controller state */
445 hsram
->State
= HAL_SRAM_STATE_BUSY
;
447 /* Read data from memory */
448 for(; BufferSize
!= 0; BufferSize
--)
450 *pDstBuffer
= *(__IO
uint32_t *)pAddress
;
455 /* Update the SRAM controller state */
456 hsram
->State
= HAL_SRAM_STATE_READY
;
458 /* Process unlocked */
465 * @brief Writes 32-bit buffer to SRAM memory.
466 * @param hsram: pointer to a SRAM_HandleTypeDef structure that contains
467 * the configuration information for SRAM module.
468 * @param pAddress: Pointer to write start address
469 * @param pSrcBuffer: Pointer to source buffer to write
470 * @param BufferSize: Size of the buffer to write to memory
473 HAL_StatusTypeDef
HAL_SRAM_Write_32b(SRAM_HandleTypeDef
*hsram
, uint32_t *pAddress
, uint32_t *pSrcBuffer
, uint32_t BufferSize
)
475 /* Check the SRAM controller state */
476 if(hsram
->State
== HAL_SRAM_STATE_PROTECTED
)
484 /* Update the SRAM controller state */
485 hsram
->State
= HAL_SRAM_STATE_BUSY
;
487 /* Write data to memory */
488 for(; BufferSize
!= 0; BufferSize
--)
490 *(__IO
uint32_t *)pAddress
= *pSrcBuffer
;
495 /* Update the SRAM controller state */
496 hsram
->State
= HAL_SRAM_STATE_READY
;
498 /* Process unlocked */
505 * @brief Reads a Words data from the SRAM memory using DMA transfer.
506 * @param hsram: pointer to a SRAM_HandleTypeDef structure that contains
507 * the configuration information for SRAM module.
508 * @param pAddress: Pointer to read start address
509 * @param pDstBuffer: Pointer to destination buffer
510 * @param BufferSize: Size of the buffer to read from memory
513 HAL_StatusTypeDef
HAL_SRAM_Read_DMA(SRAM_HandleTypeDef
*hsram
, uint32_t *pAddress
, uint32_t *pDstBuffer
, uint32_t BufferSize
)
518 /* Update the SRAM controller state */
519 hsram
->State
= HAL_SRAM_STATE_BUSY
;
521 /* Configure DMA user callbacks */
522 hsram
->hdma
->XferCpltCallback
= HAL_SRAM_DMA_XferCpltCallback
;
523 hsram
->hdma
->XferErrorCallback
= HAL_SRAM_DMA_XferErrorCallback
;
525 /* Enable the DMA Stream */
526 HAL_DMA_Start_IT(hsram
->hdma
, (uint32_t)pAddress
, (uint32_t)pDstBuffer
, (uint32_t)BufferSize
);
528 /* Update the SRAM controller state */
529 hsram
->State
= HAL_SRAM_STATE_READY
;
531 /* Process unlocked */
538 * @brief Writes a Words data buffer to SRAM memory using DMA transfer.
539 * @param hsram: pointer to a SRAM_HandleTypeDef structure that contains
540 * the configuration information for SRAM module.
541 * @param pAddress: Pointer to write start address
542 * @param pSrcBuffer: Pointer to source buffer to write
543 * @param BufferSize: Size of the buffer to write to memory
546 HAL_StatusTypeDef
HAL_SRAM_Write_DMA(SRAM_HandleTypeDef
*hsram
, uint32_t *pAddress
, uint32_t *pSrcBuffer
, uint32_t BufferSize
)
548 /* Check the SRAM controller state */
549 if(hsram
->State
== HAL_SRAM_STATE_PROTECTED
)
557 /* Update the SRAM controller state */
558 hsram
->State
= HAL_SRAM_STATE_BUSY
;
560 /* Configure DMA user callbacks */
561 hsram
->hdma
->XferCpltCallback
= HAL_SRAM_DMA_XferCpltCallback
;
562 hsram
->hdma
->XferErrorCallback
= HAL_SRAM_DMA_XferErrorCallback
;
564 /* Enable the DMA Stream */
565 HAL_DMA_Start_IT(hsram
->hdma
, (uint32_t)pSrcBuffer
, (uint32_t)pAddress
, (uint32_t)BufferSize
);
567 /* Update the SRAM controller state */
568 hsram
->State
= HAL_SRAM_STATE_READY
;
570 /* Process unlocked */
580 /** @defgroup SRAM_Exported_Functions_Group3 Control functions
581 * @brief Control functions
584 ==============================================================================
585 ##### SRAM Control functions #####
586 ==============================================================================
588 This subsection provides a set of functions allowing to control dynamically
596 * @brief Enables dynamically SRAM write operation.
597 * @param hsram: pointer to a SRAM_HandleTypeDef structure that contains
598 * the configuration information for SRAM module.
601 HAL_StatusTypeDef
HAL_SRAM_WriteOperation_Enable(SRAM_HandleTypeDef
*hsram
)
606 /* Enable write operation */
607 FMC_NORSRAM_WriteOperation_Enable(hsram
->Instance
, hsram
->Init
.NSBank
);
609 /* Update the SRAM controller state */
610 hsram
->State
= HAL_SRAM_STATE_READY
;
612 /* Process unlocked */
619 * @brief Disables dynamically SRAM write operation.
620 * @param hsram: pointer to a SRAM_HandleTypeDef structure that contains
621 * the configuration information for SRAM module.
624 HAL_StatusTypeDef
HAL_SRAM_WriteOperation_Disable(SRAM_HandleTypeDef
*hsram
)
629 /* Update the SRAM controller state */
630 hsram
->State
= HAL_SRAM_STATE_BUSY
;
632 /* Disable write operation */
633 FMC_NORSRAM_WriteOperation_Disable(hsram
->Instance
, hsram
->Init
.NSBank
);
635 /* Update the SRAM controller state */
636 hsram
->State
= HAL_SRAM_STATE_PROTECTED
;
638 /* Process unlocked */
648 /** @defgroup SRAM_Exported_Functions_Group4 Peripheral State functions
649 * @brief Peripheral State functions
652 ==============================================================================
653 ##### SRAM State functions #####
654 ==============================================================================
656 This subsection permits to get in run-time the status of the SRAM controller
664 * @brief Returns the SRAM controller state
665 * @param hsram: pointer to a SRAM_HandleTypeDef structure that contains
666 * the configuration information for SRAM module.
669 HAL_SRAM_StateTypeDef
HAL_SRAM_GetState(SRAM_HandleTypeDef
*hsram
)
681 #endif /* HAL_SRAM_MODULE_ENABLED */
690 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/