Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:08

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_nor.c
0004   * @author  MCD Application Team
0005   * @brief   NOR HAL module driver.
0006   *          This file provides a generic firmware to drive NOR memories mounted
0007   *          as external device.
0008   *
0009   ******************************************************************************
0010   * @attention
0011   *
0012   * Copyright (c) 2017 STMicroelectronics.
0013   * All rights reserved.
0014   *
0015   * This software is licensed under terms that can be found in the LICENSE file
0016   * in the root directory of this software component.
0017   * If no LICENSE file comes with this software, it is provided AS-IS.
0018   *
0019   ******************************************************************************
0020   @verbatim
0021   ==============================================================================
0022                      ##### How to use this driver #####
0023   ==============================================================================
0024     [..]
0025       This driver is a generic layered driver which contains a set of APIs used to
0026       control NOR flash memories. It uses the FMC layer functions to interface
0027       with NOR devices. This driver is used as follows:
0028 
0029       (+) NOR flash memory configuration sequence using the function HAL_NOR_Init()
0030           with control and timing parameters for both normal and extended mode.
0031 
0032       (+) Read NOR flash memory manufacturer code and device IDs using the function
0033           HAL_NOR_Read_ID(). The read information is stored in the NOR_ID_TypeDef
0034           structure declared by the function caller.
0035 
0036       (+) Access NOR flash memory by read/write data unit operations using the functions
0037           HAL_NOR_Read(), HAL_NOR_Program().
0038 
0039       (+) Perform NOR flash erase block/chip operations using the functions
0040           HAL_NOR_Erase_Block() and HAL_NOR_Erase_Chip().
0041 
0042       (+) Read the NOR flash CFI (common flash interface) IDs using the function
0043           HAL_NOR_Read_CFI(). The read information is stored in the NOR_CFI_TypeDef
0044           structure declared by the function caller.
0045 
0046       (+) You can also control the NOR device by calling the control APIs HAL_NOR_WriteOperation_Enable()/
0047           HAL_NOR_WriteOperation_Disable() to respectively enable/disable the NOR write operation
0048 
0049       (+) You can monitor the NOR device HAL state by calling the function
0050           HAL_NOR_GetState()
0051     [..]
0052      (@) This driver is a set of generic APIs which handle standard NOR flash operations.
0053          If a NOR flash device contains different operations and/or implementations,
0054          it should be implemented separately.
0055 
0056      *** NOR HAL driver macros list ***
0057      =============================================
0058      [..]
0059        Below the list of most used macros in NOR HAL driver.
0060 
0061       (+) NOR_WRITE : NOR memory write data to specified address
0062 
0063     *** Callback registration ***
0064     =============================================
0065     [..]
0066       The compilation define  USE_HAL_NOR_REGISTER_CALLBACKS when set to 1
0067       allows the user to configure dynamically the driver callbacks.
0068 
0069       Use Functions HAL_NOR_RegisterCallback() to register a user callback,
0070       it allows to register following callbacks:
0071         (+) MspInitCallback    : NOR MspInit.
0072         (+) MspDeInitCallback  : NOR MspDeInit.
0073       This function takes as parameters the HAL peripheral handle, the Callback ID
0074       and a pointer to the user callback function.
0075 
0076       Use function HAL_NOR_UnRegisterCallback() to reset a callback to the default
0077       weak (overridden) function. It allows to reset following callbacks:
0078         (+) MspInitCallback    : NOR MspInit.
0079         (+) MspDeInitCallback  : NOR MspDeInit.
0080       This function) takes as parameters the HAL peripheral handle and the Callback ID.
0081 
0082       By default, after the HAL_NOR_Init and if the state is HAL_NOR_STATE_RESET
0083       all callbacks are reset to the corresponding legacy weak (overridden) functions.
0084       Exception done for MspInit and MspDeInit callbacks that are respectively
0085       reset to the legacy weak (overridden) functions in the HAL_NOR_Init
0086       and  HAL_NOR_DeInit only when these callbacks are null (not registered beforehand).
0087       If not, MspInit or MspDeInit are not null, the HAL_NOR_Init and HAL_NOR_DeInit
0088       keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
0089 
0090       Callbacks can be registered/unregistered in READY state only.
0091       Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
0092       in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
0093       during the Init/DeInit.
0094       In that case first register the MspInit/MspDeInit user callbacks
0095       using HAL_NOR_RegisterCallback before calling HAL_NOR_DeInit
0096       or HAL_NOR_Init function.
0097 
0098       When The compilation define USE_HAL_NOR_REGISTER_CALLBACKS is set to 0 or
0099       not defined, the callback registering feature is not available
0100       and weak (overridden) callbacks are used.
0101 
0102   @endverbatim
0103   ******************************************************************************
0104   */
0105 
0106 /* Includes ------------------------------------------------------------------*/
0107 #include "stm32h7xx_hal.h"
0108 
0109 
0110 /** @addtogroup STM32H7xx_HAL_Driver
0111   * @{
0112   */
0113 
0114 #ifdef HAL_NOR_MODULE_ENABLED
0115 
0116 /** @defgroup NOR NOR
0117   * @ingroup RTEMSBSPsARMSTM32H7
0118   * @brief NOR driver modules
0119   * @{
0120   */
0121 
0122 /* Private typedef -----------------------------------------------------------*/
0123 /* Private define ------------------------------------------------------------*/
0124 
0125 /** @defgroup NOR_Private_Defines NOR Private Defines
0126   * @ingroup RTEMSBSPsARMSTM32H7
0127   * @{
0128   */
0129 
0130 /* Constants to define address to set to write a command */
0131 #define NOR_CMD_ADDRESS_FIRST_BYTE            (uint16_t)0x0AAA
0132 #define NOR_CMD_ADDRESS_FIRST_CFI_BYTE        (uint16_t)0x00AA
0133 #define NOR_CMD_ADDRESS_SECOND_BYTE           (uint16_t)0x0555
0134 #define NOR_CMD_ADDRESS_THIRD_BYTE            (uint16_t)0x0AAA
0135 
0136 #define NOR_CMD_ADDRESS_FIRST                 (uint16_t)0x0555
0137 #define NOR_CMD_ADDRESS_FIRST_CFI             (uint16_t)0x0055
0138 #define NOR_CMD_ADDRESS_SECOND                (uint16_t)0x02AA
0139 #define NOR_CMD_ADDRESS_THIRD                 (uint16_t)0x0555
0140 #define NOR_CMD_ADDRESS_FOURTH                (uint16_t)0x0555
0141 #define NOR_CMD_ADDRESS_FIFTH                 (uint16_t)0x02AA
0142 #define NOR_CMD_ADDRESS_SIXTH                 (uint16_t)0x0555
0143 
0144 /* Constants to define data to program a command */
0145 #define NOR_CMD_DATA_READ_RESET               (uint16_t)0x00F0
0146 #define NOR_CMD_DATA_FIRST                    (uint16_t)0x00AA
0147 #define NOR_CMD_DATA_SECOND                   (uint16_t)0x0055
0148 #define NOR_CMD_DATA_AUTO_SELECT              (uint16_t)0x0090
0149 #define NOR_CMD_DATA_PROGRAM                  (uint16_t)0x00A0
0150 #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD   (uint16_t)0x0080
0151 #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_FOURTH  (uint16_t)0x00AA
0152 #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_FIFTH   (uint16_t)0x0055
0153 #define NOR_CMD_DATA_CHIP_ERASE               (uint16_t)0x0010
0154 #define NOR_CMD_DATA_CFI                      (uint16_t)0x0098
0155 
0156 #define NOR_CMD_DATA_BUFFER_AND_PROG          (uint8_t)0x25
0157 #define NOR_CMD_DATA_BUFFER_AND_PROG_CONFIRM  (uint8_t)0x29
0158 #define NOR_CMD_DATA_BLOCK_ERASE              (uint8_t)0x30
0159 
0160 #define NOR_CMD_READ_ARRAY                    (uint16_t)0x00FF
0161 #define NOR_CMD_WORD_PROGRAM                  (uint16_t)0x0040
0162 #define NOR_CMD_BUFFERED_PROGRAM              (uint16_t)0x00E8
0163 #define NOR_CMD_CONFIRM                       (uint16_t)0x00D0
0164 #define NOR_CMD_BLOCK_ERASE                   (uint16_t)0x0020
0165 #define NOR_CMD_BLOCK_UNLOCK                  (uint16_t)0x0060
0166 #define NOR_CMD_READ_STATUS_REG               (uint16_t)0x0070
0167 #define NOR_CMD_CLEAR_STATUS_REG              (uint16_t)0x0050
0168 
0169 /* Mask on NOR STATUS REGISTER */
0170 #define NOR_MASK_STATUS_DQ4                   (uint16_t)0x0010
0171 #define NOR_MASK_STATUS_DQ5                   (uint16_t)0x0020
0172 #define NOR_MASK_STATUS_DQ6                   (uint16_t)0x0040
0173 #define NOR_MASK_STATUS_DQ7                   (uint16_t)0x0080
0174 
0175 /* Address of the primary command set */
0176 #define NOR_ADDRESS_COMMAND_SET               (uint16_t)0x0013
0177 
0178 /* Command set code assignment (defined in JEDEC JEP137B version may 2004) */
0179 #define NOR_INTEL_SHARP_EXT_COMMAND_SET       (uint16_t)0x0001 /* Supported in this driver */
0180 #define NOR_AMD_FUJITSU_COMMAND_SET           (uint16_t)0x0002 /* Supported in this driver */
0181 #define NOR_INTEL_STANDARD_COMMAND_SET        (uint16_t)0x0003 /* Not Supported in this driver */
0182 #define NOR_AMD_FUJITSU_EXT_COMMAND_SET       (uint16_t)0x0004 /* Not Supported in this driver */
0183 #define NOR_WINDBOND_STANDARD_COMMAND_SET     (uint16_t)0x0006 /* Not Supported in this driver */
0184 #define NOR_MITSUBISHI_STANDARD_COMMAND_SET   (uint16_t)0x0100 /* Not Supported in this driver */
0185 #define NOR_MITSUBISHI_EXT_COMMAND_SET        (uint16_t)0x0101 /* Not Supported in this driver */
0186 #define NOR_PAGE_WRITE_COMMAND_SET            (uint16_t)0x0102 /* Not Supported in this driver */
0187 #define NOR_INTEL_PERFORMANCE_COMMAND_SET     (uint16_t)0x0200 /* Not Supported in this driver */
0188 #define NOR_INTEL_DATA_COMMAND_SET            (uint16_t)0x0210 /* Not Supported in this driver */
0189 
0190 /**
0191   * @}
0192   */
0193 
0194 /* Private macro -------------------------------------------------------------*/
0195 /* Private variables ---------------------------------------------------------*/
0196 /** @defgroup NOR_Private_Variables NOR Private Variables
0197   * @ingroup RTEMSBSPsARMSTM32H7
0198   * @{
0199   */
0200 
0201 static uint32_t uwNORMemoryDataWidth  = NOR_MEMORY_8B;
0202 
0203 /**
0204   * @}
0205   */
0206 
0207 /* Private functions ---------------------------------------------------------*/
0208 /* Exported functions --------------------------------------------------------*/
0209 /** @defgroup NOR_Exported_Functions NOR Exported Functions
0210   * @ingroup RTEMSBSPsARMSTM32H7
0211   * @{
0212   */
0213 
0214 /** @defgroup NOR_Exported_Functions_Group1 Initialization and de-initialization functions
0215   * @ingroup RTEMSBSPsARMSTM32H7
0216   * @brief    Initialization and Configuration functions
0217   *
0218   @verbatim
0219   ==============================================================================
0220            ##### NOR Initialization and de_initialization functions #####
0221   ==============================================================================
0222   [..]
0223     This section provides functions allowing to initialize/de-initialize
0224     the NOR memory
0225 
0226 @endverbatim
0227   * @{
0228   */
0229 
0230 /**
0231   * @brief  Perform the NOR memory Initialization sequence
0232   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
0233   *                the configuration information for NOR module.
0234   * @param  Timing pointer to NOR control timing structure
0235   * @param  ExtTiming pointer to NOR extended mode timing structure
0236   * @retval HAL status
0237   */
0238 HAL_StatusTypeDef HAL_NOR_Init(NOR_HandleTypeDef *hnor, FMC_NORSRAM_TimingTypeDef *Timing,
0239                                FMC_NORSRAM_TimingTypeDef *ExtTiming)
0240 {
0241   uint32_t deviceaddress;
0242   HAL_StatusTypeDef status = HAL_OK;
0243 
0244   /* Check the NOR handle parameter */
0245   if (hnor == NULL)
0246   {
0247     return HAL_ERROR;
0248   }
0249 
0250   if (hnor->State == HAL_NOR_STATE_RESET)
0251   {
0252     /* Allocate lock resource and initialize it */
0253     hnor->Lock = HAL_UNLOCKED;
0254 
0255 #if (USE_HAL_NOR_REGISTER_CALLBACKS == 1)
0256     if (hnor->MspInitCallback == NULL)
0257     {
0258       hnor->MspInitCallback = HAL_NOR_MspInit;
0259     }
0260 
0261     /* Init the low level hardware */
0262     hnor->MspInitCallback(hnor);
0263 #else
0264     /* Initialize the low level hardware (MSP) */
0265     HAL_NOR_MspInit(hnor);
0266 #endif /* (USE_HAL_NOR_REGISTER_CALLBACKS) */
0267   }
0268 
0269   /* Initialize NOR control Interface */
0270   (void)FMC_NORSRAM_Init(hnor->Instance, &(hnor->Init));
0271 
0272   /* Initialize NOR timing Interface */
0273   (void)FMC_NORSRAM_Timing_Init(hnor->Instance, Timing, hnor->Init.NSBank);
0274 
0275   /* Initialize NOR extended mode timing Interface */
0276   (void)FMC_NORSRAM_Extended_Timing_Init(hnor->Extended, ExtTiming,
0277                                          hnor->Init.NSBank, hnor->Init.ExtendedMode);
0278 
0279   /* Enable the NORSRAM device */
0280   __FMC_NORSRAM_ENABLE(hnor->Instance, hnor->Init.NSBank);
0281 
0282   /* Initialize NOR Memory Data Width*/
0283   if (hnor->Init.MemoryDataWidth == FMC_NORSRAM_MEM_BUS_WIDTH_8)
0284   {
0285     uwNORMemoryDataWidth = NOR_MEMORY_8B;
0286   }
0287   else
0288   {
0289     uwNORMemoryDataWidth = NOR_MEMORY_16B;
0290   }
0291 
0292   /* Enable FMC Peripheral */
0293   __FMC_ENABLE();
0294 
0295   /* Initialize the NOR controller state */
0296   hnor->State = HAL_NOR_STATE_READY;
0297 
0298   /* Select the NOR device address */
0299   if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
0300   {
0301     deviceaddress = NOR_MEMORY_ADRESS1;
0302   }
0303   else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
0304   {
0305     deviceaddress = NOR_MEMORY_ADRESS2;
0306   }
0307   else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
0308   {
0309     deviceaddress = NOR_MEMORY_ADRESS3;
0310   }
0311   else /* FMC_NORSRAM_BANK4 */
0312   {
0313     deviceaddress = NOR_MEMORY_ADRESS4;
0314   }
0315 
0316   if (hnor->Init.WriteOperation == FMC_WRITE_OPERATION_DISABLE)
0317   {
0318     (void)FMC_NORSRAM_WriteOperation_Disable(hnor->Instance, hnor->Init.NSBank);
0319 
0320     /* Update the NOR controller state */
0321     hnor->State = HAL_NOR_STATE_PROTECTED;
0322   }
0323   else
0324   {
0325     /* Get the value of the command set */
0326     if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
0327     {
0328       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_CFI_BYTE),
0329                 NOR_CMD_DATA_CFI);
0330     }
0331     else
0332     {
0333       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_CFI), NOR_CMD_DATA_CFI);
0334     }
0335 
0336     hnor->CommandSet = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_ADDRESS_COMMAND_SET);
0337 
0338     status = HAL_NOR_ReturnToReadMode(hnor);
0339   }
0340 
0341   return status;
0342 }
0343 
0344 /**
0345   * @brief  Perform NOR memory De-Initialization sequence
0346   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
0347   *                the configuration information for NOR module.
0348   * @retval HAL status
0349   */
0350 HAL_StatusTypeDef HAL_NOR_DeInit(NOR_HandleTypeDef *hnor)
0351 {
0352 #if (USE_HAL_NOR_REGISTER_CALLBACKS == 1)
0353   if (hnor->MspDeInitCallback == NULL)
0354   {
0355     hnor->MspDeInitCallback = HAL_NOR_MspDeInit;
0356   }
0357 
0358   /* DeInit the low level hardware */
0359   hnor->MspDeInitCallback(hnor);
0360 #else
0361   /* De-Initialize the low level hardware (MSP) */
0362   HAL_NOR_MspDeInit(hnor);
0363 #endif /* (USE_HAL_NOR_REGISTER_CALLBACKS) */
0364 
0365   /* Configure the NOR registers with their reset values */
0366   (void)FMC_NORSRAM_DeInit(hnor->Instance, hnor->Extended, hnor->Init.NSBank);
0367 
0368   /* Reset the NOR controller state */
0369   hnor->State = HAL_NOR_STATE_RESET;
0370 
0371   /* Release Lock */
0372   __HAL_UNLOCK(hnor);
0373 
0374   return HAL_OK;
0375 }
0376 
0377 /**
0378   * @brief  NOR MSP Init
0379   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
0380   *                the configuration information for NOR module.
0381   * @retval None
0382   */
0383 __weak void HAL_NOR_MspInit(NOR_HandleTypeDef *hnor)
0384 {
0385   /* Prevent unused argument(s) compilation warning */
0386   UNUSED(hnor);
0387 
0388   /* NOTE : This function Should not be modified, when the callback is needed,
0389             the HAL_NOR_MspInit could be implemented in the user file
0390    */
0391 }
0392 
0393 /**
0394   * @brief  NOR MSP DeInit
0395   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
0396   *                the configuration information for NOR module.
0397   * @retval None
0398   */
0399 __weak void HAL_NOR_MspDeInit(NOR_HandleTypeDef *hnor)
0400 {
0401   /* Prevent unused argument(s) compilation warning */
0402   UNUSED(hnor);
0403 
0404   /* NOTE : This function Should not be modified, when the callback is needed,
0405             the HAL_NOR_MspDeInit could be implemented in the user file
0406    */
0407 }
0408 
0409 /**
0410   * @brief  NOR MSP Wait for Ready/Busy signal
0411   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
0412   *                the configuration information for NOR module.
0413   * @param  Timeout Maximum timeout value
0414   * @retval None
0415   */
0416 __weak void HAL_NOR_MspWait(NOR_HandleTypeDef *hnor, uint32_t Timeout)
0417 {
0418   /* Prevent unused argument(s) compilation warning */
0419   UNUSED(hnor);
0420   UNUSED(Timeout);
0421 
0422   /* NOTE : This function Should not be modified, when the callback is needed,
0423             the HAL_NOR_MspWait could be implemented in the user file
0424    */
0425 }
0426 
0427 /**
0428   * @}
0429   */
0430 
0431 /** @defgroup NOR_Exported_Functions_Group2 Input and Output functions
0432   * @ingroup RTEMSBSPsARMSTM32H7
0433   * @brief    Input Output and memory control functions
0434   *
0435   @verbatim
0436   ==============================================================================
0437                 ##### NOR Input and Output functions #####
0438   ==============================================================================
0439   [..]
0440     This section provides functions allowing to use and control the NOR memory
0441 
0442 @endverbatim
0443   * @{
0444   */
0445 
0446 /**
0447   * @brief  Read NOR flash IDs
0448   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
0449   *                the configuration information for NOR module.
0450   * @param  pNOR_ID  pointer to NOR ID structure
0451   * @retval HAL status
0452   */
0453 HAL_StatusTypeDef HAL_NOR_Read_ID(NOR_HandleTypeDef *hnor, NOR_IDTypeDef *pNOR_ID)
0454 {
0455   uint32_t deviceaddress;
0456   HAL_NOR_StateTypeDef state;
0457   HAL_StatusTypeDef status = HAL_OK;
0458 
0459   /* Check the NOR controller state */
0460   state = hnor->State;
0461   if (state == HAL_NOR_STATE_BUSY)
0462   {
0463     return HAL_BUSY;
0464   }
0465   else if (state == HAL_NOR_STATE_PROTECTED)
0466   {
0467     return HAL_ERROR;
0468   }
0469   else if (state == HAL_NOR_STATE_READY)
0470   {
0471     /* Process Locked */
0472     __HAL_LOCK(hnor);
0473 
0474     /* Update the NOR controller state */
0475     hnor->State = HAL_NOR_STATE_BUSY;
0476 
0477     /* Select the NOR device address */
0478     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
0479     {
0480       deviceaddress = NOR_MEMORY_ADRESS1;
0481     }
0482     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
0483     {
0484       deviceaddress = NOR_MEMORY_ADRESS2;
0485     }
0486     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
0487     {
0488       deviceaddress = NOR_MEMORY_ADRESS3;
0489     }
0490     else /* FMC_NORSRAM_BANK4 */
0491     {
0492       deviceaddress = NOR_MEMORY_ADRESS4;
0493     }
0494 
0495     /* Send read ID command */
0496     if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
0497     {
0498       if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
0499       {
0500         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_BYTE),
0501                   NOR_CMD_DATA_FIRST);
0502         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND_BYTE),
0503                   NOR_CMD_DATA_SECOND);
0504         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD_BYTE),
0505                   NOR_CMD_DATA_AUTO_SELECT);
0506       }
0507       else
0508       {
0509         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
0510         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
0511         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD),
0512                   NOR_CMD_DATA_AUTO_SELECT);
0513       }
0514     }
0515     else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
0516     {
0517       NOR_WRITE(deviceaddress, NOR_CMD_DATA_AUTO_SELECT);
0518     }
0519     else
0520     {
0521       /* Primary command set not supported by the driver */
0522       status = HAL_ERROR;
0523     }
0524 
0525     if (status != HAL_ERROR)
0526     {
0527       /* Read the NOR IDs */
0528       pNOR_ID->Manufacturer_Code = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, MC_ADDRESS);
0529       pNOR_ID->Device_Code1      = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth,
0530                                                                      DEVICE_CODE1_ADDR);
0531       pNOR_ID->Device_Code2      = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth,
0532                                                                      DEVICE_CODE2_ADDR);
0533       pNOR_ID->Device_Code3      = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth,
0534                                                                      DEVICE_CODE3_ADDR);
0535     }
0536 
0537     /* Check the NOR controller state */
0538     hnor->State = state;
0539 
0540     /* Process unlocked */
0541     __HAL_UNLOCK(hnor);
0542   }
0543   else
0544   {
0545     return HAL_ERROR;
0546   }
0547 
0548   return status;
0549 }
0550 
0551 /**
0552   * @brief  Returns the NOR memory to Read mode.
0553   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
0554   *                the configuration information for NOR module.
0555   * @retval HAL status
0556   */
0557 HAL_StatusTypeDef HAL_NOR_ReturnToReadMode(NOR_HandleTypeDef *hnor)
0558 {
0559   uint32_t deviceaddress;
0560   HAL_NOR_StateTypeDef state;
0561   HAL_StatusTypeDef status = HAL_OK;
0562 
0563   /* Check the NOR controller state */
0564   state = hnor->State;
0565   if (state == HAL_NOR_STATE_BUSY)
0566   {
0567     return HAL_BUSY;
0568   }
0569   else if (state == HAL_NOR_STATE_PROTECTED)
0570   {
0571     return HAL_ERROR;
0572   }
0573   else if (state == HAL_NOR_STATE_READY)
0574   {
0575     /* Process Locked */
0576     __HAL_LOCK(hnor);
0577 
0578     /* Update the NOR controller state */
0579     hnor->State = HAL_NOR_STATE_BUSY;
0580 
0581     /* Select the NOR device address */
0582     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
0583     {
0584       deviceaddress = NOR_MEMORY_ADRESS1;
0585     }
0586     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
0587     {
0588       deviceaddress = NOR_MEMORY_ADRESS2;
0589     }
0590     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
0591     {
0592       deviceaddress = NOR_MEMORY_ADRESS3;
0593     }
0594     else /* FMC_NORSRAM_BANK4 */
0595     {
0596       deviceaddress = NOR_MEMORY_ADRESS4;
0597     }
0598 
0599     if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
0600     {
0601       NOR_WRITE(deviceaddress, NOR_CMD_DATA_READ_RESET);
0602     }
0603     else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
0604     {
0605       NOR_WRITE(deviceaddress, NOR_CMD_READ_ARRAY);
0606     }
0607     else
0608     {
0609       /* Primary command set not supported by the driver */
0610       status = HAL_ERROR;
0611     }
0612 
0613     /* Check the NOR controller state */
0614     hnor->State = state;
0615 
0616     /* Process unlocked */
0617     __HAL_UNLOCK(hnor);
0618   }
0619   else
0620   {
0621     return HAL_ERROR;
0622   }
0623 
0624   return status;
0625 }
0626 
0627 /**
0628   * @brief  Read data from NOR memory
0629   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
0630   *                the configuration information for NOR module.
0631   * @param  pAddress pointer to Device address
0632   * @param  pData  pointer to read data
0633   * @retval HAL status
0634   */
0635 HAL_StatusTypeDef HAL_NOR_Read(NOR_HandleTypeDef *hnor, uint32_t *pAddress, uint16_t *pData)
0636 {
0637   uint32_t deviceaddress;
0638   HAL_NOR_StateTypeDef state;
0639   HAL_StatusTypeDef status = HAL_OK;
0640 
0641   /* Check the NOR controller state */
0642   state = hnor->State;
0643   if (state == HAL_NOR_STATE_BUSY)
0644   {
0645     return HAL_BUSY;
0646   }
0647   else if (state == HAL_NOR_STATE_PROTECTED)
0648   {
0649     return HAL_ERROR;
0650   }
0651   else if (state == HAL_NOR_STATE_READY)
0652   {
0653     /* Process Locked */
0654     __HAL_LOCK(hnor);
0655 
0656     /* Update the NOR controller state */
0657     hnor->State = HAL_NOR_STATE_BUSY;
0658 
0659     /* Select the NOR device address */
0660     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
0661     {
0662       deviceaddress = NOR_MEMORY_ADRESS1;
0663     }
0664     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
0665     {
0666       deviceaddress = NOR_MEMORY_ADRESS2;
0667     }
0668     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
0669     {
0670       deviceaddress = NOR_MEMORY_ADRESS3;
0671     }
0672     else /* FMC_NORSRAM_BANK4 */
0673     {
0674       deviceaddress = NOR_MEMORY_ADRESS4;
0675     }
0676 
0677     /* Send read data command */
0678     if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
0679     {
0680       if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
0681       {
0682         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_BYTE),
0683                   NOR_CMD_DATA_FIRST);
0684         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND_BYTE),
0685                   NOR_CMD_DATA_SECOND);
0686         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD_BYTE),
0687                   NOR_CMD_DATA_READ_RESET);
0688       }
0689       else
0690       {
0691         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
0692         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
0693         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD),
0694                   NOR_CMD_DATA_READ_RESET);
0695       }
0696     }
0697     else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
0698     {
0699       NOR_WRITE(pAddress, NOR_CMD_READ_ARRAY);
0700     }
0701     else
0702     {
0703       /* Primary command set not supported by the driver */
0704       status = HAL_ERROR;
0705     }
0706 
0707     if (status != HAL_ERROR)
0708     {
0709       /* Read the data */
0710       *pData = (uint16_t)(*(__IO uint32_t *)pAddress);
0711     }
0712 
0713     /* Check the NOR controller state */
0714     hnor->State = state;
0715 
0716     /* Process unlocked */
0717     __HAL_UNLOCK(hnor);
0718   }
0719   else
0720   {
0721     return HAL_ERROR;
0722   }
0723 
0724   return status;
0725 }
0726 
0727 /**
0728   * @brief  Program data to NOR memory
0729   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
0730   *                the configuration information for NOR module.
0731   * @param  pAddress Device address
0732   * @param  pData  pointer to the data to write
0733   * @retval HAL status
0734   */
0735 HAL_StatusTypeDef HAL_NOR_Program(NOR_HandleTypeDef *hnor, uint32_t *pAddress, uint16_t *pData)
0736 {
0737   uint32_t deviceaddress;
0738   HAL_StatusTypeDef status = HAL_OK;
0739 
0740   /* Check the NOR controller state */
0741   if (hnor->State == HAL_NOR_STATE_BUSY)
0742   {
0743     return HAL_BUSY;
0744   }
0745   else if (hnor->State == HAL_NOR_STATE_READY)
0746   {
0747     /* Process Locked */
0748     __HAL_LOCK(hnor);
0749 
0750     /* Update the NOR controller state */
0751     hnor->State = HAL_NOR_STATE_BUSY;
0752 
0753     /* Select the NOR device address */
0754     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
0755     {
0756       deviceaddress = NOR_MEMORY_ADRESS1;
0757     }
0758     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
0759     {
0760       deviceaddress = NOR_MEMORY_ADRESS2;
0761     }
0762     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
0763     {
0764       deviceaddress = NOR_MEMORY_ADRESS3;
0765     }
0766     else /* FMC_NORSRAM_BANK4 */
0767     {
0768       deviceaddress = NOR_MEMORY_ADRESS4;
0769     }
0770 
0771     /* Send program data command */
0772     if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
0773     {
0774       if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
0775       {
0776         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_BYTE),
0777                   NOR_CMD_DATA_FIRST);
0778         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND_BYTE),
0779                   NOR_CMD_DATA_SECOND);
0780         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD_BYTE),
0781                   NOR_CMD_DATA_PROGRAM);
0782       }
0783       else
0784       {
0785         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
0786         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
0787         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD), NOR_CMD_DATA_PROGRAM);
0788       }
0789     }
0790     else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
0791     {
0792       NOR_WRITE(pAddress, NOR_CMD_WORD_PROGRAM);
0793     }
0794     else
0795     {
0796       /* Primary command set not supported by the driver */
0797       status = HAL_ERROR;
0798     }
0799 
0800     if (status != HAL_ERROR)
0801     {
0802       /* Write the data */
0803       NOR_WRITE(pAddress, *pData);
0804     }
0805 
0806     /* Check the NOR controller state */
0807     hnor->State = HAL_NOR_STATE_READY;
0808 
0809     /* Process unlocked */
0810     __HAL_UNLOCK(hnor);
0811   }
0812   else
0813   {
0814     return HAL_ERROR;
0815   }
0816 
0817   return status;
0818 }
0819 
0820 /**
0821   * @brief  Reads a half-word buffer from the NOR memory.
0822   * @param  hnor pointer to the NOR handle
0823   * @param  uwAddress NOR memory internal address to read from.
0824   * @param  pData pointer to the buffer that receives the data read from the
0825   *         NOR memory.
0826   * @param  uwBufferSize  number of Half word to read.
0827   * @retval HAL status
0828   */
0829 HAL_StatusTypeDef HAL_NOR_ReadBuffer(NOR_HandleTypeDef *hnor, uint32_t uwAddress, uint16_t *pData,
0830                                      uint32_t uwBufferSize)
0831 {
0832   uint32_t deviceaddress;
0833   uint32_t size = uwBufferSize;
0834   uint32_t address = uwAddress;
0835   uint16_t *data = pData;
0836   HAL_NOR_StateTypeDef state;
0837   HAL_StatusTypeDef status = HAL_OK;
0838 
0839   /* Check the NOR controller state */
0840   state = hnor->State;
0841   if (state == HAL_NOR_STATE_BUSY)
0842   {
0843     return HAL_BUSY;
0844   }
0845   else if (state == HAL_NOR_STATE_PROTECTED)
0846   {
0847     return HAL_ERROR;
0848   }
0849   else if (state == HAL_NOR_STATE_READY)
0850   {
0851     /* Process Locked */
0852     __HAL_LOCK(hnor);
0853 
0854     /* Update the NOR controller state */
0855     hnor->State = HAL_NOR_STATE_BUSY;
0856 
0857     /* Select the NOR device address */
0858     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
0859     {
0860       deviceaddress = NOR_MEMORY_ADRESS1;
0861     }
0862     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
0863     {
0864       deviceaddress = NOR_MEMORY_ADRESS2;
0865     }
0866     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
0867     {
0868       deviceaddress = NOR_MEMORY_ADRESS3;
0869     }
0870     else /* FMC_NORSRAM_BANK4 */
0871     {
0872       deviceaddress = NOR_MEMORY_ADRESS4;
0873     }
0874 
0875     /* Send read data command */
0876     if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
0877     {
0878       if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
0879       {
0880         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_BYTE),
0881                   NOR_CMD_DATA_FIRST);
0882         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND_BYTE),
0883                   NOR_CMD_DATA_SECOND);
0884         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD_BYTE),
0885                   NOR_CMD_DATA_READ_RESET);
0886       }
0887       else
0888       {
0889         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
0890         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
0891         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD),
0892                   NOR_CMD_DATA_READ_RESET);
0893       }
0894     }
0895     else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
0896     {
0897       NOR_WRITE(deviceaddress, NOR_CMD_READ_ARRAY);
0898     }
0899     else
0900     {
0901       /* Primary command set not supported by the driver */
0902       status = HAL_ERROR;
0903     }
0904 
0905     if (status != HAL_ERROR)
0906     {
0907       /* Read buffer */
0908       while (size > 0U)
0909       {
0910         *data = *(__IO uint16_t *)address;
0911         data++;
0912         address += 2U;
0913         size--;
0914       }
0915     }
0916 
0917     /* Check the NOR controller state */
0918     hnor->State = state;
0919 
0920     /* Process unlocked */
0921     __HAL_UNLOCK(hnor);
0922   }
0923   else
0924   {
0925     return HAL_ERROR;
0926   }
0927 
0928   return status;
0929 }
0930 
0931 /**
0932   * @brief  Writes a half-word buffer to the NOR memory. This function must be used
0933             only with S29GL128P NOR memory.
0934   * @param  hnor pointer to the NOR handle
0935   * @param  uwAddress NOR memory internal start write address
0936   * @param  pData pointer to source data buffer.
0937   * @param  uwBufferSize Size of the buffer to write
0938   * @retval HAL status
0939   */
0940 HAL_StatusTypeDef HAL_NOR_ProgramBuffer(NOR_HandleTypeDef *hnor, uint32_t uwAddress, uint16_t *pData,
0941                                         uint32_t uwBufferSize)
0942 {
0943   uint16_t *p_currentaddress;
0944   const uint16_t *p_endaddress;
0945   uint16_t *data = pData;
0946   uint32_t deviceaddress;
0947   HAL_StatusTypeDef status = HAL_OK;
0948 
0949   /* Check the NOR controller state */
0950   if (hnor->State == HAL_NOR_STATE_BUSY)
0951   {
0952     return HAL_BUSY;
0953   }
0954   else if (hnor->State == HAL_NOR_STATE_READY)
0955   {
0956     /* Process Locked */
0957     __HAL_LOCK(hnor);
0958 
0959     /* Update the NOR controller state */
0960     hnor->State = HAL_NOR_STATE_BUSY;
0961 
0962     /* Select the NOR device address */
0963     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
0964     {
0965       deviceaddress = NOR_MEMORY_ADRESS1;
0966     }
0967     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
0968     {
0969       deviceaddress = NOR_MEMORY_ADRESS2;
0970     }
0971     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
0972     {
0973       deviceaddress = NOR_MEMORY_ADRESS3;
0974     }
0975     else /* FMC_NORSRAM_BANK4 */
0976     {
0977       deviceaddress = NOR_MEMORY_ADRESS4;
0978     }
0979 
0980     /* Initialize variables */
0981     p_currentaddress  = (uint16_t *)(deviceaddress + uwAddress);
0982     p_endaddress      = (uint16_t *)(deviceaddress + uwAddress + (2U * (uwBufferSize - 1U)));
0983 
0984     if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
0985     {
0986       if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
0987       {
0988         /* Issue unlock command sequence */
0989         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_BYTE),
0990                   NOR_CMD_DATA_FIRST);
0991         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND_BYTE),
0992                   NOR_CMD_DATA_SECOND);
0993       }
0994       else
0995       {
0996         /* Issue unlock command sequence */
0997         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
0998         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
0999       }
1000       /* Write Buffer Load Command */
1001       NOR_WRITE((deviceaddress + uwAddress), NOR_CMD_DATA_BUFFER_AND_PROG);
1002       NOR_WRITE((deviceaddress + uwAddress), (uint16_t)(uwBufferSize - 1U));
1003     }
1004     else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
1005     {
1006       /* Write Buffer Load Command */
1007       NOR_WRITE((deviceaddress + uwAddress), NOR_CMD_BUFFERED_PROGRAM);
1008       NOR_WRITE((deviceaddress + uwAddress), (uint16_t)(uwBufferSize - 1U));
1009     }
1010     else
1011     {
1012       /* Primary command set not supported by the driver */
1013       status = HAL_ERROR;
1014     }
1015 
1016     if (status != HAL_ERROR)
1017     {
1018       /* Load Data into NOR Buffer */
1019       while (p_currentaddress <= p_endaddress)
1020       {
1021         NOR_WRITE(p_currentaddress, *data);
1022 
1023         data++;
1024         p_currentaddress ++;
1025       }
1026 
1027       if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
1028       {
1029         NOR_WRITE((deviceaddress + uwAddress), NOR_CMD_DATA_BUFFER_AND_PROG_CONFIRM);
1030       }
1031       else /* => hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET */
1032       {
1033         NOR_WRITE((deviceaddress + uwAddress), NOR_CMD_CONFIRM);
1034       }
1035     }
1036 
1037     /* Check the NOR controller state */
1038     hnor->State = HAL_NOR_STATE_READY;
1039 
1040     /* Process unlocked */
1041     __HAL_UNLOCK(hnor);
1042   }
1043   else
1044   {
1045     return HAL_ERROR;
1046   }
1047 
1048   return status;
1049 
1050 }
1051 
1052 /**
1053   * @brief  Erase the specified block of the NOR memory
1054   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
1055   *                the configuration information for NOR module.
1056   * @param  BlockAddress  Block to erase address
1057   * @param  Address Device address
1058   * @retval HAL status
1059   */
1060 HAL_StatusTypeDef HAL_NOR_Erase_Block(NOR_HandleTypeDef *hnor, uint32_t BlockAddress, uint32_t Address)
1061 {
1062   uint32_t deviceaddress;
1063   HAL_StatusTypeDef status = HAL_OK;
1064 
1065   /* Check the NOR controller state */
1066   if (hnor->State == HAL_NOR_STATE_BUSY)
1067   {
1068     return HAL_BUSY;
1069   }
1070   else if (hnor->State == HAL_NOR_STATE_READY)
1071   {
1072     /* Process Locked */
1073     __HAL_LOCK(hnor);
1074 
1075     /* Update the NOR controller state */
1076     hnor->State = HAL_NOR_STATE_BUSY;
1077 
1078     /* Select the NOR device address */
1079     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
1080     {
1081       deviceaddress = NOR_MEMORY_ADRESS1;
1082     }
1083     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
1084     {
1085       deviceaddress = NOR_MEMORY_ADRESS2;
1086     }
1087     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
1088     {
1089       deviceaddress = NOR_MEMORY_ADRESS3;
1090     }
1091     else /* FMC_NORSRAM_BANK4 */
1092     {
1093       deviceaddress = NOR_MEMORY_ADRESS4;
1094     }
1095 
1096     /* Send block erase command sequence */
1097     if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
1098     {
1099       if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
1100       {
1101         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_BYTE),
1102                   NOR_CMD_DATA_FIRST);
1103         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND_BYTE),
1104                   NOR_CMD_DATA_SECOND);
1105         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD_BYTE),
1106                   NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD);
1107       }
1108       else
1109       {
1110         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
1111         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
1112         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD),
1113                   NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD);
1114         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FOURTH),
1115                   NOR_CMD_DATA_CHIP_BLOCK_ERASE_FOURTH);
1116         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIFTH),
1117                   NOR_CMD_DATA_CHIP_BLOCK_ERASE_FIFTH);
1118       }
1119       NOR_WRITE((uint32_t)(BlockAddress + Address), NOR_CMD_DATA_BLOCK_ERASE);
1120     }
1121     else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
1122     {
1123       NOR_WRITE((BlockAddress + Address), NOR_CMD_BLOCK_UNLOCK);
1124       NOR_WRITE((BlockAddress + Address), NOR_CMD_CONFIRM);
1125       NOR_WRITE((BlockAddress + Address), NOR_CMD_BLOCK_ERASE);
1126       NOR_WRITE((BlockAddress + Address), NOR_CMD_CONFIRM);
1127     }
1128     else
1129     {
1130       /* Primary command set not supported by the driver */
1131       status = HAL_ERROR;
1132     }
1133 
1134     /* Check the NOR memory status and update the controller state */
1135     hnor->State = HAL_NOR_STATE_READY;
1136 
1137     /* Process unlocked */
1138     __HAL_UNLOCK(hnor);
1139   }
1140   else
1141   {
1142     return HAL_ERROR;
1143   }
1144 
1145   return status;
1146 
1147 }
1148 
1149 /**
1150   * @brief  Erase the entire NOR chip.
1151   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
1152   *                the configuration information for NOR module.
1153   * @param  Address  Device address
1154   * @retval HAL status
1155   */
1156 HAL_StatusTypeDef HAL_NOR_Erase_Chip(NOR_HandleTypeDef *hnor, uint32_t Address)
1157 {
1158   uint32_t deviceaddress;
1159   HAL_StatusTypeDef status = HAL_OK;
1160   UNUSED(Address);
1161 
1162   /* Check the NOR controller state */
1163   if (hnor->State == HAL_NOR_STATE_BUSY)
1164   {
1165     return HAL_BUSY;
1166   }
1167   else if (hnor->State == HAL_NOR_STATE_READY)
1168   {
1169     /* Process Locked */
1170     __HAL_LOCK(hnor);
1171 
1172     /* Update the NOR controller state */
1173     hnor->State = HAL_NOR_STATE_BUSY;
1174 
1175     /* Select the NOR device address */
1176     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
1177     {
1178       deviceaddress = NOR_MEMORY_ADRESS1;
1179     }
1180     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
1181     {
1182       deviceaddress = NOR_MEMORY_ADRESS2;
1183     }
1184     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
1185     {
1186       deviceaddress = NOR_MEMORY_ADRESS3;
1187     }
1188     else /* FMC_NORSRAM_BANK4 */
1189     {
1190       deviceaddress = NOR_MEMORY_ADRESS4;
1191     }
1192 
1193     /* Send NOR chip erase command sequence */
1194     if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
1195     {
1196       if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
1197       {
1198         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_BYTE),
1199                   NOR_CMD_DATA_FIRST);
1200         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND_BYTE),
1201                   NOR_CMD_DATA_SECOND);
1202         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD_BYTE),
1203                   NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD);
1204       }
1205       else
1206       {
1207         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
1208         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
1209         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD),
1210                   NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD);
1211         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FOURTH),
1212                   NOR_CMD_DATA_CHIP_BLOCK_ERASE_FOURTH);
1213         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIFTH),
1214                   NOR_CMD_DATA_CHIP_BLOCK_ERASE_FIFTH);
1215         NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SIXTH),
1216                   NOR_CMD_DATA_CHIP_ERASE);
1217       }
1218     }
1219     else
1220     {
1221       /* Primary command set not supported by the driver */
1222       status = HAL_ERROR;
1223     }
1224 
1225     /* Check the NOR memory status and update the controller state */
1226     hnor->State = HAL_NOR_STATE_READY;
1227 
1228     /* Process unlocked */
1229     __HAL_UNLOCK(hnor);
1230   }
1231   else
1232   {
1233     return HAL_ERROR;
1234   }
1235 
1236   return status;
1237 }
1238 
1239 /**
1240   * @brief  Read NOR flash CFI IDs
1241   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
1242   *                the configuration information for NOR module.
1243   * @param  pNOR_CFI  pointer to NOR CFI IDs structure
1244   * @retval HAL status
1245   */
1246 HAL_StatusTypeDef HAL_NOR_Read_CFI(NOR_HandleTypeDef *hnor, NOR_CFITypeDef *pNOR_CFI)
1247 {
1248   uint32_t deviceaddress;
1249   HAL_NOR_StateTypeDef state;
1250 
1251   /* Check the NOR controller state */
1252   state = hnor->State;
1253   if (state == HAL_NOR_STATE_BUSY)
1254   {
1255     return HAL_BUSY;
1256   }
1257   else if (state == HAL_NOR_STATE_PROTECTED)
1258   {
1259     return HAL_ERROR;
1260   }
1261   else if (state == HAL_NOR_STATE_READY)
1262   {
1263     /* Process Locked */
1264     __HAL_LOCK(hnor);
1265 
1266     /* Update the NOR controller state */
1267     hnor->State = HAL_NOR_STATE_BUSY;
1268 
1269     /* Select the NOR device address */
1270     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
1271     {
1272       deviceaddress = NOR_MEMORY_ADRESS1;
1273     }
1274     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
1275     {
1276       deviceaddress = NOR_MEMORY_ADRESS2;
1277     }
1278     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
1279     {
1280       deviceaddress = NOR_MEMORY_ADRESS3;
1281     }
1282     else /* FMC_NORSRAM_BANK4 */
1283     {
1284       deviceaddress = NOR_MEMORY_ADRESS4;
1285     }
1286 
1287     /* Send read CFI query command */
1288     if (uwNORMemoryDataWidth == NOR_MEMORY_8B)
1289     {
1290       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_CFI_BYTE),
1291                 NOR_CMD_DATA_CFI);
1292     }
1293     else
1294     {
1295       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_CFI), NOR_CMD_DATA_CFI);
1296     }
1297     /* read the NOR CFI information */
1298     pNOR_CFI->CFI_1 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI1_ADDRESS);
1299     pNOR_CFI->CFI_2 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI2_ADDRESS);
1300     pNOR_CFI->CFI_3 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI3_ADDRESS);
1301     pNOR_CFI->CFI_4 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI4_ADDRESS);
1302 
1303     /* Check the NOR controller state */
1304     hnor->State = state;
1305 
1306     /* Process unlocked */
1307     __HAL_UNLOCK(hnor);
1308   }
1309   else
1310   {
1311     return HAL_ERROR;
1312   }
1313 
1314   return HAL_OK;
1315 }
1316 
1317 #if (USE_HAL_NOR_REGISTER_CALLBACKS == 1)
1318 /**
1319   * @brief  Register a User NOR Callback
1320   *         To be used to override the weak predefined callback
1321   * @param hnor : NOR handle
1322   * @param CallbackId : ID of the callback to be registered
1323   *        This parameter can be one of the following values:
1324   *          @arg @ref HAL_NOR_MSP_INIT_CB_ID       NOR MspInit callback ID
1325   *          @arg @ref HAL_NOR_MSP_DEINIT_CB_ID     NOR MspDeInit callback ID
1326   * @param pCallback : pointer to the Callback function
1327   * @retval status
1328   */
1329 HAL_StatusTypeDef HAL_NOR_RegisterCallback(NOR_HandleTypeDef *hnor, HAL_NOR_CallbackIDTypeDef CallbackId,
1330                                            pNOR_CallbackTypeDef pCallback)
1331 {
1332   HAL_StatusTypeDef status = HAL_OK;
1333   HAL_NOR_StateTypeDef state;
1334 
1335   if (pCallback == NULL)
1336   {
1337     return HAL_ERROR;
1338   }
1339 
1340   state = hnor->State;
1341   if ((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_RESET) || (state == HAL_NOR_STATE_PROTECTED))
1342   {
1343     switch (CallbackId)
1344     {
1345       case HAL_NOR_MSP_INIT_CB_ID :
1346         hnor->MspInitCallback = pCallback;
1347         break;
1348       case HAL_NOR_MSP_DEINIT_CB_ID :
1349         hnor->MspDeInitCallback = pCallback;
1350         break;
1351       default :
1352         /* update return status */
1353         status =  HAL_ERROR;
1354         break;
1355     }
1356   }
1357   else
1358   {
1359     /* update return status */
1360     status =  HAL_ERROR;
1361   }
1362 
1363   return status;
1364 }
1365 
1366 /**
1367   * @brief  Unregister a User NOR Callback
1368   *         NOR Callback is redirected to the weak predefined callback
1369   * @param hnor : NOR handle
1370   * @param CallbackId : ID of the callback to be unregistered
1371   *        This parameter can be one of the following values:
1372   *          @arg @ref HAL_NOR_MSP_INIT_CB_ID       NOR MspInit callback ID
1373   *          @arg @ref HAL_NOR_MSP_DEINIT_CB_ID     NOR MspDeInit callback ID
1374   * @retval status
1375   */
1376 HAL_StatusTypeDef HAL_NOR_UnRegisterCallback(NOR_HandleTypeDef *hnor, HAL_NOR_CallbackIDTypeDef CallbackId)
1377 {
1378   HAL_StatusTypeDef status = HAL_OK;
1379   HAL_NOR_StateTypeDef state;
1380 
1381   state = hnor->State;
1382   if ((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_RESET) || (state == HAL_NOR_STATE_PROTECTED))
1383   {
1384     switch (CallbackId)
1385     {
1386       case HAL_NOR_MSP_INIT_CB_ID :
1387         hnor->MspInitCallback = HAL_NOR_MspInit;
1388         break;
1389       case HAL_NOR_MSP_DEINIT_CB_ID :
1390         hnor->MspDeInitCallback = HAL_NOR_MspDeInit;
1391         break;
1392       default :
1393         /* update return status */
1394         status =  HAL_ERROR;
1395         break;
1396     }
1397   }
1398   else
1399   {
1400     /* update return status */
1401     status =  HAL_ERROR;
1402   }
1403 
1404   return status;
1405 }
1406 #endif /* (USE_HAL_NOR_REGISTER_CALLBACKS) */
1407 
1408 /**
1409   * @}
1410   */
1411 
1412 /** @defgroup NOR_Exported_Functions_Group3 NOR Control functions
1413   * @ingroup RTEMSBSPsARMSTM32H7
1414   *  @brief   management functions
1415   *
1416 @verbatim
1417   ==============================================================================
1418                         ##### NOR Control functions #####
1419   ==============================================================================
1420   [..]
1421     This subsection provides a set of functions allowing to control dynamically
1422     the NOR interface.
1423 
1424 @endverbatim
1425   * @{
1426   */
1427 
1428 /**
1429   * @brief  Enables dynamically NOR write operation.
1430   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
1431   *                the configuration information for NOR module.
1432   * @retval HAL status
1433   */
1434 HAL_StatusTypeDef HAL_NOR_WriteOperation_Enable(NOR_HandleTypeDef *hnor)
1435 {
1436   /* Check the NOR controller state */
1437   if (hnor->State == HAL_NOR_STATE_PROTECTED)
1438   {
1439     /* Process Locked */
1440     __HAL_LOCK(hnor);
1441 
1442     /* Update the NOR controller state */
1443     hnor->State = HAL_NOR_STATE_BUSY;
1444 
1445     /* Enable write operation */
1446     (void)FMC_NORSRAM_WriteOperation_Enable(hnor->Instance, hnor->Init.NSBank);
1447 
1448     /* Update the NOR controller state */
1449     hnor->State = HAL_NOR_STATE_READY;
1450 
1451     /* Process unlocked */
1452     __HAL_UNLOCK(hnor);
1453   }
1454   else
1455   {
1456     return HAL_ERROR;
1457   }
1458 
1459   return HAL_OK;
1460 }
1461 
1462 /**
1463   * @brief  Disables dynamically NOR write operation.
1464   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
1465   *                the configuration information for NOR module.
1466   * @retval HAL status
1467   */
1468 HAL_StatusTypeDef HAL_NOR_WriteOperation_Disable(NOR_HandleTypeDef *hnor)
1469 {
1470   /* Check the NOR controller state */
1471   if (hnor->State == HAL_NOR_STATE_READY)
1472   {
1473     /* Process Locked */
1474     __HAL_LOCK(hnor);
1475 
1476     /* Update the NOR controller state */
1477     hnor->State = HAL_NOR_STATE_BUSY;
1478 
1479     /* Disable write operation */
1480     (void)FMC_NORSRAM_WriteOperation_Disable(hnor->Instance, hnor->Init.NSBank);
1481 
1482     /* Update the NOR controller state */
1483     hnor->State = HAL_NOR_STATE_PROTECTED;
1484 
1485     /* Process unlocked */
1486     __HAL_UNLOCK(hnor);
1487   }
1488   else
1489   {
1490     return HAL_ERROR;
1491   }
1492 
1493   return HAL_OK;
1494 }
1495 
1496 /**
1497   * @}
1498   */
1499 
1500 /** @defgroup NOR_Exported_Functions_Group4 NOR State functions
1501   * @ingroup RTEMSBSPsARMSTM32H7
1502   *  @brief   Peripheral State functions
1503   *
1504 @verbatim
1505   ==============================================================================
1506                       ##### NOR State functions #####
1507   ==============================================================================
1508   [..]
1509     This subsection permits to get in run-time the status of the NOR controller
1510     and the data flow.
1511 
1512 @endverbatim
1513   * @{
1514   */
1515 
1516 /**
1517   * @brief  return the NOR controller state
1518   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
1519   *                the configuration information for NOR module.
1520   * @retval NOR controller state
1521   */
1522 HAL_NOR_StateTypeDef HAL_NOR_GetState(const NOR_HandleTypeDef *hnor)
1523 {
1524   return hnor->State;
1525 }
1526 
1527 /**
1528   * @brief  Returns the NOR operation status.
1529   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
1530   *                the configuration information for NOR module.
1531   * @param  Address Device address
1532   * @param  Timeout NOR programming Timeout
1533   * @retval NOR_Status The returned value can be: HAL_NOR_STATUS_SUCCESS, HAL_NOR_STATUS_ERROR
1534   *         or HAL_NOR_STATUS_TIMEOUT
1535   */
1536 HAL_NOR_StatusTypeDef HAL_NOR_GetStatus(NOR_HandleTypeDef *hnor, uint32_t Address, uint32_t Timeout)
1537 {
1538   HAL_NOR_StatusTypeDef status = HAL_NOR_STATUS_ONGOING;
1539   uint16_t tmpsr1;
1540   uint16_t tmpsr2;
1541   uint32_t tickstart;
1542 
1543   /* Poll on NOR memory Ready/Busy signal ------------------------------------*/
1544   HAL_NOR_MspWait(hnor, Timeout);
1545 
1546   /* Get the NOR memory operation status -------------------------------------*/
1547 
1548   /* Get tick */
1549   tickstart = HAL_GetTick();
1550 
1551   if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
1552   {
1553     while ((status != HAL_NOR_STATUS_SUCCESS) && (status != HAL_NOR_STATUS_TIMEOUT))
1554     {
1555       /* Check for the Timeout */
1556       if (Timeout != HAL_MAX_DELAY)
1557       {
1558         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1559         {
1560           status = HAL_NOR_STATUS_TIMEOUT;
1561         }
1562       }
1563 
1564       /* Read NOR status register (DQ6 and DQ5) */
1565       tmpsr1 = *(__IO uint16_t *)Address;
1566       tmpsr2 = *(__IO uint16_t *)Address;
1567 
1568       /* If DQ6 did not toggle between the two reads then return HAL_NOR_STATUS_SUCCESS  */
1569       if ((tmpsr1 & NOR_MASK_STATUS_DQ6) == (tmpsr2 & NOR_MASK_STATUS_DQ6))
1570       {
1571         return HAL_NOR_STATUS_SUCCESS ;
1572       }
1573 
1574       if ((tmpsr1 & NOR_MASK_STATUS_DQ5) == NOR_MASK_STATUS_DQ5)
1575       {
1576         status = HAL_NOR_STATUS_ONGOING;
1577       }
1578 
1579       tmpsr1 = *(__IO uint16_t *)Address;
1580       tmpsr2 = *(__IO uint16_t *)Address;
1581 
1582       /* If DQ6 did not toggle between the two reads then return HAL_NOR_STATUS_SUCCESS  */
1583       if ((tmpsr1 & NOR_MASK_STATUS_DQ6) == (tmpsr2 & NOR_MASK_STATUS_DQ6))
1584       {
1585         return HAL_NOR_STATUS_SUCCESS;
1586       }
1587       if ((tmpsr1 & NOR_MASK_STATUS_DQ5) == NOR_MASK_STATUS_DQ5)
1588       {
1589         return HAL_NOR_STATUS_ERROR;
1590       }
1591     }
1592   }
1593   else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
1594   {
1595     do
1596     {
1597       NOR_WRITE(Address, NOR_CMD_READ_STATUS_REG);
1598       tmpsr2 = *(__IO uint16_t *)(Address);
1599 
1600       /* Check for the Timeout */
1601       if (Timeout != HAL_MAX_DELAY)
1602       {
1603         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1604         {
1605           return HAL_NOR_STATUS_TIMEOUT;
1606         }
1607       }
1608     } while ((tmpsr2 & NOR_MASK_STATUS_DQ7) == 0U);
1609 
1610     NOR_WRITE(Address, NOR_CMD_READ_STATUS_REG);
1611     tmpsr1 = *(__IO uint16_t *)(Address);
1612     if ((tmpsr1  & (NOR_MASK_STATUS_DQ5 | NOR_MASK_STATUS_DQ4)) != 0U)
1613     {
1614       /* Clear the Status Register  */
1615       NOR_WRITE(Address, NOR_CMD_READ_STATUS_REG);
1616       status = HAL_NOR_STATUS_ERROR;
1617     }
1618     else
1619     {
1620       status = HAL_NOR_STATUS_SUCCESS;
1621     }
1622   }
1623   else
1624   {
1625     /* Primary command set not supported by the driver */
1626     status = HAL_NOR_STATUS_ERROR;
1627   }
1628 
1629   /* Return the operation status */
1630   return status;
1631 }
1632 
1633 /**
1634   * @}
1635   */
1636 
1637 /**
1638   * @}
1639   */
1640 
1641 /**
1642   * @}
1643   */
1644 
1645 #endif /* HAL_NOR_MODULE_ENABLED */
1646 
1647 /**
1648   * @}
1649   */
1650