stm32l4xx_hal_nand.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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) 2017 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 FMC 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 "stm32l4xx_hal.h"
  94. #if defined(FMC_BANK3)
  95. /** @addtogroup STM32L4xx_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, FMC_NAND_PCC_TimingTypeDef *ComSpace_Timing,
  134. FMC_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)FMC_NAND_Init(hnand->Instance, &(hnand->Init));
  160. /* Initialize NAND common space timing Interface */
  161. (void)FMC_NAND_CommonSpace_Timing_Init(hnand->Instance, ComSpace_Timing, hnand->Init.NandBank);
  162. /* Initialize NAND attribute space timing Interface */
  163. (void)FMC_NAND_AttributeSpace_Timing_Init(hnand->Instance, AttSpace_Timing, hnand->Init.NandBank);
  164. /* Enable the NAND device */
  165. __FMC_NAND_ENABLE(hnand->Instance);
  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)FMC_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 (__FMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_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. __FMC_NAND_CLEAR_FLAG(hnand->Instance, FMC_FLAG_RISING_EDGE);
  244. }
  245. /* Check NAND interrupt Level flag */
  246. if (__FMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_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. __FMC_NAND_CLEAR_FLAG(hnand->Instance, FMC_FLAG_LEVEL);
  256. }
  257. /* Check NAND interrupt Falling edge flag */
  258. if (__FMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_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. __FMC_NAND_CLEAR_FLAG(hnand->Instance, FMC_FLAG_FALLING_EDGE);
  268. }
  269. /* Check NAND interrupt FIFO empty flag */
  270. if (__FMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_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. __FMC_NAND_CLEAR_FLAG(hnand->Instance, FMC_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. deviceaddress = NAND_DEVICE;
  337. /* Send Read ID command sequence */
  338. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_READID;
  339. __DSB();
  340. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  341. __DSB();
  342. /* Read the electronic signature from NAND flash */
  343. if (hnand->Init.MemoryDataWidth == FMC_NAND_MEM_BUS_WIDTH_8)
  344. {
  345. data = *(__IO uint32_t *)deviceaddress;
  346. /* Return the data read */
  347. pNAND_ID->Maker_Id = ADDR_1ST_CYCLE(data);
  348. pNAND_ID->Device_Id = ADDR_2ND_CYCLE(data);
  349. pNAND_ID->Third_Id = ADDR_3RD_CYCLE(data);
  350. pNAND_ID->Fourth_Id = ADDR_4TH_CYCLE(data);
  351. }
  352. else
  353. {
  354. data = *(__IO uint32_t *)deviceaddress;
  355. data1 = *((__IO uint32_t *)deviceaddress + 4);
  356. /* Return the data read */
  357. pNAND_ID->Maker_Id = ADDR_1ST_CYCLE(data);
  358. pNAND_ID->Device_Id = ADDR_3RD_CYCLE(data);
  359. pNAND_ID->Third_Id = ADDR_1ST_CYCLE(data1);
  360. pNAND_ID->Fourth_Id = ADDR_3RD_CYCLE(data1);
  361. }
  362. /* Update the NAND controller state */
  363. hnand->State = HAL_NAND_STATE_READY;
  364. /* Process unlocked */
  365. __HAL_UNLOCK(hnand);
  366. }
  367. else
  368. {
  369. return HAL_ERROR;
  370. }
  371. return HAL_OK;
  372. }
  373. /**
  374. * @brief NAND memory reset
  375. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  376. * the configuration information for NAND module.
  377. * @retval HAL status
  378. */
  379. HAL_StatusTypeDef HAL_NAND_Reset(NAND_HandleTypeDef *hnand)
  380. {
  381. uint32_t deviceaddress;
  382. /* Check the NAND controller state */
  383. if (hnand->State == HAL_NAND_STATE_BUSY)
  384. {
  385. return HAL_BUSY;
  386. }
  387. else if (hnand->State == HAL_NAND_STATE_READY)
  388. {
  389. /* Process Locked */
  390. __HAL_LOCK(hnand);
  391. /* Update the NAND controller state */
  392. hnand->State = HAL_NAND_STATE_BUSY;
  393. /* Identify the device address */
  394. deviceaddress = NAND_DEVICE;
  395. /* Send NAND reset command */
  396. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = 0xFF;
  397. /* Update the NAND controller state */
  398. hnand->State = HAL_NAND_STATE_READY;
  399. /* Process unlocked */
  400. __HAL_UNLOCK(hnand);
  401. }
  402. else
  403. {
  404. return HAL_ERROR;
  405. }
  406. return HAL_OK;
  407. }
  408. /**
  409. * @brief Configure the device: Enter the physical parameters of the device
  410. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  411. * the configuration information for NAND module.
  412. * @param pDeviceConfig pointer to NAND_DeviceConfigTypeDef structure
  413. * @retval HAL status
  414. */
  415. HAL_StatusTypeDef HAL_NAND_ConfigDevice(NAND_HandleTypeDef *hnand, const NAND_DeviceConfigTypeDef *pDeviceConfig)
  416. {
  417. hnand->Config.PageSize = pDeviceConfig->PageSize;
  418. hnand->Config.SpareAreaSize = pDeviceConfig->SpareAreaSize;
  419. hnand->Config.BlockSize = pDeviceConfig->BlockSize;
  420. hnand->Config.BlockNbr = pDeviceConfig->BlockNbr;
  421. hnand->Config.PlaneSize = pDeviceConfig->PlaneSize;
  422. hnand->Config.PlaneNbr = pDeviceConfig->PlaneNbr;
  423. hnand->Config.ExtraCommandEnable = pDeviceConfig->ExtraCommandEnable;
  424. return HAL_OK;
  425. }
  426. /**
  427. * @brief Read Page(s) from NAND memory block (8-bits addressing)
  428. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  429. * the configuration information for NAND module.
  430. * @param pAddress pointer to NAND address structure
  431. * @param pBuffer pointer to destination read buffer
  432. * @param NumPageToRead number of pages to read from block
  433. * @retval HAL status
  434. */
  435. HAL_StatusTypeDef HAL_NAND_Read_Page_8b(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress,
  436. uint8_t *pBuffer, uint32_t NumPageToRead)
  437. {
  438. uint32_t index;
  439. uint32_t tickstart;
  440. uint32_t deviceaddress;
  441. uint32_t numpagesread = 0U;
  442. uint32_t nandaddress;
  443. uint32_t nbpages = NumPageToRead;
  444. uint8_t *buff = pBuffer;
  445. /* Check the NAND controller state */
  446. if (hnand->State == HAL_NAND_STATE_BUSY)
  447. {
  448. return HAL_BUSY;
  449. }
  450. else if (hnand->State == HAL_NAND_STATE_READY)
  451. {
  452. /* Process Locked */
  453. __HAL_LOCK(hnand);
  454. /* Update the NAND controller state */
  455. hnand->State = HAL_NAND_STATE_BUSY;
  456. /* Identify the device address */
  457. deviceaddress = NAND_DEVICE;
  458. /* NAND raw address calculation */
  459. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  460. /* Page(s) read loop */
  461. while ((nbpages != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  462. {
  463. /* Send read page command sequence */
  464. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  465. __DSB();
  466. /* Cards with page size <= 512 bytes */
  467. if ((hnand->Config.PageSize) <= 512U)
  468. {
  469. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  470. {
  471. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  472. __DSB();
  473. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  474. __DSB();
  475. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  476. __DSB();
  477. }
  478. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  479. {
  480. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  481. __DSB();
  482. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  483. __DSB();
  484. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  485. __DSB();
  486. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  487. __DSB();
  488. }
  489. }
  490. else /* (hnand->Config.PageSize) > 512 */
  491. {
  492. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  493. {
  494. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  495. __DSB();
  496. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  497. __DSB();
  498. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  499. __DSB();
  500. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  501. __DSB();
  502. }
  503. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  504. {
  505. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  506. __DSB();
  507. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  508. __DSB();
  509. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  510. __DSB();
  511. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  512. __DSB();
  513. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  514. __DSB();
  515. }
  516. }
  517. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  518. __DSB();
  519. if (hnand->Config.ExtraCommandEnable == ENABLE)
  520. {
  521. /* Get tick */
  522. tickstart = HAL_GetTick();
  523. /* Read status until NAND is ready */
  524. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  525. {
  526. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  527. {
  528. /* Update the NAND controller state */
  529. hnand->State = HAL_NAND_STATE_ERROR;
  530. /* Process unlocked */
  531. __HAL_UNLOCK(hnand);
  532. return HAL_TIMEOUT;
  533. }
  534. }
  535. /* Go back to read mode */
  536. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  537. __DSB();
  538. }
  539. /* Get Data into Buffer */
  540. for (index = 0U; index < hnand->Config.PageSize; index++)
  541. {
  542. *buff = *(uint8_t *)deviceaddress;
  543. buff++;
  544. }
  545. /* Increment read pages number */
  546. numpagesread++;
  547. /* Decrement pages to read */
  548. nbpages--;
  549. /* Increment the NAND address */
  550. nandaddress = (uint32_t)(nandaddress + 1U);
  551. }
  552. /* Update the NAND controller state */
  553. hnand->State = HAL_NAND_STATE_READY;
  554. /* Process unlocked */
  555. __HAL_UNLOCK(hnand);
  556. }
  557. else
  558. {
  559. return HAL_ERROR;
  560. }
  561. return HAL_OK;
  562. }
  563. /**
  564. * @brief Read Page(s) from NAND memory block (16-bits addressing)
  565. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  566. * the configuration information for NAND module.
  567. * @param pAddress pointer to NAND address structure
  568. * @param pBuffer pointer to destination read buffer. pBuffer should be 16bits aligned
  569. * @param NumPageToRead number of pages to read from block
  570. * @retval HAL status
  571. */
  572. HAL_StatusTypeDef HAL_NAND_Read_Page_16b(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress,
  573. uint16_t *pBuffer, uint32_t NumPageToRead)
  574. {
  575. uint32_t index;
  576. uint32_t tickstart;
  577. uint32_t deviceaddress;
  578. uint32_t numpagesread = 0U;
  579. uint32_t nandaddress;
  580. uint32_t nbpages = NumPageToRead;
  581. uint16_t *buff = pBuffer;
  582. /* Check the NAND controller state */
  583. if (hnand->State == HAL_NAND_STATE_BUSY)
  584. {
  585. return HAL_BUSY;
  586. }
  587. else if (hnand->State == HAL_NAND_STATE_READY)
  588. {
  589. /* Process Locked */
  590. __HAL_LOCK(hnand);
  591. /* Update the NAND controller state */
  592. hnand->State = HAL_NAND_STATE_BUSY;
  593. /* Identify the device address */
  594. deviceaddress = NAND_DEVICE;
  595. /* NAND raw address calculation */
  596. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  597. /* Page(s) read loop */
  598. while ((nbpages != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  599. {
  600. /* Send read page command sequence */
  601. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  602. __DSB();
  603. /* Cards with page size <= 512 bytes */
  604. if ((hnand->Config.PageSize) <= 512U)
  605. {
  606. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  607. {
  608. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  609. __DSB();
  610. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  611. __DSB();
  612. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  613. __DSB();
  614. }
  615. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  616. {
  617. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  618. __DSB();
  619. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  620. __DSB();
  621. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  622. __DSB();
  623. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  624. __DSB();
  625. }
  626. }
  627. else /* (hnand->Config.PageSize) > 512 */
  628. {
  629. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  630. {
  631. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  632. __DSB();
  633. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  634. __DSB();
  635. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  636. __DSB();
  637. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  638. __DSB();
  639. }
  640. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  641. {
  642. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  643. __DSB();
  644. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  645. __DSB();
  646. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  647. __DSB();
  648. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  649. __DSB();
  650. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  651. __DSB();
  652. }
  653. }
  654. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  655. __DSB();
  656. if (hnand->Config.ExtraCommandEnable == ENABLE)
  657. {
  658. /* Get tick */
  659. tickstart = HAL_GetTick();
  660. /* Read status until NAND is ready */
  661. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  662. {
  663. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  664. {
  665. /* Update the NAND controller state */
  666. hnand->State = HAL_NAND_STATE_ERROR;
  667. /* Process unlocked */
  668. __HAL_UNLOCK(hnand);
  669. return HAL_TIMEOUT;
  670. }
  671. }
  672. /* Go back to read mode */
  673. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  674. __DSB();
  675. }
  676. /* Calculate PageSize */
  677. if (hnand->Init.MemoryDataWidth == FMC_NAND_MEM_BUS_WIDTH_8)
  678. {
  679. hnand->Config.PageSize = hnand->Config.PageSize / 2U;
  680. }
  681. else
  682. {
  683. /* Do nothing */
  684. /* Keep the same PageSize for FMC_NAND_MEM_BUS_WIDTH_16*/
  685. }
  686. /* Get Data into Buffer */
  687. for (index = 0U; index < hnand->Config.PageSize; index++)
  688. {
  689. *buff = *(uint16_t *)deviceaddress;
  690. buff++;
  691. }
  692. /* Increment read pages number */
  693. numpagesread++;
  694. /* Decrement pages to read */
  695. nbpages--;
  696. /* Increment the NAND address */
  697. nandaddress = (uint32_t)(nandaddress + 1U);
  698. }
  699. /* Update the NAND controller state */
  700. hnand->State = HAL_NAND_STATE_READY;
  701. /* Process unlocked */
  702. __HAL_UNLOCK(hnand);
  703. }
  704. else
  705. {
  706. return HAL_ERROR;
  707. }
  708. return HAL_OK;
  709. }
  710. /**
  711. * @brief Write Page(s) to NAND memory block (8-bits addressing)
  712. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  713. * the configuration information for NAND module.
  714. * @param pAddress pointer to NAND address structure
  715. * @param pBuffer pointer to source buffer to write
  716. * @param NumPageToWrite number of pages to write to block
  717. * @retval HAL status
  718. */
  719. HAL_StatusTypeDef HAL_NAND_Write_Page_8b(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress,
  720. const uint8_t *pBuffer, uint32_t NumPageToWrite)
  721. {
  722. uint32_t index;
  723. uint32_t tickstart;
  724. uint32_t deviceaddress;
  725. uint32_t numpageswritten = 0U;
  726. uint32_t nandaddress;
  727. uint32_t nbpages = NumPageToWrite;
  728. const uint8_t *buff = pBuffer;
  729. /* Check the NAND controller state */
  730. if (hnand->State == HAL_NAND_STATE_BUSY)
  731. {
  732. return HAL_BUSY;
  733. }
  734. else if (hnand->State == HAL_NAND_STATE_READY)
  735. {
  736. /* Process Locked */
  737. __HAL_LOCK(hnand);
  738. /* Update the NAND controller state */
  739. hnand->State = HAL_NAND_STATE_BUSY;
  740. /* Identify the device address */
  741. deviceaddress = NAND_DEVICE;
  742. /* NAND raw address calculation */
  743. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  744. /* Page(s) write loop */
  745. while ((nbpages != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  746. {
  747. /* Send write page command sequence */
  748. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  749. __DSB();
  750. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  751. __DSB();
  752. /* Cards with page size <= 512 bytes */
  753. if ((hnand->Config.PageSize) <= 512U)
  754. {
  755. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  756. {
  757. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  758. __DSB();
  759. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  760. __DSB();
  761. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  762. __DSB();
  763. }
  764. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  765. {
  766. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  767. __DSB();
  768. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  769. __DSB();
  770. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  771. __DSB();
  772. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  773. __DSB();
  774. }
  775. }
  776. else /* (hnand->Config.PageSize) > 512 */
  777. {
  778. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  779. {
  780. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  781. __DSB();
  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)) = 0x00U;
  794. __DSB();
  795. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  796. __DSB();
  797. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  798. __DSB();
  799. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  800. __DSB();
  801. }
  802. }
  803. /* Write data to memory */
  804. for (index = 0U; index < hnand->Config.PageSize; index++)
  805. {
  806. *(__IO uint8_t *)deviceaddress = *buff;
  807. buff++;
  808. __DSB();
  809. }
  810. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  811. __DSB();
  812. /* Get tick */
  813. tickstart = HAL_GetTick();
  814. /* Read status until NAND is ready */
  815. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  816. {
  817. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  818. {
  819. /* Update the NAND controller state */
  820. hnand->State = HAL_NAND_STATE_ERROR;
  821. /* Process unlocked */
  822. __HAL_UNLOCK(hnand);
  823. return HAL_TIMEOUT;
  824. }
  825. }
  826. /* Increment written pages number */
  827. numpageswritten++;
  828. /* Decrement pages to write */
  829. nbpages--;
  830. /* Increment the NAND address */
  831. nandaddress = (uint32_t)(nandaddress + 1U);
  832. }
  833. /* Update the NAND controller state */
  834. hnand->State = HAL_NAND_STATE_READY;
  835. /* Process unlocked */
  836. __HAL_UNLOCK(hnand);
  837. }
  838. else
  839. {
  840. return HAL_ERROR;
  841. }
  842. return HAL_OK;
  843. }
  844. /**
  845. * @brief Write Page(s) to NAND memory block (16-bits addressing)
  846. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  847. * the configuration information for NAND module.
  848. * @param pAddress pointer to NAND address structure
  849. * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned
  850. * @param NumPageToWrite number of pages to write to block
  851. * @retval HAL status
  852. */
  853. HAL_StatusTypeDef HAL_NAND_Write_Page_16b(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress,
  854. const uint16_t *pBuffer, uint32_t NumPageToWrite)
  855. {
  856. uint32_t index;
  857. uint32_t tickstart;
  858. uint32_t deviceaddress;
  859. uint32_t numpageswritten = 0U;
  860. uint32_t nandaddress;
  861. uint32_t nbpages = NumPageToWrite;
  862. const uint16_t *buff = pBuffer;
  863. /* Check the NAND controller state */
  864. if (hnand->State == HAL_NAND_STATE_BUSY)
  865. {
  866. return HAL_BUSY;
  867. }
  868. else if (hnand->State == HAL_NAND_STATE_READY)
  869. {
  870. /* Process Locked */
  871. __HAL_LOCK(hnand);
  872. /* Update the NAND controller state */
  873. hnand->State = HAL_NAND_STATE_BUSY;
  874. /* Identify the device address */
  875. deviceaddress = NAND_DEVICE;
  876. /* NAND raw address calculation */
  877. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  878. /* Page(s) write loop */
  879. while ((nbpages != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  880. {
  881. /* Send write page command sequence */
  882. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  883. __DSB();
  884. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  885. __DSB();
  886. /* Cards with page size <= 512 bytes */
  887. if ((hnand->Config.PageSize) <= 512U)
  888. {
  889. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  890. {
  891. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  892. __DSB();
  893. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  894. __DSB();
  895. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  896. __DSB();
  897. }
  898. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  899. {
  900. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  901. __DSB();
  902. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  903. __DSB();
  904. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  905. __DSB();
  906. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  907. __DSB();
  908. }
  909. }
  910. else /* (hnand->Config.PageSize) > 512 */
  911. {
  912. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  913. {
  914. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  915. __DSB();
  916. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  917. __DSB();
  918. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  919. __DSB();
  920. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  921. __DSB();
  922. }
  923. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  924. {
  925. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  926. __DSB();
  927. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  928. __DSB();
  929. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  930. __DSB();
  931. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  932. __DSB();
  933. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  934. __DSB();
  935. }
  936. }
  937. /* Calculate PageSize */
  938. if (hnand->Init.MemoryDataWidth == FMC_NAND_MEM_BUS_WIDTH_8)
  939. {
  940. hnand->Config.PageSize = hnand->Config.PageSize / 2U;
  941. }
  942. else
  943. {
  944. /* Do nothing */
  945. /* Keep the same PageSize for FMC_NAND_MEM_BUS_WIDTH_16*/
  946. }
  947. /* Write data to memory */
  948. for (index = 0U; index < hnand->Config.PageSize; index++)
  949. {
  950. *(__IO uint16_t *)deviceaddress = *buff;
  951. buff++;
  952. __DSB();
  953. }
  954. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  955. __DSB();
  956. /* Get tick */
  957. tickstart = HAL_GetTick();
  958. /* Read status until NAND is ready */
  959. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  960. {
  961. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  962. {
  963. /* Update the NAND controller state */
  964. hnand->State = HAL_NAND_STATE_ERROR;
  965. /* Process unlocked */
  966. __HAL_UNLOCK(hnand);
  967. return HAL_TIMEOUT;
  968. }
  969. }
  970. /* Increment written pages number */
  971. numpageswritten++;
  972. /* Decrement pages to write */
  973. nbpages--;
  974. /* Increment the NAND address */
  975. nandaddress = (uint32_t)(nandaddress + 1U);
  976. }
  977. /* Update the NAND controller state */
  978. hnand->State = HAL_NAND_STATE_READY;
  979. /* Process unlocked */
  980. __HAL_UNLOCK(hnand);
  981. }
  982. else
  983. {
  984. return HAL_ERROR;
  985. }
  986. return HAL_OK;
  987. }
  988. /**
  989. * @brief Read Spare area(s) from NAND memory (8-bits addressing)
  990. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  991. * the configuration information for NAND module.
  992. * @param pAddress pointer to NAND address structure
  993. * @param pBuffer pointer to source buffer to write
  994. * @param NumSpareAreaToRead Number of spare area to read
  995. * @retval HAL status
  996. */
  997. HAL_StatusTypeDef HAL_NAND_Read_SpareArea_8b(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress,
  998. uint8_t *pBuffer, uint32_t NumSpareAreaToRead)
  999. {
  1000. uint32_t index;
  1001. uint32_t tickstart;
  1002. uint32_t deviceaddress;
  1003. uint32_t numsparearearead = 0U;
  1004. uint32_t nandaddress;
  1005. uint32_t columnaddress;
  1006. uint32_t nbspare = NumSpareAreaToRead;
  1007. uint8_t *buff = pBuffer;
  1008. /* Check the NAND controller state */
  1009. if (hnand->State == HAL_NAND_STATE_BUSY)
  1010. {
  1011. return HAL_BUSY;
  1012. }
  1013. else if (hnand->State == HAL_NAND_STATE_READY)
  1014. {
  1015. /* Process Locked */
  1016. __HAL_LOCK(hnand);
  1017. /* Update the NAND controller state */
  1018. hnand->State = HAL_NAND_STATE_BUSY;
  1019. /* Identify the device address */
  1020. deviceaddress = NAND_DEVICE;
  1021. /* NAND raw address calculation */
  1022. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1023. /* Column in page address */
  1024. columnaddress = COLUMN_ADDRESS(hnand);
  1025. /* Spare area(s) read loop */
  1026. while ((nbspare != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1027. {
  1028. /* Cards with page size <= 512 bytes */
  1029. if ((hnand->Config.PageSize) <= 512U)
  1030. {
  1031. /* Send read spare area command sequence */
  1032. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1033. __DSB();
  1034. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1035. {
  1036. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1037. __DSB();
  1038. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1039. __DSB();
  1040. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1041. __DSB();
  1042. }
  1043. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1044. {
  1045. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1046. __DSB();
  1047. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1048. __DSB();
  1049. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1050. __DSB();
  1051. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1052. __DSB();
  1053. }
  1054. }
  1055. else /* (hnand->Config.PageSize) > 512 */
  1056. {
  1057. /* Send read spare area command sequence */
  1058. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1059. __DSB();
  1060. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1061. {
  1062. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1063. __DSB();
  1064. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1065. __DSB();
  1066. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1067. __DSB();
  1068. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1069. __DSB();
  1070. }
  1071. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1072. {
  1073. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1074. __DSB();
  1075. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1076. __DSB();
  1077. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1078. __DSB();
  1079. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1080. __DSB();
  1081. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1082. __DSB();
  1083. }
  1084. }
  1085. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  1086. __DSB();
  1087. if (hnand->Config.ExtraCommandEnable == ENABLE)
  1088. {
  1089. /* Get tick */
  1090. tickstart = HAL_GetTick();
  1091. /* Read status until NAND is ready */
  1092. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  1093. {
  1094. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  1095. {
  1096. /* Update the NAND controller state */
  1097. hnand->State = HAL_NAND_STATE_ERROR;
  1098. /* Process unlocked */
  1099. __HAL_UNLOCK(hnand);
  1100. return HAL_TIMEOUT;
  1101. }
  1102. }
  1103. /* Go back to read mode */
  1104. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  1105. __DSB();
  1106. }
  1107. /* Get Data into Buffer */
  1108. for (index = 0U; index < hnand->Config.SpareAreaSize; index++)
  1109. {
  1110. *buff = *(uint8_t *)deviceaddress;
  1111. buff++;
  1112. }
  1113. /* Increment read spare areas number */
  1114. numsparearearead++;
  1115. /* Decrement spare areas to read */
  1116. nbspare--;
  1117. /* Increment the NAND address */
  1118. nandaddress = (uint32_t)(nandaddress + 1U);
  1119. }
  1120. /* Update the NAND controller state */
  1121. hnand->State = HAL_NAND_STATE_READY;
  1122. /* Process unlocked */
  1123. __HAL_UNLOCK(hnand);
  1124. }
  1125. else
  1126. {
  1127. return HAL_ERROR;
  1128. }
  1129. return HAL_OK;
  1130. }
  1131. /**
  1132. * @brief Read Spare area(s) from NAND memory (16-bits addressing)
  1133. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1134. * the configuration information for NAND module.
  1135. * @param pAddress pointer to NAND address structure
  1136. * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned.
  1137. * @param NumSpareAreaToRead Number of spare area to read
  1138. * @retval HAL status
  1139. */
  1140. HAL_StatusTypeDef HAL_NAND_Read_SpareArea_16b(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress,
  1141. uint16_t *pBuffer, uint32_t NumSpareAreaToRead)
  1142. {
  1143. uint32_t index;
  1144. uint32_t tickstart;
  1145. uint32_t deviceaddress;
  1146. uint32_t numsparearearead = 0U;
  1147. uint32_t nandaddress;
  1148. uint32_t columnaddress;
  1149. uint32_t nbspare = NumSpareAreaToRead;
  1150. uint16_t *buff = pBuffer;
  1151. /* Check the NAND controller state */
  1152. if (hnand->State == HAL_NAND_STATE_BUSY)
  1153. {
  1154. return HAL_BUSY;
  1155. }
  1156. else if (hnand->State == HAL_NAND_STATE_READY)
  1157. {
  1158. /* Process Locked */
  1159. __HAL_LOCK(hnand);
  1160. /* Update the NAND controller state */
  1161. hnand->State = HAL_NAND_STATE_BUSY;
  1162. /* Identify the device address */
  1163. deviceaddress = NAND_DEVICE;
  1164. /* NAND raw address calculation */
  1165. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1166. /* Column in page address */
  1167. columnaddress = (uint32_t)(COLUMN_ADDRESS(hnand));
  1168. /* Spare area(s) read loop */
  1169. while ((nbspare != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1170. {
  1171. /* Cards with page size <= 512 bytes */
  1172. if ((hnand->Config.PageSize) <= 512U)
  1173. {
  1174. /* Send read spare area command sequence */
  1175. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1176. __DSB();
  1177. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1178. {
  1179. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1180. __DSB();
  1181. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1182. __DSB();
  1183. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1184. __DSB();
  1185. }
  1186. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1187. {
  1188. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1189. __DSB();
  1190. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1191. __DSB();
  1192. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1193. __DSB();
  1194. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1195. __DSB();
  1196. }
  1197. }
  1198. else /* (hnand->Config.PageSize) > 512 */
  1199. {
  1200. /* Send read spare area command sequence */
  1201. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1202. __DSB();
  1203. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1204. {
  1205. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1206. __DSB();
  1207. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1208. __DSB();
  1209. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1210. __DSB();
  1211. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1212. __DSB();
  1213. }
  1214. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1215. {
  1216. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1217. __DSB();
  1218. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1219. __DSB();
  1220. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1221. __DSB();
  1222. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1223. __DSB();
  1224. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1225. __DSB();
  1226. }
  1227. }
  1228. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  1229. __DSB();
  1230. if (hnand->Config.ExtraCommandEnable == ENABLE)
  1231. {
  1232. /* Get tick */
  1233. tickstart = HAL_GetTick();
  1234. /* Read status until NAND is ready */
  1235. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  1236. {
  1237. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  1238. {
  1239. /* Update the NAND controller state */
  1240. hnand->State = HAL_NAND_STATE_ERROR;
  1241. /* Process unlocked */
  1242. __HAL_UNLOCK(hnand);
  1243. return HAL_TIMEOUT;
  1244. }
  1245. }
  1246. /* Go back to read mode */
  1247. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  1248. __DSB();
  1249. }
  1250. /* Get Data into Buffer */
  1251. for (index = 0U; index < hnand->Config.SpareAreaSize; index++)
  1252. {
  1253. *buff = *(uint16_t *)deviceaddress;
  1254. buff++;
  1255. }
  1256. /* Increment read spare areas number */
  1257. numsparearearead++;
  1258. /* Decrement spare areas to read */
  1259. nbspare--;
  1260. /* Increment the NAND address */
  1261. nandaddress = (uint32_t)(nandaddress + 1U);
  1262. }
  1263. /* Update the NAND controller state */
  1264. hnand->State = HAL_NAND_STATE_READY;
  1265. /* Process unlocked */
  1266. __HAL_UNLOCK(hnand);
  1267. }
  1268. else
  1269. {
  1270. return HAL_ERROR;
  1271. }
  1272. return HAL_OK;
  1273. }
  1274. /**
  1275. * @brief Write Spare area(s) to NAND memory (8-bits addressing)
  1276. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1277. * the configuration information for NAND module.
  1278. * @param pAddress pointer to NAND address structure
  1279. * @param pBuffer pointer to source buffer to write
  1280. * @param NumSpareAreaTowrite number of spare areas to write to block
  1281. * @retval HAL status
  1282. */
  1283. HAL_StatusTypeDef HAL_NAND_Write_SpareArea_8b(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress,
  1284. const uint8_t *pBuffer, uint32_t NumSpareAreaTowrite)
  1285. {
  1286. uint32_t index;
  1287. uint32_t tickstart;
  1288. uint32_t deviceaddress;
  1289. uint32_t numspareareawritten = 0U;
  1290. uint32_t nandaddress;
  1291. uint32_t columnaddress;
  1292. uint32_t nbspare = NumSpareAreaTowrite;
  1293. const uint8_t *buff = pBuffer;
  1294. /* Check the NAND controller state */
  1295. if (hnand->State == HAL_NAND_STATE_BUSY)
  1296. {
  1297. return HAL_BUSY;
  1298. }
  1299. else if (hnand->State == HAL_NAND_STATE_READY)
  1300. {
  1301. /* Process Locked */
  1302. __HAL_LOCK(hnand);
  1303. /* Update the NAND controller state */
  1304. hnand->State = HAL_NAND_STATE_BUSY;
  1305. /* Identify the device address */
  1306. deviceaddress = NAND_DEVICE;
  1307. /* Page address calculation */
  1308. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1309. /* Column in page address */
  1310. columnaddress = COLUMN_ADDRESS(hnand);
  1311. /* Spare area(s) write loop */
  1312. while ((nbspare != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1313. {
  1314. /* Cards with page size <= 512 bytes */
  1315. if ((hnand->Config.PageSize) <= 512U)
  1316. {
  1317. /* Send write Spare area command sequence */
  1318. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1319. __DSB();
  1320. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1321. __DSB();
  1322. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1323. {
  1324. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1325. __DSB();
  1326. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1327. __DSB();
  1328. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1329. __DSB();
  1330. }
  1331. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1332. {
  1333. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1334. __DSB();
  1335. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1336. __DSB();
  1337. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1338. __DSB();
  1339. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1340. __DSB();
  1341. }
  1342. }
  1343. else /* (hnand->Config.PageSize) > 512 */
  1344. {
  1345. /* Send write Spare area command sequence */
  1346. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1347. __DSB();
  1348. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1349. __DSB();
  1350. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1351. {
  1352. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1353. __DSB();
  1354. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1355. __DSB();
  1356. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1357. __DSB();
  1358. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1359. __DSB();
  1360. }
  1361. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1362. {
  1363. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1364. __DSB();
  1365. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1366. __DSB();
  1367. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1368. __DSB();
  1369. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1370. __DSB();
  1371. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1372. __DSB();
  1373. }
  1374. }
  1375. /* Write data to memory */
  1376. for (index = 0U; index < hnand->Config.SpareAreaSize; index++)
  1377. {
  1378. *(__IO uint8_t *)deviceaddress = *buff;
  1379. buff++;
  1380. __DSB();
  1381. }
  1382. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  1383. __DSB();
  1384. /* Get tick */
  1385. tickstart = HAL_GetTick();
  1386. /* Read status until NAND is ready */
  1387. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  1388. {
  1389. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  1390. {
  1391. /* Update the NAND controller state */
  1392. hnand->State = HAL_NAND_STATE_ERROR;
  1393. /* Process unlocked */
  1394. __HAL_UNLOCK(hnand);
  1395. return HAL_TIMEOUT;
  1396. }
  1397. }
  1398. /* Increment written spare areas number */
  1399. numspareareawritten++;
  1400. /* Decrement spare areas to write */
  1401. nbspare--;
  1402. /* Increment the NAND address */
  1403. nandaddress = (uint32_t)(nandaddress + 1U);
  1404. }
  1405. /* Update the NAND controller state */
  1406. hnand->State = HAL_NAND_STATE_READY;
  1407. /* Process unlocked */
  1408. __HAL_UNLOCK(hnand);
  1409. }
  1410. else
  1411. {
  1412. return HAL_ERROR;
  1413. }
  1414. return HAL_OK;
  1415. }
  1416. /**
  1417. * @brief Write Spare area(s) to NAND memory (16-bits addressing)
  1418. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1419. * the configuration information for NAND module.
  1420. * @param pAddress pointer to NAND address structure
  1421. * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned.
  1422. * @param NumSpareAreaTowrite number of spare areas to write to block
  1423. * @retval HAL status
  1424. */
  1425. HAL_StatusTypeDef HAL_NAND_Write_SpareArea_16b(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress,
  1426. const uint16_t *pBuffer, uint32_t NumSpareAreaTowrite)
  1427. {
  1428. uint32_t index;
  1429. uint32_t tickstart;
  1430. uint32_t deviceaddress;
  1431. uint32_t numspareareawritten = 0U;
  1432. uint32_t nandaddress;
  1433. uint32_t columnaddress;
  1434. uint32_t nbspare = NumSpareAreaTowrite;
  1435. const uint16_t *buff = pBuffer;
  1436. /* Check the NAND controller state */
  1437. if (hnand->State == HAL_NAND_STATE_BUSY)
  1438. {
  1439. return HAL_BUSY;
  1440. }
  1441. else if (hnand->State == HAL_NAND_STATE_READY)
  1442. {
  1443. /* Process Locked */
  1444. __HAL_LOCK(hnand);
  1445. /* Update the NAND controller state */
  1446. hnand->State = HAL_NAND_STATE_BUSY;
  1447. /* Identify the device address */
  1448. deviceaddress = NAND_DEVICE;
  1449. /* NAND raw address calculation */
  1450. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1451. /* Column in page address */
  1452. columnaddress = (uint32_t)(COLUMN_ADDRESS(hnand));
  1453. /* Spare area(s) write loop */
  1454. while ((nbspare != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1455. {
  1456. /* Cards with page size <= 512 bytes */
  1457. if ((hnand->Config.PageSize) <= 512U)
  1458. {
  1459. /* Send write Spare area command sequence */
  1460. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1461. __DSB();
  1462. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1463. __DSB();
  1464. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1465. {
  1466. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1467. __DSB();
  1468. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1469. __DSB();
  1470. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1471. __DSB();
  1472. }
  1473. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1474. {
  1475. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00U;
  1476. __DSB();
  1477. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1478. __DSB();
  1479. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1480. __DSB();
  1481. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1482. __DSB();
  1483. }
  1484. }
  1485. else /* (hnand->Config.PageSize) > 512 */
  1486. {
  1487. /* Send write Spare area command sequence */
  1488. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1489. __DSB();
  1490. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1491. __DSB();
  1492. if (((hnand->Config.BlockSize) * (hnand->Config.BlockNbr)) <= 65535U)
  1493. {
  1494. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1495. __DSB();
  1496. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1497. __DSB();
  1498. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1499. __DSB();
  1500. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1501. __DSB();
  1502. }
  1503. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1504. {
  1505. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1506. __DSB();
  1507. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1508. __DSB();
  1509. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1510. __DSB();
  1511. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1512. __DSB();
  1513. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1514. __DSB();
  1515. }
  1516. }
  1517. /* Write data to memory */
  1518. for (index = 0U; index < hnand->Config.SpareAreaSize; index++)
  1519. {
  1520. *(__IO uint16_t *)deviceaddress = *buff;
  1521. buff++;
  1522. __DSB();
  1523. }
  1524. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  1525. __DSB();
  1526. /* Get tick */
  1527. tickstart = HAL_GetTick();
  1528. /* Read status until NAND is ready */
  1529. while (HAL_NAND_Read_Status(hnand) != NAND_READY)
  1530. {
  1531. if ((HAL_GetTick() - tickstart) > NAND_WRITE_TIMEOUT)
  1532. {
  1533. /* Update the NAND controller state */
  1534. hnand->State = HAL_NAND_STATE_ERROR;
  1535. /* Process unlocked */
  1536. __HAL_UNLOCK(hnand);
  1537. return HAL_TIMEOUT;
  1538. }
  1539. }
  1540. /* Increment written spare areas number */
  1541. numspareareawritten++;
  1542. /* Decrement spare areas to write */
  1543. nbspare--;
  1544. /* Increment the NAND address */
  1545. nandaddress = (uint32_t)(nandaddress + 1U);
  1546. }
  1547. /* Update the NAND controller state */
  1548. hnand->State = HAL_NAND_STATE_READY;
  1549. /* Process unlocked */
  1550. __HAL_UNLOCK(hnand);
  1551. }
  1552. else
  1553. {
  1554. return HAL_ERROR;
  1555. }
  1556. return HAL_OK;
  1557. }
  1558. /**
  1559. * @brief NAND memory Block erase
  1560. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1561. * the configuration information for NAND module.
  1562. * @param pAddress pointer to NAND address structure
  1563. * @retval HAL status
  1564. */
  1565. HAL_StatusTypeDef HAL_NAND_Erase_Block(NAND_HandleTypeDef *hnand, const NAND_AddressTypeDef *pAddress)
  1566. {
  1567. uint32_t deviceaddress;
  1568. /* Check the NAND controller state */
  1569. if (hnand->State == HAL_NAND_STATE_BUSY)
  1570. {
  1571. return HAL_BUSY;
  1572. }
  1573. else if (hnand->State == HAL_NAND_STATE_READY)
  1574. {
  1575. /* Process Locked */
  1576. __HAL_LOCK(hnand);
  1577. /* Update the NAND controller state */
  1578. hnand->State = HAL_NAND_STATE_BUSY;
  1579. /* Identify the device address */
  1580. deviceaddress = NAND_DEVICE;
  1581. /* Send Erase block command sequence */
  1582. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_ERASE0;
  1583. __DSB();
  1584. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1585. __DSB();
  1586. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1587. __DSB();
  1588. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1589. __DSB();
  1590. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_ERASE1;
  1591. __DSB();
  1592. /* Update the NAND controller state */
  1593. hnand->State = HAL_NAND_STATE_READY;
  1594. /* Process unlocked */
  1595. __HAL_UNLOCK(hnand);
  1596. }
  1597. else
  1598. {
  1599. return HAL_ERROR;
  1600. }
  1601. return HAL_OK;
  1602. }
  1603. /**
  1604. * @brief Increment the NAND memory address
  1605. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1606. * the configuration information for NAND module.
  1607. * @param pAddress pointer to NAND address structure
  1608. * @retval The new status of the increment address operation. It can be:
  1609. * - NAND_VALID_ADDRESS: When the new address is valid address
  1610. * - NAND_INVALID_ADDRESS: When the new address is invalid address
  1611. */
  1612. uint32_t HAL_NAND_Address_Inc(const NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress)
  1613. {
  1614. uint32_t status = NAND_VALID_ADDRESS;
  1615. /* Increment page address */
  1616. pAddress->Page++;
  1617. /* Check NAND address is valid */
  1618. if (pAddress->Page == hnand->Config.BlockSize)
  1619. {
  1620. pAddress->Page = 0;
  1621. pAddress->Block++;
  1622. if (pAddress->Block == hnand->Config.PlaneSize)
  1623. {
  1624. pAddress->Block = 0;
  1625. pAddress->Plane++;
  1626. if (pAddress->Plane == (hnand->Config.PlaneNbr))
  1627. {
  1628. status = NAND_INVALID_ADDRESS;
  1629. }
  1630. }
  1631. }
  1632. return (status);
  1633. }
  1634. #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
  1635. /**
  1636. * @brief Register a User NAND Callback
  1637. * To be used to override the weak predefined callback
  1638. * @param hnand : NAND handle
  1639. * @param CallbackId : ID of the callback to be registered
  1640. * This parameter can be one of the following values:
  1641. * @arg @ref HAL_NAND_MSP_INIT_CB_ID NAND MspInit callback ID
  1642. * @arg @ref HAL_NAND_MSP_DEINIT_CB_ID NAND MspDeInit callback ID
  1643. * @arg @ref HAL_NAND_IT_CB_ID NAND IT callback ID
  1644. * @param pCallback : pointer to the Callback function
  1645. * @retval status
  1646. */
  1647. HAL_StatusTypeDef HAL_NAND_RegisterCallback(NAND_HandleTypeDef *hnand, HAL_NAND_CallbackIDTypeDef CallbackId,
  1648. pNAND_CallbackTypeDef pCallback)
  1649. {
  1650. HAL_StatusTypeDef status = HAL_OK;
  1651. if (pCallback == NULL)
  1652. {
  1653. return HAL_ERROR;
  1654. }
  1655. if (hnand->State == HAL_NAND_STATE_READY)
  1656. {
  1657. switch (CallbackId)
  1658. {
  1659. case HAL_NAND_MSP_INIT_CB_ID :
  1660. hnand->MspInitCallback = pCallback;
  1661. break;
  1662. case HAL_NAND_MSP_DEINIT_CB_ID :
  1663. hnand->MspDeInitCallback = pCallback;
  1664. break;
  1665. case HAL_NAND_IT_CB_ID :
  1666. hnand->ItCallback = pCallback;
  1667. break;
  1668. default :
  1669. /* update return status */
  1670. status = HAL_ERROR;
  1671. break;
  1672. }
  1673. }
  1674. else if (hnand->State == HAL_NAND_STATE_RESET)
  1675. {
  1676. switch (CallbackId)
  1677. {
  1678. case HAL_NAND_MSP_INIT_CB_ID :
  1679. hnand->MspInitCallback = pCallback;
  1680. break;
  1681. case HAL_NAND_MSP_DEINIT_CB_ID :
  1682. hnand->MspDeInitCallback = pCallback;
  1683. break;
  1684. default :
  1685. /* update return status */
  1686. status = HAL_ERROR;
  1687. break;
  1688. }
  1689. }
  1690. else
  1691. {
  1692. /* update return status */
  1693. status = HAL_ERROR;
  1694. }
  1695. return status;
  1696. }
  1697. /**
  1698. * @brief Unregister a User NAND Callback
  1699. * NAND Callback is redirected to the weak predefined callback
  1700. * @param hnand : NAND handle
  1701. * @param CallbackId : ID of the callback to be unregistered
  1702. * This parameter can be one of the following values:
  1703. * @arg @ref HAL_NAND_MSP_INIT_CB_ID NAND MspInit callback ID
  1704. * @arg @ref HAL_NAND_MSP_DEINIT_CB_ID NAND MspDeInit callback ID
  1705. * @arg @ref HAL_NAND_IT_CB_ID NAND IT callback ID
  1706. * @retval status
  1707. */
  1708. HAL_StatusTypeDef HAL_NAND_UnRegisterCallback(NAND_HandleTypeDef *hnand, HAL_NAND_CallbackIDTypeDef CallbackId)
  1709. {
  1710. HAL_StatusTypeDef status = HAL_OK;
  1711. if (hnand->State == HAL_NAND_STATE_READY)
  1712. {
  1713. switch (CallbackId)
  1714. {
  1715. case HAL_NAND_MSP_INIT_CB_ID :
  1716. hnand->MspInitCallback = HAL_NAND_MspInit;
  1717. break;
  1718. case HAL_NAND_MSP_DEINIT_CB_ID :
  1719. hnand->MspDeInitCallback = HAL_NAND_MspDeInit;
  1720. break;
  1721. case HAL_NAND_IT_CB_ID :
  1722. hnand->ItCallback = HAL_NAND_ITCallback;
  1723. break;
  1724. default :
  1725. /* update return status */
  1726. status = HAL_ERROR;
  1727. break;
  1728. }
  1729. }
  1730. else if (hnand->State == HAL_NAND_STATE_RESET)
  1731. {
  1732. switch (CallbackId)
  1733. {
  1734. case HAL_NAND_MSP_INIT_CB_ID :
  1735. hnand->MspInitCallback = HAL_NAND_MspInit;
  1736. break;
  1737. case HAL_NAND_MSP_DEINIT_CB_ID :
  1738. hnand->MspDeInitCallback = HAL_NAND_MspDeInit;
  1739. break;
  1740. default :
  1741. /* update return status */
  1742. status = HAL_ERROR;
  1743. break;
  1744. }
  1745. }
  1746. else
  1747. {
  1748. /* update return status */
  1749. status = HAL_ERROR;
  1750. }
  1751. return status;
  1752. }
  1753. #endif /* USE_HAL_NAND_REGISTER_CALLBACKS */
  1754. /**
  1755. * @}
  1756. */
  1757. /** @defgroup NAND_Exported_Functions_Group3 Peripheral Control functions
  1758. * @brief management functions
  1759. *
  1760. @verbatim
  1761. ==============================================================================
  1762. ##### NAND Control functions #####
  1763. ==============================================================================
  1764. [..]
  1765. This subsection provides a set of functions allowing to control dynamically
  1766. the NAND interface.
  1767. @endverbatim
  1768. * @{
  1769. */
  1770. /**
  1771. * @brief Enables dynamically NAND ECC feature.
  1772. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1773. * the configuration information for NAND module.
  1774. * @retval HAL status
  1775. */
  1776. HAL_StatusTypeDef HAL_NAND_ECC_Enable(NAND_HandleTypeDef *hnand)
  1777. {
  1778. /* Check the NAND controller state */
  1779. if (hnand->State == HAL_NAND_STATE_BUSY)
  1780. {
  1781. return HAL_BUSY;
  1782. }
  1783. else if (hnand->State == HAL_NAND_STATE_READY)
  1784. {
  1785. /* Update the NAND state */
  1786. hnand->State = HAL_NAND_STATE_BUSY;
  1787. /* Enable ECC feature */
  1788. (void)FMC_NAND_ECC_Enable(hnand->Instance, hnand->Init.NandBank);
  1789. /* Update the NAND state */
  1790. hnand->State = HAL_NAND_STATE_READY;
  1791. }
  1792. else
  1793. {
  1794. return HAL_ERROR;
  1795. }
  1796. return HAL_OK;
  1797. }
  1798. /**
  1799. * @brief Disables dynamically FMC_NAND ECC feature.
  1800. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1801. * the configuration information for NAND module.
  1802. * @retval HAL status
  1803. */
  1804. HAL_StatusTypeDef HAL_NAND_ECC_Disable(NAND_HandleTypeDef *hnand)
  1805. {
  1806. /* Check the NAND controller state */
  1807. if (hnand->State == HAL_NAND_STATE_BUSY)
  1808. {
  1809. return HAL_BUSY;
  1810. }
  1811. else if (hnand->State == HAL_NAND_STATE_READY)
  1812. {
  1813. /* Update the NAND state */
  1814. hnand->State = HAL_NAND_STATE_BUSY;
  1815. /* Disable ECC feature */
  1816. (void)FMC_NAND_ECC_Disable(hnand->Instance, hnand->Init.NandBank);
  1817. /* Update the NAND state */
  1818. hnand->State = HAL_NAND_STATE_READY;
  1819. }
  1820. else
  1821. {
  1822. return HAL_ERROR;
  1823. }
  1824. return HAL_OK;
  1825. }
  1826. /**
  1827. * @brief Disables dynamically NAND ECC feature.
  1828. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1829. * the configuration information for NAND module.
  1830. * @param ECCval pointer to ECC value
  1831. * @param Timeout maximum timeout to wait
  1832. * @retval HAL status
  1833. */
  1834. HAL_StatusTypeDef HAL_NAND_GetECC(NAND_HandleTypeDef *hnand, uint32_t *ECCval, uint32_t Timeout)
  1835. {
  1836. HAL_StatusTypeDef status;
  1837. /* Check the NAND controller state */
  1838. if (hnand->State == HAL_NAND_STATE_BUSY)
  1839. {
  1840. return HAL_BUSY;
  1841. }
  1842. else if (hnand->State == HAL_NAND_STATE_READY)
  1843. {
  1844. /* Update the NAND state */
  1845. hnand->State = HAL_NAND_STATE_BUSY;
  1846. /* Get NAND ECC value */
  1847. status = FMC_NAND_GetECC(hnand->Instance, ECCval, hnand->Init.NandBank, Timeout);
  1848. /* Update the NAND state */
  1849. hnand->State = HAL_NAND_STATE_READY;
  1850. }
  1851. else
  1852. {
  1853. return HAL_ERROR;
  1854. }
  1855. return status;
  1856. }
  1857. /**
  1858. * @}
  1859. */
  1860. /** @defgroup NAND_Exported_Functions_Group4 Peripheral State functions
  1861. * @brief Peripheral State functions
  1862. *
  1863. @verbatim
  1864. ==============================================================================
  1865. ##### NAND State functions #####
  1866. ==============================================================================
  1867. [..]
  1868. This subsection permits to get in run-time the status of the NAND controller
  1869. and the data flow.
  1870. @endverbatim
  1871. * @{
  1872. */
  1873. /**
  1874. * @brief return the NAND state
  1875. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1876. * the configuration information for NAND module.
  1877. * @retval HAL state
  1878. */
  1879. HAL_NAND_StateTypeDef HAL_NAND_GetState(const NAND_HandleTypeDef *hnand)
  1880. {
  1881. return hnand->State;
  1882. }
  1883. /**
  1884. * @brief NAND memory read status
  1885. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1886. * the configuration information for NAND module.
  1887. * @retval NAND status
  1888. */
  1889. uint32_t HAL_NAND_Read_Status(const NAND_HandleTypeDef *hnand)
  1890. {
  1891. uint32_t data;
  1892. uint32_t deviceaddress;
  1893. UNUSED(hnand);
  1894. /* Identify the device address */
  1895. deviceaddress = NAND_DEVICE;
  1896. /* Send Read status operation command */
  1897. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_STATUS;
  1898. /* Read status register data */
  1899. data = *(__IO uint8_t *)deviceaddress;
  1900. /* Return the status */
  1901. if ((data & NAND_ERROR) == NAND_ERROR)
  1902. {
  1903. return NAND_ERROR;
  1904. }
  1905. else if ((data & NAND_READY) == NAND_READY)
  1906. {
  1907. return NAND_READY;
  1908. }
  1909. else
  1910. {
  1911. return NAND_BUSY;
  1912. }
  1913. }
  1914. /**
  1915. * @}
  1916. */
  1917. /**
  1918. * @}
  1919. */
  1920. /**
  1921. * @}
  1922. */
  1923. #endif /* HAL_NAND_MODULE_ENABLED */
  1924. /**
  1925. * @}
  1926. */
  1927. #endif /* FMC_BANK3 */