You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_ll_fsmc.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_ll_fsmc.c
  4. * @author MCD Application Team
  5. * @brief FSMC Low Layer HAL module driver.
  6. *
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the Flexible Static Memory Controller (FSMC) peripheral memories:
  9. * + Initialization/de-initialization functions
  10. * + Peripheral Control functions
  11. * + Peripheral State functions
  12. *
  13. @verbatim
  14. =============================================================================
  15. ##### FSMC peripheral features #####
  16. =============================================================================
  17. [..] The Flexible static memory controller (FSMC) includes following memory controllers:
  18. (+) The NOR/PSRAM memory controller
  19. [..] The FSMC functional block makes the interface with synchronous and asynchronous static
  20. memories. Its main purposes are:
  21. (+) to translate AHB transactions into the appropriate external device protocol.
  22. (+) to meet the access time requirements of the external memory devices.
  23. [..] All external memories share the addresses, data and control signals with the controller.
  24. Each external device is accessed by means of a unique Chip Select. The FSMC performs
  25. only one access at a time to an external device.
  26. The main features of the FSMC controller are the following:
  27. (+) Interface with static-memory mapped devices including:
  28. (++) Static random access memory (SRAM).
  29. (++) NOR Flash memory.
  30. (++) PSRAM (4 memory banks).
  31. (+) Independent Chip Select control for each memory bank
  32. (+) Independent configuration for each memory bank
  33. @endverbatim
  34. ******************************************************************************
  35. * @attention
  36. *
  37. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  38. * All rights reserved.</center></h2>
  39. *
  40. * This software component is licensed by ST under BSD 3-Clause license,
  41. * the "License"; You may not use this file except in compliance with the
  42. * License. You may obtain a copy of the License at:
  43. * opensource.org/licenses/BSD-3-Clause
  44. *
  45. ******************************************************************************
  46. */
  47. /* Includes ------------------------------------------------------------------*/
  48. #include "stm32l1xx_hal.h"
  49. /** @addtogroup STM32L1xx_HAL_Driver
  50. * @{
  51. */
  52. #if defined(FSMC_BANK1)
  53. #if defined(HAL_SRAM_MODULE_ENABLED) || defined(HAL_NOR_MODULE_ENABLED)
  54. /** @defgroup FSMC_LL FSMC Low Layer
  55. * @brief FSMC driver modules
  56. * @{
  57. */
  58. /* Private typedef -----------------------------------------------------------*/
  59. /* Private define ------------------------------------------------------------*/
  60. /** @defgroup FSMC_LL_Private_Constants FSMC Low Layer Private Constants
  61. * @{
  62. */
  63. /* ----------------------- FSMC registers bit mask --------------------------- */
  64. /* --- BCR Register ---*/
  65. /* BCR register clear mask */
  66. #define BCR_CLEAR_MASK ((uint32_t)(FSMC_BCRx_FACCEN | FSMC_BCRx_MUXEN | \
  67. FSMC_BCRx_MTYP | FSMC_BCRx_MWID | \
  68. FSMC_BCRx_BURSTEN | FSMC_BCRx_WAITPOL | \
  69. FSMC_BCRx_WRAPMOD | FSMC_BCRx_WAITCFG | \
  70. FSMC_BCRx_WREN | FSMC_BCRx_WAITEN | \
  71. FSMC_BCRx_EXTMOD | FSMC_BCRx_ASYNCWAIT | \
  72. FSMC_BCRx_CBURSTRW))
  73. /* --- BTR Register ---*/
  74. /* BTR register clear mask */
  75. #define BTR_CLEAR_MASK ((uint32_t)(FSMC_BTRx_ADDSET | FSMC_BTRx_ADDHLD |\
  76. FSMC_BTRx_DATAST | FSMC_BTRx_BUSTURN |\
  77. FSMC_BTRx_CLKDIV | FSMC_BTRx_DATLAT |\
  78. FSMC_BTRx_ACCMOD))
  79. /* --- BWTR Register ---*/
  80. /* BWTR register clear mask */
  81. #define BWTR_CLEAR_MASK ((uint32_t)(FSMC_BWTRx_ADDSET | FSMC_BWTRx_ADDHLD | \
  82. FSMC_BWTRx_DATAST | FSMC_BWTRx_ACCMOD | \
  83. FSMC_BWTRx_BUSTURN))
  84. /**
  85. * @}
  86. */
  87. /* Private macro -------------------------------------------------------------*/
  88. /** @defgroup FSMC_LL_Private_Macros FSMC Low Layer Private Macros
  89. * @{
  90. */
  91. /**
  92. * @}
  93. */
  94. /* Private variables ---------------------------------------------------------*/
  95. /* Private function prototypes -----------------------------------------------*/
  96. /* Exported functions --------------------------------------------------------*/
  97. /** @defgroup FSMC_LL_Exported_Functions FSMC Low Layer Exported Functions
  98. * @{
  99. */
  100. /** @defgroup FSMC_NORSRAM FSMC NORSRAM Controller functions
  101. * @brief NORSRAM Controller functions
  102. *
  103. @verbatim
  104. ==============================================================================
  105. ##### How to use NORSRAM device driver #####
  106. ==============================================================================
  107. [..]
  108. This driver contains a set of APIs to interface with the FSMC NORSRAM banks in order
  109. to run the NORSRAM external devices.
  110. (+) FSMC NORSRAM bank reset using the function FSMC_NORSRAM_DeInit()
  111. (+) FSMC NORSRAM bank control configuration using the function FSMC_NORSRAM_Init()
  112. (+) FSMC NORSRAM bank timing configuration using the function FSMC_NORSRAM_Timing_Init()
  113. (+) FSMC NORSRAM bank extended timing configuration using the function
  114. FSMC_NORSRAM_Extended_Timing_Init()
  115. (+) FSMC NORSRAM bank enable/disable write operation using the functions
  116. FSMC_NORSRAM_WriteOperation_Enable()/FSMC_NORSRAM_WriteOperation_Disable()
  117. @endverbatim
  118. * @{
  119. */
  120. /** @defgroup FSMC_NORSRAM_Group1 Initialization/de-initialization functions
  121. * @brief Initialization and Configuration functions
  122. *
  123. @verbatim
  124. ==============================================================================
  125. ##### Initialization and de_initialization functions #####
  126. ==============================================================================
  127. [..]
  128. This section provides functions allowing to:
  129. (+) Initialize and configure the FSMC NORSRAM interface
  130. (+) De-initialize the FSMC NORSRAM interface
  131. (+) Configure the FSMC clock and associated GPIOs
  132. @endverbatim
  133. * @{
  134. */
  135. /**
  136. * @brief Initialize the FSMC_NORSRAM device according to the specified
  137. * control parameters in the FSMC_NORSRAM_InitTypeDef
  138. * @param Device Pointer to NORSRAM device instance
  139. * @param Init Pointer to NORSRAM Initialization structure
  140. * @retval HAL status
  141. */
  142. HAL_StatusTypeDef FSMC_NORSRAM_Init(FSMC_NORSRAM_TypeDef *Device, FSMC_NORSRAM_InitTypeDef *Init)
  143. {
  144. /* Check the parameters */
  145. assert_param(IS_FSMC_NORSRAM_DEVICE(Device));
  146. assert_param(IS_FSMC_NORSRAM_BANK(Init->NSBank));
  147. assert_param(IS_FSMC_MUX(Init->DataAddressMux));
  148. assert_param(IS_FSMC_MEMORY(Init->MemoryType));
  149. assert_param(IS_FSMC_NORSRAM_MEMORY_WIDTH(Init->MemoryDataWidth));
  150. assert_param(IS_FSMC_BURSTMODE(Init->BurstAccessMode));
  151. assert_param(IS_FSMC_WAIT_POLARITY(Init->WaitSignalPolarity));
  152. assert_param(IS_FSMC_WRAP_MODE(Init->WrapMode));
  153. assert_param(IS_FSMC_WAIT_SIGNAL_ACTIVE(Init->WaitSignalActive));
  154. assert_param(IS_FSMC_WRITE_OPERATION(Init->WriteOperation));
  155. assert_param(IS_FSMC_WAITE_SIGNAL(Init->WaitSignal));
  156. assert_param(IS_FSMC_EXTENDED_MODE(Init->ExtendedMode));
  157. assert_param(IS_FSMC_ASYNWAIT(Init->AsynchronousWait));
  158. assert_param(IS_FSMC_WRITE_BURST(Init->WriteBurst));
  159. /* Disable NORSRAM Device */
  160. __FSMC_NORSRAM_DISABLE(Device, Init->NSBank);
  161. /* Set NORSRAM device control parameters */
  162. if (Init->MemoryType == FSMC_MEMORY_TYPE_NOR)
  163. {
  164. MODIFY_REG(Device->BTCR[Init->NSBank], BCR_CLEAR_MASK, (uint32_t)(FSMC_NORSRAM_FLASH_ACCESS_ENABLE
  165. | Init->DataAddressMux
  166. | Init->MemoryType
  167. | Init->MemoryDataWidth
  168. | Init->BurstAccessMode
  169. | Init->WaitSignalPolarity
  170. | Init->WrapMode
  171. | Init->WaitSignalActive
  172. | Init->WriteOperation
  173. | Init->WaitSignal
  174. | Init->ExtendedMode
  175. | Init->AsynchronousWait
  176. | Init->WriteBurst
  177. )
  178. );
  179. }
  180. else
  181. {
  182. MODIFY_REG(Device->BTCR[Init->NSBank], BCR_CLEAR_MASK, (uint32_t)(FSMC_NORSRAM_FLASH_ACCESS_DISABLE
  183. | Init->DataAddressMux
  184. | Init->MemoryType
  185. | Init->MemoryDataWidth
  186. | Init->BurstAccessMode
  187. | Init->WaitSignalPolarity
  188. | Init->WrapMode
  189. | Init->WaitSignalActive
  190. | Init->WriteOperation
  191. | Init->WaitSignal
  192. | Init->ExtendedMode
  193. | Init->AsynchronousWait
  194. | Init->WriteBurst
  195. )
  196. );
  197. }
  198. return HAL_OK;
  199. }
  200. /**
  201. * @brief DeInitialize the FSMC_NORSRAM peripheral
  202. * @param Device Pointer to NORSRAM device instance
  203. * @param ExDevice Pointer to NORSRAM extended mode device instance
  204. * @param Bank NORSRAM bank number
  205. * @retval HAL status
  206. */
  207. HAL_StatusTypeDef FSMC_NORSRAM_DeInit(FSMC_NORSRAM_TypeDef *Device, FSMC_NORSRAM_EXTENDED_TypeDef *ExDevice, uint32_t Bank)
  208. {
  209. /* Check the parameters */
  210. assert_param(IS_FSMC_NORSRAM_DEVICE(Device));
  211. assert_param(IS_FSMC_NORSRAM_EXTENDED_DEVICE(ExDevice));
  212. assert_param(IS_FSMC_NORSRAM_BANK(Bank));
  213. /* Disable the FSMC_NORSRAM device */
  214. __FSMC_NORSRAM_DISABLE(Device, Bank);
  215. /* De-initialize the FSMC_NORSRAM device */
  216. /* FSMC_NORSRAM_BANK1 */
  217. if (Bank == FSMC_NORSRAM_BANK1)
  218. {
  219. Device->BTCR[Bank] = 0x000030DB;
  220. }
  221. /* FSMC_NORSRAM_BANK2, FSMC_NORSRAM_BANK3 or FSMC_NORSRAM_BANK4 */
  222. else
  223. {
  224. Device->BTCR[Bank] = 0x000030D2;
  225. }
  226. Device->BTCR[Bank + 1] = 0x0FFFFFFF;
  227. ExDevice->BWTR[Bank] = 0x0FFFFFFF;
  228. return HAL_OK;
  229. }
  230. /**
  231. * @brief Initialize the FSMC_NORSRAM Timing according to the specified
  232. * parameters in the FSMC_NORSRAM_TimingTypeDef
  233. * @param Device Pointer to NORSRAM device instance
  234. * @param Timing Pointer to NORSRAM Timing structure
  235. * @param Bank NORSRAM bank number
  236. * @retval HAL status
  237. */
  238. HAL_StatusTypeDef FSMC_NORSRAM_Timing_Init(FSMC_NORSRAM_TypeDef *Device, FSMC_NORSRAM_TimingTypeDef *Timing, uint32_t Bank)
  239. {
  240. /* Check the parameters */
  241. assert_param(IS_FSMC_NORSRAM_DEVICE(Device));
  242. assert_param(IS_FSMC_ADDRESS_SETUP_TIME(Timing->AddressSetupTime));
  243. assert_param(IS_FSMC_ADDRESS_HOLD_TIME(Timing->AddressHoldTime));
  244. assert_param(IS_FSMC_DATASETUP_TIME(Timing->DataSetupTime));
  245. assert_param(IS_FSMC_TURNAROUND_TIME(Timing->BusTurnAroundDuration));
  246. assert_param(IS_FSMC_CLK_DIV(Timing->CLKDivision));
  247. assert_param(IS_FSMC_DATA_LATENCY(Timing->DataLatency));
  248. assert_param(IS_FSMC_ACCESS_MODE(Timing->AccessMode));
  249. assert_param(IS_FSMC_NORSRAM_BANK(Bank));
  250. /* Set FSMC_NORSRAM device timing parameters */
  251. MODIFY_REG(Device->BTCR[Bank + 1], \
  252. BTR_CLEAR_MASK, \
  253. (uint32_t)(Timing->AddressSetupTime | \
  254. ((Timing->AddressHoldTime) << POSITION_VAL(FSMC_BTRx_ADDHLD)) | \
  255. ((Timing->DataSetupTime) << POSITION_VAL(FSMC_BTRx_DATAST)) | \
  256. ((Timing->BusTurnAroundDuration) << POSITION_VAL(FSMC_BTRx_BUSTURN)) | \
  257. (((Timing->CLKDivision) - 1) << POSITION_VAL(FSMC_BTRx_CLKDIV)) | \
  258. (((Timing->DataLatency) - 2) << POSITION_VAL(FSMC_BTRx_DATLAT)) | \
  259. (Timing->AccessMode)));
  260. return HAL_OK;
  261. }
  262. /**
  263. * @brief Initialize the FSMC_NORSRAM Extended mode Timing according to the specified
  264. * parameters in the FSMC_NORSRAM_TimingTypeDef
  265. * @param Device Pointer to NORSRAM device instance
  266. * @param Timing Pointer to NORSRAM Timing structure
  267. * @param Bank NORSRAM bank number
  268. * @param ExtendedMode FSMC Extended Mode
  269. * This parameter can be one of the following values:
  270. * @arg FSMC_EXTENDED_MODE_DISABLE
  271. * @arg FSMC_EXTENDED_MODE_ENABLE
  272. * @retval HAL status
  273. */
  274. HAL_StatusTypeDef FSMC_NORSRAM_Extended_Timing_Init(FSMC_NORSRAM_EXTENDED_TypeDef *Device, FSMC_NORSRAM_TimingTypeDef *Timing, uint32_t Bank, uint32_t ExtendedMode)
  275. {
  276. /* Check the parameters */
  277. assert_param(IS_FSMC_EXTENDED_MODE(ExtendedMode));
  278. /* Set NORSRAM device timing register for write configuration, if extended mode is used */
  279. if (ExtendedMode == FSMC_EXTENDED_MODE_ENABLE)
  280. {
  281. /* Check the parameters */
  282. assert_param(IS_FSMC_NORSRAM_EXTENDED_DEVICE(Device));
  283. assert_param(IS_FSMC_ADDRESS_SETUP_TIME(Timing->AddressSetupTime));
  284. assert_param(IS_FSMC_ADDRESS_HOLD_TIME(Timing->AddressHoldTime));
  285. assert_param(IS_FSMC_DATASETUP_TIME(Timing->DataSetupTime));
  286. assert_param(IS_FSMC_TURNAROUND_TIME(Timing->BusTurnAroundDuration));
  287. assert_param(IS_FSMC_ACCESS_MODE(Timing->AccessMode));
  288. assert_param(IS_FSMC_NORSRAM_BANK(Bank));
  289. /* Set NORSRAM device timing register for write configuration, if extended mode is used */
  290. MODIFY_REG(Device->BWTR[Bank], \
  291. BWTR_CLEAR_MASK, \
  292. (uint32_t)(Timing->AddressSetupTime | \
  293. ((Timing->AddressHoldTime) << POSITION_VAL(FSMC_BWTRx_ADDHLD)) | \
  294. ((Timing->DataSetupTime) << POSITION_VAL(FSMC_BWTRx_DATAST)) | \
  295. Timing->AccessMode | \
  296. ((Timing->BusTurnAroundDuration) << POSITION_VAL(FSMC_BWTRx_BUSTURN))));
  297. }
  298. else
  299. {
  300. Device->BWTR[Bank] = 0x0FFFFFFF;
  301. }
  302. return HAL_OK;
  303. }
  304. /**
  305. * @}
  306. */
  307. /** @defgroup FSMC_NORSRAM_Group2 Control functions
  308. * @brief management functions
  309. *
  310. @verbatim
  311. ==============================================================================
  312. ##### FSMC_NORSRAM Control functions #####
  313. ==============================================================================
  314. [..]
  315. This subsection provides a set of functions allowing to control dynamically
  316. the FSMC NORSRAM interface.
  317. @endverbatim
  318. * @{
  319. */
  320. /**
  321. * @brief Enables dynamically FSMC_NORSRAM write operation.
  322. * @param Device Pointer to NORSRAM device instance
  323. * @param Bank NORSRAM bank number
  324. * @retval HAL status
  325. */
  326. HAL_StatusTypeDef FSMC_NORSRAM_WriteOperation_Enable(FSMC_NORSRAM_TypeDef *Device, uint32_t Bank)
  327. {
  328. /* Check the parameters */
  329. assert_param(IS_FSMC_NORSRAM_DEVICE(Device));
  330. assert_param(IS_FSMC_NORSRAM_BANK(Bank));
  331. /* Enable write operation */
  332. SET_BIT(Device->BTCR[Bank], FSMC_WRITE_OPERATION_ENABLE);
  333. return HAL_OK;
  334. }
  335. /**
  336. * @brief Disables dynamically FSMC_NORSRAM write operation.
  337. * @param Device Pointer to NORSRAM device instance
  338. * @param Bank NORSRAM bank number
  339. * @retval HAL status
  340. */
  341. HAL_StatusTypeDef FSMC_NORSRAM_WriteOperation_Disable(FSMC_NORSRAM_TypeDef *Device, uint32_t Bank)
  342. {
  343. /* Check the parameters */
  344. assert_param(IS_FSMC_NORSRAM_DEVICE(Device));
  345. assert_param(IS_FSMC_NORSRAM_BANK(Bank));
  346. /* Disable write operation */
  347. CLEAR_BIT(Device->BTCR[Bank], FSMC_WRITE_OPERATION_ENABLE);
  348. return HAL_OK;
  349. }
  350. /**
  351. * @}
  352. */
  353. /**
  354. * @}
  355. */
  356. /**
  357. * @}
  358. */
  359. /**
  360. * @}
  361. */
  362. #endif /* defined(HAL_SRAM_MODULE_ENABLED) || defined(HAL_NOR_MODULE_ENABLED) */
  363. #endif /* FSMC_BANK1 */
  364. /**
  365. * @}
  366. */
  367. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/