stm32f1xx_hal_nand.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313
  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 */