stm32f1xx_hal_sram.c 33 KB

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