STM32G474xx HAL用户手册
stm32g4xx_hal_nor.c
跳转到该文件的文档。
00001 /**
00002   ******************************************************************************
00003   * @file    stm32g4xx_hal_nor.c
00004   * @author  MCD Application Team
00005   * @brief   NOR HAL module driver.
00006   *          This file provides a generic firmware to drive NOR 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 NOR flash memories. It uses the FMC layer functions to interface
00027       with NOR devices. This driver is used as follows:
00028 
00029       (+) NOR flash memory configuration sequence using the function HAL_NOR_Init()
00030           with control and timing parameters for both normal and extended mode.
00031 
00032       (+) Read NOR flash memory manufacturer code and device IDs using the function
00033           HAL_NOR_Read_ID(). The read information is stored in the NOR_ID_TypeDef
00034           structure declared by the function caller.
00035 
00036       (+) Access NOR flash memory by read/write data unit operations using the functions
00037           HAL_NOR_Read(), HAL_NOR_Program().
00038 
00039       (+) Perform NOR flash erase block/chip operations using the functions
00040           HAL_NOR_Erase_Block() and HAL_NOR_Erase_Chip().
00041 
00042       (+) Read the NOR flash CFI (common flash interface) IDs using the function
00043           HAL_NOR_Read_CFI(). The read information is stored in the NOR_CFI_TypeDef
00044           structure declared by the function caller.
00045 
00046       (+) You can also control the NOR device by calling the control APIs HAL_NOR_WriteOperation_Enable()/
00047           HAL_NOR_WriteOperation_Disable() to respectively enable/disable the NOR write operation
00048 
00049       (+) You can monitor the NOR device HAL state by calling the function
00050           HAL_NOR_GetState()
00051     [..]
00052      (@) This driver is a set of generic APIs which handle standard NOR flash operations.
00053          If a NOR flash device contains different operations and/or implementations,
00054          it should be implemented separately.
00055 
00056      *** NOR HAL driver macros list ***
00057      =============================================
00058      [..]
00059        Below the list of most used macros in NOR HAL driver.
00060 
00061       (+) NOR_WRITE : NOR memory write data to specified address
00062 
00063     *** Callback registration ***
00064     =============================================
00065     [..]
00066       The compilation define  USE_HAL_NOR_REGISTER_CALLBACKS when set to 1
00067       allows the user to configure dynamically the driver callbacks.
00068 
00069       Use Functions HAL_NOR_RegisterCallback() to register a user callback,
00070       it allows to register following callbacks:
00071         (+) MspInitCallback    : NOR MspInit.
00072         (+) MspDeInitCallback  : NOR MspDeInit.
00073       This function takes as parameters the HAL peripheral handle, the Callback ID
00074       and a pointer to the user callback function.
00075 
00076       Use function HAL_NOR_UnRegisterCallback() to reset a callback to the default
00077       weak (overridden) function. It allows to reset following callbacks:
00078         (+) MspInitCallback    : NOR MspInit.
00079         (+) MspDeInitCallback  : NOR MspDeInit.
00080       This function) takes as parameters the HAL peripheral handle and the Callback ID.
00081 
00082       By default, after the HAL_NOR_Init and if the state is HAL_NOR_STATE_RESET
00083       all callbacks are reset to the corresponding legacy weak (overridden) functions.
00084       Exception done for MspInit and MspDeInit callbacks that are respectively
00085       reset to the legacy weak (overridden) functions in the HAL_NOR_Init
00086       and  HAL_NOR_DeInit only when these callbacks are null (not registered beforehand).
00087       If not, MspInit or MspDeInit are not null, the HAL_NOR_Init and HAL_NOR_DeInit
00088       keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
00089 
00090       Callbacks can be registered/unregistered in READY state only.
00091       Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
00092       in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
00093       during the Init/DeInit.
00094       In that case first register the MspInit/MspDeInit user callbacks
00095       using HAL_NOR_RegisterCallback before calling HAL_NOR_DeInit
00096       or HAL_NOR_Init function.
00097 
00098       When The compilation define USE_HAL_NOR_REGISTER_CALLBACKS is set to 0 or
00099       not defined, the callback registering feature is not available
00100       and weak (overridden) callbacks are used.
00101 
00102   @endverbatim
00103   ******************************************************************************
00104   */
00105 
00106 /* Includes ------------------------------------------------------------------*/
00107 #include "stm32g4xx_hal.h"
00108 
00109 #if  defined(FMC_BANK1)
00110 
00111 /** @addtogroup STM32G4xx_HAL_Driver
00112   * @{
00113   */
00114 
00115 #ifdef HAL_NOR_MODULE_ENABLED
00116 
00117 /** @defgroup NOR NOR
00118   * @brief NOR driver modules
00119   * @{
00120   */
00121 
00122 /* Private typedef -----------------------------------------------------------*/
00123 /* Private define ------------------------------------------------------------*/
00124 
00125 /** @defgroup NOR_Private_Defines NOR Private Defines
00126   * @{
00127   */
00128 
00129 /* Constants to define address to set to write a command */
00130 #define NOR_CMD_ADDRESS_FIRST_BYTE            (uint16_t)0x0AAA
00131 #define NOR_CMD_ADDRESS_FIRST_CFI_BYTE        (uint16_t)0x00AA
00132 #define NOR_CMD_ADDRESS_SECOND_BYTE           (uint16_t)0x0555
00133 #define NOR_CMD_ADDRESS_THIRD_BYTE            (uint16_t)0x0AAA
00134 
00135 #define NOR_CMD_ADDRESS_FIRST                 (uint16_t)0x0555
00136 #define NOR_CMD_ADDRESS_FIRST_CFI             (uint16_t)0x0055
00137 #define NOR_CMD_ADDRESS_SECOND                (uint16_t)0x02AA
00138 #define NOR_CMD_ADDRESS_THIRD                 (uint16_t)0x0555
00139 #define NOR_CMD_ADDRESS_FOURTH                (uint16_t)0x0555
00140 #define NOR_CMD_ADDRESS_FIFTH                 (uint16_t)0x02AA
00141 #define NOR_CMD_ADDRESS_SIXTH                 (uint16_t)0x0555
00142 
00143 /* Constants to define data to program a command */
00144 #define NOR_CMD_DATA_READ_RESET               (uint16_t)0x00F0
00145 #define NOR_CMD_DATA_FIRST                    (uint16_t)0x00AA
00146 #define NOR_CMD_DATA_SECOND                   (uint16_t)0x0055
00147 #define NOR_CMD_DATA_AUTO_SELECT              (uint16_t)0x0090
00148 #define NOR_CMD_DATA_PROGRAM                  (uint16_t)0x00A0
00149 #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD   (uint16_t)0x0080
00150 #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_FOURTH  (uint16_t)0x00AA
00151 #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_FIFTH   (uint16_t)0x0055
00152 #define NOR_CMD_DATA_CHIP_ERASE               (uint16_t)0x0010
00153 #define NOR_CMD_DATA_CFI                      (uint16_t)0x0098
00154 
00155 #define NOR_CMD_DATA_BUFFER_AND_PROG          (uint8_t)0x25
00156 #define NOR_CMD_DATA_BUFFER_AND_PROG_CONFIRM  (uint8_t)0x29
00157 #define NOR_CMD_DATA_BLOCK_ERASE              (uint8_t)0x30
00158 
00159 #define NOR_CMD_READ_ARRAY                    (uint16_t)0x00FF
00160 #define NOR_CMD_WORD_PROGRAM                  (uint16_t)0x0040
00161 #define NOR_CMD_BUFFERED_PROGRAM              (uint16_t)0x00E8
00162 #define NOR_CMD_CONFIRM                       (uint16_t)0x00D0
00163 #define NOR_CMD_BLOCK_ERASE                   (uint16_t)0x0020
00164 #define NOR_CMD_BLOCK_UNLOCK                  (uint16_t)0x0060
00165 #define NOR_CMD_READ_STATUS_REG               (uint16_t)0x0070
00166 #define NOR_CMD_CLEAR_STATUS_REG              (uint16_t)0x0050
00167 
00168 /* Mask on NOR STATUS REGISTER */
00169 #define NOR_MASK_STATUS_DQ4                   (uint16_t)0x0010
00170 #define NOR_MASK_STATUS_DQ5                   (uint16_t)0x0020
00171 #define NOR_MASK_STATUS_DQ6                   (uint16_t)0x0040
00172