stm32l1xx_ll_fsmc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_ll_fsmc.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 5-September-2014
  7. * @brief FSMC Low Layer HAL module driver.
  8. *
  9. * This file provides firmware functions to manage the following
  10. * functionalities of the Flexible Static Memory Controller (FSMC) peripheral memories:
  11. * + Initialization/de-initialization functions
  12. * + Peripheral Control functions
  13. * + Peripheral State functions
  14. *
  15. @verbatim
  16. =============================================================================
  17. ##### FSMC peripheral features #####
  18. =============================================================================
  19. [..] The Flexible static memory controller (FSMC) includes following memory controllers:
  20. (+) The NOR/PSRAM memory controller
  21. [..] The FSMC functional block makes the interface with synchronous and asynchronous static
  22. memories and SDRAM memories. Its main purposes are:
  23. (+) to translate AHB transactions into the appropriate external device protocol.
  24. (+) to meet the access time requirements of the external memory devices.
  25. [..] All external memories share the addresses, data and control signals with the controller.
  26. Each external device is accessed by means of a unique Chip Select. The FSMC performs
  27. only one access at a time to an external device.
  28. The main features of the FSMC controller are the following:
  29. (+) Interface with static-memory mapped devices including:
  30. (++) Static random access memory (SRAM).
  31. (++) NOR Flash memory.
  32. (++) PSRAM (4 memory banks).
  33. (+) Independent Chip Select control for each memory bank.
  34. (+) Independent configuration for each memory bank.
  35. =============================================================================
  36. ##### How to use NORSRAM device driver #####
  37. =============================================================================
  38. [..]
  39. This driver contains a set of APIs to interface with the FSMC NORSRAM banks in order
  40. to run the NORSRAM external devices.
  41. (+) FSMC NORSRAM bank reset using the function FSMC_NORSRAM_DeInit()
  42. (+) FSMC NORSRAM bank control configuration using the function FSMC_NORSRAM_Init()
  43. (+) FSMC NORSRAM bank timing configuration using the function FSMC_NORSRAM_Timing_Init()
  44. (+) FSMC NORSRAM bank extended timing configuration using the function
  45. FSMC_NORSRAM_Extended_Timing_Init()
  46. (+) FSMC NORSRAM bank enable/disable write operation using the functions
  47. FSMC_NORSRAM_WriteOperation_Enable()/FSMC_NORSRAM_WriteOperation_Disable()
  48. @endverbatim
  49. ******************************************************************************
  50. * @attention
  51. *
  52. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  53. *
  54. * Redistribution and use in source and binary forms, with or without modification,
  55. * are permitted provided that the following conditions are met:
  56. * 1. Redistributions of source code must retain the above copyright notice,
  57. * this list of conditions and the following disclaimer.
  58. * 2. Redistributions in binary form must reproduce the above copyright notice,
  59. * this list of conditions and the following disclaimer in the documentation
  60. * and/or other materials provided with the distribution.
  61. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  62. * may be used to endorse or promote products derived from this software
  63. * without specific prior written permission.
  64. *
  65. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  66. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  67. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  68. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  69. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  70. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  71. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  72. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  73. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  74. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  75. *
  76. ******************************************************************************
  77. */
  78. /* Includes ------------------------------------------------------------------*/
  79. #include "stm32l1xx_hal.h"
  80. /** @addtogroup STM32L1xx_HAL_Driver
  81. * @{
  82. */
  83. /** @defgroup FSMC_LL FSMC_LL
  84. * @brief FSMC driver modules
  85. * @{
  86. */
  87. #if defined (HAL_SRAM_MODULE_ENABLED) || defined(HAL_NOR_MODULE_ENABLED)
  88. #if defined (STM32L151xD) || defined (STM32L152xD) || defined (STM32L162xD)
  89. /* Private typedef -----------------------------------------------------------*/
  90. /* Private define ------------------------------------------------------------*/
  91. /* Private macro -------------------------------------------------------------*/
  92. /* Private variables ---------------------------------------------------------*/
  93. /* Private function prototypes -----------------------------------------------*/
  94. /* Private functions ---------------------------------------------------------*/
  95. /** @defgroup FSMC_Exported_Functions FSMC Exported Functions
  96. * @{
  97. */
  98. /** @defgroup HAL_FSMC_NORSRAM_Group1 Initialization/de-initialization functions
  99. * @brief Initialization and Configuration functions
  100. *
  101. @verbatim
  102. ==============================================================================
  103. ##### Initialization and de_initialization functions #####
  104. ==============================================================================
  105. [..]
  106. This section provides functions allowing to:
  107. (+) Initialize and configure the FSMC NORSRAM interface
  108. (+) De-initialize the FSMC NORSRAM interface
  109. (+) Configure the FSMC clock and associated GPIOs
  110. @endverbatim
  111. * @{
  112. */
  113. /**
  114. * @brief Initialize the FSMC_NORSRAM device according to the specified
  115. * control parameters in the FSMC_NORSRAM_InitTypeDef
  116. * @param Device: Pointer to NORSRAM device instance
  117. * @param Init: Pointer to NORSRAM Initialization structure
  118. * @retval HAL status
  119. */
  120. HAL_StatusTypeDef FSMC_NORSRAM_Init(FSMC_NORSRAM_TYPEDEF *Device, FSMC_NORSRAM_InitTypeDef* Init)
  121. {
  122. uint32_t tmpr = 0;
  123. /* Check the parameters */
  124. assert_param(IS_FSMC_NORSRAM_BANK(Init->NSBank));
  125. assert_param(IS_FSMC_MUX(Init->DataAddressMux));
  126. assert_param(IS_FSMC_MEMORY(Init->MemoryType));
  127. assert_param(IS_FSMC_NORSRAM_MEMORY_WIDTH(Init->MemoryDataWidth));
  128. assert_param(IS_FSMC_BURSTMODE(Init->BurstAccessMode));
  129. assert_param(IS_FSMC_WAIT_POLARITY(Init->WaitSignalPolarity));
  130. assert_param(IS_FSMC_WRAP_MODE(Init->WrapMode));
  131. assert_param(IS_FSMC_WAIT_SIGNAL_ACTIVE(Init->WaitSignalActive));
  132. assert_param(IS_FSMC_WRITE_OPERATION(Init->WriteOperation));
  133. assert_param(IS_FSMC_WAITE_SIGNAL(Init->WaitSignal));
  134. assert_param(IS_FSMC_EXTENDED_MODE(Init->ExtendedMode));
  135. assert_param(IS_FSMC_ASYNWAIT(Init->AsynchronousWait));
  136. assert_param(IS_FSMC_WRITE_BURST(Init->WriteBurst));
  137. /* Set NORSRAM device control parameters */
  138. tmpr = (uint32_t)(Init->DataAddressMux |\
  139. Init->MemoryType |\
  140. Init->MemoryDataWidth |\
  141. Init->BurstAccessMode |\
  142. Init->WaitSignalPolarity |\
  143. Init->WrapMode |\
  144. Init->WaitSignalActive |\
  145. Init->WriteOperation |\
  146. Init->WaitSignal |\
  147. Init->ExtendedMode |\
  148. Init->AsynchronousWait |\
  149. Init->WriteBurst
  150. );
  151. if(Init->MemoryType == FSMC_MEMORY_TYPE_NOR)
  152. {
  153. tmpr |= (uint32_t)FSMC_NORSRAM_FLASH_ACCESS_ENABLE;
  154. }
  155. Device->BTCR[Init->NSBank] = tmpr;
  156. return HAL_OK;
  157. }
  158. /**
  159. * @brief DeInitialize the FSMC_NORSRAM peripheral
  160. * @param Device: Pointer to NORSRAM device instance
  161. * @param ExDevice: Pointer to NORSRAM extended mode device instance
  162. * @param Bank: NORSRAM bank number
  163. * @retval HAL status
  164. */
  165. HAL_StatusTypeDef FSMC_NORSRAM_DeInit(FSMC_NORSRAM_TYPEDEF *Device, FSMC_NORSRAM_EXTENDED_TYPEDEF *ExDevice, uint32_t Bank)
  166. {
  167. /* Check the parameters */
  168. assert_param(IS_FSMC_NORSRAM_DEVICE(Device));
  169. assert_param(IS_FSMC_NORSRAM_EXTENDED_DEVICE(ExDevice));
  170. /* Disable the FSMC_NORSRAM device */
  171. __FSMC_NORSRAM_DISABLE(Device, Bank);
  172. /* De-initialize the FSMC_NORSRAM device */
  173. /* FSMC_NORSRAM_BANK1 */
  174. if(Bank == FSMC_BANK1_NORSRAM1)
  175. {
  176. Device->BTCR[Bank] = 0x000030DB;
  177. }
  178. /* FSMC_BANK1_NORSRAM2, FSMC_BANK1_NORSRAM3 or FSMC_BANK1_NORSRAM4 */
  179. else
  180. {
  181. Device->BTCR[Bank] = 0x000030D2;
  182. }
  183. Device->BTCR[Bank + 1] = 0x0FFFFFFF;
  184. ExDevice->BWTR[Bank] = 0x0FFFFFFF;
  185. return HAL_OK;
  186. }
  187. /**
  188. * @brief Initialize the FSMC_NORSRAM Timing according to the specified
  189. * parameters in the FSMC_NORSRAM_TimingTypeDef
  190. * @param Device: Pointer to NORSRAM device instance
  191. * @param Timing: Pointer to NORSRAM Timing structure
  192. * @param Bank: NORSRAM bank number
  193. * @retval HAL status
  194. */
  195. HAL_StatusTypeDef FSMC_NORSRAM_Timing_Init(FSMC_NORSRAM_TYPEDEF *Device, FSMC_NORSRAM_TimingTypeDef *Timing, uint32_t Bank)
  196. {
  197. uint32_t tmpr = 0;
  198. /* Check the parameters */
  199. assert_param(IS_FSMC_ADDRESS_SETUP_TIME(Timing->AddressSetupTime));
  200. assert_param(IS_FSMC_ADDRESS_HOLD_TIME(Timing->AddressHoldTime));
  201. assert_param(IS_FSMC_DATASETUP_TIME(Timing->DataSetupTime));
  202. assert_param(IS_FSMC_TURNAROUND_TIME(Timing->BusTurnAroundDuration));
  203. assert_param(IS_FSMC_CLK_DIV(Timing->CLKDivision));
  204. assert_param(IS_FSMC_DATA_LATENCY(Timing->DataLatency));
  205. assert_param(IS_FSMC_ACCESS_MODE(Timing->AccessMode));
  206. /* Set FSMC_NORSRAM device timing parameters */
  207. tmpr = (uint32_t)(Timing->AddressSetupTime |\
  208. ((Timing->AddressHoldTime) << POSITION_VAL(FSMC_BTRx_ADDHLD)) |\
  209. ((Timing->DataSetupTime) << POSITION_VAL(FSMC_BTRx_DATAST)) |\
  210. ((Timing->BusTurnAroundDuration) << POSITION_VAL(FSMC_BTRx_BUSTURN)) |\
  211. (((Timing->CLKDivision)-1) << POSITION_VAL(FSMC_BTRx_CLKDIV)) |\
  212. (((Timing->DataLatency)-2) << POSITION_VAL(FSMC_BTRx_DATLAT)) |\
  213. (Timing->AccessMode)
  214. );
  215. Device->BTCR[Bank + 1] = tmpr;
  216. return HAL_OK;
  217. }
  218. /**
  219. * @brief Initialize the FSMC_NORSRAM Extended mode Timing according to the specified
  220. * parameters in the FSMC_NORSRAM_TimingTypeDef
  221. * @param Device: Pointer to NORSRAM device instance
  222. * @param Timing: Pointer to NORSRAM Timing structure
  223. * @param Bank: NORSRAM bank number
  224. * @param ExtendedMode: FSMC Extended Mode
  225. * This parameter can be one of the following values:
  226. * @arg FSMC_EXTENDED_MODE_DISABLE
  227. * @arg FSMC_EXTENDED_MODE_ENABLE
  228. * @retval HAL status
  229. */
  230. HAL_StatusTypeDef FSMC_NORSRAM_Extended_Timing_Init(FSMC_NORSRAM_EXTENDED_TYPEDEF *Device, FSMC_NORSRAM_TimingTypeDef *Timing, uint32_t Bank, uint32_t ExtendedMode)
  231. {
  232. /* Set NORSRAM device timing register for write configuration, if extended mode is used */
  233. if(ExtendedMode == FSMC_EXTENDED_MODE_ENABLE)
  234. {
  235. /* Check the parameters */
  236. assert_param(IS_FSMC_ADDRESS_SETUP_TIME(Timing->AddressSetupTime));
  237. assert_param(IS_FSMC_ADDRESS_HOLD_TIME(Timing->AddressHoldTime));
  238. assert_param(IS_FSMC_DATASETUP_TIME(Timing->DataSetupTime));
  239. assert_param(IS_FSMC_TURNAROUND_TIME(Timing->BusTurnAroundDuration));
  240. assert_param(IS_FSMC_ACCESS_MODE(Timing->AccessMode));
  241. Device->BWTR[Bank] = (uint32_t)(Timing->AddressSetupTime |\
  242. ((Timing->AddressHoldTime) << POSITION_VAL(FSMC_BWTRx_ADDHLD)) |\
  243. ((Timing->DataSetupTime) << POSITION_VAL(FSMC_BWTRx_DATAST)) |\
  244. ((Timing->BusTurnAroundDuration) << POSITION_VAL(FSMC_BWTRx_BUSTURN)) |\
  245. (Timing->AccessMode));
  246. }
  247. else
  248. {
  249. Device->BWTR[Bank] = 0x0FFFFFFF;
  250. }
  251. return HAL_OK;
  252. }
  253. /**
  254. * @}
  255. */
  256. /** @defgroup HAL_FSMC_NORSRAM_Group2 Control functions
  257. * @brief management functions
  258. *
  259. @verbatim
  260. ==============================================================================
  261. ##### FSMC_NORSRAM Control functions #####
  262. ==============================================================================
  263. [..]
  264. This subsection provides a set of functions allowing to control dynamically
  265. the FSMC NORSRAM interface.
  266. @endverbatim
  267. * @{
  268. */
  269. /**
  270. * @brief Enables dynamically FSMC_NORSRAM write operation.
  271. * @param Device: Pointer to NORSRAM device instance
  272. * @param Bank: NORSRAM bank number
  273. * @retval HAL status
  274. */
  275. HAL_StatusTypeDef FSMC_NORSRAM_WriteOperation_Enable(FSMC_NORSRAM_TYPEDEF *Device, uint32_t Bank)
  276. {
  277. /* Enable write operation */
  278. Device->BTCR[Bank] |= FSMC_WRITE_OPERATION_ENABLE;
  279. return HAL_OK;
  280. }
  281. /**
  282. * @brief Disables dynamically FSMC_NORSRAM write operation.
  283. * @param Device: Pointer to NORSRAM device instance
  284. * @param Bank: NORSRAM bank number
  285. * @retval HAL status
  286. */
  287. HAL_StatusTypeDef FSMC_NORSRAM_WriteOperation_Disable(FSMC_NORSRAM_TYPEDEF *Device, uint32_t Bank)
  288. {
  289. /* Disable write operation */
  290. Device->BTCR[Bank] &= ~FSMC_WRITE_OPERATION_ENABLE;
  291. return HAL_OK;
  292. }
  293. /**
  294. * @}
  295. */
  296. /**
  297. * @}
  298. */
  299. #endif /* STM32L151xD || STM32L152xD || STM32L162xD */
  300. #endif /* HAL_FSMC_MODULE_ENABLED */
  301. /**
  302. * @}
  303. */
  304. /**
  305. * @}
  306. */
  307. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/