stm32f1xx_hal_nand.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306
  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. @verbatim
  10. ==============================================================================
  11. ##### How to use this driver #####
  12. ==============================================================================
  13. [..]
  14. This driver is a generic layered driver which contains a set of APIs used to
  15. control NAND flash memories. It uses the FSMC layer functions to interface
  16. with NAND devices. This driver is used as follows:
  17. (+) NAND flash memory configuration sequence using the function HAL_NAND_Init()
  18. with control and timing parameters for both common and attribute spaces.
  19. (+) Read NAND flash memory maker and device IDs using the function
  20. HAL_NAND_Read_ID(). The read information is stored in the NAND_ID_TypeDef
  21. structure declared by the function caller.
  22. (+) Access NAND flash memory by read/write operations using the functions
  23. HAL_NAND_Read_Page_8b()/HAL_NAND_Read_SpareArea_8b(),
  24. HAL_NAND_Write_Page_8b()/HAL_NAND_Write_SpareArea_8b(),
  25. HAL_NAND_Read_Page_16b()/HAL_NAND_Read_SpareArea_16b(),
  26. HAL_NAND_Write_Page_16b()/HAL_NAND_Write_SpareArea_16b()
  27. to read/write page(s)/spare area(s). These functions use specific device
  28. information (Block, page size..) predefined by the user in the NAND_DeviceConfigTypeDef
  29. structure. The read/write address information is contained by the Nand_Address_Typedef
  30. structure passed as parameter.
  31. (+) Perform NAND flash Reset chip operation using the function HAL_NAND_Reset().
  32. (+) Perform NAND flash erase block operation using the function HAL_NAND_Erase_Block().
  33. The erase block address information is contained in the Nand_Address_Typedef
  34. structure passed as parameter.
  35. (+) Read the NAND flash status operation using the function HAL_NAND_Read_Status().
  36. (+) You can also control the NAND device by calling the control APIs HAL_NAND_ECC_Enable()/
  37. HAL_NAND_ECC_Disable() to respectively enable/disable the ECC code correction
  38. feature or the function HAL_NAND_GetECC() to get the ECC correction code.
  39. (+) You can monitor the NAND device HAL state by calling the function
  40. HAL_NAND_GetState()
  41. [..]
  42. (@) This driver is a set of generic APIs which handle standard NAND flash operations.
  43. If a NAND flash device contains different operations and/or implementations,
  44. it should be implemented separately.
  45. *** Callback registration ***
  46. =============================================
  47. [..]
  48. The compilation define USE_HAL_NAND_REGISTER_CALLBACKS when set to 1
  49. allows the user to configure dynamically the driver callbacks.
  50. Use Functions @ref HAL_NAND_RegisterCallback() to register a user callback,
  51. it allows to register following callbacks:
  52. (+) MspInitCallback : NAND MspInit.
  53. (+) MspDeInitCallback : NAND MspDeInit.
  54. This function takes as parameters the HAL peripheral handle, the Callback ID
  55. and a pointer to the user callback function.
  56. Use function @ref HAL_NAND_UnRegisterCallback() to reset a callback to the default
  57. weak (surcharged) function. It allows to reset following callbacks:
  58. (+) MspInitCallback : NAND MspInit.
  59. (+) MspDeInitCallback : NAND MspDeInit.
  60. This function) takes as parameters the HAL peripheral handle and the Callback ID.
  61. By default, after the @ref HAL_NAND_Init and if the state is HAL_NAND_STATE_RESET
  62. all callbacks are reset to the corresponding legacy weak (surcharged) functions.
  63. Exception done for MspInit and MspDeInit callbacks that are respectively
  64. reset to the legacy weak (surcharged) functions in the @ref HAL_NAND_Init
  65. and @ref HAL_NAND_DeInit only when these callbacks are null (not registered beforehand).
  66. If not, MspInit or MspDeInit are not null, the @ref HAL_NAND_Init and @ref HAL_NAND_DeInit
  67. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  68. Callbacks can be registered/unregistered in READY state only.
  69. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  70. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  71. during the Init/DeInit.
  72. In that case first register the MspInit/MspDeInit user callbacks
  73. using @ref HAL_NAND_RegisterCallback before calling @ref HAL_NAND_DeInit
  74. or @ref HAL_NAND_Init function.
  75. When The compilation define USE_HAL_NAND_REGISTER_CALLBACKS is set to 0 or
  76. not defined, the callback registering feature is not available
  77. and weak (surcharged) callbacks are used.
  78. @endverbatim
  79. ******************************************************************************
  80. * @attention
  81. *
  82. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  83. * All rights reserved.</center></h2>
  84. *
  85. * This software component is licensed by ST under BSD 3-Clause license,
  86. * the "License"; You may not use this file except in compliance with the
  87. * License. You may obtain a copy of the License at:
  88. * opensource.org/licenses/BSD-3-Clause
  89. *
  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
  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
  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
  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
  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
  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
  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, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer,
  450. 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, NAND_AddressTypeDef *pAddress, uint16_t *pBuffer,
  594. uint32_t NumPageToRead)
  595. {
  596. uint32_t index;
  597. uint32_t tickstart;
  598. uint32_t deviceaddress;
  599. uint32_t numpagesread = 0;
  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. /* Get Data into Buffer */
  705. for (index = 0U; index < hnand->Config.PageSize; index++)
  706. {
  707. *buff = *(uint16_t *)deviceaddress;
  708. buff++;
  709. }
  710. /* Increment read pages number */
  711. numpagesread++;
  712. /* Decrement pages to read */
  713. nbpages--;
  714. /* Increment the NAND address */
  715. nandaddress = (uint32_t)(nandaddress + 1U);
  716. }
  717. /* Update the NAND controller state */
  718. hnand->State = HAL_NAND_STATE_READY;
  719. /* Process unlocked */
  720. __HAL_UNLOCK(hnand);
  721. }
  722. else
  723. {
  724. return HAL_ERROR;
  725. }
  726. return HAL_OK;
  727. }
  728. /**
  729. * @brief Write Page(s) to NAND memory block (8-bits addressing)
  730. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  731. * the configuration information for NAND module.
  732. * @param pAddress pointer to NAND address structure
  733. * @param pBuffer pointer to source buffer to write
  734. * @param NumPageToWrite number of pages to write to block
  735. * @retval HAL status
  736. */
  737. HAL_StatusTypeDef HAL_NAND_Write_Page_8b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer,
  738. uint32_t NumPageToWrite)
  739. {
  740. uint32_t index;
  741. uint32_t tickstart;
  742. uint32_t deviceaddress;
  743. uint32_t numpageswritten = 0;
  744. uint32_t nandaddress;
  745. uint32_t nbpages = NumPageToWrite;
  746. uint8_t *buff = pBuffer;
  747. /* Check the NAND controller state */
  748. if (hnand->State == HAL_NAND_STATE_BUSY)
  749. {
  750. return HAL_BUSY;
  751. }
  752. else if (hnand->State == HAL_NAND_STATE_READY)
  753. {
  754. /* Process Locked */
  755. __HAL_LOCK(hnand);
  756. /* Update the NAND controller state */
  757. hnand->State = HAL_NAND_STATE_BUSY;
  758. /* Identify the device address */
  759. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  760. {
  761. deviceaddress = NAND_DEVICE1;
  762. }
  763. else
  764. {
  765. deviceaddress = NAND_DEVICE2;
  766. }
  767. /* NAND raw address calculation */
  768. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  769. /* Page(s) write loop */
  770. while ((nbpages != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  771. {
  772. /* Send write page command sequence */
  773. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  774. __DSB();
  775. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  776. __DSB();
  777. /* Cards with page size <= 512 bytes */
  778. if ((hnand->Config.PageSize) <= 512U)
  779. {
  780. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  781. {
  782. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  783. __DSB();
  784. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  785. __DSB();
  786. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  787. __DSB();
  788. }
  789. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  790. {
  791. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  792. __DSB();
  793. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  794. __DSB();
  795. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  796. __DSB();
  797. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  798. __DSB();
  799. }
  800. }
  801. else /* (hnand->Config.PageSize) > 512 */
  802. {
  803. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  804. {
  805. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  806. __DSB();
  807. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  808. __DSB();
  809. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  810. __DSB();
  811. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  812. __DSB();
  813. }
  814. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  815. {
  816. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  817. __DSB();
  818. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  819. __DSB();
  820. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  821. __DSB();
  822. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  823. __DSB();
  824. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  825. __DSB();
  826. }
  827. }
  828. /* Write data to memory */
  829. for (index = 0U; index < hnand->Config.PageSize; index++)
  830. {
  831. *(__IO uint8_t *)deviceaddress = *buff;
  832. buff++;
  833. __DSB();
  834. }
  835. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  836. __DSB();
  837. /* Get tick */
  838. tickstart = HAL_GetTick();
  839. /* Read status until NAND is ready */
  840. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  841. {
  842. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  843. {
  844. /* Update the NAND controller state */
  845. hnand->State = HAL_NAND_STATE_ERROR;
  846. /* Process unlocked */
  847. __HAL_UNLOCK(hnand);
  848. return HAL_TIMEOUT;
  849. }
  850. }
  851. /* Increment written pages number */
  852. numpageswritten++;
  853. /* Decrement pages to write */
  854. nbpages--;
  855. /* Increment the NAND address */
  856. nandaddress = (uint32_t)(nandaddress + 1U);
  857. }
  858. /* Update the NAND controller state */
  859. hnand->State = HAL_NAND_STATE_READY;
  860. /* Process unlocked */
  861. __HAL_UNLOCK(hnand);
  862. }
  863. else
  864. {
  865. return HAL_ERROR;
  866. }
  867. return HAL_OK;
  868. }
  869. /**
  870. * @brief Write Page(s) to NAND memory block (16-bits addressing)
  871. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  872. * the configuration information for NAND module.
  873. * @param pAddress pointer to NAND address structure
  874. * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned
  875. * @param NumPageToWrite number of pages to write to block
  876. * @retval HAL status
  877. */
  878. HAL_StatusTypeDef HAL_NAND_Write_Page_16b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint16_t *pBuffer,
  879. uint32_t NumPageToWrite)
  880. {
  881. uint32_t index;
  882. uint32_t tickstart;
  883. uint32_t deviceaddress;
  884. uint32_t numpageswritten = 0;
  885. uint32_t nandaddress;
  886. uint32_t nbpages = NumPageToWrite;
  887. uint16_t *buff = pBuffer;
  888. /* Check the NAND controller state */
  889. if (hnand->State == HAL_NAND_STATE_BUSY)
  890. {
  891. return HAL_BUSY;
  892. }
  893. else if (hnand->State == HAL_NAND_STATE_READY)
  894. {
  895. /* Process Locked */
  896. __HAL_LOCK(hnand);
  897. /* Update the NAND controller state */
  898. hnand->State = HAL_NAND_STATE_BUSY;
  899. /* Identify the device address */
  900. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  901. {
  902. deviceaddress = NAND_DEVICE1;
  903. }
  904. else
  905. {
  906. deviceaddress = NAND_DEVICE2;
  907. }
  908. /* NAND raw address calculation */
  909. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  910. /* Page(s) write loop */
  911. while ((nbpages != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  912. {
  913. /* Send write page command sequence */
  914. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  915. __DSB();
  916. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  917. __DSB();
  918. /* Cards with page size <= 512 bytes */
  919. if ((hnand->Config.PageSize) <= 512U)
  920. {
  921. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  922. {
  923. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  924. __DSB();
  925. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  926. __DSB();
  927. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  928. __DSB();
  929. }
  930. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  931. {
  932. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  933. __DSB();
  934. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  935. __DSB();
  936. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  937. __DSB();
  938. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  939. __DSB();
  940. }
  941. }
  942. else /* (hnand->Config.PageSize) > 512 */
  943. {
  944. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  945. {
  946. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  947. __DSB();
  948. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  949. __DSB();
  950. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  951. __DSB();
  952. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  953. __DSB();
  954. }
  955. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  956. {
  957. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  958. __DSB();
  959. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  960. __DSB();
  961. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  962. __DSB();
  963. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  964. __DSB();
  965. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  966. __DSB();
  967. }
  968. }
  969. /* Write data to memory */
  970. for (index = 0U; index < hnand->Config.PageSize; index++)
  971. {
  972. *(__IO uint16_t *)deviceaddress = *buff;
  973. buff++;
  974. __DSB();
  975. }
  976. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  977. __DSB();
  978. /* Get tick */
  979. tickstart = HAL_GetTick();
  980. /* Read status until NAND is ready */
  981. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  982. {
  983. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  984. {
  985. /* Update the NAND controller state */
  986. hnand->State = HAL_NAND_STATE_ERROR;
  987. /* Process unlocked */
  988. __HAL_UNLOCK(hnand);
  989. return HAL_TIMEOUT;
  990. }
  991. }
  992. /* Increment written pages number */
  993. numpageswritten++;
  994. /* Decrement pages to write */
  995. nbpages--;
  996. /* Increment the NAND address */
  997. nandaddress = (uint32_t)(nandaddress + 1U);
  998. }
  999. /* Update the NAND controller state */
  1000. hnand->State = HAL_NAND_STATE_READY;
  1001. /* Process unlocked */
  1002. __HAL_UNLOCK(hnand);
  1003. }
  1004. else
  1005. {
  1006. return HAL_ERROR;
  1007. }
  1008. return HAL_OK;
  1009. }
  1010. /**
  1011. * @brief Read Spare area(s) from NAND memory (8-bits addressing)
  1012. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1013. * the configuration information for NAND module.
  1014. * @param pAddress pointer to NAND address structure
  1015. * @param pBuffer pointer to source buffer to write
  1016. * @param NumSpareAreaToRead Number of spare area to read
  1017. * @retval HAL status
  1018. */
  1019. HAL_StatusTypeDef HAL_NAND_Read_SpareArea_8b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer,
  1020. uint32_t NumSpareAreaToRead)
  1021. {
  1022. uint32_t index;
  1023. uint32_t tickstart;
  1024. uint32_t deviceaddress;
  1025. uint32_t numsparearearead = 0;
  1026. uint32_t nandaddress;
  1027. uint32_t columnaddress;
  1028. uint32_t nbspare = NumSpareAreaToRead;
  1029. uint8_t *buff = pBuffer;
  1030. /* Check the NAND controller state */
  1031. if (hnand->State == HAL_NAND_STATE_BUSY)
  1032. {
  1033. return HAL_BUSY;
  1034. }
  1035. else if (hnand->State == HAL_NAND_STATE_READY)
  1036. {
  1037. /* Process Locked */
  1038. __HAL_LOCK(hnand);
  1039. /* Update the NAND controller state */
  1040. hnand->State = HAL_NAND_STATE_BUSY;
  1041. /* Identify the device address */
  1042. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  1043. {
  1044. deviceaddress = NAND_DEVICE1;
  1045. }
  1046. else
  1047. {
  1048. deviceaddress = NAND_DEVICE2;
  1049. }
  1050. /* NAND raw address calculation */
  1051. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1052. /* Column in page address */
  1053. columnaddress = COLUMN_ADDRESS(hnand);
  1054. /* Spare area(s) read loop */
  1055. while ((nbspare != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1056. {
  1057. /* Cards with page size <= 512 bytes */
  1058. if ((hnand->Config.PageSize) <= 512U)
  1059. {
  1060. /* Send read spare area command sequence */
  1061. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1062. __DSB();
  1063. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1064. {
  1065. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1066. __DSB();
  1067. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1068. __DSB();
  1069. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1070. __DSB();
  1071. }
  1072. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1073. {
  1074. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1075. __DSB();
  1076. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1077. __DSB();
  1078. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1079. __DSB();
  1080. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1081. __DSB();
  1082. }
  1083. }
  1084. else /* (hnand->Config.PageSize) > 512 */
  1085. {
  1086. /* Send read spare area command sequence */
  1087. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1088. __DSB();
  1089. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1090. {
  1091. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1092. __DSB();
  1093. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1094. __DSB();
  1095. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1096. __DSB();
  1097. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1098. __DSB();
  1099. }
  1100. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1101. {
  1102. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1103. __DSB();
  1104. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1105. __DSB();
  1106. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1107. __DSB();
  1108. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1109. __DSB();
  1110. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1111. __DSB();
  1112. }
  1113. }
  1114. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  1115. __DSB();
  1116. if (hnand->Config.ExtraCommandEnable == ENABLE)
  1117. {
  1118. /* Get tick */
  1119. tickstart = HAL_GetTick();
  1120. /* Read status until NAND is ready */
  1121. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  1122. {
  1123. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  1124. {
  1125. /* Update the NAND controller state */
  1126. hnand->State = HAL_NAND_STATE_ERROR;
  1127. /* Process unlocked */
  1128. __HAL_UNLOCK(hnand);
  1129. return HAL_TIMEOUT;
  1130. }
  1131. }
  1132. /* Go back to read mode */
  1133. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  1134. __DSB();
  1135. }
  1136. /* Get Data into Buffer */
  1137. for (index = 0U; index < hnand->Config.SpareAreaSize; index++)
  1138. {
  1139. *buff = *(uint8_t *)deviceaddress;
  1140. buff++;
  1141. }
  1142. /* Increment read spare areas number */
  1143. numsparearearead++;
  1144. /* Decrement spare areas to read */
  1145. nbspare--;
  1146. /* Increment the NAND address */
  1147. nandaddress = (uint32_t)(nandaddress + 1U);
  1148. }
  1149. /* Update the NAND controller state */
  1150. hnand->State = HAL_NAND_STATE_READY;
  1151. /* Process unlocked */
  1152. __HAL_UNLOCK(hnand);
  1153. }
  1154. else
  1155. {
  1156. return HAL_ERROR;
  1157. }
  1158. return HAL_OK;
  1159. }
  1160. /**
  1161. * @brief Read Spare area(s) from NAND memory (16-bits addressing)
  1162. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1163. * the configuration information for NAND module.
  1164. * @param pAddress pointer to NAND address structure
  1165. * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned.
  1166. * @param NumSpareAreaToRead Number of spare area to read
  1167. * @retval HAL status
  1168. */
  1169. HAL_StatusTypeDef HAL_NAND_Read_SpareArea_16b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress,
  1170. uint16_t *pBuffer, uint32_t NumSpareAreaToRead)
  1171. {
  1172. uint32_t index;
  1173. uint32_t tickstart;
  1174. uint32_t deviceaddress;
  1175. uint32_t numsparearearead = 0;
  1176. uint32_t nandaddress;
  1177. uint32_t columnaddress;
  1178. uint32_t nbspare = NumSpareAreaToRead;
  1179. uint16_t *buff = pBuffer;
  1180. /* Check the NAND controller state */
  1181. if (hnand->State == HAL_NAND_STATE_BUSY)
  1182. {
  1183. return HAL_BUSY;
  1184. }
  1185. else if (hnand->State == HAL_NAND_STATE_READY)
  1186. {
  1187. /* Process Locked */
  1188. __HAL_LOCK(hnand);
  1189. /* Update the NAND controller state */
  1190. hnand->State = HAL_NAND_STATE_BUSY;
  1191. /* Identify the device address */
  1192. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  1193. {
  1194. deviceaddress = NAND_DEVICE1;
  1195. }
  1196. else
  1197. {
  1198. deviceaddress = NAND_DEVICE2;
  1199. }
  1200. /* NAND raw address calculation */
  1201. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1202. /* Column in page address */
  1203. columnaddress = (uint32_t)(COLUMN_ADDRESS(hnand) * 2U);
  1204. /* Spare area(s) read loop */
  1205. while ((nbspare != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1206. {
  1207. /* Cards with page size <= 512 bytes */
  1208. if ((hnand->Config.PageSize) <= 512U)
  1209. {
  1210. /* Send read spare area command sequence */
  1211. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1212. __DSB();
  1213. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1214. {
  1215. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1216. __DSB();
  1217. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1218. __DSB();
  1219. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1220. __DSB();
  1221. }
  1222. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1223. {
  1224. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1225. __DSB();
  1226. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1227. __DSB();
  1228. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1229. __DSB();
  1230. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1231. __DSB();
  1232. }
  1233. }
  1234. else /* (hnand->Config.PageSize) > 512 */
  1235. {
  1236. /* Send read spare area command sequence */
  1237. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1238. __DSB();
  1239. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1240. {
  1241. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1242. __DSB();
  1243. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1244. __DSB();
  1245. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1246. __DSB();
  1247. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1248. __DSB();
  1249. }
  1250. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1251. {
  1252. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1253. __DSB();
  1254. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1255. __DSB();
  1256. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1257. __DSB();
  1258. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1259. __DSB();
  1260. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1261. __DSB();
  1262. }
  1263. }
  1264. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  1265. __DSB();
  1266. if (hnand->Config.ExtraCommandEnable == ENABLE)
  1267. {
  1268. /* Get tick */
  1269. tickstart = HAL_GetTick();
  1270. /* Read status until NAND is ready */
  1271. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  1272. {
  1273. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  1274. {
  1275. /* Update the NAND controller state */
  1276. hnand->State = HAL_NAND_STATE_ERROR;
  1277. /* Process unlocked */
  1278. __HAL_UNLOCK(hnand);
  1279. return HAL_TIMEOUT;
  1280. }
  1281. }
  1282. /* Go back to read mode */
  1283. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  1284. __DSB();
  1285. }
  1286. /* Get Data into Buffer */
  1287. for (index = 0U; index < hnand->Config.SpareAreaSize; index++)
  1288. {
  1289. *buff = *(uint16_t *)deviceaddress;
  1290. buff++;
  1291. }
  1292. /* Increment read spare areas number */
  1293. numsparearearead++;
  1294. /* Decrement spare areas to read */
  1295. nbspare--;
  1296. /* Increment the NAND address */
  1297. nandaddress = (uint32_t)(nandaddress + 1U);
  1298. }
  1299. /* Update the NAND controller state */
  1300. hnand->State = HAL_NAND_STATE_READY;
  1301. /* Process unlocked */
  1302. __HAL_UNLOCK(hnand);
  1303. }
  1304. else
  1305. {
  1306. return HAL_ERROR;
  1307. }
  1308. return HAL_OK;
  1309. }
  1310. /**
  1311. * @brief Write Spare area(s) to NAND memory (8-bits addressing)
  1312. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1313. * the configuration information for NAND module.
  1314. * @param pAddress pointer to NAND address structure
  1315. * @param pBuffer pointer to source buffer to write
  1316. * @param NumSpareAreaTowrite number of spare areas to write to block
  1317. * @retval HAL status
  1318. */
  1319. HAL_StatusTypeDef HAL_NAND_Write_SpareArea_8b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress,
  1320. uint8_t *pBuffer, uint32_t NumSpareAreaTowrite)
  1321. {
  1322. uint32_t index;
  1323. uint32_t tickstart;
  1324. uint32_t deviceaddress;
  1325. uint32_t numspareareawritten = 0;
  1326. uint32_t nandaddress;
  1327. uint32_t columnaddress;
  1328. uint32_t nbspare = NumSpareAreaTowrite;
  1329. uint8_t *buff = pBuffer;
  1330. /* Check the NAND controller state */
  1331. if (hnand->State == HAL_NAND_STATE_BUSY)
  1332. {
  1333. return HAL_BUSY;
  1334. }
  1335. else if (hnand->State == HAL_NAND_STATE_READY)
  1336. {
  1337. /* Process Locked */
  1338. __HAL_LOCK(hnand);
  1339. /* Update the NAND controller state */
  1340. hnand->State = HAL_NAND_STATE_BUSY;
  1341. /* Identify the device address */
  1342. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  1343. {
  1344. deviceaddress = NAND_DEVICE1;
  1345. }
  1346. else
  1347. {
  1348. deviceaddress = NAND_DEVICE2;
  1349. }
  1350. /* Page address calculation */
  1351. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1352. /* Column in page address */
  1353. columnaddress = COLUMN_ADDRESS(hnand);
  1354. /* Spare area(s) write loop */
  1355. while ((nbspare != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1356. {
  1357. /* Cards with page size <= 512 bytes */
  1358. if ((hnand->Config.PageSize) <= 512U)
  1359. {
  1360. /* Send write Spare area command sequence */
  1361. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1362. __DSB();
  1363. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1364. __DSB();
  1365. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1366. {
  1367. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1368. __DSB();
  1369. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1370. __DSB();
  1371. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1372. __DSB();
  1373. }
  1374. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1375. {
  1376. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1377. __DSB();
  1378. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1379. __DSB();
  1380. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1381. __DSB();
  1382. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1383. __DSB();
  1384. }
  1385. }
  1386. else /* (hnand->Config.PageSize) > 512 */
  1387. {
  1388. /* Send write Spare area command sequence */
  1389. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1390. __DSB();
  1391. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1392. __DSB();
  1393. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1394. {
  1395. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1396. __DSB();
  1397. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1398. __DSB();
  1399. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1400. __DSB();
  1401. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1402. __DSB();
  1403. }
  1404. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1405. {
  1406. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1407. __DSB();
  1408. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1409. __DSB();
  1410. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1411. __DSB();
  1412. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1413. __DSB();
  1414. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1415. __DSB();
  1416. }
  1417. }
  1418. /* Write data to memory */
  1419. for (index = 0U; index < hnand->Config.SpareAreaSize; index++)
  1420. {
  1421. *(__IO uint8_t *)deviceaddress = *buff;
  1422. buff++;
  1423. __DSB();
  1424. }
  1425. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  1426. __DSB();
  1427. /* Get tick */
  1428. tickstart = HAL_GetTick();
  1429. /* Read status until NAND is ready */
  1430. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  1431. {
  1432. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  1433. {
  1434. /* Update the NAND controller state */
  1435. hnand->State = HAL_NAND_STATE_ERROR;
  1436. /* Process unlocked */
  1437. __HAL_UNLOCK(hnand);
  1438. return HAL_TIMEOUT;
  1439. }
  1440. }
  1441. /* Increment written spare areas number */
  1442. numspareareawritten++;
  1443. /* Decrement spare areas to write */
  1444. nbspare--;
  1445. /* Increment the NAND address */
  1446. nandaddress = (uint32_t)(nandaddress + 1U);
  1447. }
  1448. /* Update the NAND controller state */
  1449. hnand->State = HAL_NAND_STATE_READY;
  1450. /* Process unlocked */
  1451. __HAL_UNLOCK(hnand);
  1452. }
  1453. else
  1454. {
  1455. return HAL_ERROR;
  1456. }
  1457. return HAL_OK;
  1458. }
  1459. /**
  1460. * @brief Write Spare area(s) to NAND memory (16-bits addressing)
  1461. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1462. * the configuration information for NAND module.
  1463. * @param pAddress pointer to NAND address structure
  1464. * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned.
  1465. * @param NumSpareAreaTowrite number of spare areas to write to block
  1466. * @retval HAL status
  1467. */
  1468. HAL_StatusTypeDef HAL_NAND_Write_SpareArea_16b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress,
  1469. uint16_t *pBuffer, uint32_t NumSpareAreaTowrite)
  1470. {
  1471. uint32_t index;
  1472. uint32_t tickstart;
  1473. uint32_t deviceaddress;
  1474. uint32_t numspareareawritten = 0;
  1475. uint32_t nandaddress;
  1476. uint32_t columnaddress;
  1477. uint32_t nbspare = NumSpareAreaTowrite;
  1478. uint16_t *buff = pBuffer;
  1479. /* Check the NAND controller state */
  1480. if (hnand->State == HAL_NAND_STATE_BUSY)
  1481. {
  1482. return HAL_BUSY;
  1483. }
  1484. else if (hnand->State == HAL_NAND_STATE_READY)
  1485. {
  1486. /* Process Locked */
  1487. __HAL_LOCK(hnand);
  1488. /* Update the NAND controller state */
  1489. hnand->State = HAL_NAND_STATE_BUSY;
  1490. /* Identify the device address */
  1491. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  1492. {
  1493. deviceaddress = NAND_DEVICE1;
  1494. }
  1495. else
  1496. {
  1497. deviceaddress = NAND_DEVICE2;
  1498. }
  1499. /* NAND raw address calculation */
  1500. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1501. /* Column in page address */
  1502. columnaddress = (uint32_t)(COLUMN_ADDRESS(hnand) * 2U);
  1503. /* Spare area(s) write loop */
  1504. while ((nbspare != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1505. {
  1506. /* Cards with page size <= 512 bytes */
  1507. if ((hnand->Config.PageSize) <= 512U)
  1508. {
  1509. /* Send write Spare area command sequence */
  1510. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1511. __DSB();
  1512. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1513. __DSB();
  1514. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1515. {
  1516. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1517. __DSB();
  1518. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1519. __DSB();
  1520. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1521. __DSB();
  1522. }
  1523. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1524. {
  1525. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1526. __DSB();
  1527. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1528. __DSB();
  1529. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1530. __DSB();
  1531. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1532. __DSB();
  1533. }
  1534. }
  1535. else /* (hnand->Config.PageSize) > 512 */
  1536. {
  1537. /* Send write Spare area command sequence */
  1538. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1539. __DSB();
  1540. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1541. __DSB();
  1542. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1543. {
  1544. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1545. __DSB();
  1546. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1547. __DSB();
  1548. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1549. __DSB();
  1550. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1551. __DSB();
  1552. }
  1553. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1554. {
  1555. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1556. __DSB();
  1557. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1558. __DSB();
  1559. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1560. __DSB();
  1561. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1562. __DSB();
  1563. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1564. __DSB();
  1565. }
  1566. }
  1567. /* Write data to memory */
  1568. for (index = 0U; index < hnand->Config.SpareAreaSize; index++)
  1569. {
  1570. *(__IO uint16_t *)deviceaddress = *buff;
  1571. buff++;
  1572. __DSB();
  1573. }
  1574. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  1575. __DSB();
  1576. /* Get tick */
  1577. tickstart = HAL_GetTick();
  1578. /* Read status until NAND is ready */
  1579. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  1580. {
  1581. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  1582. {
  1583. /* Update the NAND controller state */
  1584. hnand->State = HAL_NAND_STATE_ERROR;
  1585. /* Process unlocked */
  1586. __HAL_UNLOCK(hnand);
  1587. return HAL_TIMEOUT;
  1588. }
  1589. }
  1590. /* Increment written spare areas number */
  1591. numspareareawritten++;
  1592. /* Decrement spare areas to write */
  1593. nbspare--;
  1594. /* Increment the NAND address */
  1595. nandaddress = (uint32_t)(nandaddress + 1U);
  1596. }
  1597. /* Update the NAND controller state */
  1598. hnand->State = HAL_NAND_STATE_READY;
  1599. /* Process unlocked */
  1600. __HAL_UNLOCK(hnand);
  1601. }
  1602. else
  1603. {
  1604. return HAL_ERROR;
  1605. }
  1606. return HAL_OK;
  1607. }
  1608. /**
  1609. * @brief NAND memory Block erase
  1610. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1611. * the configuration information for NAND module.
  1612. * @param pAddress pointer to NAND address structure
  1613. * @retval HAL status
  1614. */
  1615. HAL_StatusTypeDef HAL_NAND_Erase_Block(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress)
  1616. {
  1617. uint32_t deviceaddress;
  1618. /* Check the NAND controller state */
  1619. if (hnand->State == HAL_NAND_STATE_BUSY)
  1620. {
  1621. return HAL_BUSY;
  1622. }
  1623. else if (hnand->State == HAL_NAND_STATE_READY)
  1624. {
  1625. /* Process Locked */
  1626. __HAL_LOCK(hnand);
  1627. /* Update the NAND controller state */
  1628. hnand->State = HAL_NAND_STATE_BUSY;
  1629. /* Identify the device address */
  1630. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  1631. {
  1632. deviceaddress = NAND_DEVICE1;
  1633. }
  1634. else
  1635. {
  1636. deviceaddress = NAND_DEVICE2;
  1637. }
  1638. /* Send Erase block command sequence */
  1639. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_ERASE0;
  1640. __DSB();
  1641. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1642. __DSB();
  1643. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1644. __DSB();
  1645. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1646. __DSB();
  1647. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_ERASE1;
  1648. __DSB();
  1649. /* Update the NAND controller state */
  1650. hnand->State = HAL_NAND_STATE_READY;
  1651. /* Process unlocked */
  1652. __HAL_UNLOCK(hnand);
  1653. }
  1654. else
  1655. {
  1656. return HAL_ERROR;
  1657. }
  1658. return HAL_OK;
  1659. }
  1660. /**
  1661. * @brief Increment the NAND memory address
  1662. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1663. * the configuration information for NAND module.
  1664. * @param pAddress pointer to NAND address structure
  1665. * @retval The new status of the increment address operation. It can be:
  1666. * - NAND_VALID_ADDRESS: When the new address is valid address
  1667. * - NAND_INVALID_ADDRESS: When the new address is invalid address
  1668. */
  1669. uint32_t HAL_NAND_Address_Inc(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress)
  1670. {
  1671. uint32_t status = NAND_VALID_ADDRESS;
  1672. /* Increment page address */
  1673. pAddress->Page++;
  1674. /* Check NAND address is valid */
  1675. if (pAddress->Page == hnand->Config.BlockSize)
  1676. {
  1677. pAddress->Page = 0;
  1678. pAddress->Block++;
  1679. if (pAddress->Block == hnand->Config.PlaneSize)
  1680. {
  1681. pAddress->Block = 0;
  1682. pAddress->Plane++;
  1683. if (pAddress->Plane == (hnand->Config.PlaneNbr))
  1684. {
  1685. status = NAND_INVALID_ADDRESS;
  1686. }
  1687. }
  1688. }
  1689. return (status);
  1690. }
  1691. #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
  1692. /**
  1693. * @brief Register a User NAND Callback
  1694. * To be used instead of the weak (surcharged) predefined callback
  1695. * @param hnand : NAND handle
  1696. * @param CallbackId : ID of the callback to be registered
  1697. * This parameter can be one of the following values:
  1698. * @arg @ref HAL_NAND_MSP_INIT_CB_ID NAND MspInit callback ID
  1699. * @arg @ref HAL_NAND_MSP_DEINIT_CB_ID NAND MspDeInit callback ID
  1700. * @arg @ref HAL_NAND_IT_CB_ID NAND IT callback ID
  1701. * @param pCallback : pointer to the Callback function
  1702. * @retval status
  1703. */
  1704. HAL_StatusTypeDef HAL_NAND_RegisterCallback(NAND_HandleTypeDef *hnand, HAL_NAND_CallbackIDTypeDef CallbackId,
  1705. pNAND_CallbackTypeDef pCallback)
  1706. {
  1707. HAL_StatusTypeDef status = HAL_OK;
  1708. if (pCallback == NULL)
  1709. {
  1710. return HAL_ERROR;
  1711. }
  1712. /* Process locked */
  1713. __HAL_LOCK(hnand);
  1714. if (hnand->State == HAL_NAND_STATE_READY)
  1715. {
  1716. switch (CallbackId)
  1717. {
  1718. case HAL_NAND_MSP_INIT_CB_ID :
  1719. hnand->MspInitCallback = pCallback;
  1720. break;
  1721. case HAL_NAND_MSP_DEINIT_CB_ID :
  1722. hnand->MspDeInitCallback = pCallback;
  1723. break;
  1724. case HAL_NAND_IT_CB_ID :
  1725. hnand->ItCallback = pCallback;
  1726. break;
  1727. default :
  1728. /* update return status */
  1729. status = HAL_ERROR;
  1730. break;
  1731. }
  1732. }
  1733. else if (hnand->State == HAL_NAND_STATE_RESET)
  1734. {
  1735. switch (CallbackId)
  1736. {
  1737. case HAL_NAND_MSP_INIT_CB_ID :
  1738. hnand->MspInitCallback = pCallback;
  1739. break;
  1740. case HAL_NAND_MSP_DEINIT_CB_ID :
  1741. hnand->MspDeInitCallback = pCallback;
  1742. break;
  1743. default :
  1744. /* update return status */
  1745. status = HAL_ERROR;
  1746. break;
  1747. }
  1748. }
  1749. else
  1750. {
  1751. /* update return status */
  1752. status = HAL_ERROR;
  1753. }
  1754. /* Release Lock */
  1755. __HAL_UNLOCK(hnand);
  1756. return status;
  1757. }
  1758. /**
  1759. * @brief Unregister a User NAND Callback
  1760. * NAND Callback is redirected to the weak (surcharged) predefined callback
  1761. * @param hnand : NAND handle
  1762. * @param CallbackId : ID of the callback to be unregistered
  1763. * This parameter can be one of the following values:
  1764. * @arg @ref HAL_NAND_MSP_INIT_CB_ID NAND MspInit callback ID
  1765. * @arg @ref HAL_NAND_MSP_DEINIT_CB_ID NAND MspDeInit callback ID
  1766. * @arg @ref HAL_NAND_IT_CB_ID NAND IT callback ID
  1767. * @retval status
  1768. */
  1769. HAL_StatusTypeDef HAL_NAND_UnRegisterCallback(NAND_HandleTypeDef *hnand, HAL_NAND_CallbackIDTypeDef CallbackId)
  1770. {
  1771. HAL_StatusTypeDef status = HAL_OK;
  1772. /* Process locked */
  1773. __HAL_LOCK(hnand);
  1774. if (hnand->State == HAL_NAND_STATE_READY)
  1775. {
  1776. switch (CallbackId)
  1777. {
  1778. case HAL_NAND_MSP_INIT_CB_ID :
  1779. hnand->MspInitCallback = HAL_NAND_MspInit;
  1780. break;
  1781. case HAL_NAND_MSP_DEINIT_CB_ID :
  1782. hnand->MspDeInitCallback = HAL_NAND_MspDeInit;
  1783. break;
  1784. case HAL_NAND_IT_CB_ID :
  1785. hnand->ItCallback = HAL_NAND_ITCallback;
  1786. break;
  1787. default :
  1788. /* update return status */
  1789. status = HAL_ERROR;
  1790. break;
  1791. }
  1792. }
  1793. else if (hnand->State == HAL_NAND_STATE_RESET)
  1794. {
  1795. switch (CallbackId)
  1796. {
  1797. case HAL_NAND_MSP_INIT_CB_ID :
  1798. hnand->MspInitCallback = HAL_NAND_MspInit;
  1799. break;
  1800. case HAL_NAND_MSP_DEINIT_CB_ID :
  1801. hnand->MspDeInitCallback = HAL_NAND_MspDeInit;
  1802. break;
  1803. default :
  1804. /* update return status */
  1805. status = HAL_ERROR;
  1806. break;
  1807. }
  1808. }
  1809. else
  1810. {
  1811. /* update return status */
  1812. status = HAL_ERROR;
  1813. }
  1814. /* Release Lock */
  1815. __HAL_UNLOCK(hnand);
  1816. return status;
  1817. }
  1818. #endif
  1819. /**
  1820. * @}
  1821. */
  1822. /** @defgroup NAND_Exported_Functions_Group3 Peripheral Control functions
  1823. * @brief management functions
  1824. *
  1825. @verbatim
  1826. ==============================================================================
  1827. ##### NAND Control functions #####
  1828. ==============================================================================
  1829. [..]
  1830. This subsection provides a set of functions allowing to control dynamically
  1831. the NAND interface.
  1832. @endverbatim
  1833. * @{
  1834. */
  1835. /**
  1836. * @brief Enables dynamically NAND ECC feature.
  1837. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1838. * the configuration information for NAND module.
  1839. * @retval HAL status
  1840. */
  1841. HAL_StatusTypeDef HAL_NAND_ECC_Enable(NAND_HandleTypeDef *hnand)
  1842. {
  1843. /* Check the NAND controller state */
  1844. if (hnand->State == HAL_NAND_STATE_BUSY)
  1845. {
  1846. return HAL_BUSY;
  1847. }
  1848. else if (hnand->State == HAL_NAND_STATE_READY)
  1849. {
  1850. /* Update the NAND state */
  1851. hnand->State = HAL_NAND_STATE_BUSY;
  1852. /* Enable ECC feature */
  1853. (void)FSMC_NAND_ECC_Enable(hnand->Instance, hnand->Init.NandBank);
  1854. /* Update the NAND state */
  1855. hnand->State = HAL_NAND_STATE_READY;
  1856. }
  1857. else
  1858. {
  1859. return HAL_ERROR;
  1860. }
  1861. return HAL_OK;
  1862. }
  1863. /**
  1864. * @brief Disables dynamically FSMC_NAND ECC feature.
  1865. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1866. * the configuration information for NAND module.
  1867. * @retval HAL status
  1868. */
  1869. HAL_StatusTypeDef HAL_NAND_ECC_Disable(NAND_HandleTypeDef *hnand)
  1870. {
  1871. /* Check the NAND controller state */
  1872. if (hnand->State == HAL_NAND_STATE_BUSY)
  1873. {
  1874. return HAL_BUSY;
  1875. }
  1876. else if (hnand->State == HAL_NAND_STATE_READY)
  1877. {
  1878. /* Update the NAND state */
  1879. hnand->State = HAL_NAND_STATE_BUSY;
  1880. /* Disable ECC feature */
  1881. (void)FSMC_NAND_ECC_Disable(hnand->Instance, hnand->Init.NandBank);
  1882. /* Update the NAND state */
  1883. hnand->State = HAL_NAND_STATE_READY;
  1884. }
  1885. else
  1886. {
  1887. return HAL_ERROR;
  1888. }
  1889. return HAL_OK;
  1890. }
  1891. /**
  1892. * @brief Disables dynamically NAND ECC feature.
  1893. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1894. * the configuration information for NAND module.
  1895. * @param ECCval pointer to ECC value
  1896. * @param Timeout maximum timeout to wait
  1897. * @retval HAL status
  1898. */
  1899. HAL_StatusTypeDef HAL_NAND_GetECC(NAND_HandleTypeDef *hnand, uint32_t *ECCval, uint32_t Timeout)
  1900. {
  1901. HAL_StatusTypeDef status;
  1902. /* Check the NAND controller state */
  1903. if (hnand->State == HAL_NAND_STATE_BUSY)
  1904. {
  1905. return HAL_BUSY;
  1906. }
  1907. else if (hnand->State == HAL_NAND_STATE_READY)
  1908. {
  1909. /* Update the NAND state */
  1910. hnand->State = HAL_NAND_STATE_BUSY;
  1911. /* Get NAND ECC value */
  1912. status = FSMC_NAND_GetECC(hnand->Instance, ECCval, hnand->Init.NandBank, Timeout);
  1913. /* Update the NAND state */
  1914. hnand->State = HAL_NAND_STATE_READY;
  1915. }
  1916. else
  1917. {
  1918. return HAL_ERROR;
  1919. }
  1920. return status;
  1921. }
  1922. /**
  1923. * @}
  1924. */
  1925. /** @defgroup NAND_Exported_Functions_Group4 Peripheral State functions
  1926. * @brief Peripheral State functions
  1927. *
  1928. @verbatim
  1929. ==============================================================================
  1930. ##### NAND State functions #####
  1931. ==============================================================================
  1932. [..]
  1933. This subsection permits to get in run-time the status of the NAND controller
  1934. and the data flow.
  1935. @endverbatim
  1936. * @{
  1937. */
  1938. /**
  1939. * @brief return the NAND state
  1940. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1941. * the configuration information for NAND module.
  1942. * @retval HAL state
  1943. */
  1944. HAL_NAND_StateTypeDef HAL_NAND_GetState(NAND_HandleTypeDef *hnand)
  1945. {
  1946. return hnand->State;
  1947. }
  1948. /**
  1949. * @brief NAND memory read status
  1950. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1951. * the configuration information for NAND module.
  1952. * @retval NAND status
  1953. */
  1954. uint32_t HAL_NAND_Read_Status(NAND_HandleTypeDef *hnand)
  1955. {
  1956. uint32_t data;
  1957. uint32_t deviceaddress;
  1958. UNUSED(hnand);
  1959. /* Identify the device address */
  1960. if (hnand->Init.NandBank == FSMC_NAND_BANK2)
  1961. {
  1962. deviceaddress = NAND_DEVICE1;
  1963. }
  1964. else
  1965. {
  1966. deviceaddress = NAND_DEVICE2;
  1967. }
  1968. /* Send Read status operation command */
  1969. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_STATUS;
  1970. /* Read status register data */
  1971. data = *(__IO uint8_t *)deviceaddress;
  1972. /* Return the status */
  1973. if ((data & NAND_ERROR) == NAND_ERROR)
  1974. {
  1975. return NAND_ERROR;
  1976. }
  1977. else if ((data & NAND_READY) == NAND_READY)
  1978. {
  1979. return NAND_READY;
  1980. }
  1981. else
  1982. {
  1983. return NAND_BUSY;
  1984. }
  1985. }
  1986. /**
  1987. * @}
  1988. */
  1989. /**
  1990. * @}
  1991. */
  1992. /**
  1993. * @}
  1994. */
  1995. #endif /* HAL_NAND_MODULE_ENABLED */
  1996. /**
  1997. * @}
  1998. */
  1999. #endif /* FSMC_BANK3 */
  2000. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/