stm32f4xx_hal_nand.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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 FMC/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 HAL_NAND_Info_TypeDef
  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) 2017 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 "stm32f4xx_hal.h"
  94. /** @addtogroup STM32F4xx_HAL_Driver
  95. * @{
  96. */
  97. #ifdef HAL_NAND_MODULE_ENABLED
  98. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
  99. defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
  100. defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx)
  101. /** @defgroup NAND NAND
  102. * @brief NAND HAL module driver
  103. * @{
  104. */
  105. /* Private typedef -----------------------------------------------------------*/
  106. /* Private define ------------------------------------------------------------*/
  107. /** @defgroup NAND_Private_Constants NAND Private Constants
  108. * @{
  109. */
  110. /**
  111. * @}
  112. */
  113. /* Private macro -------------------------------------------------------------*/
  114. /** @defgroup NAND_Private_Macros NAND Private Macros
  115. * @{
  116. */
  117. /**
  118. * @}
  119. */
  120. /* Private variables ---------------------------------------------------------*/
  121. /* Private function prototypes -----------------------------------------------*/
  122. /* Exported functions --------------------------------------------------------*/
  123. /** @defgroup NAND_Exported_Functions NAND Exported Functions
  124. * @{
  125. */
  126. /** @defgroup NAND_Exported_Functions_Group1 Initialization and de-initialization functions
  127. * @brief Initialization and Configuration functions
  128. *
  129. @verbatim
  130. ==============================================================================
  131. ##### NAND Initialization and de-initialization functions #####
  132. ==============================================================================
  133. [..]
  134. This section provides functions allowing to initialize/de-initialize
  135. the NAND memory
  136. @endverbatim
  137. * @{
  138. */
  139. /**
  140. * @brief Perform NAND memory Initialization sequence
  141. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  142. * the configuration information for NAND module.
  143. * @param ComSpace_Timing pointer to Common space timing structure
  144. * @param AttSpace_Timing pointer to Attribute space timing structure
  145. * @retval HAL status
  146. */
  147. HAL_StatusTypeDef HAL_NAND_Init(NAND_HandleTypeDef *hnand, FMC_NAND_PCC_TimingTypeDef *ComSpace_Timing, FMC_NAND_PCC_TimingTypeDef *AttSpace_Timing)
  148. {
  149. /* Check the NAND handle state */
  150. if(hnand == NULL)
  151. {
  152. return HAL_ERROR;
  153. }
  154. if(hnand->State == HAL_NAND_STATE_RESET)
  155. {
  156. /* Allocate lock resource and initialize it */
  157. hnand->Lock = HAL_UNLOCKED;
  158. #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
  159. if(hnand->MspInitCallback == NULL)
  160. {
  161. hnand->MspInitCallback = HAL_NAND_MspInit;
  162. }
  163. hnand->ItCallback = HAL_NAND_ITCallback;
  164. /* Init the low level hardware */
  165. hnand->MspInitCallback(hnand);
  166. #else
  167. /* Initialize the low level hardware (MSP) */
  168. HAL_NAND_MspInit(hnand);
  169. #endif
  170. }
  171. /* Initialize NAND control Interface */
  172. FMC_NAND_Init(hnand->Instance, &(hnand->Init));
  173. /* Initialize NAND common space timing Interface */
  174. FMC_NAND_CommonSpace_Timing_Init(hnand->Instance, ComSpace_Timing, hnand->Init.NandBank);
  175. /* Initialize NAND attribute space timing Interface */
  176. FMC_NAND_AttributeSpace_Timing_Init(hnand->Instance, AttSpace_Timing, hnand->Init.NandBank);
  177. /* Enable the NAND device */
  178. __FMC_NAND_ENABLE(hnand->Instance, hnand->Init.NandBank);
  179. /* Update the NAND controller state */
  180. hnand->State = HAL_NAND_STATE_READY;
  181. return HAL_OK;
  182. }
  183. /**
  184. * @brief Perform NAND memory De-Initialization sequence
  185. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  186. * the configuration information for NAND module.
  187. * @retval HAL status
  188. */
  189. HAL_StatusTypeDef HAL_NAND_DeInit(NAND_HandleTypeDef *hnand)
  190. {
  191. #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
  192. if(hnand->MspDeInitCallback == NULL)
  193. {
  194. hnand->MspDeInitCallback = HAL_NAND_MspDeInit;
  195. }
  196. /* DeInit the low level hardware */
  197. hnand->MspDeInitCallback(hnand);
  198. #else
  199. /* Initialize the low level hardware (MSP) */
  200. HAL_NAND_MspDeInit(hnand);
  201. #endif
  202. /* Configure the NAND registers with their reset values */
  203. FMC_NAND_DeInit(hnand->Instance, hnand->Init.NandBank);
  204. /* Reset the NAND controller state */
  205. hnand->State = HAL_NAND_STATE_RESET;
  206. /* Release Lock */
  207. __HAL_UNLOCK(hnand);
  208. return HAL_OK;
  209. }
  210. /**
  211. * @brief NAND MSP Init
  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_MspInit(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_MspInit could be implemented in the user file
  222. */
  223. }
  224. /**
  225. * @brief NAND MSP DeInit
  226. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  227. * the configuration information for NAND module.
  228. * @retval None
  229. */
  230. __weak void HAL_NAND_MspDeInit(NAND_HandleTypeDef *hnand)
  231. {
  232. /* Prevent unused argument(s) compilation warning */
  233. UNUSED(hnand);
  234. /* NOTE : This function Should not be modified, when the callback is needed,
  235. the HAL_NAND_MspDeInit could be implemented in the user file
  236. */
  237. }
  238. /**
  239. * @brief This function handles NAND device interrupt request.
  240. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  241. * the configuration information for NAND module.
  242. * @retval HAL status
  243. */
  244. void HAL_NAND_IRQHandler(NAND_HandleTypeDef *hnand)
  245. {
  246. /* Check NAND interrupt Rising edge flag */
  247. if(__FMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_RISING_EDGE))
  248. {
  249. /* NAND interrupt callback*/
  250. #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
  251. hnand->ItCallback(hnand);
  252. #else
  253. HAL_NAND_ITCallback(hnand);
  254. #endif
  255. /* Clear NAND interrupt Rising edge pending bit */
  256. __FMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_RISING_EDGE);
  257. }
  258. /* Check NAND interrupt Level flag */
  259. if(__FMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_LEVEL))
  260. {
  261. /* NAND interrupt callback*/
  262. #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
  263. hnand->ItCallback(hnand);
  264. #else
  265. HAL_NAND_ITCallback(hnand);
  266. #endif
  267. /* Clear NAND interrupt Level pending bit */
  268. __FMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_LEVEL);
  269. }
  270. /* Check NAND interrupt Falling edge flag */
  271. if(__FMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_FALLING_EDGE))
  272. {
  273. /* NAND interrupt callback*/
  274. #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
  275. hnand->ItCallback(hnand);
  276. #else
  277. HAL_NAND_ITCallback(hnand);
  278. #endif
  279. /* Clear NAND interrupt Falling edge pending bit */
  280. __FMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_FALLING_EDGE);
  281. }
  282. /* Check NAND interrupt FIFO empty flag */
  283. if(__FMC_NAND_GET_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_FEMPT))
  284. {
  285. /* NAND interrupt callback*/
  286. #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
  287. hnand->ItCallback(hnand);
  288. #else
  289. HAL_NAND_ITCallback(hnand);
  290. #endif
  291. /* Clear NAND interrupt FIFO empty pending bit */
  292. __FMC_NAND_CLEAR_FLAG(hnand->Instance, hnand->Init.NandBank, FMC_FLAG_FEMPT);
  293. }
  294. }
  295. /**
  296. * @brief NAND interrupt feature callback
  297. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  298. * the configuration information for NAND module.
  299. * @retval None
  300. */
  301. __weak void HAL_NAND_ITCallback(NAND_HandleTypeDef *hnand)
  302. {
  303. /* Prevent unused argument(s) compilation warning */
  304. UNUSED(hnand);
  305. /* NOTE : This function Should not be modified, when the callback is needed,
  306. the HAL_NAND_ITCallback could be implemented in the user file
  307. */
  308. }
  309. /**
  310. * @}
  311. */
  312. /** @defgroup NAND_Exported_Functions_Group2 Input and Output functions
  313. * @brief Input Output and memory control functions
  314. *
  315. @verbatim
  316. ==============================================================================
  317. ##### NAND Input and Output functions #####
  318. ==============================================================================
  319. [..]
  320. This section provides functions allowing to use and control the NAND
  321. memory
  322. @endverbatim
  323. * @{
  324. */
  325. /**
  326. * @brief Read the NAND memory electronic signature
  327. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  328. * the configuration information for NAND module.
  329. * @param pNAND_ID NAND ID structure
  330. * @retval HAL status
  331. */
  332. HAL_StatusTypeDef HAL_NAND_Read_ID(NAND_HandleTypeDef *hnand, NAND_IDTypeDef *pNAND_ID)
  333. {
  334. __IO uint32_t data = 0U;
  335. __IO uint32_t data1 = 0U;
  336. uint32_t deviceaddress = 0U;
  337. /* Process Locked */
  338. __HAL_LOCK(hnand);
  339. /* Check the NAND controller state */
  340. if(hnand->State == HAL_NAND_STATE_BUSY)
  341. {
  342. return HAL_BUSY;
  343. }
  344. /* Identify the device address */
  345. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  346. {
  347. deviceaddress = NAND_DEVICE1;
  348. }
  349. else
  350. {
  351. deviceaddress = NAND_DEVICE2;
  352. }
  353. /* Update the NAND controller state */
  354. hnand->State = HAL_NAND_STATE_BUSY;
  355. /* Send Read ID command sequence */
  356. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_READID;
  357. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  358. /* Read the electronic signature from NAND flash */
  359. #ifdef FSMC_PCR2_PWID
  360. if (hnand->Init.MemoryDataWidth == FSMC_NAND_PCC_MEM_BUS_WIDTH_8)
  361. #else /* FMC_PCR2_PWID is defined */
  362. if (hnand->Init.MemoryDataWidth == FMC_NAND_PCC_MEM_BUS_WIDTH_8)
  363. #endif
  364. {
  365. data = *(__IO uint32_t *)deviceaddress;
  366. /* Return the data read */
  367. pNAND_ID->Maker_Id = ADDR_1ST_CYCLE(data);
  368. pNAND_ID->Device_Id = ADDR_2ND_CYCLE(data);
  369. pNAND_ID->Third_Id = ADDR_3RD_CYCLE(data);
  370. pNAND_ID->Fourth_Id = ADDR_4TH_CYCLE(data);
  371. }
  372. else
  373. {
  374. data = *(__IO uint32_t *)deviceaddress;
  375. data1 = *((__IO uint32_t *)deviceaddress + 4U);
  376. /* Return the data read */
  377. pNAND_ID->Maker_Id = ADDR_1ST_CYCLE(data);
  378. pNAND_ID->Device_Id = ADDR_3RD_CYCLE(data);
  379. pNAND_ID->Third_Id = ADDR_1ST_CYCLE(data1);
  380. pNAND_ID->Fourth_Id = ADDR_3RD_CYCLE(data1);
  381. }
  382. /* Update the NAND controller state */
  383. hnand->State = HAL_NAND_STATE_READY;
  384. /* Process unlocked */
  385. __HAL_UNLOCK(hnand);
  386. return HAL_OK;
  387. }
  388. /**
  389. * @brief NAND memory reset
  390. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  391. * the configuration information for NAND module.
  392. * @retval HAL status
  393. */
  394. HAL_StatusTypeDef HAL_NAND_Reset(NAND_HandleTypeDef *hnand)
  395. {
  396. uint32_t deviceaddress = 0U;
  397. /* Process Locked */
  398. __HAL_LOCK(hnand);
  399. /* Check the NAND controller state */
  400. if(hnand->State == HAL_NAND_STATE_BUSY)
  401. {
  402. return HAL_BUSY;
  403. }
  404. /* Identify the device address */
  405. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  406. {
  407. deviceaddress = NAND_DEVICE1;
  408. }
  409. else
  410. {
  411. deviceaddress = NAND_DEVICE2;
  412. }
  413. /* Update the NAND controller state */
  414. hnand->State = HAL_NAND_STATE_BUSY;
  415. /* Send NAND reset command */
  416. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = 0xFF;
  417. /* Update the NAND controller state */
  418. hnand->State = HAL_NAND_STATE_READY;
  419. /* Process unlocked */
  420. __HAL_UNLOCK(hnand);
  421. return HAL_OK;
  422. }
  423. /**
  424. * @brief Configure the device: Enter the physical parameters of the device
  425. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  426. * the configuration information for NAND module.
  427. * @param pDeviceConfig pointer to NAND_DeviceConfigTypeDef structure
  428. * @retval HAL status
  429. */
  430. HAL_StatusTypeDef HAL_NAND_ConfigDevice(NAND_HandleTypeDef *hnand, NAND_DeviceConfigTypeDef *pDeviceConfig)
  431. {
  432. hnand->Config.PageSize = pDeviceConfig->PageSize;
  433. hnand->Config.SpareAreaSize = pDeviceConfig->SpareAreaSize;
  434. hnand->Config.BlockSize = pDeviceConfig->BlockSize;
  435. hnand->Config.BlockNbr = pDeviceConfig->BlockNbr;
  436. hnand->Config.PlaneSize = pDeviceConfig->PlaneSize;
  437. hnand->Config.PlaneNbr = pDeviceConfig->PlaneNbr;
  438. hnand->Config.ExtraCommandEnable = pDeviceConfig->ExtraCommandEnable;
  439. return HAL_OK;
  440. }
  441. /**
  442. * @brief Read Page(s) from NAND memory block (8-bits addressing)
  443. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  444. * the configuration information for NAND module.
  445. * @param pAddress pointer to NAND address structure
  446. * @param pBuffer pointer to destination read buffer
  447. * @param NumPageToRead number of pages to read from block
  448. * @retval HAL status
  449. */
  450. HAL_StatusTypeDef HAL_NAND_Read_Page_8b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer, uint32_t NumPageToRead)
  451. {
  452. __IO uint32_t index = 0U;
  453. uint32_t tickstart = 0U;
  454. uint32_t deviceaddress = 0U, size = 0U, numPagesRead = 0U, nandaddress = 0U;
  455. /* Process Locked */
  456. __HAL_LOCK(hnand);
  457. /* Check the NAND controller state */
  458. if(hnand->State == HAL_NAND_STATE_BUSY)
  459. {
  460. return HAL_BUSY;
  461. }
  462. /* Identify the device address */
  463. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  464. {
  465. deviceaddress = NAND_DEVICE1;
  466. }
  467. else
  468. {
  469. deviceaddress = NAND_DEVICE2;
  470. }
  471. /* Update the NAND controller state */
  472. hnand->State = HAL_NAND_STATE_BUSY;
  473. /* NAND raw address calculation */
  474. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  475. /* Page(s) read loop */
  476. while((NumPageToRead != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  477. {
  478. /* update the buffer size */
  479. size = (hnand->Config.PageSize) + ((hnand->Config.PageSize) * numPagesRead);
  480. /* Send read page command sequence */
  481. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  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)) = 0x00;
  488. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  489. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  490. }
  491. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  492. {
  493. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  494. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  495. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  496. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  497. }
  498. }
  499. else /* (hnand->Config.PageSize) > 512 */
  500. {
  501. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  502. {
  503. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  504. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  505. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  506. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  507. }
  508. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  509. {
  510. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  511. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  512. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  513. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  514. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  515. }
  516. }
  517. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  518. /* Check if an extra command is needed for reading pages */
  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. return HAL_TIMEOUT;
  529. }
  530. }
  531. /* Go back to read mode */
  532. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  533. __DSB();
  534. }
  535. /* Get Data into Buffer */
  536. for(; index < size; index++)
  537. {
  538. *(uint8_t *)pBuffer++ = *(uint8_t *)deviceaddress;
  539. }
  540. /* Increment read pages number */
  541. numPagesRead++;
  542. /* Decrement pages to read */
  543. NumPageToRead--;
  544. /* Increment the NAND address */
  545. nandaddress = (uint32_t)(nandaddress + 1U);
  546. }
  547. /* Update the NAND controller state */
  548. hnand->State = HAL_NAND_STATE_READY;
  549. /* Process unlocked */
  550. __HAL_UNLOCK(hnand);
  551. return HAL_OK;
  552. }
  553. /**
  554. * @brief Read Page(s) from NAND memory block (16-bits addressing)
  555. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  556. * the configuration information for NAND module.
  557. * @param pAddress pointer to NAND address structure
  558. * @param pBuffer pointer to destination read buffer. pBuffer should be 16bits aligned
  559. * @param NumPageToRead number of pages to read from block
  560. * @retval HAL status
  561. */
  562. HAL_StatusTypeDef HAL_NAND_Read_Page_16b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint16_t *pBuffer, uint32_t NumPageToRead)
  563. {
  564. __IO uint32_t index = 0U;
  565. uint32_t tickstart = 0U;
  566. uint32_t deviceaddress = 0U, size = 0U, numPagesRead = 0U, nandaddress = 0U;
  567. /* Process Locked */
  568. __HAL_LOCK(hnand);
  569. /* Check the NAND controller state */
  570. if(hnand->State == HAL_NAND_STATE_BUSY)
  571. {
  572. return HAL_BUSY;
  573. }
  574. /* Identify the device address */
  575. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  576. {
  577. deviceaddress = NAND_DEVICE1;
  578. }
  579. else
  580. {
  581. deviceaddress = NAND_DEVICE2;
  582. }
  583. /* Update the NAND controller state */
  584. hnand->State = HAL_NAND_STATE_BUSY;
  585. /* NAND raw address calculation */
  586. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  587. /* Page(s) read loop */
  588. while((NumPageToRead != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  589. {
  590. /* update the buffer size */
  591. size = (hnand->Config.PageSize) + ((hnand->Config.PageSize) * numPagesRead);
  592. /* Send read page command sequence */
  593. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  594. __DSB();
  595. /* Cards with page size <= 512 bytes */
  596. if((hnand->Config.PageSize) <= 512U)
  597. {
  598. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  599. {
  600. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  601. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  602. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  603. }
  604. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  605. {
  606. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  607. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  608. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  609. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  610. }
  611. }
  612. else /* (hnand->Config.PageSize) > 512 */
  613. {
  614. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  615. {
  616. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  617. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  618. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  619. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  620. }
  621. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  622. {
  623. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  624. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  625. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  626. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  627. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  628. }
  629. }
  630. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  631. if(hnand->Config.ExtraCommandEnable == ENABLE)
  632. {
  633. /* Get tick */
  634. tickstart = HAL_GetTick();
  635. /* Read status until NAND is ready */
  636. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  637. {
  638. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  639. {
  640. return HAL_TIMEOUT;
  641. }
  642. }
  643. /* Go back to read mode */
  644. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  645. }
  646. /* Get Data into Buffer */
  647. for(; index < size; index++)
  648. {
  649. *(uint16_t *)pBuffer++ = *(uint16_t *)deviceaddress;
  650. }
  651. /* Increment read pages number */
  652. numPagesRead++;
  653. /* Decrement pages to read */
  654. NumPageToRead--;
  655. /* Increment the NAND address */
  656. nandaddress = (uint32_t)(nandaddress + 1U);
  657. }
  658. /* Update the NAND controller state */
  659. hnand->State = HAL_NAND_STATE_READY;
  660. /* Process unlocked */
  661. __HAL_UNLOCK(hnand);
  662. return HAL_OK;
  663. }
  664. /**
  665. * @brief Write Page(s) to NAND memory block (8-bits addressing)
  666. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  667. * the configuration information for NAND module.
  668. * @param pAddress pointer to NAND address structure
  669. * @param pBuffer pointer to source buffer to write
  670. * @param NumPageToWrite number of pages to write to block
  671. * @retval HAL status
  672. */
  673. HAL_StatusTypeDef HAL_NAND_Write_Page_8b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer, uint32_t NumPageToWrite)
  674. {
  675. __IO uint32_t index = 0U;
  676. uint32_t tickstart = 0U;
  677. uint32_t deviceaddress = 0U, size = 0U, numPagesWritten = 0U, nandaddress = 0U;
  678. /* Process Locked */
  679. __HAL_LOCK(hnand);
  680. /* Check the NAND controller state */
  681. if(hnand->State == HAL_NAND_STATE_BUSY)
  682. {
  683. return HAL_BUSY;
  684. }
  685. /* Identify the device address */
  686. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  687. {
  688. deviceaddress = NAND_DEVICE1;
  689. }
  690. else
  691. {
  692. deviceaddress = NAND_DEVICE2;
  693. }
  694. /* Update the NAND controller state */
  695. hnand->State = HAL_NAND_STATE_BUSY;
  696. /* NAND raw address calculation */
  697. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  698. /* Page(s) write loop */
  699. while((NumPageToWrite != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  700. {
  701. /* update the buffer size */
  702. size = hnand->Config.PageSize + ((hnand->Config.PageSize) * numPagesWritten);
  703. /* Send write page command sequence */
  704. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  705. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  706. /* Cards with page size <= 512 bytes */
  707. if((hnand->Config.PageSize) <= 512U)
  708. {
  709. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  710. {
  711. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  712. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  713. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  714. }
  715. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  716. {
  717. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  718. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  719. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  720. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  721. }
  722. }
  723. else /* (hnand->Config.PageSize) > 512 */
  724. {
  725. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  726. {
  727. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  728. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  729. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  730. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  731. }
  732. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  733. {
  734. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  735. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  736. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  737. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  738. __DSB();
  739. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  740. __DSB();
  741. }
  742. }
  743. /* Write data to memory */
  744. for(; index < size; index++)
  745. {
  746. *(__IO uint8_t *)deviceaddress = *(uint8_t *)pBuffer++;
  747. }
  748. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  749. /* Get tick */
  750. tickstart = HAL_GetTick();
  751. /* Read status until NAND is ready */
  752. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  753. {
  754. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  755. {
  756. return HAL_TIMEOUT;
  757. }
  758. }
  759. /* Increment written pages number */
  760. numPagesWritten++;
  761. /* Decrement pages to write */
  762. NumPageToWrite--;
  763. /* Increment the NAND address */
  764. nandaddress = (uint32_t)(nandaddress + 1U);
  765. }
  766. /* Update the NAND controller state */
  767. hnand->State = HAL_NAND_STATE_READY;
  768. /* Process unlocked */
  769. __HAL_UNLOCK(hnand);
  770. return HAL_OK;
  771. }
  772. /**
  773. * @brief Write Page(s) to NAND memory block (16-bits addressing)
  774. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  775. * the configuration information for NAND module.
  776. * @param pAddress pointer to NAND address structure
  777. * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned
  778. * @param NumPageToWrite number of pages to write to block
  779. * @retval HAL status
  780. */
  781. HAL_StatusTypeDef HAL_NAND_Write_Page_16b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint16_t *pBuffer, uint32_t NumPageToWrite)
  782. {
  783. __IO uint32_t index = 0U;
  784. uint32_t tickstart = 0U;
  785. uint32_t deviceaddress = 0U, size = 0U, numPagesWritten = 0U, nandaddress = 0U;
  786. /* Process Locked */
  787. __HAL_LOCK(hnand);
  788. /* Check the NAND controller state */
  789. if(hnand->State == HAL_NAND_STATE_BUSY)
  790. {
  791. return HAL_BUSY;
  792. }
  793. /* Identify the device address */
  794. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  795. {
  796. deviceaddress = NAND_DEVICE1;
  797. }
  798. else
  799. {
  800. deviceaddress = NAND_DEVICE2;
  801. }
  802. /* Update the NAND controller state */
  803. hnand->State = HAL_NAND_STATE_BUSY;
  804. /* NAND raw address calculation */
  805. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  806. /* Page(s) write loop */
  807. while((NumPageToWrite != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  808. {
  809. /* update the buffer size */
  810. size = (hnand->Config.PageSize) + ((hnand->Config.PageSize) * numPagesWritten);
  811. /* Send write page command sequence */
  812. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  813. __DSB();
  814. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  815. __DSB();
  816. /* Cards with page size <= 512 bytes */
  817. if((hnand->Config.PageSize) <= 512U)
  818. {
  819. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  820. {
  821. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  822. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  823. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  824. }
  825. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  826. {
  827. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  828. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  829. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  830. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  831. }
  832. }
  833. else /* (hnand->Config.PageSize) > 512 */
  834. {
  835. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  836. {
  837. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  838. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  839. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  840. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  841. }
  842. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  843. {
  844. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  845. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  846. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  847. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  848. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  849. }
  850. }
  851. /* Write data to memory */
  852. for(; index < size; index++)
  853. {
  854. *(__IO uint16_t *)deviceaddress = *(uint16_t *)pBuffer++;
  855. }
  856. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  857. /* Get tick */
  858. tickstart = HAL_GetTick();
  859. /* Read status until NAND is ready */
  860. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  861. {
  862. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  863. {
  864. return HAL_TIMEOUT;
  865. }
  866. }
  867. /* Increment written pages number */
  868. numPagesWritten++;
  869. /* Decrement pages to write */
  870. NumPageToWrite--;
  871. /* Increment the NAND address */
  872. nandaddress = (uint32_t)(nandaddress + 1U);
  873. }
  874. /* Update the NAND controller state */
  875. hnand->State = HAL_NAND_STATE_READY;
  876. /* Process unlocked */
  877. __HAL_UNLOCK(hnand);
  878. return HAL_OK;
  879. }
  880. /**
  881. * @brief Read Spare area(s) from NAND memory
  882. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  883. * the configuration information for NAND module.
  884. * @param pAddress pointer to NAND address structure
  885. * @param pBuffer pointer to source buffer to write
  886. * @param NumSpareAreaToRead Number of spare area to read
  887. * @retval HAL status
  888. */
  889. HAL_StatusTypeDef HAL_NAND_Read_SpareArea_8b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer, uint32_t NumSpareAreaToRead)
  890. {
  891. __IO uint32_t index = 0U;
  892. uint32_t tickstart = 0U;
  893. uint32_t deviceaddress = 0U, size = 0U, numSpareAreaRead = 0U, nandaddress = 0U, columnaddress = 0U;
  894. /* Process Locked */
  895. __HAL_LOCK(hnand);
  896. /* Check the NAND controller state */
  897. if(hnand->State == HAL_NAND_STATE_BUSY)
  898. {
  899. return HAL_BUSY;
  900. }
  901. /* Identify the device address */
  902. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  903. {
  904. deviceaddress = NAND_DEVICE1;
  905. }
  906. else
  907. {
  908. deviceaddress = NAND_DEVICE2;
  909. }
  910. /* Update the NAND controller state */
  911. hnand->State = HAL_NAND_STATE_BUSY;
  912. /* NAND raw address calculation */
  913. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  914. /* Column in page address */
  915. columnaddress = COLUMN_ADDRESS(hnand);
  916. /* Spare area(s) read loop */
  917. while((NumSpareAreaToRead != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  918. {
  919. /* update the buffer size */
  920. size = (hnand->Config.SpareAreaSize) + ((hnand->Config.SpareAreaSize) * numSpareAreaRead);
  921. /* Cards with page size <= 512 bytes */
  922. if((hnand->Config.PageSize) <= 512U)
  923. {
  924. /* Send read spare area command sequence */
  925. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  926. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  927. {
  928. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  929. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  930. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  931. }
  932. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  933. {
  934. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  935. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  936. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  937. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  938. }
  939. }
  940. else /* (hnand->Config.PageSize) > 512 */
  941. {
  942. /* Send read spare area command sequence */
  943. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  944. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  945. {
  946. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  947. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  948. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  949. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  950. }
  951. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  952. {
  953. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  954. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  955. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  956. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  957. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  958. }
  959. }
  960. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  961. if(hnand->Config.ExtraCommandEnable == ENABLE)
  962. {
  963. /* Get tick */
  964. tickstart = HAL_GetTick();
  965. /* Read status until NAND is ready */
  966. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  967. {
  968. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  969. {
  970. return HAL_TIMEOUT;
  971. }
  972. }
  973. /* Go back to read mode */
  974. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  975. }
  976. /* Get Data into Buffer */
  977. for(; index < size; index++)
  978. {
  979. *(uint8_t *)pBuffer++ = *(uint8_t *)deviceaddress;
  980. }
  981. /* Increment read spare areas number */
  982. numSpareAreaRead++;
  983. /* Decrement spare areas to read */
  984. NumSpareAreaToRead--;
  985. /* Increment the NAND address */
  986. nandaddress = (uint32_t)(nandaddress + 1U);
  987. }
  988. /* Update the NAND controller state */
  989. hnand->State = HAL_NAND_STATE_READY;
  990. /* Process unlocked */
  991. __HAL_UNLOCK(hnand);
  992. return HAL_OK;
  993. }
  994. /**
  995. * @brief Read Spare area(s) from NAND memory (16-bits addressing)
  996. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  997. * the configuration information for NAND module.
  998. * @param pAddress pointer to NAND address structure
  999. * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned.
  1000. * @param NumSpareAreaToRead Number of spare area to read
  1001. * @retval HAL status
  1002. */
  1003. HAL_StatusTypeDef HAL_NAND_Read_SpareArea_16b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint16_t *pBuffer, uint32_t NumSpareAreaToRead)
  1004. {
  1005. __IO uint32_t index = 0U;
  1006. uint32_t tickstart = 0U;
  1007. uint32_t deviceaddress = 0U, size = 0U, numSpareAreaRead = 0U, nandaddress = 0U, columnaddress = 0U;
  1008. /* Process Locked */
  1009. __HAL_LOCK(hnand);
  1010. /* Check the NAND controller state */
  1011. if(hnand->State == HAL_NAND_STATE_BUSY)
  1012. {
  1013. return HAL_BUSY;
  1014. }
  1015. /* Identify the device address */
  1016. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  1017. {
  1018. deviceaddress = NAND_DEVICE1;
  1019. }
  1020. else
  1021. {
  1022. deviceaddress = NAND_DEVICE2;
  1023. }
  1024. /* Update the NAND controller state */
  1025. hnand->State = HAL_NAND_STATE_BUSY;
  1026. /* NAND raw address calculation */
  1027. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1028. /* Column in page address */
  1029. columnaddress = (uint32_t)(COLUMN_ADDRESS(hnand) * 2U);
  1030. /* Spare area(s) read loop */
  1031. while((NumSpareAreaToRead != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1032. {
  1033. /* update the buffer size */
  1034. size = (hnand->Config.SpareAreaSize) + ((hnand->Config.SpareAreaSize) * numSpareAreaRead);
  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. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  1041. {
  1042. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  1043. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1044. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1045. }
  1046. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1047. {
  1048. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  1049. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1050. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1051. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1052. }
  1053. }
  1054. else /* (hnand->Config.PageSize) > 512 */
  1055. {
  1056. /* Send read spare area command sequence */
  1057. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1058. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  1059. {
  1060. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1061. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1062. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1063. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1064. }
  1065. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1066. {
  1067. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1068. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1069. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1070. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1071. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1072. }
  1073. }
  1074. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_TRUE1;
  1075. if(hnand->Config.ExtraCommandEnable == ENABLE)
  1076. {
  1077. /* Get tick */
  1078. tickstart = HAL_GetTick();
  1079. /* Read status until NAND is ready */
  1080. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  1081. {
  1082. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  1083. {
  1084. return HAL_TIMEOUT;
  1085. }
  1086. }
  1087. /* Go back to read mode */
  1088. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = ((uint8_t)0x00);
  1089. }
  1090. /* Get Data into Buffer */
  1091. for(; index < size; index++)
  1092. {
  1093. *(uint16_t *)pBuffer++ = *(uint16_t *)deviceaddress;
  1094. }
  1095. /* Increment read spare areas number */
  1096. numSpareAreaRead++;
  1097. /* Decrement spare areas to read */
  1098. NumSpareAreaToRead--;
  1099. /* Increment the NAND address */
  1100. nandaddress = (uint32_t)(nandaddress + 1U);
  1101. }
  1102. /* Update the NAND controller state */
  1103. hnand->State = HAL_NAND_STATE_READY;
  1104. /* Process unlocked */
  1105. __HAL_UNLOCK(hnand);
  1106. return HAL_OK;
  1107. }
  1108. /**
  1109. * @brief Write Spare area(s) to NAND memory
  1110. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1111. * the configuration information for NAND module.
  1112. * @param pAddress pointer to NAND address structure
  1113. * @param pBuffer pointer to source buffer to write
  1114. * @param NumSpareAreaTowrite number of spare areas to write to block
  1115. * @retval HAL status
  1116. */
  1117. HAL_StatusTypeDef HAL_NAND_Write_SpareArea_8b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint8_t *pBuffer, uint32_t NumSpareAreaTowrite)
  1118. {
  1119. __IO uint32_t index = 0U;
  1120. uint32_t tickstart = 0U;
  1121. uint32_t deviceaddress = 0U, size = 0U, numSpareAreaWritten = 0U, nandaddress = 0U, columnaddress = 0U;
  1122. /* Process Locked */
  1123. __HAL_LOCK(hnand);
  1124. /* Check the NAND controller state */
  1125. if(hnand->State == HAL_NAND_STATE_BUSY)
  1126. {
  1127. return HAL_BUSY;
  1128. }
  1129. /* Identify the device address */
  1130. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  1131. {
  1132. deviceaddress = NAND_DEVICE1;
  1133. }
  1134. else
  1135. {
  1136. deviceaddress = NAND_DEVICE2;
  1137. }
  1138. /* Update the FMC_NAND controller state */
  1139. hnand->State = HAL_NAND_STATE_BUSY;
  1140. /* Page address calculation */
  1141. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1142. /* Column in page address */
  1143. columnaddress = COLUMN_ADDRESS(hnand);
  1144. /* Spare area(s) write loop */
  1145. while((NumSpareAreaTowrite != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1146. {
  1147. /* update the buffer size */
  1148. size = (hnand->Config.SpareAreaSize) + ((hnand->Config.SpareAreaSize) * numSpareAreaWritten);
  1149. /* Cards with page size <= 512 bytes */
  1150. if((hnand->Config.PageSize) <= 512U)
  1151. {
  1152. /* Send write Spare area command sequence */
  1153. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1154. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1155. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  1156. {
  1157. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  1158. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1159. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1160. }
  1161. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1162. {
  1163. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  1164. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1165. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1166. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1167. }
  1168. }
  1169. else /* (hnand->Config.PageSize) > 512 */
  1170. {
  1171. /* Send write Spare area command sequence */
  1172. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1173. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1174. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  1175. {
  1176. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1177. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1178. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1179. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1180. }
  1181. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1182. {
  1183. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1184. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1185. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1186. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1187. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1188. }
  1189. }
  1190. /* Write data to memory */
  1191. for(; index < size; index++)
  1192. {
  1193. *(__IO uint8_t *)deviceaddress = *(uint8_t *)pBuffer++;
  1194. }
  1195. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  1196. /* Get tick */
  1197. tickstart = HAL_GetTick();
  1198. /* Read status until NAND is ready */
  1199. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  1200. {
  1201. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  1202. {
  1203. return HAL_TIMEOUT;
  1204. }
  1205. }
  1206. /* Increment written spare areas number */
  1207. numSpareAreaWritten++;
  1208. /* Decrement spare areas to write */
  1209. NumSpareAreaTowrite--;
  1210. /* Increment the NAND address */
  1211. nandaddress = (uint32_t)(nandaddress + 1U);
  1212. }
  1213. /* Update the NAND controller state */
  1214. hnand->State = HAL_NAND_STATE_READY;
  1215. /* Process unlocked */
  1216. __HAL_UNLOCK(hnand);
  1217. return HAL_OK;
  1218. }
  1219. /**
  1220. * @brief Write Spare area(s) to NAND memory (16-bits addressing)
  1221. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1222. * the configuration information for NAND module.
  1223. * @param pAddress pointer to NAND address structure
  1224. * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned.
  1225. * @param NumSpareAreaTowrite number of spare areas to write to block
  1226. * @retval HAL status
  1227. */
  1228. HAL_StatusTypeDef HAL_NAND_Write_SpareArea_16b(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress, uint16_t *pBuffer, uint32_t NumSpareAreaTowrite)
  1229. {
  1230. __IO uint32_t index = 0U;
  1231. uint32_t tickstart = 0U;
  1232. uint32_t deviceaddress = 0U, size = 0U, numSpareAreaWritten = 0U, nandaddress = 0U, columnaddress = 0U;
  1233. /* Process Locked */
  1234. __HAL_LOCK(hnand);
  1235. /* Check the NAND controller state */
  1236. if(hnand->State == HAL_NAND_STATE_BUSY)
  1237. {
  1238. return HAL_BUSY;
  1239. }
  1240. /* Identify the device address */
  1241. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  1242. {
  1243. deviceaddress = NAND_DEVICE1;
  1244. }
  1245. else
  1246. {
  1247. deviceaddress = NAND_DEVICE2;
  1248. }
  1249. /* Update the FMC_NAND controller state */
  1250. hnand->State = HAL_NAND_STATE_BUSY;
  1251. /* NAND raw address calculation */
  1252. nandaddress = ARRAY_ADDRESS(pAddress, hnand);
  1253. /* Column in page address */
  1254. columnaddress = (uint32_t)(COLUMN_ADDRESS(hnand) * 2U);
  1255. /* Spare area(s) write loop */
  1256. while((NumSpareAreaTowrite != 0U) && (nandaddress < ((hnand->Config.BlockSize) * (hnand->Config.BlockNbr))))
  1257. {
  1258. /* update the buffer size */
  1259. size = (hnand->Config.SpareAreaSize) + ((hnand->Config.SpareAreaSize) * numSpareAreaWritten);
  1260. /* Cards with page size <= 512 bytes */
  1261. if((hnand->Config.PageSize) <= 512U)
  1262. {
  1263. /* Send write Spare area command sequence */
  1264. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_C;
  1265. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1266. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  1267. {
  1268. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  1269. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1270. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1271. }
  1272. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1273. {
  1274. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = 0x00;
  1275. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1276. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1277. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1278. }
  1279. }
  1280. else /* (hnand->Config.PageSize) > 512 */
  1281. {
  1282. /* Send write Spare area command sequence */
  1283. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_AREA_A;
  1284. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE0;
  1285. if (((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) <= 65535U)
  1286. {
  1287. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1288. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1289. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1290. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1291. }
  1292. else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
  1293. {
  1294. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_1ST_CYCLE(columnaddress);
  1295. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = COLUMN_2ND_CYCLE(columnaddress);
  1296. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(nandaddress);
  1297. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(nandaddress);
  1298. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(nandaddress);
  1299. }
  1300. }
  1301. /* Write data to memory */
  1302. for(; index < size; index++)
  1303. {
  1304. *(__IO uint16_t *)deviceaddress = *(uint16_t *)pBuffer++;
  1305. }
  1306. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_WRITE_TRUE1;
  1307. /* Get tick */
  1308. tickstart = HAL_GetTick();
  1309. /* Read status until NAND is ready */
  1310. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  1311. {
  1312. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  1313. {
  1314. return HAL_TIMEOUT;
  1315. }
  1316. }
  1317. /* Increment written spare areas number */
  1318. numSpareAreaWritten++;
  1319. /* Decrement spare areas to write */
  1320. NumSpareAreaTowrite--;
  1321. /* Increment the NAND address */
  1322. nandaddress = (uint32_t)(nandaddress + 1U);
  1323. }
  1324. /* Update the NAND controller state */
  1325. hnand->State = HAL_NAND_STATE_READY;
  1326. /* Process unlocked */
  1327. __HAL_UNLOCK(hnand);
  1328. return HAL_OK;
  1329. }
  1330. /**
  1331. * @brief NAND memory Block erase
  1332. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1333. * the configuration information for NAND module.
  1334. * @param pAddress pointer to NAND address structure
  1335. * @retval HAL status
  1336. */
  1337. HAL_StatusTypeDef HAL_NAND_Erase_Block(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress)
  1338. {
  1339. uint32_t deviceaddress = 0U;
  1340. uint32_t tickstart = 0U;
  1341. /* Process Locked */
  1342. __HAL_LOCK(hnand);
  1343. /* Check the NAND controller state */
  1344. if(hnand->State == HAL_NAND_STATE_BUSY)
  1345. {
  1346. return HAL_BUSY;
  1347. }
  1348. /* Identify the device address */
  1349. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  1350. {
  1351. deviceaddress = NAND_DEVICE1;
  1352. }
  1353. else
  1354. {
  1355. deviceaddress = NAND_DEVICE2;
  1356. }
  1357. /* Update the NAND controller state */
  1358. hnand->State = HAL_NAND_STATE_BUSY;
  1359. /* Send Erase block command sequence */
  1360. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_ERASE0;
  1361. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_1ST_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1362. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_2ND_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1363. *(__IO uint8_t *)((uint32_t)(deviceaddress | ADDR_AREA)) = ADDR_3RD_CYCLE(ARRAY_ADDRESS(pAddress, hnand));
  1364. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_ERASE1;
  1365. /* Update the NAND controller state */
  1366. hnand->State = HAL_NAND_STATE_READY;
  1367. /* Get tick */
  1368. tickstart = HAL_GetTick();
  1369. /* Read status until NAND is ready */
  1370. while(HAL_NAND_Read_Status(hnand) != NAND_READY)
  1371. {
  1372. if((HAL_GetTick() - tickstart ) > NAND_WRITE_TIMEOUT)
  1373. {
  1374. /* Process unlocked */
  1375. __HAL_UNLOCK(hnand);
  1376. return HAL_TIMEOUT;
  1377. }
  1378. }
  1379. /* Process unlocked */
  1380. __HAL_UNLOCK(hnand);
  1381. return HAL_OK;
  1382. }
  1383. /**
  1384. * @brief NAND memory read status
  1385. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1386. * the configuration information for NAND module.
  1387. * @retval NAND status
  1388. */
  1389. uint32_t HAL_NAND_Read_Status(NAND_HandleTypeDef *hnand)
  1390. {
  1391. uint32_t data = 0U;
  1392. uint32_t deviceaddress = 0U;
  1393. /* Identify the device address */
  1394. if(hnand->Init.NandBank == FMC_NAND_BANK2)
  1395. {
  1396. deviceaddress = NAND_DEVICE1;
  1397. }
  1398. else
  1399. {
  1400. deviceaddress = NAND_DEVICE2;
  1401. }
  1402. /* Send Read status operation command */
  1403. *(__IO uint8_t *)((uint32_t)(deviceaddress | CMD_AREA)) = NAND_CMD_STATUS;
  1404. /* Read status register data */
  1405. data = *(__IO uint8_t *)deviceaddress;
  1406. /* Return the status */
  1407. if((data & NAND_ERROR) == NAND_ERROR)
  1408. {
  1409. return NAND_ERROR;
  1410. }
  1411. else if((data & NAND_READY) == NAND_READY)
  1412. {
  1413. return NAND_READY;
  1414. }
  1415. return NAND_BUSY;
  1416. }
  1417. /**
  1418. * @brief Increment the NAND memory address
  1419. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1420. * the configuration information for NAND module.
  1421. * @param pAddress pointer to NAND address structure
  1422. * @retval The new status of the increment address operation. It can be:
  1423. * - NAND_VALID_ADDRESS: When the new address is valid address
  1424. * - NAND_INVALID_ADDRESS: When the new address is invalid address
  1425. */
  1426. uint32_t HAL_NAND_Address_Inc(NAND_HandleTypeDef *hnand, NAND_AddressTypeDef *pAddress)
  1427. {
  1428. uint32_t status = NAND_VALID_ADDRESS;
  1429. /* Increment page address */
  1430. pAddress->Page++;
  1431. /* Check NAND address is valid */
  1432. if(pAddress->Page == hnand->Config.BlockSize)
  1433. {
  1434. pAddress->Page = 0U;
  1435. pAddress->Block++;
  1436. if(pAddress->Block == hnand->Config.PlaneSize)
  1437. {
  1438. pAddress->Block = 0U;
  1439. pAddress->Plane++;
  1440. if(pAddress->Plane == (hnand->Config.PlaneNbr))
  1441. {
  1442. status = NAND_INVALID_ADDRESS;
  1443. }
  1444. }
  1445. }
  1446. return (status);
  1447. }
  1448. #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
  1449. /**
  1450. * @brief Register a User NAND Callback
  1451. * To be used instead of the weak (surcharged) predefined callback
  1452. * @param hnand : NAND handle
  1453. * @param CallbackId : ID of the callback to be registered
  1454. * This parameter can be one of the following values:
  1455. * @arg @ref HAL_NAND_MSP_INIT_CB_ID NAND MspInit callback ID
  1456. * @arg @ref HAL_NAND_MSP_DEINIT_CB_ID NAND MspDeInit callback ID
  1457. * @arg @ref HAL_NAND_IT_CB_ID NAND IT callback ID
  1458. * @param pCallback : pointer to the Callback function
  1459. * @retval status
  1460. */
  1461. HAL_StatusTypeDef HAL_NAND_RegisterCallback (NAND_HandleTypeDef *hnand, HAL_NAND_CallbackIDTypeDef CallbackId, pNAND_CallbackTypeDef pCallback)
  1462. {
  1463. HAL_StatusTypeDef status = HAL_OK;
  1464. if(pCallback == NULL)
  1465. {
  1466. return HAL_ERROR;
  1467. }
  1468. /* Process locked */
  1469. __HAL_LOCK(hnand);
  1470. if(hnand->State == HAL_NAND_STATE_READY)
  1471. {
  1472. switch (CallbackId)
  1473. {
  1474. case HAL_NAND_MSP_INIT_CB_ID :
  1475. hnand->MspInitCallback = pCallback;
  1476. break;
  1477. case HAL_NAND_MSP_DEINIT_CB_ID :
  1478. hnand->MspDeInitCallback = pCallback;
  1479. break;
  1480. case HAL_NAND_IT_CB_ID :
  1481. hnand->ItCallback = pCallback;
  1482. break;
  1483. default :
  1484. /* update return status */
  1485. status = HAL_ERROR;
  1486. break;
  1487. }
  1488. }
  1489. else if(hnand->State == HAL_NAND_STATE_RESET)
  1490. {
  1491. switch (CallbackId)
  1492. {
  1493. case HAL_NAND_MSP_INIT_CB_ID :
  1494. hnand->MspInitCallback = pCallback;
  1495. break;
  1496. case HAL_NAND_MSP_DEINIT_CB_ID :
  1497. hnand->MspDeInitCallback = pCallback;
  1498. break;
  1499. default :
  1500. /* update return status */
  1501. status = HAL_ERROR;
  1502. break;
  1503. }
  1504. }
  1505. else
  1506. {
  1507. /* update return status */
  1508. status = HAL_ERROR;
  1509. }
  1510. /* Release Lock */
  1511. __HAL_UNLOCK(hnand);
  1512. return status;
  1513. }
  1514. /**
  1515. * @brief Unregister a User NAND Callback
  1516. * NAND Callback is redirected to the weak (surcharged) predefined callback
  1517. * @param hnand : NAND handle
  1518. * @param CallbackId : ID of the callback to be unregistered
  1519. * This parameter can be one of the following values:
  1520. * @arg @ref HAL_NAND_MSP_INIT_CB_ID NAND MspInit callback ID
  1521. * @arg @ref HAL_NAND_MSP_DEINIT_CB_ID NAND MspDeInit callback ID
  1522. * @arg @ref HAL_NAND_IT_CB_ID NAND IT callback ID
  1523. * @retval status
  1524. */
  1525. HAL_StatusTypeDef HAL_NAND_UnRegisterCallback (NAND_HandleTypeDef *hnand, HAL_NAND_CallbackIDTypeDef CallbackId)
  1526. {
  1527. HAL_StatusTypeDef status = HAL_OK;
  1528. /* Process locked */
  1529. __HAL_LOCK(hnand);
  1530. if(hnand->State == HAL_NAND_STATE_READY)
  1531. {
  1532. switch (CallbackId)
  1533. {
  1534. case HAL_NAND_MSP_INIT_CB_ID :
  1535. hnand->MspInitCallback = HAL_NAND_MspInit;
  1536. break;
  1537. case HAL_NAND_MSP_DEINIT_CB_ID :
  1538. hnand->MspDeInitCallback = HAL_NAND_MspDeInit;
  1539. break;
  1540. case HAL_NAND_IT_CB_ID :
  1541. hnand->ItCallback = HAL_NAND_ITCallback;
  1542. break;
  1543. default :
  1544. /* update return status */
  1545. status = HAL_ERROR;
  1546. break;
  1547. }
  1548. }
  1549. else if(hnand->State == HAL_NAND_STATE_RESET)
  1550. {
  1551. switch (CallbackId)
  1552. {
  1553. case HAL_NAND_MSP_INIT_CB_ID :
  1554. hnand->MspInitCallback = HAL_NAND_MspInit;
  1555. break;
  1556. case HAL_NAND_MSP_DEINIT_CB_ID :
  1557. hnand->MspDeInitCallback = HAL_NAND_MspDeInit;
  1558. break;
  1559. default :
  1560. /* update return status */
  1561. status = HAL_ERROR;
  1562. break;
  1563. }
  1564. }
  1565. else
  1566. {
  1567. /* update return status */
  1568. status = HAL_ERROR;
  1569. }
  1570. /* Release Lock */
  1571. __HAL_UNLOCK(hnand);
  1572. return status;
  1573. }
  1574. #endif
  1575. /**
  1576. * @}
  1577. */
  1578. /** @defgroup NAND_Exported_Functions_Group3 Peripheral Control functions
  1579. * @brief management functions
  1580. *
  1581. @verbatim
  1582. ==============================================================================
  1583. ##### NAND Control functions #####
  1584. ==============================================================================
  1585. [..]
  1586. This subsection provides a set of functions allowing to control dynamically
  1587. the NAND interface.
  1588. @endverbatim
  1589. * @{
  1590. */
  1591. /**
  1592. * @brief Enables dynamically NAND ECC feature.
  1593. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1594. * the configuration information for NAND module.
  1595. * @retval HAL status
  1596. */
  1597. HAL_StatusTypeDef HAL_NAND_ECC_Enable(NAND_HandleTypeDef *hnand)
  1598. {
  1599. /* Check the NAND controller state */
  1600. if(hnand->State == HAL_NAND_STATE_BUSY)
  1601. {
  1602. return HAL_BUSY;
  1603. }
  1604. /* Update the NAND state */
  1605. hnand->State = HAL_NAND_STATE_BUSY;
  1606. /* Enable ECC feature */
  1607. FMC_NAND_ECC_Enable(hnand->Instance, hnand->Init.NandBank);
  1608. /* Update the NAND state */
  1609. hnand->State = HAL_NAND_STATE_READY;
  1610. return HAL_OK;
  1611. }
  1612. /**
  1613. * @brief Disables dynamically FMC_NAND ECC feature.
  1614. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1615. * the configuration information for NAND module.
  1616. * @retval HAL status
  1617. */
  1618. HAL_StatusTypeDef HAL_NAND_ECC_Disable(NAND_HandleTypeDef *hnand)
  1619. {
  1620. /* Check the NAND controller state */
  1621. if(hnand->State == HAL_NAND_STATE_BUSY)
  1622. {
  1623. return HAL_BUSY;
  1624. }
  1625. /* Update the NAND state */
  1626. hnand->State = HAL_NAND_STATE_BUSY;
  1627. /* Disable ECC feature */
  1628. FMC_NAND_ECC_Disable(hnand->Instance, hnand->Init.NandBank);
  1629. /* Update the NAND state */
  1630. hnand->State = HAL_NAND_STATE_READY;
  1631. return HAL_OK;
  1632. }
  1633. /**
  1634. * @brief Disables dynamically NAND ECC feature.
  1635. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1636. * the configuration information for NAND module.
  1637. * @param ECCval pointer to ECC value
  1638. * @param Timeout maximum timeout to wait
  1639. * @retval HAL status
  1640. */
  1641. HAL_StatusTypeDef HAL_NAND_GetECC(NAND_HandleTypeDef *hnand, uint32_t *ECCval, uint32_t Timeout)
  1642. {
  1643. HAL_StatusTypeDef status = HAL_OK;
  1644. /* Check the NAND controller state */
  1645. if(hnand->State == HAL_NAND_STATE_BUSY)
  1646. {
  1647. return HAL_BUSY;
  1648. }
  1649. /* Update the NAND state */
  1650. hnand->State = HAL_NAND_STATE_BUSY;
  1651. /* Get NAND ECC value */
  1652. status = FMC_NAND_GetECC(hnand->Instance, ECCval, hnand->Init.NandBank, Timeout);
  1653. /* Update the NAND state */
  1654. hnand->State = HAL_NAND_STATE_READY;
  1655. return status;
  1656. }
  1657. /**
  1658. * @}
  1659. */
  1660. /** @defgroup NAND_Exported_Functions_Group4 Peripheral State functions
  1661. * @brief Peripheral State functions
  1662. *
  1663. @verbatim
  1664. ==============================================================================
  1665. ##### NAND State functions #####
  1666. ==============================================================================
  1667. [..]
  1668. This subsection permits to get in run-time the status of the NAND controller
  1669. and the data flow.
  1670. @endverbatim
  1671. * @{
  1672. */
  1673. /**
  1674. * @brief return the NAND state
  1675. * @param hnand pointer to a NAND_HandleTypeDef structure that contains
  1676. * the configuration information for NAND module.
  1677. * @retval HAL state
  1678. */
  1679. HAL_NAND_StateTypeDef HAL_NAND_GetState(NAND_HandleTypeDef *hnand)
  1680. {
  1681. return hnand->State;
  1682. }
  1683. /**
  1684. * @}
  1685. */
  1686. /**
  1687. * @}
  1688. */
  1689. /**
  1690. * @}
  1691. */
  1692. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx ||\
  1693. STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||\
  1694. STM32F446xx || STM32F469xx || STM32F479xx */
  1695. #endif /* HAL_NAND_MODULE_ENABLED */
  1696. /**
  1697. * @}
  1698. */
  1699. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/