Merge pull request #11189 from klutvott123/move-telemetry-displayport-init
[betaflight.git] / lib / main / STM32F3 / Drivers / STM32F3xx_HAL_Driver / Src / stm32f3xx_hal_sram.c
blobd9c141b6801ff725134fc97d4ab3e015be9bc381
1 /**
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.
8 *
9 @verbatim
10 ==============================================================================
11 ##### How to use this driver #####
12 ==============================================================================
13 [..]
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
16 with SRAM devices.
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
42 FMC_NORSRAM_Init()
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
51 following APIs:
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
59 HAL_SRAM_GetState()
61 @endverbatim
62 ******************************************************************************
63 * @attention
65 * <h2><center>&copy; 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 ******************************************************************************
90 */
92 /* Includes ------------------------------------------------------------------*/
93 #include "stm32f3xx_hal.h"
95 #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx)
97 /** @addtogroup STM32F3xx_HAL_Driver
98 * @{
101 #ifdef HAL_SRAM_MODULE_ENABLED
103 /** @defgroup SRAM SRAM
104 * @brief SRAM HAL module driver
105 * @{
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
115 * @{
118 /** @defgroup SRAM_Exported_Functions_Group1 Initialization and de-initialization functions
119 * @brief Initialization and Configuration functions.
121 @verbatim
122 ==============================================================================
123 ##### SRAM Initialization and de_initialization functions #####
124 ==============================================================================
125 [..] This section provides functions allowing to initialize/de-initialize
126 the SRAM memory
128 @endverbatim
129 * @{
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
138 * @retval HAL status
140 HAL_StatusTypeDef HAL_SRAM_Init(SRAM_HandleTypeDef *hsram, FMC_NORSRAM_TimingTypeDef *Timing, FMC_NORSRAM_TimingTypeDef *ExtTiming)
142 /* Check the SRAM handle parameter */
143 if(hsram == NULL)
145 return HAL_ERROR;
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);
169 return HAL_OK;
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.
176 * @retval HAL status
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;
188 /* Release Lock */
189 __HAL_UNLOCK(hsram);
191 return HAL_OK;
195 * @brief SRAM MSP Init.
196 * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
197 * the configuration information for SRAM module.
198 * @retval None
200 __weak void HAL_SRAM_MspInit(SRAM_HandleTypeDef *hsram)
202 /* Prevent unused argument(s) compilation warning */
203 UNUSED(hsram);
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.
214 * @retval None
216 __weak void HAL_SRAM_MspDeInit(SRAM_HandleTypeDef *hsram)
218 /* Prevent unused argument(s) compilation warning */
219 UNUSED(hsram);
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.
230 * @retval None
232 __weak void HAL_SRAM_DMA_XferCpltCallback(DMA_HandleTypeDef *hdma)
234 /* Prevent unused argument(s) compilation warning */
235 UNUSED(hdma);
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.
246 * @retval None
248 __weak void HAL_SRAM_DMA_XferErrorCallback(DMA_HandleTypeDef *hdma)
250 /* Prevent unused argument(s) compilation warning */
251 UNUSED(hdma);
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
259 * @}
262 /** @defgroup SRAM_Exported_Functions_Group2 Input Output and memory control functions
263 * @brief Input Output and memory control functions
265 @verbatim
266 ==============================================================================
267 ##### SRAM Input and Output functions #####
268 ==============================================================================
269 [..]
270 This section provides functions allowing to use and control the SRAM memory
272 @endverbatim
273 * @{
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
283 * @retval HAL status
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;
289 /* Process Locked */
290 __HAL_LOCK(hsram);
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;
299 pDstBuffer++;
300 psramaddress++;
303 /* Update the SRAM controller state */
304 hsram->State = HAL_SRAM_STATE_READY;
306 /* Process unlocked */
307 __HAL_UNLOCK(hsram);
309 return HAL_OK;
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
319 * @retval HAL status
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)
328 return HAL_ERROR;
331 /* Process Locked */
332 __HAL_LOCK(hsram);
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;
341 pSrcBuffer++;
342 psramaddress++;
345 /* Update the SRAM controller state */
346 hsram->State = HAL_SRAM_STATE_READY;
348 /* Process unlocked */
349 __HAL_UNLOCK(hsram);
351 return HAL_OK;
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
361 * @retval HAL status
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;
367 /* Process Locked */
368 __HAL_LOCK(hsram);
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;
377 pDstBuffer++;
378 psramaddress++;
381 /* Update the SRAM controller state */
382 hsram->State = HAL_SRAM_STATE_READY;
384 /* Process unlocked */
385 __HAL_UNLOCK(hsram);
387 return HAL_OK;
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
397 * @retval HAL status
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)
406 return HAL_ERROR;
409 /* Process Locked */
410 __HAL_LOCK(hsram);
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;
419 pSrcBuffer++;
420 psramaddress++;
423 /* Update the SRAM controller state */
424 hsram->State = HAL_SRAM_STATE_READY;
426 /* Process unlocked */
427 __HAL_UNLOCK(hsram);
429 return HAL_OK;
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
439 * @retval HAL status
441 HAL_StatusTypeDef HAL_SRAM_Read_32b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
443 /* Process Locked */
444 __HAL_LOCK(hsram);
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;
453 pDstBuffer++;
454 pAddress++;
457 /* Update the SRAM controller state */
458 hsram->State = HAL_SRAM_STATE_READY;
460 /* Process unlocked */
461 __HAL_UNLOCK(hsram);
463 return HAL_OK;
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
473 * @retval HAL status
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)
480 return HAL_ERROR;
483 /* Process Locked */
484 __HAL_LOCK(hsram);
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;
493 pSrcBuffer++;
494 pAddress++;
497 /* Update the SRAM controller state */
498 hsram->State = HAL_SRAM_STATE_READY;
500 /* Process unlocked */
501 __HAL_UNLOCK(hsram);
503 return HAL_OK;
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
513 * @retval HAL status
515 HAL_StatusTypeDef HAL_SRAM_Read_DMA(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
517 /* Process Locked */
518 __HAL_LOCK(hsram);
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 */
534 __HAL_UNLOCK(hsram);
536 return HAL_OK;
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
546 * @retval HAL status
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)
553 return HAL_ERROR;
556 /* Process Locked */
557 __HAL_LOCK(hsram);
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 */
573 __HAL_UNLOCK(hsram);
575 return HAL_OK;
579 * @}
582 /** @defgroup SRAM_Exported_Functions_Group3 Control functions
583 * @brief Control functions
585 @verbatim
586 ==============================================================================
587 ##### SRAM Control functions #####
588 ==============================================================================
589 [..]
590 This subsection provides a set of functions allowing to control dynamically
591 the SRAM interface.
593 @endverbatim
594 * @{
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.
601 * @retval HAL status
603 HAL_StatusTypeDef HAL_SRAM_WriteOperation_Enable(SRAM_HandleTypeDef *hsram)
605 /* Process Locked */
606 __HAL_LOCK(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 */
615 __HAL_UNLOCK(hsram);
617 return HAL_OK;
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.
624 * @retval HAL status
626 HAL_StatusTypeDef HAL_SRAM_WriteOperation_Disable(SRAM_HandleTypeDef *hsram)
628 /* Process Locked */
629 __HAL_LOCK(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 */
641 __HAL_UNLOCK(hsram);
643 return HAL_OK;
647 * @}
650 /** @defgroup SRAM_Exported_Functions_Group4 Peripheral State functions
651 * @brief Peripheral State functions
653 @verbatim
654 ==============================================================================
655 ##### SRAM State functions #####
656 ==============================================================================
657 [..]
658 This subsection permits to get in run-time the status of the SRAM controller
659 and the data flow.
661 @endverbatim
662 * @{
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.
669 * @retval HAL state
671 HAL_SRAM_StateTypeDef HAL_SRAM_GetState(SRAM_HandleTypeDef *hsram)
673 return hsram->State;
677 * @}
681 * @}
684 * @}
686 #endif /* HAL_SRAM_MODULE_ENABLED */
689 * @}
691 #endif /* STM32F302xE || STM32F303xE || STM32F398xx */
693 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/