stm32f1xx_hal_nand.c 71 KB

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