stm32f7xx_hal_nand.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  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****/