stm32f3xx_ll_fmc.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /**
  2. ******************************************************************************
  3. * @file stm32f3xx_ll_fmc.c
  4. * @author MCD Application Team
  5. * @brief FMC Low Layer HAL module driver.
  6. *
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the Flexible Memory Controller (FMC) peripheral memories:
  9. * + Initialization/de-initialization functions
  10. * + Peripheral Control functions
  11. * + Peripheral State functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2016 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### FMC peripheral features #####
  27. ==============================================================================
  28. [..] The Flexible memory controller (FMC) includes following memory controllers:
  29. (+) The NOR/PSRAM memory controller
  30. (+) The NAND/PC Card memory controller
  31. [..] The FMC functional block makes the interface with synchronous and asynchronous static
  32. memories and 16-bit PC memory cards. 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. (++) 16-bit PC Card compatible devices
  45. (++) Two banks of NAND Flash memory with ECC hardware to check up to 8 Kbytes of
  46. data
  47. (+) Independent Chip Select control for each memory bank
  48. (+) Independent configuration for each memory bank
  49. @endverbatim
  50. ******************************************************************************
  51. */
  52. /* Includes ------------------------------------------------------------------*/
  53. #include "stm32f3xx_hal.h"
  54. /** @addtogroup STM32F3xx_HAL_Driver
  55. * @{
  56. */
  57. #if defined(HAL_NOR_MODULE_ENABLED) || defined(HAL_NAND_MODULE_ENABLED) || defined(HAL_PCCARD_MODULE_ENABLED) \
  58. || defined(HAL_SRAM_MODULE_ENABLED)
  59. /** @defgroup FMC_LL FMC Low Layer
  60. * @brief FMC driver modules
  61. * @{
  62. */
  63. /* Private typedef -----------------------------------------------------------*/
  64. /* Private define ------------------------------------------------------------*/
  65. /** @defgroup FMC_LL_Private_Constants FMC Low Layer Private Constants
  66. * @{
  67. */
  68. /* ----------------------- FMC registers bit mask --------------------------- */
  69. #if defined(FMC_BANK1)
  70. /* --- BCR Register ---*/
  71. /* BCR register clear mask */
  72. /* --- BTR Register ---*/
  73. /* BTR register clear mask */
  74. #define BTR_CLEAR_MASK ((uint32_t)(FMC_BTRx_ADDSET | FMC_BTRx_ADDHLD |\
  75. FMC_BTRx_DATAST | FMC_BTRx_BUSTURN |\
  76. FMC_BTRx_CLKDIV | FMC_BTRx_DATLAT |\
  77. FMC_BTRx_ACCMOD))
  78. /* --- BWTR Register ---*/
  79. /* BWTR register clear mask */
  80. #define BWTR_CLEAR_MASK ((uint32_t)(FMC_BWTRx_ADDSET | FMC_BWTRx_ADDHLD |\
  81. FMC_BWTRx_DATAST | FMC_BWTRx_ACCMOD))
  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_PCRx_PWAITEN | FMC_PCRx_PBKEN | \
  87. FMC_PCRx_PTYP | FMC_PCRx_PWID | \
  88. FMC_PCRx_ECCEN | FMC_PCRx_TCLR | \
  89. FMC_PCRx_TAR | FMC_PCRx_ECCPS))
  90. /* --- PMEM Register ---*/
  91. /* PMEM register clear mask */
  92. #define PMEM_CLEAR_MASK ((uint32_t)(FMC_PMEMx_MEMSETx | FMC_PMEMx_MEMWAITx |\
  93. FMC_PMEMx_MEMHOLDx | FMC_PMEMx_MEMHIZx))
  94. /* --- PATT Register ---*/
  95. /* PATT register clear mask */
  96. #define PATT_CLEAR_MASK ((uint32_t)(FMC_PATTx_ATTSETx | FMC_PATTx_ATTWAITx |\
  97. FMC_PATTx_ATTHOLDx | FMC_PATTx_ATTHIZx))
  98. #endif /* FMC_BANK3 */
  99. #if defined(FMC_BANK4)
  100. /* --- PCR Register ---*/
  101. /* PCR register clear mask */
  102. #define PCR4_CLEAR_MASK ((uint32_t)(FMC_PCR4_PWAITEN | FMC_PCR4_PBKEN | \
  103. FMC_PCR4_PTYP | FMC_PCR4_PWID | \
  104. FMC_PCR4_ECCEN | FMC_PCR4_TCLR | \
  105. FMC_PCR4_TAR | FMC_PCR4_ECCPS))
  106. /* --- PMEM Register ---*/
  107. /* PMEM register clear mask */
  108. #define PMEM4_CLEAR_MASK ((uint32_t)(FMC_PMEM4_MEMSET4 | FMC_PMEM4_MEMWAIT4 |\
  109. FMC_PMEM4_MEMHOLD4 | FMC_PMEM4_MEMHIZ4))
  110. /* --- PATT Register ---*/
  111. /* PATT register clear mask */
  112. #define PATT4_CLEAR_MASK ((uint32_t)(FMC_PATT4_ATTSET4 | FMC_PATT4_ATTWAIT4 |\
  113. FMC_PATT4_ATTHOLD4 | FMC_PATT4_ATTHIZ4))
  114. /* --- PIO4 Register ---*/
  115. /* PIO4 register clear mask */
  116. #define PIO4_CLEAR_MASK ((uint32_t)(FMC_PIO4_IOSET4 | FMC_PIO4_IOWAIT4 | \
  117. FMC_PIO4_IOHOLD4 | FMC_PIO4_IOHIZ4))
  118. #endif /* FMC_BANK4 */
  119. /**
  120. * @}
  121. */
  122. /* Private macro -------------------------------------------------------------*/
  123. /* Private variables ---------------------------------------------------------*/
  124. /* Private function prototypes -----------------------------------------------*/
  125. /* Exported functions --------------------------------------------------------*/
  126. /** @defgroup FMC_LL_Exported_Functions FMC Low Layer Exported Functions
  127. * @{
  128. */
  129. #if defined(FMC_BANK1)
  130. /** @defgroup FMC_LL_Exported_Functions_NORSRAM FMC Low Layer NOR SRAM Exported Functions
  131. * @brief NORSRAM Controller functions
  132. *
  133. @verbatim
  134. ==============================================================================
  135. ##### How to use NORSRAM device driver #####
  136. ==============================================================================
  137. [..]
  138. This driver contains a set of APIs to interface with the FMC NORSRAM banks in order
  139. to run the NORSRAM external devices.
  140. (+) FMC NORSRAM bank reset using the function FMC_NORSRAM_DeInit()
  141. (+) FMC NORSRAM bank control configuration using the function FMC_NORSRAM_Init()
  142. (+) FMC NORSRAM bank timing configuration using the function FMC_NORSRAM_Timing_Init()
  143. (+) FMC NORSRAM bank extended timing configuration using the function
  144. FMC_NORSRAM_Extended_Timing_Init()
  145. (+) FMC NORSRAM bank enable/disable write operation using the functions
  146. FMC_NORSRAM_WriteOperation_Enable()/FMC_NORSRAM_WriteOperation_Disable()
  147. @endverbatim
  148. * @{
  149. */
  150. /** @defgroup FMC_LL_NORSRAM_Exported_Functions_Group1 Initialization and de-initialization functions
  151. * @brief Initialization and Configuration functions
  152. *
  153. @verbatim
  154. ==============================================================================
  155. ##### Initialization and de_initialization functions #####
  156. ==============================================================================
  157. [..]
  158. This section provides functions allowing to:
  159. (+) Initialize and configure the FMC NORSRAM interface
  160. (+) De-initialize the FMC NORSRAM interface
  161. (+) Configure the FMC clock and associated GPIOs
  162. @endverbatim
  163. * @{
  164. */
  165. /**
  166. * @brief Initialize the FMC_NORSRAM device according to the specified
  167. * control parameters in the FMC_NORSRAM_InitTypeDef
  168. * @param Device Pointer to NORSRAM device instance
  169. * @param Init Pointer to NORSRAM Initialization structure
  170. * @retval HAL status
  171. */
  172. HAL_StatusTypeDef FMC_NORSRAM_Init(FMC_NORSRAM_TypeDef *Device,
  173. FMC_NORSRAM_InitTypeDef *Init)
  174. {
  175. uint32_t flashaccess;
  176. uint32_t btcr_reg;
  177. uint32_t mask;
  178. /* Check the parameters */
  179. assert_param(IS_FMC_NORSRAM_DEVICE(Device));
  180. assert_param(IS_FMC_NORSRAM_BANK(Init->NSBank));
  181. assert_param(IS_FMC_MUX(Init->DataAddressMux));
  182. assert_param(IS_FMC_MEMORY(Init->MemoryType));
  183. assert_param(IS_FMC_NORSRAM_MEMORY_WIDTH(Init->MemoryDataWidth));
  184. assert_param(IS_FMC_BURSTMODE(Init->BurstAccessMode));
  185. assert_param(IS_FMC_WAIT_POLARITY(Init->WaitSignalPolarity));
  186. assert_param(IS_FMC_WRAP_MODE(Init->WrapMode));
  187. assert_param(IS_FMC_WAIT_SIGNAL_ACTIVE(Init->WaitSignalActive));
  188. assert_param(IS_FMC_WRITE_OPERATION(Init->WriteOperation));
  189. assert_param(IS_FMC_WAITE_SIGNAL(Init->WaitSignal));
  190. assert_param(IS_FMC_EXTENDED_MODE(Init->ExtendedMode));
  191. assert_param(IS_FMC_ASYNWAIT(Init->AsynchronousWait));
  192. assert_param(IS_FMC_WRITE_BURST(Init->WriteBurst));
  193. assert_param(IS_FMC_CONTINOUS_CLOCK(Init->ContinuousClock));
  194. /* Disable NORSRAM Device */
  195. __FMC_NORSRAM_DISABLE(Device, Init->NSBank);
  196. /* Set NORSRAM device control parameters */
  197. if (Init->MemoryType == FMC_MEMORY_TYPE_NOR)
  198. {
  199. flashaccess = FMC_NORSRAM_FLASH_ACCESS_ENABLE;
  200. }
  201. else
  202. {
  203. flashaccess = FMC_NORSRAM_FLASH_ACCESS_DISABLE;
  204. }
  205. btcr_reg = (flashaccess | \
  206. Init->DataAddressMux | \
  207. Init->MemoryType | \
  208. Init->MemoryDataWidth | \
  209. Init->BurstAccessMode | \
  210. Init->WaitSignalPolarity | \
  211. Init->WaitSignalActive | \
  212. Init->WriteOperation | \
  213. Init->WaitSignal | \
  214. Init->ExtendedMode | \
  215. Init->AsynchronousWait | \
  216. Init->WriteBurst);
  217. btcr_reg |= Init->WrapMode;
  218. btcr_reg |= Init->ContinuousClock;
  219. mask = (FMC_BCRx_MBKEN |
  220. FMC_BCRx_MUXEN |
  221. FMC_BCRx_MTYP |
  222. FMC_BCRx_MWID |
  223. FMC_BCRx_FACCEN |
  224. FMC_BCRx_BURSTEN |
  225. FMC_BCRx_WAITPOL |
  226. FMC_BCRx_WAITCFG |
  227. FMC_BCRx_WREN |
  228. FMC_BCRx_WAITEN |
  229. FMC_BCRx_EXTMOD |
  230. FMC_BCRx_ASYNCWAIT |
  231. FMC_BCRx_CBURSTRW);
  232. mask |= FMC_BCRx_WRAPMOD;
  233. mask |= FMC_BCR1_CCLKEN;
  234. MODIFY_REG(Device->BTCR[Init->NSBank], mask, btcr_reg);
  235. /* Configure synchronous mode when Continuous clock is enabled for bank2..4 */
  236. if ((Init->ContinuousClock == FMC_CONTINUOUS_CLOCK_SYNC_ASYNC) && (Init->NSBank != FMC_NORSRAM_BANK1))
  237. {
  238. MODIFY_REG(Device->BTCR[FMC_NORSRAM_BANK1], FMC_BCR1_CCLKEN, Init->ContinuousClock);
  239. }
  240. return HAL_OK;
  241. }
  242. /**
  243. * @brief DeInitialize the FMC_NORSRAM peripheral
  244. * @param Device Pointer to NORSRAM device instance
  245. * @param ExDevice Pointer to NORSRAM extended mode device instance
  246. * @param Bank NORSRAM bank number
  247. * @retval HAL status
  248. */
  249. HAL_StatusTypeDef FMC_NORSRAM_DeInit(FMC_NORSRAM_TypeDef *Device,
  250. FMC_NORSRAM_EXTENDED_TypeDef *ExDevice, uint32_t Bank)
  251. {
  252. /* Check the parameters */
  253. assert_param(IS_FMC_NORSRAM_DEVICE(Device));
  254. assert_param(IS_FMC_NORSRAM_EXTENDED_DEVICE(ExDevice));
  255. assert_param(IS_FMC_NORSRAM_BANK(Bank));
  256. /* Disable the FMC_NORSRAM device */
  257. __FMC_NORSRAM_DISABLE(Device, Bank);
  258. /* De-initialize the FMC_NORSRAM device */
  259. /* FMC_NORSRAM_BANK1 */
  260. if (Bank == FMC_NORSRAM_BANK1)
  261. {
  262. Device->BTCR[Bank] = 0x000030DBU;
  263. }
  264. /* FMC_NORSRAM_BANK2, FMC_NORSRAM_BANK3 or FMC_NORSRAM_BANK4 */
  265. else
  266. {
  267. Device->BTCR[Bank] = 0x000030D2U;
  268. }
  269. Device->BTCR[Bank + 1U] = 0x0FFFFFFFU;
  270. ExDevice->BWTR[Bank] = 0x0FFFFFFFU;
  271. return HAL_OK;
  272. }
  273. /**
  274. * @brief Initialize the FMC_NORSRAM Timing according to the specified
  275. * parameters in the FMC_NORSRAM_TimingTypeDef
  276. * @param Device Pointer to NORSRAM device instance
  277. * @param Timing Pointer to NORSRAM Timing structure
  278. * @param Bank NORSRAM bank number
  279. * @retval HAL status
  280. */
  281. HAL_StatusTypeDef FMC_NORSRAM_Timing_Init(FMC_NORSRAM_TypeDef *Device,
  282. FMC_NORSRAM_TimingTypeDef *Timing, uint32_t Bank)
  283. {
  284. uint32_t tmpr;
  285. /* Check the parameters */
  286. assert_param(IS_FMC_NORSRAM_DEVICE(Device));
  287. assert_param(IS_FMC_ADDRESS_SETUP_TIME(Timing->AddressSetupTime));
  288. assert_param(IS_FMC_ADDRESS_HOLD_TIME(Timing->AddressHoldTime));
  289. assert_param(IS_FMC_DATASETUP_TIME(Timing->DataSetupTime));
  290. assert_param(IS_FMC_TURNAROUND_TIME(Timing->BusTurnAroundDuration));
  291. assert_param(IS_FMC_CLK_DIV(Timing->CLKDivision));
  292. assert_param(IS_FMC_DATA_LATENCY(Timing->DataLatency));
  293. assert_param(IS_FMC_ACCESS_MODE(Timing->AccessMode));
  294. assert_param(IS_FMC_NORSRAM_BANK(Bank));
  295. /* Set FMC_NORSRAM device timing parameters */
  296. Device->BTCR[Bank + 1U] =
  297. (Timing->AddressSetupTime << FMC_BTRx_ADDSET_Pos) |
  298. (Timing->AddressHoldTime << FMC_BTRx_ADDHLD_Pos) |
  299. (Timing->DataSetupTime << FMC_BTRx_DATAST_Pos) |
  300. (Timing->BusTurnAroundDuration << FMC_BTRx_BUSTURN_Pos) |
  301. ((Timing->CLKDivision - 1U) << FMC_BTRx_CLKDIV_Pos) |
  302. ((Timing->DataLatency - 2U) << FMC_BTRx_DATLAT_Pos) |
  303. Timing->AccessMode;
  304. /* Configure Clock division value (in NORSRAM bank 1) when continuous clock is enabled */
  305. if (HAL_IS_BIT_SET(Device->BTCR[FMC_NORSRAM_BANK1], FMC_BCR1_CCLKEN))
  306. {
  307. tmpr = (uint32_t)(Device->BTCR[FMC_NORSRAM_BANK1 + 1U] & ~((0x0FU) << FMC_BTRx_CLKDIV_Pos));
  308. tmpr |= (uint32_t)(((Timing->CLKDivision) - 1U) << FMC_BTRx_CLKDIV_Pos);
  309. MODIFY_REG(Device->BTCR[FMC_NORSRAM_BANK1 + 1U], FMC_BTRx_CLKDIV, tmpr);
  310. }
  311. return HAL_OK;
  312. }
  313. /**
  314. * @brief Initialize the FMC_NORSRAM Extended mode Timing according to the specified
  315. * parameters in the FMC_NORSRAM_TimingTypeDef
  316. * @param Device Pointer to NORSRAM device instance
  317. * @param Timing Pointer to NORSRAM Timing structure
  318. * @param Bank NORSRAM bank number
  319. * @param ExtendedMode FMC Extended Mode
  320. * This parameter can be one of the following values:
  321. * @arg FMC_EXTENDED_MODE_DISABLE
  322. * @arg FMC_EXTENDED_MODE_ENABLE
  323. * @retval HAL status
  324. */
  325. HAL_StatusTypeDef FMC_NORSRAM_Extended_Timing_Init(FMC_NORSRAM_EXTENDED_TypeDef *Device,
  326. FMC_NORSRAM_TimingTypeDef *Timing, uint32_t Bank,
  327. uint32_t ExtendedMode)
  328. {
  329. /* Check the parameters */
  330. assert_param(IS_FMC_EXTENDED_MODE(ExtendedMode));
  331. /* Set NORSRAM device timing register for write configuration, if extended mode is used */
  332. if (ExtendedMode == FMC_EXTENDED_MODE_ENABLE)
  333. {
  334. /* Check the parameters */
  335. assert_param(IS_FMC_NORSRAM_EXTENDED_DEVICE(Device));
  336. assert_param(IS_FMC_ADDRESS_SETUP_TIME(Timing->AddressSetupTime));
  337. assert_param(IS_FMC_ADDRESS_HOLD_TIME(Timing->AddressHoldTime));
  338. assert_param(IS_FMC_DATASETUP_TIME(Timing->DataSetupTime));
  339. assert_param(IS_FMC_ACCESS_MODE(Timing->AccessMode));
  340. assert_param(IS_FMC_NORSRAM_BANK(Bank));
  341. /* Set NORSRAM device timing register for write configuration, if extended mode is used */
  342. MODIFY_REG(Device->BWTR[Bank], BWTR_CLEAR_MASK, (Timing->AddressSetupTime |
  343. ((Timing->AddressHoldTime) << FMC_BWTRx_ADDHLD_Pos) |
  344. ((Timing->DataSetupTime) << FMC_BWTRx_DATAST_Pos) |
  345. Timing->AccessMode));
  346. }
  347. else
  348. {
  349. Device->BWTR[Bank] = 0x0FFFFFFFU;
  350. }
  351. return HAL_OK;
  352. }
  353. /**
  354. * @}
  355. */
  356. /** @addtogroup FMC_LL_NORSRAM_Private_Functions_Group2
  357. * @brief management functions
  358. *
  359. @verbatim
  360. ==============================================================================
  361. ##### FMC_NORSRAM Control functions #####
  362. ==============================================================================
  363. [..]
  364. This subsection provides a set of functions allowing to control dynamically
  365. the FMC NORSRAM interface.
  366. @endverbatim
  367. * @{
  368. */
  369. /**
  370. * @brief Enables dynamically FMC_NORSRAM write operation.
  371. * @param Device Pointer to NORSRAM device instance
  372. * @param Bank NORSRAM bank number
  373. * @retval HAL status
  374. */
  375. HAL_StatusTypeDef FMC_NORSRAM_WriteOperation_Enable(FMC_NORSRAM_TypeDef *Device, uint32_t Bank)
  376. {
  377. /* Check the parameters */
  378. assert_param(IS_FMC_NORSRAM_DEVICE(Device));
  379. assert_param(IS_FMC_NORSRAM_BANK(Bank));
  380. /* Enable write operation */
  381. SET_BIT(Device->BTCR[Bank], FMC_WRITE_OPERATION_ENABLE);
  382. return HAL_OK;
  383. }
  384. /**
  385. * @brief Disables dynamically FMC_NORSRAM write operation.
  386. * @param Device Pointer to NORSRAM device instance
  387. * @param Bank NORSRAM bank number
  388. * @retval HAL status
  389. */
  390. HAL_StatusTypeDef FMC_NORSRAM_WriteOperation_Disable(FMC_NORSRAM_TypeDef *Device, uint32_t Bank)
  391. {
  392. /* Check the parameters */
  393. assert_param(IS_FMC_NORSRAM_DEVICE(Device));
  394. assert_param(IS_FMC_NORSRAM_BANK(Bank));
  395. /* Disable write operation */
  396. CLEAR_BIT(Device->BTCR[Bank], FMC_WRITE_OPERATION_ENABLE);
  397. return HAL_OK;
  398. }
  399. /**
  400. * @}
  401. */
  402. /**
  403. * @}
  404. */
  405. #endif /* FMC_BANK1 */
  406. #if defined(FMC_BANK3)
  407. /** @defgroup FMC_LL_Exported_Functions_NAND FMC Low Layer NAND Exported Functions
  408. * @brief NAND Controller functions
  409. *
  410. @verbatim
  411. ==============================================================================
  412. ##### How to use NAND device driver #####
  413. ==============================================================================
  414. [..]
  415. This driver contains a set of APIs to interface with the FMC NAND banks in order
  416. to run the NAND external devices.
  417. (+) FMC NAND bank reset using the function FMC_NAND_DeInit()
  418. (+) FMC NAND bank control configuration using the function FMC_NAND_Init()
  419. (+) FMC NAND bank common space timing configuration using the function
  420. FMC_NAND_CommonSpace_Timing_Init()
  421. (+) FMC NAND bank attribute space timing configuration using the function
  422. FMC_NAND_AttributeSpace_Timing_Init()
  423. (+) FMC NAND bank enable/disable ECC correction feature using the functions
  424. FMC_NAND_ECC_Enable()/FMC_NAND_ECC_Disable()
  425. (+) FMC NAND bank get ECC correction code using the function FMC_NAND_GetECC()
  426. @endverbatim
  427. * @{
  428. */
  429. /** @defgroup FMC_LL_NAND_Exported_Functions_Group1 Initialization and de-initialization functions
  430. * @brief Initialization and Configuration functions
  431. *
  432. @verbatim
  433. ==============================================================================
  434. ##### Initialization and de_initialization functions #####
  435. ==============================================================================
  436. [..]
  437. This section provides functions allowing to:
  438. (+) Initialize and configure the FMC NAND interface
  439. (+) De-initialize the FMC NAND interface
  440. (+) Configure the FMC clock and associated GPIOs
  441. @endverbatim
  442. * @{
  443. */
  444. /**
  445. * @brief Initializes the FMC_NAND device according to the specified
  446. * control parameters in the FMC_NAND_HandleTypeDef
  447. * @param Device Pointer to NAND device instance
  448. * @param Init Pointer to NAND Initialization structure
  449. * @retval HAL status
  450. */
  451. HAL_StatusTypeDef FMC_NAND_Init(FMC_NAND_TypeDef *Device, FMC_NAND_InitTypeDef *Init)
  452. {
  453. /* Check the parameters */
  454. assert_param(IS_FMC_NAND_DEVICE(Device));
  455. assert_param(IS_FMC_NAND_BANK(Init->NandBank));
  456. assert_param(IS_FMC_WAIT_FEATURE(Init->Waitfeature));
  457. assert_param(IS_FMC_NAND_MEMORY_WIDTH(Init->MemoryDataWidth));
  458. assert_param(IS_FMC_ECC_STATE(Init->EccComputation));
  459. assert_param(IS_FMC_ECCPAGE_SIZE(Init->ECCPageSize));
  460. assert_param(IS_FMC_TCLR_TIME(Init->TCLRSetupTime));
  461. assert_param(IS_FMC_TAR_TIME(Init->TARSetupTime));
  462. /* Set NAND device control parameters */
  463. if (Init->NandBank == FMC_NAND_BANK2)
  464. {
  465. /* NAND bank 2 registers configuration */
  466. MODIFY_REG(Device->PCR2, PCR_CLEAR_MASK, (Init->Waitfeature |
  467. FMC_PCR_MEMORY_TYPE_NAND |
  468. Init->MemoryDataWidth |
  469. Init->EccComputation |
  470. Init->ECCPageSize |
  471. ((Init->TCLRSetupTime) << FMC_PCRx_TCLR_Pos) |
  472. ((Init->TARSetupTime) << FMC_PCRx_TAR_Pos)));
  473. }
  474. else
  475. {
  476. /* NAND bank 3 registers configuration */
  477. MODIFY_REG(Device->PCR3, PCR_CLEAR_MASK, (Init->Waitfeature |
  478. FMC_PCR_MEMORY_TYPE_NAND |
  479. Init->MemoryDataWidth |
  480. Init->EccComputation |
  481. Init->ECCPageSize |
  482. ((Init->TCLRSetupTime) << FMC_PCRx_TCLR_Pos) |
  483. ((Init->TARSetupTime) << FMC_PCRx_TAR_Pos)));
  484. }
  485. return HAL_OK;
  486. }
  487. /**
  488. * @brief Initializes the FMC_NAND Common space Timing according to the specified
  489. * parameters in the FMC_NAND_PCC_TimingTypeDef
  490. * @param Device Pointer to NAND device instance
  491. * @param Timing Pointer to NAND timing structure
  492. * @param Bank NAND bank number
  493. * @retval HAL status
  494. */
  495. HAL_StatusTypeDef FMC_NAND_CommonSpace_Timing_Init(FMC_NAND_TypeDef *Device,
  496. FMC_NAND_PCC_TimingTypeDef *Timing, uint32_t Bank)
  497. {
  498. /* Check the parameters */
  499. assert_param(IS_FMC_NAND_DEVICE(Device));
  500. assert_param(IS_FMC_SETUP_TIME(Timing->SetupTime));
  501. assert_param(IS_FMC_WAIT_TIME(Timing->WaitSetupTime));
  502. assert_param(IS_FMC_HOLD_TIME(Timing->HoldSetupTime));
  503. assert_param(IS_FMC_HIZ_TIME(Timing->HiZSetupTime));
  504. assert_param(IS_FMC_NAND_BANK(Bank));
  505. /* Set FMC_NAND device timing parameters */
  506. if (Bank == FMC_NAND_BANK2)
  507. {
  508. /* NAND bank 2 registers configuration */
  509. MODIFY_REG(Device->PMEM2, PMEM_CLEAR_MASK, (Timing->SetupTime |
  510. ((Timing->WaitSetupTime) << FMC_PMEMx_MEMWAITx_Pos) |
  511. ((Timing->HoldSetupTime) << FMC_PMEMx_MEMHOLDx_Pos) |
  512. ((Timing->HiZSetupTime) << FMC_PMEMx_MEMHIZx_Pos)));
  513. }
  514. else
  515. {
  516. /* NAND bank 3 registers configuration */
  517. MODIFY_REG(Device->PMEM3, PMEM_CLEAR_MASK, (Timing->SetupTime |
  518. ((Timing->WaitSetupTime) << FMC_PMEMx_MEMWAITx_Pos) |
  519. ((Timing->HoldSetupTime) << FMC_PMEMx_MEMHOLDx_Pos) |
  520. ((Timing->HiZSetupTime) << FMC_PMEMx_MEMHIZx_Pos)));
  521. }
  522. return HAL_OK;
  523. }
  524. /**
  525. * @brief Initializes the FMC_NAND Attribute space Timing according to the specified
  526. * parameters in the FMC_NAND_PCC_TimingTypeDef
  527. * @param Device Pointer to NAND device instance
  528. * @param Timing Pointer to NAND timing structure
  529. * @param Bank NAND bank number
  530. * @retval HAL status
  531. */
  532. HAL_StatusTypeDef FMC_NAND_AttributeSpace_Timing_Init(FMC_NAND_TypeDef *Device,
  533. FMC_NAND_PCC_TimingTypeDef *Timing, uint32_t Bank)
  534. {
  535. /* Check the parameters */
  536. assert_param(IS_FMC_NAND_DEVICE(Device));
  537. assert_param(IS_FMC_SETUP_TIME(Timing->SetupTime));
  538. assert_param(IS_FMC_WAIT_TIME(Timing->WaitSetupTime));
  539. assert_param(IS_FMC_HOLD_TIME(Timing->HoldSetupTime));
  540. assert_param(IS_FMC_HIZ_TIME(Timing->HiZSetupTime));
  541. assert_param(IS_FMC_NAND_BANK(Bank));
  542. /* Set FMC_NAND device timing parameters */
  543. if (Bank == FMC_NAND_BANK2)
  544. {
  545. /* NAND bank 2 registers configuration */
  546. MODIFY_REG(Device->PATT2, PATT_CLEAR_MASK, (Timing->SetupTime |
  547. ((Timing->WaitSetupTime) << FMC_PATTx_ATTWAITx_Pos) |
  548. ((Timing->HoldSetupTime) << FMC_PATTx_ATTHOLDx_Pos) |
  549. ((Timing->HiZSetupTime) << FMC_PATTx_ATTHIZx_Pos)));
  550. }
  551. else
  552. {
  553. /* NAND bank 3 registers configuration */
  554. MODIFY_REG(Device->PATT3, PATT_CLEAR_MASK, (Timing->SetupTime |
  555. ((Timing->WaitSetupTime) << FMC_PATTx_ATTWAITx_Pos) |
  556. ((Timing->HoldSetupTime) << FMC_PATTx_ATTHOLDx_Pos) |
  557. ((Timing->HiZSetupTime) << FMC_PATTx_ATTHIZx_Pos)));
  558. }
  559. return HAL_OK;
  560. }
  561. /**
  562. * @brief DeInitializes the FMC_NAND device
  563. * @param Device Pointer to NAND device instance
  564. * @param Bank NAND bank number
  565. * @retval HAL status
  566. */
  567. HAL_StatusTypeDef FMC_NAND_DeInit(FMC_NAND_TypeDef *Device, uint32_t Bank)
  568. {
  569. /* Check the parameters */
  570. assert_param(IS_FMC_NAND_DEVICE(Device));
  571. assert_param(IS_FMC_NAND_BANK(Bank));
  572. /* Disable the NAND Bank */
  573. __FMC_NAND_DISABLE(Device, Bank);
  574. /* De-initialize the NAND Bank */
  575. if (Bank == FMC_NAND_BANK2)
  576. {
  577. /* Set the FMC_NAND_BANK2 registers to their reset values */
  578. WRITE_REG(Device->PCR2, 0x00000018U);
  579. WRITE_REG(Device->SR2, 0x00000040U);
  580. WRITE_REG(Device->PMEM2, 0xFCFCFCFCU);
  581. WRITE_REG(Device->PATT2, 0xFCFCFCFCU);
  582. }
  583. /* FMC_Bank3_NAND */
  584. else
  585. {
  586. /* Set the FMC_NAND_BANK3 registers to their reset values */
  587. WRITE_REG(Device->PCR3, 0x00000018U);
  588. WRITE_REG(Device->SR3, 0x00000040U);
  589. WRITE_REG(Device->PMEM3, 0xFCFCFCFCU);
  590. WRITE_REG(Device->PATT3, 0xFCFCFCFCU);
  591. }
  592. return HAL_OK;
  593. }
  594. /**
  595. * @}
  596. */
  597. /** @defgroup HAL_FMC_NAND_Group2 Peripheral Control functions
  598. * @brief management functions
  599. *
  600. @verbatim
  601. ==============================================================================
  602. ##### FMC_NAND Control functions #####
  603. ==============================================================================
  604. [..]
  605. This subsection provides a set of functions allowing to control dynamically
  606. the FMC NAND interface.
  607. @endverbatim
  608. * @{
  609. */
  610. /**
  611. * @brief Enables dynamically FMC_NAND ECC feature.
  612. * @param Device Pointer to NAND device instance
  613. * @param Bank NAND bank number
  614. * @retval HAL status
  615. */
  616. HAL_StatusTypeDef FMC_NAND_ECC_Enable(FMC_NAND_TypeDef *Device, uint32_t Bank)
  617. {
  618. /* Check the parameters */
  619. assert_param(IS_FMC_NAND_DEVICE(Device));
  620. assert_param(IS_FMC_NAND_BANK(Bank));
  621. /* Enable ECC feature */
  622. if (Bank == FMC_NAND_BANK2)
  623. {
  624. SET_BIT(Device->PCR2, FMC_PCRx_ECCEN);
  625. }
  626. else
  627. {
  628. SET_BIT(Device->PCR3, FMC_PCRx_ECCEN);
  629. }
  630. return HAL_OK;
  631. }
  632. /**
  633. * @brief Disables dynamically FMC_NAND ECC feature.
  634. * @param Device Pointer to NAND device instance
  635. * @param Bank NAND bank number
  636. * @retval HAL status
  637. */
  638. HAL_StatusTypeDef FMC_NAND_ECC_Disable(FMC_NAND_TypeDef *Device, uint32_t Bank)
  639. {
  640. /* Check the parameters */
  641. assert_param(IS_FMC_NAND_DEVICE(Device));
  642. assert_param(IS_FMC_NAND_BANK(Bank));
  643. /* Disable ECC feature */
  644. if (Bank == FMC_NAND_BANK2)
  645. {
  646. CLEAR_BIT(Device->PCR2, FMC_PCRx_ECCEN);
  647. }
  648. else
  649. {
  650. CLEAR_BIT(Device->PCR3, FMC_PCRx_ECCEN);
  651. }
  652. return HAL_OK;
  653. }
  654. /**
  655. * @brief Disables dynamically FMC_NAND ECC feature.
  656. * @param Device Pointer to NAND device instance
  657. * @param ECCval Pointer to ECC value
  658. * @param Bank NAND bank number
  659. * @param Timeout Timeout wait value
  660. * @retval HAL status
  661. */
  662. HAL_StatusTypeDef FMC_NAND_GetECC(FMC_NAND_TypeDef *Device, uint32_t *ECCval, uint32_t Bank,
  663. uint32_t Timeout)
  664. {
  665. uint32_t tickstart;
  666. /* Check the parameters */
  667. assert_param(IS_FMC_NAND_DEVICE(Device));
  668. assert_param(IS_FMC_NAND_BANK(Bank));
  669. /* Get tick */
  670. tickstart = HAL_GetTick();
  671. /* Wait until FIFO is empty */
  672. while (__FMC_NAND_GET_FLAG(Device, Bank, FMC_FLAG_FEMPT) == RESET)
  673. {
  674. /* Check for the Timeout */
  675. if (Timeout != HAL_MAX_DELAY)
  676. {
  677. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  678. {
  679. return HAL_TIMEOUT;
  680. }
  681. }
  682. }
  683. if (Bank == FMC_NAND_BANK2)
  684. {
  685. /* Get the ECCR2 register value */
  686. *ECCval = (uint32_t)Device->ECCR2;
  687. }
  688. else
  689. {
  690. /* Get the ECCR3 register value */
  691. *ECCval = (uint32_t)Device->ECCR3;
  692. }
  693. return HAL_OK;
  694. }
  695. /**
  696. * @}
  697. */
  698. #endif /* FMC_BANK3 */
  699. #if defined(FMC_BANK4)
  700. /** @addtogroup FMC_LL_PCCARD
  701. * @brief PCCARD Controller functions
  702. *
  703. @verbatim
  704. ==============================================================================
  705. ##### How to use PCCARD device driver #####
  706. ==============================================================================
  707. [..]
  708. This driver contains a set of APIs to interface with the FMC PCCARD bank in order
  709. to run the PCCARD/compact flash external devices.
  710. (+) FMC PCCARD bank reset using the function FMC_PCCARD_DeInit()
  711. (+) FMC PCCARD bank control configuration using the function FMC_PCCARD_Init()
  712. (+) FMC PCCARD bank common space timing configuration using the function
  713. FMC_PCCARD_CommonSpace_Timing_Init()
  714. (+) FMC PCCARD bank attribute space timing configuration using the function
  715. FMC_PCCARD_AttributeSpace_Timing_Init()
  716. (+) FMC PCCARD bank IO space timing configuration using the function
  717. FMC_PCCARD_IOSpace_Timing_Init()
  718. @endverbatim
  719. * @{
  720. */
  721. /** @addtogroup FMC_LL_PCCARD_Private_Functions_Group1
  722. * @brief Initialization and Configuration functions
  723. *
  724. @verbatim
  725. ==============================================================================
  726. ##### Initialization and de_initialization functions #####
  727. ==============================================================================
  728. [..]
  729. This section provides functions allowing to:
  730. (+) Initialize and configure the FMC PCCARD interface
  731. (+) De-initialize the FMC PCCARD interface
  732. (+) Configure the FMC clock and associated GPIOs
  733. @endverbatim
  734. * @{
  735. */
  736. /**
  737. * @brief Initializes the FMC_PCCARD device according to the specified
  738. * control parameters in the FMC_PCCARD_HandleTypeDef
  739. * @param Device Pointer to PCCARD device instance
  740. * @param Init Pointer to PCCARD Initialization structure
  741. * @retval HAL status
  742. */
  743. HAL_StatusTypeDef FMC_PCCARD_Init(FMC_PCCARD_TypeDef *Device, FMC_PCCARD_InitTypeDef *Init)
  744. {
  745. /* Check the parameters */
  746. assert_param(IS_FMC_PCCARD_DEVICE(Device));
  747. #if defined(FMC_BANK3)
  748. assert_param(IS_FMC_WAIT_FEATURE(Init->Waitfeature));
  749. assert_param(IS_FMC_TCLR_TIME(Init->TCLRSetupTime));
  750. assert_param(IS_FMC_TAR_TIME(Init->TARSetupTime));
  751. #endif /* FMC_BANK3 */
  752. /* Set FMC_PCCARD device control parameters */
  753. MODIFY_REG(Device->PCR4,
  754. (FMC_PCR4_PTYP |
  755. FMC_PCR4_PWAITEN |
  756. FMC_PCR4_PWID |
  757. FMC_PCR4_TCLR |
  758. FMC_PCR4_TAR),
  759. (FMC_PCR_MEMORY_TYPE_PCCARD |
  760. Init->Waitfeature |
  761. FMC_NAND_PCC_MEM_BUS_WIDTH_16 |
  762. (Init->TCLRSetupTime << FMC_PCR4_TCLR_Pos) |
  763. (Init->TARSetupTime << FMC_PCR4_TAR_Pos)));
  764. return HAL_OK;
  765. }
  766. /**
  767. * @brief Initializes the FMC_PCCARD Common space Timing according to the specified
  768. * parameters in the FMC_NAND_PCC_TimingTypeDef
  769. * @param Device Pointer to PCCARD device instance
  770. * @param Timing Pointer to PCCARD timing structure
  771. * @retval HAL status
  772. */
  773. HAL_StatusTypeDef FMC_PCCARD_CommonSpace_Timing_Init(FMC_PCCARD_TypeDef *Device,
  774. FMC_NAND_PCC_TimingTypeDef *Timing)
  775. {
  776. /* Check the parameters */
  777. assert_param(IS_FMC_PCCARD_DEVICE(Device));
  778. #if defined(FMC_BANK3)
  779. assert_param(IS_FMC_SETUP_TIME(Timing->SetupTime));
  780. assert_param(IS_FMC_WAIT_TIME(Timing->WaitSetupTime));
  781. assert_param(IS_FMC_HOLD_TIME(Timing->HoldSetupTime));
  782. assert_param(IS_FMC_HIZ_TIME(Timing->HiZSetupTime));
  783. #endif /* FMC_BANK3 */
  784. /* Set PCCARD timing parameters */
  785. MODIFY_REG(Device->PMEM4, PMEM4_CLEAR_MASK,
  786. (Timing->SetupTime |
  787. ((Timing->WaitSetupTime) << FMC_PMEM4_MEMWAIT4_Pos) |
  788. ((Timing->HoldSetupTime) << FMC_PMEM4_MEMHOLD4_Pos) |
  789. ((Timing->HiZSetupTime) << FMC_PMEM4_MEMHIZ4_Pos)));
  790. return HAL_OK;
  791. }
  792. /**
  793. * @brief Initializes the FMC_PCCARD Attribute space Timing according to the specified
  794. * parameters in the FMC_NAND_PCC_TimingTypeDef
  795. * @param Device Pointer to PCCARD device instance
  796. * @param Timing Pointer to PCCARD timing structure
  797. * @retval HAL status
  798. */
  799. HAL_StatusTypeDef FMC_PCCARD_AttributeSpace_Timing_Init(FMC_PCCARD_TypeDef *Device,
  800. FMC_NAND_PCC_TimingTypeDef *Timing)
  801. {
  802. /* Check the parameters */
  803. assert_param(IS_FMC_PCCARD_DEVICE(Device));
  804. #if defined(FMC_BANK3)
  805. assert_param(IS_FMC_SETUP_TIME(Timing->SetupTime));
  806. assert_param(IS_FMC_WAIT_TIME(Timing->WaitSetupTime));
  807. assert_param(IS_FMC_HOLD_TIME(Timing->HoldSetupTime));
  808. assert_param(IS_FMC_HIZ_TIME(Timing->HiZSetupTime));
  809. #endif /* FMC_BANK3 */
  810. /* Set PCCARD timing parameters */
  811. MODIFY_REG(Device->PATT4, PATT4_CLEAR_MASK,
  812. (Timing->SetupTime |
  813. ((Timing->WaitSetupTime) << FMC_PATT4_ATTWAIT4_Pos) |
  814. ((Timing->HoldSetupTime) << FMC_PATT4_ATTHOLD4_Pos) |
  815. ((Timing->HiZSetupTime) << FMC_PATT4_ATTHIZ4_Pos)));
  816. return HAL_OK;
  817. }
  818. /**
  819. * @brief Initializes the FMC_PCCARD IO space Timing according to the specified
  820. * parameters in the FMC_NAND_PCC_TimingTypeDef
  821. * @param Device Pointer to PCCARD device instance
  822. * @param Timing Pointer to PCCARD timing structure
  823. * @retval HAL status
  824. */
  825. HAL_StatusTypeDef FMC_PCCARD_IOSpace_Timing_Init(FMC_PCCARD_TypeDef *Device,
  826. FMC_NAND_PCC_TimingTypeDef *Timing)
  827. {
  828. /* Check the parameters */
  829. assert_param(IS_FMC_PCCARD_DEVICE(Device));
  830. #if defined(FMC_BANK3)
  831. assert_param(IS_FMC_SETUP_TIME(Timing->SetupTime));
  832. assert_param(IS_FMC_WAIT_TIME(Timing->WaitSetupTime));
  833. assert_param(IS_FMC_HOLD_TIME(Timing->HoldSetupTime));
  834. assert_param(IS_FMC_HIZ_TIME(Timing->HiZSetupTime));
  835. #endif /* FMC_BANK3 */
  836. /* Set FMC_PCCARD device timing parameters */
  837. MODIFY_REG(Device->PIO4, PIO4_CLEAR_MASK,
  838. (Timing->SetupTime |
  839. (Timing->WaitSetupTime << FMC_PIO4_IOWAIT4_Pos) |
  840. (Timing->HoldSetupTime << FMC_PIO4_IOHOLD4_Pos) |
  841. (Timing->HiZSetupTime << FMC_PIO4_IOHIZ4_Pos)));
  842. return HAL_OK;
  843. }
  844. /**
  845. * @brief DeInitializes the FMC_PCCARD device
  846. * @param Device Pointer to PCCARD device instance
  847. * @retval HAL status
  848. */
  849. HAL_StatusTypeDef FMC_PCCARD_DeInit(FMC_PCCARD_TypeDef *Device)
  850. {
  851. /* Check the parameters */
  852. assert_param(IS_FMC_PCCARD_DEVICE(Device));
  853. /* Disable the FMC_PCCARD device */
  854. __FMC_PCCARD_DISABLE(Device);
  855. /* De-initialize the FMC_PCCARD device */
  856. Device->PCR4 = 0x00000018U;
  857. Device->SR4 = 0x00000040U;
  858. Device->PMEM4 = 0xFCFCFCFCU;
  859. Device->PATT4 = 0xFCFCFCFCU;
  860. Device->PIO4 = 0xFCFCFCFCU;
  861. return HAL_OK;
  862. }
  863. /**
  864. * @}
  865. */
  866. #endif /* FMC_BANK4 */
  867. /**
  868. * @}
  869. */
  870. /**
  871. * @}
  872. */
  873. #endif /* HAL_NOR_MODULE_ENABLED */
  874. /**
  875. * @}
  876. */
  877. /**
  878. * @}
  879. */