stm32l1xx_ll_fsmc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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</center></h2>
  38. *
  39. * Redistribution and use in source and binary forms, with or without modification,
  40. * are permitted provided that the following conditions are met:
  41. * 1. Redistributions of source code must retain the above copyright notice,
  42. * this list of conditions and the following disclaimer.
  43. * 2. Redistributions in binary form must reproduce the above copyright notice,
  44. * this list of conditions and the following disclaimer in the documentation
  45. * and/or other materials provided with the distribution.
  46. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  47. * may be used to endorse or promote products derived from this software
  48. * without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  51. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  52. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  53. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  54. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  55. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  56. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  57. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  58. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  59. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60. *
  61. ******************************************************************************
  62. */
  63. /* Includes ------------------------------------------------------------------*/
  64. #include "stm32l1xx_hal.h"
  65. /** @addtogroup STM32L1xx_HAL_Driver
  66. * @{
  67. */
  68. #if defined(FSMC_BANK1)
  69. #if defined(HAL_SRAM_MODULE_ENABLED) || defined(HAL_NOR_MODULE_ENABLED)
  70. /** @defgroup FSMC_LL FSMC Low Layer
  71. * @brief FSMC driver modules
  72. * @{
  73. */
  74. /* Private typedef -----------------------------------------------------------*/
  75. /* Private define ------------------------------------------------------------*/
  76. /** @defgroup FSMC_LL_Private_Constants FSMC Low Layer Private Constants
  77. * @{
  78. */
  79. /* ----------------------- FSMC registers bit mask --------------------------- */
  80. /* --- BCR Register ---*/
  81. /* BCR register clear mask */
  82. #define BCR_CLEAR_MASK ((uint32_t)(FSMC_BCRx_FACCEN | FSMC_BCRx_MUXEN | \
  83. FSMC_BCRx_MTYP | FSMC_BCRx_MWID | \
  84. FSMC_BCRx_BURSTEN | FSMC_BCRx_WAITPOL | \
  85. FSMC_BCRx_WRAPMOD | FSMC_BCRx_WAITCFG | \
  86. FSMC_BCRx_WREN | FSMC_BCRx_WAITEN | \
  87. FSMC_BCRx_EXTMOD | FSMC_BCRx_ASYNCWAIT | \
  88. FSMC_BCRx_CBURSTRW))
  89. /* --- BTR Register ---*/
  90. /* BTR register clear mask */
  91. #define BTR_CLEAR_MASK ((uint32_t)(FSMC_BTRx_ADDSET | FSMC_BTRx_ADDHLD |\
  92. FSMC_BTRx_DATAST | FSMC_BTRx_BUSTURN |\
  93. FSMC_BTRx_CLKDIV | FSMC_BTRx_DATLAT |\
  94. FSMC_BTRx_ACCMOD))
  95. /* --- BWTR Register ---*/
  96. /* BWTR register clear mask */
  97. #define BWTR_CLEAR_MASK ((uint32_t)(FSMC_BWTRx_ADDSET | FSMC_BWTRx_ADDHLD | \
  98. FSMC_BWTRx_DATAST | FSMC_BWTRx_ACCMOD | \
  99. FSMC_BWTRx_BUSTURN))
  100. /**
  101. * @}
  102. */
  103. /* Private macro -------------------------------------------------------------*/
  104. /** @defgroup FSMC_LL_Private_Macros FSMC Low Layer Private Macros
  105. * @{
  106. */
  107. /**
  108. * @}
  109. */
  110. /* Private variables ---------------------------------------------------------*/
  111. /* Private function prototypes -----------------------------------------------*/
  112. /* Exported functions --------------------------------------------------------*/
  113. /** @defgroup FSMC_LL_Exported_Functions FSMC Low Layer Exported Functions
  114. * @{
  115. */
  116. /** @defgroup FSMC_NORSRAM FSMC NORSRAM Controller functions
  117. * @brief NORSRAM Controller functions
  118. *
  119. @verbatim
  120. ==============================================================================
  121. ##### How to use NORSRAM device driver #####
  122. ==============================================================================
  123. [..]
  124. This driver contains a set of APIs to interface with the FSMC NORSRAM banks in order
  125. to run the NORSRAM external devices.
  126. (+) FSMC NORSRAM bank reset using the function FSMC_NORSRAM_DeInit()
  127. (+) FSMC NORSRAM bank control configuration using the function FSMC_NORSRAM_Init()
  128. (+) FSMC NORSRAM bank timing configuration using the function FSMC_NORSRAM_Timing_Init()
  129. (+) FSMC NORSRAM bank extended timing configuration using the function
  130. FSMC_NORSRAM_Extended_Timing_Init()
  131. (+) FSMC NORSRAM bank enable/disable write operation using the functions
  132. FSMC_NORSRAM_WriteOperation_Enable()/FSMC_NORSRAM_WriteOperation_Disable()
  133. @endverbatim
  134. * @{
  135. */
  136. /** @defgroup FSMC_NORSRAM_Group1 Initialization/de-initialization functions
  137. * @brief Initialization and Configuration functions
  138. *
  139. @verbatim
  140. ==============================================================================
  141. ##### Initialization and de_initialization functions #####
  142. ==============================================================================
  143. [..]
  144. This section provides functions allowing to:
  145. (+) Initialize and configure the FSMC NORSRAM interface
  146. (+) De-initialize the FSMC NORSRAM interface
  147. (+) Configure the FSMC clock and associated GPIOs
  148. @endverbatim
  149. * @{
  150. */
  151. /**
  152. * @brief Initialize the FSMC_NORSRAM device according to the specified
  153. * control parameters in the FSMC_NORSRAM_InitTypeDef
  154. * @param Device Pointer to NORSRAM device instance
  155. * @param Init Pointer to NORSRAM Initialization structure
  156. * @retval HAL status
  157. */
  158. HAL_StatusTypeDef FSMC_NORSRAM_Init(FSMC_NORSRAM_TypeDef *Device, FSMC_NORSRAM_InitTypeDef *Init)
  159. {
  160. /* Check the parameters */
  161. assert_param(IS_FSMC_NORSRAM_DEVICE(Device));
  162. assert_param(IS_FSMC_NORSRAM_BANK(Init->NSBank));
  163. assert_param(IS_FSMC_MUX(Init->DataAddressMux));
  164. assert_param(IS_FSMC_MEMORY(Init->MemoryType));
  165. assert_param(IS_FSMC_NORSRAM_MEMORY_WIDTH(Init->MemoryDataWidth));
  166. assert_param(IS_FSMC_BURSTMODE(Init->BurstAccessMode));
  167. assert_param(IS_FSMC_WAIT_POLARITY(Init->WaitSignalPolarity));
  168. assert_param(IS_FSMC_WRAP_MODE(Init->WrapMode));
  169. assert_param(IS_FSMC_WAIT_SIGNAL_ACTIVE(Init->WaitSignalActive));
  170. assert_param(IS_FSMC_WRITE_OPERATION(Init->WriteOperation));
  171. assert_param(IS_FSMC_WAITE_SIGNAL(Init->WaitSignal));
  172. assert_param(IS_FSMC_EXTENDED_MODE(Init->ExtendedMode));
  173. assert_param(IS_FSMC_ASYNWAIT(Init->AsynchronousWait));
  174. assert_param(IS_FSMC_WRITE_BURST(Init->WriteBurst));
  175. /* Disable NORSRAM Device */
  176. __FSMC_NORSRAM_DISABLE(Device, Init->NSBank);
  177. /* Set NORSRAM device control parameters */
  178. if (Init->MemoryType == FSMC_MEMORY_TYPE_NOR)
  179. {
  180. MODIFY_REG(Device->BTCR[Init->NSBank], BCR_CLEAR_MASK, (uint32_t)(FSMC_NORSRAM_FLASH_ACCESS_ENABLE
  181. | Init->DataAddressMux
  182. | Init->MemoryType
  183. | Init->MemoryDataWidth
  184. | Init->BurstAccessMode
  185. | Init->WaitSignalPolarity
  186. | Init->WrapMode
  187. | Init->WaitSignalActive
  188. | Init->WriteOperation
  189. | Init->WaitSignal
  190. | Init->ExtendedMode
  191. | Init->AsynchronousWait
  192. | Init->WriteBurst
  193. )
  194. );
  195. }
  196. else
  197. {
  198. MODIFY_REG(Device->BTCR[Init->NSBank], BCR_CLEAR_MASK, (uint32_t)(FSMC_NORSRAM_FLASH_ACCESS_DISABLE
  199. | Init->DataAddressMux
  200. | Init->MemoryType
  201. | Init->MemoryDataWidth
  202. | Init->BurstAccessMode
  203. | Init->WaitSignalPolarity
  204. | Init->WrapMode
  205. | Init->WaitSignalActive
  206. | Init->WriteOperation
  207. | Init->WaitSignal
  208. | Init->ExtendedMode
  209. | Init->AsynchronousWait
  210. | Init->WriteBurst
  211. )
  212. );
  213. }
  214. return HAL_OK;
  215. }
  216. /**
  217. * @brief DeInitialize the FSMC_NORSRAM peripheral
  218. * @param Device Pointer to NORSRAM device instance
  219. * @param ExDevice Pointer to NORSRAM extended mode device instance
  220. * @param Bank NORSRAM bank number
  221. * @retval HAL status
  222. */
  223. HAL_StatusTypeDef FSMC_NORSRAM_DeInit(FSMC_NORSRAM_TypeDef *Device, FSMC_NORSRAM_EXTENDED_TypeDef *ExDevice, uint32_t Bank)
  224. {
  225. /* Check the parameters */
  226. assert_param(IS_FSMC_NORSRAM_DEVICE(Device));
  227. assert_param(IS_FSMC_NORSRAM_EXTENDED_DEVICE(ExDevice));
  228. assert_param(IS_FSMC_NORSRAM_BANK(Bank));
  229. /* Disable the FSMC_NORSRAM device */
  230. __FSMC_NORSRAM_DISABLE(Device, Bank);
  231. /* De-initialize the FSMC_NORSRAM device */
  232. /* FSMC_NORSRAM_BANK1 */
  233. if (Bank == FSMC_NORSRAM_BANK1)
  234. {
  235. Device->BTCR[Bank] = 0x000030DB;
  236. }
  237. /* FSMC_NORSRAM_BANK2, FSMC_NORSRAM_BANK3 or FSMC_NORSRAM_BANK4 */
  238. else
  239. {
  240. Device->BTCR[Bank] = 0x000030D2;
  241. }
  242. Device->BTCR[Bank + 1] = 0x0FFFFFFF;
  243. ExDevice->BWTR[Bank] = 0x0FFFFFFF;
  244. return HAL_OK;
  245. }
  246. /**
  247. * @brief Initialize the FSMC_NORSRAM Timing according to the specified
  248. * parameters in the FSMC_NORSRAM_TimingTypeDef
  249. * @param Device Pointer to NORSRAM device instance
  250. * @param Timing Pointer to NORSRAM Timing structure
  251. * @param Bank NORSRAM bank number
  252. * @retval HAL status
  253. */
  254. HAL_StatusTypeDef FSMC_NORSRAM_Timing_Init(FSMC_NORSRAM_TypeDef *Device, FSMC_NORSRAM_TimingTypeDef *Timing, uint32_t Bank)
  255. {
  256. /* Check the parameters */
  257. assert_param(IS_FSMC_NORSRAM_DEVICE(Device));
  258. assert_param(IS_FSMC_ADDRESS_SETUP_TIME(Timing->AddressSetupTime));
  259. assert_param(IS_FSMC_ADDRESS_HOLD_TIME(Timing->AddressHoldTime));
  260. assert_param(IS_FSMC_DATASETUP_TIME(Timing->DataSetupTime));
  261. assert_param(IS_FSMC_TURNAROUND_TIME(Timing->BusTurnAroundDuration));
  262. assert_param(IS_FSMC_CLK_DIV(Timing->CLKDivision));
  263. assert_param(IS_FSMC_DATA_LATENCY(Timing->DataLatency));
  264. assert_param(IS_FSMC_ACCESS_MODE(Timing->AccessMode));
  265. assert_param(IS_FSMC_NORSRAM_BANK(Bank));
  266. /* Set FSMC_NORSRAM device timing parameters */
  267. MODIFY_REG(Device->BTCR[Bank + 1], \
  268. BTR_CLEAR_MASK, \
  269. (uint32_t)(Timing->AddressSetupTime | \
  270. ((Timing->AddressHoldTime) << POSITION_VAL(FSMC_BTRx_ADDHLD)) | \
  271. ((Timing->DataSetupTime) << POSITION_VAL(FSMC_BTRx_DATAST)) | \
  272. ((Timing->BusTurnAroundDuration) << POSITION_VAL(FSMC_BTRx_BUSTURN)) | \
  273. (((Timing->CLKDivision) - 1) << POSITION_VAL(FSMC_BTRx_CLKDIV)) | \
  274. (((Timing->DataLatency) - 2) << POSITION_VAL(FSMC_BTRx_DATLAT)) | \
  275. (Timing->AccessMode)));
  276. return HAL_OK;
  277. }
  278. /**
  279. * @brief Initialize the FSMC_NORSRAM Extended mode Timing according to the specified
  280. * parameters in the FSMC_NORSRAM_TimingTypeDef
  281. * @param Device Pointer to NORSRAM device instance
  282. * @param Timing Pointer to NORSRAM Timing structure
  283. * @param Bank NORSRAM bank number
  284. * @param ExtendedMode FSMC Extended Mode
  285. * This parameter can be one of the following values:
  286. * @arg FSMC_EXTENDED_MODE_DISABLE
  287. * @arg FSMC_EXTENDED_MODE_ENABLE
  288. * @retval HAL status
  289. */
  290. HAL_StatusTypeDef FSMC_NORSRAM_Extended_Timing_Init(FSMC_NORSRAM_EXTENDED_TypeDef *Device, FSMC_NORSRAM_TimingTypeDef *Timing, uint32_t Bank, uint32_t ExtendedMode)
  291. {
  292. /* Check the parameters */
  293. assert_param(IS_FSMC_EXTENDED_MODE(ExtendedMode));
  294. /* Set NORSRAM device timing register for write configuration, if extended mode is used */
  295. if (ExtendedMode == FSMC_EXTENDED_MODE_ENABLE)
  296. {
  297. /* Check the parameters */
  298. assert_param(IS_FSMC_NORSRAM_EXTENDED_DEVICE(Device));
  299. assert_param(IS_FSMC_ADDRESS_SETUP_TIME(Timing->AddressSetupTime));
  300. assert_param(IS_FSMC_ADDRESS_HOLD_TIME(Timing->AddressHoldTime));
  301. assert_param(IS_FSMC_DATASETUP_TIME(Timing->DataSetupTime));
  302. assert_param(IS_FSMC_TURNAROUND_TIME(Timing->BusTurnAroundDuration));
  303. assert_param(IS_FSMC_ACCESS_MODE(Timing->AccessMode));
  304. assert_param(IS_FSMC_NORSRAM_BANK(Bank));
  305. /* Set NORSRAM device timing register for write configuration, if extended mode is used */
  306. MODIFY_REG(Device->BWTR[Bank], \
  307. BWTR_CLEAR_MASK, \
  308. (uint32_t)(Timing->AddressSetupTime | \
  309. ((Timing->AddressHoldTime) << POSITION_VAL(FSMC_BWTRx_ADDHLD)) | \
  310. ((Timing->DataSetupTime) << POSITION_VAL(FSMC_BWTRx_DATAST)) | \
  311. Timing->AccessMode | \
  312. ((Timing->BusTurnAroundDuration) << POSITION_VAL(FSMC_BWTRx_BUSTURN))));
  313. }
  314. else
  315. {
  316. Device->BWTR[Bank] = 0x0FFFFFFF;
  317. }
  318. return HAL_OK;
  319. }
  320. /**
  321. * @}
  322. */
  323. /** @defgroup FSMC_NORSRAM_Group2 Control functions
  324. * @brief management functions
  325. *
  326. @verbatim
  327. ==============================================================================
  328. ##### FSMC_NORSRAM Control functions #####
  329. ==============================================================================
  330. [..]
  331. This subsection provides a set of functions allowing to control dynamically
  332. the FSMC NORSRAM interface.
  333. @endverbatim
  334. * @{
  335. */
  336. /**
  337. * @brief Enables dynamically FSMC_NORSRAM write operation.
  338. * @param Device Pointer to NORSRAM device instance
  339. * @param Bank NORSRAM bank number
  340. * @retval HAL status
  341. */
  342. HAL_StatusTypeDef FSMC_NORSRAM_WriteOperation_Enable(FSMC_NORSRAM_TypeDef *Device, uint32_t Bank)
  343. {
  344. /* Check the parameters */
  345. assert_param(IS_FSMC_NORSRAM_DEVICE(Device));
  346. assert_param(IS_FSMC_NORSRAM_BANK(Bank));
  347. /* Enable write operation */
  348. SET_BIT(Device->BTCR[Bank], FSMC_WRITE_OPERATION_ENABLE);
  349. return HAL_OK;
  350. }
  351. /**
  352. * @brief Disables dynamically FSMC_NORSRAM write operation.
  353. * @param Device Pointer to NORSRAM device instance
  354. * @param Bank NORSRAM bank number
  355. * @retval HAL status
  356. */
  357. HAL_StatusTypeDef FSMC_NORSRAM_WriteOperation_Disable(FSMC_NORSRAM_TypeDef *Device, uint32_t Bank)
  358. {
  359. /* Check the parameters */
  360. assert_param(IS_FSMC_NORSRAM_DEVICE(Device));
  361. assert_param(IS_FSMC_NORSRAM_BANK(Bank));
  362. /* Disable write operation */
  363. CLEAR_BIT(Device->BTCR[Bank], FSMC_WRITE_OPERATION_ENABLE);
  364. return HAL_OK;
  365. }
  366. /**
  367. * @}
  368. */
  369. /**
  370. * @}
  371. */
  372. /**
  373. * @}
  374. */
  375. /**
  376. * @}
  377. */
  378. #endif /* defined(HAL_SRAM_MODULE_ENABLED) || defined(HAL_NOR_MODULE_ENABLED) */
  379. #endif /* FSMC_BANK1 */
  380. /**
  381. * @}
  382. */
  383. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/