stm32f4xx_ll_fsmc.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_ll_fsmc.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 09-March-2015
  7. * @brief FSMC Low Layer HAL module driver.
  8. *
  9. * This file provides firmware functions to manage the following
  10. * functionalities of the Flexible Static Memory Controller (FSMC) peripheral memories:
  11. * + Initialization/de-initialization functions
  12. * + Peripheral Control functions
  13. * + Peripheral State functions
  14. *
  15. @verbatim
  16. ==============================================================================
  17. ##### FSMC peripheral features #####
  18. ==============================================================================
  19. [..] The Flexible static memory controller (FSMC) includes two memory controllers:
  20. (+) The NOR/PSRAM memory controller
  21. (+) The NAND/PC Card memory controller
  22. [..] The FSMC functional block makes the interface with synchronous and asynchronous static
  23. memories, SDRAM memories, and 16-bit PC memory cards. 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 FSMC performs
  28. only one access at a time to an external device.
  29. The main features of the FSMC controller are the following:
  30. (+) Interface with static-memory mapped devices including:
  31. (++) Static random access memory (SRAM).
  32. (++) Read-only memory (ROM).
  33. (++) NOR Flash memory/OneNAND Flash memory.
  34. (++) PSRAM (4 memory banks).
  35. (++) 16-bit PC Card compatible devices.
  36. (++) Two banks of NAND Flash memory with ECC hardware to check up to 8 Kbytes of
  37. data.
  38. (+) Independent Chip Select control for each memory bank.
  39. (+) Independent configuration for each memory bank.
  40. @endverbatim
  41. ******************************************************************************
  42. * @attention
  43. *
  44. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  45. *
  46. * Redistribution and use in source and binary forms, with or without modification,
  47. * are permitted provided that the following conditions are met:
  48. * 1. Redistributions of source code must retain the above copyright notice,
  49. * this list of conditions and the following disclaimer.
  50. * 2. Redistributions in binary form must reproduce the above copyright notice,
  51. * this list of conditions and the following disclaimer in the documentation
  52. * and/or other materials provided with the distribution.
  53. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  54. * may be used to endorse or promote products derived from this software
  55. * without specific prior written permission.
  56. *
  57. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  58. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  59. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  60. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  61. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  62. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  63. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  64. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  65. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  66. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  67. *
  68. ******************************************************************************
  69. */
  70. /* Includes ------------------------------------------------------------------*/
  71. #include "stm32f4xx_hal.h"
  72. /** @addtogroup STM32F4xx_HAL_Driver
  73. * @{
  74. */
  75. /** @defgroup FSMC_LL FSMC Low Layer
  76. * @brief FSMC driver modules
  77. * @{
  78. */
  79. #if defined (HAL_SRAM_MODULE_ENABLED) || defined(HAL_NOR_MODULE_ENABLED) || defined(HAL_NAND_MODULE_ENABLED) || defined(HAL_PCCARD_MODULE_ENABLED)
  80. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)
  81. /* Private typedef -----------------------------------------------------------*/
  82. /* Private define ------------------------------------------------------------*/
  83. /* Private macro -------------------------------------------------------------*/
  84. /* Private variables ---------------------------------------------------------*/
  85. /* Private function prototypes -----------------------------------------------*/
  86. /* Private functions ---------------------------------------------------------*/
  87. /** @addtogroup FSMC_LL_Private_Functions
  88. * @{
  89. */
  90. /** @addtogroup FSMC_LL_NORSRAM
  91. * @brief NORSRAM Controller functions
  92. *
  93. @verbatim
  94. ==============================================================================
  95. ##### How to use NORSRAM device driver #####
  96. ==============================================================================
  97. [..]
  98. This driver contains a set of APIs to interface with the FSMC NORSRAM banks in order
  99. to run the NORSRAM external devices.
  100. (+) FSMC NORSRAM bank reset using the function FSMC_NORSRAM_DeInit()
  101. (+) FSMC NORSRAM bank control configuration using the function FSMC_NORSRAM_Init()
  102. (+) FSMC NORSRAM bank timing configuration using the function FSMC_NORSRAM_Timing_Init()
  103. (+) FSMC NORSRAM bank extended timing configuration using the function
  104. FSMC_NORSRAM_Extended_Timing_Init()
  105. (+) FSMC NORSRAM bank enable/disable write operation using the functions
  106. FSMC_NORSRAM_WriteOperation_Enable()/FSMC_NORSRAM_WriteOperation_Disable()
  107. @endverbatim
  108. * @{
  109. */
  110. /** @addtogroup FSMC_LL_NORSRAM_Private_Functions_Group1
  111. * @brief Initialization and Configuration functions
  112. *
  113. @verbatim
  114. ==============================================================================
  115. ##### Initialization and de_initialization functions #####
  116. ==============================================================================
  117. [..]
  118. This section provides functions allowing to:
  119. (+) Initialize and configure the FSMC NORSRAM interface
  120. (+) De-initialize the FSMC NORSRAM interface
  121. (+) Configure the FSMC clock and associated GPIOs
  122. @endverbatim
  123. * @{
  124. */
  125. /**
  126. * @brief Initialize the FSMC_NORSRAM device according to the specified
  127. * control parameters in the FSMC_NORSRAM_InitTypeDef
  128. * @param Device: Pointer to NORSRAM device instance
  129. * @param Init: Pointer to NORSRAM Initialization structure
  130. * @retval HAL status
  131. */
  132. HAL_StatusTypeDef FSMC_NORSRAM_Init(FSMC_NORSRAM_TypeDef *Device, FSMC_NORSRAM_InitTypeDef* Init)
  133. {
  134. uint32_t tmpr = 0;
  135. /* Check the parameters */
  136. assert_param(IS_FSMC_NORSRAM_BANK(Init->NSBank));
  137. assert_param(IS_FSMC_MUX(Init->DataAddressMux));
  138. assert_param(IS_FSMC_MEMORY(Init->MemoryType));
  139. assert_param(IS_FSMC_NORSRAM_MEMORY_WIDTH(Init->MemoryDataWidth));
  140. assert_param(IS_FSMC_BURSTMODE(Init->BurstAccessMode));
  141. assert_param(IS_FSMC_WAIT_POLARITY(Init->WaitSignalPolarity));
  142. assert_param(IS_FSMC_WRAP_MODE(Init->WrapMode));
  143. assert_param(IS_FSMC_WAIT_SIGNAL_ACTIVE(Init->WaitSignalActive));
  144. assert_param(IS_FSMC_WRITE_OPERATION(Init->WriteOperation));
  145. assert_param(IS_FSMC_WAITE_SIGNAL(Init->WaitSignal));
  146. assert_param(IS_FSMC_EXTENDED_MODE(Init->ExtendedMode));
  147. assert_param(IS_FSMC_ASYNWAIT(Init->AsynchronousWait));
  148. assert_param(IS_FSMC_WRITE_BURST(Init->WriteBurst));
  149. /* Get the BTCR register value */
  150. tmpr = Device->BTCR[Init->NSBank];
  151. /* Clear MBKEN, MUXEN, MTYP, MWID, FACCEN, BURSTEN, WAITPOL, WRAPMOD, WAITCFG, WREN,
  152. WAITEN, EXTMOD, ASYNCWAIT, CBURSTRW and CCLKEN bits */
  153. tmpr &= ((uint32_t)~(FSMC_BCR1_MBKEN | FSMC_BCR1_MUXEN | FSMC_BCR1_MTYP | \
  154. FSMC_BCR1_MWID | FSMC_BCR1_FACCEN | FSMC_BCR1_BURSTEN | \
  155. FSMC_BCR1_WAITPOL | FSMC_BCR1_WRAPMOD | FSMC_BCR1_WAITCFG | \
  156. FSMC_BCR1_WREN | FSMC_BCR1_WAITEN | FSMC_BCR1_EXTMOD | \
  157. FSMC_BCR1_ASYNCWAIT | FSMC_BCR1_CBURSTRW));
  158. /* Set NORSRAM device control parameters */
  159. tmpr |= (uint32_t)(Init->DataAddressMux |\
  160. Init->MemoryType |\
  161. Init->MemoryDataWidth |\
  162. Init->BurstAccessMode |\
  163. Init->WaitSignalPolarity |\
  164. Init->WrapMode |\
  165. Init->WaitSignalActive |\
  166. Init->WriteOperation |\
  167. Init->WaitSignal |\
  168. Init->ExtendedMode |\
  169. Init->AsynchronousWait |\
  170. Init->WriteBurst
  171. );
  172. if(Init->MemoryType == FSMC_MEMORY_TYPE_NOR)
  173. {
  174. tmpr |= (uint32_t)FSMC_NORSRAM_FLASH_ACCESS_ENABLE;
  175. }
  176. Device->BTCR[Init->NSBank] = tmpr;
  177. return HAL_OK;
  178. }
  179. /**
  180. * @brief DeInitialize the FSMC_NORSRAM peripheral
  181. * @param Device: Pointer to NORSRAM device instance
  182. * @param ExDevice: Pointer to NORSRAM extended mode device instance
  183. * @param Bank: NORSRAM bank number
  184. * @retval HAL status
  185. */
  186. HAL_StatusTypeDef FSMC_NORSRAM_DeInit(FSMC_NORSRAM_TypeDef *Device, FSMC_NORSRAM_EXTENDED_TypeDef *ExDevice, uint32_t Bank)
  187. {
  188. /* Check the parameters */
  189. assert_param(IS_FSMC_NORSRAM_DEVICE(Device));
  190. assert_param(IS_FSMC_NORSRAM_EXTENDED_DEVICE(ExDevice));
  191. /* Disable the FSMC_NORSRAM device */
  192. __FSMC_NORSRAM_DISABLE(Device, Bank);
  193. /* De-initialize the FSMC_NORSRAM device */
  194. /* FSMC_NORSRAM_BANK1 */
  195. if(Bank == FSMC_NORSRAM_BANK1)
  196. {
  197. Device->BTCR[Bank] = 0x000030DB;
  198. }
  199. /* FSMC_NORSRAM_BANK2, FSMC_NORSRAM_BANK3 or FSMC_NORSRAM_BANK4 */
  200. else
  201. {
  202. Device->BTCR[Bank] = 0x000030D2;
  203. }
  204. Device->BTCR[Bank + 1] = 0x0FFFFFFF;
  205. ExDevice->BWTR[Bank] = 0x0FFFFFFF;
  206. return HAL_OK;
  207. }
  208. /**
  209. * @brief Initialize the FSMC_NORSRAM Timing according to the specified
  210. * parameters in the FSMC_NORSRAM_TimingTypeDef
  211. * @param Device: Pointer to NORSRAM device instance
  212. * @param Timing: Pointer to NORSRAM Timing structure
  213. * @param Bank: NORSRAM bank number
  214. * @retval HAL status
  215. */
  216. HAL_StatusTypeDef FSMC_NORSRAM_Timing_Init(FSMC_NORSRAM_TypeDef *Device, FSMC_NORSRAM_TimingTypeDef *Timing, uint32_t Bank)
  217. {
  218. uint32_t tmpr = 0;
  219. /* Check the parameters */
  220. assert_param(IS_FSMC_ADDRESS_SETUP_TIME(Timing->AddressSetupTime));
  221. assert_param(IS_FSMC_ADDRESS_HOLD_TIME(Timing->AddressHoldTime));
  222. assert_param(IS_FSMC_DATASETUP_TIME(Timing->DataSetupTime));
  223. assert_param(IS_FSMC_TURNAROUND_TIME(Timing->BusTurnAroundDuration));
  224. assert_param(IS_FSMC_CLK_DIV(Timing->CLKDivision));
  225. assert_param(IS_FSMC_DATA_LATENCY(Timing->DataLatency));
  226. assert_param(IS_FSMC_ACCESS_MODE(Timing->AccessMode));
  227. /* Get the BTCR register value */
  228. tmpr = Device->BTCR[Bank + 1];
  229. /* Clear ADDSET, ADDHLD, DATAST, BUSTURN, CLKDIV, DATLAT and ACCMOD bits */
  230. tmpr &= ((uint32_t)~(FSMC_BTR1_ADDSET | FSMC_BTR1_ADDHLD | FSMC_BTR1_DATAST | \
  231. FSMC_BTR1_BUSTURN | FSMC_BTR1_CLKDIV | FSMC_BTR1_DATLAT | \
  232. FSMC_BTR1_ACCMOD));
  233. /* Set FSMC_NORSRAM device timing parameters */
  234. tmpr |= (uint32_t)(Timing->AddressSetupTime |\
  235. ((Timing->AddressHoldTime) << 4) |\
  236. ((Timing->DataSetupTime) << 8) |\
  237. ((Timing->BusTurnAroundDuration) << 16) |\
  238. (((Timing->CLKDivision)-1) << 20) |\
  239. (((Timing->DataLatency)-2) << 24) |\
  240. (Timing->AccessMode));
  241. Device->BTCR[Bank + 1] = tmpr;
  242. return HAL_OK;
  243. }
  244. /**
  245. * @brief Initialize the FSMC_NORSRAM Extended mode Timing according to the specified
  246. * parameters in the FSMC_NORSRAM_TimingTypeDef
  247. * @param Device: Pointer to NORSRAM device instance
  248. * @param Timing: Pointer to NORSRAM Timing structure
  249. * @param Bank: NORSRAM bank number
  250. * @retval HAL status
  251. */
  252. HAL_StatusTypeDef FSMC_NORSRAM_Extended_Timing_Init(FSMC_NORSRAM_EXTENDED_TypeDef *Device, FSMC_NORSRAM_TimingTypeDef *Timing, uint32_t Bank, uint32_t ExtendedMode)
  253. {
  254. uint32_t tmpr = 0;
  255. /* Set NORSRAM device timing register for write configuration, if extended mode is used */
  256. if(ExtendedMode == FSMC_EXTENDED_MODE_ENABLE)
  257. {
  258. /* Check the parameters */
  259. assert_param(IS_FSMC_ADDRESS_SETUP_TIME(Timing->AddressSetupTime));
  260. assert_param(IS_FSMC_ADDRESS_HOLD_TIME(Timing->AddressHoldTime));
  261. assert_param(IS_FSMC_DATASETUP_TIME(Timing->DataSetupTime));
  262. assert_param(IS_FSMC_TURNAROUND_TIME(Timing->BusTurnAroundDuration));
  263. assert_param(IS_FSMC_CLK_DIV(Timing->CLKDivision));
  264. assert_param(IS_FSMC_DATA_LATENCY(Timing->DataLatency));
  265. assert_param(IS_FSMC_ACCESS_MODE(Timing->AccessMode));
  266. /* Get the BWTR register value */
  267. tmpr = Device->BWTR[Bank];
  268. /* Clear ADDSET, ADDHLD, DATAST, BUSTURN, CLKDIV, DATLAT and ACCMOD bits */
  269. tmpr &= ((uint32_t)~(FSMC_BWTR1_ADDSET | FSMC_BWTR1_ADDHLD | FSMC_BWTR1_DATAST | \
  270. FSMC_BWTR1_BUSTURN | FSMC_BWTR1_CLKDIV | FSMC_BWTR1_DATLAT | \
  271. FSMC_BWTR1_ACCMOD));
  272. tmpr |= (uint32_t)(Timing->AddressSetupTime |\
  273. ((Timing->AddressHoldTime) << 4) |\
  274. ((Timing->DataSetupTime) << 8) |\
  275. ((Timing->BusTurnAroundDuration) << 16) |\
  276. (((Timing->CLKDivision)-1) << 20) |\
  277. (((Timing->DataLatency)-2) << 24) |\
  278. (Timing->AccessMode));
  279. Device->BWTR[Bank] = tmpr;
  280. }
  281. else
  282. {
  283. Device->BWTR[Bank] = 0x0FFFFFFF;
  284. }
  285. return HAL_OK;
  286. }
  287. /**
  288. * @}
  289. */
  290. /** @addtogroup FSMC_LL_NORSRAM_Private_Functions_Group2
  291. * @brief management functions
  292. *
  293. @verbatim
  294. ==============================================================================
  295. ##### FSMC_NORSRAM Control functions #####
  296. ==============================================================================
  297. [..]
  298. This subsection provides a set of functions allowing to control dynamically
  299. the FSMC NORSRAM interface.
  300. @endverbatim
  301. * @{
  302. */
  303. /**
  304. * @brief Enables dynamically FSMC_NORSRAM write operation.
  305. * @param Device: Pointer to NORSRAM device instance
  306. * @param Bank: NORSRAM bank number
  307. * @retval HAL status
  308. */
  309. HAL_StatusTypeDef FSMC_NORSRAM_WriteOperation_Enable(FSMC_NORSRAM_TypeDef *Device, uint32_t Bank)
  310. {
  311. /* Enable write operation */
  312. Device->BTCR[Bank] |= FSMC_WRITE_OPERATION_ENABLE;
  313. return HAL_OK;
  314. }
  315. /**
  316. * @brief Disables dynamically FSMC_NORSRAM write operation.
  317. * @param Device: Pointer to NORSRAM device instance
  318. * @param Bank: NORSRAM bank number
  319. * @retval HAL status
  320. */
  321. HAL_StatusTypeDef FSMC_NORSRAM_WriteOperation_Disable(FSMC_NORSRAM_TypeDef *Device, uint32_t Bank)
  322. {
  323. /* Disable write operation */
  324. Device->BTCR[Bank] &= ~FSMC_WRITE_OPERATION_ENABLE;
  325. return HAL_OK;
  326. }
  327. /**
  328. * @}
  329. */
  330. /**
  331. * @}
  332. */
  333. /** @addtogroup FSMC_LL_NAND
  334. * @brief NAND Controller functions
  335. *
  336. @verbatim
  337. ==============================================================================
  338. ##### How to use NAND device driver #####
  339. ==============================================================================
  340. [..]
  341. This driver contains a set of APIs to interface with the FSMC NAND banks in order
  342. to run the NAND external devices.
  343. (+) FSMC NAND bank reset using the function FSMC_NAND_DeInit()
  344. (+) FSMC NAND bank control configuration using the function FSMC_NAND_Init()
  345. (+) FSMC NAND bank common space timing configuration using the function
  346. FSMC_NAND_CommonSpace_Timing_Init()
  347. (+) FSMC NAND bank attribute space timing configuration using the function
  348. FSMC_NAND_AttributeSpace_Timing_Init()
  349. (+) FSMC NAND bank enable/disable ECC correction feature using the functions
  350. FSMC_NAND_ECC_Enable()/FSMC_NAND_ECC_Disable()
  351. (+) FSMC NAND bank get ECC correction code using the function FSMC_NAND_GetECC()
  352. @endverbatim
  353. * @{
  354. */
  355. /** @addtogroup FSMC_LL_NAND_Private_Functions_Group1
  356. * @brief Initialization and Configuration functions
  357. *
  358. @verbatim
  359. ==============================================================================
  360. ##### Initialization and de_initialization functions #####
  361. ==============================================================================
  362. [..]
  363. This section provides functions allowing to:
  364. (+) Initialize and configure the FSMC NAND interface
  365. (+) De-initialize the FSMC NAND interface
  366. (+) Configure the FSMC clock and associated GPIOs
  367. @endverbatim
  368. * @{
  369. */
  370. /**
  371. * @brief Initializes the FSMC_NAND device according to the specified
  372. * control parameters in the FSMC_NAND_HandleTypeDef
  373. * @param Device: Pointer to NAND device instance
  374. * @param Init: Pointer to NAND Initialization structure
  375. * @retval HAL status
  376. */
  377. HAL_StatusTypeDef FSMC_NAND_Init(FSMC_NAND_TypeDef *Device, FSMC_NAND_InitTypeDef *Init)
  378. {
  379. uint32_t tmpr = 0;
  380. /* Check the parameters */
  381. assert_param(IS_FSMC_NAND_BANK(Init->NandBank));
  382. assert_param(IS_FSMC_WAIT_FEATURE(Init->Waitfeature));
  383. assert_param(IS_FSMC_NAND_MEMORY_WIDTH(Init->MemoryDataWidth));
  384. assert_param(IS_FSMC_ECC_STATE(Init->EccComputation));
  385. assert_param(IS_FSMC_ECCPAGE_SIZE(Init->ECCPageSize));
  386. assert_param(IS_FSMC_TCLR_TIME(Init->TCLRSetupTime));
  387. assert_param(IS_FSMC_TAR_TIME(Init->TARSetupTime));
  388. if(Init->NandBank == FSMC_NAND_BANK2)
  389. {
  390. /* Get the NAND bank 2 register value */
  391. tmpr = Device->PCR2;
  392. }
  393. else
  394. {
  395. /* Get the NAND bank 3 register value */
  396. tmpr = Device->PCR3;
  397. }
  398. /* Clear PWAITEN, PBKEN, PTYP, PWID, ECCEN, TCLR, TAR and ECCPS bits */
  399. tmpr &= ((uint32_t)~(FSMC_PCR2_PWAITEN | FSMC_PCR2_PBKEN | FSMC_PCR2_PTYP | \
  400. FSMC_PCR2_PWID | FSMC_PCR2_ECCEN | FSMC_PCR2_TCLR | \
  401. FSMC_PCR2_TAR | FSMC_PCR2_ECCPS));
  402. /* Set NAND device control parameters */
  403. tmpr |= (uint32_t)(Init->Waitfeature |\
  404. FSMC_PCR_MEMORY_TYPE_NAND |\
  405. Init->MemoryDataWidth |\
  406. Init->EccComputation |\
  407. Init->ECCPageSize |\
  408. ((Init->TCLRSetupTime) << 9) |\
  409. ((Init->TARSetupTime) << 13));
  410. if(Init->NandBank == FSMC_NAND_BANK2)
  411. {
  412. /* NAND bank 2 registers configuration */
  413. Device->PCR2 = tmpr;
  414. }
  415. else
  416. {
  417. /* NAND bank 3 registers configuration */
  418. Device->PCR3 = tmpr;
  419. }
  420. return HAL_OK;
  421. }
  422. /**
  423. * @brief Initializes the FSMC_NAND Common space Timing according to the specified
  424. * parameters in the FSMC_NAND_PCC_TimingTypeDef
  425. * @param Device: Pointer to NAND device instance
  426. * @param Timing: Pointer to NAND timing structure
  427. * @param Bank: NAND bank number
  428. * @retval HAL status
  429. */
  430. HAL_StatusTypeDef FSMC_NAND_CommonSpace_Timing_Init(FSMC_NAND_TypeDef *Device, FSMC_NAND_PCC_TimingTypeDef *Timing, uint32_t Bank)
  431. {
  432. uint32_t tmpr = 0;
  433. /* Check the parameters */
  434. assert_param(IS_FSMC_SETUP_TIME(Timing->SetupTime));
  435. assert_param(IS_FSMC_WAIT_TIME(Timing->WaitSetupTime));
  436. assert_param(IS_FSMC_HOLD_TIME(Timing->HoldSetupTime));
  437. assert_param(IS_FSMC_HIZ_TIME(Timing->HiZSetupTime));
  438. if(Bank == FSMC_NAND_BANK2)
  439. {
  440. /* Get the NAND bank 2 register value */
  441. tmpr = Device->PMEM2;
  442. }
  443. else
  444. {
  445. /* Get the NAND bank 3 register value */
  446. tmpr = Device->PMEM3;
  447. }
  448. /* Clear MEMSETx, MEMWAITx, MEMHOLDx and MEMHIZx bits */
  449. tmpr &= ((uint32_t)~(FSMC_PMEM2_MEMSET2 | FSMC_PMEM2_MEMWAIT2 | FSMC_PMEM2_MEMHOLD2 | \
  450. FSMC_PMEM2_MEMHIZ2));
  451. /* Set FSMC_NAND device timing parameters */
  452. tmpr |= (uint32_t)(Timing->SetupTime |\
  453. ((Timing->WaitSetupTime) << 8) |\
  454. ((Timing->HoldSetupTime) << 16) |\
  455. ((Timing->HiZSetupTime) << 24)
  456. );
  457. if(Bank == FSMC_NAND_BANK2)
  458. {
  459. /* NAND bank 2 registers configuration */
  460. Device->PMEM2 = tmpr;
  461. }
  462. else
  463. {
  464. /* NAND bank 3 registers configuration */
  465. Device->PMEM3 = tmpr;
  466. }
  467. return HAL_OK;
  468. }
  469. /**
  470. * @brief Initializes the FSMC_NAND Attribute space Timing according to the specified
  471. * parameters in the FSMC_NAND_PCC_TimingTypeDef
  472. * @param Device: Pointer to NAND device instance
  473. * @param Timing: Pointer to NAND timing structure
  474. * @param Bank: NAND bank number
  475. * @retval HAL status
  476. */
  477. HAL_StatusTypeDef FSMC_NAND_AttributeSpace_Timing_Init(FSMC_NAND_TypeDef *Device, FSMC_NAND_PCC_TimingTypeDef *Timing, uint32_t Bank)
  478. {
  479. uint32_t tmpr = 0;
  480. /* Check the parameters */
  481. assert_param(IS_FSMC_SETUP_TIME(Timing->SetupTime));
  482. assert_param(IS_FSMC_WAIT_TIME(Timing->WaitSetupTime));
  483. assert_param(IS_FSMC_HOLD_TIME(Timing->HoldSetupTime));
  484. assert_param(IS_FSMC_HIZ_TIME(Timing->HiZSetupTime));
  485. if(Bank == FSMC_NAND_BANK2)
  486. {
  487. /* Get the NAND bank 2 register value */
  488. tmpr = Device->PATT2;
  489. }
  490. else
  491. {
  492. /* Get the NAND bank 3 register value */
  493. tmpr = Device->PATT3;
  494. }
  495. /* Clear ATTSETx, ATTWAITx, ATTHOLDx and ATTHIZx bits */
  496. tmpr &= ((uint32_t)~(FSMC_PATT2_ATTSET2 | FSMC_PATT2_ATTWAIT2 | FSMC_PATT2_ATTHOLD2 | \
  497. FSMC_PATT2_ATTHIZ2));
  498. /* Set FSMC_NAND device timing parameters */
  499. tmpr |= (uint32_t)(Timing->SetupTime |\
  500. ((Timing->WaitSetupTime) << 8) |\
  501. ((Timing->HoldSetupTime) << 16) |\
  502. ((Timing->HiZSetupTime) << 24)
  503. );
  504. if(Bank == FSMC_NAND_BANK2)
  505. {
  506. /* NAND bank 2 registers configuration */
  507. Device->PATT2 = tmpr;
  508. }
  509. else
  510. {
  511. /* NAND bank 3 registers configuration */
  512. Device->PATT3 = tmpr;
  513. }
  514. return HAL_OK;
  515. }
  516. /**
  517. * @brief DeInitializes the FSMC_NAND device
  518. * @param Device: Pointer to NAND device instance
  519. * @param Bank: NAND bank number
  520. * @retval HAL status
  521. */
  522. HAL_StatusTypeDef FSMC_NAND_DeInit(FSMC_NAND_TypeDef *Device, uint32_t Bank)
  523. {
  524. /* Disable the NAND Bank */
  525. __FSMC_NAND_DISABLE(Device, Bank);
  526. /* De-initialize the NAND Bank */
  527. if(Bank == FSMC_NAND_BANK2)
  528. {
  529. /* Set the FSMC_NAND_BANK2 registers to their reset values */
  530. Device->PCR2 = 0x00000018;
  531. Device->SR2 = 0x00000040;
  532. Device->PMEM2 = 0xFCFCFCFC;
  533. Device->PATT2 = 0xFCFCFCFC;
  534. }
  535. /* FSMC_Bank3_NAND */
  536. else
  537. {
  538. /* Set the FSMC_NAND_BANK3 registers to their reset values */
  539. Device->PCR3 = 0x00000018;
  540. Device->SR3 = 0x00000040;
  541. Device->PMEM3 = 0xFCFCFCFC;
  542. Device->PATT3 = 0xFCFCFCFC;
  543. }
  544. return HAL_OK;
  545. }
  546. /**
  547. * @}
  548. */
  549. /** @addtogroup FSMC_LL_NAND_Private_Functions_Group2
  550. * @brief management functions
  551. *
  552. @verbatim
  553. ==============================================================================
  554. ##### FSMC_NAND Control functions #####
  555. ==============================================================================
  556. [..]
  557. This subsection provides a set of functions allowing to control dynamically
  558. the FSMC NAND interface.
  559. @endverbatim
  560. * @{
  561. */
  562. /**
  563. * @brief Enables dynamically FSMC_NAND ECC feature.
  564. * @param Device: Pointer to NAND device instance
  565. * @param Bank: NAND bank number
  566. * @retval HAL status
  567. */
  568. HAL_StatusTypeDef FSMC_NAND_ECC_Enable(FSMC_NAND_TypeDef *Device, uint32_t Bank)
  569. {
  570. /* Enable ECC feature */
  571. if(Bank == FSMC_NAND_BANK2)
  572. {
  573. Device->PCR2 |= FSMC_PCR2_ECCEN;
  574. }
  575. else
  576. {
  577. Device->PCR3 |= FSMC_PCR3_ECCEN;
  578. }
  579. return HAL_OK;
  580. }
  581. /**
  582. * @brief Disables dynamically FSMC_NAND ECC feature.
  583. * @param Device: Pointer to NAND device instance
  584. * @param Bank: NAND bank number
  585. * @retval HAL status
  586. */
  587. HAL_StatusTypeDef FSMC_NAND_ECC_Disable(FSMC_NAND_TypeDef *Device, uint32_t Bank)
  588. {
  589. /* Disable ECC feature */
  590. if(Bank == FSMC_NAND_BANK2)
  591. {
  592. Device->PCR2 &= ~FSMC_PCR2_ECCEN;
  593. }
  594. else
  595. {
  596. Device->PCR3 &= ~FSMC_PCR3_ECCEN;
  597. }
  598. return HAL_OK;
  599. }
  600. /**
  601. * @brief Disables dynamically FSMC_NAND ECC feature.
  602. * @param Device: Pointer to NAND device instance
  603. * @param ECCval: Pointer to ECC value
  604. * @param Bank: NAND bank number
  605. * @param Timeout: Timeout wait value
  606. * @retval HAL status
  607. */
  608. HAL_StatusTypeDef FSMC_NAND_GetECC(FSMC_NAND_TypeDef *Device, uint32_t *ECCval, uint32_t Bank, uint32_t Timeout)
  609. {
  610. uint32_t tickstart = 0;
  611. /* Check the parameters */
  612. assert_param(IS_FSMC_NAND_DEVICE(Device));
  613. assert_param(IS_FSMC_NAND_BANK(Bank));
  614. /* Get tick */
  615. tickstart = HAL_GetTick();
  616. /* Wait until FIFO is empty */
  617. while(__FSMC_NAND_GET_FLAG(Device, Bank, FSMC_FLAG_FEMPT) == RESET)
  618. {
  619. /* Check for the Timeout */
  620. if(Timeout != HAL_MAX_DELAY)
  621. {
  622. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  623. {
  624. return HAL_TIMEOUT;
  625. }
  626. }
  627. }
  628. if(Bank == FSMC_NAND_BANK2)
  629. {
  630. /* Get the ECCR2 register value */
  631. *ECCval = (uint32_t)Device->ECCR2;
  632. }
  633. else
  634. {
  635. /* Get the ECCR3 register value */
  636. *ECCval = (uint32_t)Device->ECCR3;
  637. }
  638. return HAL_OK;
  639. }
  640. /**
  641. * @}
  642. */
  643. /**
  644. * @}
  645. */
  646. /** @addtogroup FSMC_LL_PCCARD
  647. * @brief PCCARD Controller functions
  648. *
  649. @verbatim
  650. ==============================================================================
  651. ##### How to use PCCARD device driver #####
  652. ==============================================================================
  653. [..]
  654. This driver contains a set of APIs to interface with the FSMC PCCARD bank in order
  655. to run the PCCARD/compact flash external devices.
  656. (+) FSMC PCCARD bank reset using the function FSMC_PCCARD_DeInit()
  657. (+) FSMC PCCARD bank control configuration using the function FSMC_PCCARD_Init()
  658. (+) FSMC PCCARD bank common space timing configuration using the function
  659. FSMC_PCCARD_CommonSpace_Timing_Init()
  660. (+) FSMC PCCARD bank attribute space timing configuration using the function
  661. FSMC_PCCARD_AttributeSpace_Timing_Init()
  662. (+) FSMC PCCARD bank IO space timing configuration using the function
  663. FSMC_PCCARD_IOSpace_Timing_Init()
  664. @endverbatim
  665. * @{
  666. */
  667. /** @addtogroup FSMC_LL_PCCARD_Private_Functions_Group1
  668. * @brief Initialization and Configuration functions
  669. *
  670. @verbatim
  671. ==============================================================================
  672. ##### Initialization and de_initialization functions #####
  673. ==============================================================================
  674. [..]
  675. This section provides functions allowing to:
  676. (+) Initialize and configure the FSMC PCCARD interface
  677. (+) De-initialize the FSMC PCCARD interface
  678. (+) Configure the FSMC clock and associated GPIOs
  679. @endverbatim
  680. * @{
  681. */
  682. /**
  683. * @brief Initializes the FSMC_PCCARD device according to the specified
  684. * control parameters in the FSMC_PCCARD_HandleTypeDef
  685. * @param Device: Pointer to PCCARD device instance
  686. * @param Init: Pointer to PCCARD Initialization structure
  687. * @retval HAL status
  688. */
  689. HAL_StatusTypeDef FSMC_PCCARD_Init(FSMC_PCCARD_TypeDef *Device, FSMC_PCCARD_InitTypeDef *Init)
  690. {
  691. uint32_t tmpr = 0;
  692. /* Check the parameters */
  693. assert_param(IS_FSMC_WAIT_FEATURE(Init->Waitfeature));
  694. assert_param(IS_FSMC_TCLR_TIME(Init->TCLRSetupTime));
  695. assert_param(IS_FSMC_TAR_TIME(Init->TARSetupTime));
  696. /* Get PCCARD control register value */
  697. tmpr = Device->PCR4;
  698. /* Clear TAR, TCLR, PWAITEN and PWID bits */
  699. tmpr &= ((uint32_t)~(FSMC_PCR4_TAR | FSMC_PCR4_TCLR | FSMC_PCR4_PWAITEN | \
  700. FSMC_PCR4_PWID));
  701. /* Set FSMC_PCCARD device control parameters */
  702. tmpr |= (uint32_t)(Init->Waitfeature |\
  703. FSMC_NAND_PCC_MEM_BUS_WIDTH_16 |\
  704. (Init->TCLRSetupTime << 9) |\
  705. (Init->TARSetupTime << 13));
  706. Device->PCR4 = tmpr;
  707. return HAL_OK;
  708. }
  709. /**
  710. * @brief Initializes the FSMC_PCCARD Common space Timing according to the specified
  711. * parameters in the FSMC_NAND_PCC_TimingTypeDef
  712. * @param Device: Pointer to PCCARD device instance
  713. * @param Timing: Pointer to PCCARD timing structure
  714. * @retval HAL status
  715. */
  716. HAL_StatusTypeDef FSMC_PCCARD_CommonSpace_Timing_Init(FSMC_PCCARD_TypeDef *Device, FSMC_NAND_PCC_TimingTypeDef *Timing)
  717. {
  718. uint32_t tmpr = 0;
  719. /* Check the parameters */
  720. assert_param(IS_FSMC_SETUP_TIME(Timing->SetupTime));
  721. assert_param(IS_FSMC_WAIT_TIME(Timing->WaitSetupTime));
  722. assert_param(IS_FSMC_HOLD_TIME(Timing->HoldSetupTime));
  723. assert_param(IS_FSMC_HIZ_TIME(Timing->HiZSetupTime));
  724. /* Get PCCARD common space timing register value */
  725. tmpr = Device->PMEM4;
  726. /* Clear MEMSETx, MEMWAITx, MEMHOLDx and MEMHIZx bits */
  727. tmpr &= ((uint32_t)~(FSMC_PMEM4_MEMSET4 | FSMC_PMEM4_MEMWAIT4 | FSMC_PMEM4_MEMHOLD4 | \
  728. FSMC_PMEM4_MEMHIZ4));
  729. /* Set PCCARD timing parameters */
  730. tmpr |= (uint32_t)((Timing->SetupTime |\
  731. ((Timing->WaitSetupTime) << 8) |\
  732. (Timing->HoldSetupTime) << 16) |\
  733. ((Timing->HiZSetupTime) << 24));
  734. Device->PMEM4 = tmpr;
  735. return HAL_OK;
  736. }
  737. /**
  738. * @brief Initializes the FSMC_PCCARD Attribute space Timing according to the specified
  739. * parameters in the FSMC_NAND_PCC_TimingTypeDef
  740. * @param Device: Pointer to PCCARD device instance
  741. * @param Timing: Pointer to PCCARD timing structure
  742. * @retval HAL status
  743. */
  744. HAL_StatusTypeDef FSMC_PCCARD_AttributeSpace_Timing_Init(FSMC_PCCARD_TypeDef *Device, FSMC_NAND_PCC_TimingTypeDef *Timing)
  745. {
  746. uint32_t tmpr = 0;
  747. /* Check the parameters */
  748. assert_param(IS_FSMC_SETUP_TIME(Timing->SetupTime));
  749. assert_param(IS_FSMC_WAIT_TIME(Timing->WaitSetupTime));
  750. assert_param(IS_FSMC_HOLD_TIME(Timing->HoldSetupTime));
  751. assert_param(IS_FSMC_HIZ_TIME(Timing->HiZSetupTime));
  752. /* Get PCCARD timing parameters */
  753. tmpr = Device->PATT4;
  754. /* Clear ATTSETx, ATTWAITx, ATTHOLDx and ATTHIZx bits */
  755. tmpr &= ((uint32_t)~(FSMC_PATT4_ATTSET4 | FSMC_PATT4_ATTWAIT4 | FSMC_PATT4_ATTHOLD4 | \
  756. FSMC_PATT4_ATTHIZ4));
  757. /* Set PCCARD timing parameters */
  758. tmpr |= (uint32_t)(Timing->SetupTime |\
  759. ((Timing->WaitSetupTime) << 8) |\
  760. ((Timing->HoldSetupTime) << 16) |\
  761. ((Timing->HiZSetupTime) << 24));
  762. Device->PATT4 = tmpr;
  763. return HAL_OK;
  764. }
  765. /**
  766. * @brief Initializes the FSMC_PCCARD IO space Timing according to the specified
  767. * parameters in the FSMC_NAND_PCC_TimingTypeDef
  768. * @param Device: Pointer to PCCARD device instance
  769. * @param Timing: Pointer to PCCARD timing structure
  770. * @retval HAL status
  771. */
  772. HAL_StatusTypeDef FSMC_PCCARD_IOSpace_Timing_Init(FSMC_PCCARD_TypeDef *Device, FSMC_NAND_PCC_TimingTypeDef *Timing)
  773. {
  774. uint32_t tmpr = 0;
  775. /* Check the parameters */
  776. assert_param(IS_FSMC_SETUP_TIME(Timing->SetupTime));
  777. assert_param(IS_FSMC_WAIT_TIME(Timing->WaitSetupTime));
  778. assert_param(IS_FSMC_HOLD_TIME(Timing->HoldSetupTime));
  779. assert_param(IS_FSMC_HIZ_TIME(Timing->HiZSetupTime));
  780. /* Get FSMC_PCCARD device timing parameters */
  781. tmpr = Device->PIO4;
  782. /* Clear IOSET4, IOWAIT4, IOHOLD4 and IOHIZ4 bits */
  783. tmpr &= ((uint32_t)~(FSMC_PIO4_IOSET4 | FSMC_PIO4_IOWAIT4 | FSMC_PIO4_IOHOLD4 | \
  784. FSMC_PIO4_IOHIZ4));
  785. /* Set FSMC_PCCARD device timing parameters */
  786. tmpr |= (uint32_t)(Timing->SetupTime |\
  787. ((Timing->WaitSetupTime) << 8) |\
  788. ((Timing->HoldSetupTime) << 16) |\
  789. ((Timing->HiZSetupTime) << 24));
  790. Device->PIO4 = tmpr;
  791. return HAL_OK;
  792. }
  793. /**
  794. * @brief DeInitializes the FSMC_PCCARD device
  795. * @param Device: Pointer to PCCARD device instance
  796. * @retval HAL status
  797. */
  798. HAL_StatusTypeDef FSMC_PCCARD_DeInit(FSMC_PCCARD_TypeDef *Device)
  799. {
  800. /* Disable the FSMC_PCCARD device */
  801. __FSMC_PCCARD_DISABLE(Device);
  802. /* De-initialize the FSMC_PCCARD device */
  803. Device->PCR4 = 0x00000018;
  804. Device->SR4 = 0x00000000;
  805. Device->PMEM4 = 0xFCFCFCFC;
  806. Device->PATT4 = 0xFCFCFCFC;
  807. Device->PIO4 = 0xFCFCFCFC;
  808. return HAL_OK;
  809. }
  810. /**
  811. * @}
  812. */
  813. /**
  814. * @}
  815. */
  816. /**
  817. * @}
  818. */
  819. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx */
  820. #endif /* HAL_SRAM_MODULE_ENABLED || HAL_NOR_MODULE_ENABLED || HAL_NAND_MODULE_ENABLED || HAL_PCCARD_MODULE_ENABLED */
  821. /**
  822. * @}
  823. */
  824. /**
  825. * @}
  826. */
  827. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/