STM32G474xx HAL 用户手册
stm32g4xx_hal_nand.c
转到此文件的文档。
00001 /**
00002   ******************************************************************************
00003   * @file    stm32g4xx_hal_nand.c
00004   * @author  MCD Application Team
00005   * @brief   NAND HAL module driver.
00006   *          This file provides a generic firmware to drive NAND memories mounted
00007   *          as external device.
00008   *
00009   ******************************************************************************
00010   * @attention
00011   *
00012   * Copyright (c) 2019 STMicroelectronics.
00013   * All rights reserved.
00014   *
00015   * This software is licensed under terms that can be found in the LICENSE file
00016   * in the root directory of this software component.
00017   * If no LICENSE file comes with this software, it is provided AS-IS.
00018   *
00019   ******************************************************************************
00020   @verbatim
00021   ==============================================================================
00022                          ##### How to use this driver #####
00023   ==============================================================================
00024     [..]
00025       This driver is a generic layered driver which contains a set of APIs used to
00026       control NAND flash memories. It uses the FMC layer functions to interface
00027       with NAND devices. This driver is used as follows:
00028 
00029       (+) NAND flash memory configuration sequence using the function HAL_NAND_Init()
00030           with control and timing parameters for both common and attribute spaces.
00031 
00032       (+) Read NAND flash memory maker and device IDs using the function
00033           HAL_NAND_Read_ID(). The read information is stored in the NAND_ID_TypeDef
00034           structure declared by the function caller.
00035 
00036       (+) Access NAND flash memory by read/write operations using the functions
00037           HAL_NAND_Read_Page_8b()/HAL_NAND_Read_SpareArea_8b(),
00038           HAL_NAND_Write_Page_8b()/HAL_NAND_Write_SpareArea_8b(),
00039           HAL_NAND_Read_Page_16b()/HAL_NAND_Read_SpareArea_16b(),
00040           HAL_NAND_Write_Page_16b()/HAL_NAND_Write_SpareArea_16b()
00041           to read/write page(s)/spare area(s). These functions use specific device
00042           information (Block, page size..) predefined by the user in the NAND_DeviceConfigTypeDef
00043           structure. The read/write address information is contained by the Nand_Address_Typedef
00044           structure passed as parameter.
00045 
00046       (+) Perform NAND flash Reset chip operation using the function HAL_NAND_Reset().
00047 
00048       (+) Perform NAND flash erase block operation using the function HAL_NAND_Erase_Block().
00049           The erase block address information is contained in the Nand_Address_Typedef
00050           structure passed as parameter.
00051 
00052       (+) Read the NAND flash status operation using the function HAL_NAND_Read_Status().
00053 
00054       (+) You can also control the NAND device by calling the control APIs HAL_NAND_ECC_Enable()/
00055           HAL_NAND_ECC_Disable() to respectively enable/disable the ECC code correction
00056           feature or the function HAL_NAND_GetECC() to get the ECC correction code.
00057 
00058       (+) You can monitor the NAND device HAL state by calling the function
00059           HAL_NAND_GetState()
00060 
00061     [..]
00062       (@) This driver is a set of generic APIs which handle standard NAND flash operations.
00063           If a NAND flash device contains different operations and/or implementations,
00064           it should be implemented separately.
00065 
00066     *** Callback registration ***
00067     =============================================
00068     [..]
00069       The compilation define  USE_HAL_NAND_REGISTER_CALLBACKS when set to 1
00070       allows the user to configure dynamically the driver callbacks.
00071 
00072       Use Functions HAL_NAND_RegisterCallback() to register a user callback,
00073       it allows to register following callbacks:
00074         (+) MspInitCallback    : NAND MspInit.
00075         (+) MspDeInitCallback  : NAND MspDeInit.
00076       This function takes as parameters the HAL peripheral handle, the Callback ID
00077       and a pointer to the user callback function.
00078 
00079       Use function HAL_NAND_UnRegisterCallback() to reset a callback to the default
00080       weak (overridden) function. It allows to reset following callbacks:
00081         (+) MspInitCallback    : NAND MspInit.
00082         (+) MspDeInitCallback  : NAND MspDeInit.
00083       This function) takes as parameters the HAL peripheral handle and the Callback ID.
00084 
00085       By default, after the HAL_NAND_Init and if the state is HAL_NAND_STATE_RESET
00086       all callbacks are reset to the corresponding legacy weak (overridden) functions.
00087       Exception done for MspInit and MspDeInit callbacks that are respectively
00088       reset to the legacy weak (overridden) functions in the HAL_NAND_Init
00089       and  HAL_NAND_DeInit only when these callbacks are null (not registered beforehand).
00090       If not, MspInit or MspDeInit are not null, the HAL_NAND_Init and HAL_NAND_DeInit
00091       keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
00092 
00093       Callbacks can be registered/unregistered in READY state only.
00094       Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
00095       in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
00096       during the Init/DeInit.
00097       In that case first register the MspInit/MspDeInit user callbacks
00098       using HAL_NAND_RegisterCallback before calling HAL_NAND_DeInit
00099       or HAL_NAND_Init function.
00100 
00101       When The compilation define USE_HAL_NAND_REGISTER_CALLBACKS is set to 0 or
00102       not defined, the callback registering feature is not available
00103       and weak (overridden) callbacks are used.
00104 
00105   @endverbatim
00106   ******************************************************************************
00107   */
00108 
00109 /* Includes ------------------------------------------------------------------*/
00110 #include "stm32g4xx_hal.h"
00111 
00112 #if defined(FMC_BANK3)
00113 
00114 /** @addtogroup STM32G4xx_HAL_Driver
00115   * @{
00116   */
00117 
00118 #ifdef HAL_NAND_MODULE_ENABLED
00119 
00120 /** @defgroup NAND NAND
00121   * @brief NAND HAL module driver
00122   * @{
00123   */
00124 
00125 /* Private typedef -----------------------------------------------------------*/
00126 /* Private Constants ------------------------------------------------------------*/
00127 /* Private macro -------------------------------------------------------------*/
00128 /* Private variables ---------------------------------------------------------*/
00129 /* Private function prototypes -----------------------------------------------*/
00130 /* Exported functions ---------------------------------------------------------*/
00131 
00132 /** @defgroup NAND_Exported_Functions NAND Exported Functions
00133   * @{
00134   */
00135 
00136 /** @defgroup NAND_Exported_Functions_Group1 Initialization and de-initialization functions
00137   * @brief    Initialization and Configuration functions
00138   *
00139   @verbatim
00140   ==============================================================================
00141             ##### NAND Initialization and de-initialization functions #####
00142   ==============================================================================
00143   [..]
00144     This section provides functions allowing to initialize/de-initialize
00145     the NAND memory
00146 
00147 @endverbatim
00148   * @{
00149   */
00150 
00151 /**
00152   * @brief  Perform NAND memory Initialization sequence
00153   * @param  hnand pointer to a NAND_HandleTypeDef structure that contains
00154   *                the configuration information for NAND module.
00155   * @param  ComSpace_Timing pointer to Common space timing structure
00156   * @param  AttSpace_Timing pointer to Attribute space timing structure
00157   * @retval HAL status
00158   */
00159 HAL_StatusTypeDef  HAL_NAND_Init(NAND_HandleTypeDef *hnand, FMC_NAND_PCC_TimingTypeDef *ComSpace_Timing,
00160                                  FMC_NAND_PCC_TimingTypeDef *AttSpace_Timing)
00161 {
00162   /* Check the NAND handle state */
00163   if (hnand == NULL)
00164   {
00165     return HAL_ERROR;
00166   }
00167 
00168   if (hnand->State == HAL_NAND_STATE_RESET)
00169   {
00170     /* Allocate lock resource and initialize it */
00171     hnand->Lock = HAL_UNLOCKED;
00172 
00173 #if (USE_HAL_NAND_REGISTER_CALLBACKS == 1)
00174     if (hnand->MspInitCallback == NULL)
00175     {
00176       hnand->MspInitCallback = HAL_NAND_MspInit;
00177     }
00178     hnand->ItCallback = HAL_NAND_ITCallback;
00179 
00180     /* Init the low level hardware */
00181     hnand->MspInitCallback(hnand);
00182 #else
00183     /* Initialize the low level hardware (MSP) */
00184     HAL_NAND_MspInit(hnand);
00185 #endif /* (USE_HAL_NAND_REGISTER_CALLBACKS) */
00186   }
00187 
00188   /* Initialize NAND control Interface */
00189   (void)FMC_NAND_Init(hnand->Instance, &(hnand->Init));
00190