You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2184 lines
69 KiB

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