stm32f1xx_hal_nand.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_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. ******************************************************************************
  10. * @attention
  11. *
  12. * Copyright (c) 2016 STMicroelectronics.
  13. * All rights reserved.
  14. *
  15. * This software is licensed under terms that can be found in the LICENSE file
  16. * in the root directory of this software component.
  17. * If no LICENSE file comes with this software, it is provided AS-IS.
  18. *
  19. ******************************************************************************
  20. @verbatim
  21. ==============================================================================
  22. ##### How to use this driver #####
  23. ==============================================================================
  24. [..]
  25. This driver is a generic layered driver which contains a set of APIs used to
  26. control NAND flash memories. It uses the FSMC layer functions to interface
  27. with NAND devices. This driver is used as follows:
  28. (+) NAND flash memory configuration sequence using the function HAL_NAND_Init()
  29. with control and timing parameters for both common and attribute spaces.
  30. (+) Read NAND flash memory maker and device IDs using the function
  31. HAL_NAND_Read_ID(). The read information is stored in the NAND_ID_TypeDef
  32. structure declared by the function caller.
  33. (+) Access NAND flash memory by read/write operations using the functions
  34. HAL_NAND_Read_Page_8b()/HAL_NAND_Read_SpareArea_8b(),
  35. HAL_NAND_Write_Page_8b()/HAL_NAND_Write_SpareArea_8b(),
  36. HAL_NAND_Read_Page_16b()/HAL_NAND_Read_SpareArea_16b(),
  37. HAL_NAND_Write_Page_16b()/HAL_NAND_Write_SpareArea_16b()
  38. to read/write page(s)/spare area(s). These functions use specific device
  39. information (Block, page size..) predefined by the user in the NAND_DeviceConfigTypeDef
  40. structure. The read/write address information is contained by the Nand_Address_Typedef
  41. structure passed as parameter.
  42. (+) Perform NAND flash Reset chip operation using the function HAL_NAND_Reset().
  43. (+) Perform NAND flash erase block operation using the function HAL_NAND_Erase_Block().
  44. The erase block address information is contained in the Nand_Address_Typedef
  45. structure passed as parameter.
  46. (+) Read the NAND flash status operation using the function HAL_NAND_Read_Status().
  47. (+) You can also control the NAND device by calling the control APIs HAL_NAND_ECC_Enable()/
  48. HAL_NAND_ECC_Disable() to respectively enable/disable the ECC code correction
  49. feature or the function HAL_NAND_GetECC() to get the ECC correction code.
  50. (+) You can monitor the NAND device HAL state by calling the function
  51. HAL_NAND_GetState()
  52. [..]
  53. (@) This driver is a set of generic APIs which handle standard NAND flash operations.
  54. If a NAND flash device contains different operations and/or implementations,
  55. it should be implemented separately.
  56. *** Callback registration ***
  57. =============================================
  58. [..]
  59. The compilation define USE_HAL_NAND_REGISTER_CALLBACKS when set to 1
  60. allows the user to configure dynamically the driver callbacks.
  61. Use Functions HAL_NAND_RegisterCallback() to register a user callback,
  62. it allows to register following callbacks:
  63. (+) MspInitCallback : NAND MspInit.
  64. (+) MspDeInitCallback : NAND MspDeInit.
  65. This function takes as parameters the HAL peripheral handle, the Callback ID
  66. and a pointer to the user callback function.
  67. Use function HAL_NAND_UnRegisterCallback() to reset a callback to the default
  68. weak (overridden) function. It allows to reset following callbacks:
  69. (+) MspInitCallback : NAND MspInit.
  70. (+) MspDeInitCallback : NAND MspDeInit.
  71. This function) takes as parameters the HAL peripheral handle and the Callback ID.
  72. By default, after the HAL_NAND_Init and if the state is HAL_NAND_STATE_RESET
  73. all callbacks are reset to the corresponding legacy weak (overridden) functions.
  74. Exception done for MspInit and MspDeInit callbacks that are respectively
  75. reset to the legacy weak (overridden) functions in the HAL_NAND_Init
  76. and HAL_NAND_DeInit only when these callbacks are null (not registered beforehand).
  77. If not, MspInit or MspDeInit are not null, the HAL_NAND_Init and HAL_NAND_DeInit
  78. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  79. Callbacks can be registered/unregistered in READY state only.
  80. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  81. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  82. during the Init/DeInit.
  83. In that case first register the MspInit/MspDeInit user callbacks
  84. using HAL_NAND_RegisterCallback before calling HAL_NAND_DeInit
  85. or HAL_NAND_Init function.
  86. When The compilation define USE_HAL_NAND_REGISTER_CALLBACKS is set to 0 or
  87. not defined, the callback registering feature is not available
  88. and weak (overridden) callbacks are used.
  89. @endverbatim
  90. ******************************************************************************
  91. */
  92. /* Includes ------------------------------------------------------------------*/
  93. #include "stm32f1xx_hal.h"
  94. #if defined(FSMC_BANK3)
  95. /** @addtogroup STM32F1xx_HAL_Driver
  96. * @{
  97. */
  98. #ifdef HAL_NAND_MODULE_ENABLED
  99. /** @defgroup NAND NAND
  100. * @brief NAND HAL module driver
  101. * @{
  102. */
  103. /* Private typedef -----------------------------------------------------------*/
  104. /* Private Constants ------------------------------------------------------------*/
  105. /* Private macro -------------------------------------------------------------*/
  106. /* Private variables ---------------------------------------------------------*/
  107. /* Private function prototypes -----------------------------------------------*/
  108. /* Exported functions ---------------------------------------------------------*/
  109. /** @defgroup NAND_Exported_Functions NAND Exported Functions
  110. * @{
  111. */
  112. /** @defgroup NAND_Exported_Functions_Group1 Initialization and de-initialization functions
  113. * @brief Initialization and Configuration functions
  114. *
  115. @verbatim
  116. ==============================================================================
  117. ##### NAND Initialization and de-initialization functions #####
  118. ==============================================================================
  119. [..]
  120. This section provides functions allowing to initialize/de-initialize
  121. the NAND memory
  122. @endverbatim
  123. * @{
  124. */
  125. /**
  126. * @brief Perform NAND memory Initialization sequence
  127. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  128. * the configuration information for NAND module.
  129. * @param ComSpace_Timing pointer to Common space timing structure
  130. * @param AttSpace_Timing pointer to Attribute space timing structure
  131. * @retval HAL status
  132. */
  133. HAL_StatusTypeDef HAL_NAND_Init(NAND_HandleTypeDef *hnand, FSMC_NAND_PCC_TimingTypeDef *ComSpace_Timing,
  134. FSMC_NAND_PCC_TimingTypeDef *AttSpace_Timing)
  135. {
  136. /* Check the NAND handle state */
  137. if (hnand == NULL)
  138. {
  139. return HAL_ERROR;
  140. }
  141. if (hnand->State == HAL_NAND_STATE_RESET)
  142. {
  143. /* Allocate lock resource and initialize it */
  144. hnand->Lock = HAL_UNLOCKED;
  145. #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
  146. if (hnand->MspInitCallback == NULL)
  147. {
  148. hnand->MspInitCallback = HAL_NAND_MspInit;
  149. }
  150. hnand->ItCallback = HAL_NAND_ITCallback;
  151. /* Init the low level hardware */
  152. hnand->MspInitCallback(hnand);
  153. #else
  154. /* Initialize the low level hardware (MSP) */
  155. HAL_NAND_MspInit(hnand);
  156. #endif /* (USE_HAL_NAND_REGISTER_CALLBACKS) */
  157. }
  158. /* Initialize NAND control Interface */
  159. (void)FSMC_NAND_Init(hnand->Instance, &(hnand->Init));
  160. /* Initialize NAND common space timing Interface */
  161. (void)FSMC_NAND_CommonSpace_Timing_Init(hnand->Instance, ComSpace_Timing, hnand->Init.NandBank);
  162. /* Initialize NAND attribute space timing Interface */
  163. (void)FSMC_NAND_AttributeSpace_Timing_Init(hnand->Instance, AttSpace_Timing, hnand->Init.NandBank);
  164. /* Enable the NAND device */
  165. __FSMC_NAND_ENABLE(hnand->Instance, hnand->Init.NandBank);
  166. /* Update the NAND controller state */
  167. hnand->State = HAL_NAND_STATE_READY;
  168. return HAL_OK;
  169. }
  170. /**
  171. * @brief Perform NAND memory De-Initialization sequence
  172. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  173. * the configuration information for NAND module.
  174. * @retval HAL status
  175. */
  176. HAL_StatusTypeDef HAL_NAND_DeInit(NAND_HandleTypeDef *hnand)
  177. {
  178. #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
  179. if (hnand->MspDeInitCallback == NULL)
  180. {
  181. hnand->MspDeInitCallback = HAL_NAND_MspDeInit;
  182. }
  183. /* DeInit the low level hardware */
  184. hnand->MspDeInitCallback(hnand);
  185. #else
  186. /* Initialize the low level hardware (MSP) */
  187. HAL_NAND_MspDeInit(hnand);
  188. #endif /* (USE_HAL_NAND_REGISTER_CALLBACKS) */
  189. /* Configure the NAND registers with their reset values */
  190. (void)FSMC_NAND_DeInit(hnand->Instance, hnand->Init.NandBank);
  191. /* Reset the NAND controller state */
  192. hnand->State = HAL_NAND_STATE_RESET;
  193. /* Release Lock */
  194. __HAL_UNLOCK(hnand);
  195. return HAL_OK;
  196. }
  197. /**
  198. * @brief NAND MSP Init
  199. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  200. * the configuration information for NAND module.
  201. * @retval None
  202. */
  203. __weak void HAL_NAND_MspInit(NAND_HandleTypeDef *hnand)
  204. {
  205. /* Prevent unused argument(s) compilation warning */
  206. UNUSED(hnand);
  207. /* NOTE : This function Should not be modified, when the callback is needed,
  208. the HAL_NAND_MspInit could be implemented in the user file
  209. */
  210. }
  211. /**
  212. * @brief NAND MSP DeInit
  213. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  214. * the configuration information for NAND module.
  215. * @retval None
  216. */
  217. __weak void HAL_NAND_MspDeInit(NAND_HandleTypeDef *hnand)
  218. {
  219. /* Prevent unused argument(s) compilation warning */
  220. UNUSED(hnand);
  221. /* NOTE : This function Should not be modified, when the callback is needed,
  222. the HAL_NAND_MspDeInit could be implemented in the user file
  223. */
  224. }
  225. /**
  226. * @brief This function handles NAND device interrupt request.
  227. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  228. * the configuration information for NAND module.
  229. * @retval HAL status
  230. */
  231. void HAL_NAND_IRQHandler(NAND_HandleTypeDef *hnand)
  232. {
  233. /* Check NAND interrupt Rising edge flag */
  234. if (__FSMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FSMC_FLAG_RISING_EDGE))
  235. {
  236. /* NAND interrupt callback*/
  237. #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
  238. hnand->ItCallback(hnand);
  239. #else
  240. HAL_NAND_ITCallback(hnand);
  241. #endif /* (USE_HAL_NAND_REGISTER_CALLBACKS) */
  242. /* Clear NAND interrupt Rising edge pending bit */
  243. __FSMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FSMC_FLAG_RISING_EDGE);
  244. }
  245. /* Check NAND interrupt Level flag */
  246. if (__FSMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FSMC_FLAG_LEVEL))
  247. {
  248. /* NAND interrupt callback*/
  249. #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
  250. hnand->ItCallback(hnand);
  251. #else
  252. HAL_NAND_ITCallback(hnand);
  253. #endif /* (USE_HAL_NAND_REGISTER_CALLBACKS) */
  254. /* Clear NAND interrupt Level pending bit */
  255. __FSMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FSMC_FLAG_LEVEL);
  256. }
  257. /* Check NAND interrupt Falling edge flag */
  258. if (__FSMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FSMC_FLAG_FALLING_EDGE))
  259. {
  260. /* NAND interrupt callback*/
  261. #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
  262. hnand->ItCallback(hnand);
  263. #else
  264. HAL_NAND_ITCallback(hnand);
  265. #endif /* (USE_HAL_NAND_REGISTER_CALLBACKS) */
  266. /* Clear NAND interrupt Falling edge pending bit */
  267. __FSMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FSMC_FLAG_FALLING_EDGE);
  268. }
  269. /* Check NAND interrupt FIFO empty flag */
  270. if (__FSMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FSMC_FLAG_FEMPT))
  271. {
  272. /* NAND interrupt callback*/
  273. #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
  274. hnand->ItCallback(hnand);
  275. #else
  276. HAL_NAND_ITCallback(hnand);
  277. #endif /* (USE_HAL_NAND_REGISTER_CALLBACKS) */
  278. /* Clear NAND interrupt FIFO empty pending bit */
  279. __FSMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FSMC_FLAG_FEMPT);
  280. }
  281. }
  282. /**
  283. * @brief NAND interrupt feature callback
  284. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  285. * the configuration information for NAND module.
  286. * @retval None
  287. */
  288. __weak void HAL_NAND_ITCallback(NAND_HandleTypeDef *hnand)
  289. {
  290. /* Prevent unused argument(s) compilation warning */
  291. UNUSED(hnand);
  292. /* NOTE : This function Should not be modified, when the callback is needed,
  293. the HAL_NAND_ITCallback could be implemented in the user file
  294. */
  295. }
  296. /**
  297. * @}
  298. */
  299. /** @defgroup NAND_Exported_Functions_Group2 Input and Output functions
  300. * @brief Input Output and memory control functions
  301. *
  302. @verbatim
  303. ==============================================================================
  304. ##### NAND Input and Output functions #####
  305. ==============================================================================
  306. [..]
  307. This section provides functions allowing to use and control the NAND
  308. memory
  309. @endverbatim
  310. * @{
  311. */
  312. /**
  313. * @brief Read the NAND memory electronic signature
  314. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  315. * the configuration information for NAND module.
  316. * @param pNAND_ID NAND ID structure
  317. * @retval HAL status
  318. */
  319. HAL_StatusTypeDef HAL_NAND_Read_ID(NAND_HandleTypeDef *hnand, NAND_IDTypeDef *pNAND_ID)
  320. {
  321. __IO uint32_t data = 0;
  322. __IO uint32_t data1 = 0;
  323. uint32_t deviceaddress;
  324. /* Check the NAND controller state */
  325. if (hnand->State == HAL_NAND_STATE_BUSY)
  326. {
  327. return HAL_BUSY;
  328. }
  329. else if (hnand->State == HAL_NAND_STATE_READY)
  330. {
  331. /* Process Locked */
  332. __HAL_LOCK(hnand);
  333. /* Update the NAND controller state */
  334. hnand->State = HAL_NAND_STATE_BUSY;
  335. /* Identify the device address */
  336. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  337. {
  338. deviceaddress = NAND_DEVICE1;
  339. }
  340. else
  341. {
  342. deviceaddress = NAND_DEVICE2;
  343. }
  344. /* Send Read ID command sequence */
  345. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_READID;
  346. __DSB();
  347. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  348. __DSB();
  349. /* Read the electronic signature from NAND flash */
  350. if (hnand->Init.MemoryDataWidth == FSMC_NAND_PCC_MEM_BUS_WIDTH_8)
  351. {
  352. data = *(__IO uint32_t *)deviceaddress;
  353. /* Return the data read */
  354. pNAND_ID->Maker_Id = ADDR_1ST_CYCLE(data);
  355. pNAND_ID->Device_Id = ADDR_2ND_CYCLE(data);
  356. pNAND_ID->Third_Id = ADDR_3RD_CYCLE(data);
  357. pNAND_ID->Fourth_Id = ADDR_4TH_CYCLE(data);
  358. }
  359. else
  360. {
  361. data = *(__IO uint32_t *)deviceaddress;
  362. data1 = *((__IO uint32_t *)deviceaddress + 4);
  363. /* Return the data read */
  364. pNAND_ID->Maker_Id = ADDR_1ST_CYCLE(data);
  365. pNAND_ID->Device_Id = ADDR_3RD_CYCLE(data);
  366. pNAND_ID->Third_Id = ADDR_1ST_CYCLE(data1);
  367. pNAND_ID->Fourth_Id = ADDR_3RD_CYCLE(data1);
  368. }
  369. /* Update the NAND controller state */
  370. hnand->State = HAL_NAND_STATE_READY;
  371. /* Process unlocked */
  372. __HAL_UNLOCK(hnand);
  373. }
  374. else
  375. {
  376. return HAL_ERROR;
  377. }
  378. return HAL_OK;
  379. }
  380. /**
  381. * @brief NAND memory reset
  382. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  383. * the configuration information for NAND module.
  384. * @retval HAL status
  385. */
  386. HAL_StatusTypeDef HAL_NAND_Reset(NAND_HandleTypeDef *hnand)
  387. {
  388. uint32_t deviceaddress;
  389. /* Check the NAND controller state */
  390. if (hnand->State == HAL_NAND_STATE_BUSY)
  391. {
  392. return HAL_BUSY;
  393. }
  394. else if (hnand->State == HAL_NAND_STATE_READY)
  395. {
  396. /* Process Locked */
  397. __HAL_LOCK(hnand);
  398. /* Update the NAND controller state */
  399. hnand->State = HAL_NAND_STATE_BUSY;
  400. /* Identify the device address */
  401. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  402. {
  403. deviceaddress = NAND_DEVICE1;
  404. }
  405. else
  406. {
  407. deviceaddress = NAND_DEVICE2;
  408. }
  409. /* Send NAND reset command */
  410. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = 0xFF;
  411. /* Update the NAND controller state */
  412. hnand->State = HAL_NAND_STATE_READY;
  413. /* Process unlocked */
  414. __HAL_UNLOCK(hnand);
  415. }
  416. else
  417. {
  418. return HAL_ERROR;
  419. }
  420. return HAL_OK;
  421. }
  422. /**
  423. * @brief Configure the device: Enter the physical parameters of the device
  424. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  425. * the configuration information for NAND module.
  426. * @param pDeviceConfig pointer to NAND_DeviceConfigTypeDef structure
  427. * @retval HAL status
  428. */
  429. HAL_StatusTypeDef HAL_NAND_ConfigDevice(NAND_HandleTypeDef *hnand, NAND_DeviceConfigTypeDef *pDeviceConfig)
  430. {
  431. hnand->Config.PageSize = pDeviceConfig->PageSize;
  432. hnand->Config.SpareAreaSize = pDeviceConfig->SpareAreaSize;
  433. hnand->Config.BlockSize = pDeviceConfig->BlockSize;
  434. hnand->Config.BlockNbr = pDeviceConfig->BlockNbr;
  435. hnand->Config.PlaneSize = pDeviceConfig->PlaneSize;
  436. hnand->Config.PlaneNbr = pDeviceConfig->PlaneNbr;
  437. hnand->Config.ExtraCommandEnable = pDeviceConfig->ExtraCommandEnable;
  438. return HAL_OK;
  439. }
  440. /**
  441. * @brief Read Page(s) from NAND memory block (8-bits addressing)
  442. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  443. * the configuration information for NAND module.
  444. * @param pAddress pointer to NAND address structure
  445. * @param pBuffer pointer to destination read buffer
  446. * @param NumPageToRead number of pages to read from block
  447. * @retval HAL status
  448. */
  449. HAL_StatusTypeDef HAL_NAND_Read_Page_8b(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress,
  450. uint8_t *pBuffer, uint32_t NumPageToRead)
  451. {
  452. uint32_t index;
  453. uint32_t tickstart;
  454. uint32_t deviceaddress;
  455. uint32_t numpagesread = 0U;
  456. uint32_t nandaddress;
  457. uint32_t nbpages = NumPageToRead;
  458. uint8_t *buff = pBuffer;
  459. /* Check the NAND controller state */
  460. if (hnand->State == HAL_NAND_STATE_BUSY)
  461. {
  462. return HAL_BUSY;
  463. }
  464. else if (hnand->State == HAL_NAND_STATE_READY)
  465. {
  466. /* Process Locked */
  467. __HAL_LOCK(hnand);
  468. /* Update the NAND controller state */
  469. hnand->State = HAL_NAND_STATE_BUSY;
  470. /* Identify the device address */
  471. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  472. {
  473. deviceaddress = NAND_DEVICE1;
  474. }
  475. else
  476. {
  477. deviceaddress = NAND_DEVICE2;
  478. }
  479. /* NAND raw address calculation */
  480. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  481. /* Page(s) read loop */
  482. while ((nbpages != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  483. {
  484. /* Send read page command sequence */
  485. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  486. __DSB();
  487. /* Cards with page size <= 512 bytes */
  488. if ((hnand->Config.PageSize) <= 512U)
  489. {
  490. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  491. {
  492. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  493. __DSB();
  494. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  495. __DSB();
  496. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  497. __DSB();
  498. }
  499. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  500. {
  501. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  502. __DSB();
  503. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  504. __DSB();
  505. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  506. __DSB();
  507. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  508. __DSB();
  509. }
  510. }
  511. else /* (hnand->Config.PageSize) > 512 */
  512. {
  513. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  514. {
  515. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  516. __DSB();
  517. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  518. __DSB();
  519. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  520. __DSB();
  521. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  522. __DSB();
  523. }
  524. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  525. {
  526. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  527. __DSB();
  528. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  529. __DSB();
  530. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  531. __DSB();
  532. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  533. __DSB();
  534. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  535. __DSB();
  536. }
  537. }
  538. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  539. __DSB();
  540. if (hnand->Config.ExtraCommandEnable == ENABLE)
  541. {
  542. /* Get tick */
  543. tickstart = HAL_GetTick();
  544. /* Read status until NAND is ready */
  545. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  546. {
  547. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  548. {
  549. /* Update the NAND controller state */
  550. hnand->State = HAL_NAND_STATE_ERROR;
  551. /* Process unlocked */
  552. __HAL_UNLOCK(hnand);
  553. return HAL_TIMEOUT;
  554. }
  555. }
  556. /* Go back to read mode */
  557. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  558. __DSB();
  559. }
  560. /* Get Data into Buffer */
  561. for (index = 0U; index < hnand->Config.PageSize; index++)
  562. {
  563. *buff = *(uint8_t *)deviceaddress;
  564. buff++;
  565. }
  566. /* Increment read pages number */
  567. numpagesread++;
  568. /* Decrement pages to read */
  569. nbpages--;
  570. /* Increment the NAND address */
  571. nandaddress = (uint32_t)(nandaddress + 1U);
  572. }
  573. /* Update the NAND controller state */
  574. hnand->State = HAL_NAND_STATE_READY;
  575. /* Process unlocked */
  576. __HAL_UNLOCK(hnand);
  577. }
  578. else
  579. {
  580. return HAL_ERROR;
  581. }
  582. return HAL_OK;
  583. }
  584. /**
  585. * @brief Read Page(s) from NAND memory block (16-bits addressing)
  586. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  587. * the configuration information for NAND module.
  588. * @param pAddress pointer to NAND address structure
  589. * @param pBuffer pointer to destination read buffer. pBuffer should be 16bits aligned
  590. * @param NumPageToRead number of pages to read from block
  591. * @retval HAL status
  592. */
  593. HAL_StatusTypeDef HAL_NAND_Read_Page_16b(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress,
  594. uint16_t *pBuffer, uint32_t NumPageToRead)
  595. {
  596. uint32_t index;
  597. uint32_t tickstart;
  598. uint32_t deviceaddress;
  599. uint32_t numpagesread = 0U;
  600. uint32_t nandaddress;
  601. uint32_t nbpages = NumPageToRead;
  602. uint16_t *buff = pBuffer;
  603. /* Check the NAND controller state */
  604. if (hnand->State == HAL_NAND_STATE_BUSY)
  605. {
  606. return HAL_BUSY;
  607. }
  608. else if (hnand->State == HAL_NAND_STATE_READY)
  609. {
  610. /* Process Locked */
  611. __HAL_LOCK(hnand);
  612. /* Update the NAND controller state */
  613. hnand->State = HAL_NAND_STATE_BUSY;
  614. /* Identify the device address */
  615. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  616. {
  617. deviceaddress = NAND_DEVICE1;
  618. }
  619. else
  620. {
  621. deviceaddress = NAND_DEVICE2;
  622. }
  623. /* NAND raw address calculation */
  624. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  625. /* Page(s) read loop */
  626. while ((nbpages != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  627. {
  628. /* Send read page command sequence */
  629. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  630. __DSB();
  631. /* Cards with page size <= 512 bytes */
  632. if ((hnand->Config.PageSize) <= 512U)
  633. {
  634. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  635. {
  636. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  637. __DSB();
  638. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  639. __DSB();
  640. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  641. __DSB();
  642. }
  643. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  644. {
  645. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  646. __DSB();
  647. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  648. __DSB();
  649. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  650. __DSB();
  651. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  652. __DSB();
  653. }
  654. }
  655. else /* (hnand->Config.PageSize) > 512 */
  656. {
  657. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  658. {
  659. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  660. __DSB();
  661. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  662. __DSB();
  663. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  664. __DSB();
  665. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  666. __DSB();
  667. }
  668. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  669. {
  670. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  671. __DSB();
  672. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  673. __DSB();
  674. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  675. __DSB();
  676. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  677. __DSB();
  678. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  679. __DSB();
  680. }
  681. }
  682. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  683. __DSB();
  684. if (hnand->Config.ExtraCommandEnable == ENABLE)
  685. {
  686. /* Get tick */
  687. tickstart = HAL_GetTick();
  688. /* Read status until NAND is ready */
  689. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  690. {
  691. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  692. {
  693. /* Update the NAND controller state */
  694. hnand->State = HAL_NAND_STATE_ERROR;
  695. /* Process unlocked */
  696. __HAL_UNLOCK(hnand);
  697. return HAL_TIMEOUT;
  698. }
  699. }
  700. /* Go back to read mode */
  701. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  702. __DSB();
  703. }
  704. /* Calculate PageSize */
  705. if (hnand->Init.MemoryDataWidth == FSMC_NAND_PCC_MEM_BUS_WIDTH_8)
  706. {
  707. hnand->Config.PageSize = hnand->Config.PageSize / 2U;
  708. }
  709. else
  710. {
  711. /* Do nothing */
  712. /* Keep the same PageSize for FMC_NAND_MEM_BUS_WIDTH_16*/
  713. }
  714. /* Get Data into Buffer */
  715. for (index = 0U; index < hnand->Config.PageSize; index++)
  716. {
  717. *buff = *(uint16_t *)deviceaddress;
  718. buff++;
  719. }
  720. /* Increment read pages number */
  721. numpagesread++;
  722. /* Decrement pages to read */
  723. nbpages--;
  724. /* Increment the NAND address */
  725. nandaddress = (uint32_t)(nandaddress + 1U);
  726. }
  727. /* Update the NAND controller state */
  728. hnand->State = HAL_NAND_STATE_READY;
  729. /* Process unlocked */
  730. __HAL_UNLOCK(hnand);
  731. }
  732. else
  733. {
  734. return HAL_ERROR;
  735. }
  736. return HAL_OK;
  737. }
  738. /**
  739. * @brief Write Page(s) to NAND memory block (8-bits addressing)
  740. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  741. * the configuration information for NAND module.
  742. * @param pAddress pointer to NAND address structure
  743. * @param pBuffer pointer to source buffer to write
  744. * @param NumPageToWrite number of pages to write to block
  745. * @retval HAL status
  746. */
  747. HAL_StatusTypeDef HAL_NAND_Write_Page_8b(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress,
  748. const uint8_t *pBuffer, uint32_t NumPageToWrite)
  749. {
  750. uint32_t index;
  751. uint32_t tickstart;
  752. uint32_t deviceaddress;
  753. uint32_t numpageswritten = 0U;
  754. uint32_t nandaddress;
  755. uint32_t nbpages = NumPageToWrite;
  756. const uint8_t *buff = pBuffer;
  757. /* Check the NAND controller state */
  758. if (hnand->State == HAL_NAND_STATE_BUSY)
  759. {
  760. return HAL_BUSY;
  761. }
  762. else if (hnand->State == HAL_NAND_STATE_READY)
  763. {
  764. /* Process Locked */
  765. __HAL_LOCK(hnand);
  766. /* Update the NAND controller state */
  767. hnand->State = HAL_NAND_STATE_BUSY;
  768. /* Identify the device address */
  769. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  770. {
  771. deviceaddress = NAND_DEVICE1;
  772. }
  773. else
  774. {
  775. deviceaddress = NAND_DEVICE2;
  776. }
  777. /* NAND raw address calculation */
  778. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  779. /* Page(s) write loop */
  780. while ((nbpages != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  781. {
  782. /* Send write page command sequence */
  783. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  784. __DSB();
  785. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  786. __DSB();
  787. /* Cards with page size <= 512 bytes */
  788. if ((hnand->Config.PageSize) <= 512U)
  789. {
  790. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  791. {
  792. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  793. __DSB();
  794. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  795. __DSB();
  796. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  797. __DSB();
  798. }
  799. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  800. {
  801. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  802. __DSB();
  803. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  804. __DSB();
  805. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  806. __DSB();
  807. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  808. __DSB();
  809. }
  810. }
  811. else /* (hnand->Config.PageSize) > 512 */
  812. {
  813. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  814. {
  815. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  816. __DSB();
  817. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  818. __DSB();
  819. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  820. __DSB();
  821. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  822. __DSB();
  823. }
  824. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  825. {
  826. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  827. __DSB();
  828. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  829. __DSB();
  830. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  831. __DSB();
  832. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  833. __DSB();
  834. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  835. __DSB();
  836. }
  837. }
  838. /* Write data to memory */
  839. for (index = 0U; index < hnand->Config.PageSize; index++)
  840. {
  841. *(__IO uint8_t *)deviceaddress = *buff;
  842. buff++;
  843. __DSB();
  844. }
  845. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  846. __DSB();
  847. /* Get tick */
  848. tickstart = HAL_GetTick();
  849. /* Read status until NAND is ready */
  850. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  851. {
  852. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  853. {
  854. /* Update the NAND controller state */
  855. hnand->State = HAL_NAND_STATE_ERROR;
  856. /* Process unlocked */
  857. __HAL_UNLOCK(hnand);
  858. return HAL_TIMEOUT;
  859. }
  860. }
  861. /* Increment written pages number */
  862. numpageswritten++;
  863. /* Decrement pages to write */
  864. nbpages--;
  865. /* Increment the NAND address */
  866. nandaddress = (uint32_t)(nandaddress + 1U);
  867. }
  868. /* Update the NAND controller state */
  869. hnand->State = HAL_NAND_STATE_READY;
  870. /* Process unlocked */
  871. __HAL_UNLOCK(hnand);
  872. }
  873. else
  874. {
  875. return HAL_ERROR;
  876. }
  877. return HAL_OK;
  878. }
  879. /**
  880. * @brief Write Page(s) to NAND memory block (16-bits addressing)
  881. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  882. * the configuration information for NAND module.
  883. * @param pAddress pointer to NAND address structure
  884. * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned
  885. * @param NumPageToWrite number of pages to write to block
  886. * @retval HAL status
  887. */
  888. HAL_StatusTypeDef HAL_NAND_Write_Page_16b(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress,
  889. const uint16_t *pBuffer, uint32_t NumPageToWrite)
  890. {
  891. uint32_t index;
  892. uint32_t tickstart;
  893. uint32_t deviceaddress;
  894. uint32_t numpageswritten = 0U;
  895. uint32_t nandaddress;
  896. uint32_t nbpages = NumPageToWrite;
  897. const uint16_t *buff = pBuffer;
  898. /* Check the NAND controller state */
  899. if (hnand->State == HAL_NAND_STATE_BUSY)
  900. {
  901. return HAL_BUSY;
  902. }
  903. else if (hnand->State == HAL_NAND_STATE_READY)
  904. {
  905. /* Process Locked */
  906. __HAL_LOCK(hnand);
  907. /* Update the NAND controller state */
  908. hnand->State = HAL_NAND_STATE_BUSY;
  909. /* Identify the device address */
  910. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  911. {
  912. deviceaddress = NAND_DEVICE1;
  913. }
  914. else
  915. {
  916. deviceaddress = NAND_DEVICE2;
  917. }
  918. /* NAND raw address calculation */
  919. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  920. /* Page(s) write loop */
  921. while ((nbpages != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  922. {
  923. /* Send write page command sequence */
  924. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  925. __DSB();
  926. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  927. __DSB();
  928. /* Cards with page size <= 512 bytes */
  929. if ((hnand->Config.PageSize) <= 512U)
  930. {
  931. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  932. {
  933. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  934. __DSB();
  935. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  936. __DSB();
  937. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  938. __DSB();
  939. }
  940. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  941. {
  942. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  943. __DSB();
  944. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  945. __DSB();
  946. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  947. __DSB();
  948. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  949. __DSB();
  950. }
  951. }
  952. else /* (hnand->Config.PageSize) > 512 */
  953. {
  954. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  955. {
  956. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  957. __DSB();
  958. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  959. __DSB();
  960. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  961. __DSB();
  962. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  963. __DSB();
  964. }
  965. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  966. {
  967. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  968. __DSB();
  969. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  970. __DSB();
  971. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  972. __DSB();
  973. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  974. __DSB();
  975. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  976. __DSB();
  977. }
  978. }
  979. /* Calculate PageSize */
  980. if (hnand->Init.MemoryDataWidth == FSMC_NAND_PCC_MEM_BUS_WIDTH_8)
  981. {
  982. hnand->Config.PageSize = hnand->Config.PageSize / 2U;
  983. }
  984. else
  985. {
  986. /* Do nothing */
  987. /* Keep the same PageSize for FMC_NAND_MEM_BUS_WIDTH_16*/
  988. }
  989. /* Write data to memory */
  990. for (index = 0U; index < hnand->Config.PageSize; index++)
  991. {
  992. *(__IO uint16_t *)deviceaddress = *buff;
  993. buff++;
  994. __DSB();
  995. }
  996. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  997. __DSB();
  998. /* Get tick */
  999. tickstart = HAL_GetTick();
  1000. /* Read status until NAND is ready */
  1001. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  1002. {
  1003. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  1004. {
  1005. /* Update the NAND controller state */
  1006. hnand->State = HAL_NAND_STATE_ERROR;
  1007. /* Process unlocked */
  1008. __HAL_UNLOCK(hnand);
  1009. return HAL_TIMEOUT;
  1010. }
  1011. }
  1012. /* Increment written pages number */
  1013. numpageswritten++;
  1014. /* Decrement pages to write */
  1015. nbpages--;
  1016. /* Increment the NAND address */
  1017. nandaddress = (uint32_t)(nandaddress + 1U);
  1018. }
  1019. /* Update the NAND controller state */
  1020. hnand->State = HAL_NAND_STATE_READY;
  1021. /* Process unlocked */
  1022. __HAL_UNLOCK(hnand);
  1023. }
  1024. else
  1025. {
  1026. return HAL_ERROR;
  1027. }
  1028. return HAL_OK;
  1029. }
  1030. /**
  1031. * @brief Read Spare area(s) from NAND memory (8-bits addressing)
  1032. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1033. * the configuration information for NAND module.
  1034. * @param pAddress pointer to NAND address structure
  1035. * @param pBuffer pointer to source buffer to write
  1036. * @param NumSpareAreaToRead Number of spare area to read
  1037. * @retval HAL status
  1038. */
  1039. HAL_StatusTypeDef HAL_NAND_Read_SpareArea_8b(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress,
  1040. uint8_t *pBuffer, uint32_t NumSpareAreaToRead)
  1041. {
  1042. uint32_t index;
  1043. uint32_t tickstart;
  1044. uint32_t deviceaddress;
  1045. uint32_t numsparearearead = 0U;
  1046. uint32_t nandaddress;
  1047. uint32_t columnaddress;
  1048. uint32_t nbspare = NumSpareAreaToRead;
  1049. uint8_t *buff = pBuffer;
  1050. /* Check the NAND controller state */
  1051. if (hnand->State == HAL_NAND_STATE_BUSY)
  1052. {
  1053. return HAL_BUSY;
  1054. }
  1055. else if (hnand->State == HAL_NAND_STATE_READY)
  1056. {
  1057. /* Process Locked */
  1058. __HAL_LOCK(hnand);
  1059. /* Update the NAND controller state */
  1060. hnand->State = HAL_NAND_STATE_BUSY;
  1061. /* Identify the device address */
  1062. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  1063. {
  1064. deviceaddress = NAND_DEVICE1;
  1065. }
  1066. else
  1067. {
  1068. deviceaddress = NAND_DEVICE2;
  1069. }
  1070. /* NAND raw address calculation */
  1071. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1072. /* Column in page address */
  1073. columnaddress = COLUMN_ADDRESS(hnand);
  1074. /* Spare area(s) read loop */
  1075. while ((nbspare != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1076. {
  1077. /* Cards with page size <= 512 bytes */
  1078. if ((hnand->Config.PageSize) <= 512U)
  1079. {
  1080. /* Send read spare area command sequence */
  1081. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1082. __DSB();
  1083. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1084. {
  1085. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1086. __DSB();
  1087. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1088. __DSB();
  1089. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1090. __DSB();
  1091. }
  1092. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1093. {
  1094. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1095. __DSB();
  1096. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1097. __DSB();
  1098. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1099. __DSB();
  1100. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1101. __DSB();
  1102. }
  1103. }
  1104. else /* (hnand->Config.PageSize) > 512 */
  1105. {
  1106. /* Send read spare area command sequence */
  1107. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1108. __DSB();
  1109. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1110. {
  1111. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1112. __DSB();
  1113. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1114. __DSB();
  1115. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1116. __DSB();
  1117. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1118. __DSB();
  1119. }
  1120. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1121. {
  1122. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1123. __DSB();
  1124. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1125. __DSB();
  1126. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1127. __DSB();
  1128. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1129. __DSB();
  1130. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1131. __DSB();
  1132. }
  1133. }
  1134. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  1135. __DSB();
  1136. if (hnand->Config.ExtraCommandEnable == ENABLE)
  1137. {
  1138. /* Get tick */
  1139. tickstart = HAL_GetTick();
  1140. /* Read status until NAND is ready */
  1141. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  1142. {
  1143. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  1144. {
  1145. /* Update the NAND controller state */
  1146. hnand->State = HAL_NAND_STATE_ERROR;
  1147. /* Process unlocked */
  1148. __HAL_UNLOCK(hnand);
  1149. return HAL_TIMEOUT;
  1150. }
  1151. }
  1152. /* Go back to read mode */
  1153. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  1154. __DSB();
  1155. }
  1156. /* Get Data into Buffer */
  1157. for (index = 0U; index < hnand->Config.SpareAreaSize; index++)
  1158. {
  1159. *buff = *(uint8_t *)deviceaddress;
  1160. buff++;
  1161. }
  1162. /* Increment read spare areas number */
  1163. numsparearearead++;
  1164. /* Decrement spare areas to read */
  1165. nbspare--;
  1166. /* Increment the NAND address */
  1167. nandaddress = (uint32_t)(nandaddress + 1U);
  1168. }
  1169. /* Update the NAND controller state */
  1170. hnand->State = HAL_NAND_STATE_READY;
  1171. /* Process unlocked */
  1172. __HAL_UNLOCK(hnand);
  1173. }
  1174. else
  1175. {
  1176. return HAL_ERROR;
  1177. }
  1178. return HAL_OK;
  1179. }
  1180. /**
  1181. * @brief Read Spare area(s) from NAND memory (16-bits addressing)
  1182. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1183. * the configuration information for NAND module.
  1184. * @param pAddress pointer to NAND address structure
  1185. * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned.
  1186. * @param NumSpareAreaToRead Number of spare area to read
  1187. * @retval HAL status
  1188. */
  1189. HAL_StatusTypeDef HAL_NAND_Read_SpareArea_16b(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress,
  1190. uint16_t *pBuffer, uint32_t NumSpareAreaToRead)
  1191. {
  1192. uint32_t index;
  1193. uint32_t tickstart;
  1194. uint32_t deviceaddress;
  1195. uint32_t numsparearearead = 0U;
  1196. uint32_t nandaddress;
  1197. uint32_t columnaddress;
  1198. uint32_t nbspare = NumSpareAreaToRead;
  1199. uint16_t *buff = pBuffer;
  1200. /* Check the NAND controller state */
  1201. if (hnand->State == HAL_NAND_STATE_BUSY)
  1202. {
  1203. return HAL_BUSY;
  1204. }
  1205. else if (hnand->State == HAL_NAND_STATE_READY)
  1206. {
  1207. /* Process Locked */
  1208. __HAL_LOCK(hnand);
  1209. /* Update the NAND controller state */
  1210. hnand->State = HAL_NAND_STATE_BUSY;
  1211. /* Identify the device address */
  1212. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  1213. {
  1214. deviceaddress = NAND_DEVICE1;
  1215. }
  1216. else
  1217. {
  1218. deviceaddress = NAND_DEVICE2;
  1219. }
  1220. /* NAND raw address calculation */
  1221. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1222. /* Column in page address */
  1223. columnaddress = (uint32_t)(COLUMN_ADDRESS(hnand));
  1224. /* Spare area(s) read loop */
  1225. while ((nbspare != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1226. {
  1227. /* Cards with page size <= 512 bytes */
  1228. if ((hnand->Config.PageSize) <= 512U)
  1229. {
  1230. /* Send read spare area command sequence */
  1231. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1232. __DSB();
  1233. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1234. {
  1235. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1236. __DSB();
  1237. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1238. __DSB();
  1239. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1240. __DSB();
  1241. }
  1242. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1243. {
  1244. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1245. __DSB();
  1246. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1247. __DSB();
  1248. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1249. __DSB();
  1250. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1251. __DSB();
  1252. }
  1253. }
  1254. else /* (hnand->Config.PageSize) > 512 */
  1255. {
  1256. /* Send read spare area command sequence */
  1257. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1258. __DSB();
  1259. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1260. {
  1261. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1262. __DSB();
  1263. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1264. __DSB();
  1265. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1266. __DSB();
  1267. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1268. __DSB();
  1269. }
  1270. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1271. {
  1272. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1273. __DSB();
  1274. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1275. __DSB();
  1276. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1277. __DSB();
  1278. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1279. __DSB();
  1280. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1281. __DSB();
  1282. }
  1283. }
  1284. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  1285. __DSB();
  1286. if (hnand->Config.ExtraCommandEnable == ENABLE)
  1287. {
  1288. /* Get tick */
  1289. tickstart = HAL_GetTick();
  1290. /* Read status until NAND is ready */
  1291. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  1292. {
  1293. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  1294. {
  1295. /* Update the NAND controller state */
  1296. hnand->State = HAL_NAND_STATE_ERROR;
  1297. /* Process unlocked */
  1298. __HAL_UNLOCK(hnand);
  1299. return HAL_TIMEOUT;
  1300. }
  1301. }
  1302. /* Go back to read mode */
  1303. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  1304. __DSB();
  1305. }
  1306. /* Get Data into Buffer */
  1307. for (index = 0U; index < hnand->Config.SpareAreaSize; index++)
  1308. {
  1309. *buff = *(uint16_t *)deviceaddress;
  1310. buff++;
  1311. }
  1312. /* Increment read spare areas number */
  1313. numsparearearead++;
  1314. /* Decrement spare areas to read */
  1315. nbspare--;
  1316. /* Increment the NAND address */
  1317. nandaddress = (uint32_t)(nandaddress + 1U);
  1318. }
  1319. /* Update the NAND controller state */
  1320. hnand->State = HAL_NAND_STATE_READY;
  1321. /* Process unlocked */
  1322. __HAL_UNLOCK(hnand);
  1323. }
  1324. else
  1325. {
  1326. return HAL_ERROR;
  1327. }
  1328. return HAL_OK;
  1329. }
  1330. /**
  1331. * @brief Write Spare area(s) to NAND memory (8-bits addressing)
  1332. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1333. * the configuration information for NAND module.
  1334. * @param pAddress pointer to NAND address structure
  1335. * @param pBuffer pointer to source buffer to write
  1336. * @param NumSpareAreaTowrite number of spare areas to write to block
  1337. * @retval HAL status
  1338. */
  1339. HAL_StatusTypeDef HAL_NAND_Write_SpareArea_8b(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress,
  1340. const uint8_t *pBuffer, uint32_t NumSpareAreaTowrite)
  1341. {
  1342. uint32_t index;
  1343. uint32_t tickstart;
  1344. uint32_t deviceaddress;
  1345. uint32_t numspareareawritten = 0U;
  1346. uint32_t nandaddress;
  1347. uint32_t columnaddress;
  1348. uint32_t nbspare = NumSpareAreaTowrite;
  1349. const uint8_t *buff = pBuffer;
  1350. /* Check the NAND controller state */
  1351. if (hnand->State == HAL_NAND_STATE_BUSY)
  1352. {
  1353. return HAL_BUSY;
  1354. }
  1355. else if (hnand->State == HAL_NAND_STATE_READY)
  1356. {
  1357. /* Process Locked */
  1358. __HAL_LOCK(hnand);
  1359. /* Update the NAND controller state */
  1360. hnand->State = HAL_NAND_STATE_BUSY;
  1361. /* Identify the device address */
  1362. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  1363. {
  1364. deviceaddress = NAND_DEVICE1;
  1365. }
  1366. else
  1367. {
  1368. deviceaddress = NAND_DEVICE2;
  1369. }
  1370. /* Page address calculation */
  1371. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1372. /* Column in page address */
  1373. columnaddress = COLUMN_ADDRESS(hnand);
  1374. /* Spare area(s) write loop */
  1375. while ((nbspare != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1376. {
  1377. /* Cards with page size <= 512 bytes */
  1378. if ((hnand->Config.PageSize) <= 512U)
  1379. {
  1380. /* Send write Spare area command sequence */
  1381. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1382. __DSB();
  1383. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1384. __DSB();
  1385. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1386. {
  1387. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1388. __DSB();
  1389. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1390. __DSB();
  1391. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1392. __DSB();
  1393. }
  1394. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1395. {
  1396. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1397. __DSB();
  1398. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1399. __DSB();
  1400. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1401. __DSB();
  1402. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1403. __DSB();
  1404. }
  1405. }
  1406. else /* (hnand->Config.PageSize) > 512 */
  1407. {
  1408. /* Send write Spare area command sequence */
  1409. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1410. __DSB();
  1411. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1412. __DSB();
  1413. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1414. {
  1415. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1416. __DSB();
  1417. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1418. __DSB();
  1419. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1420. __DSB();
  1421. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1422. __DSB();
  1423. }
  1424. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1425. {
  1426. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1427. __DSB();
  1428. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1429. __DSB();
  1430. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1431. __DSB();
  1432. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1433. __DSB();
  1434. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1435. __DSB();
  1436. }
  1437. }
  1438. /* Write data to memory */
  1439. for (index = 0U; index < hnand->Config.SpareAreaSize; index++)
  1440. {
  1441. *(__IO uint8_t *)deviceaddress = *buff;
  1442. buff++;
  1443. __DSB();
  1444. }
  1445. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  1446. __DSB();
  1447. /* Get tick */
  1448. tickstart = HAL_GetTick();
  1449. /* Read status until NAND is ready */
  1450. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  1451. {
  1452. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  1453. {
  1454. /* Update the NAND controller state */
  1455. hnand->State = HAL_NAND_STATE_ERROR;
  1456. /* Process unlocked */
  1457. __HAL_UNLOCK(hnand);
  1458. return HAL_TIMEOUT;
  1459. }
  1460. }
  1461. /* Increment written spare areas number */
  1462. numspareareawritten++;
  1463. /* Decrement spare areas to write */
  1464. nbspare--;
  1465. /* Increment the NAND address */
  1466. nandaddress = (uint32_t)(nandaddress + 1U);
  1467. }
  1468. /* Update the NAND controller state */
  1469. hnand->State = HAL_NAND_STATE_READY;
  1470. /* Process unlocked */
  1471. __HAL_UNLOCK(hnand);
  1472. }
  1473. else
  1474. {
  1475. return HAL_ERROR;
  1476. }
  1477. return HAL_OK;
  1478. }
  1479. /**
  1480. * @brief Write Spare area(s) to NAND memory (16-bits addressing)
  1481. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1482. * the configuration information for NAND module.
  1483. * @param pAddress pointer to NAND address structure
  1484. * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned.
  1485. * @param NumSpareAreaTowrite number of spare areas to write to block
  1486. * @retval HAL status
  1487. */
  1488. HAL_StatusTypeDef HAL_NAND_Write_SpareArea_16b(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress,
  1489. const uint16_t *pBuffer, uint32_t NumSpareAreaTowrite)
  1490. {
  1491. uint32_t index;
  1492. uint32_t tickstart;
  1493. uint32_t deviceaddress;
  1494. uint32_t numspareareawritten = 0U;
  1495. uint32_t nandaddress;
  1496. uint32_t columnaddress;
  1497. uint32_t nbspare = NumSpareAreaTowrite;
  1498. const uint16_t *buff = pBuffer;
  1499. /* Check the NAND controller state */
  1500. if (hnand->State == HAL_NAND_STATE_BUSY)
  1501. {
  1502. return HAL_BUSY;
  1503. }
  1504. else if (hnand->State == HAL_NAND_STATE_READY)
  1505. {
  1506. /* Process Locked */
  1507. __HAL_LOCK(hnand);
  1508. /* Update the NAND controller state */
  1509. hnand->State = HAL_NAND_STATE_BUSY;
  1510. /* Identify the device address */
  1511. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  1512. {
  1513. deviceaddress = NAND_DEVICE1;
  1514. }
  1515. else
  1516. {
  1517. deviceaddress = NAND_DEVICE2;
  1518. }
  1519. /* NAND raw address calculation */
  1520. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1521. /* Column in page address */
  1522. columnaddress = (uint32_t)(COLUMN_ADDRESS(hnand));
  1523. /* Spare area(s) write loop */
  1524. while ((nbspare != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1525. {
  1526. /* Cards with page size <= 512 bytes */
  1527. if ((hnand->Config.PageSize) <= 512U)
  1528. {
  1529. /* Send write Spare area command sequence */
  1530. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1531. __DSB();
  1532. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1533. __DSB();
  1534. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1535. {
  1536. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1537. __DSB();
  1538. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1539. __DSB();
  1540. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1541. __DSB();
  1542. }
  1543. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1544. {
  1545. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1546. __DSB();
  1547. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1548. __DSB();
  1549. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1550. __DSB();
  1551. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1552. __DSB();
  1553. }
  1554. }
  1555. else /* (hnand->Config.PageSize) > 512 */
  1556. {
  1557. /* Send write Spare area command sequence */
  1558. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1559. __DSB();
  1560. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1561. __DSB();
  1562. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1563. {
  1564. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1565. __DSB();
  1566. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1567. __DSB();
  1568. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1569. __DSB();
  1570. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1571. __DSB();
  1572. }
  1573. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1574. {
  1575. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1576. __DSB();
  1577. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1578. __DSB();
  1579. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1580. __DSB();
  1581. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1582. __DSB();
  1583. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1584. __DSB();
  1585. }
  1586. }
  1587. /* Write data to memory */
  1588. for (index = 0U; index < hnand->Config.SpareAreaSize; index++)
  1589. {
  1590. *(__IO uint16_t *)deviceaddress = *buff;
  1591. buff++;
  1592. __DSB();
  1593. }
  1594. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  1595. __DSB();
  1596. /* Get tick */
  1597. tickstart = HAL_GetTick();
  1598. /* Read status until NAND is ready */
  1599. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  1600. {
  1601. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  1602. {
  1603. /* Update the NAND controller state */
  1604. hnand->State = HAL_NAND_STATE_ERROR;
  1605. /* Process unlocked */
  1606. __HAL_UNLOCK(hnand);
  1607. return HAL_TIMEOUT;
  1608. }
  1609. }
  1610. /* Increment written spare areas number */
  1611. numspareareawritten++;
  1612. /* Decrement spare areas to write */
  1613. nbspare--;
  1614. /* Increment the NAND address */
  1615. nandaddress = (uint32_t)(nandaddress + 1U);
  1616. }
  1617. /* Update the NAND controller state */
  1618. hnand->State = HAL_NAND_STATE_READY;
  1619. /* Process unlocked */
  1620. __HAL_UNLOCK(hnand);
  1621. }
  1622. else
  1623. {
  1624. return HAL_ERROR;
  1625. }
  1626. return HAL_OK;
  1627. }
  1628. /**
  1629. * @brief NAND memory Block erase
  1630. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1631. * the configuration information for NAND module.
  1632. * @param pAddress pointer to NAND address structure
  1633. * @retval HAL status
  1634. */
  1635. HAL_StatusTypeDef HAL_NAND_Erase_Block(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress)
  1636. {
  1637. uint32_t deviceaddress;
  1638. /* Check the NAND controller state */
  1639. if (hnand->State == HAL_NAND_STATE_BUSY)
  1640. {
  1641. return HAL_BUSY;
  1642. }
  1643. else if (hnand->State == HAL_NAND_STATE_READY)
  1644. {
  1645. /* Process Locked */
  1646. __HAL_LOCK(hnand);
  1647. /* Update the NAND controller state */
  1648. hnand->State = HAL_NAND_STATE_BUSY;
  1649. /* Identify the device address */
  1650. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  1651. {
  1652. deviceaddress = NAND_DEVICE1;
  1653. }
  1654. else
  1655. {
  1656. deviceaddress = NAND_DEVICE2;
  1657. }
  1658. /* Send Erase block command sequence */
  1659. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_ERASE0;
  1660. __DSB();
  1661. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1662. __DSB();
  1663. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1664. __DSB();
  1665. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1666. __DSB();
  1667. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_ERASE1;
  1668. __DSB();
  1669. /* Update the NAND controller state */
  1670. hnand->State = HAL_NAND_STATE_READY;
  1671. /* Process unlocked */
  1672. __HAL_UNLOCK(hnand);
  1673. }
  1674. else
  1675. {
  1676. return HAL_ERROR;
  1677. }
  1678. return HAL_OK;
  1679. }
  1680. /**
  1681. * @brief Increment the NAND memory address
  1682. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1683. * the configuration information for NAND module.
  1684. * @param pAddress pointer to NAND address structure
  1685. * @retval The new status of the increment address operation. It can be:
  1686. * - NAND_VALID_ADDRESS: When the new address is valid address
  1687. * - NAND_INVALID_ADDRESS: When the new address is invalid address
  1688. */
  1689. uint32_t HAL_NAND_Address_Inc(const NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress)
  1690. {
  1691. uint32_t status = NAND_VALID_ADDRESS;
  1692. /* Increment page address */
  1693. pAddress->Page++;
  1694. /* Check NAND address is valid */
  1695. if (pAddress->Page == hnand->Config.BlockSize)
  1696. {
  1697. pAddress->Page = 0;
  1698. pAddress->Block++;
  1699. if (pAddress->Block == hnand->Config.PlaneSize)
  1700. {
  1701. pAddress->Block = 0;
  1702. pAddress->Plane++;
  1703. if (pAddress->Plane == (hnand->Config.PlaneNbr))
  1704. {
  1705. status = NAND_INVALID_ADDRESS;
  1706. }
  1707. }
  1708. }
  1709. return (status);
  1710. }
  1711. #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
  1712. /**
  1713. * @brief Register a User NAND Callback
  1714. * To be used to override the weak predefined callback
  1715. * @param hnand : NAND handle
  1716. * @param CallbackId : ID of the callback to be registered
  1717. * This parameter can be one of the following values:
  1718. * @arg @ref HAL_NAND_MSP_INIT_CB_ID NAND MspInit callback ID
  1719. * @arg @ref HAL_NAND_MSP_DEINIT_CB_ID NAND MspDeInit callback ID
  1720. * @arg @ref HAL_NAND_IT_CB_ID NAND IT callback ID
  1721. * @param pCallback : pointer to the Callback function
  1722. * @retval status
  1723. */
  1724. HAL_StatusTypeDef HAL_NAND_RegisterCallback(NAND_HandleTypeDef *hnand, HAL_NAND_CallbackIDTypeDef CallbackId,
  1725. pNAND_CallbackTypeDef pCallback)
  1726. {
  1727. HAL_StatusTypeDef status = HAL_OK;
  1728. if (pCallback == NULL)
  1729. {
  1730. return HAL_ERROR;
  1731. }
  1732. if (hnand->State == HAL_NAND_STATE_READY)
  1733. {
  1734. switch (CallbackId)
  1735. {
  1736. case HAL_NAND_MSP_INIT_CB_ID :
  1737. hnand->MspInitCallback = pCallback;
  1738. break;
  1739. case HAL_NAND_MSP_DEINIT_CB_ID :
  1740. hnand->MspDeInitCallback = pCallback;
  1741. break;
  1742. case HAL_NAND_IT_CB_ID :
  1743. hnand->ItCallback = pCallback;
  1744. break;
  1745. default :
  1746. /* update return status */
  1747. status = HAL_ERROR;
  1748. break;
  1749. }
  1750. }
  1751. else if (hnand->State == HAL_NAND_STATE_RESET)
  1752. {
  1753. switch (CallbackId)
  1754. {
  1755. case HAL_NAND_MSP_INIT_CB_ID :
  1756. hnand->MspInitCallback = pCallback;
  1757. break;
  1758. case HAL_NAND_MSP_DEINIT_CB_ID :
  1759. hnand->MspDeInitCallback = pCallback;
  1760. break;
  1761. default :
  1762. /* update return status */
  1763. status = HAL_ERROR;
  1764. break;
  1765. }
  1766. }
  1767. else
  1768. {
  1769. /* update return status */
  1770. status = HAL_ERROR;
  1771. }
  1772. }
  1773. /**
  1774. * @brief Unregister a User NAND Callback
  1775. * NAND Callback is redirected to the weak predefined callback
  1776. * @param hnand : NAND handle
  1777. * @param CallbackId : ID of the callback to be unregistered
  1778. * This parameter can be one of the following values:
  1779. * @arg @ref HAL_NAND_MSP_INIT_CB_ID NAND MspInit callback ID
  1780. * @arg @ref HAL_NAND_MSP_DEINIT_CB_ID NAND MspDeInit callback ID
  1781. * @arg @ref HAL_NAND_IT_CB_ID NAND IT callback ID
  1782. * @retval status
  1783. */
  1784. HAL_StatusTypeDef HAL_NAND_UnRegisterCallback(NAND_HandleTypeDef *hnand, HAL_NAND_CallbackIDTypeDef CallbackId)
  1785. {
  1786. HAL_StatusTypeDef status = HAL_OK;
  1787. if (hnand->State == HAL_NAND_STATE_READY)
  1788. {
  1789. switch (CallbackId)
  1790. {
  1791. case HAL_NAND_MSP_INIT_CB_ID :
  1792. hnand->MspInitCallback = HAL_NAND_MspInit;
  1793. break;
  1794. case HAL_NAND_MSP_DEINIT_CB_ID :
  1795. hnand->MspDeInitCallback = HAL_NAND_MspDeInit;
  1796. break;
  1797. case HAL_NAND_IT_CB_ID :
  1798. hnand->ItCallback = HAL_NAND_ITCallback;
  1799. break;
  1800. default :
  1801. /* update return status */
  1802. status = HAL_ERROR;
  1803. break;
  1804. }
  1805. }
  1806. else if (hnand->State == HAL_NAND_STATE_RESET)
  1807. {
  1808. switch (CallbackId)
  1809. {
  1810. case HAL_NAND_MSP_INIT_CB_ID :
  1811. hnand->MspInitCallback = HAL_NAND_MspInit;
  1812. break;
  1813. case HAL_NAND_MSP_DEINIT_CB_ID :
  1814. hnand->MspDeInitCallback = HAL_NAND_MspDeInit;
  1815. break;
  1816. default :
  1817. /* update return status */
  1818. status = HAL_ERROR;
  1819. break;
  1820. }
  1821. }
  1822. else
  1823. {
  1824. /* update return status */
  1825. status = HAL_ERROR;
  1826. }
  1827. }
  1828. #endif /* USE_HAL_NAND_REGISTER_CALLBACKS */
  1829. /**
  1830. * @}
  1831. */
  1832. /** @defgroup NAND_Exported_Functions_Group3 Peripheral Control functions
  1833. * @brief management functions
  1834. *
  1835. @verbatim
  1836. ==============================================================================
  1837. ##### NAND Control functions #####
  1838. ==============================================================================
  1839. [..]
  1840. This subsection provides a set of functions allowing to control dynamically
  1841. the NAND interface.
  1842. @endverbatim
  1843. * @{
  1844. */
  1845. /**
  1846. * @brief Enables dynamically NAND ECC feature.
  1847. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1848. * the configuration information for NAND module.
  1849. * @retval HAL status
  1850. */
  1851. HAL_StatusTypeDef HAL_NAND_ECC_Enable(NAND_HandleTypeDef *hnand)
  1852. {
  1853. /* Check the NAND controller state */
  1854. if (hnand->State == HAL_NAND_STATE_BUSY)
  1855. {
  1856. return HAL_BUSY;
  1857. }
  1858. else if (hnand->State == HAL_NAND_STATE_READY)
  1859. {
  1860. /* Update the NAND state */
  1861. hnand->State = HAL_NAND_STATE_BUSY;
  1862. /* Enable ECC feature */
  1863. (void)FSMC_NAND_ECC_Enable(hnand->Instance, hnand->Init.NandBank);
  1864. /* Update the NAND state */
  1865. hnand->State = HAL_NAND_STATE_READY;
  1866. }
  1867. else
  1868. {
  1869. return HAL_ERROR;
  1870. }
  1871. return HAL_OK;
  1872. }
  1873. /**
  1874. * @brief Disables dynamically FSMC_NAND ECC feature.
  1875. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1876. * the configuration information for NAND module.
  1877. * @retval HAL status
  1878. */
  1879. HAL_StatusTypeDef HAL_NAND_ECC_Disable(NAND_HandleTypeDef *hnand)
  1880. {
  1881. /* Check the NAND controller state */
  1882. if (hnand->State == HAL_NAND_STATE_BUSY)
  1883. {
  1884. return HAL_BUSY;
  1885. }
  1886. else if (hnand->State == HAL_NAND_STATE_READY)
  1887. {
  1888. /* Update the NAND state */
  1889. hnand->State = HAL_NAND_STATE_BUSY;
  1890. /* Disable ECC feature */
  1891. (void)FSMC_NAND_ECC_Disable(hnand->Instance, hnand->Init.NandBank);
  1892. /* Update the NAND state */
  1893. hnand->State = HAL_NAND_STATE_READY;
  1894. }
  1895. else
  1896. {
  1897. return HAL_ERROR;
  1898. }
  1899. return HAL_OK;
  1900. }
  1901. /**
  1902. * @brief Disables dynamically NAND ECC feature.
  1903. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1904. * the configuration information for NAND module.
  1905. * @param ECCval pointer to ECC value
  1906. * @param Timeout maximum timeout to wait
  1907. * @retval HAL status
  1908. */
  1909. HAL_StatusTypeDef HAL_NAND_GetECC(NAND_HandleTypeDef *hnand, uint32_t *ECCval, uint32_t Timeout)
  1910. {
  1911. HAL_StatusTypeDef status;
  1912. /* Check the NAND controller state */
  1913. if (hnand->State == HAL_NAND_STATE_BUSY)
  1914. {
  1915. return HAL_BUSY;
  1916. }
  1917. else if (hnand->State == HAL_NAND_STATE_READY)
  1918. {
  1919. /* Update the NAND state */
  1920. hnand->State = HAL_NAND_STATE_BUSY;
  1921. /* Get NAND ECC value */
  1922. status = FSMC_NAND_GetECC(hnand->Instance, ECCval, hnand->Init.NandBank, Timeout);
  1923. /* Update the NAND state */
  1924. hnand->State = HAL_NAND_STATE_READY;
  1925. }
  1926. else
  1927. {
  1928. return HAL_ERROR;
  1929. }
  1930. return status;
  1931. }
  1932. /**
  1933. * @}
  1934. */
  1935. /** @defgroup NAND_Exported_Functions_Group4 Peripheral State functions
  1936. * @brief Peripheral State functions
  1937. *
  1938. @verbatim
  1939. ==============================================================================
  1940. ##### NAND State functions #####
  1941. ==============================================================================
  1942. [..]
  1943. This subsection permits to get in run-time the status of the NAND controller
  1944. and the data flow.
  1945. @endverbatim
  1946. * @{
  1947. */
  1948. /**
  1949. * @brief return the NAND state
  1950. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1951. * the configuration information for NAND module.
  1952. * @retval HAL state
  1953. */
  1954. HAL_NAND_StateTypeDef HAL_NAND_GetState(const NAND_HandleTypeDef *hnand)
  1955. {
  1956. return hnand->State;
  1957. }
  1958. /**
  1959. * @brief NAND memory read status
  1960. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1961. * the configuration information for NAND module.
  1962. * @retval NAND status
  1963. */
  1964. uint32_t HAL_NAND_Read_Status(const NAND_HandleTypeDef *hnand)
  1965. {
  1966. uint32_t data;
  1967. uint32_t deviceaddress;
  1968. UNUSED(hnand);
  1969. /* Identify the device address */
  1970. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  1971. {
  1972. deviceaddress = NAND_DEVICE1;
  1973. }
  1974. else
  1975. {
  1976. deviceaddress = NAND_DEVICE2;
  1977. }
  1978. /* Send Read status operation command */
  1979. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_STATUS;
  1980. /* Read status register data */
  1981. data = *(__IO uint8_t *)deviceaddress;
  1982. /* Return the status */
  1983. if ((data & NAND_ERROR) == NAND_ERROR)
  1984. {
  1985. return NAND_ERROR;
  1986. }
  1987. else if ((data & NAND_READY) == NAND_READY)
  1988. {
  1989. return NAND_READY;
  1990. }
  1991. else
  1992. {
  1993. return NAND_BUSY;
  1994. }
  1995. }
  1996. /**
  1997. * @}
  1998. */
  1999. /**
  2000. * @}
  2001. */
  2002. /**
  2003. * @}
  2004. */
  2005. #endif /* HAL_NAND_MODULE_ENABLED */
  2006. /**
  2007. * @}
  2008. */
  2009. #endif /* FSMC_BANK3 */