stm32f4xx_ll_fsmc.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_ll_fsmc.c
  4. * @author MCD Application Team
  5. * @version V1.4.3
  6. * @date 11-December-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_DEVICE(Device));
  137. assert_param(IS_FSMC_NORSRAM_BANK(Init->NSBank));
  138. assert_param(IS_FSMC_MUX(Init->DataAddressMux));
  139. assert_param(IS_FSMC_MEMORY(Init->MemoryType));
  140. assert_param(IS_FSMC_NORSRAM_MEMORY_WIDTH(Init->MemoryDataWidth));
  141. assert_param(IS_FSMC_BURSTMODE(Init->BurstAccessMode));
  142. assert_param(IS_FSMC_WAIT_POLARITY(Init->WaitSignalPolarity));
  143. assert_param(IS_FSMC_WRAP_MODE(Init->WrapMode));
  144. assert_param(IS_FSMC_WAIT_SIGNAL_ACTIVE(Init->WaitSignalActive));
  145. assert_param(IS_FSMC_WRITE_OPERATION(Init->WriteOperation));
  146. assert_param(IS_FSMC_WAITE_SIGNAL(Init->WaitSignal));
  147. assert_param(IS_FSMC_EXTENDED_MODE(Init->ExtendedMode));
  148. assert_param(IS_FSMC_ASYNWAIT(Init->AsynchronousWait));
  149. assert_param(IS_FSMC_WRITE_BURST(Init->WriteBurst));
  150. assert_param(IS_FSMC_PAGESIZE(Init->PageSize));
  151. /* Get the BTCR register value */
  152. tmpr = Device->BTCR[Init->NSBank];
  153. /* Clear MBKEN, MUXEN, MTYP, MWID, FACCEN, BURSTEN, WAITPOL, WRAPMOD, WAITCFG, WREN,
  154. WAITEN, EXTMOD, ASYNCWAIT, CPSIZE and CBURSTRW bits */
  155. tmpr &= ((uint32_t)~(FSMC_BCR1_MBKEN | FSMC_BCR1_MUXEN | FSMC_BCR1_MTYP | \
  156. FSMC_BCR1_MWID | FSMC_BCR1_FACCEN | FSMC_BCR1_BURSTEN | \
  157. FSMC_BCR1_WAITPOL | FSMC_BCR1_WRAPMOD | FSMC_BCR1_WAITCFG | \
  158. FSMC_BCR1_WREN | FSMC_BCR1_WAITEN | FSMC_BCR1_EXTMOD | \
  159. FSMC_BCR1_ASYNCWAIT | FSMC_BCR1_CPSIZE | FSMC_BCR1_CBURSTRW));
  160. /* Set NORSRAM device control parameters */
  161. tmpr |= (uint32_t)(Init->DataAddressMux |\
  162. Init->MemoryType |\
  163. Init->MemoryDataWidth |\
  164. Init->BurstAccessMode |\
  165. Init->WaitSignalPolarity |\
  166. Init->WrapMode |\
  167. Init->WaitSignalActive |\
  168. Init->WriteOperation |\
  169. Init->WaitSignal |\
  170. Init->ExtendedMode |\
  171. Init->AsynchronousWait |\
  172. Init->PageSize |\
  173. Init->WriteBurst
  174. );
  175. if(Init->MemoryType == FSMC_MEMORY_TYPE_NOR)
  176. {
  177. tmpr |= (uint32_t)FSMC_NORSRAM_FLASH_ACCESS_ENABLE;
  178. }
  179. Device->BTCR[Init->NSBank] = tmpr;
  180. return HAL_OK;
  181. }
  182. /**
  183. * @brief DeInitialize the FSMC_NORSRAM peripheral
  184. * @param Device: Pointer to NORSRAM device instance
  185. * @param ExDevice: Pointer to NORSRAM extended mode device instance
  186. * @param Bank: NORSRAM bank number
  187. * @retval HAL status
  188. */
  189. HAL_StatusTypeDef FSMC_NORSRAM_DeInit(FSMC_NORSRAM_TypeDef *Device, FSMC_NORSRAM_EXTENDED_TypeDef *ExDevice, uint32_t Bank)
  190. {
  191. /* Check the parameters */
  192. assert_param(IS_FSMC_NORSRAM_DEVICE(Device));
  193. assert_param(IS_FSMC_NORSRAM_EXTENDED_DEVICE(ExDevice));
  194. assert_param(IS_FSMC_NORSRAM_BANK(Bank));
  195. /* Disable the FSMC_NORSRAM device */
  196. __FSMC_NORSRAM_DISABLE(Device, Bank);
  197. /* De-initialize the FSMC_NORSRAM device */
  198. /* FSMC_NORSRAM_BANK1 */
  199. if(Bank == FSMC_NORSRAM_BANK1)
  200. {
  201. Device->BTCR[Bank] = 0x000030DB;
  202. }
  203. /* FSMC_NORSRAM_BANK2, FSMC_NORSRAM_BANK3 or FSMC_NORSRAM_BANK4 */
  204. else
  205. {
  206. Device->BTCR[Bank] = 0x000030D2;
  207. }
  208. Device->BTCR[Bank + 1] = 0x0FFFFFFF;
  209. ExDevice->BWTR[Bank] = 0x0FFFFFFF;
  210. return HAL_OK;
  211. }
  212. /**
  213. * @brief Initialize the FSMC_NORSRAM Timing according to the specified
  214. * parameters in the FSMC_NORSRAM_TimingTypeDef
  215. * @param Device: Pointer to NORSRAM device instance
  216. * @param Timing: Pointer to NORSRAM Timing structure
  217. * @param Bank: NORSRAM bank number
  218. * @retval HAL status
  219. */
  220. HAL_StatusTypeDef FSMC_NORSRAM_Timing_Init(FSMC_NORSRAM_TypeDef *Device, FSMC_NORSRAM_TimingTypeDef *Timing, uint32_t Bank)
  221. {
  222. uint32_t tmpr = 0;
  223. /* Check the parameters */
  224. assert_param(IS_FSMC_NORSRAM_DEVICE(Device));
  225. assert_param(IS_FSMC_ADDRESS_SETUP_TIME(Timing->AddressSetupTime));
  226. assert_param(IS_FSMC_ADDRESS_HOLD_TIME(Timing->AddressHoldTime));
  227. assert_param(IS_FSMC_DATASETUP_TIME(Timing->DataSetupTime));
  228. assert_param(IS_FSMC_TURNAROUND_TIME(Timing->BusTurnAroundDuration));
  229. assert_param(IS_FSMC_CLK_DIV(Timing->CLKDivision));
  230. assert_param(IS_FSMC_DATA_LATENCY(Timing->DataLatency));
  231. assert_param(IS_FSMC_ACCESS_MODE(Timing->AccessMode));
  232. assert_param(IS_FSMC_NORSRAM_BANK(Bank));
  233. /* Get the BTCR register value */
  234. tmpr = Device->BTCR[Bank + 1];
  235. /* Clear ADDSET, ADDHLD, DATAST, BUSTURN, CLKDIV, DATLAT and ACCMOD bits */
  236. tmpr &= ((uint32_t)~(FSMC_BTR1_ADDSET | FSMC_BTR1_ADDHLD | FSMC_BTR1_DATAST | \
  237. FSMC_BTR1_BUSTURN | FSMC_BTR1_CLKDIV | FSMC_BTR1_DATLAT | \
  238. FSMC_BTR1_ACCMOD));
  239. /* Set FSMC_NORSRAM device timing parameters */
  240. tmpr |= (uint32_t)(Timing->AddressSetupTime |\
  241. ((Timing->AddressHoldTime) << 4) |\
  242. ((Timing->DataSetupTime) << 8) |\
  243. ((Timing->BusTurnAroundDuration) << 16) |\
  244. (((Timing->CLKDivision)-1) << 20) |\
  245. (((Timing->DataLatency)-2) << 24) |\
  246. (Timing->AccessMode));
  247. Device->BTCR[Bank + 1] = tmpr;
  248. return HAL_OK;
  249. }
  250. /**
  251. * @brief Initialize the FSMC_NORSRAM Extended mode Timing according to the specified
  252. * parameters in the FSMC_NORSRAM_TimingTypeDef
  253. * @param Device: Pointer to NORSRAM device instance
  254. * @param Timing: Pointer to NORSRAM Timing structure
  255. * @param Bank: NORSRAM bank number
  256. * @retval HAL status
  257. */
  258. HAL_StatusTypeDef FSMC_NORSRAM_Extended_Timing_Init(FSMC_NORSRAM_EXTENDED_TypeDef *Device, FSMC_NORSRAM_TimingTypeDef *Timing, uint32_t Bank, uint32_t ExtendedMode)
  259. {
  260. uint32_t tmpr = 0;
  261. /* Check the parameters */
  262. assert_param(IS_FSMC_EXTENDED_MODE(ExtendedMode));
  263. /* Set NORSRAM device timing register for write configuration, if extended mode is used */
  264. if(ExtendedMode == FSMC_EXTENDED_MODE_ENABLE)
  265. {
  266. /* Check the parameters */
  267. assert_param(IS_FSMC_NORSRAM_EXTENDED_DEVICE(Device));
  268. assert_param(IS_FSMC_ADDRESS_SETUP_TIME(Timing->AddressSetupTime));
  269. assert_param(IS_FSMC_ADDRESS_HOLD_TIME(Timing->AddressHoldTime));
  270. assert_param(IS_FSMC_DATASETUP_TIME(Timing->DataSetupTime));
  271. assert_param(IS_FSMC_TURNAROUND_TIME(Timing->BusTurnAroundDuration));
  272. assert_param(IS_FSMC_ACCESS_MODE(Timing->AccessMode));
  273. assert_param(IS_FSMC_NORSRAM_BANK(Bank));
  274. /* Get the BWTR register value */
  275. tmpr = Device->BWTR[Bank];
  276. /* Clear ADDSET, ADDHLD, DATAST, BUSTURN and ACCMOD bits */
  277. tmpr &= ((uint32_t)~(FSMC_BWTR1_ADDSET | FSMC_BWTR1_ADDHLD | FSMC_BWTR1_DATAST | \
  278. FSMC_BWTR1_BUSTURN | FSMC_BWTR1_ACCMOD));
  279. tmpr |= (uint32_t)(Timing->AddressSetupTime |\
  280. ((Timing->AddressHoldTime) << 4) |\
  281. ((Timing->DataSetupTime) << 8) |\
  282. ((Timing->BusTurnAroundDuration) << 16) |\
  283. (Timing->AccessMode));
  284. Device->BWTR[Bank] = tmpr;
  285. }
  286. else
  287. {
  288. Device->BWTR[Bank] = 0x0FFFFFFF;
  289. }
  290. return HAL_OK;
  291. }
  292. /**
  293. * @}
  294. */
  295. /** @addtogroup FSMC_LL_NORSRAM_Private_Functions_Group2
  296. * @brief management functions
  297. *
  298. @verbatim
  299. ==============================================================================
  300. ##### FSMC_NORSRAM Control functions #####
  301. ==============================================================================
  302. [..]
  303. This subsection provides a set of functions allowing to control dynamically
  304. the FSMC NORSRAM interface.
  305. @endverbatim
  306. * @{
  307. */
  308. /**
  309. * @brief Enables dynamically FSMC_NORSRAM write operation.
  310. * @param Device: Pointer to NORSRAM device instance
  311. * @param Bank: NORSRAM bank number
  312. * @retval HAL status
  313. */
  314. HAL_StatusTypeDef FSMC_NORSRAM_WriteOperation_Enable(FSMC_NORSRAM_TypeDef *Device, uint32_t Bank)
  315. {
  316. /* Check the parameters */
  317. assert_param(IS_FSMC_NORSRAM_DEVICE(Device));
  318. assert_param(IS_FSMC_NORSRAM_BANK(Bank));
  319. /* Enable write operation */
  320. Device->BTCR[Bank] |= FSMC_WRITE_OPERATION_ENABLE;
  321. return HAL_OK;
  322. }
  323. /**
  324. * @brief Disables dynamically FSMC_NORSRAM write operation.
  325. * @param Device: Pointer to NORSRAM device instance
  326. * @param Bank: NORSRAM bank number
  327. * @retval HAL status
  328. */
  329. HAL_StatusTypeDef FSMC_NORSRAM_WriteOperation_Disable(FSMC_NORSRAM_TypeDef *Device, uint32_t Bank)
  330. {
  331. /* Check the parameters */
  332. assert_param(IS_FSMC_NORSRAM_DEVICE(Device));
  333. assert_param(IS_FSMC_NORSRAM_BANK(Bank));
  334. /* Disable write operation */
  335. Device->BTCR[Bank] &= ~FSMC_WRITE_OPERATION_ENABLE;
  336. return HAL_OK;
  337. }
  338. /**
  339. * @}
  340. */
  341. /**
  342. * @}
  343. */
  344. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx)
  345. /** @addtogroup FSMC_LL_NAND
  346. * @brief NAND Controller functions
  347. *
  348. @verbatim
  349. ==============================================================================
  350. ##### How to use NAND device driver #####
  351. ==============================================================================
  352. [..]
  353. This driver contains a set of APIs to interface with the FSMC NAND banks in order
  354. to run the NAND external devices.
  355. (+) FSMC NAND bank reset using the function FSMC_NAND_DeInit()
  356. (+) FSMC NAND bank control configuration using the function FSMC_NAND_Init()
  357. (+) FSMC NAND bank common space timing configuration using the function
  358. FSMC_NAND_CommonSpace_Timing_Init()
  359. (+) FSMC NAND bank attribute space timing configuration using the function
  360. FSMC_NAND_AttributeSpace_Timing_Init()
  361. (+) FSMC NAND bank enable/disable ECC correction feature using the functions
  362. FSMC_NAND_ECC_Enable()/FSMC_NAND_ECC_Disable()
  363. (+) FSMC NAND bank get ECC correction code using the function FSMC_NAND_GetECC()
  364. @endverbatim
  365. * @{
  366. */
  367. /** @addtogroup FSMC_LL_NAND_Private_Functions_Group1
  368. * @brief Initialization and Configuration functions
  369. *
  370. @verbatim
  371. ==============================================================================
  372. ##### Initialization and de_initialization functions #####
  373. ==============================================================================
  374. [..]
  375. This section provides functions allowing to:
  376. (+) Initialize and configure the FSMC NAND interface
  377. (+) De-initialize the FSMC NAND interface
  378. (+) Configure the FSMC clock and associated GPIOs
  379. @endverbatim
  380. * @{
  381. */
  382. /**
  383. * @brief Initializes the FSMC_NAND device according to the specified
  384. * control parameters in the FSMC_NAND_HandleTypeDef
  385. * @param Device: Pointer to NAND device instance
  386. * @param Init: Pointer to NAND Initialization structure
  387. * @retval HAL status
  388. */
  389. HAL_StatusTypeDef FSMC_NAND_Init(FSMC_NAND_TypeDef *Device, FSMC_NAND_InitTypeDef *Init)
  390. {
  391. uint32_t tmpr = 0;
  392. /* Check the parameters */
  393. assert_param(IS_FSMC_NAND_BANK(Init->NandBank));
  394. assert_param(IS_FSMC_WAIT_FEATURE(Init->Waitfeature));
  395. assert_param(IS_FSMC_NAND_MEMORY_WIDTH(Init->MemoryDataWidth));
  396. assert_param(IS_FSMC_ECC_STATE(Init->EccComputation));
  397. assert_param(IS_FSMC_ECCPAGE_SIZE(Init->ECCPageSize));
  398. assert_param(IS_FSMC_TCLR_TIME(Init->TCLRSetupTime));
  399. assert_param(IS_FSMC_TAR_TIME(Init->TARSetupTime));
  400. if(Init->NandBank == FSMC_NAND_BANK2)
  401. {
  402. /* Get the NAND bank 2 register value */
  403. tmpr = Device->PCR2;
  404. }
  405. else
  406. {
  407. /* Get the NAND bank 3 register value */
  408. tmpr = Device->PCR3;
  409. }
  410. /* Clear PWAITEN, PBKEN, PTYP, PWID, ECCEN, TCLR, TAR and ECCPS bits */
  411. tmpr &= ((uint32_t)~(FSMC_PCR2_PWAITEN | FSMC_PCR2_PBKEN | FSMC_PCR2_PTYP | \
  412. FSMC_PCR2_PWID | FSMC_PCR2_ECCEN | FSMC_PCR2_TCLR | \
  413. FSMC_PCR2_TAR | FSMC_PCR2_ECCPS));
  414. /* Set NAND device control parameters */
  415. tmpr |= (uint32_t)(Init->Waitfeature |\
  416. FSMC_PCR_MEMORY_TYPE_NAND |\
  417. Init->MemoryDataWidth |\
  418. Init->EccComputation |\
  419. Init->ECCPageSize |\
  420. ((Init->TCLRSetupTime) << 9) |\
  421. ((Init->TARSetupTime) << 13));
  422. if(Init->NandBank == FSMC_NAND_BANK2)
  423. {
  424. /* NAND bank 2 registers configuration */
  425. Device->PCR2 = tmpr;
  426. }
  427. else
  428. {
  429. /* NAND bank 3 registers configuration */
  430. Device->PCR3 = tmpr;
  431. }
  432. return HAL_OK;
  433. }
  434. /**
  435. * @brief Initializes the FSMC_NAND Common space Timing according to the specified
  436. * parameters in the FSMC_NAND_PCC_TimingTypeDef
  437. * @param Device: Pointer to NAND device instance
  438. * @param Timing: Pointer to NAND timing structure
  439. * @param Bank: NAND bank number
  440. * @retval HAL status
  441. */
  442. HAL_StatusTypeDef FSMC_NAND_CommonSpace_Timing_Init(FSMC_NAND_TypeDef *Device, FSMC_NAND_PCC_TimingTypeDef *Timing, uint32_t Bank)
  443. {
  444. uint32_t tmpr = 0;
  445. /* Check the parameters */
  446. assert_param(IS_FSMC_SETUP_TIME(Timing->SetupTime));
  447. assert_param(IS_FSMC_WAIT_TIME(Timing->WaitSetupTime));
  448. assert_param(IS_FSMC_HOLD_TIME(Timing->HoldSetupTime));
  449. assert_param(IS_FSMC_HIZ_TIME(Timing->HiZSetupTime));
  450. if(Bank == FSMC_NAND_BANK2)
  451. {
  452. /* Get the NAND bank 2 register value */
  453. tmpr = Device->PMEM2;
  454. }
  455. else
  456. {
  457. /* Get the NAND bank 3 register value */
  458. tmpr = Device->PMEM3;
  459. }
  460. /* Clear MEMSETx, MEMWAITx, MEMHOLDx and MEMHIZx bits */
  461. tmpr &= ((uint32_t)~(FSMC_PMEM2_MEMSET2 | FSMC_PMEM2_MEMWAIT2 | FSMC_PMEM2_MEMHOLD2 | \
  462. FSMC_PMEM2_MEMHIZ2));
  463. /* Set FSMC_NAND device timing parameters */
  464. tmpr |= (uint32_t)(Timing->SetupTime |\
  465. ((Timing->WaitSetupTime) << 8) |\
  466. ((Timing->HoldSetupTime) << 16) |\
  467. ((Timing->HiZSetupTime) << 24)
  468. );
  469. if(Bank == FSMC_NAND_BANK2)
  470. {
  471. /* NAND bank 2 registers configuration */
  472. Device->PMEM2 = tmpr;
  473. }
  474. else
  475. {
  476. /* NAND bank 3 registers configuration */
  477. Device->PMEM3 = tmpr;
  478. }
  479. return HAL_OK;
  480. }
  481. /**
  482. * @brief Initializes the FSMC_NAND Attribute space Timing according to the specified
  483. * parameters in the FSMC_NAND_PCC_TimingTypeDef
  484. * @param Device: Pointer to NAND device instance
  485. * @param Timing: Pointer to NAND timing structure
  486. * @param Bank: NAND bank number
  487. * @retval HAL status
  488. */
  489. HAL_StatusTypeDef FSMC_NAND_AttributeSpace_Timing_Init(FSMC_NAND_TypeDef *Device, FSMC_NAND_PCC_TimingTypeDef *Timing, uint32_t Bank)
  490. {
  491. uint32_t tmpr = 0;
  492. /* Check the parameters */
  493. assert_param(IS_FSMC_SETUP_TIME(Timing->SetupTime));
  494. assert_param(IS_FSMC_WAIT_TIME(Timing->WaitSetupTime));
  495. assert_param(IS_FSMC_HOLD_TIME(Timing->HoldSetupTime));
  496. assert_param(IS_FSMC_HIZ_TIME(Timing->HiZSetupTime));
  497. if(Bank == FSMC_NAND_BANK2)
  498. {
  499. /* Get the NAND bank 2 register value */
  500. tmpr = Device->PATT2;
  501. }
  502. else
  503. {
  504. /* Get the NAND bank 3 register value */
  505. tmpr = Device->PATT3;
  506. }
  507. /* Clear ATTSETx, ATTWAITx, ATTHOLDx and ATTHIZx bits */
  508. tmpr &= ((uint32_t)~(FSMC_PATT2_ATTSET2 | FSMC_PATT2_ATTWAIT2 | FSMC_PATT2_ATTHOLD2 | \
  509. FSMC_PATT2_ATTHIZ2));
  510. /* Set FSMC_NAND device timing parameters */
  511. tmpr |= (uint32_t)(Timing->SetupTime |\
  512. ((Timing->WaitSetupTime) << 8) |\
  513. ((Timing->HoldSetupTime) << 16) |\
  514. ((Timing->HiZSetupTime) << 24)
  515. );
  516. if(Bank == FSMC_NAND_BANK2)
  517. {
  518. /* NAND bank 2 registers configuration */
  519. Device->PATT2 = tmpr;
  520. }
  521. else
  522. {
  523. /* NAND bank 3 registers configuration */
  524. Device->PATT3 = tmpr;
  525. }
  526. return HAL_OK;
  527. }
  528. /**
  529. * @brief DeInitializes the FSMC_NAND device
  530. * @param Device: Pointer to NAND device instance
  531. * @param Bank: NAND bank number
  532. * @retval HAL status
  533. */
  534. HAL_StatusTypeDef FSMC_NAND_DeInit(FSMC_NAND_TypeDef *Device, uint32_t Bank)
  535. {
  536. /* Disable the NAND Bank */
  537. __FSMC_NAND_DISABLE(Device, Bank);
  538. /* De-initialize the NAND Bank */
  539. if(Bank == FSMC_NAND_BANK2)
  540. {
  541. /* Set the FSMC_NAND_BANK2 registers to their reset values */
  542. Device->PCR2 = 0x00000018;
  543. Device->SR2 = 0x00000040;
  544. Device->PMEM2 = 0xFCFCFCFC;
  545. Device->PATT2 = 0xFCFCFCFC;
  546. }
  547. /* FSMC_Bank3_NAND */
  548. else
  549. {
  550. /* Set the FSMC_NAND_BANK3 registers to their reset values */
  551. Device->PCR3 = 0x00000018;
  552. Device->SR3 = 0x00000040;
  553. Device->PMEM3 = 0xFCFCFCFC;
  554. Device->PATT3 = 0xFCFCFCFC;
  555. }
  556. return HAL_OK;
  557. }
  558. /**
  559. * @}
  560. */
  561. /** @addtogroup FSMC_LL_NAND_Private_Functions_Group2
  562. * @brief management functions
  563. *
  564. @verbatim
  565. ==============================================================================
  566. ##### FSMC_NAND Control functions #####
  567. ==============================================================================
  568. [..]
  569. This subsection provides a set of functions allowing to control dynamically
  570. the FSMC NAND interface.
  571. @endverbatim
  572. * @{
  573. */
  574. /**
  575. * @brief Enables dynamically FSMC_NAND ECC feature.
  576. * @param Device: Pointer to NAND device instance
  577. * @param Bank: NAND bank number
  578. * @retval HAL status
  579. */
  580. HAL_StatusTypeDef FSMC_NAND_ECC_Enable(FSMC_NAND_TypeDef *Device, uint32_t Bank)
  581. {
  582. /* Enable ECC feature */
  583. if(Bank == FSMC_NAND_BANK2)
  584. {
  585. Device->PCR2 |= FSMC_PCR2_ECCEN;
  586. }
  587. else
  588. {
  589. Device->PCR3 |= FSMC_PCR3_ECCEN;
  590. }
  591. return HAL_OK;
  592. }
  593. /**
  594. * @brief Disables dynamically FSMC_NAND ECC feature.
  595. * @param Device: Pointer to NAND device instance
  596. * @param Bank: NAND bank number
  597. * @retval HAL status
  598. */
  599. HAL_StatusTypeDef FSMC_NAND_ECC_Disable(FSMC_NAND_TypeDef *Device, uint32_t Bank)
  600. {
  601. /* Disable ECC feature */
  602. if(Bank == FSMC_NAND_BANK2)
  603. {
  604. Device->PCR2 &= ~FSMC_PCR2_ECCEN;
  605. }
  606. else
  607. {
  608. Device->PCR3 &= ~FSMC_PCR3_ECCEN;
  609. }
  610. return HAL_OK;
  611. }
  612. /**
  613. * @brief Disables dynamically FSMC_NAND ECC feature.
  614. * @param Device: Pointer to NAND device instance
  615. * @param ECCval: Pointer to ECC value
  616. * @param Bank: NAND bank number
  617. * @param Timeout: Timeout wait value
  618. * @retval HAL status
  619. */
  620. HAL_StatusTypeDef FSMC_NAND_GetECC(FSMC_NAND_TypeDef *Device, uint32_t *ECCval, uint32_t Bank, uint32_t Timeout)
  621. {
  622. uint32_t tickstart = 0;
  623. /* Check the parameters */
  624. assert_param(IS_FSMC_NAND_DEVICE(Device));
  625. assert_param(IS_FSMC_NAND_BANK(Bank));
  626. /* Get tick */
  627. tickstart = HAL_GetTick();
  628. /* Wait until FIFO is empty */
  629. while(__FSMC_NAND_GET_FLAG(Device, Bank, FSMC_FLAG_FEMPT) == RESET)
  630. {
  631. /* Check for the Timeout */
  632. if(Timeout != HAL_MAX_DELAY)
  633. {
  634. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  635. {
  636. return HAL_TIMEOUT;
  637. }
  638. }
  639. }
  640. if(Bank == FSMC_NAND_BANK2)
  641. {
  642. /* Get the ECCR2 register value */
  643. *ECCval = (uint32_t)Device->ECCR2;
  644. }
  645. else
  646. {
  647. /* Get the ECCR3 register value */
  648. *ECCval = (uint32_t)Device->ECCR3;
  649. }
  650. return HAL_OK;
  651. }
  652. /**
  653. * @}
  654. */
  655. /**
  656. * @}
  657. */
  658. /** @addtogroup FSMC_LL_PCCARD
  659. * @brief PCCARD Controller functions
  660. *
  661. @verbatim
  662. ==============================================================================
  663. ##### How to use PCCARD device driver #####
  664. ==============================================================================
  665. [..]
  666. This driver contains a set of APIs to interface with the FSMC PCCARD bank in order
  667. to run the PCCARD/compact flash external devices.
  668. (+) FSMC PCCARD bank reset using the function FSMC_PCCARD_DeInit()
  669. (+) FSMC PCCARD bank control configuration using the function FSMC_PCCARD_Init()
  670. (+) FSMC PCCARD bank common space timing configuration using the function
  671. FSMC_PCCARD_CommonSpace_Timing_Init()
  672. (+) FSMC PCCARD bank attribute space timing configuration using the function
  673. FSMC_PCCARD_AttributeSpace_Timing_Init()
  674. (+) FSMC PCCARD bank IO space timing configuration using the function
  675. FSMC_PCCARD_IOSpace_Timing_Init()
  676. @endverbatim
  677. * @{
  678. */
  679. /** @addtogroup FSMC_LL_PCCARD_Private_Functions_Group1
  680. * @brief Initialization and Configuration functions
  681. *
  682. @verbatim
  683. ==============================================================================
  684. ##### Initialization and de_initialization functions #####
  685. ==============================================================================
  686. [..]
  687. This section provides functions allowing to:
  688. (+) Initialize and configure the FSMC PCCARD interface
  689. (+) De-initialize the FSMC PCCARD interface
  690. (+) Configure the FSMC clock and associated GPIOs
  691. @endverbatim
  692. * @{
  693. */
  694. /**
  695. * @brief Initializes the FSMC_PCCARD device according to the specified
  696. * control parameters in the FSMC_PCCARD_HandleTypeDef
  697. * @param Device: Pointer to PCCARD device instance
  698. * @param Init: Pointer to PCCARD Initialization structure
  699. * @retval HAL status
  700. */
  701. HAL_StatusTypeDef FSMC_PCCARD_Init(FSMC_PCCARD_TypeDef *Device, FSMC_PCCARD_InitTypeDef *Init)
  702. {
  703. uint32_t tmpr = 0;
  704. /* Check the parameters */
  705. assert_param(IS_FSMC_WAIT_FEATURE(Init->Waitfeature));
  706. assert_param(IS_FSMC_TCLR_TIME(Init->TCLRSetupTime));
  707. assert_param(IS_FSMC_TAR_TIME(Init->TARSetupTime));
  708. /* Get PCCARD control register value */
  709. tmpr = Device->PCR4;
  710. /* Clear TAR, TCLR, PWAITEN and PWID bits */
  711. tmpr &= ((uint32_t)~(FSMC_PCR4_TAR | FSMC_PCR4_TCLR | FSMC_PCR4_PWAITEN | \
  712. FSMC_PCR4_PWID));
  713. /* Set FSMC_PCCARD device control parameters */
  714. tmpr |= (uint32_t)(Init->Waitfeature |\
  715. FSMC_NAND_PCC_MEM_BUS_WIDTH_16 |\
  716. (Init->TCLRSetupTime << 9) |\
  717. (Init->TARSetupTime << 13));
  718. Device->PCR4 = tmpr;
  719. return HAL_OK;
  720. }
  721. /**
  722. * @brief Initializes the FSMC_PCCARD Common space Timing according to the specified
  723. * parameters in the FSMC_NAND_PCC_TimingTypeDef
  724. * @param Device: Pointer to PCCARD device instance
  725. * @param Timing: Pointer to PCCARD timing structure
  726. * @retval HAL status
  727. */
  728. HAL_StatusTypeDef FSMC_PCCARD_CommonSpace_Timing_Init(FSMC_PCCARD_TypeDef *Device, FSMC_NAND_PCC_TimingTypeDef *Timing)
  729. {
  730. uint32_t tmpr = 0;
  731. /* Check the parameters */
  732. assert_param(IS_FSMC_SETUP_TIME(Timing->SetupTime));
  733. assert_param(IS_FSMC_WAIT_TIME(Timing->WaitSetupTime));
  734. assert_param(IS_FSMC_HOLD_TIME(Timing->HoldSetupTime));
  735. assert_param(IS_FSMC_HIZ_TIME(Timing->HiZSetupTime));
  736. /* Get PCCARD common space timing register value */
  737. tmpr = Device->PMEM4;
  738. /* Clear MEMSETx, MEMWAITx, MEMHOLDx and MEMHIZx bits */
  739. tmpr &= ((uint32_t)~(FSMC_PMEM4_MEMSET4 | FSMC_PMEM4_MEMWAIT4 | FSMC_PMEM4_MEMHOLD4 | \
  740. FSMC_PMEM4_MEMHIZ4));
  741. /* Set PCCARD timing parameters */
  742. tmpr |= (uint32_t)((Timing->SetupTime |\
  743. ((Timing->WaitSetupTime) << 8) |\
  744. (Timing->HoldSetupTime) << 16) |\
  745. ((Timing->HiZSetupTime) << 24));
  746. Device->PMEM4 = tmpr;
  747. return HAL_OK;
  748. }
  749. /**
  750. * @brief Initializes the FSMC_PCCARD Attribute space Timing according to the specified
  751. * parameters in the FSMC_NAND_PCC_TimingTypeDef
  752. * @param Device: Pointer to PCCARD device instance
  753. * @param Timing: Pointer to PCCARD timing structure
  754. * @retval HAL status
  755. */
  756. HAL_StatusTypeDef FSMC_PCCARD_AttributeSpace_Timing_Init(FSMC_PCCARD_TypeDef *Device, FSMC_NAND_PCC_TimingTypeDef *Timing)
  757. {
  758. uint32_t tmpr = 0;
  759. /* Check the parameters */
  760. assert_param(IS_FSMC_SETUP_TIME(Timing->SetupTime));
  761. assert_param(IS_FSMC_WAIT_TIME(Timing->WaitSetupTime));
  762. assert_param(IS_FSMC_HOLD_TIME(Timing->HoldSetupTime));
  763. assert_param(IS_FSMC_HIZ_TIME(Timing->HiZSetupTime));
  764. /* Get PCCARD timing parameters */
  765. tmpr = Device->PATT4;
  766. /* Clear ATTSETx, ATTWAITx, ATTHOLDx and ATTHIZx bits */
  767. tmpr &= ((uint32_t)~(FSMC_PATT4_ATTSET4 | FSMC_PATT4_ATTWAIT4 | FSMC_PATT4_ATTHOLD4 | \
  768. FSMC_PATT4_ATTHIZ4));
  769. /* Set PCCARD timing parameters */
  770. tmpr |= (uint32_t)(Timing->SetupTime |\
  771. ((Timing->WaitSetupTime) << 8) |\
  772. ((Timing->HoldSetupTime) << 16) |\
  773. ((Timing->HiZSetupTime) << 24));
  774. Device->PATT4 = tmpr;
  775. return HAL_OK;
  776. }
  777. /**
  778. * @brief Initializes the FSMC_PCCARD IO space Timing according to the specified
  779. * parameters in the FSMC_NAND_PCC_TimingTypeDef
  780. * @param Device: Pointer to PCCARD device instance
  781. * @param Timing: Pointer to PCCARD timing structure
  782. * @retval HAL status
  783. */
  784. HAL_StatusTypeDef FSMC_PCCARD_IOSpace_Timing_Init(FSMC_PCCARD_TypeDef *Device, FSMC_NAND_PCC_TimingTypeDef *Timing)
  785. {
  786. uint32_t tmpr = 0;
  787. /* Check the parameters */
  788. assert_param(IS_FSMC_SETUP_TIME(Timing->SetupTime));
  789. assert_param(IS_FSMC_WAIT_TIME(Timing->WaitSetupTime));
  790. assert_param(IS_FSMC_HOLD_TIME(Timing->HoldSetupTime));
  791. assert_param(IS_FSMC_HIZ_TIME(Timing->HiZSetupTime));
  792. /* Get FSMC_PCCARD device timing parameters */
  793. tmpr = Device->PIO4;
  794. /* Clear IOSET4, IOWAIT4, IOHOLD4 and IOHIZ4 bits */
  795. tmpr &= ((uint32_t)~(FSMC_PIO4_IOSET4 | FSMC_PIO4_IOWAIT4 | FSMC_PIO4_IOHOLD4 | \
  796. FSMC_PIO4_IOHIZ4));
  797. /* Set FSMC_PCCARD device timing parameters */
  798. tmpr |= (uint32_t)(Timing->SetupTime |\
  799. ((Timing->WaitSetupTime) << 8) |\
  800. ((Timing->HoldSetupTime) << 16) |\
  801. ((Timing->HiZSetupTime) << 24));
  802. Device->PIO4 = tmpr;
  803. return HAL_OK;
  804. }
  805. /**
  806. * @brief DeInitializes the FSMC_PCCARD device
  807. * @param Device: Pointer to PCCARD device instance
  808. * @retval HAL status
  809. */
  810. HAL_StatusTypeDef FSMC_PCCARD_DeInit(FSMC_PCCARD_TypeDef *Device)
  811. {
  812. /* Disable the FSMC_PCCARD device */
  813. __FSMC_PCCARD_DISABLE(Device);
  814. /* De-initialize the FSMC_PCCARD device */
  815. Device->PCR4 = 0x00000018;
  816. Device->SR4 = 0x00000000;
  817. Device->PMEM4 = 0xFCFCFCFC;
  818. Device->PATT4 = 0xFCFCFCFC;
  819. Device->PIO4 = 0xFCFCFCFC;
  820. return HAL_OK;
  821. }
  822. /**
  823. * @}
  824. */
  825. /**
  826. * @}
  827. */
  828. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx */
  829. /**
  830. * @}
  831. */
  832. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx */
  833. #endif /* HAL_SRAM_MODULE_ENABLED || HAL_NOR_MODULE_ENABLED || HAL_NAND_MODULE_ENABLED || HAL_PCCARD_MODULE_ENABLED */
  834. /**
  835. * @}
  836. */
  837. /**
  838. * @}
  839. */
  840. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/