stm32l5xx_hal_sram.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. /**
  2. ******************************************************************************
  3. * @file stm32l5xx_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. ******************************************************************************
  10. * @attention
  11. *
  12. * Copyright (c) 2019 STMicroelectronics.
  13. * All rights reserved.
  14. *
  15. * This software is licensed under terms that can be found in the LICENSE file
  16. * in the root directory of this software component.
  17. * If no LICENSE file comes with this software, it is provided AS-IS.
  18. *
  19. ******************************************************************************
  20. @verbatim
  21. ==============================================================================
  22. ##### How to use this driver #####
  23. ==============================================================================
  24. [..]
  25. This driver is a generic layered driver which contains a set of APIs used to
  26. control SRAM memories. It uses the FMC layer functions to interface
  27. with SRAM devices.
  28. The following sequence should be followed to configure the FMC to interface
  29. with SRAM/PSRAM memories:
  30. (#) Declare a SRAM_HandleTypeDef handle structure, for example:
  31. SRAM_HandleTypeDef hsram; and:
  32. (++) Fill the SRAM_HandleTypeDef handle "Init" field with the allowed
  33. values of the structure member.
  34. (++) Fill the SRAM_HandleTypeDef handle "Instance" field with a predefined
  35. base register instance for NOR or SRAM device
  36. (++) Fill the SRAM_HandleTypeDef handle "Extended" field with a predefined
  37. base register instance for NOR or SRAM extended mode
  38. (#) Declare two FMC_NORSRAM_TimingTypeDef structures, for both normal and extended
  39. mode timings; for example:
  40. FMC_NORSRAM_TimingTypeDef Timing and FMC_NORSRAM_TimingTypeDef ExTiming;
  41. and fill its fields with the allowed values of the structure member.
  42. (#) Initialize the SRAM Controller by calling the function HAL_SRAM_Init(). This function
  43. performs the following sequence:
  44. (##) MSP hardware layer configuration using the function HAL_SRAM_MspInit()
  45. (##) Control register configuration using the FMC NORSRAM interface function
  46. FMC_NORSRAM_Init()
  47. (##) Timing register configuration using the FMC NORSRAM interface function
  48. FMC_NORSRAM_Timing_Init()
  49. (##) Extended mode Timing register configuration using the FMC NORSRAM interface function
  50. FMC_NORSRAM_Extended_Timing_Init()
  51. (##) Enable the SRAM device using the macro __FMC_NORSRAM_ENABLE()
  52. (#) At this stage you can perform read/write accesses from/to the memory connected
  53. to the NOR/SRAM Bank. You can perform either polling or DMA transfer using the
  54. following APIs:
  55. (++) HAL_SRAM_Read()/HAL_SRAM_Write() for polling read/write access
  56. (++) 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
  59. (#) You can continuously monitor the SRAM device HAL state by calling the function
  60. HAL_SRAM_GetState()
  61. *** Callback registration ***
  62. =============================================
  63. [..]
  64. The compilation define USE_HAL_SRAM_REGISTER_CALLBACKS when set to 1
  65. allows the user to configure dynamically the driver callbacks.
  66. Use Functions HAL_SRAM_RegisterCallback() to register a user callback,
  67. it allows to register following callbacks:
  68. (+) MspInitCallback : SRAM MspInit.
  69. (+) MspDeInitCallback : SRAM MspDeInit.
  70. This function takes as parameters the HAL peripheral handle, the Callback ID
  71. and a pointer to the user callback function.
  72. Use function HAL_SRAM_UnRegisterCallback() to reset a callback to the default
  73. weak (surcharged) function. It allows to reset following callbacks:
  74. (+) MspInitCallback : SRAM MspInit.
  75. (+) MspDeInitCallback : SRAM MspDeInit.
  76. This function) takes as parameters the HAL peripheral handle and the Callback ID.
  77. By default, after the HAL_SRAM_Init and if the state is HAL_SRAM_STATE_RESET
  78. all callbacks are reset to the corresponding legacy weak (surcharged) functions.
  79. Exception done for MspInit and MspDeInit callbacks that are respectively
  80. reset to the legacy weak (surcharged) functions in the HAL_SRAM_Init
  81. and HAL_SRAM_DeInit only when these callbacks are null (not registered beforehand).
  82. If not, MspInit or MspDeInit are not null, the HAL_SRAM_Init and HAL_SRAM_DeInit
  83. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  84. Callbacks can be registered/unregistered in READY state only.
  85. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  86. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  87. during the Init/DeInit.
  88. In that case first register the MspInit/MspDeInit user callbacks
  89. using HAL_SRAM_RegisterCallback before calling HAL_SRAM_DeInit
  90. or HAL_SRAM_Init function.
  91. When The compilation define USE_HAL_SRAM_REGISTER_CALLBACKS is set to 0 or
  92. not defined, the callback registering feature is not available
  93. and weak (surcharged) callbacks are used.
  94. @endverbatim
  95. ******************************************************************************
  96. */
  97. /* Includes ------------------------------------------------------------------*/
  98. #include "stm32l5xx_hal.h"
  99. /** @addtogroup STM32L5xx_HAL_Driver
  100. * @{
  101. */
  102. #ifdef HAL_SRAM_MODULE_ENABLED
  103. /** @defgroup SRAM SRAM
  104. * @brief SRAM driver modules
  105. * @{
  106. */
  107. /* Private typedef -----------------------------------------------------------*/
  108. /* Private define ------------------------------------------------------------*/
  109. /* Private macro -------------------------------------------------------------*/
  110. /* Private variables ---------------------------------------------------------*/
  111. /* Private function prototypes -----------------------------------------------*/
  112. /** @addtogroup SRAM_Private_Functions SRAM Private Functions
  113. * @{
  114. */
  115. static void SRAM_DMACplt(DMA_HandleTypeDef *hdma);
  116. static void SRAM_DMACpltProt(DMA_HandleTypeDef *hdma);
  117. static void SRAM_DMAError(DMA_HandleTypeDef *hdma);
  118. /**
  119. * @}
  120. */
  121. /* Exported functions --------------------------------------------------------*/
  122. /** @defgroup SRAM_Exported_Functions SRAM Exported Functions
  123. * @{
  124. */
  125. /** @defgroup SRAM_Exported_Functions_Group1 Initialization and de-initialization functions
  126. * @brief Initialization and Configuration functions.
  127. *
  128. @verbatim
  129. ==============================================================================
  130. ##### SRAM Initialization and de_initialization functions #####
  131. ==============================================================================
  132. [..] This section provides functions allowing to initialize/de-initialize
  133. the SRAM memory
  134. @endverbatim
  135. * @{
  136. */
  137. /**
  138. * @brief Performs the SRAM device initialization sequence
  139. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  140. * the configuration information for SRAM module.
  141. * @param Timing Pointer to SRAM control timing structure
  142. * @param ExtTiming Pointer to SRAM extended mode timing structure
  143. * @retval HAL status
  144. */
  145. HAL_StatusTypeDef HAL_SRAM_Init(SRAM_HandleTypeDef *hsram, FMC_NORSRAM_TimingTypeDef *Timing,
  146. FMC_NORSRAM_TimingTypeDef *ExtTiming)
  147. {
  148. /* Check the SRAM handle parameter */
  149. if (hsram == NULL)
  150. {
  151. return HAL_ERROR;
  152. }
  153. if (hsram->State == HAL_SRAM_STATE_RESET)
  154. {
  155. /* Allocate lock resource and initialize it */
  156. hsram->Lock = HAL_UNLOCKED;
  157. #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
  158. if (hsram->MspInitCallback == NULL)
  159. {
  160. hsram->MspInitCallback = HAL_SRAM_MspInit;
  161. }
  162. hsram->DmaXferCpltCallback = HAL_SRAM_DMA_XferCpltCallback;
  163. hsram->DmaXferErrorCallback = HAL_SRAM_DMA_XferErrorCallback;
  164. /* Init the low level hardware */
  165. hsram->MspInitCallback(hsram);
  166. #else
  167. /* Initialize the low level hardware (MSP) */
  168. HAL_SRAM_MspInit(hsram);
  169. #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
  170. }
  171. /* Initialize SRAM control Interface */
  172. (void)FMC_NORSRAM_Init(hsram->Instance, &(hsram->Init));
  173. /* Initialize SRAM timing Interface */
  174. (void)FMC_NORSRAM_Timing_Init(hsram->Instance, Timing, hsram->Init.NSBank);
  175. /* Initialize SRAM extended mode timing Interface */
  176. (void)FMC_NORSRAM_Extended_Timing_Init(hsram->Extended, ExtTiming, hsram->Init.NSBank,
  177. hsram->Init.ExtendedMode);
  178. /* Enable the NORSRAM device */
  179. __FMC_NORSRAM_ENABLE(hsram->Instance, hsram->Init.NSBank);
  180. /* Initialize the SRAM controller state */
  181. hsram->State = HAL_SRAM_STATE_READY;
  182. return HAL_OK;
  183. }
  184. /**
  185. * @brief Performs the SRAM device De-initialization sequence.
  186. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  187. * the configuration information for SRAM module.
  188. * @retval HAL status
  189. */
  190. HAL_StatusTypeDef HAL_SRAM_DeInit(SRAM_HandleTypeDef *hsram)
  191. {
  192. #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
  193. if (hsram->MspDeInitCallback == NULL)
  194. {
  195. hsram->MspDeInitCallback = HAL_SRAM_MspDeInit;
  196. }
  197. /* DeInit the low level hardware */
  198. hsram->MspDeInitCallback(hsram);
  199. #else
  200. /* De-Initialize the low level hardware (MSP) */
  201. HAL_SRAM_MspDeInit(hsram);
  202. #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
  203. /* Configure the SRAM registers with their reset values */
  204. (void)FMC_NORSRAM_DeInit(hsram->Instance, hsram->Extended, hsram->Init.NSBank);
  205. /* Reset the SRAM controller state */
  206. hsram->State = HAL_SRAM_STATE_RESET;
  207. /* Release Lock */
  208. __HAL_UNLOCK(hsram);
  209. return HAL_OK;
  210. }
  211. /**
  212. * @brief SRAM MSP Init.
  213. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  214. * the configuration information for SRAM module.
  215. * @retval None
  216. */
  217. __weak void HAL_SRAM_MspInit(SRAM_HandleTypeDef *hsram)
  218. {
  219. /* Prevent unused argument(s) compilation warning */
  220. UNUSED(hsram);
  221. /* NOTE : This function Should not be modified, when the callback is needed,
  222. the HAL_SRAM_MspInit could be implemented in the user file
  223. */
  224. }
  225. /**
  226. * @brief SRAM MSP DeInit.
  227. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  228. * the configuration information for SRAM module.
  229. * @retval None
  230. */
  231. __weak void HAL_SRAM_MspDeInit(SRAM_HandleTypeDef *hsram)
  232. {
  233. /* Prevent unused argument(s) compilation warning */
  234. UNUSED(hsram);
  235. /* NOTE : This function Should not be modified, when the callback is needed,
  236. the HAL_SRAM_MspDeInit could be implemented in the user file
  237. */
  238. }
  239. /**
  240. * @brief DMA transfer complete callback.
  241. * @param hdma pointer to a SRAM_HandleTypeDef structure that contains
  242. * the configuration information for SRAM module.
  243. * @retval None
  244. */
  245. __weak void HAL_SRAM_DMA_XferCpltCallback(DMA_HandleTypeDef *hdma)
  246. {
  247. /* Prevent unused argument(s) compilation warning */
  248. UNUSED(hdma);
  249. /* NOTE : This function Should not be modified, when the callback is needed,
  250. the HAL_SRAM_DMA_XferCpltCallback could be implemented in the user file
  251. */
  252. }
  253. /**
  254. * @brief DMA transfer complete error callback.
  255. * @param hdma pointer to a SRAM_HandleTypeDef structure that contains
  256. * the configuration information for SRAM module.
  257. * @retval None
  258. */
  259. __weak void HAL_SRAM_DMA_XferErrorCallback(DMA_HandleTypeDef *hdma)
  260. {
  261. /* Prevent unused argument(s) compilation warning */
  262. UNUSED(hdma);
  263. /* NOTE : This function Should not be modified, when the callback is needed,
  264. the HAL_SRAM_DMA_XferErrorCallback could be implemented in the user file
  265. */
  266. }
  267. /**
  268. * @}
  269. */
  270. /** @defgroup SRAM_Exported_Functions_Group2 Input Output and memory control functions
  271. * @brief Input Output and memory control functions
  272. *
  273. @verbatim
  274. ==============================================================================
  275. ##### SRAM Input and Output functions #####
  276. ==============================================================================
  277. [..]
  278. This section provides functions allowing to use and control the SRAM memory
  279. @endverbatim
  280. * @{
  281. */
  282. /**
  283. * @brief Reads 8-bit buffer from SRAM memory.
  284. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  285. * the configuration information for SRAM module.
  286. * @param pAddress Pointer to read start address
  287. * @param pDstBuffer Pointer to destination buffer
  288. * @param BufferSize Size of the buffer to read from memory
  289. * @retval HAL status
  290. */
  291. HAL_StatusTypeDef HAL_SRAM_Read_8b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint8_t *pDstBuffer,
  292. uint32_t BufferSize)
  293. {
  294. uint32_t size;
  295. __IO uint8_t *psramaddress = (uint8_t *)pAddress;
  296. uint8_t *pdestbuff = pDstBuffer;
  297. HAL_SRAM_StateTypeDef state = hsram->State;
  298. /* Check the SRAM controller state */
  299. if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
  300. {
  301. /* Process Locked */
  302. __HAL_LOCK(hsram);
  303. /* Update the SRAM controller state */
  304. hsram->State = HAL_SRAM_STATE_BUSY;
  305. /* Read data from memory */
  306. for (size = BufferSize; size != 0U; size--)
  307. {
  308. *pdestbuff = *psramaddress;
  309. pdestbuff++;
  310. psramaddress++;
  311. }
  312. /* Update the SRAM controller state */
  313. hsram->State = state;
  314. /* Process unlocked */
  315. __HAL_UNLOCK(hsram);
  316. }
  317. else
  318. {
  319. return HAL_ERROR;
  320. }
  321. return HAL_OK;
  322. }
  323. /**
  324. * @brief Writes 8-bit buffer to SRAM memory.
  325. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  326. * the configuration information for SRAM module.
  327. * @param pAddress Pointer to write start address
  328. * @param pSrcBuffer Pointer to source buffer to write
  329. * @param BufferSize Size of the buffer to write to memory
  330. * @retval HAL status
  331. */
  332. HAL_StatusTypeDef HAL_SRAM_Write_8b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint8_t *pSrcBuffer,
  333. uint32_t BufferSize)
  334. {
  335. uint32_t size;
  336. __IO uint8_t *psramaddress = (uint8_t *)pAddress;
  337. uint8_t *psrcbuff = pSrcBuffer;
  338. /* Check the SRAM controller state */
  339. if (hsram->State == HAL_SRAM_STATE_READY)
  340. {
  341. /* Process Locked */
  342. __HAL_LOCK(hsram);
  343. /* Update the SRAM controller state */
  344. hsram->State = HAL_SRAM_STATE_BUSY;
  345. /* Write data to memory */
  346. for (size = BufferSize; size != 0U; size--)
  347. {
  348. *psramaddress = *psrcbuff;
  349. psrcbuff++;
  350. psramaddress++;
  351. }
  352. /* Update the SRAM controller state */
  353. hsram->State = HAL_SRAM_STATE_READY;
  354. /* Process unlocked */
  355. __HAL_UNLOCK(hsram);
  356. }
  357. else
  358. {
  359. return HAL_ERROR;
  360. }
  361. return HAL_OK;
  362. }
  363. /**
  364. * @brief Reads 16-bit buffer from SRAM memory.
  365. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  366. * the configuration information for SRAM module.
  367. * @param pAddress Pointer to read start address
  368. * @param pDstBuffer Pointer to destination buffer
  369. * @param BufferSize Size of the buffer to read from memory
  370. * @retval HAL status
  371. */
  372. HAL_StatusTypeDef HAL_SRAM_Read_16b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint16_t *pDstBuffer,
  373. uint32_t BufferSize)
  374. {
  375. uint32_t size;
  376. __IO uint32_t *psramaddress = pAddress;
  377. uint16_t *pdestbuff = pDstBuffer;
  378. uint8_t limit;
  379. HAL_SRAM_StateTypeDef state = hsram->State;
  380. /* Check the SRAM controller state */
  381. if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
  382. {
  383. /* Process Locked */
  384. __HAL_LOCK(hsram);
  385. /* Update the SRAM controller state */
  386. hsram->State = HAL_SRAM_STATE_BUSY;
  387. /* Check if the size is a 32-bits multiple */
  388. limit = (((BufferSize % 2U) != 0U) ? 1U : 0U);
  389. /* Read data from memory */
  390. for (size = BufferSize; size != limit; size -= 2U)
  391. {
  392. *pdestbuff = (uint16_t)((*psramaddress) & 0x0000FFFFU);
  393. pdestbuff++;
  394. *pdestbuff = (uint16_t)(((*psramaddress) & 0xFFFF0000U) >> 16U);
  395. pdestbuff++;
  396. psramaddress++;
  397. }
  398. /* Read last 16-bits if size is not 32-bits multiple */
  399. if (limit != 0U)
  400. {
  401. *pdestbuff = (uint16_t)((*psramaddress) & 0x0000FFFFU);
  402. }
  403. /* Update the SRAM controller state */
  404. hsram->State = state;
  405. /* Process unlocked */
  406. __HAL_UNLOCK(hsram);
  407. }
  408. else
  409. {
  410. return HAL_ERROR;
  411. }
  412. return HAL_OK;
  413. }
  414. /**
  415. * @brief Writes 16-bit buffer to SRAM memory.
  416. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  417. * the configuration information for SRAM module.
  418. * @param pAddress Pointer to write start address
  419. * @param pSrcBuffer Pointer to source buffer to write
  420. * @param BufferSize Size of the buffer to write to memory
  421. * @retval HAL status
  422. */
  423. HAL_StatusTypeDef HAL_SRAM_Write_16b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint16_t *pSrcBuffer,
  424. uint32_t BufferSize)
  425. {
  426. uint32_t size;
  427. __IO uint32_t *psramaddress = pAddress;
  428. uint16_t *psrcbuff = pSrcBuffer;
  429. uint8_t limit;
  430. /* Check the SRAM controller state */
  431. if (hsram->State == HAL_SRAM_STATE_READY)
  432. {
  433. /* Process Locked */
  434. __HAL_LOCK(hsram);
  435. /* Update the SRAM controller state */
  436. hsram->State = HAL_SRAM_STATE_BUSY;
  437. /* Check if the size is a 32-bits multiple */
  438. limit = (((BufferSize % 2U) != 0U) ? 1U : 0U);
  439. /* Write data to memory */
  440. for (size = BufferSize; size != limit; size -= 2U)
  441. {
  442. *psramaddress = (uint32_t)(*psrcbuff);
  443. psrcbuff++;
  444. *psramaddress |= ((uint32_t)(*psrcbuff) << 16U);
  445. psrcbuff++;
  446. psramaddress++;
  447. }
  448. /* Write last 16-bits if size is not 32-bits multiple */
  449. if (limit != 0U)
  450. {
  451. *psramaddress = ((uint32_t)(*psrcbuff) & 0x0000FFFFU) | ((*psramaddress) & 0xFFFF0000U);
  452. }
  453. /* Update the SRAM controller state */
  454. hsram->State = HAL_SRAM_STATE_READY;
  455. /* Process unlocked */
  456. __HAL_UNLOCK(hsram);
  457. }
  458. else
  459. {
  460. return HAL_ERROR;
  461. }
  462. return HAL_OK;
  463. }
  464. /**
  465. * @brief Reads 32-bit buffer from 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 read start address
  469. * @param pDstBuffer Pointer to destination buffer
  470. * @param BufferSize Size of the buffer to read from memory
  471. * @retval HAL status
  472. */
  473. HAL_StatusTypeDef HAL_SRAM_Read_32b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pDstBuffer,
  474. uint32_t BufferSize)
  475. {
  476. uint32_t size;
  477. __IO uint32_t *psramaddress = pAddress;
  478. uint32_t *pdestbuff = pDstBuffer;
  479. HAL_SRAM_StateTypeDef state = hsram->State;
  480. /* Check the SRAM controller state */
  481. if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
  482. {
  483. /* Process Locked */
  484. __HAL_LOCK(hsram);
  485. /* Update the SRAM controller state */
  486. hsram->State = HAL_SRAM_STATE_BUSY;
  487. /* Read data from memory */
  488. for (size = BufferSize; size != 0U; size--)
  489. {
  490. *pdestbuff = *psramaddress;
  491. pdestbuff++;
  492. psramaddress++;
  493. }
  494. /* Update the SRAM controller state */
  495. hsram->State = state;
  496. /* Process unlocked */
  497. __HAL_UNLOCK(hsram);
  498. }
  499. else
  500. {
  501. return HAL_ERROR;
  502. }
  503. return HAL_OK;
  504. }
  505. /**
  506. * @brief Writes 32-bit buffer to SRAM memory.
  507. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  508. * the configuration information for SRAM module.
  509. * @param pAddress Pointer to write start address
  510. * @param pSrcBuffer Pointer to source buffer to write
  511. * @param BufferSize Size of the buffer to write to memory
  512. * @retval HAL status
  513. */
  514. HAL_StatusTypeDef HAL_SRAM_Write_32b(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pSrcBuffer,
  515. uint32_t BufferSize)
  516. {
  517. uint32_t size;
  518. __IO uint32_t *psramaddress = pAddress;
  519. uint32_t *psrcbuff = pSrcBuffer;
  520. /* Check the SRAM controller state */
  521. if (hsram->State == HAL_SRAM_STATE_READY)
  522. {
  523. /* Process Locked */
  524. __HAL_LOCK(hsram);
  525. /* Update the SRAM controller state */
  526. hsram->State = HAL_SRAM_STATE_BUSY;
  527. /* Write data to memory */
  528. for (size = BufferSize; size != 0U; size--)
  529. {
  530. *psramaddress = *psrcbuff;
  531. psrcbuff++;
  532. psramaddress++;
  533. }
  534. /* Update the SRAM controller state */
  535. hsram->State = HAL_SRAM_STATE_READY;
  536. /* Process unlocked */
  537. __HAL_UNLOCK(hsram);
  538. }
  539. else
  540. {
  541. return HAL_ERROR;
  542. }
  543. return HAL_OK;
  544. }
  545. /**
  546. * @brief Reads a Words data from the SRAM memory using DMA transfer.
  547. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  548. * the configuration information for SRAM module.
  549. * @param pAddress Pointer to read start address
  550. * @param pDstBuffer Pointer to destination buffer
  551. * @param BufferSize Size of the buffer to read from memory
  552. * @retval HAL status
  553. */
  554. HAL_StatusTypeDef HAL_SRAM_Read_DMA(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pDstBuffer,
  555. uint32_t BufferSize)
  556. {
  557. HAL_StatusTypeDef status;
  558. HAL_SRAM_StateTypeDef state = hsram->State;
  559. /* Check the SRAM controller state */
  560. if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
  561. {
  562. /* Process Locked */
  563. __HAL_LOCK(hsram);
  564. /* Update the SRAM controller state */
  565. hsram->State = HAL_SRAM_STATE_BUSY;
  566. /* Configure DMA user callbacks */
  567. if (state == HAL_SRAM_STATE_READY)
  568. {
  569. hsram->hdma->XferCpltCallback = SRAM_DMACplt;
  570. }
  571. else
  572. {
  573. hsram->hdma->XferCpltCallback = SRAM_DMACpltProt;
  574. }
  575. hsram->hdma->XferErrorCallback = SRAM_DMAError;
  576. /* Enable the DMA Stream */
  577. status = HAL_DMA_Start_IT(hsram->hdma, (uint32_t)pAddress, (uint32_t)pDstBuffer, (uint32_t)BufferSize);
  578. /* Process unlocked */
  579. __HAL_UNLOCK(hsram);
  580. }
  581. else
  582. {
  583. status = HAL_ERROR;
  584. }
  585. return status;
  586. }
  587. /**
  588. * @brief Writes a Words data buffer to SRAM memory using DMA transfer.
  589. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  590. * the configuration information for SRAM module.
  591. * @param pAddress Pointer to write start address
  592. * @param pSrcBuffer Pointer to source buffer to write
  593. * @param BufferSize Size of the buffer to write to memory
  594. * @retval HAL status
  595. */
  596. HAL_StatusTypeDef HAL_SRAM_Write_DMA(SRAM_HandleTypeDef *hsram, uint32_t *pAddress, uint32_t *pSrcBuffer,
  597. uint32_t BufferSize)
  598. {
  599. HAL_StatusTypeDef status;
  600. /* Check the SRAM controller state */
  601. if (hsram->State == HAL_SRAM_STATE_READY)
  602. {
  603. /* Process Locked */
  604. __HAL_LOCK(hsram);
  605. /* Update the SRAM controller state */
  606. hsram->State = HAL_SRAM_STATE_BUSY;
  607. /* Configure DMA user callbacks */
  608. hsram->hdma->XferCpltCallback = SRAM_DMACplt;
  609. hsram->hdma->XferErrorCallback = SRAM_DMAError;
  610. /* Enable the DMA Stream */
  611. status = HAL_DMA_Start_IT(hsram->hdma, (uint32_t)pSrcBuffer, (uint32_t)pAddress, (uint32_t)BufferSize);
  612. /* Process unlocked */
  613. __HAL_UNLOCK(hsram);
  614. }
  615. else
  616. {
  617. status = HAL_ERROR;
  618. }
  619. return status;
  620. }
  621. #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
  622. /**
  623. * @brief Register a User SRAM Callback
  624. * To be used instead of the weak (surcharged) predefined callback
  625. * @param hsram : SRAM handle
  626. * @param CallbackId : ID of the callback to be registered
  627. * This parameter can be one of the following values:
  628. * @arg @ref HAL_SRAM_MSP_INIT_CB_ID SRAM MspInit callback ID
  629. * @arg @ref HAL_SRAM_MSP_DEINIT_CB_ID SRAM MspDeInit callback ID
  630. * @param pCallback : pointer to the Callback function
  631. * @retval status
  632. */
  633. HAL_StatusTypeDef HAL_SRAM_RegisterCallback(SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId,
  634. pSRAM_CallbackTypeDef pCallback)
  635. {
  636. HAL_StatusTypeDef status = HAL_OK;
  637. HAL_SRAM_StateTypeDef state;
  638. if (pCallback == NULL)
  639. {
  640. return HAL_ERROR;
  641. }
  642. /* Process locked */
  643. __HAL_LOCK(hsram);
  644. state = hsram->State;
  645. if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_RESET) || (state == HAL_SRAM_STATE_PROTECTED))
  646. {
  647. switch (CallbackId)
  648. {
  649. case HAL_SRAM_MSP_INIT_CB_ID :
  650. hsram->MspInitCallback = pCallback;
  651. break;
  652. case HAL_SRAM_MSP_DEINIT_CB_ID :
  653. hsram->MspDeInitCallback = pCallback;
  654. break;
  655. default :
  656. /* update return status */
  657. status = HAL_ERROR;
  658. break;
  659. }
  660. }
  661. else
  662. {
  663. /* update return status */
  664. status = HAL_ERROR;
  665. }
  666. /* Release Lock */
  667. __HAL_UNLOCK(hsram);
  668. return status;
  669. }
  670. /**
  671. * @brief Unregister a User SRAM Callback
  672. * SRAM Callback is redirected to the weak (surcharged) predefined callback
  673. * @param hsram : SRAM handle
  674. * @param CallbackId : ID of the callback to be unregistered
  675. * This parameter can be one of the following values:
  676. * @arg @ref HAL_SRAM_MSP_INIT_CB_ID SRAM MspInit callback ID
  677. * @arg @ref HAL_SRAM_MSP_DEINIT_CB_ID SRAM MspDeInit callback ID
  678. * @arg @ref HAL_SRAM_DMA_XFER_CPLT_CB_ID SRAM DMA Xfer Complete callback ID
  679. * @arg @ref HAL_SRAM_DMA_XFER_ERR_CB_ID SRAM DMA Xfer Error callback ID
  680. * @retval status
  681. */
  682. HAL_StatusTypeDef HAL_SRAM_UnRegisterCallback(SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId)
  683. {
  684. HAL_StatusTypeDef status = HAL_OK;
  685. HAL_SRAM_StateTypeDef state;
  686. /* Process locked */
  687. __HAL_LOCK(hsram);
  688. state = hsram->State;
  689. if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
  690. {
  691. switch (CallbackId)
  692. {
  693. case HAL_SRAM_MSP_INIT_CB_ID :
  694. hsram->MspInitCallback = HAL_SRAM_MspInit;
  695. break;
  696. case HAL_SRAM_MSP_DEINIT_CB_ID :
  697. hsram->MspDeInitCallback = HAL_SRAM_MspDeInit;
  698. break;
  699. case HAL_SRAM_DMA_XFER_CPLT_CB_ID :
  700. hsram->DmaXferCpltCallback = HAL_SRAM_DMA_XferCpltCallback;
  701. break;
  702. case HAL_SRAM_DMA_XFER_ERR_CB_ID :
  703. hsram->DmaXferErrorCallback = HAL_SRAM_DMA_XferErrorCallback;
  704. break;
  705. default :
  706. /* update return status */
  707. status = HAL_ERROR;
  708. break;
  709. }
  710. }
  711. else if (state == HAL_SRAM_STATE_RESET)
  712. {
  713. switch (CallbackId)
  714. {
  715. case HAL_SRAM_MSP_INIT_CB_ID :
  716. hsram->MspInitCallback = HAL_SRAM_MspInit;
  717. break;
  718. case HAL_SRAM_MSP_DEINIT_CB_ID :
  719. hsram->MspDeInitCallback = HAL_SRAM_MspDeInit;
  720. break;
  721. default :
  722. /* update return status */
  723. status = HAL_ERROR;
  724. break;
  725. }
  726. }
  727. else
  728. {
  729. /* update return status */
  730. status = HAL_ERROR;
  731. }
  732. /* Release Lock */
  733. __HAL_UNLOCK(hsram);
  734. return status;
  735. }
  736. /**
  737. * @brief Register a User SRAM Callback for DMA transfers
  738. * To be used instead of the weak (surcharged) predefined callback
  739. * @param hsram : SRAM handle
  740. * @param CallbackId : ID of the callback to be registered
  741. * This parameter can be one of the following values:
  742. * @arg @ref HAL_SRAM_DMA_XFER_CPLT_CB_ID SRAM DMA Xfer Complete callback ID
  743. * @arg @ref HAL_SRAM_DMA_XFER_ERR_CB_ID SRAM DMA Xfer Error callback ID
  744. * @param pCallback : pointer to the Callback function
  745. * @retval status
  746. */
  747. HAL_StatusTypeDef HAL_SRAM_RegisterDmaCallback(SRAM_HandleTypeDef *hsram, HAL_SRAM_CallbackIDTypeDef CallbackId,
  748. pSRAM_DmaCallbackTypeDef pCallback)
  749. {
  750. HAL_StatusTypeDef status = HAL_OK;
  751. HAL_SRAM_StateTypeDef state;
  752. if (pCallback == NULL)
  753. {
  754. return HAL_ERROR;
  755. }
  756. /* Process locked */
  757. __HAL_LOCK(hsram);
  758. state = hsram->State;
  759. if ((state == HAL_SRAM_STATE_READY) || (state == HAL_SRAM_STATE_PROTECTED))
  760. {
  761. switch (CallbackId)
  762. {
  763. case HAL_SRAM_DMA_XFER_CPLT_CB_ID :
  764. hsram->DmaXferCpltCallback = pCallback;
  765. break;
  766. case HAL_SRAM_DMA_XFER_ERR_CB_ID :
  767. hsram->DmaXferErrorCallback = pCallback;
  768. break;
  769. default :
  770. /* update return status */
  771. status = HAL_ERROR;
  772. break;
  773. }
  774. }
  775. else
  776. {
  777. /* update return status */
  778. status = HAL_ERROR;
  779. }
  780. /* Release Lock */
  781. __HAL_UNLOCK(hsram);
  782. return status;
  783. }
  784. #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
  785. /**
  786. * @}
  787. */
  788. /** @defgroup SRAM_Exported_Functions_Group3 Control functions
  789. * @brief Control functions
  790. *
  791. @verbatim
  792. ==============================================================================
  793. ##### SRAM Control functions #####
  794. ==============================================================================
  795. [..]
  796. This subsection provides a set of functions allowing to control dynamically
  797. the SRAM interface.
  798. @endverbatim
  799. * @{
  800. */
  801. /**
  802. * @brief Enables dynamically SRAM write operation.
  803. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  804. * the configuration information for SRAM module.
  805. * @retval HAL status
  806. */
  807. HAL_StatusTypeDef HAL_SRAM_WriteOperation_Enable(SRAM_HandleTypeDef *hsram)
  808. {
  809. /* Check the SRAM controller state */
  810. if (hsram->State == HAL_SRAM_STATE_PROTECTED)
  811. {
  812. /* Process Locked */
  813. __HAL_LOCK(hsram);
  814. /* Update the SRAM controller state */
  815. hsram->State = HAL_SRAM_STATE_BUSY;
  816. /* Enable write operation */
  817. (void)FMC_NORSRAM_WriteOperation_Enable(hsram->Instance, hsram->Init.NSBank);
  818. /* Update the SRAM controller state */
  819. hsram->State = HAL_SRAM_STATE_READY;
  820. /* Process unlocked */
  821. __HAL_UNLOCK(hsram);
  822. }
  823. else
  824. {
  825. return HAL_ERROR;
  826. }
  827. return HAL_OK;
  828. }
  829. /**
  830. * @brief Disables dynamically SRAM write operation.
  831. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  832. * the configuration information for SRAM module.
  833. * @retval HAL status
  834. */
  835. HAL_StatusTypeDef HAL_SRAM_WriteOperation_Disable(SRAM_HandleTypeDef *hsram)
  836. {
  837. /* Check the SRAM controller state */
  838. if (hsram->State == HAL_SRAM_STATE_READY)
  839. {
  840. /* Process Locked */
  841. __HAL_LOCK(hsram);
  842. /* Update the SRAM controller state */
  843. hsram->State = HAL_SRAM_STATE_BUSY;
  844. /* Disable write operation */
  845. (void)FMC_NORSRAM_WriteOperation_Disable(hsram->Instance, hsram->Init.NSBank);
  846. /* Update the SRAM controller state */
  847. hsram->State = HAL_SRAM_STATE_PROTECTED;
  848. /* Process unlocked */
  849. __HAL_UNLOCK(hsram);
  850. }
  851. else
  852. {
  853. return HAL_ERROR;
  854. }
  855. return HAL_OK;
  856. }
  857. /**
  858. * @}
  859. */
  860. /** @defgroup SRAM_Exported_Functions_Group4 Peripheral State functions
  861. * @brief Peripheral State functions
  862. *
  863. @verbatim
  864. ==============================================================================
  865. ##### SRAM State functions #####
  866. ==============================================================================
  867. [..]
  868. This subsection permits to get in run-time the status of the SRAM controller
  869. and the data flow.
  870. @endverbatim
  871. * @{
  872. */
  873. /**
  874. * @brief Returns the SRAM controller state
  875. * @param hsram pointer to a SRAM_HandleTypeDef structure that contains
  876. * the configuration information for SRAM module.
  877. * @retval HAL state
  878. */
  879. HAL_SRAM_StateTypeDef HAL_SRAM_GetState(SRAM_HandleTypeDef *hsram)
  880. {
  881. return hsram->State;
  882. }
  883. /**
  884. * @}
  885. */
  886. /**
  887. * @}
  888. */
  889. /** @addtogroup SRAM_Private_Functions SRAM Private Functions
  890. * @{
  891. */
  892. /**
  893. * @brief DMA SRAM process complete callback.
  894. * @param hdma : DMA handle
  895. * @retval None
  896. */
  897. static void SRAM_DMACplt(DMA_HandleTypeDef *hdma)
  898. {
  899. SRAM_HandleTypeDef *hsram = (SRAM_HandleTypeDef *)(hdma->Parent);
  900. /* Disable the DMA channel */
  901. __HAL_DMA_DISABLE(hdma);
  902. /* Update the SRAM controller state */
  903. hsram->State = HAL_SRAM_STATE_READY;
  904. #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
  905. hsram->DmaXferCpltCallback(hdma);
  906. #else
  907. HAL_SRAM_DMA_XferCpltCallback(hdma);
  908. #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
  909. }
  910. /**
  911. * @brief DMA SRAM process complete callback.
  912. * @param hdma : DMA handle
  913. * @retval None
  914. */
  915. static void SRAM_DMACpltProt(DMA_HandleTypeDef *hdma)
  916. {
  917. SRAM_HandleTypeDef *hsram = (SRAM_HandleTypeDef *)(hdma->Parent);
  918. /* Disable the DMA channel */
  919. __HAL_DMA_DISABLE(hdma);
  920. /* Update the SRAM controller state */
  921. hsram->State = HAL_SRAM_STATE_PROTECTED;
  922. #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
  923. hsram->DmaXferCpltCallback(hdma);
  924. #else
  925. HAL_SRAM_DMA_XferCpltCallback(hdma);
  926. #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
  927. }
  928. /**
  929. * @brief DMA SRAM error callback.
  930. * @param hdma : DMA handle
  931. * @retval None
  932. */
  933. static void SRAM_DMAError(DMA_HandleTypeDef *hdma)
  934. {
  935. SRAM_HandleTypeDef *hsram = (SRAM_HandleTypeDef *)(hdma->Parent);
  936. /* Disable the DMA channel */
  937. __HAL_DMA_DISABLE(hdma);
  938. /* Update the SRAM controller state */
  939. hsram->State = HAL_SRAM_STATE_ERROR;
  940. #if (USE_HAL_SRAM_REGISTER_CALLBACKS == 1)
  941. hsram->DmaXferErrorCallback(hdma);
  942. #else
  943. HAL_SRAM_DMA_XferErrorCallback(hdma);
  944. #endif /* USE_HAL_SRAM_REGISTER_CALLBACKS */
  945. }
  946. /**
  947. * @}
  948. */
  949. /**
  950. * @}
  951. */
  952. #endif /* HAL_SRAM_MODULE_ENABLED */
  953. /**
  954. * @}
  955. */