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.
 
 
 

1860 lines
60 KiB

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