25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

1498 satır
54 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_ll_fmc.c
  4. * @author MCD Application Team
  5. * @brief FMC Low Layer HAL module driver.
  6. *
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the Flexible Memory Controller (FMC) peripheral memories:
  9. * + Initialization/de-initialization functions
  10. * + Peripheral Control functions
  11. * + Peripheral State functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2016 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### FMC peripheral features #####
  27. ==============================================================================
  28. [..] The Flexible memory controller (FMC) includes following memory controllers:
  29. (+) The NOR/PSRAM memory controller
  30. (+) The NAND/PC Card memory controller
  31. (+) The Synchronous DRAM (SDRAM) controller
  32. [..] The FMC functional block makes the interface with synchronous and asynchronous static
  33. memories, SDRAM memories, and 16-bit PC memory cards. Its main purposes are:
  34. (+) to translate AHB transactions into the appropriate external device protocol
  35. (+) to meet the access time requirements of the external memory devices
  36. [..] All external memories share the addresses, data and control signals with the controller.
  37. Each external device is accessed by means of a unique Chip Select. The FMC performs
  38. only one access at a time to an external device.
  39. The main features of the FMC controller are the following:
  40. (+) Interface with static-memory mapped devices including:
  41. (++) Static random access memory (SRAM)
  42. (++) Read-only memory (ROM)
  43. (++) NOR Flash memory/OneNAND Flash memory
  44. (++) PSRAM (4 memory banks)
  45. (++) 16-bit PC Card compatible devices
  46. (++) Two banks of NAND Flash memory with ECC hardware to check up to 8 Kbytes of
  47. data
  48. (+) Interface with synchronous DRAM (SDRAM) memories
  49. (+) Independent Chip Select control for each memory bank
  50. (+) Independent configuration for each memory bank
  51. @endverbatim
  52. ******************************************************************************
  53. */
  54. /* Includes ------------------------------------------------------------------*/
  55. #include "stm32f4xx_hal.h"
  56. /** @addtogroup STM32F4xx_HAL_Driver
  57. * @{
  58. */
  59. #if defined(HAL_NOR_MODULE_ENABLED) || (defined(HAL_NAND_MODULE_ENABLED)) || defined(HAL_PCCARD_MODULE_ENABLED) || defined(HAL_SDRAM_MODULE_ENABLED)\
  60. || defined(HAL_SRAM_MODULE_ENABLED)
  61. /** @defgroup FMC_LL FMC Low Layer
  62. * @brief FMC driver modules
  63. * @{
  64. */
  65. /* Private typedef -----------------------------------------------------------*/
  66. /* Private define ------------------------------------------------------------*/
  67. /** @defgroup FMC_LL_Private_Constants FMC Low Layer Private Constants
  68. * @{
  69. */
  70. /* ----------------------- FMC registers bit mask --------------------------- */
  71. #if defined(FMC_Bank1)
  72. /* --- BCR Register ---*/
  73. /* BCR register clear mask */
  74. /* --- BTR Register ---*/
  75. /* BTR register clear mask */
  76. #define BTR_CLEAR_MASK ((uint32_t)(FMC_BTR1_ADDSET | FMC_BTR1_ADDHLD |\
  77. FMC_BTR1_DATAST | FMC_BTR1_BUSTURN |\
  78. FMC_BTR1_CLKDIV | FMC_BTR1_DATLAT |\
  79. FMC_BTR1_ACCMOD))
  80. /* --- BWTR Register ---*/
  81. /* BWTR register clear mask */
  82. #define BWTR_CLEAR_MASK ((uint32_t)(FMC_BWTR1_ADDSET | FMC_BWTR1_ADDHLD |\
  83. FMC_BWTR1_DATAST | FMC_BWTR1_BUSTURN |\
  84. FMC_BWTR1_ACCMOD))
  85. #endif /* FMC_Bank1 */
  86. #if defined(FMC_Bank3) || defined(FMC_Bank2_3)
  87. #if defined (FMC_PCR_PWAITEN)
  88. /* --- PCR Register ---*/
  89. /* PCR register clear mask */
  90. #define PCR_CLEAR_MASK ((uint32_t)(FMC_PCR_PWAITEN | FMC_PCR_PBKEN | \
  91. FMC_PCR_PTYP | FMC_PCR_PWID | \
  92. FMC_PCR_ECCEN | FMC_PCR_TCLR | \
  93. FMC_PCR_TAR | FMC_PCR_ECCPS))
  94. /* --- PMEM Register ---*/
  95. /* PMEM register clear mask */
  96. #define PMEM_CLEAR_MASK ((uint32_t)(FMC_PMEM_MEMSET2 | FMC_PMEM_MEMWAIT2 |\
  97. FMC_PMEM_MEMHOLD2 | FMC_PMEM_MEMHIZ2))
  98. /* --- PATT Register ---*/
  99. /* PATT register clear mask */
  100. #define PATT_CLEAR_MASK ((uint32_t)(FMC_PATT_ATTSET2 | FMC_PATT_ATTWAIT2 |\
  101. FMC_PATT_ATTHOLD2 | FMC_PATT_ATTHIZ2))
  102. #else
  103. /* --- PCR Register ---*/
  104. /* PCR register clear mask */
  105. #define PCR_CLEAR_MASK ((uint32_t)(FMC_PCR2_PWAITEN | FMC_PCR2_PBKEN | \
  106. FMC_PCR2_PTYP | FMC_PCR2_PWID | \
  107. FMC_PCR2_ECCEN | FMC_PCR2_TCLR | \
  108. FMC_PCR2_TAR | FMC_PCR2_ECCPS))
  109. /* --- PMEM Register ---*/
  110. /* PMEM register clear mask */
  111. #define PMEM_CLEAR_MASK ((uint32_t)(FMC_PMEM2_MEMSET2 | FMC_PMEM2_MEMWAIT2 |\
  112. FMC_PMEM2_MEMHOLD2 | FMC_PMEM2_MEMHIZ2))
  113. /* --- PATT Register ---*/
  114. /* PATT register clear mask */
  115. #define PATT_CLEAR_MASK ((uint32_t)(FMC_PATT2_ATTSET2 | FMC_PATT2_ATTWAIT2 |\
  116. FMC_PATT2_ATTHOLD2 | FMC_PATT2_ATTHIZ2))
  117. #endif /* FMC_PCR_PWAITEN */
  118. #endif /* FMC_Bank3) || defined(FMC_Bank2_3 */
  119. #if defined(FMC_Bank4)
  120. /* --- PCR Register ---*/
  121. /* PCR register clear mask */
  122. #define PCR4_CLEAR_MASK ((uint32_t)(FMC_PCR4_PWAITEN | FMC_PCR4_PBKEN | \
  123. FMC_PCR4_PTYP | FMC_PCR4_PWID | \
  124. FMC_PCR4_ECCEN | FMC_PCR4_TCLR | \
  125. FMC_PCR4_TAR | FMC_PCR4_ECCPS))
  126. /* --- PMEM Register ---*/
  127. /* PMEM register clear mask */
  128. #define PMEM4_CLEAR_MASK ((uint32_t)(FMC_PMEM4_MEMSET4 | FMC_PMEM4_MEMWAIT4 |\
  129. FMC_PMEM4_MEMHOLD4 | FMC_PMEM4_MEMHIZ4))
  130. /* --- PATT Register ---*/
  131. /* PATT register clear mask */
  132. #define PATT4_CLEAR_MASK ((uint32_t)(FMC_PATT4_ATTSET4 | FMC_PATT4_ATTWAIT4 |\
  133. FMC_PATT4_ATTHOLD4 | FMC_PATT4_ATTHIZ4))
  134. /* --- PIO4 Register ---*/
  135. /* PIO4 register clear mask */
  136. #define PIO4_CLEAR_MASK ((uint32_t)(FMC_PIO4_IOSET4 | FMC_PIO4_IOWAIT4 | \
  137. FMC_PIO4_IOHOLD4 | FMC_PIO4_IOHIZ4))
  138. #endif /* FMC_Bank4 */
  139. #if defined(FMC_Bank5_6)
  140. /* --- SDCR Register ---*/
  141. /* SDCR register clear mask */
  142. #define SDCR_CLEAR_MASK ((uint32_t)(FMC_SDCR1_NC | FMC_SDCR1_NR | \
  143. FMC_SDCR1_MWID | FMC_SDCR1_NB | \
  144. FMC_SDCR1_CAS | FMC_SDCR1_WP | \
  145. FMC_SDCR1_SDCLK | FMC_SDCR1_RBURST | \
  146. FMC_SDCR1_RPIPE))
  147. /* --- SDTR Register ---*/
  148. /* SDTR register clear mask */
  149. #define SDTR_CLEAR_MASK ((uint32_t)(FMC_SDTR1_TMRD | FMC_SDTR1_TXSR | \
  150. FMC_SDTR1_TRAS | FMC_SDTR1_TRC | \
  151. FMC_SDTR1_TWR | FMC_SDTR1_TRP | \
  152. FMC_SDTR1_TRCD))
  153. #endif /* FMC_Bank5_6 */
  154. /**
  155. * @}
  156. */
  157. /* Private macro -------------------------------------------------------------*/
  158. /* Private variables ---------------------------------------------------------*/
  159. /* Private function prototypes -----------------------------------------------*/
  160. /* Exported functions --------------------------------------------------------*/
  161. /** @defgroup FMC_LL_Exported_Functions FMC Low Layer Exported Functions
  162. * @{
  163. */
  164. #if defined(FMC_Bank1)
  165. /** @defgroup FMC_LL_Exported_Functions_NORSRAM FMC Low Layer NOR SRAM Exported Functions
  166. * @brief NORSRAM Controller functions
  167. *
  168. @verbatim
  169. ==============================================================================
  170. ##### How to use NORSRAM device driver #####
  171. ==============================================================================
  172. [..]
  173. This driver contains a set of APIs to interface with the FMC NORSRAM banks in order
  174. to run the NORSRAM external devices.
  175. (+) FMC NORSRAM bank reset using the function FMC_NORSRAM_DeInit()
  176. (+) FMC NORSRAM bank control configuration using the function FMC_NORSRAM_Init()
  177. (+) FMC NORSRAM bank timing configuration using the function FMC_NORSRAM_Timing_Init()
  178. (+) FMC NORSRAM bank extended timing configuration using the function
  179. FMC_NORSRAM_Extended_Timing_Init()
  180. (+) FMC NORSRAM bank enable/disable write operation using the functions
  181. FMC_NORSRAM_WriteOperation_Enable()/FMC_NORSRAM_WriteOperation_Disable()
  182. @endverbatim
  183. * @{
  184. */
  185. /** @defgroup FMC_LL_NORSRAM_Exported_Functions_Group1 Initialization and de-initialization functions
  186. * @brief Initialization and Configuration functions
  187. *
  188. @verbatim
  189. ==============================================================================
  190. ##### Initialization and de_initialization functions #####
  191. ==============================================================================
  192. [..]
  193. This section provides functions allowing to:
  194. (+) Initialize and configure the FMC NORSRAM interface
  195. (+) De-initialize the FMC NORSRAM interface
  196. (+) Configure the FMC clock and associated GPIOs
  197. @endverbatim
  198. * @{
  199. */
  200. /**
  201. * @brief Initialize the FMC_NORSRAM device according to the specified
  202. * control parameters in the FMC_NORSRAM_InitTypeDef
  203. * @param Device Pointer to NORSRAM device instance
  204. * @param Init Pointer to NORSRAM Initialization structure
  205. * @retval HAL status
  206. */
  207. HAL_StatusTypeDef FMC_NORSRAM_Init(FMC_NORSRAM_TypeDef *Device,
  208. const FMC_NORSRAM_InitTypeDef *Init)
  209. {
  210. uint32_t flashaccess;
  211. uint32_t btcr_reg;
  212. uint32_t mask;
  213. /* Check the parameters */
  214. assert_param(IS_FMC_NORSRAM_DEVICE(Device));
  215. assert_param(IS_FMC_NORSRAM_BANK(Init->NSBank));
  216. assert_param(IS_FMC_MUX(Init->DataAddressMux));
  217. assert_param(IS_FMC_MEMORY(Init->MemoryType));
  218. assert_param(IS_FMC_NORSRAM_MEMORY_WIDTH(Init->MemoryDataWidth));
  219. assert_param(IS_FMC_BURSTMODE(Init->BurstAccessMode));
  220. assert_param(IS_FMC_WAIT_POLARITY(Init->WaitSignalPolarity));
  221. #if defined(FMC_BCR1_WRAPMOD)
  222. assert_param(IS_FMC_WRAP_MODE(Init->WrapMode));
  223. #endif /* FMC_BCR1_WRAPMOD */
  224. assert_param(IS_FMC_WAIT_SIGNAL_ACTIVE(Init->WaitSignalActive));
  225. assert_param(IS_FMC_WRITE_OPERATION(Init->WriteOperation));
  226. assert_param(IS_FMC_WAITE_SIGNAL(Init->WaitSignal));
  227. assert_param(IS_FMC_EXTENDED_MODE(Init->ExtendedMode));
  228. assert_param(IS_FMC_ASYNWAIT(Init->AsynchronousWait));
  229. assert_param(IS_FMC_WRITE_BURST(Init->WriteBurst));
  230. #if defined(FMC_BCR1_CCLKEN)
  231. assert_param(IS_FMC_CONTINOUS_CLOCK(Init->ContinuousClock));
  232. #endif /* FMC_BCR1_CCLKEN */
  233. #if defined(FMC_BCR1_WFDIS)
  234. assert_param(IS_FMC_WRITE_FIFO(Init->WriteFifo));
  235. #endif /* FMC_BCR1_WFDIS */
  236. assert_param(IS_FMC_PAGESIZE(Init->PageSize));
  237. /* Disable NORSRAM Device */
  238. __FMC_NORSRAM_DISABLE(Device, Init->NSBank);
  239. /* Set NORSRAM device control parameters */
  240. if (Init->MemoryType == FMC_MEMORY_TYPE_NOR)
  241. {
  242. flashaccess = FMC_NORSRAM_FLASH_ACCESS_ENABLE;
  243. }
  244. else
  245. {
  246. flashaccess = FMC_NORSRAM_FLASH_ACCESS_DISABLE;
  247. }
  248. btcr_reg = (flashaccess | \
  249. Init->DataAddressMux | \
  250. Init->MemoryType | \
  251. Init->MemoryDataWidth | \
  252. Init->BurstAccessMode | \
  253. Init->WaitSignalPolarity | \
  254. Init->WaitSignalActive | \
  255. Init->WriteOperation | \
  256. Init->WaitSignal | \
  257. Init->ExtendedMode | \
  258. Init->AsynchronousWait | \
  259. Init->WriteBurst);
  260. #if defined(FMC_BCR1_WRAPMOD)
  261. btcr_reg |= Init->WrapMode;
  262. #endif /* FMC_BCR1_WRAPMOD */
  263. #if defined(FMC_BCR1_CCLKEN)
  264. btcr_reg |= Init->ContinuousClock;
  265. #endif /* FMC_BCR1_CCLKEN */
  266. #if defined(FMC_BCR1_WFDIS)
  267. btcr_reg |= Init->WriteFifo;
  268. #endif /* FMC_BCR1_WFDIS */
  269. btcr_reg |= Init->PageSize;
  270. mask = (FMC_BCR1_MBKEN |
  271. FMC_BCR1_MUXEN |
  272. FMC_BCR1_MTYP |
  273. FMC_BCR1_MWID |
  274. FMC_BCR1_FACCEN |
  275. FMC_BCR1_BURSTEN |
  276. FMC_BCR1_WAITPOL |
  277. FMC_BCR1_WAITCFG |
  278. FMC_BCR1_WREN |
  279. FMC_BCR1_WAITEN |
  280. FMC_BCR1_EXTMOD |
  281. FMC_BCR1_ASYNCWAIT |
  282. FMC_BCR1_CBURSTRW);
  283. #if defined(FMC_BCR1_WRAPMOD)
  284. mask |= FMC_BCR1_WRAPMOD;
  285. #endif /* FMC_BCR1_WRAPMOD */
  286. #if defined(FMC_BCR1_CCLKEN)
  287. mask |= FMC_BCR1_CCLKEN;
  288. #endif /* FMC_BCR1_CCLKEN */
  289. #if defined(FMC_BCR1_WFDIS)
  290. mask |= FMC_BCR1_WFDIS;
  291. #endif /* FMC_BCR1_WFDIS */
  292. mask |= FMC_BCR1_CPSIZE;
  293. MODIFY_REG(Device->BTCR[Init->NSBank], mask, btcr_reg);
  294. #if defined(FMC_BCR1_CCLKEN)
  295. /* Configure synchronous mode when Continuous clock is enabled for bank2..4 */
  296. if ((Init->ContinuousClock == FMC_CONTINUOUS_CLOCK_SYNC_ASYNC) && (Init->NSBank != FMC_NORSRAM_BANK1))
  297. {
  298. MODIFY_REG(Device->BTCR[FMC_NORSRAM_BANK1], FMC_BCR1_CCLKEN, Init->ContinuousClock);
  299. }
  300. #endif /* FMC_BCR1_CCLKEN */
  301. #if defined(FMC_BCR1_WFDIS)
  302. if (Init->NSBank != FMC_NORSRAM_BANK1)
  303. {
  304. /* Configure Write FIFO mode when Write Fifo is enabled for bank2..4 */
  305. SET_BIT(Device->BTCR[FMC_NORSRAM_BANK1], (uint32_t)(Init->WriteFifo));
  306. }
  307. #endif /* FMC_BCR1_WFDIS */
  308. return HAL_OK;
  309. }
  310. /**
  311. * @brief DeInitialize the FMC_NORSRAM peripheral
  312. * @param Device Pointer to NORSRAM device instance
  313. * @param ExDevice Pointer to NORSRAM extended mode device instance
  314. * @param Bank NORSRAM bank number
  315. * @retval HAL status
  316. */
  317. HAL_StatusTypeDef FMC_NORSRAM_DeInit(FMC_NORSRAM_TypeDef *Device,
  318. FMC_NORSRAM_EXTENDED_TypeDef *ExDevice, uint32_t Bank)
  319. {
  320. /* Check the parameters */
  321. assert_param(IS_FMC_NORSRAM_DEVICE(Device));
  322. assert_param(IS_FMC_NORSRAM_EXTENDED_DEVICE(ExDevice));
  323. assert_param(IS_FMC_NORSRAM_BANK(Bank));
  324. /* Disable the FMC_NORSRAM device */
  325. __FMC_NORSRAM_DISABLE(Device, Bank);
  326. /* De-initialize the FMC_NORSRAM device */
  327. /* FMC_NORSRAM_BANK1 */
  328. if (Bank == FMC_NORSRAM_BANK1)
  329. {
  330. Device->BTCR[Bank] = 0x000030DBU;
  331. }
  332. /* FMC_NORSRAM_BANK2, FMC_NORSRAM_BANK3 or FMC_NORSRAM_BANK4 */
  333. else
  334. {
  335. Device->BTCR[Bank] = 0x000030D2U;
  336. }
  337. Device->BTCR[Bank + 1U] = 0x0FFFFFFFU;
  338. ExDevice->BWTR[Bank] = 0x0FFFFFFFU;
  339. return HAL_OK;
  340. }
  341. /**
  342. * @brief Initialize the FMC_NORSRAM Timing according to the specified
  343. * parameters in the FMC_NORSRAM_TimingTypeDef
  344. * @param Device Pointer to NORSRAM device instance
  345. * @param Timing Pointer to NORSRAM Timing structure
  346. * @param Bank NORSRAM bank number
  347. * @retval HAL status
  348. */
  349. HAL_StatusTypeDef FMC_NORSRAM_Timing_Init(FMC_NORSRAM_TypeDef *Device,
  350. const FMC_NORSRAM_TimingTypeDef *Timing, uint32_t Bank)
  351. {
  352. #if defined(FMC_BCR1_CCLKEN)
  353. uint32_t tmpr;
  354. #endif /* FMC_BCR1_CCLKEN */
  355. /* Check the parameters */
  356. assert_param(IS_FMC_NORSRAM_DEVICE(Device));
  357. assert_param(IS_FMC_ADDRESS_SETUP_TIME(Timing->AddressSetupTime));
  358. assert_param(IS_FMC_ADDRESS_HOLD_TIME(Timing->AddressHoldTime));
  359. assert_param(IS_FMC_DATASETUP_TIME(Timing->DataSetupTime));
  360. assert_param(IS_FMC_TURNAROUND_TIME(Timing->BusTurnAroundDuration));
  361. assert_param(IS_FMC_CLK_DIV(Timing->CLKDivision));
  362. assert_param(IS_FMC_DATA_LATENCY(Timing->DataLatency));
  363. assert_param(IS_FMC_ACCESS_MODE(Timing->AccessMode));
  364. assert_param(IS_FMC_NORSRAM_BANK(Bank));
  365. /* Set FMC_NORSRAM device timing parameters */
  366. Device->BTCR[Bank + 1U] =
  367. (Timing->AddressSetupTime << FMC_BTR1_ADDSET_Pos) |
  368. (Timing->AddressHoldTime << FMC_BTR1_ADDHLD_Pos) |
  369. (Timing->DataSetupTime << FMC_BTR1_DATAST_Pos) |
  370. (Timing->BusTurnAroundDuration << FMC_BTR1_BUSTURN_Pos) |
  371. ((Timing->CLKDivision - 1U) << FMC_BTR1_CLKDIV_Pos) |
  372. ((Timing->DataLatency - 2U) << FMC_BTR1_DATLAT_Pos) |
  373. Timing->AccessMode;
  374. #if defined(FMC_BCR1_CCLKEN)
  375. /* Configure Clock division value (in NORSRAM bank 1) when continuous clock is enabled */
  376. if (HAL_IS_BIT_SET(Device->BTCR[FMC_NORSRAM_BANK1], FMC_BCR1_CCLKEN))
  377. {
  378. tmpr = (uint32_t)(Device->BTCR[FMC_NORSRAM_BANK1 + 1U] & ~((0x0FU) << FMC_BTR1_CLKDIV_Pos));
  379. tmpr |= (uint32_t)(((Timing->CLKDivision) - 1U) << FMC_BTR1_CLKDIV_Pos);
  380. MODIFY_REG(Device->BTCR[FMC_NORSRAM_BANK1 + 1U], FMC_BTR1_CLKDIV, tmpr);
  381. }
  382. #endif /* FMC_BCR1_CCLKEN */
  383. return HAL_OK;
  384. }
  385. /**
  386. * @brief Initialize the FMC_NORSRAM Extended mode Timing according to the specified
  387. * parameters in the FMC_NORSRAM_TimingTypeDef
  388. * @param Device Pointer to NORSRAM device instance
  389. * @param Timing Pointer to NORSRAM Timing structure
  390. * @param Bank NORSRAM bank number
  391. * @param ExtendedMode FMC Extended Mode
  392. * This parameter can be one of the following values:
  393. * @arg FMC_EXTENDED_MODE_DISABLE
  394. * @arg FMC_EXTENDED_MODE_ENABLE
  395. * @retval HAL status
  396. */
  397. HAL_StatusTypeDef FMC_NORSRAM_Extended_Timing_Init(FMC_NORSRAM_EXTENDED_TypeDef *Device,
  398. const FMC_NORSRAM_TimingTypeDef *Timing, uint32_t Bank,
  399. uint32_t ExtendedMode)
  400. {
  401. /* Check the parameters */
  402. assert_param(IS_FMC_EXTENDED_MODE(ExtendedMode));
  403. /* Set NORSRAM device timing register for write configuration, if extended mode is used */
  404. if (ExtendedMode == FMC_EXTENDED_MODE_ENABLE)
  405. {
  406. /* Check the parameters */
  407. assert_param(IS_FMC_NORSRAM_EXTENDED_DEVICE(Device));
  408. assert_param(IS_FMC_ADDRESS_SETUP_TIME(Timing->AddressSetupTime));
  409. assert_param(IS_FMC_ADDRESS_HOLD_TIME(Timing->AddressHoldTime));
  410. assert_param(IS_FMC_DATASETUP_TIME(Timing->DataSetupTime));
  411. assert_param(IS_FMC_TURNAROUND_TIME(Timing->BusTurnAroundDuration));
  412. assert_param(IS_FMC_ACCESS_MODE(Timing->AccessMode));
  413. assert_param(IS_FMC_NORSRAM_BANK(Bank));
  414. /* Set NORSRAM device timing register for write configuration, if extended mode is used */
  415. MODIFY_REG(Device->BWTR[Bank], BWTR_CLEAR_MASK, (Timing->AddressSetupTime |
  416. ((Timing->AddressHoldTime) << FMC_BWTR1_ADDHLD_Pos) |
  417. ((Timing->DataSetupTime) << FMC_BWTR1_DATAST_Pos) |
  418. Timing->AccessMode |
  419. ((Timing->BusTurnAroundDuration) << FMC_BWTR1_BUSTURN_Pos)));
  420. }
  421. else
  422. {
  423. Device->BWTR[Bank] = 0x0FFFFFFFU;
  424. }
  425. return HAL_OK;
  426. }
  427. /**
  428. * @}
  429. */
  430. /** @addtogroup FMC_LL_NORSRAM_Private_Functions_Group2
  431. * @brief management functions
  432. *
  433. @verbatim
  434. ==============================================================================
  435. ##### FMC_NORSRAM Control functions #####
  436. ==============================================================================
  437. [..]
  438. This subsection provides a set of functions allowing to control dynamically
  439. the FMC NORSRAM interface.
  440. @endverbatim
  441. * @{
  442. */
  443. /**
  444. * @brief Enables dynamically FMC_NORSRAM write operation.
  445. * @param Device Pointer to NORSRAM device instance
  446. * @param Bank NORSRAM bank number
  447. * @retval HAL status
  448. */
  449. HAL_StatusTypeDef FMC_NORSRAM_WriteOperation_Enable(FMC_NORSRAM_TypeDef *Device, uint32_t Bank)
  450. {
  451. /* Check the parameters */
  452. assert_param(IS_FMC_NORSRAM_DEVICE(Device));
  453. assert_param(IS_FMC_NORSRAM_BANK(Bank));
  454. /* Enable write operation */
  455. SET_BIT(Device->BTCR[Bank], FMC_WRITE_OPERATION_ENABLE);
  456. return HAL_OK;
  457. }
  458. /**
  459. * @brief Disables dynamically FMC_NORSRAM write operation.
  460. * @param Device Pointer to NORSRAM device instance
  461. * @param Bank NORSRAM bank number
  462. * @retval HAL status
  463. */
  464. HAL_StatusTypeDef FMC_NORSRAM_WriteOperation_Disable(FMC_NORSRAM_TypeDef *Device, uint32_t Bank)
  465. {
  466. /* Check the parameters */
  467. assert_param(IS_FMC_NORSRAM_DEVICE(Device));
  468. assert_param(IS_FMC_NORSRAM_BANK(Bank));
  469. /* Disable write operation */
  470. CLEAR_BIT(Device->BTCR[Bank], FMC_WRITE_OPERATION_ENABLE);
  471. return HAL_OK;
  472. }
  473. /**
  474. * @}
  475. */
  476. /**
  477. * @}
  478. */
  479. #endif /* FMC_Bank1 */
  480. #if defined(FMC_Bank3) || defined(FMC_Bank2_3)
  481. /** @defgroup FMC_LL_Exported_Functions_NAND FMC Low Layer NAND Exported Functions
  482. * @brief NAND Controller functions
  483. *
  484. @verbatim
  485. ==============================================================================
  486. ##### How to use NAND device driver #####
  487. ==============================================================================
  488. [..]
  489. This driver contains a set of APIs to interface with the FMC NAND banks in order
  490. to run the NAND external devices.
  491. (+) FMC NAND bank reset using the function FMC_NAND_DeInit()
  492. (+) FMC NAND bank control configuration using the function FMC_NAND_Init()
  493. (+) FMC NAND bank common space timing configuration using the function
  494. FMC_NAND_CommonSpace_Timing_Init()
  495. (+) FMC NAND bank attribute space timing configuration using the function
  496. FMC_NAND_AttributeSpace_Timing_Init()
  497. (+) FMC NAND bank enable/disable ECC correction feature using the functions
  498. FMC_NAND_ECC_Enable()/FMC_NAND_ECC_Disable()
  499. (+) FMC NAND bank get ECC correction code using the function FMC_NAND_GetECC()
  500. @endverbatim
  501. * @{
  502. */
  503. /** @defgroup FMC_LL_NAND_Exported_Functions_Group1 Initialization and de-initialization functions
  504. * @brief Initialization and Configuration functions
  505. *
  506. @verbatim
  507. ==============================================================================
  508. ##### Initialization and de_initialization functions #####
  509. ==============================================================================
  510. [..]
  511. This section provides functions allowing to:
  512. (+) Initialize and configure the FMC NAND interface
  513. (+) De-initialize the FMC NAND interface
  514. (+) Configure the FMC clock and associated GPIOs
  515. @endverbatim
  516. * @{
  517. */
  518. /**
  519. * @brief Initializes the FMC_NAND device according to the specified
  520. * control parameters in the FMC_NAND_HandleTypeDef
  521. * @param Device Pointer to NAND device instance
  522. * @param Init Pointer to NAND Initialization structure
  523. * @retval HAL status
  524. */
  525. HAL_StatusTypeDef FMC_NAND_Init(FMC_NAND_TypeDef *Device, const FMC_NAND_InitTypeDef *Init)
  526. {
  527. /* Check the parameters */
  528. assert_param(IS_FMC_NAND_DEVICE(Device));
  529. assert_param(IS_FMC_NAND_BANK(Init->NandBank));
  530. assert_param(IS_FMC_WAIT_FEATURE(Init->Waitfeature));
  531. assert_param(IS_FMC_NAND_MEMORY_WIDTH(Init->MemoryDataWidth));
  532. assert_param(IS_FMC_ECC_STATE(Init->EccComputation));
  533. assert_param(IS_FMC_ECCPAGE_SIZE(Init->ECCPageSize));
  534. assert_param(IS_FMC_TCLR_TIME(Init->TCLRSetupTime));
  535. assert_param(IS_FMC_TAR_TIME(Init->TARSetupTime));
  536. #if defined(FMC_Bank2_3)
  537. /* Set NAND device control parameters */
  538. if (Init->NandBank == FMC_NAND_BANK2)
  539. {
  540. /* NAND bank 2 registers configuration */
  541. MODIFY_REG(Device->PCR2, PCR_CLEAR_MASK, (Init->Waitfeature |
  542. FMC_PCR_MEMORY_TYPE_NAND |
  543. Init->MemoryDataWidth |
  544. Init->EccComputation |
  545. Init->ECCPageSize |
  546. ((Init->TCLRSetupTime) << FMC_PCR2_TCLR_Pos) |
  547. ((Init->TARSetupTime) << FMC_PCR2_TAR_Pos)));
  548. }
  549. else
  550. {
  551. /* NAND bank 3 registers configuration */
  552. MODIFY_REG(Device->PCR3, PCR_CLEAR_MASK, (Init->Waitfeature |
  553. FMC_PCR_MEMORY_TYPE_NAND |
  554. Init->MemoryDataWidth |
  555. Init->EccComputation |
  556. Init->ECCPageSize |
  557. ((Init->TCLRSetupTime) << FMC_PCR2_TCLR_Pos) |
  558. ((Init->TARSetupTime) << FMC_PCR2_TAR_Pos)));
  559. }
  560. #else
  561. /* NAND bank 3 registers configuration */
  562. MODIFY_REG(Device->PCR, PCR_CLEAR_MASK, (Init->Waitfeature |
  563. FMC_PCR_MEMORY_TYPE_NAND |
  564. Init->MemoryDataWidth |
  565. Init->EccComputation |
  566. Init->ECCPageSize |
  567. ((Init->TCLRSetupTime) << FMC_PCR_TCLR_Pos) |
  568. ((Init->TARSetupTime) << FMC_PCR_TAR_Pos)));
  569. #endif /* FMC_Bank2_3 */
  570. return HAL_OK;
  571. }
  572. /**
  573. * @brief Initializes the FMC_NAND Common space Timing according to the specified
  574. * parameters in the FMC_NAND_PCC_TimingTypeDef
  575. * @param Device Pointer to NAND device instance
  576. * @param Timing Pointer to NAND timing structure
  577. * @param Bank NAND bank number
  578. * @retval HAL status
  579. */
  580. HAL_StatusTypeDef FMC_NAND_CommonSpace_Timing_Init(FMC_NAND_TypeDef *Device,
  581. const FMC_NAND_PCC_TimingTypeDef *Timing, uint32_t Bank)
  582. {
  583. /* Check the parameters */
  584. assert_param(IS_FMC_NAND_DEVICE(Device));
  585. assert_param(IS_FMC_SETUP_TIME(Timing->SetupTime));
  586. assert_param(IS_FMC_WAIT_TIME(Timing->WaitSetupTime));
  587. assert_param(IS_FMC_HOLD_TIME(Timing->HoldSetupTime));
  588. assert_param(IS_FMC_HIZ_TIME(Timing->HiZSetupTime));
  589. assert_param(IS_FMC_NAND_BANK(Bank));
  590. #if defined(FMC_Bank2_3)
  591. /* Set FMC_NAND device timing parameters */
  592. if (Bank == FMC_NAND_BANK2)
  593. {
  594. /* NAND bank 2 registers configuration */
  595. WRITE_REG(Device->PMEM2, (Timing->SetupTime |
  596. ((Timing->WaitSetupTime) << FMC_PMEM2_MEMWAIT2_Pos) |
  597. ((Timing->HoldSetupTime) << FMC_PMEM2_MEMHOLD2_Pos) |
  598. ((Timing->HiZSetupTime) << FMC_PMEM2_MEMHIZ2_Pos)));
  599. }
  600. else
  601. {
  602. /* NAND bank 3 registers configuration */
  603. WRITE_REG(Device->PMEM3, (Timing->SetupTime |
  604. ((Timing->WaitSetupTime) << FMC_PMEM2_MEMWAIT2_Pos) |
  605. ((Timing->HoldSetupTime) << FMC_PMEM2_MEMHOLD2_Pos) |
  606. ((Timing->HiZSetupTime) << FMC_PMEM2_MEMHIZ2_Pos)));
  607. }
  608. #else
  609. /* Prevent unused argument(s) compilation warning if no assert_param check */
  610. UNUSED(Bank);
  611. /* NAND bank 3 registers configuration */
  612. Device->PMEM = (Timing->SetupTime |
  613. ((Timing->WaitSetupTime) << FMC_PMEM_MEMWAIT2_Pos) |
  614. ((Timing->HoldSetupTime) << FMC_PMEM_MEMHOLD2_Pos) |
  615. ((Timing->HiZSetupTime) << FMC_PMEM_MEMHIZ2_Pos));
  616. #endif /* FMC_Bank2_3 */
  617. return HAL_OK;
  618. }
  619. /**
  620. * @brief Initializes the FMC_NAND Attribute space Timing according to the specified
  621. * parameters in the FMC_NAND_PCC_TimingTypeDef
  622. * @param Device Pointer to NAND device instance
  623. * @param Timing Pointer to NAND timing structure
  624. * @param Bank NAND bank number
  625. * @retval HAL status
  626. */
  627. HAL_StatusTypeDef FMC_NAND_AttributeSpace_Timing_Init(FMC_NAND_TypeDef *Device,
  628. const FMC_NAND_PCC_TimingTypeDef *Timing, uint32_t Bank)
  629. {
  630. /* Check the parameters */
  631. assert_param(IS_FMC_NAND_DEVICE(Device));
  632. assert_param(IS_FMC_SETUP_TIME(Timing->SetupTime));
  633. assert_param(IS_FMC_WAIT_TIME(Timing->WaitSetupTime));
  634. assert_param(IS_FMC_HOLD_TIME(Timing->HoldSetupTime));
  635. assert_param(IS_FMC_HIZ_TIME(Timing->HiZSetupTime));
  636. assert_param(IS_FMC_NAND_BANK(Bank));
  637. #if defined(FMC_Bank2_3)
  638. /* Set FMC_NAND device timing parameters */
  639. if (Bank == FMC_NAND_BANK2)
  640. {
  641. /* NAND bank 2 registers configuration */
  642. WRITE_REG(Device->PATT2, (Timing->SetupTime |
  643. ((Timing->WaitSetupTime) << FMC_PATT2_ATTWAIT2_Pos) |
  644. ((Timing->HoldSetupTime) << FMC_PATT2_ATTHOLD2_Pos) |
  645. ((Timing->HiZSetupTime) << FMC_PATT2_ATTHIZ2_Pos)));
  646. }
  647. else
  648. {
  649. /* NAND bank 3 registers configuration */
  650. WRITE_REG(Device->PATT3, (Timing->SetupTime |
  651. ((Timing->WaitSetupTime) << FMC_PATT2_ATTWAIT2_Pos) |
  652. ((Timing->HoldSetupTime) << FMC_PATT2_ATTHOLD2_Pos) |
  653. ((Timing->HiZSetupTime) << FMC_PATT2_ATTHIZ2_Pos)));
  654. }
  655. #else
  656. /* Prevent unused argument(s) compilation warning if no assert_param check */
  657. UNUSED(Bank);
  658. /* NAND bank 3 registers configuration */
  659. Device->PATT = (Timing->SetupTime |
  660. ((Timing->WaitSetupTime) << FMC_PATT_ATTWAIT2_Pos) |
  661. ((Timing->HoldSetupTime) << FMC_PATT_ATTHOLD2_Pos) |
  662. ((Timing->HiZSetupTime) << FMC_PATT_ATTHIZ2_Pos));
  663. #endif /* FMC_Bank2_3 */
  664. return HAL_OK;
  665. }
  666. /**
  667. * @brief DeInitializes the FMC_NAND device
  668. * @param Device Pointer to NAND device instance
  669. * @param Bank NAND bank number
  670. * @retval HAL status
  671. */
  672. HAL_StatusTypeDef FMC_NAND_DeInit(FMC_NAND_TypeDef *Device, uint32_t Bank)
  673. {
  674. /* Check the parameters */
  675. assert_param(IS_FMC_NAND_DEVICE(Device));
  676. assert_param(IS_FMC_NAND_BANK(Bank));
  677. /* Disable the NAND Bank */
  678. __FMC_NAND_DISABLE(Device, Bank);
  679. /* De-initialize the NAND Bank */
  680. #if defined(FMC_Bank2_3)
  681. if (Bank == FMC_NAND_BANK2)
  682. {
  683. /* Set the FMC_NAND_BANK2 registers to their reset values */
  684. WRITE_REG(Device->PCR2, 0x00000018U);
  685. WRITE_REG(Device->SR2, 0x00000040U);
  686. WRITE_REG(Device->PMEM2, 0xFCFCFCFCU);
  687. WRITE_REG(Device->PATT2, 0xFCFCFCFCU);
  688. }
  689. /* FMC_Bank3_NAND */
  690. else
  691. {
  692. /* Set the FMC_NAND_BANK3 registers to their reset values */
  693. WRITE_REG(Device->PCR3, 0x00000018U);
  694. WRITE_REG(Device->SR3, 0x00000040U);
  695. WRITE_REG(Device->PMEM3, 0xFCFCFCFCU);
  696. WRITE_REG(Device->PATT3, 0xFCFCFCFCU);
  697. }
  698. #else
  699. /* Prevent unused argument(s) compilation warning if no assert_param check */
  700. UNUSED(Bank);
  701. /* Set the FMC_NAND_BANK3 registers to their reset values */
  702. WRITE_REG(Device->PCR, 0x00000018U);
  703. WRITE_REG(Device->SR, 0x00000040U);
  704. WRITE_REG(Device->PMEM, 0xFCFCFCFCU);
  705. WRITE_REG(Device->PATT, 0xFCFCFCFCU);
  706. #endif /* FMC_Bank2_3 */
  707. return HAL_OK;
  708. }
  709. /**
  710. * @}
  711. */
  712. /** @defgroup HAL_FMC_NAND_Group2 Peripheral Control functions
  713. * @brief management functions
  714. *
  715. @verbatim
  716. ==============================================================================
  717. ##### FMC_NAND Control functions #####
  718. ==============================================================================
  719. [..]
  720. This subsection provides a set of functions allowing to control dynamically
  721. the FMC NAND interface.
  722. @endverbatim
  723. * @{
  724. */
  725. /**
  726. * @brief Enables dynamically FMC_NAND ECC feature.
  727. * @param Device Pointer to NAND device instance
  728. * @param Bank NAND bank number
  729. * @retval HAL status
  730. */
  731. HAL_StatusTypeDef FMC_NAND_ECC_Enable(FMC_NAND_TypeDef *Device, uint32_t Bank)
  732. {
  733. /* Check the parameters */
  734. assert_param(IS_FMC_NAND_DEVICE(Device));
  735. assert_param(IS_FMC_NAND_BANK(Bank));
  736. /* Enable ECC feature */
  737. #if defined(FMC_Bank2_3)
  738. if (Bank == FMC_NAND_BANK2)
  739. {
  740. SET_BIT(Device->PCR2, FMC_PCR2_ECCEN);
  741. }
  742. else
  743. {
  744. SET_BIT(Device->PCR3, FMC_PCR2_ECCEN);
  745. }
  746. #else
  747. /* Prevent unused argument(s) compilation warning if no assert_param check */
  748. UNUSED(Bank);
  749. SET_BIT(Device->PCR, FMC_PCR_ECCEN);
  750. #endif /* FMC_Bank2_3 */
  751. return HAL_OK;
  752. }
  753. /**
  754. * @brief Disables dynamically FMC_NAND ECC feature.
  755. * @param Device Pointer to NAND device instance
  756. * @param Bank NAND bank number
  757. * @retval HAL status
  758. */
  759. HAL_StatusTypeDef FMC_NAND_ECC_Disable(FMC_NAND_TypeDef *Device, uint32_t Bank)
  760. {
  761. /* Check the parameters */
  762. assert_param(IS_FMC_NAND_DEVICE(Device));
  763. assert_param(IS_FMC_NAND_BANK(Bank));
  764. /* Disable ECC feature */
  765. #if defined(FMC_Bank2_3)
  766. if (Bank == FMC_NAND_BANK2)
  767. {
  768. CLEAR_BIT(Device->PCR2, FMC_PCR2_ECCEN);
  769. }
  770. else
  771. {
  772. CLEAR_BIT(Device->PCR3, FMC_PCR2_ECCEN);
  773. }
  774. #else
  775. /* Prevent unused argument(s) compilation warning if no assert_param check */
  776. UNUSED(Bank);
  777. CLEAR_BIT(Device->PCR, FMC_PCR_ECCEN);
  778. #endif /* FMC_Bank2_3 */
  779. return HAL_OK;
  780. }
  781. /**
  782. * @brief Disables dynamically FMC_NAND ECC feature.
  783. * @param Device Pointer to NAND device instance
  784. * @param ECCval Pointer to ECC value
  785. * @param Bank NAND bank number
  786. * @param Timeout Timeout wait value
  787. * @retval HAL status
  788. */
  789. HAL_StatusTypeDef FMC_NAND_GetECC(const FMC_NAND_TypeDef *Device, uint32_t *ECCval, uint32_t Bank,
  790. uint32_t Timeout)
  791. {
  792. uint32_t tickstart;
  793. /* Check the parameters */
  794. assert_param(IS_FMC_NAND_DEVICE(Device));
  795. assert_param(IS_FMC_NAND_BANK(Bank));
  796. /* Get tick */
  797. tickstart = HAL_GetTick();
  798. /* Wait until FIFO is empty */
  799. while (__FMC_NAND_GET_FLAG(Device, Bank, FMC_FLAG_FEMPT) == RESET)
  800. {
  801. /* Check for the Timeout */
  802. if (Timeout != HAL_MAX_DELAY)
  803. {
  804. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  805. {
  806. return HAL_TIMEOUT;
  807. }
  808. }
  809. }
  810. #if defined(FMC_Bank2_3)
  811. if (Bank == FMC_NAND_BANK2)
  812. {
  813. /* Get the ECCR2 register value */
  814. *ECCval = (uint32_t)Device->ECCR2;
  815. }
  816. else
  817. {
  818. /* Get the ECCR3 register value */
  819. *ECCval = (uint32_t)Device->ECCR3;
  820. }
  821. #else
  822. /* Prevent unused argument(s) compilation warning if no assert_param check */
  823. UNUSED(Bank);
  824. /* Get the ECCR register value */
  825. *ECCval = (uint32_t)Device->ECCR;
  826. #endif /* FMC_Bank2_3 */
  827. return HAL_OK;
  828. }
  829. /**
  830. * @}
  831. */
  832. #endif /* FMC_Bank3) || defined(FMC_Bank2_3 */
  833. #if defined(FMC_Bank4)
  834. /** @addtogroup FMC_LL_PCCARD
  835. * @brief PCCARD Controller functions
  836. *
  837. @verbatim
  838. ==============================================================================
  839. ##### How to use PCCARD device driver #####
  840. ==============================================================================
  841. [..]
  842. This driver contains a set of APIs to interface with the FMC PCCARD bank in order
  843. to run the PCCARD/compact flash external devices.
  844. (+) FMC PCCARD bank reset using the function FMC_PCCARD_DeInit()
  845. (+) FMC PCCARD bank control configuration using the function FMC_PCCARD_Init()
  846. (+) FMC PCCARD bank common space timing configuration using the function
  847. FMC_PCCARD_CommonSpace_Timing_Init()
  848. (+) FMC PCCARD bank attribute space timing configuration using the function
  849. FMC_PCCARD_AttributeSpace_Timing_Init()
  850. (+) FMC PCCARD bank IO space timing configuration using the function
  851. FMC_PCCARD_IOSpace_Timing_Init()
  852. @endverbatim
  853. * @{
  854. */
  855. /** @addtogroup FMC_LL_PCCARD_Private_Functions_Group1
  856. * @brief Initialization and Configuration functions
  857. *
  858. @verbatim
  859. ==============================================================================
  860. ##### Initialization and de_initialization functions #####
  861. ==============================================================================
  862. [..]
  863. This section provides functions allowing to:
  864. (+) Initialize and configure the FMC PCCARD interface
  865. (+) De-initialize the FMC PCCARD interface
  866. (+) Configure the FMC clock and associated GPIOs
  867. @endverbatim
  868. * @{
  869. */
  870. /**
  871. * @brief Initializes the FMC_PCCARD device according to the specified
  872. * control parameters in the FMC_PCCARD_HandleTypeDef
  873. * @param Device Pointer to PCCARD device instance
  874. * @param Init Pointer to PCCARD Initialization structure
  875. * @retval HAL status
  876. */
  877. HAL_StatusTypeDef FMC_PCCARD_Init(FMC_PCCARD_TypeDef *Device, const FMC_PCCARD_InitTypeDef *Init)
  878. {
  879. /* Check the parameters */
  880. assert_param(IS_FMC_PCCARD_DEVICE(Device));
  881. #if defined(FMC_Bank3) || defined(FMC_Bank2_3)
  882. assert_param(IS_FMC_WAIT_FEATURE(Init->Waitfeature));
  883. assert_param(IS_FMC_TCLR_TIME(Init->TCLRSetupTime));
  884. assert_param(IS_FMC_TAR_TIME(Init->TARSetupTime));
  885. #endif /* FMC_Bank3) || defined(FMC_Bank2_3 */
  886. /* Set FMC_PCCARD device control parameters */
  887. MODIFY_REG(Device->PCR4,
  888. (FMC_PCR4_PTYP |
  889. FMC_PCR4_PWAITEN |
  890. FMC_PCR4_PWID |
  891. FMC_PCR4_TCLR |
  892. FMC_PCR4_TAR),
  893. (FMC_PCR_MEMORY_TYPE_PCCARD |
  894. Init->Waitfeature |
  895. FMC_NAND_PCC_MEM_BUS_WIDTH_16 |
  896. (Init->TCLRSetupTime << FMC_PCR4_TCLR_Pos) |
  897. (Init->TARSetupTime << FMC_PCR4_TAR_Pos)));
  898. return HAL_OK;
  899. }
  900. /**
  901. * @brief Initializes the FMC_PCCARD Common space Timing according to the specified
  902. * parameters in the FMC_NAND_PCC_TimingTypeDef
  903. * @param Device Pointer to PCCARD device instance
  904. * @param Timing Pointer to PCCARD timing structure
  905. * @retval HAL status
  906. */
  907. HAL_StatusTypeDef FMC_PCCARD_CommonSpace_Timing_Init(FMC_PCCARD_TypeDef *Device,
  908. const FMC_NAND_PCC_TimingTypeDef *Timing)
  909. {
  910. /* Check the parameters */
  911. assert_param(IS_FMC_PCCARD_DEVICE(Device));
  912. #if defined(FMC_Bank3) || defined(FMC_Bank2_3)
  913. assert_param(IS_FMC_SETUP_TIME(Timing->SetupTime));
  914. assert_param(IS_FMC_WAIT_TIME(Timing->WaitSetupTime));
  915. assert_param(IS_FMC_HOLD_TIME(Timing->HoldSetupTime));
  916. assert_param(IS_FMC_HIZ_TIME(Timing->HiZSetupTime));
  917. #endif /* FMC_Bank3) || defined(FMC_Bank2_3 */
  918. /* Set PCCARD timing parameters */
  919. WRITE_REG(Device->PMEM4, (Timing->SetupTime |
  920. ((Timing->WaitSetupTime) << FMC_PMEM4_MEMWAIT4_Pos) |
  921. ((Timing->HoldSetupTime) << FMC_PMEM4_MEMHOLD4_Pos) |
  922. ((Timing->HiZSetupTime) << FMC_PMEM4_MEMHIZ4_Pos)));
  923. return HAL_OK;
  924. }
  925. /**
  926. * @brief Initializes the FMC_PCCARD Attribute space Timing according to the specified
  927. * parameters in the FMC_NAND_PCC_TimingTypeDef
  928. * @param Device Pointer to PCCARD device instance
  929. * @param Timing Pointer to PCCARD timing structure
  930. * @retval HAL status
  931. */
  932. HAL_StatusTypeDef FMC_PCCARD_AttributeSpace_Timing_Init(FMC_PCCARD_TypeDef *Device,
  933. const FMC_NAND_PCC_TimingTypeDef *Timing)
  934. {
  935. /* Check the parameters */
  936. assert_param(IS_FMC_PCCARD_DEVICE(Device));
  937. #if defined(FMC_Bank3) || defined(FMC_Bank2_3)
  938. assert_param(IS_FMC_SETUP_TIME(Timing->SetupTime));
  939. assert_param(IS_FMC_WAIT_TIME(Timing->WaitSetupTime));
  940. assert_param(IS_FMC_HOLD_TIME(Timing->HoldSetupTime));
  941. assert_param(IS_FMC_HIZ_TIME(Timing->HiZSetupTime));
  942. #endif /* FMC_Bank3) || defined(FMC_Bank2_3 */
  943. /* Set PCCARD timing parameters */
  944. WRITE_REG(Device->PATT4, (Timing->SetupTime |
  945. ((Timing->WaitSetupTime) << FMC_PATT4_ATTWAIT4_Pos) |
  946. ((Timing->HoldSetupTime) << FMC_PATT4_ATTHOLD4_Pos) |
  947. ((Timing->HiZSetupTime) << FMC_PATT4_ATTHIZ4_Pos)));
  948. return HAL_OK;
  949. }
  950. /**
  951. * @brief Initializes the FMC_PCCARD IO space Timing according to the specified
  952. * parameters in the FMC_NAND_PCC_TimingTypeDef
  953. * @param Device Pointer to PCCARD device instance
  954. * @param Timing Pointer to PCCARD timing structure
  955. * @retval HAL status
  956. */
  957. HAL_StatusTypeDef FMC_PCCARD_IOSpace_Timing_Init(FMC_PCCARD_TypeDef *Device,
  958. const FMC_NAND_PCC_TimingTypeDef *Timing)
  959. {
  960. /* Check the parameters */
  961. assert_param(IS_FMC_PCCARD_DEVICE(Device));
  962. #if defined(FMC_Bank3) || defined(FMC_Bank2_3)
  963. assert_param(IS_FMC_SETUP_TIME(Timing->SetupTime));
  964. assert_param(IS_FMC_WAIT_TIME(Timing->WaitSetupTime));
  965. assert_param(IS_FMC_HOLD_TIME(Timing->HoldSetupTime));
  966. assert_param(IS_FMC_HIZ_TIME(Timing->HiZSetupTime));
  967. #endif /* FMC_Bank3) || defined(FMC_Bank2_3 */
  968. /* Set FMC_PCCARD device timing parameters */
  969. WRITE_REG(Device->PIO4, (Timing->SetupTime |
  970. (Timing->WaitSetupTime << FMC_PIO4_IOWAIT4_Pos) |
  971. (Timing->HoldSetupTime << FMC_PIO4_IOHOLD4_Pos) |
  972. (Timing->HiZSetupTime << FMC_PIO4_IOHIZ4_Pos)));
  973. return HAL_OK;
  974. }
  975. /**
  976. * @brief DeInitializes the FMC_PCCARD device
  977. * @param Device Pointer to PCCARD device instance
  978. * @retval HAL status
  979. */
  980. HAL_StatusTypeDef FMC_PCCARD_DeInit(FMC_PCCARD_TypeDef *Device)
  981. {
  982. /* Check the parameters */
  983. assert_param(IS_FMC_PCCARD_DEVICE(Device));
  984. /* Disable the FMC_PCCARD device */
  985. __FMC_PCCARD_DISABLE(Device);
  986. /* De-initialize the FMC_PCCARD device */
  987. Device->PCR4 = 0x00000018U;
  988. Device->SR4 = 0x00000040U;
  989. Device->PMEM4 = 0xFCFCFCFCU;
  990. Device->PATT4 = 0xFCFCFCFCU;
  991. Device->PIO4 = 0xFCFCFCFCU;
  992. return HAL_OK;
  993. }
  994. /**
  995. * @}
  996. */
  997. #endif /* FMC_Bank4 */
  998. #if defined(FMC_Bank5_6)
  999. /** @defgroup FMC_LL_SDRAM
  1000. * @brief SDRAM Controller functions
  1001. *
  1002. @verbatim
  1003. ==============================================================================
  1004. ##### How to use SDRAM device driver #####
  1005. ==============================================================================
  1006. [..]
  1007. This driver contains a set of APIs to interface with the FMC SDRAM banks in order
  1008. to run the SDRAM external devices.
  1009. (+) FMC SDRAM bank reset using the function FMC_SDRAM_DeInit()
  1010. (+) FMC SDRAM bank control configuration using the function FMC_SDRAM_Init()
  1011. (+) FMC SDRAM bank timing configuration using the function FMC_SDRAM_Timing_Init()
  1012. (+) FMC SDRAM bank enable/disable write operation using the functions
  1013. FMC_SDRAM_WriteOperation_Enable()/FMC_SDRAM_WriteOperation_Disable()
  1014. (+) FMC SDRAM bank send command using the function FMC_SDRAM_SendCommand()
  1015. @endverbatim
  1016. * @{
  1017. */
  1018. /** @addtogroup FMC_LL_SDRAM_Private_Functions_Group1
  1019. * @brief Initialization and Configuration functions
  1020. *
  1021. @verbatim
  1022. ==============================================================================
  1023. ##### Initialization and de_initialization functions #####
  1024. ==============================================================================
  1025. [..]
  1026. This section provides functions allowing to:
  1027. (+) Initialize and configure the FMC SDRAM interface
  1028. (+) De-initialize the FMC SDRAM interface
  1029. (+) Configure the FMC clock and associated GPIOs
  1030. @endverbatim
  1031. * @{
  1032. */
  1033. /**
  1034. * @brief Initializes the FMC_SDRAM device according to the specified
  1035. * control parameters in the FMC_SDRAM_InitTypeDef
  1036. * @param Device Pointer to SDRAM device instance
  1037. * @param Init Pointer to SDRAM Initialization structure
  1038. * @retval HAL status
  1039. */
  1040. HAL_StatusTypeDef FMC_SDRAM_Init(FMC_SDRAM_TypeDef *Device, const FMC_SDRAM_InitTypeDef *Init)
  1041. {
  1042. /* Check the parameters */
  1043. assert_param(IS_FMC_SDRAM_DEVICE(Device));
  1044. assert_param(IS_FMC_SDRAM_BANK(Init->SDBank));
  1045. assert_param(IS_FMC_COLUMNBITS_NUMBER(Init->ColumnBitsNumber));
  1046. assert_param(IS_FMC_ROWBITS_NUMBER(Init->RowBitsNumber));
  1047. assert_param(IS_FMC_SDMEMORY_WIDTH(Init->MemoryDataWidth));
  1048. assert_param(IS_FMC_INTERNALBANK_NUMBER(Init->InternalBankNumber));
  1049. assert_param(IS_FMC_CAS_LATENCY(Init->CASLatency));
  1050. assert_param(IS_FMC_WRITE_PROTECTION(Init->WriteProtection));
  1051. assert_param(IS_FMC_SDCLOCK_PERIOD(Init->SDClockPeriod));
  1052. assert_param(IS_FMC_READ_BURST(Init->ReadBurst));
  1053. assert_param(IS_FMC_READPIPE_DELAY(Init->ReadPipeDelay));
  1054. /* Set SDRAM bank configuration parameters */
  1055. if (Init->SDBank == FMC_SDRAM_BANK1)
  1056. {
  1057. MODIFY_REG(Device->SDCR[FMC_SDRAM_BANK1],
  1058. SDCR_CLEAR_MASK,
  1059. (Init->ColumnBitsNumber |
  1060. Init->RowBitsNumber |
  1061. Init->MemoryDataWidth |
  1062. Init->InternalBankNumber |
  1063. Init->CASLatency |
  1064. Init->WriteProtection |
  1065. Init->SDClockPeriod |
  1066. Init->ReadBurst |
  1067. Init->ReadPipeDelay));
  1068. }
  1069. else /* FMC_Bank2_SDRAM */
  1070. {
  1071. MODIFY_REG(Device->SDCR[FMC_SDRAM_BANK1],
  1072. FMC_SDCR1_SDCLK |
  1073. FMC_SDCR1_RBURST |
  1074. FMC_SDCR1_RPIPE,
  1075. (Init->SDClockPeriod |
  1076. Init->ReadBurst |
  1077. Init->ReadPipeDelay));
  1078. MODIFY_REG(Device->SDCR[FMC_SDRAM_BANK2],
  1079. SDCR_CLEAR_MASK,
  1080. (Init->ColumnBitsNumber |
  1081. Init->RowBitsNumber |
  1082. Init->MemoryDataWidth |
  1083. Init->InternalBankNumber |
  1084. Init->CASLatency |
  1085. Init->WriteProtection));
  1086. }
  1087. return HAL_OK;
  1088. }
  1089. /**
  1090. * @brief Initializes the FMC_SDRAM device timing according to the specified
  1091. * parameters in the FMC_SDRAM_TimingTypeDef
  1092. * @param Device Pointer to SDRAM device instance
  1093. * @param Timing Pointer to SDRAM Timing structure
  1094. * @param Bank SDRAM bank number
  1095. * @retval HAL status
  1096. */
  1097. HAL_StatusTypeDef FMC_SDRAM_Timing_Init(FMC_SDRAM_TypeDef *Device,
  1098. const FMC_SDRAM_TimingTypeDef *Timing, uint32_t Bank)
  1099. {
  1100. /* Check the parameters */
  1101. assert_param(IS_FMC_SDRAM_DEVICE(Device));
  1102. assert_param(IS_FMC_LOADTOACTIVE_DELAY(Timing->LoadToActiveDelay));
  1103. assert_param(IS_FMC_EXITSELFREFRESH_DELAY(Timing->ExitSelfRefreshDelay));
  1104. assert_param(IS_FMC_SELFREFRESH_TIME(Timing->SelfRefreshTime));
  1105. assert_param(IS_FMC_ROWCYCLE_DELAY(Timing->RowCycleDelay));
  1106. assert_param(IS_FMC_WRITE_RECOVERY_TIME(Timing->WriteRecoveryTime));
  1107. assert_param(IS_FMC_RP_DELAY(Timing->RPDelay));
  1108. assert_param(IS_FMC_RCD_DELAY(Timing->RCDDelay));
  1109. assert_param(IS_FMC_SDRAM_BANK(Bank));
  1110. /* Set SDRAM device timing parameters */
  1111. if (Bank == FMC_SDRAM_BANK1)
  1112. {
  1113. MODIFY_REG(Device->SDTR[FMC_SDRAM_BANK1],
  1114. SDTR_CLEAR_MASK,
  1115. (((Timing->LoadToActiveDelay) - 1U) |
  1116. (((Timing->ExitSelfRefreshDelay) - 1U) << FMC_SDTR1_TXSR_Pos) |
  1117. (((Timing->SelfRefreshTime) - 1U) << FMC_SDTR1_TRAS_Pos) |
  1118. (((Timing->RowCycleDelay) - 1U) << FMC_SDTR1_TRC_Pos) |
  1119. (((Timing->WriteRecoveryTime) - 1U) << FMC_SDTR1_TWR_Pos) |
  1120. (((Timing->RPDelay) - 1U) << FMC_SDTR1_TRP_Pos) |
  1121. (((Timing->RCDDelay) - 1U) << FMC_SDTR1_TRCD_Pos)));
  1122. }
  1123. else /* FMC_Bank2_SDRAM */
  1124. {
  1125. MODIFY_REG(Device->SDTR[FMC_SDRAM_BANK1],
  1126. FMC_SDTR1_TRC |
  1127. FMC_SDTR1_TRP,
  1128. (((Timing->RowCycleDelay) - 1U) << FMC_SDTR1_TRC_Pos) |
  1129. (((Timing->RPDelay) - 1U) << FMC_SDTR1_TRP_Pos));
  1130. MODIFY_REG(Device->SDTR[FMC_SDRAM_BANK2],
  1131. SDTR_CLEAR_MASK,
  1132. (((Timing->LoadToActiveDelay) - 1U) |
  1133. (((Timing->ExitSelfRefreshDelay) - 1U) << FMC_SDTR1_TXSR_Pos) |
  1134. (((Timing->SelfRefreshTime) - 1U) << FMC_SDTR1_TRAS_Pos) |
  1135. (((Timing->WriteRecoveryTime) - 1U) << FMC_SDTR1_TWR_Pos) |
  1136. (((Timing->RCDDelay) - 1U) << FMC_SDTR1_TRCD_Pos)));
  1137. }
  1138. return HAL_OK;
  1139. }
  1140. /**
  1141. * @brief DeInitializes the FMC_SDRAM peripheral
  1142. * @param Device Pointer to SDRAM device instance
  1143. * @retval HAL status
  1144. */
  1145. HAL_StatusTypeDef FMC_SDRAM_DeInit(FMC_SDRAM_TypeDef *Device, uint32_t Bank)
  1146. {
  1147. /* Check the parameters */
  1148. assert_param(IS_FMC_SDRAM_DEVICE(Device));
  1149. assert_param(IS_FMC_SDRAM_BANK(Bank));
  1150. /* De-initialize the SDRAM device */
  1151. Device->SDCR[Bank] = 0x000002D0U;
  1152. Device->SDTR[Bank] = 0x0FFFFFFFU;
  1153. Device->SDCMR = 0x00000000U;
  1154. Device->SDRTR = 0x00000000U;
  1155. Device->SDSR = 0x00000000U;
  1156. return HAL_OK;
  1157. }
  1158. /**
  1159. * @}
  1160. */
  1161. /** @addtogroup FMC_LL_SDRAMPrivate_Functions_Group2
  1162. * @brief management functions
  1163. *
  1164. @verbatim
  1165. ==============================================================================
  1166. ##### FMC_SDRAM Control functions #####
  1167. ==============================================================================
  1168. [..]
  1169. This subsection provides a set of functions allowing to control dynamically
  1170. the FMC SDRAM interface.
  1171. @endverbatim
  1172. * @{
  1173. */
  1174. /**
  1175. * @brief Enables dynamically FMC_SDRAM write protection.
  1176. * @param Device Pointer to SDRAM device instance
  1177. * @param Bank SDRAM bank number
  1178. * @retval HAL status
  1179. */
  1180. HAL_StatusTypeDef FMC_SDRAM_WriteProtection_Enable(FMC_SDRAM_TypeDef *Device, uint32_t Bank)
  1181. {
  1182. /* Check the parameters */
  1183. assert_param(IS_FMC_SDRAM_DEVICE(Device));
  1184. assert_param(IS_FMC_SDRAM_BANK(Bank));
  1185. /* Enable write protection */
  1186. SET_BIT(Device->SDCR[Bank], FMC_SDRAM_WRITE_PROTECTION_ENABLE);
  1187. return HAL_OK;
  1188. }
  1189. /**
  1190. * @brief Disables dynamically FMC_SDRAM write protection.
  1191. * @param hsdram FMC_SDRAM handle
  1192. * @retval HAL status
  1193. */
  1194. HAL_StatusTypeDef FMC_SDRAM_WriteProtection_Disable(FMC_SDRAM_TypeDef *Device, uint32_t Bank)
  1195. {
  1196. /* Check the parameters */
  1197. assert_param(IS_FMC_SDRAM_DEVICE(Device));
  1198. assert_param(IS_FMC_SDRAM_BANK(Bank));
  1199. /* Disable write protection */
  1200. CLEAR_BIT(Device->SDCR[Bank], FMC_SDRAM_WRITE_PROTECTION_ENABLE);
  1201. return HAL_OK;
  1202. }
  1203. /**
  1204. * @brief Send Command to the FMC SDRAM bank
  1205. * @param Device Pointer to SDRAM device instance
  1206. * @param Command Pointer to SDRAM command structure
  1207. * @param Timing Pointer to SDRAM Timing structure
  1208. * @param Timeout Timeout wait value
  1209. * @retval HAL state
  1210. */
  1211. HAL_StatusTypeDef FMC_SDRAM_SendCommand(FMC_SDRAM_TypeDef *Device,
  1212. const FMC_SDRAM_CommandTypeDef *Command, uint32_t Timeout)
  1213. {
  1214. uint32_t tickstart = 0U;
  1215. /* Check the parameters */
  1216. assert_param(IS_FMC_SDRAM_DEVICE(Device));
  1217. assert_param(IS_FMC_COMMAND_MODE(Command->CommandMode));
  1218. assert_param(IS_FMC_COMMAND_TARGET(Command->CommandTarget));
  1219. assert_param(IS_FMC_AUTOREFRESH_NUMBER(Command->AutoRefreshNumber));
  1220. assert_param(IS_FMC_MODE_REGISTER(Command->ModeRegisterDefinition));
  1221. /* Set command register */
  1222. MODIFY_REG(Device->SDCMR, (FMC_SDCMR_MODE | FMC_SDCMR_CTB2 | FMC_SDCMR_CTB1 | FMC_SDCMR_NRFS | FMC_SDCMR_MRD),
  1223. ((Command->CommandMode) | (Command->CommandTarget) |
  1224. (((Command->AutoRefreshNumber) - 1U) << FMC_SDCMR_NRFS_Pos) |
  1225. ((Command->ModeRegisterDefinition) << FMC_SDCMR_MRD_Pos)));
  1226. /* Get tick */
  1227. tickstart = HAL_GetTick();
  1228. /* wait until command is send */
  1229. while (HAL_IS_BIT_SET(Device->SDSR, FMC_SDSR_BUSY))
  1230. {
  1231. /* Check for the Timeout */
  1232. if (Timeout != HAL_MAX_DELAY)
  1233. {
  1234. if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  1235. {
  1236. return HAL_TIMEOUT;
  1237. }
  1238. }
  1239. }
  1240. return HAL_OK;
  1241. }
  1242. /**
  1243. * @brief Program the SDRAM Memory Refresh rate.
  1244. * @param Device Pointer to SDRAM device instance
  1245. * @param RefreshRate The SDRAM refresh rate value.
  1246. * @retval HAL state
  1247. */
  1248. HAL_StatusTypeDef FMC_SDRAM_ProgramRefreshRate(FMC_SDRAM_TypeDef *Device, uint32_t RefreshRate)
  1249. {
  1250. /* Check the parameters */
  1251. assert_param(IS_FMC_SDRAM_DEVICE(Device));
  1252. assert_param(IS_FMC_REFRESH_RATE(RefreshRate));
  1253. /* Set the refresh rate in command register */
  1254. MODIFY_REG(Device->SDRTR, FMC_SDRTR_COUNT, (RefreshRate << FMC_SDRTR_COUNT_Pos));
  1255. return HAL_OK;
  1256. }
  1257. /**
  1258. * @brief Set the Number of consecutive SDRAM Memory auto Refresh commands.
  1259. * @param Device Pointer to SDRAM device instance
  1260. * @param AutoRefreshNumber Specifies the auto Refresh number.
  1261. * @retval None
  1262. */
  1263. HAL_StatusTypeDef FMC_SDRAM_SetAutoRefreshNumber(FMC_SDRAM_TypeDef *Device,
  1264. uint32_t AutoRefreshNumber)
  1265. {
  1266. /* Check the parameters */
  1267. assert_param(IS_FMC_SDRAM_DEVICE(Device));
  1268. assert_param(IS_FMC_AUTOREFRESH_NUMBER(AutoRefreshNumber));
  1269. /* Set the Auto-refresh number in command register */
  1270. MODIFY_REG(Device->SDCMR, FMC_SDCMR_NRFS, ((AutoRefreshNumber - 1U) << FMC_SDCMR_NRFS_Pos));
  1271. return HAL_OK;
  1272. }
  1273. /**
  1274. * @brief Returns the indicated FMC SDRAM bank mode status.
  1275. * @param Device Pointer to SDRAM device instance
  1276. * @param Bank Defines the FMC SDRAM bank. This parameter can be
  1277. * FMC_Bank1_SDRAM or FMC_Bank2_SDRAM.
  1278. * @retval The FMC SDRAM bank mode status, could be on of the following values:
  1279. * FMC_SDRAM_NORMAL_MODE, FMC_SDRAM_SELF_REFRESH_MODE or
  1280. * FMC_SDRAM_POWER_DOWN_MODE.
  1281. */
  1282. uint32_t FMC_SDRAM_GetModeStatus(const FMC_SDRAM_TypeDef *Device, uint32_t Bank)
  1283. {
  1284. uint32_t tmpreg;
  1285. /* Check the parameters */
  1286. assert_param(IS_FMC_SDRAM_DEVICE(Device));
  1287. assert_param(IS_FMC_SDRAM_BANK(Bank));
  1288. /* Get the corresponding bank mode */
  1289. if (Bank == FMC_SDRAM_BANK1)
  1290. {
  1291. tmpreg = (uint32_t)(Device->SDSR & FMC_SDSR_MODES1);
  1292. }
  1293. else
  1294. {
  1295. tmpreg = ((uint32_t)(Device->SDSR & FMC_SDSR_MODES2) >> 2U);
  1296. }
  1297. /* Return the mode status */
  1298. return tmpreg;
  1299. }
  1300. /**
  1301. * @}
  1302. */
  1303. /**
  1304. * @}
  1305. */
  1306. #endif /* FMC_Bank5_6 */
  1307. /**
  1308. * @}
  1309. */
  1310. /**
  1311. * @}
  1312. */
  1313. #endif /* HAL_NOR_MODULE_ENABLED */
  1314. /**
  1315. * @}
  1316. */
  1317. /**
  1318. * @}
  1319. */