stm32g4xx_ll_fmc.c 28 KB

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