stm32f1xx_hal_sram.c 32 KB

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