stm32l4xx_ll_fmc.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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) 2017 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 memory controller
  31. [..] The FMC functional block makes the interface with synchronous and asynchronous static
  32. memories. Its main purposes are:
  33. (+) to translate AHB transactions into the appropriate external device protocol
  34. (+) to meet the access time requirements of the external memory devices
  35. [..] All external memories share the addresses, data and control signals with the controller.
  36. Each external device is accessed by means of a unique Chip Select. The FMC performs
  37. only one access at a time to an external device.
  38. The main features of the FMC controller are the following:
  39. (+) Interface with static-memory mapped devices including:
  40. (++) Static random access memory (SRAM)
  41. (++) Read-only memory (ROM)
  42. (++) NOR Flash memory/OneNAND Flash memory
  43. (++) PSRAM (4 memory banks)
  44. (++) Two banks of NAND Flash memory with ECC hardware to check up to 8 Kbytes of
  45. data
  46. (+) Independent Chip Select control for each memory bank
  47. (+) Independent configuration for each memory bank
  48. @endverbatim
  49. ******************************************************************************
  50. */
  51. /* Includes ------------------------------------------------------------------*/
  52. #include "stm32l4xx_hal.h"
  53. /** @addtogroup STM32L4xx_HAL_Driver
  54. * @{
  55. */
  56. #if defined(HAL_NOR_MODULE_ENABLED) || defined(HAL_NAND_MODULE_ENABLED) \
  57. || defined(HAL_SRAM_MODULE_ENABLED)
  58. /** @defgroup FMC_LL FMC Low Layer
  59. * @brief FMC driver modules
  60. * @{
  61. */
  62. /* Private typedef -----------------------------------------------------------*/
  63. /* Private define ------------------------------------------------------------*/
  64. /** @defgroup FMC_LL_Private_Constants FMC Low Layer Private Constants
  65. * @{
  66. */
  67. /* ----------------------- FMC registers bit mask --------------------------- */
  68. #if defined(FMC_BANK1)
  69. /* --- BCR Register ---*/
  70. /* BCR register clear mask */
  71. /* --- BTR Register ---*/
  72. /* BTR register clear mask */
  73. #if defined(FMC_BTRx_DATAHLD)
  74. #define BTR_CLEAR_MASK ((uint32_t)(FMC_BTRx_ADDSET | FMC_BTRx_ADDHLD |\
  75. FMC_BTRx_DATAST | FMC_BTRx_BUSTURN |\
  76. FMC_BTRx_CLKDIV | FMC_BTRx_DATLAT |\
  77. FMC_BTRx_ACCMOD | FMC_BTRx_DATAHLD))
  78. #else
  79. #define BTR_CLEAR_MASK ((uint32_t)(FMC_BTRx_ADDSET | FMC_BTRx_ADDHLD |\
  80. FMC_BTRx_DATAST | FMC_BTRx_BUSTURN |\
  81. FMC_BTRx_CLKDIV | FMC_BTRx_DATLAT |\
  82. FMC_BTRx_ACCMOD))
  83. #endif /* FMC_BTRx_DATAHLD */
  84. /* --- BWTR Register ---*/
  85. /* BWTR register clear mask */
  86. #if defined(FMC_BWTRx_DATAHLD)
  87. #define BWTR_CLEAR_MASK ((uint32_t)(FMC_BWTRx_ADDSET | FMC_BWTRx_ADDHLD |\
  88. FMC_BWTRx_DATAST | FMC_BWTRx_BUSTURN |\
  89. FMC_BWTRx_ACCMOD | FMC_BWTRx_DATAHLD))
  90. #else
  91. #define BWTR_CLEAR_MASK ((uint32_t)(FMC_BWTRx_ADDSET | FMC_BWTRx_ADDHLD |\
  92. FMC_BWTRx_DATAST | FMC_BWTRx_BUSTURN |\
  93. FMC_BWTRx_ACCMOD))
  94. #endif /* FMC_BWTRx_DATAHLD */
  95. #endif /* FMC_BANK1 */
  96. #if defined(FMC_BANK3)
  97. /* --- PCR Register ---*/
  98. /* PCR register clear mask */
  99. #define PCR_CLEAR_MASK ((uint32_t)(FMC_PCR_PWAITEN | FMC_PCR_PBKEN | \
  100. FMC_PCR_PTYP | FMC_PCR_PWID | \
  101. FMC_PCR_ECCEN | FMC_PCR_TCLR | \
  102. FMC_PCR_TAR | FMC_PCR_ECCPS))
  103. /* --- PMEM Register ---*/
  104. /* PMEM register clear mask */
  105. #define PMEM_CLEAR_MASK ((uint32_t)(FMC_PMEM_MEMSET | FMC_PMEM_MEMWAIT |\
  106. FMC_PMEM_MEMHOLD | FMC_PMEM_MEMHIZ))
  107. /* --- PATT Register ---*/
  108. /* PATT register clear mask */
  109. #define PATT_CLEAR_MASK ((uint32_t)(FMC_PATT_ATTSET | FMC_PATT_ATTWAIT |\
  110. FMC_PATT_ATTHOLD | FMC_PATT_ATTHIZ))
  111. #endif /* FMC_BANK3 */
  112. /**
  113. * @}
  114. */
  115. /* Private macro -------------------------------------------------------------*/
  116. /* Private variables ---------------------------------------------------------*/
  117. /* Private function prototypes -----------------------------------------------*/
  118. /* Exported functions --------------------------------------------------------*/
  119. /** @defgroup FMC_LL_Exported_Functions FMC Low Layer Exported Functions
  120. * @{
  121. */
  122. #if defined(FMC_BANK1)
  123. /** @defgroup FMC_LL_Exported_Functions_NORSRAM FMC Low Layer NOR SRAM Exported Functions
  124. * @brief NORSRAM Controller functions
  125. *
  126. @verbatim
  127. ==============================================================================
  128. ##### How to use NORSRAM device driver #####
  129. ==============================================================================
  130. [..]
  131. This driver contains a set of APIs to interface with the FMC NORSRAM banks in order
  132. to run the NORSRAM external devices.
  133. (+) FMC NORSRAM bank reset using the function FMC_NORSRAM_DeInit()
  134. (+) FMC NORSRAM bank control configuration using the function FMC_NORSRAM_Init()
  135. (+) FMC NORSRAM bank timing configuration using the function FMC_NORSRAM_Timing_Init()
  136. (+) FMC NORSRAM bank extended timing configuration using the function
  137. FMC_NORSRAM_Extended_Timing_Init()
  138. (+) FMC NORSRAM bank enable/disable write operation using the functions
  139. FMC_NORSRAM_WriteOperation_Enable()/FMC_NORSRAM_WriteOperation_Disable()
  140. @endverbatim
  141. * @{
  142. */
  143. /** @defgroup FMC_LL_NORSRAM_Exported_Functions_Group1 Initialization and de-initialization functions
  144. * @brief Initialization and Configuration functions
  145. *
  146. @verbatim
  147. ==============================================================================
  148. ##### Initialization and de_initialization functions #####
  149. ==============================================================================
  150. [..]
  151. This section provides functions allowing to:
  152. (+) Initialize and configure the FMC NORSRAM interface
  153. (+) De-initialize the FMC NORSRAM interface
  154. (+) Configure the FMC clock and associated GPIOs
  155. @endverbatim
  156. * @{
  157. */
  158. /**
  159. * @brief Initialize the FMC_NORSRAM device according to the specified
  160. * control parameters in the FMC_NORSRAM_InitTypeDef
  161. * @param Device Pointer to NORSRAM device instance
  162. * @param Init Pointer to NORSRAM Initialization structure
  163. * @retval HAL status
  164. */
  165. HAL_StatusTypeDef FMC_NORSRAM_Init(FMC_NORSRAM_TypeDef *Device,
  166. const FMC_NORSRAM_InitTypeDef *Init)
  167. {
  168. uint32_t flashaccess;
  169. uint32_t btcr_reg;
  170. uint32_t mask;
  171. /* Check the parameters */
  172. assert_param(IS_FMC_NORSRAM_DEVICE(Device));
  173. assert_param(IS_FMC_NORSRAM_BANK(Init->NSBank));
  174. assert_param(IS_FMC_MUX(Init->DataAddressMux));
  175. assert_param(IS_FMC_MEMORY(Init->MemoryType));
  176. assert_param(IS_FMC_NORSRAM_MEMORY_WIDTH(Init->MemoryDataWidth));
  177. assert_param(IS_FMC_BURSTMODE(Init->BurstAccessMode));
  178. assert_param(IS_FMC_WAIT_POLARITY(Init->WaitSignalPolarity));
  179. assert_param(IS_FMC_WAIT_SIGNAL_ACTIVE(Init->WaitSignalActive));
  180. assert_param(IS_FMC_WRITE_OPERATION(Init->WriteOperation));
  181. assert_param(IS_FMC_WAITE_SIGNAL(Init->WaitSignal));
  182. assert_param(IS_FMC_EXTENDED_MODE(Init->ExtendedMode));
  183. assert_param(IS_FMC_ASYNWAIT(Init->AsynchronousWait));
  184. assert_param(IS_FMC_WRITE_BURST(Init->WriteBurst));
  185. assert_param(IS_FMC_CONTINOUS_CLOCK(Init->ContinuousClock));
  186. #if defined(FMC_BCR1_WFDIS)
  187. assert_param(IS_FMC_WRITE_FIFO(Init->WriteFifo));
  188. #endif /* FMC_BCR1_WFDIS */
  189. assert_param(IS_FMC_PAGESIZE(Init->PageSize));
  190. #if defined(FMC_BCRx_NBLSET)
  191. assert_param(IS_FMC_NBL_SETUPTIME(Init->NBLSetupTime));
  192. #endif /* FMC_BCRx_NBLSET */
  193. #if defined(FMC_PCSCNTR_CSCOUNT)
  194. assert_param(IS_FUNCTIONAL_STATE(Init->MaxChipSelectPulse));
  195. #endif /* FMC_PCSCNTR_CSCOUNT */
  196. /* Disable NORSRAM Device */
  197. __FMC_NORSRAM_DISABLE(Device, Init->NSBank);
  198. /* Set NORSRAM device control parameters */
  199. if (Init->MemoryType == FMC_MEMORY_TYPE_NOR)
  200. {
  201. flashaccess = FMC_NORSRAM_FLASH_ACCESS_ENABLE;
  202. }
  203. else
  204. {
  205. flashaccess = FMC_NORSRAM_FLASH_ACCESS_DISABLE;
  206. }
  207. btcr_reg = (flashaccess | \
  208. Init->DataAddressMux | \
  209. Init->MemoryType | \
  210. Init->MemoryDataWidth | \
  211. Init->BurstAccessMode | \
  212. Init->WaitSignalPolarity | \
  213. Init->WaitSignalActive | \
  214. Init->WriteOperation | \
  215. Init->WaitSignal | \
  216. Init->ExtendedMode | \
  217. Init->AsynchronousWait | \
  218. Init->WriteBurst);
  219. btcr_reg |= Init->ContinuousClock;
  220. #if defined(FMC_BCR1_WFDIS)
  221. btcr_reg |= Init->WriteFifo;
  222. #endif /* FMC_BCR1_WFDIS */
  223. #if defined(FMC_BCRx_NBLSET)
  224. btcr_reg |= Init->NBLSetupTime;
  225. #endif /* FMC_BCRx_NBLSET */
  226. btcr_reg |= Init->PageSize;
  227. mask = (FMC_BCRx_MBKEN |
  228. FMC_BCRx_MUXEN |
  229. FMC_BCRx_MTYP |
  230. FMC_BCRx_MWID |
  231. FMC_BCRx_FACCEN |
  232. FMC_BCRx_BURSTEN |
  233. FMC_BCRx_WAITPOL |
  234. FMC_BCRx_WAITCFG |
  235. FMC_BCRx_WREN |
  236. FMC_BCRx_WAITEN |
  237. FMC_BCRx_EXTMOD |
  238. FMC_BCRx_ASYNCWAIT |
  239. FMC_BCRx_CBURSTRW);
  240. mask |= FMC_BCR1_CCLKEN;
  241. #if defined(FMC_BCR1_WFDIS)
  242. mask |= FMC_BCR1_WFDIS;
  243. #endif /* FMC_BCR1_WFDIS */
  244. #if defined(FMC_BCRx_NBLSET)
  245. mask |= FMC_BCRx_NBLSET;
  246. #endif /* FMC_BCRx_NBLSET */
  247. mask |= FMC_BCRx_CPSIZE;
  248. MODIFY_REG(Device->BTCR[Init->NSBank], mask, btcr_reg);
  249. /* Configure synchronous mode when Continuous clock is enabled for bank2..4 */
  250. if ((Init->ContinuousClock == FMC_CONTINUOUS_CLOCK_SYNC_ASYNC) && (Init->NSBank != FMC_NORSRAM_BANK1))
  251. {
  252. MODIFY_REG(Device->BTCR[FMC_NORSRAM_BANK1], FMC_BCR1_CCLKEN, Init->ContinuousClock);
  253. }
  254. #if defined(FMC_BCR1_WFDIS)
  255. if (Init->NSBank != FMC_NORSRAM_BANK1)
  256. {
  257. /* Configure Write FIFO mode when Write Fifo is enabled for bank2..4 */
  258. SET_BIT(Device->BTCR[FMC_NORSRAM_BANK1], (uint32_t)(Init->WriteFifo));
  259. }
  260. #endif /* FMC_BCR1_WFDIS */
  261. #if defined(FMC_PCSCNTR_CSCOUNT)
  262. /* Check PSRAM chip select counter state */
  263. if (Init->MaxChipSelectPulse == ENABLE)
  264. {
  265. /* Check the parameters */
  266. assert_param(IS_FMC_MAX_CHIP_SELECT_PULSE_TIME(Init->MaxChipSelectPulseTime));
  267. /* Configure PSRAM chip select counter value */
  268. MODIFY_REG(Device->PCSCNTR, FMC_PCSCNTR_CSCOUNT, (uint32_t)(Init->MaxChipSelectPulseTime));
  269. /* Enable PSRAM chip select counter for the bank */
  270. switch (Init->NSBank)
  271. {
  272. case FMC_NORSRAM_BANK1 :
  273. SET_BIT(Device->PCSCNTR, FMC_PCSCNTR_CNTB1EN);
  274. break;
  275. case FMC_NORSRAM_BANK2 :
  276. SET_BIT(Device->PCSCNTR, FMC_PCSCNTR_CNTB2EN);
  277. break;
  278. case FMC_NORSRAM_BANK3 :
  279. SET_BIT(Device->PCSCNTR, FMC_PCSCNTR_CNTB3EN);
  280. break;
  281. default :
  282. SET_BIT(Device->PCSCNTR, FMC_PCSCNTR_CNTB4EN);
  283. break;
  284. }
  285. }
  286. #endif /* FMC_PCSCNTR_CSCOUNT */
  287. return HAL_OK;
  288. }
  289. /**
  290. * @brief DeInitialize the FMC_NORSRAM peripheral
  291. * @param Device Pointer to NORSRAM device instance
  292. * @param ExDevice Pointer to NORSRAM extended mode device instance
  293. * @param Bank NORSRAM bank number
  294. * @retval HAL status
  295. */
  296. HAL_StatusTypeDef FMC_NORSRAM_DeInit(FMC_NORSRAM_TypeDef *Device,
  297. FMC_NORSRAM_EXTENDED_TypeDef *ExDevice, uint32_t Bank)
  298. {
  299. /* Check the parameters */
  300. assert_param(IS_FMC_NORSRAM_DEVICE(Device));
  301. assert_param(IS_FMC_NORSRAM_EXTENDED_DEVICE(ExDevice));
  302. assert_param(IS_FMC_NORSRAM_BANK(Bank));
  303. /* Disable the FMC_NORSRAM device */
  304. __FMC_NORSRAM_DISABLE(Device, Bank);
  305. /* De-initialize the FMC_NORSRAM device */
  306. /* FMC_NORSRAM_BANK1 */
  307. if (Bank == FMC_NORSRAM_BANK1)
  308. {
  309. Device->BTCR[Bank] = 0x000030DBU;
  310. }
  311. /* FMC_NORSRAM_BANK2, FMC_NORSRAM_BANK3 or FMC_NORSRAM_BANK4 */
  312. else
  313. {
  314. Device->BTCR[Bank] = 0x000030D2U;
  315. }
  316. Device->BTCR[Bank + 1U] = 0x0FFFFFFFU;
  317. ExDevice->BWTR[Bank] = 0x0FFFFFFFU;
  318. #if defined(FMC_PCSCNTR_CSCOUNT)
  319. /* De-initialize PSRAM chip select counter */
  320. switch (Bank)
  321. {
  322. case FMC_NORSRAM_BANK1 :
  323. CLEAR_BIT(Device->PCSCNTR, FMC_PCSCNTR_CNTB1EN);
  324. break;
  325. case FMC_NORSRAM_BANK2 :
  326. CLEAR_BIT(Device->PCSCNTR, FMC_PCSCNTR_CNTB2EN);
  327. break;
  328. case FMC_NORSRAM_BANK3 :
  329. CLEAR_BIT(Device->PCSCNTR, FMC_PCSCNTR_CNTB3EN);
  330. break;
  331. default :
  332. CLEAR_BIT(Device->PCSCNTR, FMC_PCSCNTR_CNTB4EN);
  333. break;
  334. }
  335. #endif /* FMC_PCSCNTR_CSCOUNT */
  336. return HAL_OK;
  337. }
  338. /**
  339. * @brief Initialize the FMC_NORSRAM Timing according to the specified
  340. * parameters in the FMC_NORSRAM_TimingTypeDef
  341. * @param Device Pointer to NORSRAM device instance
  342. * @param Timing Pointer to NORSRAM Timing structure
  343. * @param Bank NORSRAM bank number
  344. * @retval HAL status
  345. */
  346. HAL_StatusTypeDef FMC_NORSRAM_Timing_Init(FMC_NORSRAM_TypeDef *Device,
  347. const FMC_NORSRAM_TimingTypeDef *Timing, uint32_t Bank)
  348. {
  349. uint32_t tmpr;
  350. /* Check the parameters */
  351. assert_param(IS_FMC_NORSRAM_DEVICE(Device));
  352. assert_param(IS_FMC_ADDRESS_SETUP_TIME(Timing->AddressSetupTime));
  353. assert_param(IS_FMC_ADDRESS_HOLD_TIME(Timing->AddressHoldTime));
  354. #if defined(FMC_BTRx_DATAHLD)
  355. assert_param(IS_FMC_DATAHOLD_DURATION(Timing->DataHoldTime));
  356. #endif /* FMC_BTRx_DATAHLD */
  357. assert_param(IS_FMC_DATASETUP_TIME(Timing->DataSetupTime));
  358. assert_param(IS_FMC_TURNAROUND_TIME(Timing->BusTurnAroundDuration));
  359. assert_param(IS_FMC_CLK_DIV(Timing->CLKDivision));
  360. assert_param(IS_FMC_DATA_LATENCY(Timing->DataLatency));
  361. assert_param(IS_FMC_ACCESS_MODE(Timing->AccessMode));
  362. assert_param(IS_FMC_NORSRAM_BANK(Bank));
  363. /* Set FMC_NORSRAM device timing parameters */
  364. #if defined(FMC_BTRx_DATAHLD)
  365. Device->BTCR[Bank + 1U] =
  366. (Timing->AddressSetupTime << FMC_BTRx_ADDSET_Pos) |
  367. (Timing->AddressHoldTime << FMC_BTRx_ADDHLD_Pos) |
  368. (Timing->DataSetupTime << FMC_BTRx_DATAST_Pos) |
  369. (Timing->DataHoldTime << FMC_BTRx_DATAHLD_Pos) |
  370. (Timing->BusTurnAroundDuration << FMC_BTRx_BUSTURN_Pos) |
  371. ((Timing->CLKDivision - 1U) << FMC_BTRx_CLKDIV_Pos) |
  372. ((Timing->DataLatency - 2U) << FMC_BTRx_DATLAT_Pos) |
  373. Timing->AccessMode;
  374. #else /* FMC_BTRx_DATAHLD */
  375. Device->BTCR[Bank + 1U] =
  376. (Timing->AddressSetupTime << FMC_BTRx_ADDSET_Pos) |
  377. (Timing->AddressHoldTime << FMC_BTRx_ADDHLD_Pos) |
  378. (Timing->DataSetupTime << FMC_BTRx_DATAST_Pos) |
  379. (Timing->BusTurnAroundDuration << FMC_BTRx_BUSTURN_Pos) |
  380. ((Timing->CLKDivision - 1U) << FMC_BTRx_CLKDIV_Pos) |
  381. ((Timing->DataLatency - 2U) << FMC_BTRx_DATLAT_Pos) |
  382. Timing->AccessMode;
  383. #endif /* FMC_BTRx_DATAHLD */
  384. /* Configure Clock division value (in NORSRAM bank 1) when continuous clock is enabled */
  385. if (HAL_IS_BIT_SET(Device->BTCR[FMC_NORSRAM_BANK1], FMC_BCR1_CCLKEN))
  386. {
  387. tmpr = (uint32_t)(Device->BTCR[FMC_NORSRAM_BANK1 + 1U] & ~((0x0FU) << FMC_BTRx_CLKDIV_Pos));
  388. tmpr |= (uint32_t)(((Timing->CLKDivision) - 1U) << FMC_BTRx_CLKDIV_Pos);
  389. MODIFY_REG(Device->BTCR[FMC_NORSRAM_BANK1 + 1U], FMC_BTRx_CLKDIV, tmpr);
  390. }
  391. return HAL_OK;
  392. }
  393. /**
  394. * @brief Initialize the FMC_NORSRAM Extended mode Timing according to the specified
  395. * parameters in the FMC_NORSRAM_TimingTypeDef
  396. * @param Device Pointer to NORSRAM device instance
  397. * @param Timing Pointer to NORSRAM Timing structure
  398. * @param Bank NORSRAM bank number
  399. * @param ExtendedMode FMC Extended Mode
  400. * This parameter can be one of the following values:
  401. * @arg FMC_EXTENDED_MODE_DISABLE
  402. * @arg FMC_EXTENDED_MODE_ENABLE
  403. * @retval HAL status
  404. */
  405. HAL_StatusTypeDef FMC_NORSRAM_Extended_Timing_Init(FMC_NORSRAM_EXTENDED_TypeDef *Device,
  406. const FMC_NORSRAM_TimingTypeDef *Timing, uint32_t Bank,
  407. uint32_t ExtendedMode)
  408. {
  409. /* Check the parameters */
  410. assert_param(IS_FMC_EXTENDED_MODE(ExtendedMode));
  411. /* Set NORSRAM device timing register for write configuration, if extended mode is used */
  412. if (ExtendedMode == FMC_EXTENDED_MODE_ENABLE)
  413. {
  414. /* Check the parameters */
  415. assert_param(IS_FMC_NORSRAM_EXTENDED_DEVICE(Device));
  416. assert_param(IS_FMC_ADDRESS_SETUP_TIME(Timing->AddressSetupTime));
  417. assert_param(IS_FMC_ADDRESS_HOLD_TIME(Timing->AddressHoldTime));
  418. assert_param(IS_FMC_DATASETUP_TIME(Timing->DataSetupTime));
  419. #if defined(FMC_BTRx_DATAHLD)
  420. assert_param(IS_FMC_DATAHOLD_DURATION(Timing->DataHoldTime));
  421. #endif /* FMC_BTRx_DATAHLD */
  422. assert_param(IS_FMC_TURNAROUND_TIME(Timing->BusTurnAroundDuration));
  423. assert_param(IS_FMC_ACCESS_MODE(Timing->AccessMode));
  424. assert_param(IS_FMC_NORSRAM_BANK(Bank));
  425. /* Set NORSRAM device timing register for write configuration, if extended mode is used */
  426. #if defined(FMC_BTRx_DATAHLD)
  427. MODIFY_REG(Device->BWTR[Bank], BWTR_CLEAR_MASK, (Timing->AddressSetupTime |
  428. ((Timing->AddressHoldTime) << FMC_BWTRx_ADDHLD_Pos) |
  429. ((Timing->DataSetupTime) << FMC_BWTRx_DATAST_Pos) |
  430. ((Timing->DataHoldTime) << FMC_BWTRx_DATAHLD_Pos) |
  431. Timing->AccessMode |
  432. ((Timing->BusTurnAroundDuration) << FMC_BWTRx_BUSTURN_Pos)));
  433. #else /* FMC_BTRx_DATAHLD */
  434. MODIFY_REG(Device->BWTR[Bank], BWTR_CLEAR_MASK, (Timing->AddressSetupTime |
  435. ((Timing->AddressHoldTime) << FMC_BWTRx_ADDHLD_Pos) |
  436. ((Timing->DataSetupTime) << FMC_BWTRx_DATAST_Pos) |
  437. Timing->AccessMode |
  438. ((Timing->BusTurnAroundDuration) << FMC_BWTRx_BUSTURN_Pos)));
  439. #endif /* FMC_BTRx_DATAHLD */
  440. }
  441. else
  442. {
  443. Device->BWTR[Bank] = 0x0FFFFFFFU;
  444. }
  445. return HAL_OK;
  446. }
  447. /**
  448. * @}
  449. */
  450. /** @addtogroup FMC_LL_NORSRAM_Private_Functions_Group2
  451. * @brief management functions
  452. *
  453. @verbatim
  454. ==============================================================================
  455. ##### FMC_NORSRAM Control functions #####
  456. ==============================================================================
  457. [..]
  458. This subsection provides a set of functions allowing to control dynamically
  459. the FMC NORSRAM interface.
  460. @endverbatim
  461. * @{
  462. */
  463. /**
  464. * @brief Enables dynamically FMC_NORSRAM write operation.
  465. * @param Device Pointer to NORSRAM device instance
  466. * @param Bank NORSRAM bank number
  467. * @retval HAL status
  468. */
  469. HAL_StatusTypeDef FMC_NORSRAM_WriteOperation_Enable(FMC_NORSRAM_TypeDef *Device, uint32_t Bank)
  470. {
  471. /* Check the parameters */
  472. assert_param(IS_FMC_NORSRAM_DEVICE(Device));
  473. assert_param(IS_FMC_NORSRAM_BANK(Bank));
  474. /* Enable write operation */
  475. SET_BIT(Device->BTCR[Bank], FMC_WRITE_OPERATION_ENABLE);
  476. return HAL_OK;
  477. }
  478. /**
  479. * @brief Disables dynamically FMC_NORSRAM write operation.
  480. * @param Device Pointer to NORSRAM device instance
  481. * @param Bank NORSRAM bank number
  482. * @retval HAL status
  483. */
  484. HAL_StatusTypeDef FMC_NORSRAM_WriteOperation_Disable(FMC_NORSRAM_TypeDef *Device, uint32_t Bank)
  485. {
  486. /* Check the parameters */
  487. assert_param(IS_FMC_NORSRAM_DEVICE(Device));
  488. assert_param(IS_FMC_NORSRAM_BANK(Bank));
  489. /* Disable write operation */
  490. CLEAR_BIT(Device->BTCR[Bank], FMC_WRITE_OPERATION_ENABLE);
  491. return HAL_OK;
  492. }
  493. /**
  494. * @}
  495. */
  496. /**
  497. * @}
  498. */
  499. #endif /* FMC_BANK1 */
  500. #if defined(FMC_BANK3)
  501. /** @defgroup FMC_LL_Exported_Functions_NAND FMC Low Layer NAND Exported Functions
  502. * @brief NAND Controller functions
  503. *
  504. @verbatim
  505. ==============================================================================
  506. ##### How to use NAND device driver #####
  507. ==============================================================================
  508. [..]
  509. This driver contains a set of APIs to interface with the FMC NAND banks in order
  510. to run the NAND external devices.
  511. (+) FMC NAND bank reset using the function FMC_NAND_DeInit()
  512. (+) FMC NAND bank control configuration using the function FMC_NAND_Init()
  513. (+) FMC NAND bank common space timing configuration using the function
  514. FMC_NAND_CommonSpace_Timing_Init()
  515. (+) FMC NAND bank attribute space timing configuration using the function
  516. FMC_NAND_AttributeSpace_Timing_Init()
  517. (+) FMC NAND bank enable/disable ECC correction feature using the functions
  518. FMC_NAND_ECC_Enable()/FMC_NAND_ECC_Disable()
  519. (+) FMC NAND bank get ECC correction code using the function FMC_NAND_GetECC()
  520. @endverbatim
  521. * @{
  522. */
  523. /** @defgroup FMC_LL_NAND_Exported_Functions_Group1 Initialization and de-initialization functions
  524. * @brief Initialization and Configuration functions
  525. *
  526. @verbatim
  527. ==============================================================================
  528. ##### Initialization and de_initialization functions #####
  529. ==============================================================================
  530. [..]
  531. This section provides functions allowing to:
  532. (+) Initialize and configure the FMC NAND interface
  533. (+) De-initialize the FMC NAND interface
  534. (+) Configure the FMC clock and associated GPIOs
  535. @endverbatim
  536. * @{
  537. */
  538. /**
  539. * @brief Initializes the FMC_NAND device according to the specified
  540. * control parameters in the FMC_NAND_HandleTypeDef
  541. * @param Device Pointer to NAND device instance
  542. * @param Init Pointer to NAND Initialization structure
  543. * @retval HAL status
  544. */
  545. HAL_StatusTypeDef FMC_NAND_Init(FMC_NAND_TypeDef *Device, const FMC_NAND_InitTypeDef *Init)
  546. {
  547. /* Check the parameters */
  548. assert_param(IS_FMC_NAND_DEVICE(Device));
  549. assert_param(IS_FMC_NAND_BANK(Init->NandBank));
  550. assert_param(IS_FMC_WAIT_FEATURE(Init->Waitfeature));
  551. assert_param(IS_FMC_NAND_MEMORY_WIDTH(Init->MemoryDataWidth));
  552. assert_param(IS_FMC_ECC_STATE(Init->EccComputation));
  553. assert_param(IS_FMC_ECCPAGE_SIZE(Init->ECCPageSize));
  554. assert_param(IS_FMC_TCLR_TIME(Init->TCLRSetupTime));
  555. assert_param(IS_FMC_TAR_TIME(Init->TARSetupTime));
  556. /* NAND bank 3 registers configuration */
  557. MODIFY_REG(Device->PCR, PCR_CLEAR_MASK, (Init->Waitfeature |
  558. FMC_PCR_MEMORY_TYPE_NAND |
  559. Init->MemoryDataWidth |
  560. Init->EccComputation |
  561. Init->ECCPageSize |
  562. ((Init->TCLRSetupTime) << FMC_PCR_TCLR_Pos) |
  563. ((Init->TARSetupTime) << FMC_PCR_TAR_Pos)));
  564. return HAL_OK;
  565. }
  566. /**
  567. * @brief Initializes the FMC_NAND Common space Timing according to the specified
  568. * parameters in the FMC_NAND_PCC_TimingTypeDef
  569. * @param Device Pointer to NAND device instance
  570. * @param Timing Pointer to NAND timing structure
  571. * @param Bank NAND bank number
  572. * @retval HAL status
  573. */
  574. HAL_StatusTypeDef FMC_NAND_CommonSpace_Timing_Init(FMC_NAND_TypeDef *Device,
  575. const FMC_NAND_PCC_TimingTypeDef *Timing, uint32_t Bank)
  576. {
  577. /* Check the parameters */
  578. assert_param(IS_FMC_NAND_DEVICE(Device));
  579. assert_param(IS_FMC_SETUP_TIME(Timing->SetupTime));
  580. assert_param(IS_FMC_WAIT_TIME(Timing->WaitSetupTime));
  581. assert_param(IS_FMC_HOLD_TIME(Timing->HoldSetupTime));
  582. assert_param(IS_FMC_HIZ_TIME(Timing->HiZSetupTime));
  583. assert_param(IS_FMC_NAND_BANK(Bank));
  584. /* Prevent unused argument(s) compilation warning if no assert_param check */
  585. UNUSED(Bank);
  586. /* NAND bank 3 registers configuration */
  587. Device->PMEM = (Timing->SetupTime |
  588. ((Timing->WaitSetupTime) << FMC_PMEM_MEMWAIT_Pos) |
  589. ((Timing->HoldSetupTime) << FMC_PMEM_MEMHOLD_Pos) |
  590. ((Timing->HiZSetupTime) << FMC_PMEM_MEMHIZ_Pos));
  591. return HAL_OK;
  592. }
  593. /**
  594. * @brief Initializes the FMC_NAND Attribute space Timing according to the specified
  595. * parameters in the FMC_NAND_PCC_TimingTypeDef
  596. * @param Device Pointer to NAND device instance
  597. * @param Timing Pointer to NAND timing structure
  598. * @param Bank NAND bank number
  599. * @retval HAL status
  600. */
  601. HAL_StatusTypeDef FMC_NAND_AttributeSpace_Timing_Init(FMC_NAND_TypeDef *Device,
  602. const FMC_NAND_PCC_TimingTypeDef *Timing, uint32_t Bank)
  603. {
  604. /* Check the parameters */
  605. assert_param(IS_FMC_NAND_DEVICE(Device));
  606. assert_param(IS_FMC_SETUP_TIME(Timing->SetupTime));
  607. assert_param(IS_FMC_WAIT_TIME(Timing->WaitSetupTime));
  608. assert_param(IS_FMC_HOLD_TIME(Timing->HoldSetupTime));
  609. assert_param(IS_FMC_HIZ_TIME(Timing->HiZSetupTime));
  610. assert_param(IS_FMC_NAND_BANK(Bank));
  611. /* Prevent unused argument(s) compilation warning if no assert_param check */
  612. UNUSED(Bank);
  613. /* NAND bank 3 registers configuration */
  614. Device->PATT = (Timing->SetupTime |
  615. ((Timing->WaitSetupTime) << FMC_PATT_ATTWAIT_Pos) |
  616. ((Timing->HoldSetupTime) << FMC_PATT_ATTHOLD_Pos) |
  617. ((Timing->HiZSetupTime) << FMC_PATT_ATTHIZ_Pos));
  618. return HAL_OK;
  619. }
  620. /**
  621. * @brief DeInitializes the FMC_NAND device
  622. * @param Device Pointer to NAND device instance
  623. * @param Bank NAND bank number
  624. * @retval HAL status
  625. */
  626. HAL_StatusTypeDef FMC_NAND_DeInit(FMC_NAND_TypeDef *Device, uint32_t Bank)
  627. {
  628. /* Check the parameters */
  629. assert_param(IS_FMC_NAND_DEVICE(Device));
  630. assert_param(IS_FMC_NAND_BANK(Bank));
  631. /* Disable the NAND Bank */
  632. __FMC_NAND_DISABLE(Device, Bank);
  633. /* De-initialize the NAND Bank */
  634. /* Prevent unused argument(s) compilation warning if no assert_param check */
  635. UNUSED(Bank);
  636. /* Set the FMC_NAND_BANK3 registers to their reset values */
  637. WRITE_REG(Device->PCR, 0x00000018U);
  638. WRITE_REG(Device->SR, 0x00000040U);
  639. WRITE_REG(Device->PMEM, 0xFCFCFCFCU);
  640. WRITE_REG(Device->PATT, 0xFCFCFCFCU);
  641. return HAL_OK;
  642. }
  643. /**
  644. * @}
  645. */
  646. /** @defgroup HAL_FMC_NAND_Group2 Peripheral Control functions
  647. * @brief management functions
  648. *
  649. @verbatim
  650. ==============================================================================
  651. ##### FMC_NAND Control functions #####
  652. ==============================================================================
  653. [..]
  654. This subsection provides a set of functions allowing to control dynamically
  655. the FMC NAND interface.
  656. @endverbatim
  657. * @{
  658. */
  659. /**
  660. * @brief Enables dynamically FMC_NAND ECC feature.
  661. * @param Device Pointer to NAND device instance
  662. * @param Bank NAND bank number
  663. * @retval HAL status
  664. */
  665. HAL_StatusTypeDef FMC_NAND_ECC_Enable(FMC_NAND_TypeDef *Device, uint32_t Bank)
  666. {
  667. /* Check the parameters */
  668. assert_param(IS_FMC_NAND_DEVICE(Device));
  669. assert_param(IS_FMC_NAND_BANK(Bank));
  670. /* Enable ECC feature */
  671. /* Prevent unused argument(s) compilation warning if no assert_param check */
  672. UNUSED(Bank);
  673. SET_BIT(Device->PCR, FMC_PCR_ECCEN);
  674. return HAL_OK;
  675. }
  676. /**
  677. * @brief Disables dynamically FMC_NAND ECC feature.
  678. * @param Device Pointer to NAND device instance
  679. * @param Bank NAND bank number
  680. * @retval HAL status
  681. */
  682. HAL_StatusTypeDef FMC_NAND_ECC_Disable(FMC_NAND_TypeDef *Device, uint32_t Bank)
  683. {
  684. /* Check the parameters */
  685. assert_param(IS_FMC_NAND_DEVICE(Device));
  686. assert_param(IS_FMC_NAND_BANK(Bank));
  687. /* Disable ECC feature */
  688. /* Prevent unused argument(s) compilation warning if no assert_param check */
  689. UNUSED(Bank);
  690. CLEAR_BIT(Device->PCR, FMC_PCR_ECCEN);
  691. return HAL_OK;
  692. }
  693. /**
  694. * @brief Disables dynamically FMC_NAND ECC feature.
  695. * @param Device Pointer to NAND device instance
  696. * @param ECCval Pointer to ECC value
  697. * @param Bank NAND bank number
  698. * @param Timeout Timeout wait value
  699. * @retval HAL status
  700. */
  701. HAL_StatusTypeDef FMC_NAND_GetECC(const FMC_NAND_TypeDef *Device, uint32_t *ECCval, uint32_t Bank,
  702. uint32_t Timeout)
  703. {
  704. uint32_t tickstart;
  705. /* Check the parameters */
  706. assert_param(IS_FMC_NAND_DEVICE(Device));
  707. assert_param(IS_FMC_NAND_BANK(Bank));
  708. /* Get tick */
  709. tickstart = HAL_GetTick();
  710. /* Wait until FIFO is empty */
  711. while (__FMC_NAND_GET_FLAG(Device, Bank, FMC_FLAG_FEMPT) == RESET)
  712. {
  713. /* Check for the Timeout */
  714. if (Timeout != HAL_MAX_DELAY)
  715. {
  716. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  717. {
  718. return HAL_TIMEOUT;
  719. }
  720. }
  721. }
  722. /* Prevent unused argument(s) compilation warning if no assert_param check */
  723. UNUSED(Bank);
  724. /* Get the ECCR register value */
  725. *ECCval = (uint32_t)Device->ECCR;
  726. return HAL_OK;
  727. }
  728. /**
  729. * @}
  730. */
  731. #endif /* FMC_BANK3 */
  732. /**
  733. * @}
  734. */
  735. /**
  736. * @}
  737. */
  738. #endif /* HAL_NOR_MODULE_ENABLED */
  739. /**
  740. * @}
  741. */
  742. /**
  743. * @}
  744. */