stm32f4xx_hal_sram.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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/FSMC to interface
  18. with SRAM/PSRAM memories:
  19. (#) Declare a SRAM_HandleTypeDef handle structure, for example:
  20. SRAM_HandleTypeDef hsram; and:
  21. (++) Fill the SRAM_HandleTypeDef handle "Init" field with the allowed
  22. values of the structure member.
  23. (++) Fill the SRAM_HandleTypeDef handle "Instance" field with a predefined
  24. base register instance for NOR or SRAM device
  25. (++) Fill the SRAM_HandleTypeDef handle "Extended" field with a predefined
  26. base register instance for NOR or SRAM extended mode
  27. (#) Declare two FMC_NORSRAM_TimingTypeDef structures, for both normal and extended
  28. mode timings; for example:
  29. FMC_NORSRAM_TimingTypeDef Timing and FMC_NORSRAM_TimingTypeDef ExTiming;
  30. and fill its fields with the allowed values of the structure member.
  31. (#) Initialize the SRAM Controller by calling the function HAL_SRAM_Init(). This function
  32. performs the following sequence:
  33. (##) MSP hardware layer configuration using the function HAL_SRAM_MspInit()
  34. (##) Control register configuration using the FMC NORSRAM interface function
  35. FMC_NORSRAM_Init()
  36. (##) Timing register configuration using the FMC NORSRAM interface function
  37. FMC_NORSRAM_Timing_Init()
  38. (##) Extended mode Timing register configuration using the FMC NORSRAM interface function
  39. FMC_NORSRAM_Extended_Timing_Init()
  40. (##) Enable the SRAM device using the macro __FMC_NORSRAM_ENABLE()
  41. (#) At this stage you can perform read/write accesses from/to the memory connected
  42. to the NOR/SRAM Bank. You can perform either polling or DMA transfer using the
  43. following APIs:
  44. (++) HAL_SRAM_Read()/HAL_SRAM_Write() for polling read/write access
  45. (++) HAL_SRAM_Read_DMA()/HAL_SRAM_Write_DMA() for DMA read/write transfer
  46. (#) You can also control the SRAM device by calling the control APIs HAL_SRAM_WriteOperation_Enable()/
  47. HAL_SRAM_WriteOperation_Disable() to respectively enable/disable the SRAM write operation
  48. (#) You can continuously monitor the SRAM device HAL state by calling the function
  49. HAL_SRAM_GetState()
  50. *** Callback registration ***
  51. =============================================
  52. [..]
  53. The compilation define USE_HAL_SRAM_REGISTER_CALLBACKS when set to 1
  54. allows the user to configure dynamically the driver callbacks.
  55. Use Functions @ref HAL_SRAM_RegisterCallback() to register a user callback,
  56. it allows to register following callbacks:
  57. (+) MspInitCallback : SRAM MspInit.
  58. (+) MspDeInitCallback : SRAM MspDeInit.
  59. This function takes as parameters the HAL peripheral handle, the Callback ID
  60. and a pointer to the user callback function.
  61. Use function @ref HAL_SRAM_UnRegisterCallback() to reset a callback to the default
  62. weak (surcharged) function. It allows to reset following callbacks:
  63. (+) MspInitCallback : SRAM MspInit.
  64. (+) MspDeInitCallback : SRAM MspDeInit.
  65. This function) takes as parameters the HAL peripheral handle and the Callback ID.
  66. By default, after the @ref HAL_SRAM_Init and if the state is HAL_SRAM_STATE_RESET
  67. all callbacks are reset to the corresponding legacy weak (surcharged) functions.
  68. Exception done for MspInit and MspDeInit callbacks that are respectively
  69. reset to the legacy weak (surcharged) functions in the @ref HAL_SRAM_Init
  70. and @ref HAL_SRAM_DeInit only when these callbacks are null (not registered beforehand).
  71. If not, MspInit or MspDeInit are not null, the @ref HAL_SRAM_Init and @ref HAL_SRAM_DeInit
  72. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  73. Callbacks can be registered/unregistered in READY state only.
  74. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  75. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  76. during the Init/DeInit.
  77. In that case first register the MspInit/MspDeInit user callbacks
  78. using @ref HAL_SRAM_RegisterCallback before calling @ref HAL_SRAM_DeInit
  79. or @ref HAL_SRAM_Init function.
  80. When The compilation define USE_HAL_SRAM_REGISTER_CALLBACKS is set to 0 or
  81. not defined, the callback registering feature is not available
  82. and weak (surcharged) callbacks are used.
  83. @endverbatim
  84. ******************************************************************************
  85. * @attention
  86. *
  87. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  88. * All rights reserved.</center></h2>
  89. *
  90. * This software component is licensed by ST under BSD 3-Clause license,
  91. * the "License"; You may not use this file except in compliance with the
  92. * License. You may obtain a copy of the License at:
  93. * opensource.org/licenses/BSD-3-Clause
  94. *
  95. ******************************************************************************
  96. */
  97. /* Includes ------------------------------------------------------------------*/
  98. #include "stm32f4xx_hal.h"
  99. /** @addtogroup STM32F4xx_HAL_Driver
  100. * @{
  101. */
  102. /** @defgroup SRAM SRAM
  103. * @brief SRAM driver modules
  104. * @{
  105. */
  106. #ifdef HAL_SRAM_MODULE_ENABLED
  107. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
  108. defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
  109. defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) ||\
  110. defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F413xx) || defined(STM32F423xx)
  111. /* Private typedef -----------------------------------------------------------*/
  112. /* Private define ------------------------------------------------------------*/
  113. /* Private macro -------------------------------------------------------------*/
  114. /* Private variables ---------------------------------------------------------*/
  115. /* Private functions ---------------------------------------------------------*/
  116. /* Exported functions --------------------------------------------------------*/
  117. /** @defgroup SRAM_Exported_Functions SRAM Exported Functions
  118. * @{
  119. */
  120. /** @defgroup SRAM_Exported_Functions_Group1 Initialization and de-initialization functions
  121. * @brief Initialization and Configuration functions
  122. *
  123. @verbatim
  124. ==============================================================================
  125. ##### SRAM Initialization and de_initialization functions #####
  126. ==============================================================================
  127. [..] This section provides functions allowing to initialize/de-initialize
  128. the SRAM memory
  129. @endverbatim
  130. * @{
  131. */
  132. /**
  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
  139. */
  140. HAL_StatusTypeDef HAL_SRAM_Init(SRAM_HandleTypeDef *hsram, FMC_NORSRAM_TimingTypeDef *Timing, FMC_NORSRAM_TimingTypeDef *ExtTiming)
  141. {
  142. /* Check the SRAM handle parameter */
  143. if(hsram == NULL)
  144. {
  145. return HAL_ERROR;
  146. }
  147. if(hsram->State == HAL_SRAM_STATE_RESET)
  148. {
  149. /* Allocate lock resource and initialize it */
  150. hsram->Lock = HAL_UNLOCKED;
  151. #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
  152. if(hsram->MspInitCallback == NULL)
  153. {
  154. hsram->MspInitCallback = HAL_SRAM_MspInit;
  155. }
  156. hsram->DmaXferCpltCallback = HAL_SRAM_DMA_XferCpltCallback;
  157. hsram->DmaXferErrorCallback = HAL_SRAM_DMA_XferErrorCallback;
  158. /* Init the low level hardware */
  159. hsram->MspInitCallback(hsram);
  160. #else
  161. /* Initialize the low level hardware (MSP) */
  162. HAL_SRAM_MspInit(hsram);
  163. #endif
  164. }
  165. /* Initialize SRAM control Interface */
  166. FMC_NORSRAM_Init(hsram->Instance, &(hsram->Init));
  167. /* Initialize SRAM timing Interface */
  168. FMC_NORSRAM_Timing_Init(hsram->Instance, Timing, hsram->Init.NSBank);
  169. /* Initialize SRAM extended mode timing Interface */
  170. FMC_NORSRAM_Extended_Timing_Init(hsram->Extended, ExtTiming, hsram->Init.NSBank, hsram->Init.ExtendedMode);
  171. /* Enable the NORSRAM device */
  172. __FMC_NORSRAM_ENABLE(hsram->Instance, hsram->Init.NSBank);
  173. return HAL_OK;
  174. }
  175. /**
  176. * @brief Performs the SRAM device De-initialization sequence.
  177. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  178. * the configuration information for SRAM module.
  179. * @retval HAL status
  180. */
  181. HAL_StatusTypeDef HAL_SRAM_DeInit(SRAM_HandleTypeDef *hsram)
  182. {
  183. #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
  184. if(hsram->MspDeInitCallback == NULL)
  185. {
  186. hsram->MspDeInitCallback = HAL_SRAM_MspDeInit;
  187. }
  188. /* DeInit the low level hardware */
  189. hsram->MspDeInitCallback(hsram);
  190. #else
  191. /* De-Initialize the low level hardware (MSP) */
  192. HAL_SRAM_MspDeInit(hsram);
  193. #endif
  194. /* Configure the SRAM registers with their reset values */
  195. FMC_NORSRAM_DeInit(hsram->Instance, hsram->Extended, hsram->Init.NSBank);
  196. hsram->State = HAL_SRAM_STATE_RESET;
  197. /* Release Lock */
  198. __HAL_UNLOCK(hsram);
  199. return HAL_OK;
  200. }
  201. /**
  202. * @brief SRAM MSP Init.
  203. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  204. * the configuration information for SRAM module.
  205. * @retval None
  206. */
  207. __weak void HAL_SRAM_MspInit(SRAM_HandleTypeDef *hsram)
  208. {
  209. /* Prevent unused argument(s) compilation warning */
  210. UNUSED(hsram);
  211. /* NOTE : This function Should not be modified, when the callback is needed,
  212. the HAL_SRAM_MspInit could be implemented in the user file
  213. */
  214. }
  215. /**
  216. * @brief SRAM MSP DeInit.
  217. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  218. * the configuration information for SRAM module.
  219. * @retval None
  220. */
  221. __weak void HAL_SRAM_MspDeInit(SRAM_HandleTypeDef *hsram)
  222. {
  223. /* Prevent unused argument(s) compilation warning */
  224. UNUSED(hsram);
  225. /* NOTE : This function Should not be modified, when the callback is needed,
  226. the HAL_SRAM_MspDeInit could be implemented in the user file
  227. */
  228. }
  229. /**
  230. * @brief DMA transfer complete callback.
  231. * @param hdma pointer to a SRAM_HandleTypeDef structure that contains
  232. * the configuration information for SRAM module.
  233. * @retval None
  234. */
  235. __weak void HAL_SRAM_DMA_XferCpltCallback(DMA_HandleTypeDef *hdma)
  236. {
  237. /* Prevent unused argument(s) compilation warning */
  238. UNUSED(hdma);
  239. /* NOTE : This function Should not be modified, when the callback is needed,
  240. the HAL_SRAM_DMA_XferCpltCallback could be implemented in the user file
  241. */
  242. }
  243. /**
  244. * @brief DMA transfer complete error callback.
  245. * @param hdma pointer to a SRAM_HandleTypeDef structure that contains
  246. * the configuration information for SRAM module.
  247. * @retval None
  248. */
  249. __weak void HAL_SRAM_DMA_XferErrorCallback(DMA_HandleTypeDef *hdma)
  250. {
  251. /* Prevent unused argument(s) compilation warning */
  252. 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
  255. */
  256. }
  257. /**
  258. * @}
  259. */
  260. /** @defgroup SRAM_Exported_Functions_Group2 Input and Output functions
  261. * @brief Input Output and memory control functions
  262. *
  263. @verbatim
  264. ==============================================================================
  265. ##### SRAM Input and Output functions #####
  266. ==============================================================================
  267. [..]
  268. This section provides functions allowing to use and control the SRAM memory
  269. @endverbatim
  270. * @{
  271. */
  272. /**
  273. * @brief Reads 8-bit buffer from SRAM memory.
  274. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  275. * the configuration information for SRAM module.
  276. * @param pAddress Pointer to read start address
  277. * @param pDstBuffer Pointer to destination buffer
  278. * @param BufferSize Size of the buffer to read from memory
  279. * @retval HAL status
  280. */
  281. HAL_StatusTypeDef HAL_SRAM_Read_8b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint8_t *pDstBuffer, uint32_t BufferSize)
  282. {
  283. __IO uint8_t * pSramAddress = (uint8_t *)pAddress;
  284. /* Process Locked */
  285. __HAL_LOCK(hsram);
  286. /* Update the SRAM controller state */
  287. hsram->State = HAL_SRAM_STATE_BUSY;
  288. /* Read data from memory */
  289. for(; BufferSize != 0U; BufferSize--)
  290. {
  291. *pDstBuffer = *(__IO uint8_t *)pSramAddress;
  292. pDstBuffer++;
  293. pSramAddress++;
  294. }
  295. /* Update the SRAM controller state */
  296. hsram->State = HAL_SRAM_STATE_READY;
  297. /* Process unlocked */
  298. __HAL_UNLOCK(hsram);
  299. return HAL_OK;
  300. }
  301. /**
  302. * @brief Writes 8-bit buffer to SRAM memory.
  303. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  304. * the configuration information for SRAM module.
  305. * @param pAddress Pointer to write start address
  306. * @param pSrcBuffer Pointer to source buffer to write
  307. * @param BufferSize Size of the buffer to write to memory
  308. * @retval HAL status
  309. */
  310. HAL_StatusTypeDef HAL_SRAM_Write_8b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint8_t *pSrcBuffer, uint32_t BufferSize)
  311. {
  312. __IO uint8_t * pSramAddress = (uint8_t *)pAddress;
  313. /* Check the SRAM controller state */
  314. if(hsram->State == HAL_SRAM_STATE_PROTECTED)
  315. {
  316. return HAL_ERROR;
  317. }
  318. /* Process Locked */
  319. __HAL_LOCK(hsram);
  320. /* Update the SRAM controller state */
  321. hsram->State = HAL_SRAM_STATE_BUSY;
  322. /* Write data to memory */
  323. for(; BufferSize != 0U; BufferSize--)
  324. {
  325. *(__IO uint8_t *)pSramAddress = *pSrcBuffer;
  326. pSrcBuffer++;
  327. pSramAddress++;
  328. }
  329. /* Update the SRAM controller state */
  330. hsram->State = HAL_SRAM_STATE_READY;
  331. /* Process unlocked */
  332. __HAL_UNLOCK(hsram);
  333. return HAL_OK;
  334. }
  335. /**
  336. * @brief Reads 16-bit buffer from SRAM memory.
  337. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  338. * the configuration information for SRAM module.
  339. * @param pAddress Pointer to read start address
  340. * @param pDstBuffer Pointer to destination buffer
  341. * @param BufferSize Size of the buffer to read from memory
  342. * @retval HAL status
  343. */
  344. HAL_StatusTypeDef HAL_SRAM_Read_16b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint16_t *pDstBuffer, uint32_t BufferSize)
  345. {
  346. __IO uint16_t * pSramAddress = (uint16_t *)pAddress;
  347. /* Process Locked */
  348. __HAL_LOCK(hsram);
  349. /* Update the SRAM controller state */
  350. hsram->State = HAL_SRAM_STATE_BUSY;
  351. /* Read data from memory */
  352. for(; BufferSize != 0U; BufferSize--)
  353. {
  354. *pDstBuffer = *(__IO uint16_t *)pSramAddress;
  355. pDstBuffer++;
  356. pSramAddress++;
  357. }
  358. /* Update the SRAM controller state */
  359. hsram->State = HAL_SRAM_STATE_READY;
  360. /* Process unlocked */
  361. __HAL_UNLOCK(hsram);
  362. return HAL_OK;
  363. }
  364. /**
  365. * @brief Writes 16-bit buffer to SRAM memory.
  366. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  367. * the configuration information for SRAM module.
  368. * @param pAddress Pointer to write start address
  369. * @param pSrcBuffer Pointer to source buffer to write
  370. * @param BufferSize Size of the buffer to write to memory
  371. * @retval HAL status
  372. */
  373. HAL_StatusTypeDef HAL_SRAM_Write_16b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint16_t *pSrcBuffer, uint32_t BufferSize)
  374. {
  375. __IO uint16_t * pSramAddress = (uint16_t *)pAddress;
  376. /* Check the SRAM controller state */
  377. if(hsram->State == HAL_SRAM_STATE_PROTECTED)
  378. {
  379. return HAL_ERROR;
  380. }
  381. /* Process Locked */
  382. __HAL_LOCK(hsram);
  383. /* Update the SRAM controller state */
  384. hsram->State = HAL_SRAM_STATE_BUSY;
  385. /* Write data to memory */
  386. for(; BufferSize != 0U; BufferSize--)
  387. {
  388. *(__IO uint16_t *)pSramAddress = *pSrcBuffer;
  389. pSrcBuffer++;
  390. pSramAddress++;
  391. }
  392. /* Update the SRAM controller state */
  393. hsram->State = HAL_SRAM_STATE_READY;
  394. /* Process unlocked */
  395. __HAL_UNLOCK(hsram);
  396. return HAL_OK;
  397. }
  398. /**
  399. * @brief Reads 32-bit buffer from SRAM memory.
  400. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  401. * the configuration information for SRAM module.
  402. * @param pAddress Pointer to read start address
  403. * @param pDstBuffer Pointer to destination buffer
  404. * @param BufferSize Size of the buffer to read from memory
  405. * @retval HAL status
  406. */
  407. HAL_StatusTypeDef HAL_SRAM_Read_32b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
  408. {
  409. /* Process Locked */
  410. __HAL_LOCK(hsram);
  411. /* Update the SRAM controller state */
  412. hsram->State = HAL_SRAM_STATE_BUSY;
  413. /* Read data from memory */
  414. for(; BufferSize != 0U; BufferSize--)
  415. {
  416. *pDstBuffer = *(__IO uint32_t *)pAddress;
  417. pDstBuffer++;
  418. pAddress++;
  419. }
  420. /* Update the SRAM controller state */
  421. hsram->State = HAL_SRAM_STATE_READY;
  422. /* Process unlocked */
  423. __HAL_UNLOCK(hsram);
  424. return HAL_OK;
  425. }
  426. /**
  427. * @brief Writes 32-bit buffer to SRAM memory.
  428. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  429. * the configuration information for SRAM module.
  430. * @param pAddress Pointer to write start address
  431. * @param pSrcBuffer Pointer to source buffer to write
  432. * @param BufferSize Size of the buffer to write to memory
  433. * @retval HAL status
  434. */
  435. HAL_StatusTypeDef HAL_SRAM_Write_32b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)
  436. {
  437. /* Check the SRAM controller state */
  438. if(hsram->State == HAL_SRAM_STATE_PROTECTED)
  439. {
  440. return HAL_ERROR;
  441. }
  442. /* Process Locked */
  443. __HAL_LOCK(hsram);
  444. /* Update the SRAM controller state */
  445. hsram->State = HAL_SRAM_STATE_BUSY;
  446. /* Write data to memory */
  447. for(; BufferSize != 0U; BufferSize--)
  448. {
  449. *(__IO uint32_t *)pAddress = *pSrcBuffer;
  450. pSrcBuffer++;
  451. pAddress++;
  452. }
  453. /* Update the SRAM controller state */
  454. hsram->State = HAL_SRAM_STATE_READY;
  455. /* Process unlocked */
  456. __HAL_UNLOCK(hsram);
  457. return HAL_OK;
  458. }
  459. /**
  460. * @brief Reads a Words data from the SRAM memory using DMA transfer.
  461. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  462. * the configuration information for SRAM module.
  463. * @param pAddress Pointer to read start address
  464. * @param pDstBuffer Pointer to destination buffer
  465. * @param BufferSize Size of the buffer to read from memory
  466. * @retval HAL status
  467. */
  468. HAL_StatusTypeDef HAL_SRAM_Read_DMA(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pDstBuffer, uint32_t BufferSize)
  469. {
  470. /* Process Locked */
  471. __HAL_LOCK(hsram);
  472. /* Update the SRAM controller state */
  473. hsram->State = HAL_SRAM_STATE_BUSY;
  474. /* Configure DMA user callbacks */
  475. hsram->hdma->XferCpltCallback = HAL_SRAM_DMA_XferCpltCallback;
  476. hsram->hdma->XferErrorCallback = HAL_SRAM_DMA_XferErrorCallback;
  477. /* Enable the DMA Stream */
  478. HAL_DMA_Start_IT(hsram->hdma, (uint32_t)pAddress, (uint32_t)pDstBuffer, (uint32_t)BufferSize);
  479. /* Update the SRAM controller state */
  480. hsram->State = HAL_SRAM_STATE_READY;
  481. /* Process unlocked */
  482. __HAL_UNLOCK(hsram);
  483. return HAL_OK;
  484. }
  485. /**
  486. * @brief Writes a Words data buffer to SRAM memory using DMA transfer.
  487. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  488. * the configuration information for SRAM module.
  489. * @param pAddress Pointer to write start address
  490. * @param pSrcBuffer Pointer to source buffer to write
  491. * @param BufferSize Size of the buffer to write to memory
  492. * @retval HAL status
  493. */
  494. HAL_StatusTypeDef HAL_SRAM_Write_DMA(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pSrcBuffer, uint32_t BufferSize)
  495. {
  496. /* Check the SRAM controller state */
  497. if(hsram->State == HAL_SRAM_STATE_PROTECTED)
  498. {
  499. return HAL_ERROR;
  500. }
  501. /* Process Locked */
  502. __HAL_LOCK(hsram);
  503. /* Update the SRAM controller state */
  504. hsram->State = HAL_SRAM_STATE_BUSY;
  505. /* Configure DMA user callbacks */
  506. hsram->hdma->XferCpltCallback = HAL_SRAM_DMA_XferCpltCallback;
  507. hsram->hdma->XferErrorCallback = HAL_SRAM_DMA_XferErrorCallback;
  508. /* Enable the DMA Stream */
  509. HAL_DMA_Start_IT(hsram->hdma, (uint32_t)pSrcBuffer, (uint32_t)pAddress, (uint32_t)BufferSize);
  510. /* Update the SRAM controller state */
  511. hsram->State = HAL_SRAM_STATE_READY;
  512. /* Process unlocked */
  513. __HAL_UNLOCK(hsram);
  514. return HAL_OK;
  515. }
  516. #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
  517. /**
  518. * @brief Register a User SRAM Callback
  519. * To be used instead of the weak (surcharged) predefined callback
  520. * @param hsram : SRAM handle
  521. * @param CallbackId : ID of the callback to be registered
  522. * This parameter can be one of the following values:
  523. * @arg @ref HAL_SRAM_MSP_INIT_CB_ID SRAM MspInit callback ID
  524. * @arg @ref HAL_SRAM_MSP_DEINIT_CB_ID SRAM MspDeInit callback ID
  525. * @param pCallback : pointer to the Callback function
  526. * @retval status
  527. */
  528. HAL_StatusTypeDef HAL_SRAM_RegisterCallback (SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId, pSRAM_CallbackTypeDef pCallback)
  529. {
  530. HAL_StatusTypeDef status = HAL_OK;
  531. HAL_SRAM_StateTypeDef state;
  532. if(pCallback == NULL)
  533. {
  534. return HAL_ERROR;
  535. }
  536. /* Process locked */
  537. __HAL_LOCK(hsram);
  538. state = hsram->State;
  539. if((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_RESET) || (state == HAL_SRAM_STATE_PROTECTED))
  540. {
  541. switch (CallbackId)
  542. {
  543. case HAL_SRAM_MSP_INIT_CB_ID :
  544. hsram->MspInitCallback = pCallback;
  545. break;
  546. case HAL_SRAM_MSP_DEINIT_CB_ID :
  547. hsram->MspDeInitCallback = pCallback;
  548. break;
  549. default :
  550. /* update return status */
  551. status = HAL_ERROR;
  552. break;
  553. }
  554. }
  555. else
  556. {
  557. /* update return status */
  558. status = HAL_ERROR;
  559. }
  560. /* Release Lock */
  561. __HAL_UNLOCK(hsram);
  562. return status;
  563. }
  564. /**
  565. * @brief Unregister a User SRAM Callback
  566. * SRAM Callback is redirected to the weak (surcharged) predefined callback
  567. * @param hsram : SRAM handle
  568. * @param CallbackId : ID of the callback to be unregistered
  569. * This parameter can be one of the following values:
  570. * @arg @ref HAL_SRAM_MSP_INIT_CB_ID SRAM MspInit callback ID
  571. * @arg @ref HAL_SRAM_MSP_DEINIT_CB_ID SRAM MspDeInit callback ID
  572. * @arg @ref HAL_SRAM_DMA_XFER_CPLT_CB_ID SRAM DMA Xfer Complete callback ID
  573. * @arg @ref HAL_SRAM_DMA_XFER_ERR_CB_ID SRAM DMA Xfer Error callback ID
  574. * @retval status
  575. */
  576. HAL_StatusTypeDef HAL_SRAM_UnRegisterCallback (SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId)
  577. {
  578. HAL_StatusTypeDef status = HAL_OK;
  579. HAL_SRAM_StateTypeDef state;
  580. /* Process locked */
  581. __HAL_LOCK(hsram);
  582. state = hsram->State;
  583. if((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
  584. {
  585. switch (CallbackId)
  586. {
  587. case HAL_SRAM_MSP_INIT_CB_ID :
  588. hsram->MspInitCallback = HAL_SRAM_MspInit;
  589. break;
  590. case HAL_SRAM_MSP_DEINIT_CB_ID :
  591. hsram->MspDeInitCallback = HAL_SRAM_MspDeInit;
  592. break;
  593. case HAL_SRAM_DMA_XFER_CPLT_CB_ID :
  594. hsram->DmaXferCpltCallback = HAL_SRAM_DMA_XferCpltCallback;
  595. break;
  596. case HAL_SRAM_DMA_XFER_ERR_CB_ID :
  597. hsram->DmaXferErrorCallback = HAL_SRAM_DMA_XferErrorCallback;
  598. break;
  599. default :
  600. /* update return status */
  601. status = HAL_ERROR;
  602. break;
  603. }
  604. }
  605. else if(state == HAL_SRAM_STATE_RESET)
  606. {
  607. switch (CallbackId)
  608. {
  609. case HAL_SRAM_MSP_INIT_CB_ID :
  610. hsram->MspInitCallback = HAL_SRAM_MspInit;
  611. break;
  612. case HAL_SRAM_MSP_DEINIT_CB_ID :
  613. hsram->MspDeInitCallback = HAL_SRAM_MspDeInit;
  614. break;
  615. default :
  616. /* update return status */
  617. status = HAL_ERROR;
  618. break;
  619. }
  620. }
  621. else
  622. {
  623. /* update return status */
  624. status = HAL_ERROR;
  625. }
  626. /* Release Lock */
  627. __HAL_UNLOCK(hsram);
  628. return status;
  629. }
  630. /**
  631. * @brief Register a User SRAM Callback for DMA transfers
  632. * To be used instead of the weak (surcharged) predefined callback
  633. * @param hsram : SRAM handle
  634. * @param CallbackId : ID of the callback to be registered
  635. * This parameter can be one of the following values:
  636. * @arg @ref HAL_SRAM_DMA_XFER_CPLT_CB_ID SRAM DMA Xfer Complete callback ID
  637. * @arg @ref HAL_SRAM_DMA_XFER_ERR_CB_ID SRAM DMA Xfer Error callback ID
  638. * @param pCallback : pointer to the Callback function
  639. * @retval status
  640. */
  641. HAL_StatusTypeDef HAL_SRAM_RegisterDmaCallback(SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId, pSRAM_DmaCallbackTypeDef pCallback)
  642. {
  643. HAL_StatusTypeDef status = HAL_OK;
  644. HAL_SRAM_StateTypeDef state;
  645. if(pCallback == NULL)
  646. {
  647. return HAL_ERROR;
  648. }
  649. /* Process locked */
  650. __HAL_LOCK(hsram);
  651. state = hsram->State;
  652. if((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
  653. {
  654. switch (CallbackId)
  655. {
  656. case HAL_SRAM_DMA_XFER_CPLT_CB_ID :
  657. hsram->DmaXferCpltCallback = pCallback;
  658. break;
  659. case HAL_SRAM_DMA_XFER_ERR_CB_ID :
  660. hsram->DmaXferErrorCallback = pCallback;
  661. break;
  662. default :
  663. /* update return status */
  664. status = HAL_ERROR;
  665. break;
  666. }
  667. }
  668. else
  669. {
  670. /* update return status */
  671. status = HAL_ERROR;
  672. }
  673. /* Release Lock */
  674. __HAL_UNLOCK(hsram);
  675. return status;
  676. }
  677. #endif
  678. /**
  679. * @}
  680. */
  681. /** @defgroup SRAM_Exported_Functions_Group3 Control functions
  682. * @brief management functions
  683. *
  684. @verbatim
  685. ==============================================================================
  686. ##### SRAM Control functions #####
  687. ==============================================================================
  688. [..]
  689. This subsection provides a set of functions allowing to control dynamically
  690. the SRAM interface.
  691. @endverbatim
  692. * @{
  693. */
  694. /**
  695. * @brief Enables dynamically SRAM write operation.
  696. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  697. * the configuration information for SRAM module.
  698. * @retval HAL status
  699. */
  700. HAL_StatusTypeDef HAL_SRAM_WriteOperation_Enable(SRAM_HandleTypeDef *hsram)
  701. {
  702. /* Process Locked */
  703. __HAL_LOCK(hsram);
  704. /* Enable write operation */
  705. FMC_NORSRAM_WriteOperation_Enable(hsram->Instance, hsram->Init.NSBank);
  706. /* Update the SRAM controller state */
  707. hsram->State = HAL_SRAM_STATE_READY;
  708. /* Process unlocked */
  709. __HAL_UNLOCK(hsram);
  710. return HAL_OK;
  711. }
  712. /**
  713. * @brief Disables dynamically SRAM write operation.
  714. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  715. * the configuration information for SRAM module.
  716. * @retval HAL status
  717. */
  718. HAL_StatusTypeDef HAL_SRAM_WriteOperation_Disable(SRAM_HandleTypeDef *hsram)
  719. {
  720. /* Process Locked */
  721. __HAL_LOCK(hsram);
  722. /* Update the SRAM controller state */
  723. hsram->State = HAL_SRAM_STATE_BUSY;
  724. /* Disable write operation */
  725. FMC_NORSRAM_WriteOperation_Disable(hsram->Instance, hsram->Init.NSBank);
  726. /* Update the SRAM controller state */
  727. hsram->State = HAL_SRAM_STATE_PROTECTED;
  728. /* Process unlocked */
  729. __HAL_UNLOCK(hsram);
  730. return HAL_OK;
  731. }
  732. /**
  733. * @}
  734. */
  735. /** @defgroup SRAM_Exported_Functions_Group4 State functions
  736. * @brief Peripheral State functions
  737. *
  738. @verbatim
  739. ==============================================================================
  740. ##### SRAM State functions #####
  741. ==============================================================================
  742. [..]
  743. This subsection permits to get in run-time the status of the SRAM controller
  744. and the data flow.
  745. @endverbatim
  746. * @{
  747. */
  748. /**
  749. * @brief Returns the SRAM controller state
  750. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  751. * the configuration information for SRAM module.
  752. * @retval HAL state
  753. */
  754. HAL_SRAM_StateTypeDef HAL_SRAM_GetState(SRAM_HandleTypeDef *hsram)
  755. {
  756. return hsram->State;
  757. }
  758. /**
  759. * @}
  760. */
  761. /**
  762. * @}
  763. */
  764. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
  765. STM32F429xx || STM32F439xx || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx ||\
  766. STM32F412Vx || STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */
  767. #endif /* HAL_SRAM_MODULE_ENABLED */
  768. /**
  769. * @}
  770. */
  771. /**
  772. * @}
  773. */
  774. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/