stm32l4xx_ll_fmc.c 29 KB

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