stm32l4xx_ll_fmc.c 31 KB

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