stm32l5xx_ll_fmc.c 29 KB

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