stm32l4xx_ll_fmc.c 32 KB

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