2 ******************************************************************************
3 * @file stm32f3xx_hal_sram.c
4 * @author MCD Application Team
5 * @brief SRAM HAL module driver.
6 * This file provides a generic firmware to drive SRAM memories
7 * mounted as external device.
10 ==============================================================================
11 ##### How to use this driver #####
12 ==============================================================================
14 This driver is a generic layered driver which contains a set of APIs used to
15 control SRAM memories. It uses the FMC layer functions to interface
17 The following sequence should be followed to configure the FMC to interface
18 with SRAM/PSRAM memories:
20 (#) Declare a SRAM_HandleTypeDef handle structure, for example:
21 SRAM_HandleTypeDef hsram; and:
23 (++) Fill the SRAM_HandleTypeDef handle "Init" field with the allowed
24 values of the structure member.
26 (++) Fill the SRAM_HandleTypeDef handle "Instance" field with a predefined
27 base register instance for NOR or SRAM device
29 (++) Fill the SRAM_HandleTypeDef handle "Extended" field with a predefined
30 base register instance for NOR or SRAM extended mode
32 (#) Declare two FMC_NORSRAM_TimingTypeDef structures, for both normal and extended
33 mode timings; for example:
34 FMC_NORSRAM_TimingTypeDef Timing and FMC_NORSRAM_TimingTypeDef ExTiming;
35 and fill its fields with the allowed values of the structure member.
37 (#) Initialize the SRAM Controller by calling the function HAL_SRAM_Init(). This function
38 performs the following sequence:
40 (##) MSP hardware layer configuration using the function HAL_SRAM_MspInit()
41 (##) Control register configuration using the FMC NORSRAM interface function
43 (##) Timing register configuration using the FMC NORSRAM interface function
44 FMC_NORSRAM_Timing_Init()
45 (##) Extended mode Timing register configuration using the FMC NORSRAM interface function
46 FMC_NORSRAM_Extended_Timing_Init()
47 (##) Enable the SRAM device using the macro __FMC_NORSRAM_ENABLE()
49 (#) At this stage you can perform read/write accesses from/to the memory connected
50 to the NOR/SRAM Bank. You can perform either polling or DMA transfer using the
52 (++) HAL_SRAM_Read()/HAL_SRAM_Write() for polling read/write access
53 (++) HAL_SRAM_Read_DMA()/HAL_SRAM_Write_DMA() for DMA read/write transfer
55 (#) You can also control the SRAM device by calling the control APIs HAL_SRAM_WriteOperation_Enable()/
56 HAL_SRAM_WriteOperation_Disable() to respectively enable/disable the SRAM write operation
58 (#) You can continuously monitor the SRAM device HAL state by calling the function
62 ******************************************************************************
65 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
67 * Redistribution and use in source and binary forms, with or without modification,
68 * are permitted provided that the following conditions are met:
69 * 1. Redistributions of source code must retain the above copyright notice,
70 * this list of conditions and the following disclaimer.
71 * 2. Redistributions in binary form must reproduce the above copyright notice,
72 * this list of conditions and the following disclaimer in the documentation
73 * and/or other materials provided with the distribution.
74 * 3. Neither the name of STMicroelectronics nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
78 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
79 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
81 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
84 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
85 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
86 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
87 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
89 ******************************************************************************
92 /* Includes ------------------------------------------------------------------*/
93 #include "stm32f3xx_hal.h"
95 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx)
97 /** @addtogroup STM32F3xx_HAL_Driver
101 #ifdef HAL_SRAM_MODULE_ENABLED
103 /** @defgroup SRAM SRAM
104 * @brief SRAM HAL module driver
107 /* Private typedef -----------------------------------------------------------*/
108 /* Private define ------------------------------------------------------------*/
109 /* Private macro -------------------------------------------------------------*/
110 /* Private variables ---------------------------------------------------------*/
111 /* Private function prototypes -----------------------------------------------*/
112 /* Exported functions --------------------------------------------------------*/
114 /** @defgroup SRAM_Exported_Functions SRAM Exported Functions
118 /** @defgroup SRAM_Exported_Functions_Group1 Initialization and de-initialization functions
119 * @brief Initialization and Configuration functions.
122 ==============================================================================
123 ##### SRAM Initialization and de_initialization functions #####
124 ==============================================================================
125 [..] This section provides functions allowing to initialize/de-initialize
133 * @brief Performs the SRAM device initialization sequence
134 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
135 * the configuration information for SRAM module.
136 * @param Timing Pointer to SRAM control timing structure
137 * @param ExtTiming Pointer to SRAM extended mode timing structure
140 HAL_StatusTypeDef
HAL_SRAM_Init(SRAM_HandleTypeDef
*hsram
, FMC_NORSRAM_TimingTypeDef
*Timing
, FMC_NORSRAM_TimingTypeDef
*ExtTiming
)
142 /* Check the SRAM handle parameter */
148 if(hsram
->State
== HAL_SRAM_STATE_RESET
)
150 /* Allocate lock resource and initialize it */
151 hsram
->Lock
= HAL_UNLOCKED
;
153 /* Initialize the low level hardware (MSP) */
154 HAL_SRAM_MspInit(hsram
);
157 /* Initialize SRAM control Interface */
158 FMC_NORSRAM_Init(hsram
->Instance
, &(hsram
->Init
));
160 /* Initialize SRAM timing Interface */
161 FMC_NORSRAM_Timing_Init(hsram
->Instance
, Timing
, hsram
->Init
.NSBank
);
163 /* Initialize SRAM extended mode timing Interface */
164 FMC_NORSRAM_Extended_Timing_Init(hsram
->Extended
, ExtTiming
, hsram
->Init
.NSBank
, hsram
->Init
.ExtendedMode
);
166 /* Enable the NORSRAM device */
167 __FMC_NORSRAM_ENABLE(hsram
->Instance
, hsram
->Init
.NSBank
);
173 * @brief Performs the SRAM device De-initialization sequence.
174 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
175 * the configuration information for SRAM module.
178 HAL_StatusTypeDef
HAL_SRAM_DeInit(SRAM_HandleTypeDef
*hsram
)
180 /* De-Initialize the low level hardware (MSP) */
181 HAL_SRAM_MspDeInit(hsram
);
183 /* Configure the SRAM registers with their reset values */
184 FMC_NORSRAM_DeInit(hsram
->Instance
, hsram
->Extended
, hsram
->Init
.NSBank
);
186 hsram
->State
= HAL_SRAM_STATE_RESET
;
195 * @brief SRAM MSP Init.
196 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
197 * the configuration information for SRAM module.
200 __weak
void HAL_SRAM_MspInit(SRAM_HandleTypeDef
*hsram
)
202 /* Prevent unused argument(s) compilation warning */
205 /* NOTE : This function Should not be modified, when the callback is needed,
206 the HAL_SRAM_MspInit could be implemented in the user file
211 * @brief SRAM MSP DeInit.
212 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
213 * the configuration information for SRAM module.
216 __weak
void HAL_SRAM_MspDeInit(SRAM_HandleTypeDef
*hsram
)
218 /* Prevent unused argument(s) compilation warning */
221 /* NOTE : This function Should not be modified, when the callback is needed,
222 the HAL_SRAM_MspDeInit could be implemented in the user file
227 * @brief DMA transfer complete callback.
228 * @param hdma pointer to a SRAM_HandleTypeDef structure that contains
229 * the configuration information for SRAM module.
232 __weak
void HAL_SRAM_DMA_XferCpltCallback(DMA_HandleTypeDef
*hdma
)
234 /* Prevent unused argument(s) compilation warning */
237 /* NOTE : This function Should not be modified, when the callback is needed,
238 the HAL_SRAM_DMA_XferCpltCallback could be implemented in the user file
243 * @brief DMA transfer complete error callback.
244 * @param hdma pointer to a SRAM_HandleTypeDef structure that contains
245 * the configuration information for SRAM module.
248 __weak
void HAL_SRAM_DMA_XferErrorCallback(DMA_HandleTypeDef
*hdma
)
250 /* Prevent unused argument(s) compilation warning */
253 /* NOTE : This function Should not be modified, when the callback is needed,
254 the HAL_SRAM_DMA_XferErrorCallback could be implemented in the user file
262 /** @defgroup SRAM_Exported_Functions_Group2 Input Output and memory control functions
263 * @brief Input Output and memory control functions
266 ==============================================================================
267 ##### SRAM Input and Output functions #####
268 ==============================================================================
270 This section provides functions allowing to use and control the SRAM memory
277 * @brief Reads 8-bit buffer from SRAM memory.
278 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
279 * the configuration information for SRAM module.
280 * @param pAddress Pointer to read start address
281 * @param pDstBuffer Pointer to destination buffer
282 * @param BufferSize Size of the buffer to read from memory
285 HAL_StatusTypeDef
HAL_SRAM_Read_8b(SRAM_HandleTypeDef
*hsram
, uint32_t *pAddress
, uint8_t *pDstBuffer
, uint32_t BufferSize
)
287 __IO
uint8_t * psramaddress
= (uint8_t *)pAddress
;
292 /* Update the SRAM controller state */
293 hsram
->State
= HAL_SRAM_STATE_BUSY
;
295 /* Read data from memory */
296 for(; BufferSize
!= 0U; BufferSize
--)
298 *pDstBuffer
= *(__IO
uint8_t *)psramaddress
;
303 /* Update the SRAM controller state */
304 hsram
->State
= HAL_SRAM_STATE_READY
;
306 /* Process unlocked */
313 * @brief Writes 8-bit buffer to SRAM memory.
314 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
315 * the configuration information for SRAM module.
316 * @param pAddress Pointer to write start address
317 * @param pSrcBuffer Pointer to source buffer to write
318 * @param BufferSize Size of the buffer to write to memory
321 HAL_StatusTypeDef
HAL_SRAM_Write_8b(SRAM_HandleTypeDef
*hsram
, uint32_t *pAddress
, uint8_t *pSrcBuffer
, uint32_t BufferSize
)
323 __IO
uint8_t * psramaddress
= (uint8_t *)pAddress
;
325 /* Check the SRAM controller state */
326 if(hsram
->State
== HAL_SRAM_STATE_PROTECTED
)
334 /* Update the SRAM controller state */
335 hsram
->State
= HAL_SRAM_STATE_BUSY
;
337 /* Write data to memory */
338 for(; BufferSize
!= 0U; BufferSize
--)
340 *(__IO
uint8_t *)psramaddress
= *pSrcBuffer
;
345 /* Update the SRAM controller state */
346 hsram
->State
= HAL_SRAM_STATE_READY
;
348 /* Process unlocked */
355 * @brief Reads 16-bit buffer from SRAM memory.
356 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
357 * the configuration information for SRAM module.
358 * @param pAddress Pointer to read start address
359 * @param pDstBuffer Pointer to destination buffer
360 * @param BufferSize Size of the buffer to read from memory
363 HAL_StatusTypeDef
HAL_SRAM_Read_16b(SRAM_HandleTypeDef
*hsram
, uint32_t *pAddress
, uint16_t *pDstBuffer
, uint32_t BufferSize
)
365 __IO
uint16_t * psramaddress
= (uint16_t *)pAddress
;
370 /* Update the SRAM controller state */
371 hsram
->State
= HAL_SRAM_STATE_BUSY
;
373 /* Read data from memory */
374 for(; BufferSize
!= 0U; BufferSize
--)
376 *pDstBuffer
= *(__IO
uint16_t *)psramaddress
;
381 /* Update the SRAM controller state */
382 hsram
->State
= HAL_SRAM_STATE_READY
;
384 /* Process unlocked */
391 * @brief Writes 16-bit buffer to SRAM memory.
392 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
393 * the configuration information for SRAM module.
394 * @param pAddress Pointer to write start address
395 * @param pSrcBuffer Pointer to source buffer to write
396 * @param BufferSize Size of the buffer to write to memory
399 HAL_StatusTypeDef
HAL_SRAM_Write_16b(SRAM_HandleTypeDef
*hsram
, uint32_t *pAddress
, uint16_t *pSrcBuffer
, uint32_t BufferSize
)
401 __IO
uint16_t * psramaddress
= (uint16_t *)pAddress
;
403 /* Check the SRAM controller state */
404 if(hsram
->State
== HAL_SRAM_STATE_PROTECTED
)
412 /* Update the SRAM controller state */
413 hsram
->State
= HAL_SRAM_STATE_BUSY
;
415 /* Write data to memory */
416 for(; BufferSize
!= 0U; BufferSize
--)
418 *(__IO
uint16_t *)psramaddress
= *pSrcBuffer
;
423 /* Update the SRAM controller state */
424 hsram
->State
= HAL_SRAM_STATE_READY
;
426 /* Process unlocked */
433 * @brief Reads 32-bit buffer from SRAM memory.
434 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
435 * the configuration information for SRAM module.
436 * @param pAddress Pointer to read start address
437 * @param pDstBuffer Pointer to destination buffer
438 * @param BufferSize Size of the buffer to read from memory
441 HAL_StatusTypeDef
HAL_SRAM_Read_32b(SRAM_HandleTypeDef
*hsram
, uint32_t *pAddress
, uint32_t *pDstBuffer
, uint32_t BufferSize
)
446 /* Update the SRAM controller state */
447 hsram
->State
= HAL_SRAM_STATE_BUSY
;
449 /* Read data from memory */
450 for(; BufferSize
!= 0U; BufferSize
--)
452 *pDstBuffer
= *(__IO
uint32_t *)pAddress
;
457 /* Update the SRAM controller state */
458 hsram
->State
= HAL_SRAM_STATE_READY
;
460 /* Process unlocked */
467 * @brief Writes 32-bit buffer to SRAM memory.
468 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
469 * the configuration information for SRAM module.
470 * @param pAddress Pointer to write start address
471 * @param pSrcBuffer Pointer to source buffer to write
472 * @param BufferSize Size of the buffer to write to memory
475 HAL_StatusTypeDef
HAL_SRAM_Write_32b(SRAM_HandleTypeDef
*hsram
, uint32_t *pAddress
, uint32_t *pSrcBuffer
, uint32_t BufferSize
)
477 /* Check the SRAM controller state */
478 if(hsram
->State
== HAL_SRAM_STATE_PROTECTED
)
486 /* Update the SRAM controller state */
487 hsram
->State
= HAL_SRAM_STATE_BUSY
;
489 /* Write data to memory */
490 for(; BufferSize
!= 0U; BufferSize
--)
492 *(__IO
uint32_t *)pAddress
= *pSrcBuffer
;
497 /* Update the SRAM controller state */
498 hsram
->State
= HAL_SRAM_STATE_READY
;
500 /* Process unlocked */
507 * @brief Reads a Words data from the SRAM memory using DMA transfer.
508 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
509 * the configuration information for SRAM module.
510 * @param pAddress Pointer to read start address
511 * @param pDstBuffer Pointer to destination buffer
512 * @param BufferSize Size of the buffer to read from memory
515 HAL_StatusTypeDef
HAL_SRAM_Read_DMA(SRAM_HandleTypeDef
*hsram
, uint32_t *pAddress
, uint32_t *pDstBuffer
, uint32_t BufferSize
)
520 /* Update the SRAM controller state */
521 hsram
->State
= HAL_SRAM_STATE_BUSY
;
523 /* Configure DMA user callbacks */
524 hsram
->hdma
->XferCpltCallback
= HAL_SRAM_DMA_XferCpltCallback
;
525 hsram
->hdma
->XferErrorCallback
= HAL_SRAM_DMA_XferErrorCallback
;
527 /* Enable the DMA Channel */
528 HAL_DMA_Start_IT(hsram
->hdma
, (uint32_t)pAddress
, (uint32_t)pDstBuffer
, (uint32_t)BufferSize
);
530 /* Update the SRAM controller state */
531 hsram
->State
= HAL_SRAM_STATE_READY
;
533 /* Process unlocked */
540 * @brief Writes a Words data buffer to SRAM memory using DMA transfer.
541 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
542 * the configuration information for SRAM module.
543 * @param pAddress Pointer to write start address
544 * @param pSrcBuffer Pointer to source buffer to write
545 * @param BufferSize Size of the buffer to write to memory
548 HAL_StatusTypeDef
HAL_SRAM_Write_DMA(SRAM_HandleTypeDef
*hsram
, uint32_t *pAddress
, uint32_t *pSrcBuffer
, uint32_t BufferSize
)
550 /* Check the SRAM controller state */
551 if(hsram
->State
== HAL_SRAM_STATE_PROTECTED
)
559 /* Update the SRAM controller state */
560 hsram
->State
= HAL_SRAM_STATE_BUSY
;
562 /* Configure DMA user callbacks */
563 hsram
->hdma
->XferCpltCallback
= HAL_SRAM_DMA_XferCpltCallback
;
564 hsram
->hdma
->XferErrorCallback
= HAL_SRAM_DMA_XferErrorCallback
;
566 /* Enable the DMA Channel */
567 HAL_DMA_Start_IT(hsram
->hdma
, (uint32_t)pSrcBuffer
, (uint32_t)pAddress
, (uint32_t)BufferSize
);
569 /* Update the SRAM controller state */
570 hsram
->State
= HAL_SRAM_STATE_READY
;
572 /* Process unlocked */
582 /** @defgroup SRAM_Exported_Functions_Group3 Control functions
583 * @brief Control functions
586 ==============================================================================
587 ##### SRAM Control functions #####
588 ==============================================================================
590 This subsection provides a set of functions allowing to control dynamically
598 * @brief Enables dynamically SRAM write operation.
599 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
600 * the configuration information for SRAM module.
603 HAL_StatusTypeDef
HAL_SRAM_WriteOperation_Enable(SRAM_HandleTypeDef
*hsram
)
608 /* Enable write operation */
609 FMC_NORSRAM_WriteOperation_Enable(hsram
->Instance
, hsram
->Init
.NSBank
);
611 /* Update the SRAM controller state */
612 hsram
->State
= HAL_SRAM_STATE_READY
;
614 /* Process unlocked */
621 * @brief Disables dynamically SRAM write operation.
622 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
623 * the configuration information for SRAM module.
626 HAL_StatusTypeDef
HAL_SRAM_WriteOperation_Disable(SRAM_HandleTypeDef
*hsram
)
631 /* Update the SRAM controller state */
632 hsram
->State
= HAL_SRAM_STATE_BUSY
;
634 /* Disable write operation */
635 FMC_NORSRAM_WriteOperation_Disable(hsram
->Instance
, hsram
->Init
.NSBank
);
637 /* Update the SRAM controller state */
638 hsram
->State
= HAL_SRAM_STATE_PROTECTED
;
640 /* Process unlocked */
650 /** @defgroup SRAM_Exported_Functions_Group4 Peripheral State functions
651 * @brief Peripheral State functions
654 ==============================================================================
655 ##### SRAM State functions #####
656 ==============================================================================
658 This subsection permits to get in run-time the status of the SRAM controller
666 * @brief Returns the SRAM controller state
667 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
668 * the configuration information for SRAM module.
671 HAL_SRAM_StateTypeDef
HAL_SRAM_GetState(SRAM_HandleTypeDef
*hsram
)
686 #endif /* HAL_SRAM_MODULE_ENABLED */
691 #endif /* STM32F302xE || STM32F303xE || STM32F398xx */
693 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/