stm32f7xx_hal_nand.c 61 KB

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