2 ******************************************************************************
3 * @file stm32g4xx_hal_nand.c
4 * @author MCD Application Team
5 * @brief NAND HAL module driver.
6 * This file provides a generic firmware to drive NAND memories mounted
10 ==============================================================================
11 ##### How to use this driver #####
12 ==============================================================================
14 This driver is a generic layered driver which contains a set of APIs used to
15 control NAND flash memories. It uses the FMC layer functions to interface
16 with NAND devices. This driver is used as follows:
18 (+) NAND flash memory configuration sequence using the function HAL_NAND_Init()
19 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.
25 (+) Access NAND flash memory by read/write operations using the functions
26 HAL_NAND_Read_Page_8b()/HAL_NAND_Read_SpareArea_8b(),
27 HAL_NAND_Write_Page_8b()/HAL_NAND_Write_SpareArea_8b(),
28 HAL_NAND_Read_Page_16b()/HAL_NAND_Read_SpareArea_16b(),
29 HAL_NAND_Write_Page_16b()/HAL_NAND_Write_SpareArea_16b()
30 to read/write page(s)/spare area(s). These functions use specific device
31 information (Block, page size..) predefined by the user in the NAND_DeviceConfigTypeDef
32 structure. The read/write address information is contained by the Nand_Address_Typedef
33 structure passed as parameter.
35 (+) Perform NAND flash Reset chip operation using the function HAL_NAND_Reset().
37 (+) Perform NAND flash erase block operation using the function HAL_NAND_Erase_Block().
38 The erase block address information is contained in the Nand_Address_Typedef
39 structure passed as parameter.
41 (+) Read the NAND flash status operation using the function HAL_NAND_Read_Status().
43 (+) You can also control the NAND device by calling the control APIs HAL_NAND_ECC_Enable()/
44 HAL_NAND_ECC_Disable() to respectively enable/disable the ECC code correction
45 feature or the function HAL_NAND_GetECC() to get the ECC correction code.
47 (+) You can monitor the NAND device HAL state by calling the function
51 (@) This driver is a set of generic APIs which handle standard NAND flash operations.
52 If a NAND flash device contains different operations and/or implementations,
53 it should be implemented separately.
55 *** Callback registration ***
56 =============================================
58 The compilation define USE_HAL_NAND_REGISTER_CALLBACKS when set to 1
59 allows the user to configure dynamically the driver callbacks.
61 Use Functions @ref HAL_NAND_RegisterCallback() to register a user callback,
62 it allows to register following callbacks:
63 (+) MspInitCallback : NAND MspInit.
64 (+) MspDeInitCallback : NAND MspDeInit.
65 This function takes as parameters the HAL peripheral handle, the Callback ID
66 and a pointer to the user callback function.
68 Use function @ref HAL_NAND_UnRegisterCallback() to reset a callback to the default
69 weak (surcharged) function. It allows to reset following callbacks:
70 (+) MspInitCallback : NAND MspInit.
71 (+) MspDeInitCallback : NAND MspDeInit.
72 This function) takes as parameters the HAL peripheral handle and the Callback ID.
74 By default, after the @ref HAL_NAND_Init and if the state is HAL_NAND_STATE_RESET
75 all callbacks are reset to the corresponding legacy weak (surcharged) functions.
76 Exception done for MspInit and MspDeInit callbacks that are respectively
77 reset to the legacy weak (surcharged) functions in the @ref HAL_NAND_Init
78 and @ref HAL_NAND_DeInit only when these callbacks are null (not registered beforehand).
79 If not, MspInit or MspDeInit are not null, the @ref HAL_NAND_Init and @ref HAL_NAND_DeInit
80 keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
82 Callbacks can be registered/unregistered in READY state only.
83 Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
84 in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
85 during the Init/DeInit.
86 In that case first register the MspInit/MspDeInit user callbacks
87 using @ref HAL_NAND_RegisterCallback before calling @ref HAL_NAND_DeInit
88 or @ref HAL_NAND_Init function.
90 When The compilation define USE_HAL_NAND_REGISTER_CALLBACKS is set to 0 or
91 not defined, the callback registering feature is not available
92 and weak (surcharged) callbacks are used.
95 ******************************************************************************
98 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
99 * All rights reserved.</center></h2>
101 * This software component is licensed by ST under BSD 3-Clause license,
102 * the "License"; You may not use this file except in compliance with the
103 * License. You may obtain a copy of the License at:
104 * opensource.org/licenses/BSD-3-Clause
106 ******************************************************************************
109 /* Includes ------------------------------------------------------------------*/
110 #include "stm32g4xx_hal.h"
112 #if defined(FMC_BANK3)
114 /** @addtogroup STM32G4xx_HAL_Driver
118 #ifdef HAL_NAND_MODULE_ENABLED
120 /** @defgroup NAND NAND
121 * @brief NAND HAL module driver
125 /* Private typedef -----------------------------------------------------------*/
126 /* Private Constants ------------------------------------------------------------*/
127 /* Private macro -------------------------------------------------------------*/
128 /* Private variables ---------------------------------------------------------*/
129 /* Private function prototypes -----------------------------------------------*/
130 /* Exported functions ---------------------------------------------------------*/
132 /** @defgroup NAND_Exported_Functions NAND Exported Functions
136 /** @defgroup NAND_Exported_Functions_Group1 Initialization and de-initialization functions
137 * @brief Initialization and Configuration functions
140 ==============================================================================
141 ##### NAND Initialization and de-initialization functions #####
142 ==============================================================================
144 This section provides functions allowing to initialize/de-initialize
152 * @brief Perform NAND memory Initialization sequence
153 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
154 * the configuration information for NAND module.
155 * @param ComSpace_Timing pointer to Common space timing structure
156 * @param AttSpace_Timing pointer to Attribute space timing structure
159 HAL_StatusTypeDef
HAL_NAND_Init(NAND_HandleTypeDef
*hnand
, FMC_NAND_PCC_TimingTypeDef
*ComSpace_Timing
, FMC_NAND_PCC_TimingTypeDef
*AttSpace_Timing
)
161 /* Check the NAND handle state */
167 if (hnand
->State
== HAL_NAND_STATE_RESET
)
169 /* Allocate lock resource and initialize it */
170 hnand
->Lock
= HAL_UNLOCKED
;
172 #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
173 if(hnand
->MspInitCallback
== NULL
)
175 hnand
->MspInitCallback
= HAL_NAND_MspInit
;
177 hnand
->ItCallback
= HAL_NAND_ITCallback
;
179 /* Init the low level hardware */
180 hnand
->MspInitCallback(hnand
);
182 /* Initialize the low level hardware (MSP) */
183 HAL_NAND_MspInit(hnand
);
187 /* Initialize NAND control Interface */
188 (void)FMC_NAND_Init(hnand
->Instance
, &(hnand
->Init
));
190 /* Initialize NAND common space timing Interface */
191 (void)FMC_NAND_CommonSpace_Timing_Init(hnand
->Instance
, ComSpace_Timing
, hnand
->Init
.NandBank
);
193 /* Initialize NAND attribute space timing Interface */
194 (void)FMC_NAND_AttributeSpace_Timing_Init(hnand
->Instance
, AttSpace_Timing
, hnand
->Init
.NandBank
);
196 /* Enable the NAND device */
197 __FMC_NAND_ENABLE(hnand
->Instance
);
199 /* Update the NAND controller state */
200 hnand
->State
= HAL_NAND_STATE_READY
;
206 * @brief Perform NAND memory De-Initialization sequence
207 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
208 * the configuration information for NAND module.
211 HAL_StatusTypeDef
HAL_NAND_DeInit(NAND_HandleTypeDef
*hnand
)
213 #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
214 if(hnand
->MspDeInitCallback
== NULL
)
216 hnand
->MspDeInitCallback
= HAL_NAND_MspDeInit
;
219 /* DeInit the low level hardware */
220 hnand
->MspDeInitCallback(hnand
);
222 /* Initialize the low level hardware (MSP) */
223 HAL_NAND_MspDeInit(hnand
);
226 /* Configure the NAND registers with their reset values */
227 (void)FMC_NAND_DeInit(hnand
->Instance
, hnand
->Init
.NandBank
);
229 /* Reset the NAND controller state */
230 hnand
->State
= HAL_NAND_STATE_RESET
;
239 * @brief NAND MSP Init
240 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
241 * the configuration information for NAND module.
244 __weak
void HAL_NAND_MspInit(NAND_HandleTypeDef
*hnand
)
246 /* Prevent unused argument(s) compilation warning */
249 /* NOTE : This function Should not be modified, when the callback is needed,
250 the HAL_NAND_MspInit could be implemented in the user file
255 * @brief NAND MSP DeInit
256 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
257 * the configuration information for NAND module.
260 __weak
void HAL_NAND_MspDeInit(NAND_HandleTypeDef
*hnand
)
262 /* Prevent unused argument(s) compilation warning */
265 /* NOTE : This function Should not be modified, when the callback is needed,
266 the HAL_NAND_MspDeInit could be implemented in the user file
272 * @brief This function handles NAND device interrupt request.
273 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
274 * the configuration information for NAND module.
277 void HAL_NAND_IRQHandler(NAND_HandleTypeDef
*hnand
)
279 /* Check NAND interrupt Rising edge flag */
280 if (__FMC_NAND_GET_FLAG(hnand
->Instance
, hnand
->Init
.NandBank
, FMC_FLAG_RISING_EDGE
))
282 /* NAND interrupt callback*/
283 #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
284 hnand
->ItCallback(hnand
);
286 HAL_NAND_ITCallback(hnand
);
289 /* Clear NAND interrupt Rising edge pending bit */
290 __FMC_NAND_CLEAR_FLAG(hnand
->Instance
, FMC_FLAG_RISING_EDGE
);
293 /* Check NAND interrupt Level flag */
294 if (__FMC_NAND_GET_FLAG(hnand
->Instance
, hnand
->Init
.NandBank
, FMC_FLAG_LEVEL
))
296 /* NAND interrupt callback*/
297 #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
298 hnand
->ItCallback(hnand
);
300 HAL_NAND_ITCallback(hnand
);
303 /* Clear NAND interrupt Level pending bit */
304 __FMC_NAND_CLEAR_FLAG(hnand
->Instance
, FMC_FLAG_LEVEL
);
307 /* Check NAND interrupt Falling edge flag */
308 if (__FMC_NAND_GET_FLAG(hnand
->Instance
, hnand
->Init
.NandBank
, FMC_FLAG_FALLING_EDGE
))
310 /* NAND interrupt callback*/
311 #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
312 hnand
->ItCallback(hnand
);
314 HAL_NAND_ITCallback(hnand
);
317 /* Clear NAND interrupt Falling edge pending bit */
318 __FMC_NAND_CLEAR_FLAG(hnand
->Instance
, FMC_FLAG_FALLING_EDGE
);
321 /* Check NAND interrupt FIFO empty flag */
322 if (__FMC_NAND_GET_FLAG(hnand
->Instance
, hnand
->Init
.NandBank
, FMC_FLAG_FEMPT
))
324 /* NAND interrupt callback*/
325 #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
326 hnand
->ItCallback(hnand
);
328 HAL_NAND_ITCallback(hnand
);
331 /* Clear NAND interrupt FIFO empty pending bit */
332 __FMC_NAND_CLEAR_FLAG(hnand
->Instance
, FMC_FLAG_FEMPT
);
338 * @brief NAND interrupt feature callback
339 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
340 * the configuration information for NAND module.
343 __weak
void HAL_NAND_ITCallback(NAND_HandleTypeDef
*hnand
)
345 /* Prevent unused argument(s) compilation warning */
348 /* NOTE : This function Should not be modified, when the callback is needed,
349 the HAL_NAND_ITCallback could be implemented in the user file
357 /** @defgroup NAND_Exported_Functions_Group2 Input and Output functions
358 * @brief Input Output and memory control functions
361 ==============================================================================
362 ##### NAND Input and Output functions #####
363 ==============================================================================
365 This section provides functions allowing to use and control the NAND
373 * @brief Read the NAND memory electronic signature
374 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
375 * the configuration information for NAND module.
376 * @param pNAND_ID NAND ID structure
379 HAL_StatusTypeDef
HAL_NAND_Read_ID(NAND_HandleTypeDef
*hnand
, NAND_IDTypeDef
*pNAND_ID
)
381 __IO
uint32_t data
= 0;
382 __IO
uint32_t data1
= 0;
383 uint32_t deviceAddress
;
385 /* Check the NAND controller state */
386 if (hnand
->State
== HAL_NAND_STATE_BUSY
)
390 else if (hnand
->State
== HAL_NAND_STATE_READY
)
395 /* Update the NAND controller state */
396 hnand
->State
= HAL_NAND_STATE_BUSY
;
398 /* Identify the device address */
399 deviceAddress
= NAND_DEVICE
;
401 /* Send Read ID command sequence */
402 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_READID
;
404 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00;
407 /* Read the electronic signature from NAND flash */
408 if (hnand
->Init
.MemoryDataWidth
== FMC_NAND_MEM_BUS_WIDTH_8
)
410 data
= *(__IO
uint32_t *)deviceAddress
;
412 /* Return the data read */
413 pNAND_ID
->Maker_Id
= ADDR_1ST_CYCLE(data
);
414 pNAND_ID
->Device_Id
= ADDR_2ND_CYCLE(data
);
415 pNAND_ID
->Third_Id
= ADDR_3RD_CYCLE(data
);
416 pNAND_ID
->Fourth_Id
= ADDR_4TH_CYCLE(data
);
420 data
= *(__IO
uint32_t *)deviceAddress
;
421 data1
= *((__IO
uint32_t *)deviceAddress
+ 4);
423 /* Return the data read */
424 pNAND_ID
->Maker_Id
= ADDR_1ST_CYCLE(data
);
425 pNAND_ID
->Device_Id
= ADDR_3RD_CYCLE(data
);
426 pNAND_ID
->Third_Id
= ADDR_1ST_CYCLE(data1
);
427 pNAND_ID
->Fourth_Id
= ADDR_3RD_CYCLE(data1
);
430 /* Update the NAND controller state */
431 hnand
->State
= HAL_NAND_STATE_READY
;
433 /* Process unlocked */
445 * @brief NAND memory reset
446 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
447 * the configuration information for NAND module.
450 HAL_StatusTypeDef
HAL_NAND_Reset(NAND_HandleTypeDef
*hnand
)
452 uint32_t deviceAddress
;
454 /* Check the NAND controller state */
455 if (hnand
->State
== HAL_NAND_STATE_BUSY
)
459 else if (hnand
->State
== HAL_NAND_STATE_READY
)
464 /* Update the NAND controller state */
465 hnand
->State
= HAL_NAND_STATE_BUSY
;
467 /* Identify the device address */
468 deviceAddress
= NAND_DEVICE
;
470 /* Send NAND reset command */
471 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = 0xFF;
473 /* Update the NAND controller state */
474 hnand
->State
= HAL_NAND_STATE_READY
;
476 /* Process unlocked */
489 * @brief Configure the device: Enter the physical parameters of the device
490 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
491 * the configuration information for NAND module.
492 * @param pDeviceConfig pointer to NAND_DeviceConfigTypeDef structure
495 HAL_StatusTypeDef
HAL_NAND_ConfigDevice(NAND_HandleTypeDef
*hnand
, NAND_DeviceConfigTypeDef
*pDeviceConfig
)
497 hnand
->Config
.PageSize
= pDeviceConfig
->PageSize
;
498 hnand
->Config
.SpareAreaSize
= pDeviceConfig
->SpareAreaSize
;
499 hnand
->Config
.BlockSize
= pDeviceConfig
->BlockSize
;
500 hnand
->Config
.BlockNbr
= pDeviceConfig
->BlockNbr
;
501 hnand
->Config
.PlaneSize
= pDeviceConfig
->PlaneSize
;
502 hnand
->Config
.PlaneNbr
= pDeviceConfig
->PlaneNbr
;
503 hnand
->Config
.ExtraCommandEnable
= pDeviceConfig
->ExtraCommandEnable
;
510 * @brief Read Page(s) from NAND memory block (8-bits addressing)
511 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
512 * the configuration information for NAND module.
513 * @param pAddress pointer to NAND address structure
514 * @param pBuffer pointer to destination read buffer
515 * @param NumPageToRead number of pages to read from block
518 HAL_StatusTypeDef
HAL_NAND_Read_Page_8b(NAND_HandleTypeDef
*hnand
, NAND_AddressTypeDef
*pAddress
, uint8_t *pBuffer
, uint32_t NumPageToRead
)
522 uint32_t deviceAddress
, numPagesRead
= 0U, nandAddress
, nbpages
= NumPageToRead
;
523 uint8_t * buff
= pBuffer
;
525 /* Check the NAND controller state */
526 if (hnand
->State
== HAL_NAND_STATE_BUSY
)
530 else if (hnand
->State
== HAL_NAND_STATE_READY
)
535 /* Update the NAND controller state */
536 hnand
->State
= HAL_NAND_STATE_BUSY
;
538 /* Identify the device address */
539 deviceAddress
= NAND_DEVICE
;
541 /* NAND raw address calculation */
542 nandAddress
= ARRAY_ADDRESS(pAddress
, hnand
);
544 /* Page(s) read loop */
545 while ((nbpages
!= 0U) && (nandAddress
< ((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
))))
547 /* Send read page command sequence */
548 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_AREA_A
;
551 /* Cards with page size <= 512 bytes */
552 if ((hnand
->Config
.PageSize
) <= 512U)
554 if (((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
)) <= 65535U)
556 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
558 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
560 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
563 else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
565 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
567 *(__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
);
571 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(nandAddress
);
575 else /* (hnand->Config.PageSize) > 512 */
577 if (((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
)) <= 65535U)
579 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
581 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
583 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
585 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
588 else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
590 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
592 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
594 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
596 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
598 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(nandAddress
);
603 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_AREA_TRUE1
;
607 if (hnand
->Config
.ExtraCommandEnable
== ENABLE
)
610 tickstart
= HAL_GetTick();
612 /* Read status until NAND is ready */
613 while (HAL_NAND_Read_Status(hnand
) != NAND_READY
)
615 if ((HAL_GetTick() - tickstart
) > NAND_WRITE_TIMEOUT
)
617 /* Update the NAND controller state */
618 hnand
->State
= HAL_NAND_STATE_ERROR
;
620 /* Process unlocked */
627 /* Go back to read mode */
628 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = ((uint8_t)0x00U
);
632 /* Get Data into Buffer */
633 for (index
= 0U; index
< hnand
->Config
.PageSize
; index
++)
635 *buff
= *(uint8_t *)deviceAddress
;
639 /* Increment read pages number */
642 /* Decrement pages to read */
645 /* Increment the NAND address */
646 nandAddress
= (uint32_t)(nandAddress
+ 1U);
649 /* Update the NAND controller state */
650 hnand
->State
= HAL_NAND_STATE_READY
;
652 /* Process unlocked */
665 * @brief Read Page(s) from NAND memory block (16-bits addressing)
666 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
667 * the configuration information for NAND module.
668 * @param pAddress pointer to NAND address structure
669 * @param pBuffer pointer to destination read buffer. pBuffer should be 16bits aligned
670 * @param NumPageToRead number of pages to read from block
673 HAL_StatusTypeDef
HAL_NAND_Read_Page_16b(NAND_HandleTypeDef
*hnand
, NAND_AddressTypeDef
*pAddress
, uint16_t *pBuffer
, uint32_t NumPageToRead
)
677 uint32_t deviceAddress
, numPagesRead
= 0, nandAddress
, nbpages
= NumPageToRead
;
678 uint16_t * buff
= pBuffer
;
680 /* Check the NAND controller state */
681 if (hnand
->State
== HAL_NAND_STATE_BUSY
)
685 else if (hnand
->State
== HAL_NAND_STATE_READY
)
690 /* Update the NAND controller state */
691 hnand
->State
= HAL_NAND_STATE_BUSY
;
693 /* Identify the device address */
694 deviceAddress
= NAND_DEVICE
;
696 /* NAND raw address calculation */
697 nandAddress
= ARRAY_ADDRESS(pAddress
, hnand
);
699 /* Page(s) read loop */
700 while ((nbpages
!= 0U) && (nandAddress
< ((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
))))
702 /* Send read page command sequence */
703 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_AREA_A
;
706 /* Cards with page size <= 512 bytes */
707 if ((hnand
->Config
.PageSize
) <= 512U)
709 if (((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
)) <= 65535U)
711 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
713 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
715 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
718 else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
720 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
722 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
724 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
726 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(nandAddress
);
730 else /* (hnand->Config.PageSize) > 512 */
732 if (((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
)) <= 65535U)
734 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
736 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
738 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
740 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
743 else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
745 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
747 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
749 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
751 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
753 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(nandAddress
);
758 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_AREA_TRUE1
;
761 if (hnand
->Config
.ExtraCommandEnable
== ENABLE
)
764 tickstart
= HAL_GetTick();
766 /* Read status until NAND is ready */
767 while (HAL_NAND_Read_Status(hnand
) != NAND_READY
)
769 if ((HAL_GetTick() - tickstart
) > NAND_WRITE_TIMEOUT
)
771 /* Update the NAND controller state */
772 hnand
->State
= HAL_NAND_STATE_ERROR
;
774 /* Process unlocked */
781 /* Go back to read mode */
782 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = ((uint8_t)0x00U
);
786 /* Get Data into Buffer */
787 for (index
= 0U; index
< hnand
->Config
.PageSize
; index
++)
789 *buff
= *(uint16_t *)deviceAddress
;
793 /* Increment read pages number */
796 /* Decrement pages to read */
799 /* Increment the NAND address */
800 nandAddress
= (uint32_t)(nandAddress
+ 1U);
803 /* Update the NAND controller state */
804 hnand
->State
= HAL_NAND_STATE_READY
;
806 /* Process unlocked */
818 * @brief Write Page(s) to NAND memory block (8-bits addressing)
819 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
820 * the configuration information for NAND module.
821 * @param pAddress pointer to NAND address structure
822 * @param pBuffer pointer to source buffer to write
823 * @param NumPageToWrite number of pages to write to block
826 HAL_StatusTypeDef
HAL_NAND_Write_Page_8b(NAND_HandleTypeDef
*hnand
, NAND_AddressTypeDef
*pAddress
, uint8_t *pBuffer
, uint32_t NumPageToWrite
)
830 uint32_t deviceAddress
, numPagesWritten
= 0, nandAddress
, nbpages
= NumPageToWrite
;
831 uint8_t * buff
= pBuffer
;
833 /* Check the NAND controller state */
834 if (hnand
->State
== HAL_NAND_STATE_BUSY
)
838 else if (hnand
->State
== HAL_NAND_STATE_READY
)
843 /* Update the NAND controller state */
844 hnand
->State
= HAL_NAND_STATE_BUSY
;
846 /* Identify the device address */
847 deviceAddress
= NAND_DEVICE
;
849 /* NAND raw address calculation */
850 nandAddress
= ARRAY_ADDRESS(pAddress
, hnand
);
852 /* Page(s) write loop */
853 while ((nbpages
!= 0U) && (nandAddress
< ((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
))))
855 /* Send write page command sequence */
856 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_AREA_A
;
858 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_WRITE0
;
861 /* Cards with page size <= 512 bytes */
862 if ((hnand
->Config
.PageSize
) <= 512U)
864 if (((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
)) <= 65535U)
866 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
868 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
870 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
873 else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
875 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
877 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
879 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
881 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(nandAddress
);
885 else /* (hnand->Config.PageSize) > 512 */
887 if (((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
)) <= 65535U)
889 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
891 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
893 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
895 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
898 else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
900 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
902 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
904 *(__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
);
908 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(nandAddress
);
913 /* Write data to memory */
914 for (index
= 0U; index
< hnand
->Config
.PageSize
; index
++)
916 *(__IO
uint8_t *)deviceAddress
= *buff
;
921 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_WRITE_TRUE1
;
925 tickstart
= HAL_GetTick();
927 /* Read status until NAND is ready */
928 while (HAL_NAND_Read_Status(hnand
) != NAND_READY
)
930 if ((HAL_GetTick() - tickstart
) > NAND_WRITE_TIMEOUT
)
932 /* Update the NAND controller state */
933 hnand
->State
= HAL_NAND_STATE_ERROR
;
935 /* Process unlocked */
942 /* Increment written pages number */
945 /* Decrement pages to write */
948 /* Increment the NAND address */
949 nandAddress
= (uint32_t)(nandAddress
+ 1U);
952 /* Update the NAND controller state */
953 hnand
->State
= HAL_NAND_STATE_READY
;
955 /* Process unlocked */
967 * @brief Write Page(s) to NAND memory block (16-bits addressing)
968 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
969 * the configuration information for NAND module.
970 * @param pAddress pointer to NAND address structure
971 * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned
972 * @param NumPageToWrite number of pages to write to block
975 HAL_StatusTypeDef
HAL_NAND_Write_Page_16b(NAND_HandleTypeDef
*hnand
, NAND_AddressTypeDef
*pAddress
, uint16_t *pBuffer
, uint32_t NumPageToWrite
)
979 uint32_t deviceAddress
, numPagesWritten
= 0, nandAddress
, nbpages
= NumPageToWrite
;
980 uint16_t * buff
= pBuffer
;
982 /* Check the NAND controller state */
983 if (hnand
->State
== HAL_NAND_STATE_BUSY
)
987 else if (hnand
->State
== HAL_NAND_STATE_READY
)
992 /* Update the NAND controller state */
993 hnand
->State
= HAL_NAND_STATE_BUSY
;
995 /* Identify the device address */
996 deviceAddress
= NAND_DEVICE
;
998 /* NAND raw address calculation */
999 nandAddress
= ARRAY_ADDRESS(pAddress
, hnand
);
1001 /* Page(s) write loop */
1002 while ((nbpages
!= 0U) && (nandAddress
< ((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
))))
1004 /* Send write page command sequence */
1005 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_AREA_A
;
1007 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_WRITE0
;
1010 /* Cards with page size <= 512 bytes */
1011 if ((hnand
->Config
.PageSize
) <= 512U)
1013 if (((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
)) <= 65535U)
1015 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
1017 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1019 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1022 else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
1024 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
1026 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1028 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1030 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(nandAddress
);
1034 else /* (hnand->Config.PageSize) > 512 */
1036 if (((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
)) <= 65535U)
1038 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
1040 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
1042 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1044 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1047 else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
1049 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
1051 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
1053 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1055 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1057 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(nandAddress
);
1062 /* Write data to memory */
1063 for (index
= 0U; index
< hnand
->Config
.PageSize
; index
++)
1065 *(__IO
uint16_t *)deviceAddress
= *buff
;
1070 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_WRITE_TRUE1
;
1074 tickstart
= HAL_GetTick();
1076 /* Read status until NAND is ready */
1077 while (HAL_NAND_Read_Status(hnand
) != NAND_READY
)
1079 if ((HAL_GetTick() - tickstart
) > NAND_WRITE_TIMEOUT
)
1081 /* Update the NAND controller state */
1082 hnand
->State
= HAL_NAND_STATE_ERROR
;
1084 /* Process unlocked */
1085 __HAL_UNLOCK(hnand
);
1091 /* Increment written pages number */
1094 /* Decrement pages to write */
1097 /* Increment the NAND address */
1098 nandAddress
= (uint32_t)(nandAddress
+ 1U);
1101 /* Update the NAND controller state */
1102 hnand
->State
= HAL_NAND_STATE_READY
;
1104 /* Process unlocked */
1105 __HAL_UNLOCK(hnand
);
1116 * @brief Read Spare area(s) from NAND memory (8-bits addressing)
1117 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
1118 * the configuration information for NAND module.
1119 * @param pAddress pointer to NAND address structure
1120 * @param pBuffer pointer to source buffer to write
1121 * @param NumSpareAreaToRead Number of spare area to read
1122 * @retval HAL status
1124 HAL_StatusTypeDef
HAL_NAND_Read_SpareArea_8b(NAND_HandleTypeDef
*hnand
, NAND_AddressTypeDef
*pAddress
, uint8_t *pBuffer
, uint32_t NumSpareAreaToRead
)
1128 uint32_t deviceAddress
, numSpareAreaRead
= 0, nandAddress
, columnAddress
, nbspare
= NumSpareAreaToRead
;
1129 uint8_t * buff
= pBuffer
;
1131 /* Check the NAND controller state */
1132 if (hnand
->State
== HAL_NAND_STATE_BUSY
)
1136 else if (hnand
->State
== HAL_NAND_STATE_READY
)
1138 /* Process Locked */
1141 /* Update the NAND controller state */
1142 hnand
->State
= HAL_NAND_STATE_BUSY
;
1144 /* Identify the device address */
1145 deviceAddress
= NAND_DEVICE
;
1147 /* NAND raw address calculation */
1148 nandAddress
= ARRAY_ADDRESS(pAddress
, hnand
);
1150 /* Column in page address */
1151 columnAddress
= COLUMN_ADDRESS(hnand
);
1153 /* Spare area(s) read loop */
1154 while ((nbspare
!= 0U) && (nandAddress
< ((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
))))
1156 /* Cards with page size <= 512 bytes */
1157 if ((hnand
->Config
.PageSize
) <= 512U)
1159 /* Send read spare area command sequence */
1160 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_AREA_C
;
1163 if (((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
)) <= 65535U)
1165 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
1167 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1169 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1172 else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
1174 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
1176 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1178 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1180 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(nandAddress
);
1184 else /* (hnand->Config.PageSize) > 512 */
1186 /* Send read spare area command sequence */
1187 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_AREA_A
;
1190 if (((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
)) <= 65535U)
1192 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = COLUMN_1ST_CYCLE(columnAddress
);
1194 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = COLUMN_2ND_CYCLE(columnAddress
);
1196 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1198 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1201 else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
1203 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = COLUMN_1ST_CYCLE(columnAddress
);
1205 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = COLUMN_2ND_CYCLE(columnAddress
);
1207 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1209 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1211 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(nandAddress
);
1216 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_AREA_TRUE1
;
1219 if (hnand
->Config
.ExtraCommandEnable
== ENABLE
)
1222 tickstart
= HAL_GetTick();
1224 /* Read status until NAND is ready */
1225 while (HAL_NAND_Read_Status(hnand
) != NAND_READY
)
1227 if ((HAL_GetTick() - tickstart
) > NAND_WRITE_TIMEOUT
)
1229 /* Update the NAND controller state */
1230 hnand
->State
= HAL_NAND_STATE_ERROR
;
1232 /* Process unlocked */
1233 __HAL_UNLOCK(hnand
);
1239 /* Go back to read mode */
1240 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = ((uint8_t)0x00U
);
1244 /* Get Data into Buffer */
1245 for (index
= 0U; index
< hnand
->Config
.SpareAreaSize
; index
++)
1247 *buff
= *(uint8_t *)deviceAddress
;
1251 /* Increment read spare areas number */
1254 /* Decrement spare areas to read */
1257 /* Increment the NAND address */
1258 nandAddress
= (uint32_t)(nandAddress
+ 1U);
1261 /* Update the NAND controller state */
1262 hnand
->State
= HAL_NAND_STATE_READY
;
1264 /* Process unlocked */
1265 __HAL_UNLOCK(hnand
);
1276 * @brief Read Spare area(s) from NAND memory (16-bits addressing)
1277 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
1278 * the configuration information for NAND module.
1279 * @param pAddress pointer to NAND address structure
1280 * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned.
1281 * @param NumSpareAreaToRead Number of spare area to read
1282 * @retval HAL status
1284 HAL_StatusTypeDef
HAL_NAND_Read_SpareArea_16b(NAND_HandleTypeDef
*hnand
, NAND_AddressTypeDef
*pAddress
, uint16_t *pBuffer
, uint32_t NumSpareAreaToRead
)
1288 uint32_t deviceAddress
, numSpareAreaRead
= 0, nandAddress
, columnAddress
, nbspare
= NumSpareAreaToRead
;
1289 uint16_t * buff
= pBuffer
;
1291 /* Check the NAND controller state */
1292 if (hnand
->State
== HAL_NAND_STATE_BUSY
)
1296 else if (hnand
->State
== HAL_NAND_STATE_READY
)
1298 /* Process Locked */
1301 /* Update the NAND controller state */
1302 hnand
->State
= HAL_NAND_STATE_BUSY
;
1304 /* Identify the device address */
1305 deviceAddress
= NAND_DEVICE
;
1307 /* NAND raw address calculation */
1308 nandAddress
= ARRAY_ADDRESS(pAddress
, hnand
);
1310 /* Column in page address */
1311 columnAddress
= (uint32_t)(COLUMN_ADDRESS(hnand
) * 2U);
1313 /* Spare area(s) read loop */
1314 while ((nbspare
!= 0U) && (nandAddress
< ((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
))))
1316 /* Cards with page size <= 512 bytes */
1317 if ((hnand
->Config
.PageSize
) <= 512U)
1319 /* Send read spare area command sequence */
1320 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_AREA_C
;
1323 if (((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
)) <= 65535U)
1325 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
1327 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1329 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1332 else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
1334 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
1336 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1338 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1340 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(nandAddress
);
1344 else /* (hnand->Config.PageSize) > 512 */
1346 /* Send read spare area command sequence */
1347 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_AREA_A
;
1350 if (((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
)) <= 65535U)
1352 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = COLUMN_1ST_CYCLE(columnAddress
);
1354 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = COLUMN_2ND_CYCLE(columnAddress
);
1356 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1358 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1361 else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
1363 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = COLUMN_1ST_CYCLE(columnAddress
);
1365 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = COLUMN_2ND_CYCLE(columnAddress
);
1367 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1369 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1371 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(nandAddress
);
1376 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_AREA_TRUE1
;
1379 if (hnand
->Config
.ExtraCommandEnable
== ENABLE
)
1382 tickstart
= HAL_GetTick();
1384 /* Read status until NAND is ready */
1385 while (HAL_NAND_Read_Status(hnand
) != NAND_READY
)
1387 if ((HAL_GetTick() - tickstart
) > NAND_WRITE_TIMEOUT
)
1389 /* Update the NAND controller state */
1390 hnand
->State
= HAL_NAND_STATE_ERROR
;
1392 /* Process unlocked */
1393 __HAL_UNLOCK(hnand
);
1399 /* Go back to read mode */
1400 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = ((uint8_t)0x00U
);
1404 /* Get Data into Buffer */
1405 for (index
= 0U; index
< hnand
->Config
.SpareAreaSize
; index
++)
1407 *buff
= *(uint16_t *)deviceAddress
;
1411 /* Increment read spare areas number */
1414 /* Decrement spare areas to read */
1417 /* Increment the NAND address */
1418 nandAddress
= (uint32_t)(nandAddress
+ 1U);
1421 /* Update the NAND controller state */
1422 hnand
->State
= HAL_NAND_STATE_READY
;
1424 /* Process unlocked */
1425 __HAL_UNLOCK(hnand
);
1436 * @brief Write Spare area(s) to NAND memory (8-bits addressing)
1437 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
1438 * the configuration information for NAND module.
1439 * @param pAddress pointer to NAND address structure
1440 * @param pBuffer pointer to source buffer to write
1441 * @param NumSpareAreaTowrite number of spare areas to write to block
1442 * @retval HAL status
1444 HAL_StatusTypeDef
HAL_NAND_Write_SpareArea_8b(NAND_HandleTypeDef
*hnand
, NAND_AddressTypeDef
*pAddress
, uint8_t *pBuffer
, uint32_t NumSpareAreaTowrite
)
1448 uint32_t deviceAddress
, numSpareAreaWritten
= 0, nandAddress
, columnAddress
, nbspare
= NumSpareAreaTowrite
;
1449 uint8_t * buff
= pBuffer
;
1451 /* Check the NAND controller state */
1452 if (hnand
->State
== HAL_NAND_STATE_BUSY
)
1456 else if (hnand
->State
== HAL_NAND_STATE_READY
)
1458 /* Process Locked */
1461 /* Update the NAND controller state */
1462 hnand
->State
= HAL_NAND_STATE_BUSY
;
1464 /* Identify the device address */
1465 deviceAddress
= NAND_DEVICE
;
1467 /* Page address calculation */
1468 nandAddress
= ARRAY_ADDRESS(pAddress
, hnand
);
1470 /* Column in page address */
1471 columnAddress
= COLUMN_ADDRESS(hnand
);
1473 /* Spare area(s) write loop */
1474 while ((nbspare
!= 0U) && (nandAddress
< ((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
))))
1476 /* Cards with page size <= 512 bytes */
1477 if ((hnand
->Config
.PageSize
) <= 512U)
1479 /* Send write Spare area command sequence */
1480 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_AREA_C
;
1482 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_WRITE0
;
1485 if (((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
)) <= 65535U)
1487 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
1489 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1491 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1494 else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
1496 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
1498 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1500 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1502 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(nandAddress
);
1506 else /* (hnand->Config.PageSize) > 512 */
1508 /* Send write Spare area command sequence */
1509 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_AREA_A
;
1511 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_WRITE0
;
1514 if (((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
)) <= 65535U)
1516 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = COLUMN_1ST_CYCLE(columnAddress
);
1518 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = COLUMN_2ND_CYCLE(columnAddress
);
1520 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1522 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1525 else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
1527 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = COLUMN_1ST_CYCLE(columnAddress
);
1529 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = COLUMN_2ND_CYCLE(columnAddress
);
1531 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1533 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1535 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(nandAddress
);
1540 /* Write data to memory */
1541 for (index
= 0U; index
< hnand
->Config
.SpareAreaSize
; index
++)
1543 *(__IO
uint8_t *)deviceAddress
= *buff
;
1548 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_WRITE_TRUE1
;
1552 tickstart
= HAL_GetTick();
1554 /* Read status until NAND is ready */
1555 while (HAL_NAND_Read_Status(hnand
) != NAND_READY
)
1557 if ((HAL_GetTick() - tickstart
) > NAND_WRITE_TIMEOUT
)
1559 /* Update the NAND controller state */
1560 hnand
->State
= HAL_NAND_STATE_ERROR
;
1562 /* Process unlocked */
1563 __HAL_UNLOCK(hnand
);
1569 /* Increment written spare areas number */
1570 numSpareAreaWritten
++;
1572 /* Decrement spare areas to write */
1575 /* Increment the NAND address */
1576 nandAddress
= (uint32_t)(nandAddress
+ 1U);
1579 /* Update the NAND controller state */
1580 hnand
->State
= HAL_NAND_STATE_READY
;
1582 /* Process unlocked */
1583 __HAL_UNLOCK(hnand
);
1594 * @brief Write Spare area(s) to NAND memory (16-bits addressing)
1595 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
1596 * the configuration information for NAND module.
1597 * @param pAddress pointer to NAND address structure
1598 * @param pBuffer pointer to source buffer to write. pBuffer should be 16bits aligned.
1599 * @param NumSpareAreaTowrite number of spare areas to write to block
1600 * @retval HAL status
1602 HAL_StatusTypeDef
HAL_NAND_Write_SpareArea_16b(NAND_HandleTypeDef
*hnand
, NAND_AddressTypeDef
*pAddress
, uint16_t *pBuffer
, uint32_t NumSpareAreaTowrite
)
1606 uint32_t deviceAddress
, numSpareAreaWritten
= 0, nandAddress
, columnAddress
, nbspare
= NumSpareAreaTowrite
;
1607 uint16_t * buff
= pBuffer
;
1609 /* Check the NAND controller state */
1610 if (hnand
->State
== HAL_NAND_STATE_BUSY
)
1614 else if (hnand
->State
== HAL_NAND_STATE_READY
)
1616 /* Process Locked */
1619 /* Update the NAND controller state */
1620 hnand
->State
= HAL_NAND_STATE_BUSY
;
1622 /* Identify the device address */
1623 deviceAddress
= NAND_DEVICE
;
1625 /* NAND raw address calculation */
1626 nandAddress
= ARRAY_ADDRESS(pAddress
, hnand
);
1628 /* Column in page address */
1629 columnAddress
= (uint32_t)(COLUMN_ADDRESS(hnand
) * 2U);
1631 /* Spare area(s) write loop */
1632 while ((nbspare
!= 0U) && (nandAddress
< ((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
))))
1634 /* Cards with page size <= 512 bytes */
1635 if ((hnand
->Config
.PageSize
) <= 512U)
1637 /* Send write Spare area command sequence */
1638 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_AREA_C
;
1640 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_WRITE0
;
1643 if (((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
)) <= 65535U)
1645 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
1647 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1649 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1652 else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
1654 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = 0x00U
;
1656 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1658 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1660 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(nandAddress
);
1664 else /* (hnand->Config.PageSize) > 512 */
1666 /* Send write Spare area command sequence */
1667 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_AREA_A
;
1669 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_WRITE0
;
1672 if (((hnand
->Config
.BlockSize
) * (hnand
->Config
.BlockNbr
)) <= 65535U)
1674 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = COLUMN_1ST_CYCLE(columnAddress
);
1676 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = COLUMN_2ND_CYCLE(columnAddress
);
1678 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1680 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1683 else /* ((hnand->Config.BlockSize)*(hnand->Config.BlockNbr)) > 65535 */
1685 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = COLUMN_1ST_CYCLE(columnAddress
);
1687 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = COLUMN_2ND_CYCLE(columnAddress
);
1689 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(nandAddress
);
1691 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(nandAddress
);
1693 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(nandAddress
);
1698 /* Write data to memory */
1699 for (index
= 0U; index
< hnand
->Config
.SpareAreaSize
; index
++)
1701 *(__IO
uint16_t *)deviceAddress
= *buff
;
1706 *(__IO
uint8_t *)((uint32_t)(deviceAddress
| CMD_AREA
)) = NAND_CMD_WRITE_TRUE1
;
1710 tickstart
= HAL_GetTick();
1712 /* Read status until NAND is ready */
1713 while (HAL_NAND_Read_Status(hnand
) != NAND_READY
)
1715 if ((HAL_GetTick() - tickstart
) > NAND_WRITE_TIMEOUT
)
1717 /* Update the NAND controller state */
1718 hnand
->State
= HAL_NAND_STATE_ERROR
;
1720 /* Process unlocked */
1721 __HAL_UNLOCK(hnand
);
1727 /* Increment written spare areas number */
1728 numSpareAreaWritten
++;
1730 /* Decrement spare areas to write */
1733 /* Increment the NAND address */
1734 nandAddress
= (uint32_t)(nandAddress
+ 1U);
1737 /* Update the NAND controller state */
1738 hnand
->State
= HAL_NAND_STATE_READY
;
1740 /* Process unlocked */
1741 __HAL_UNLOCK(hnand
);
1752 * @brief NAND memory Block erase
1753 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
1754 * the configuration information for NAND module.
1755 * @param pAddress pointer to NAND address structure
1756 * @retval HAL status
1758 HAL_StatusTypeDef
HAL_NAND_Erase_Block(NAND_HandleTypeDef
*hnand
, NAND_AddressTypeDef
*pAddress
)
1760 uint32_t DeviceAddress
;
1762 /* Check the NAND controller state */
1763 if (hnand
->State
== HAL_NAND_STATE_BUSY
)
1767 else if (hnand
->State
== HAL_NAND_STATE_READY
)
1769 /* Process Locked */
1772 /* Update the NAND controller state */
1773 hnand
->State
= HAL_NAND_STATE_BUSY
;
1775 /* Identify the device address */
1776 DeviceAddress
= NAND_DEVICE
;
1778 /* Send Erase block command sequence */
1779 *(__IO
uint8_t *)((uint32_t)(DeviceAddress
| CMD_AREA
)) = NAND_CMD_ERASE0
;
1781 *(__IO
uint8_t *)((uint32_t)(DeviceAddress
| ADDR_AREA
)) = ADDR_1ST_CYCLE(ARRAY_ADDRESS(pAddress
, hnand
));
1783 *(__IO
uint8_t *)((uint32_t)(DeviceAddress
| ADDR_AREA
)) = ADDR_2ND_CYCLE(ARRAY_ADDRESS(pAddress
, hnand
));
1785 *(__IO
uint8_t *)((uint32_t)(DeviceAddress
| ADDR_AREA
)) = ADDR_3RD_CYCLE(ARRAY_ADDRESS(pAddress
, hnand
));
1788 *(__IO
uint8_t *)((uint32_t)(DeviceAddress
| CMD_AREA
)) = NAND_CMD_ERASE1
;
1791 /* Update the NAND controller state */
1792 hnand
->State
= HAL_NAND_STATE_READY
;
1794 /* Process unlocked */
1795 __HAL_UNLOCK(hnand
);
1806 * @brief Increment the NAND memory address
1807 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
1808 * the configuration information for NAND module.
1809 * @param pAddress pointer to NAND address structure
1810 * @retval The new status of the increment address operation. It can be:
1811 * - NAND_VALID_ADDRESS: When the new address is valid address
1812 * - NAND_INVALID_ADDRESS: When the new address is invalid address
1814 uint32_t HAL_NAND_Address_Inc(NAND_HandleTypeDef
*hnand
, NAND_AddressTypeDef
*pAddress
)
1816 uint32_t status
= NAND_VALID_ADDRESS
;
1818 /* Increment page address */
1821 /* Check NAND address is valid */
1822 if (pAddress
->Page
== hnand
->Config
.BlockSize
)
1827 if (pAddress
->Block
== hnand
->Config
.PlaneSize
)
1829 pAddress
->Block
= 0;
1832 if (pAddress
->Plane
== (hnand
->Config
.PlaneNbr
))
1834 status
= NAND_INVALID_ADDRESS
;
1842 #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
1844 * @brief Register a User NAND Callback
1845 * To be used instead of the weak (surcharged) predefined callback
1846 * @param hnand : NAND handle
1847 * @param CallbackId : ID of the callback to be registered
1848 * This parameter can be one of the following values:
1849 * @arg @ref HAL_NAND_MSP_INIT_CB_ID NAND MspInit callback ID
1850 * @arg @ref HAL_NAND_MSP_DEINIT_CB_ID NAND MspDeInit callback ID
1851 * @arg @ref HAL_NAND_IT_CB_ID NAND IT callback ID
1852 * @param pCallback : pointer to the Callback function
1855 HAL_StatusTypeDef
HAL_NAND_RegisterCallback (NAND_HandleTypeDef
*hnand
, HAL_NAND_CallbackIDTypeDef CallbackId
, pNAND_CallbackTypeDef pCallback
)
1857 HAL_StatusTypeDef status
= HAL_OK
;
1859 if(pCallback
== NULL
)
1864 /* Process locked */
1867 if(hnand
->State
== HAL_NAND_STATE_READY
)
1871 case HAL_NAND_MSP_INIT_CB_ID
:
1872 hnand
->MspInitCallback
= pCallback
;
1874 case HAL_NAND_MSP_DEINIT_CB_ID
:
1875 hnand
->MspDeInitCallback
= pCallback
;
1877 case HAL_NAND_IT_CB_ID
:
1878 hnand
->ItCallback
= pCallback
;
1881 /* update return status */
1886 else if(hnand
->State
== HAL_NAND_STATE_RESET
)
1890 case HAL_NAND_MSP_INIT_CB_ID
:
1891 hnand
->MspInitCallback
= pCallback
;
1893 case HAL_NAND_MSP_DEINIT_CB_ID
:
1894 hnand
->MspDeInitCallback
= pCallback
;
1897 /* update return status */
1904 /* update return status */
1909 __HAL_UNLOCK(hnand
);
1914 * @brief Unregister a User NAND Callback
1915 * NAND Callback is redirected to the weak (surcharged) predefined callback
1916 * @param hnand : NAND handle
1917 * @param CallbackId : ID of the callback to be unregistered
1918 * This parameter can be one of the following values:
1919 * @arg @ref HAL_NAND_MSP_INIT_CB_ID NAND MspInit callback ID
1920 * @arg @ref HAL_NAND_MSP_DEINIT_CB_ID NAND MspDeInit callback ID
1921 * @arg @ref HAL_NAND_IT_CB_ID NAND IT callback ID
1924 HAL_StatusTypeDef
HAL_NAND_UnRegisterCallback (NAND_HandleTypeDef
*hnand
, HAL_NAND_CallbackIDTypeDef CallbackId
)
1926 HAL_StatusTypeDef status
= HAL_OK
;
1928 /* Process locked */
1931 if(hnand
->State
== HAL_NAND_STATE_READY
)
1935 case HAL_NAND_MSP_INIT_CB_ID
:
1936 hnand
->MspInitCallback
= HAL_NAND_MspInit
;
1938 case HAL_NAND_MSP_DEINIT_CB_ID
:
1939 hnand
->MspDeInitCallback
= HAL_NAND_MspDeInit
;
1941 case HAL_NAND_IT_CB_ID
:
1942 hnand
->ItCallback
= HAL_NAND_ITCallback
;
1945 /* update return status */
1950 else if(hnand
->State
== HAL_NAND_STATE_RESET
)
1954 case HAL_NAND_MSP_INIT_CB_ID
:
1955 hnand
->MspInitCallback
= HAL_NAND_MspInit
;
1957 case HAL_NAND_MSP_DEINIT_CB_ID
:
1958 hnand
->MspDeInitCallback
= HAL_NAND_MspDeInit
;
1961 /* update return status */
1968 /* update return status */
1973 __HAL_UNLOCK(hnand
);
1982 /** @defgroup NAND_Exported_Functions_Group3 Peripheral Control functions
1983 * @brief management functions
1986 ==============================================================================
1987 ##### NAND Control functions #####
1988 ==============================================================================
1990 This subsection provides a set of functions allowing to control dynamically
1999 * @brief Enables dynamically NAND ECC feature.
2000 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
2001 * the configuration information for NAND module.
2002 * @retval HAL status
2004 HAL_StatusTypeDef
HAL_NAND_ECC_Enable(NAND_HandleTypeDef
*hnand
)
2006 /* Check the NAND controller state */
2007 if (hnand
->State
== HAL_NAND_STATE_BUSY
)
2011 else if (hnand
->State
== HAL_NAND_STATE_READY
)
2013 /* Update the NAND state */
2014 hnand
->State
= HAL_NAND_STATE_BUSY
;
2016 /* Enable ECC feature */
2017 (void)FMC_NAND_ECC_Enable(hnand
->Instance
, hnand
->Init
.NandBank
);
2019 /* Update the NAND state */
2020 hnand
->State
= HAL_NAND_STATE_READY
;
2031 * @brief Disables dynamically FMC_NAND ECC feature.
2032 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
2033 * the configuration information for NAND module.
2034 * @retval HAL status
2036 HAL_StatusTypeDef
HAL_NAND_ECC_Disable(NAND_HandleTypeDef
*hnand
)
2038 /* Check the NAND controller state */
2039 if (hnand
->State
== HAL_NAND_STATE_BUSY
)
2043 else if (hnand
->State
== HAL_NAND_STATE_READY
)
2045 /* Update the NAND state */
2046 hnand
->State
= HAL_NAND_STATE_BUSY
;
2048 /* Disable ECC feature */
2049 (void)FMC_NAND_ECC_Disable(hnand
->Instance
, hnand
->Init
.NandBank
);
2051 /* Update the NAND state */
2052 hnand
->State
= HAL_NAND_STATE_READY
;
2063 * @brief Disables dynamically NAND ECC feature.
2064 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
2065 * the configuration information for NAND module.
2066 * @param ECCval pointer to ECC value
2067 * @param Timeout maximum timeout to wait
2068 * @retval HAL status
2070 HAL_StatusTypeDef
HAL_NAND_GetECC(NAND_HandleTypeDef
*hnand
, uint32_t *ECCval
, uint32_t Timeout
)
2072 HAL_StatusTypeDef status
;
2074 /* Check the NAND controller state */
2075 if (hnand
->State
== HAL_NAND_STATE_BUSY
)
2079 else if (hnand
->State
== HAL_NAND_STATE_READY
)
2081 /* Update the NAND state */
2082 hnand
->State
= HAL_NAND_STATE_BUSY
;
2084 /* Get NAND ECC value */
2085 status
= FMC_NAND_GetECC(hnand
->Instance
, ECCval
, hnand
->Init
.NandBank
, Timeout
);
2087 /* Update the NAND state */
2088 hnand
->State
= HAL_NAND_STATE_READY
;
2103 /** @defgroup NAND_Exported_Functions_Group4 Peripheral State functions
2104 * @brief Peripheral State functions
2107 ==============================================================================
2108 ##### NAND State functions #####
2109 ==============================================================================
2111 This subsection permits to get in run-time the status of the NAND controller
2119 * @brief return the NAND state
2120 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
2121 * the configuration information for NAND module.
2124 HAL_NAND_StateTypeDef
HAL_NAND_GetState(NAND_HandleTypeDef
*hnand
)
2126 return hnand
->State
;
2130 * @brief NAND memory read status
2131 * @param hnand pointer to a NAND_HandleTypeDef structure that contains
2132 * the configuration information for NAND module.
2133 * @retval NAND status
2135 uint32_t HAL_NAND_Read_Status(NAND_HandleTypeDef
*hnand
)
2138 uint32_t DeviceAddress
;
2141 /* Identify the device address */
2142 DeviceAddress
= NAND_DEVICE
;
2144 /* Send Read status operation command */
2145 *(__IO
uint8_t *)((uint32_t)(DeviceAddress
| CMD_AREA
)) = NAND_CMD_STATUS
;
2147 /* Read status register data */
2148 data
= *(__IO
uint8_t *)DeviceAddress
;
2150 /* Return the status */
2151 if ((data
& NAND_ERROR
) == NAND_ERROR
)
2155 else if ((data
& NAND_READY
) == NAND_READY
)
2177 #endif /* HAL_NAND_MODULE_ENABLED */
2183 #endif /* FMC_BANK3 */
2185 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/