stm32f4xx_hal_nand.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_nand.c
  4. * @author MCD Application Team
  5. * @brief NAND HAL module driver.
  6. * This file provides a generic firmware to drive NAND memories mounted
  7. * as external device.
  8. *
  9. @verbatim
  10. ==============================================================================
  11. ##### How to use this driver #####
  12. ==============================================================================
  13. [..]
  14. This driver is a generic layered driver which contains a set of APIs used to
  15. control NAND flash memories. It uses the FMC/FSMC layer functions to interface
  16. with NAND devices. This driver is used as follows:
  17. (+) NAND flash memory configuration sequence using the function HAL_NAND_Init()
  18. with control and timing parameters for both common and attribute spaces.
  19. (+) Read NAND flash memory maker and device IDs using the function
  20. HAL_NAND_Read_ID(). The read information is stored in the NAND_ID_TypeDef
  21. structure declared by the function caller.
  22. (+) Access NAND flash memory by read/write operations using the functions
  23. HAL_NAND_Read_Page_8b()/HAL_NAND_Read_SpareArea_8b(),
  24. HAL_NAND_Write_Page_8b()/HAL_NAND_Write_SpareArea_8b(),
  25. HAL_NAND_Read_Page_16b()/HAL_NAND_Read_SpareArea_16b(),
  26. HAL_NAND_Write_Page_16b()/HAL_NAND_Write_SpareArea_16b()
  27. to read/write page(s)/spare area(s). These functions use specific device
  28. information (Block, page size..) predefined by the user in the HAL_NAND_Info_TypeDef
  29. structure. The read/write address information is contained by the Nand_Address_Typedef
  30. structure passed as parameter.
  31. (+) Perform NAND flash Reset chip operation using the function HAL_NAND_Reset().
  32. (+) Perform NAND flash erase block operation using the function HAL_NAND_Erase_Block().
  33. The erase block address information is contained in the Nand_Address_Typedef
  34. structure passed as parameter.
  35. (+) Read the NAND flash status operation using the function HAL_NAND_Read_Status().
  36. (+) You can also control the NAND device by calling the control APIs HAL_NAND_ECC_Enable()/
  37. HAL_NAND_ECC_Disable() to respectively enable/disable the ECC code correction
  38. feature or the function HAL_NAND_GetECC() to get the ECC correction code.
  39. (+) You can monitor the NAND device HAL state by calling the function
  40. HAL_NAND_GetState()
  41. [..]
  42. (@) This driver is a set of generic APIs which handle standard NAND flash operations.
  43. If a NAND flash device contains different operations and/or implementations,
  44. it should be implemented separately.
  45. @endverbatim
  46. ******************************************************************************
  47. * @attention
  48. *
  49. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  50. *
  51. * Redistribution and use in source and binary forms, with or without modification,
  52. * are permitted provided that the following conditions are met:
  53. * 1. Redistributions of source code must retain the above copyright notice,
  54. * this list of conditions and the following disclaimer.
  55. * 2. Redistributions in binary form must reproduce the above copyright notice,
  56. * this list of conditions and the following disclaimer in the documentation
  57. * and/or other materials provided with the distribution.
  58. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  59. * may be used to endorse or promote products derived from this software
  60. * without specific prior written permission.
  61. *
  62. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  63. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  64. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  65. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  66. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  67. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  68. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  69. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  70. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  71. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  72. *
  73. ******************************************************************************
  74. */
  75. /* Includes ------------------------------------------------------------------*/
  76. #include "stm32f4xx_hal.h"
  77. /** @addtogroup STM32F4xx_HAL_Driver
  78. * @{
  79. */
  80. #ifdef HAL_NAND_MODULE_ENABLED
  81. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
  82. defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
  83. defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
  84. /** @defgroup NAND NAND
  85. * @brief NAND HAL module driver
  86. * @{
  87. */
  88. /* Private typedef -----------------------------------------------------------*/
  89. /* Private define ------------------------------------------------------------*/
  90. /** @defgroup NAND_Private_Constants NAND Private Constants
  91. * @{
  92. */
  93. /**
  94. * @}
  95. */
  96. /* Private macro -------------------------------------------------------------*/
  97. /** @defgroup NAND_Private_Macros NAND Private Macros
  98. * @{
  99. */
  100. /**
  101. * @}
  102. */
  103. /* Private variables ---------------------------------------------------------*/
  104. /* Private function prototypes -----------------------------------------------*/
  105. /* Exported functions --------------------------------------------------------*/
  106. /** @defgroup NAND_Exported_Functions NAND Exported Functions
  107. * @{
  108. */
  109. /** @defgroup NAND_Exported_Functions_Group1 Initialization and de-initialization functions
  110. * @brief Initialization and Configuration functions
  111. *
  112. @verbatim
  113. ==============================================================================
  114. ##### NAND Initialization and de-initialization functions #####
  115. ==============================================================================
  116. [..]
  117. This section provides functions allowing to initialize/de-initialize
  118. the NAND memory
  119. @endverbatim
  120. * @{
  121. */
  122. /**
  123. * @brief Perform NAND memory Initialization sequence
  124. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  125. * the configuration information for NAND module.
  126. * @param ComSpace_Timing pointer to Common space timing structure
  127. * @param AttSpace_Timing pointer to Attribute space timing structure
  128. * @retval HAL status
  129. */
  130. HAL_StatusTypeDef HAL_NAND_Init(NAND_HandleTypeDef *hnand, FMC_NAND_PCC_TimingTypeDef *ComSpace_Timing, FMC_NAND_PCC_TimingTypeDef *AttSpace_Timing)
  131. {
  132. /* Check the NAND handle state */
  133. if(hnand == NULL)
  134. {
  135. return HAL_ERROR;
  136. }
  137. if(hnand->State == HAL_NAND_STATE_RESET)
  138. {
  139. /* Allocate lock resource and initialize it */
  140. hnand->Lock = HAL_UNLOCKED;
  141. /* Initialize the low level hardware (MSP) */
  142. HAL_NAND_MspInit(hnand);
  143. }
  144. /* Initialize NAND control Interface */
  145. FMC_NAND_Init(hnand->Instance, &(hnand->Init));
  146. /* Initialize NAND common space timing Interface */
  147. FMC_NAND_CommonSpace_Timing_Init(hnand->Instance, ComSpace_Timing, hnand->Init.NandBank);
  148. /* Initialize NAND attribute space timing Interface */
  149. FMC_NAND_AttributeSpace_Timing_Init(hnand->Instance, AttSpace_Timing, hnand->Init.NandBank);
  150. /* Enable the NAND device */
  151. __FMC_NAND_ENABLE(hnand->Instance, hnand->Init.NandBank);
  152. /* Update the NAND controller state */
  153. hnand->State = HAL_NAND_STATE_READY;
  154. return HAL_OK;
  155. }
  156. /**
  157. * @brief Perform NAND memory De-Initialization sequence
  158. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  159. * the configuration information for NAND module.
  160. * @retval HAL status
  161. */
  162. HAL_StatusTypeDef HAL_NAND_DeInit(NAND_HandleTypeDef *hnand)
  163. {
  164. /* Initialize the low level hardware (MSP) */
  165. HAL_NAND_MspDeInit(hnand);
  166. /* Configure the NAND registers with their reset values */
  167. FMC_NAND_DeInit(hnand->Instance, hnand->Init.NandBank);
  168. /* Reset the NAND controller state */
  169. hnand->State = HAL_NAND_STATE_RESET;
  170. /* Release Lock */
  171. __HAL_UNLOCK(hnand);
  172. return HAL_OK;
  173. }
  174. /**
  175. * @brief NAND MSP Init
  176. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  177. * the configuration information for NAND module.
  178. * @retval None
  179. */
  180. __weak void HAL_NAND_MspInit(NAND_HandleTypeDef *hnand)
  181. {
  182. /* Prevent unused argument(s) compilation warning */
  183. UNUSED(hnand);
  184. /* NOTE : This function Should not be modified, when the callback is needed,
  185. the HAL_NAND_MspInit could be implemented in the user file
  186. */
  187. }
  188. /**
  189. * @brief NAND MSP DeInit
  190. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  191. * the configuration information for NAND module.
  192. * @retval None
  193. */
  194. __weak void HAL_NAND_MspDeInit(NAND_HandleTypeDef *hnand)
  195. {
  196. /* Prevent unused argument(s) compilation warning */
  197. UNUSED(hnand);
  198. /* NOTE : This function Should not be modified, when the callback is needed,
  199. the HAL_NAND_MspDeInit could be implemented in the user file
  200. */
  201. }
  202. /**
  203. * @brief This function handles NAND device interrupt request.
  204. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  205. * the configuration information for NAND module.
  206. * @retval HAL status
  207. */
  208. void HAL_NAND_IRQHandler(NAND_HandleTypeDef *hnand)
  209. {
  210. /* Check NAND interrupt Rising edge flag */
  211. if(__FMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_RISING_EDGE))
  212. {
  213. /* NAND interrupt callback*/
  214. HAL_NAND_ITCallback(hnand);
  215. /* Clear NAND interrupt Rising edge pending bit */
  216. __FMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_RISING_EDGE);
  217. }
  218. /* Check NAND interrupt Level flag */
  219. if(__FMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_LEVEL))
  220. {
  221. /* NAND interrupt callback*/
  222. HAL_NAND_ITCallback(hnand);
  223. /* Clear NAND interrupt Level pending bit */
  224. __FMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_LEVEL);
  225. }
  226. /* Check NAND interrupt Falling edge flag */
  227. if(__FMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_FALLING_EDGE))
  228. {
  229. /* NAND interrupt callback*/
  230. HAL_NAND_ITCallback(hnand);
  231. /* Clear NAND interrupt Falling edge pending bit */
  232. __FMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_FALLING_EDGE);
  233. }
  234. /* Check NAND interrupt FIFO empty flag */
  235. if(__FMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_FEMPT))
  236. {
  237. /* NAND interrupt callback*/
  238. HAL_NAND_ITCallback(hnand);
  239. /* Clear NAND interrupt FIFO empty pending bit */
  240. __FMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_FEMPT);
  241. }
  242. }
  243. /**
  244. * @brief NAND interrupt feature callback
  245. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  246. * the configuration information for NAND module.
  247. * @retval None
  248. */
  249. __weak void HAL_NAND_ITCallback(NAND_HandleTypeDef *hnand)
  250. {
  251. /* Prevent unused argument(s) compilation warning */
  252. UNUSED(hnand);
  253. /* NOTE : This function Should not be modified, when the callback is needed,
  254. the HAL_NAND_ITCallback could be implemented in the user file
  255. */
  256. }
  257. /**
  258. * @}
  259. */
  260. /** @defgroup NAND_Exported_Functions_Group2 Input and Output functions
  261. * @brief Input Output and memory control functions
  262. *
  263. @verbatim
  264. ==============================================================================
  265. ##### NAND Input and Output functions #####
  266. ==============================================================================
  267. [..]
  268. This section provides functions allowing to use and control the NAND
  269. memory
  270. @endverbatim
  271. * @{
  272. */
  273. /**
  274. * @brief Read the NAND memory electronic signature
  275. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  276. * the configuration information for NAND module.
  277. * @param pNAND_ID NAND ID structure
  278. * @retval HAL status
  279. */
  280. HAL_StatusTypeDef HAL_NAND_Read_ID(NAND_HandleTypeDef *hnand, NAND_IDTypeDef *pNAND_ID)
  281. {
  282. __IO uint32_t data = 0U;
  283. __IO uint32_t data1 = 0U;
  284. uint32_t deviceaddress = 0U;
  285. /* Process Locked */
  286. __HAL_LOCK(hnand);
  287. /* Check the NAND controller state */
  288. if(hnand->State == HAL_NAND_STATE_BUSY)
  289. {
  290. return HAL_BUSY;
  291. }
  292. /* Identify the device address */
  293. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  294. {
  295. deviceaddress = NAND_DEVICE1;
  296. }
  297. else
  298. {
  299. deviceaddress = NAND_DEVICE2;
  300. }
  301. /* Update the NAND controller state */
  302. hnand->State = HAL_NAND_STATE_BUSY;
  303. /* Send Read ID command sequence */
  304. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_READID;
  305. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  306. /* Read the electronic signature from NAND flash */
  307. #ifdef FSMC_PCR2_PWID
  308. if (hnand->Init.MemoryDataWidth == FSMC_NAND_PCC_MEM_BUS_WIDTH_8)
  309. #else /* FMC_PCR2_PWID is defined */
  310. if (hnand->Init.MemoryDataWidth == FMC_NAND_PCC_MEM_BUS_WIDTH_8)
  311. #endif
  312. {
  313. data = *(__IO uint32_t *)deviceaddress;
  314. /* Return the data read */
  315. pNAND_ID->Maker_Id = ADDR_1ST_CYCLE(data);
  316. pNAND_ID->Device_Id = ADDR_2ND_CYCLE(data);
  317. pNAND_ID->Third_Id = ADDR_3RD_CYCLE(data);
  318. pNAND_ID->Fourth_Id = ADDR_4TH_CYCLE(data);
  319. }
  320. else
  321. {
  322. data = *(__IO uint32_t *)deviceaddress;
  323. data1 = *((__IO uint32_t *)deviceaddress + 4U);
  324. /* Return the data read */
  325. pNAND_ID->Maker_Id = ADDR_1ST_CYCLE(data);
  326. pNAND_ID->Device_Id = ADDR_3RD_CYCLE(data);
  327. pNAND_ID->Third_Id = ADDR_1ST_CYCLE(data1);
  328. pNAND_ID->Fourth_Id = ADDR_3RD_CYCLE(data1);
  329. }
  330. /* Update the NAND controller state */
  331. hnand->State = HAL_NAND_STATE_READY;
  332. /* Process unlocked */
  333. __HAL_UNLOCK(hnand);
  334. return HAL_OK;
  335. }
  336. /**
  337. * @brief NAND memory reset
  338. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  339. * the configuration information for NAND module.
  340. * @retval HAL status
  341. */
  342. HAL_StatusTypeDef HAL_NAND_Reset(NAND_HandleTypeDef *hnand)
  343. {
  344. uint32_t deviceaddress = 0U;
  345. /* Process Locked */
  346. __HAL_LOCK(hnand);
  347. /* Check the NAND controller state */
  348. if(hnand->State == HAL_NAND_STATE_BUSY)
  349. {
  350. return HAL_BUSY;
  351. }
  352. /* Identify the device address */
  353. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  354. {
  355. deviceaddress = NAND_DEVICE1;
  356. }
  357. else
  358. {
  359. deviceaddress = NAND_DEVICE2;
  360. }
  361. /* Update the NAND controller state */
  362. hnand->State = HAL_NAND_STATE_BUSY;
  363. /* Send NAND reset command */
  364. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = 0xFF;
  365. /* Update the NAND controller state */
  366. hnand->State = HAL_NAND_STATE_READY;
  367. /* Process unlocked */
  368. __HAL_UNLOCK(hnand);
  369. return HAL_OK;
  370. }
  371. /**
  372. * @brief Configure the device: Enter the physical parameters of the device
  373. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  374. * the configuration information for NAND module.
  375. * @param pDeviceConfig pointer to NAND_DeviceConfigTypeDef structure
  376. * @retval HAL status
  377. */
  378. HAL_StatusTypeDef HAL_NAND_ConfigDevice(NAND_HandleTypeDef *hnand, NAND_DeviceConfigTypeDef *pDeviceConfig)
  379. {
  380. hnand->Config.PageSize = pDeviceConfig->PageSize;
  381. hnand->Config.SpareAreaSize = pDeviceConfig->SpareAreaSize;
  382. hnand->Config.BlockSize = pDeviceConfig->BlockSize;
  383. hnand->Config.BlockNbr = pDeviceConfig->BlockNbr;
  384. hnand->Config.PlaneSize = pDeviceConfig->PlaneSize;
  385. hnand->Config.PlaneNbr = pDeviceConfig->PlaneNbr;
  386. hnand->Config.ExtraCommandEnable = pDeviceConfig->ExtraCommandEnable;
  387. return HAL_OK;
  388. }
  389. /**
  390. * @brief Read Page(s) from NAND memory block (8-bits addressing)
  391. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  392. * the configuration information for NAND module.
  393. * @param pAddress pointer to NAND address structure
  394. * @param pBuffer pointer to destination read buffer
  395. * @param NumPageToRead number of pages to read from block
  396. * @retval HAL status
  397. */
  398. HAL_StatusTypeDef HAL_NAND_Read_Page_8b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer, uint32_t NumPageToRead)
  399. {
  400. __IO uint32_t index = 0U;
  401. uint32_t tickstart = 0U;
  402. uint32_t deviceaddress = 0U, size = 0U, numPagesRead = 0U, nandaddress = 0U;
  403. /* Process Locked */
  404. __HAL_LOCK(hnand);
  405. /* Check the NAND controller state */
  406. if(hnand->State == HAL_NAND_STATE_BUSY)
  407. {
  408. return HAL_BUSY;
  409. }
  410. /* Identify the device address */
  411. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  412. {
  413. deviceaddress = NAND_DEVICE1;
  414. }
  415. else
  416. {
  417. deviceaddress = NAND_DEVICE2;
  418. }
  419. /* Update the NAND controller state */
  420. hnand->State = HAL_NAND_STATE_BUSY;
  421. /* NAND raw address calculation */
  422. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  423. /* Page(s) read loop */
  424. while((NumPageToRead != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  425. {
  426. /* update the buffer size */
  427. size = (hnand->Config.PageSize) + ((hnand->Config.PageSize) * numPagesRead);
  428. /* Send read page command sequence */
  429. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  430. /* Cards with page size <= 512 bytes */
  431. if((hnand->Config.PageSize) <= 512U)
  432. {
  433. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  434. {
  435. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  436. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  437. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  438. }
  439. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  440. {
  441. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  442. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  443. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  444. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  445. }
  446. }
  447. else /* (hnand->Config.PageSize) > 512 */
  448. {
  449. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  450. {
  451. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  452. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  453. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  454. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  455. }
  456. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  457. {
  458. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  459. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  460. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  461. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  462. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  463. }
  464. }
  465. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  466. /* Check if an extra command is needed for reading pages */
  467. if(hnand->Config.ExtraCommandEnable == ENABLE)
  468. {
  469. /* Get tick */
  470. tickstart = HAL_GetTick();
  471. /* Read status until NAND is ready */
  472. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  473. {
  474. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  475. {
  476. return HAL_TIMEOUT;
  477. }
  478. }
  479. /* Go back to read mode */
  480. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  481. __DSB();
  482. }
  483. /* Get Data into Buffer */
  484. for(; index < size; index++)
  485. {
  486. *(uint8_t *)pBuffer++ = *(uint8_t *)deviceaddress;
  487. }
  488. /* Increment read pages number */
  489. numPagesRead++;
  490. /* Decrement pages to read */
  491. NumPageToRead--;
  492. /* Increment the NAND address */
  493. nandaddress = (uint32_t)(nandaddress + 1U);
  494. }
  495. /* Update the NAND controller state */
  496. hnand->State = HAL_NAND_STATE_READY;
  497. /* Process unlocked */
  498. __HAL_UNLOCK(hnand);
  499. return HAL_OK;
  500. }
  501. /**
  502. * @brief Read Page(s) from NAND memory block (16-bits addressing)
  503. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  504. * the configuration information for NAND module.
  505. * @param pAddress pointer to NAND address structure
  506. * @param pBuffer pointer to destination read buffer. pBuffer should be 16bits aligned
  507. * @param NumPageToRead number of pages to read from block
  508. * @retval HAL status
  509. */
  510. HAL_StatusTypeDef HAL_NAND_Read_Page_16b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint16_t *pBuffer, uint32_t NumPageToRead)
  511. {
  512. __IO uint32_t index = 0U;
  513. uint32_t tickstart = 0U;
  514. uint32_t deviceaddress = 0U, size = 0U, numPagesRead = 0U, nandaddress = 0U;
  515. /* Process Locked */
  516. __HAL_LOCK(hnand);
  517. /* Check the NAND controller state */
  518. if(hnand->State == HAL_NAND_STATE_BUSY)
  519. {
  520. return HAL_BUSY;
  521. }
  522. /* Identify the device address */
  523. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  524. {
  525. deviceaddress = NAND_DEVICE1;
  526. }
  527. else
  528. {
  529. deviceaddress = NAND_DEVICE2;
  530. }
  531. /* Update the NAND controller state */
  532. hnand->State = HAL_NAND_STATE_BUSY;
  533. /* NAND raw address calculation */
  534. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  535. /* Page(s) read loop */
  536. while((NumPageToRead != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  537. {
  538. /* update the buffer size */
  539. size = (hnand->Config.PageSize) + ((hnand->Config.PageSize) * numPagesRead);
  540. /* Send read page command sequence */
  541. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  542. __DSB();
  543. /* Cards with page size <= 512 bytes */
  544. if((hnand->Config.PageSize) <= 512U)
  545. {
  546. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  547. {
  548. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  549. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  550. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  551. }
  552. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  553. {
  554. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  555. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  556. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  557. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  558. }
  559. }
  560. else /* (hnand->Config.PageSize) > 512 */
  561. {
  562. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  563. {
  564. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  565. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  566. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  567. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  568. }
  569. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  570. {
  571. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  572. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  573. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  574. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  575. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  576. }
  577. }
  578. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  579. if(hnand->Config.ExtraCommandEnable == ENABLE)
  580. {
  581. /* Get tick */
  582. tickstart = HAL_GetTick();
  583. /* Read status until NAND is ready */
  584. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  585. {
  586. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  587. {
  588. return HAL_TIMEOUT;
  589. }
  590. }
  591. /* Go back to read mode */
  592. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  593. }
  594. /* Get Data into Buffer */
  595. for(; index < size; index++)
  596. {
  597. *(uint16_t *)pBuffer++ = *(uint16_t *)deviceaddress;
  598. }
  599. /* Increment read pages number */
  600. numPagesRead++;
  601. /* Decrement pages to read */
  602. NumPageToRead--;
  603. /* Increment the NAND address */
  604. nandaddress = (uint32_t)(nandaddress + 1U);
  605. }
  606. /* Update the NAND controller state */
  607. hnand->State = HAL_NAND_STATE_READY;
  608. /* Process unlocked */
  609. __HAL_UNLOCK(hnand);
  610. return HAL_OK;
  611. }
  612. /**
  613. * @brief Write Page(s) to NAND memory block (8-bits addressing)
  614. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  615. * the configuration information for NAND module.
  616. * @param pAddress pointer to NAND address structure
  617. * @param pBuffer pointer to source buffer to write
  618. * @param NumPageToWrite number of pages to write to block
  619. * @retval HAL status
  620. */
  621. HAL_StatusTypeDef HAL_NAND_Write_Page_8b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer, uint32_t NumPageToWrite)
  622. {
  623. __IO uint32_t index = 0U;
  624. uint32_t tickstart = 0U;
  625. uint32_t deviceaddress = 0U, size = 0U, numPagesWritten = 0U, nandaddress = 0U;
  626. /* Process Locked */
  627. __HAL_LOCK(hnand);
  628. /* Check the NAND controller state */
  629. if(hnand->State == HAL_NAND_STATE_BUSY)
  630. {
  631. return HAL_BUSY;
  632. }
  633. /* Identify the device address */
  634. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  635. {
  636. deviceaddress = NAND_DEVICE1;
  637. }
  638. else
  639. {
  640. deviceaddress = NAND_DEVICE2;
  641. }
  642. /* Update the NAND controller state */
  643. hnand->State = HAL_NAND_STATE_BUSY;
  644. /* NAND raw address calculation */
  645. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  646. /* Page(s) write loop */
  647. while((NumPageToWrite != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  648. {
  649. /* update the buffer size */
  650. size = hnand->Config.PageSize + ((hnand->Config.PageSize) * numPagesWritten);
  651. /* Send write page command sequence */
  652. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  653. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  654. /* Cards with page size <= 512 bytes */
  655. if((hnand->Config.PageSize) <= 512U)
  656. {
  657. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  658. {
  659. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  660. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  661. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  662. }
  663. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  664. {
  665. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  666. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  667. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  668. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  669. }
  670. }
  671. else /* (hnand->Config.PageSize) > 512 */
  672. {
  673. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  674. {
  675. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  676. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  677. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  678. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  679. }
  680. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  681. {
  682. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  683. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  684. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  685. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  686. __DSB();
  687. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  688. __DSB();
  689. }
  690. }
  691. /* Write data to memory */
  692. for(; index < size; index++)
  693. {
  694. *(__IO uint8_t *)deviceaddress = *(uint8_t *)pBuffer++;
  695. }
  696. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  697. /* Read status until NAND is ready */
  698. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  699. {
  700. /* Get tick */
  701. tickstart = HAL_GetTick();
  702. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  703. {
  704. return HAL_TIMEOUT;
  705. }
  706. }
  707. /* Increment written pages number */
  708. numPagesWritten++;
  709. /* Decrement pages to write */
  710. NumPageToWrite--;
  711. /* Increment the NAND address */
  712. nandaddress = (uint32_t)(nandaddress + 1U);
  713. }
  714. /* Update the NAND controller state */
  715. hnand->State = HAL_NAND_STATE_READY;
  716. /* Process unlocked */
  717. __HAL_UNLOCK(hnand);
  718. return HAL_OK;
  719. }
  720. /**
  721. * @brief Write Page(s) to NAND memory block (16-bits addressing)
  722. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  723. * the configuration information for NAND module.
  724. * @param pAddress pointer to NAND address structure
  725. * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned
  726. * @param NumPageToWrite number of pages to write to block
  727. * @retval HAL status
  728. */
  729. HAL_StatusTypeDef HAL_NAND_Write_Page_16b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint16_t *pBuffer, uint32_t NumPageToWrite)
  730. {
  731. __IO uint32_t index = 0U;
  732. uint32_t tickstart = 0U;
  733. uint32_t deviceaddress = 0U, size = 0U, numPagesWritten = 0U, nandaddress = 0U;
  734. /* Process Locked */
  735. __HAL_LOCK(hnand);
  736. /* Check the NAND controller state */
  737. if(hnand->State == HAL_NAND_STATE_BUSY)
  738. {
  739. return HAL_BUSY;
  740. }
  741. /* Identify the device address */
  742. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  743. {
  744. deviceaddress = NAND_DEVICE1;
  745. }
  746. else
  747. {
  748. deviceaddress = NAND_DEVICE2;
  749. }
  750. /* Update the NAND controller state */
  751. hnand->State = HAL_NAND_STATE_BUSY;
  752. /* NAND raw address calculation */
  753. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  754. /* Page(s) write loop */
  755. while((NumPageToWrite != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  756. {
  757. /* update the buffer size */
  758. size = (hnand->Config.PageSize) + ((hnand->Config.PageSize) * numPagesWritten);
  759. /* Send write page command sequence */
  760. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  761. __DSB();
  762. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  763. __DSB();
  764. /* Cards with page size <= 512 bytes */
  765. if((hnand->Config.PageSize) <= 512U)
  766. {
  767. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  768. {
  769. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  770. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  771. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  772. }
  773. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  774. {
  775. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  776. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  777. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  778. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  779. }
  780. }
  781. else /* (hnand->Config.PageSize) > 512 */
  782. {
  783. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  784. {
  785. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  786. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  787. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  788. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  789. }
  790. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  791. {
  792. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  793. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  794. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  795. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  796. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  797. }
  798. }
  799. /* Write data to memory */
  800. for(; index < size; index++)
  801. {
  802. *(__IO uint16_t *)deviceaddress = *(uint16_t *)pBuffer++;
  803. }
  804. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  805. /* Read status until NAND is ready */
  806. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  807. {
  808. /* Get tick */
  809. tickstart = HAL_GetTick();
  810. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  811. {
  812. return HAL_TIMEOUT;
  813. }
  814. }
  815. /* Increment written pages number */
  816. numPagesWritten++;
  817. /* Decrement pages to write */
  818. NumPageToWrite--;
  819. /* Increment the NAND address */
  820. nandaddress = (uint32_t)(nandaddress + 1U);
  821. }
  822. /* Update the NAND controller state */
  823. hnand->State = HAL_NAND_STATE_READY;
  824. /* Process unlocked */
  825. __HAL_UNLOCK(hnand);
  826. return HAL_OK;
  827. }
  828. /**
  829. * @brief Read Spare area(s) from NAND memory
  830. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  831. * the configuration information for NAND module.
  832. * @param pAddress pointer to NAND address structure
  833. * @param pBuffer pointer to source buffer to write
  834. * @param NumSpareAreaToRead Number of spare area to read
  835. * @retval HAL status
  836. */
  837. HAL_StatusTypeDef HAL_NAND_Read_SpareArea_8b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer, uint32_t NumSpareAreaToRead)
  838. {
  839. __IO uint32_t index = 0U;
  840. uint32_t tickstart = 0U;
  841. uint32_t deviceaddress = 0U, size = 0U, numSpareAreaRead = 0U, nandaddress = 0U, columnaddress = 0U;
  842. /* Process Locked */
  843. __HAL_LOCK(hnand);
  844. /* Check the NAND controller state */
  845. if(hnand->State == HAL_NAND_STATE_BUSY)
  846. {
  847. return HAL_BUSY;
  848. }
  849. /* Identify the device address */
  850. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  851. {
  852. deviceaddress = NAND_DEVICE1;
  853. }
  854. else
  855. {
  856. deviceaddress = NAND_DEVICE2;
  857. }
  858. /* Update the NAND controller state */
  859. hnand->State = HAL_NAND_STATE_BUSY;
  860. /* NAND raw address calculation */
  861. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  862. /* Column in page address */
  863. columnaddress = COLUMN_ADDRESS(hnand);
  864. /* Spare area(s) read loop */
  865. while((NumSpareAreaToRead != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  866. {
  867. /* update the buffer size */
  868. size = (hnand->Config.SpareAreaSize) + ((hnand->Config.SpareAreaSize) * numSpareAreaRead);
  869. /* Cards with page size <= 512 bytes */
  870. if((hnand->Config.PageSize) <= 512U)
  871. {
  872. /* Send read spare area command sequence */
  873. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  874. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  875. {
  876. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  877. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  878. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  879. }
  880. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  881. {
  882. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  883. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  884. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  885. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  886. }
  887. }
  888. else /* (hnand->Config.PageSize) > 512 */
  889. {
  890. /* Send read spare area command sequence */
  891. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  892. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  893. {
  894. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  895. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  896. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  897. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  898. }
  899. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  900. {
  901. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  902. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  903. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  904. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  905. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  906. }
  907. }
  908. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  909. if(hnand->Config.ExtraCommandEnable == ENABLE)
  910. {
  911. /* Get tick */
  912. tickstart = HAL_GetTick();
  913. /* Read status until NAND is ready */
  914. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  915. {
  916. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  917. {
  918. return HAL_TIMEOUT;
  919. }
  920. }
  921. /* Go back to read mode */
  922. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  923. }
  924. /* Get Data into Buffer */
  925. for(; index < size; index++)
  926. {
  927. *(uint8_t *)pBuffer++ = *(uint8_t *)deviceaddress;
  928. }
  929. /* Increment read spare areas number */
  930. numSpareAreaRead++;
  931. /* Decrement spare areas to read */
  932. NumSpareAreaToRead--;
  933. /* Increment the NAND address */
  934. nandaddress = (uint32_t)(nandaddress + 1U);
  935. }
  936. /* Update the NAND controller state */
  937. hnand->State = HAL_NAND_STATE_READY;
  938. /* Process unlocked */
  939. __HAL_UNLOCK(hnand);
  940. return HAL_OK;
  941. }
  942. /**
  943. * @brief Read Spare area(s) from NAND memory (16-bits addressing)
  944. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  945. * the configuration information for NAND module.
  946. * @param pAddress pointer to NAND address structure
  947. * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned.
  948. * @param NumSpareAreaToRead Number of spare area to read
  949. * @retval HAL status
  950. */
  951. HAL_StatusTypeDef HAL_NAND_Read_SpareArea_16b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint16_t *pBuffer, uint32_t NumSpareAreaToRead)
  952. {
  953. __IO uint32_t index = 0U;
  954. uint32_t tickstart = 0U;
  955. uint32_t deviceaddress = 0U, size = 0U, numSpareAreaRead = 0U, nandaddress = 0U, columnaddress = 0U;
  956. /* Process Locked */
  957. __HAL_LOCK(hnand);
  958. /* Check the NAND controller state */
  959. if(hnand->State == HAL_NAND_STATE_BUSY)
  960. {
  961. return HAL_BUSY;
  962. }
  963. /* Identify the device address */
  964. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  965. {
  966. deviceaddress = NAND_DEVICE1;
  967. }
  968. else
  969. {
  970. deviceaddress = NAND_DEVICE2;
  971. }
  972. /* Update the NAND controller state */
  973. hnand->State = HAL_NAND_STATE_BUSY;
  974. /* NAND raw address calculation */
  975. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  976. /* Column in page address */
  977. columnaddress = (uint32_t)(COLUMN_ADDRESS(hnand) * 2U);
  978. /* Spare area(s) read loop */
  979. while((NumSpareAreaToRead != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  980. {
  981. /* update the buffer size */
  982. size = (hnand->Config.SpareAreaSize) + ((hnand->Config.SpareAreaSize) * numSpareAreaRead);
  983. /* Cards with page size <= 512 bytes */
  984. if((hnand->Config.PageSize) <= 512U)
  985. {
  986. /* Send read spare area command sequence */
  987. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  988. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  989. {
  990. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  991. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  992. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  993. }
  994. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  995. {
  996. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  997. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  998. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  999. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1000. }
  1001. }
  1002. else /* (hnand->Config.PageSize) > 512 */
  1003. {
  1004. /* Send read spare area command sequence */
  1005. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1006. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  1007. {
  1008. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1009. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1010. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1011. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1012. }
  1013. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1014. {
  1015. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1016. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1017. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1018. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1019. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1020. }
  1021. }
  1022. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  1023. if(hnand->Config.ExtraCommandEnable == ENABLE)
  1024. {
  1025. /* Get tick */
  1026. tickstart = HAL_GetTick();
  1027. /* Read status until NAND is ready */
  1028. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  1029. {
  1030. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  1031. {
  1032. return HAL_TIMEOUT;
  1033. }
  1034. }
  1035. /* Go back to read mode */
  1036. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  1037. }
  1038. /* Get Data into Buffer */
  1039. for(; index < size; index++)
  1040. {
  1041. *(uint16_t *)pBuffer++ = *(uint16_t *)deviceaddress;
  1042. }
  1043. /* Increment read spare areas number */
  1044. numSpareAreaRead++;
  1045. /* Decrement spare areas to read */
  1046. NumSpareAreaToRead--;
  1047. /* Increment the NAND address */
  1048. nandaddress = (uint32_t)(nandaddress + 1U);
  1049. }
  1050. /* Update the NAND controller state */
  1051. hnand->State = HAL_NAND_STATE_READY;
  1052. /* Process unlocked */
  1053. __HAL_UNLOCK(hnand);
  1054. return HAL_OK;
  1055. }
  1056. /**
  1057. * @brief Write Spare area(s) to NAND memory
  1058. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1059. * the configuration information for NAND module.
  1060. * @param pAddress pointer to NAND address structure
  1061. * @param pBuffer pointer to source buffer to write
  1062. * @param NumSpareAreaTowrite number of spare areas to write to block
  1063. * @retval HAL status
  1064. */
  1065. HAL_StatusTypeDef HAL_NAND_Write_SpareArea_8b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer, uint32_t NumSpareAreaTowrite)
  1066. {
  1067. __IO uint32_t index = 0U;
  1068. uint32_t tickstart = 0U;
  1069. uint32_t deviceaddress = 0U, size = 0U, numSpareAreaWritten = 0U, nandaddress = 0U, columnaddress = 0U;
  1070. /* Process Locked */
  1071. __HAL_LOCK(hnand);
  1072. /* Check the NAND controller state */
  1073. if(hnand->State == HAL_NAND_STATE_BUSY)
  1074. {
  1075. return HAL_BUSY;
  1076. }
  1077. /* Identify the device address */
  1078. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  1079. {
  1080. deviceaddress = NAND_DEVICE1;
  1081. }
  1082. else
  1083. {
  1084. deviceaddress = NAND_DEVICE2;
  1085. }
  1086. /* Update the FMC_NAND controller state */
  1087. hnand->State = HAL_NAND_STATE_BUSY;
  1088. /* Page address calculation */
  1089. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1090. /* Column in page address */
  1091. columnaddress = COLUMN_ADDRESS(hnand);
  1092. /* Spare area(s) write loop */
  1093. while((NumSpareAreaTowrite != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1094. {
  1095. /* update the buffer size */
  1096. size = (hnand->Config.SpareAreaSize) + ((hnand->Config.SpareAreaSize) * numSpareAreaWritten);
  1097. /* Cards with page size <= 512 bytes */
  1098. if((hnand->Config.PageSize) <= 512U)
  1099. {
  1100. /* Send write Spare area command sequence */
  1101. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1102. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1103. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  1104. {
  1105. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  1106. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1107. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1108. }
  1109. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1110. {
  1111. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  1112. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1113. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1114. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1115. }
  1116. }
  1117. else /* (hnand->Config.PageSize) > 512 */
  1118. {
  1119. /* Send write Spare area command sequence */
  1120. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1121. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1122. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  1123. {
  1124. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1125. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1126. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1127. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1128. }
  1129. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1130. {
  1131. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1132. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1133. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1134. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1135. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1136. }
  1137. }
  1138. /* Write data to memory */
  1139. for(; index < size; index++)
  1140. {
  1141. *(__IO uint8_t *)deviceaddress = *(uint8_t *)pBuffer++;
  1142. }
  1143. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  1144. /* Get tick */
  1145. tickstart = HAL_GetTick();
  1146. /* Read status until NAND is ready */
  1147. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  1148. {
  1149. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  1150. {
  1151. return HAL_TIMEOUT;
  1152. }
  1153. }
  1154. /* Increment written spare areas number */
  1155. numSpareAreaWritten++;
  1156. /* Decrement spare areas to write */
  1157. NumSpareAreaTowrite--;
  1158. /* Increment the NAND address */
  1159. nandaddress = (uint32_t)(nandaddress + 1U);
  1160. }
  1161. /* Update the NAND controller state */
  1162. hnand->State = HAL_NAND_STATE_READY;
  1163. /* Process unlocked */
  1164. __HAL_UNLOCK(hnand);
  1165. return HAL_OK;
  1166. }
  1167. /**
  1168. * @brief Write Spare area(s) to NAND memory (16-bits addressing)
  1169. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1170. * the configuration information for NAND module.
  1171. * @param pAddress pointer to NAND address structure
  1172. * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned.
  1173. * @param NumSpareAreaTowrite number of spare areas to write to block
  1174. * @retval HAL status
  1175. */
  1176. HAL_StatusTypeDef HAL_NAND_Write_SpareArea_16b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint16_t *pBuffer, uint32_t NumSpareAreaTowrite)
  1177. {
  1178. __IO uint32_t index = 0U;
  1179. uint32_t tickstart = 0U;
  1180. uint32_t deviceaddress = 0U, size = 0U, numSpareAreaWritten = 0U, nandaddress = 0U, columnaddress = 0U;
  1181. /* Process Locked */
  1182. __HAL_LOCK(hnand);
  1183. /* Check the NAND controller state */
  1184. if(hnand->State == HAL_NAND_STATE_BUSY)
  1185. {
  1186. return HAL_BUSY;
  1187. }
  1188. /* Identify the device address */
  1189. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  1190. {
  1191. deviceaddress = NAND_DEVICE1;
  1192. }
  1193. else
  1194. {
  1195. deviceaddress = NAND_DEVICE2;
  1196. }
  1197. /* Update the FMC_NAND controller state */
  1198. hnand->State = HAL_NAND_STATE_BUSY;
  1199. /* NAND raw address calculation */
  1200. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1201. /* Column in page address */
  1202. columnaddress = (uint32_t)(COLUMN_ADDRESS(hnand) * 2U);
  1203. /* Spare area(s) write loop */
  1204. while((NumSpareAreaTowrite != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1205. {
  1206. /* update the buffer size */
  1207. size = (hnand->Config.SpareAreaSize) + ((hnand->Config.SpareAreaSize) * numSpareAreaWritten);
  1208. /* Cards with page size <= 512 bytes */
  1209. if((hnand->Config.PageSize) <= 512U)
  1210. {
  1211. /* Send write Spare area command sequence */
  1212. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1213. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1214. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  1215. {
  1216. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  1217. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1218. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1219. }
  1220. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1221. {
  1222. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  1223. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1224. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1225. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1226. }
  1227. }
  1228. else /* (hnand->Config.PageSize) > 512 */
  1229. {
  1230. /* Send write Spare area command sequence */
  1231. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1232. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1233. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  1234. {
  1235. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1236. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1237. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1238. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1239. }
  1240. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1241. {
  1242. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1243. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1244. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1245. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1246. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1247. }
  1248. }
  1249. /* Write data to memory */
  1250. for(; index < size; index++)
  1251. {
  1252. *(__IO uint16_t *)deviceaddress = *(uint16_t *)pBuffer++;
  1253. }
  1254. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  1255. /* Read status until NAND is ready */
  1256. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  1257. {
  1258. /* Get tick */
  1259. tickstart = HAL_GetTick();
  1260. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  1261. {
  1262. return HAL_TIMEOUT;
  1263. }
  1264. }
  1265. /* Increment written spare areas number */
  1266. numSpareAreaWritten++;
  1267. /* Decrement spare areas to write */
  1268. NumSpareAreaTowrite--;
  1269. /* Increment the NAND address */
  1270. nandaddress = (uint32_t)(nandaddress + 1U);
  1271. }
  1272. /* Update the NAND controller state */
  1273. hnand->State = HAL_NAND_STATE_READY;
  1274. /* Process unlocked */
  1275. __HAL_UNLOCK(hnand);
  1276. return HAL_OK;
  1277. }
  1278. /**
  1279. * @brief NAND memory Block erase
  1280. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1281. * the configuration information for NAND module.
  1282. * @param pAddress pointer to NAND address structure
  1283. * @retval HAL status
  1284. */
  1285. HAL_StatusTypeDef HAL_NAND_Erase_Block(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress)
  1286. {
  1287. uint32_t deviceaddress = 0U;
  1288. uint32_t tickstart = 0U;
  1289. /* Process Locked */
  1290. __HAL_LOCK(hnand);
  1291. /* Check the NAND controller state */
  1292. if(hnand->State == HAL_NAND_STATE_BUSY)
  1293. {
  1294. return HAL_BUSY;
  1295. }
  1296. /* Identify the device address */
  1297. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  1298. {
  1299. deviceaddress = NAND_DEVICE1;
  1300. }
  1301. else
  1302. {
  1303. deviceaddress = NAND_DEVICE2;
  1304. }
  1305. /* Update the NAND controller state */
  1306. hnand->State = HAL_NAND_STATE_BUSY;
  1307. /* Send Erase block command sequence */
  1308. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_ERASE0;
  1309. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1310. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1311. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1312. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_ERASE1;
  1313. /* Update the NAND controller state */
  1314. hnand->State = HAL_NAND_STATE_READY;
  1315. /* Get tick */
  1316. tickstart = HAL_GetTick();
  1317. /* Read status until NAND is ready */
  1318. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  1319. {
  1320. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  1321. {
  1322. /* Process unlocked */
  1323. __HAL_UNLOCK(hnand);
  1324. return HAL_TIMEOUT;
  1325. }
  1326. }
  1327. /* Process unlocked */
  1328. __HAL_UNLOCK(hnand);
  1329. return HAL_OK;
  1330. }
  1331. /**
  1332. * @brief NAND memory read status
  1333. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1334. * the configuration information for NAND module.
  1335. * @retval NAND status
  1336. */
  1337. uint32_t HAL_NAND_Read_Status(NAND_HandleTypeDef *hnand)
  1338. {
  1339. uint32_t data = 0U;
  1340. uint32_t deviceaddress = 0U;
  1341. /* Identify the device address */
  1342. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  1343. {
  1344. deviceaddress = NAND_DEVICE1;
  1345. }
  1346. else
  1347. {
  1348. deviceaddress = NAND_DEVICE2;
  1349. }
  1350. /* Send Read status operation command */
  1351. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_STATUS;
  1352. /* Read status register data */
  1353. data = *(__IO uint8_t *)deviceaddress;
  1354. /* Return the status */
  1355. if((data & NAND_ERROR) == NAND_ERROR)
  1356. {
  1357. return NAND_ERROR;
  1358. }
  1359. else if((data & NAND_READY) == NAND_READY)
  1360. {
  1361. return NAND_READY;
  1362. }
  1363. return NAND_BUSY;
  1364. }
  1365. /**
  1366. * @brief Increment the NAND memory address
  1367. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1368. * the configuration information for NAND module.
  1369. * @param pAddress pointer to NAND address structure
  1370. * @retval The new status of the increment address operation. It can be:
  1371. * - NAND_VALID_ADDRESS: When the new address is valid address
  1372. * - NAND_INVALID_ADDRESS: When the new address is invalid address
  1373. */
  1374. uint32_t HAL_NAND_Address_Inc(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress)
  1375. {
  1376. uint32_t status = NAND_VALID_ADDRESS;
  1377. /* Increment page address */
  1378. pAddress->Page++;
  1379. /* Check NAND address is valid */
  1380. if(pAddress->Page == hnand->Config.BlockSize)
  1381. {
  1382. pAddress->Page = 0U;
  1383. pAddress->Block++;
  1384. if(pAddress->Block == hnand->Config.PlaneSize)
  1385. {
  1386. pAddress->Block = 0U;
  1387. pAddress->Plane++;
  1388. if(pAddress->Plane == (hnand->Config.PlaneNbr))
  1389. {
  1390. status = NAND_INVALID_ADDRESS;
  1391. }
  1392. }
  1393. }
  1394. return (status);
  1395. }
  1396. /**
  1397. * @}
  1398. */
  1399. /** @defgroup NAND_Exported_Functions_Group3 Peripheral Control functions
  1400. * @brief management functions
  1401. *
  1402. @verbatim
  1403. ==============================================================================
  1404. ##### NAND Control functions #####
  1405. ==============================================================================
  1406. [..]
  1407. This subsection provides a set of functions allowing to control dynamically
  1408. the NAND interface.
  1409. @endverbatim
  1410. * @{
  1411. */
  1412. /**
  1413. * @brief Enables dynamically NAND ECC feature.
  1414. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1415. * the configuration information for NAND module.
  1416. * @retval HAL status
  1417. */
  1418. HAL_StatusTypeDef HAL_NAND_ECC_Enable(NAND_HandleTypeDef *hnand)
  1419. {
  1420. /* Check the NAND controller state */
  1421. if(hnand->State == HAL_NAND_STATE_BUSY)
  1422. {
  1423. return HAL_BUSY;
  1424. }
  1425. /* Update the NAND state */
  1426. hnand->State = HAL_NAND_STATE_BUSY;
  1427. /* Enable ECC feature */
  1428. FMC_NAND_ECC_Enable(hnand->Instance, hnand->Init.NandBank);
  1429. /* Update the NAND state */
  1430. hnand->State = HAL_NAND_STATE_READY;
  1431. return HAL_OK;
  1432. }
  1433. /**
  1434. * @brief Disables dynamically FMC_NAND ECC feature.
  1435. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1436. * the configuration information for NAND module.
  1437. * @retval HAL status
  1438. */
  1439. HAL_StatusTypeDef HAL_NAND_ECC_Disable(NAND_HandleTypeDef *hnand)
  1440. {
  1441. /* Check the NAND controller state */
  1442. if(hnand->State == HAL_NAND_STATE_BUSY)
  1443. {
  1444. return HAL_BUSY;
  1445. }
  1446. /* Update the NAND state */
  1447. hnand->State = HAL_NAND_STATE_BUSY;
  1448. /* Disable ECC feature */
  1449. FMC_NAND_ECC_Disable(hnand->Instance, hnand->Init.NandBank);
  1450. /* Update the NAND state */
  1451. hnand->State = HAL_NAND_STATE_READY;
  1452. return HAL_OK;
  1453. }
  1454. /**
  1455. * @brief Disables dynamically NAND ECC feature.
  1456. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1457. * the configuration information for NAND module.
  1458. * @param ECCval pointer to ECC value
  1459. * @param Timeout maximum timeout to wait
  1460. * @retval HAL status
  1461. */
  1462. HAL_StatusTypeDef HAL_NAND_GetECC(NAND_HandleTypeDef *hnand, uint32_t *ECCval, uint32_t Timeout)
  1463. {
  1464. HAL_StatusTypeDef status = HAL_OK;
  1465. /* Check the NAND controller state */
  1466. if(hnand->State == HAL_NAND_STATE_BUSY)
  1467. {
  1468. return HAL_BUSY;
  1469. }
  1470. /* Update the NAND state */
  1471. hnand->State = HAL_NAND_STATE_BUSY;
  1472. /* Get NAND ECC value */
  1473. status = FMC_NAND_GetECC(hnand->Instance, ECCval, hnand->Init.NandBank, Timeout);
  1474. /* Update the NAND state */
  1475. hnand->State = HAL_NAND_STATE_READY;
  1476. return status;
  1477. }
  1478. /**
  1479. * @}
  1480. */
  1481. /** @defgroup NAND_Exported_Functions_Group4 Peripheral State functions
  1482. * @brief Peripheral State functions
  1483. *
  1484. @verbatim
  1485. ==============================================================================
  1486. ##### NAND State functions #####
  1487. ==============================================================================
  1488. [..]
  1489. This subsection permits to get in run-time the status of the NAND controller
  1490. and the data flow.
  1491. @endverbatim
  1492. * @{
  1493. */
  1494. /**
  1495. * @brief return the NAND state
  1496. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1497. * the configuration information for NAND module.
  1498. * @retval HAL state
  1499. */
  1500. HAL_NAND_StateTypeDef HAL_NAND_GetState(NAND_HandleTypeDef *hnand)
  1501. {
  1502. return hnand->State;
  1503. }
  1504. /**
  1505. * @}
  1506. */
  1507. /**
  1508. * @}
  1509. */
  1510. /**
  1511. * @}
  1512. */
  1513. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx ||\
  1514. STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||\
  1515. STM32F446xx || STM32F469xx || STM32F479xx */
  1516. #endif /* HAL_NAND_MODULE_ENABLED */
  1517. /**
  1518. * @}
  1519. */
  1520. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/