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.
 
 
 

1819 lines
60 KiB

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