STM32L443xx HAL User Manual
stm32l4xx_hal_nor.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_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) 2017 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 (surcharged) 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 (surcharged) functions.
00084       Exception done for MspInit and MspDeInit callbacks that are respectively
00085       reset to the legacy weak (surcharged) 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 (surcharged) callbacks are used.
00101 
00102   @endverbatim
00103   ******************************************************************************
00104   */
00105 
00106 /* Includes ------------------------------------------------------------------*/
00107 #include "stm32l4xx_hal.h"
00108 
00109 #if defined(FMC_BANK1)
00110 
00111 /** @addtogroup STM32L4xx_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                 (uint16_t)0x0555
00131 #define NOR_CMD_ADDRESS_FIRST_CFI             (uint16_t)0x0055
00132 #define NOR_CMD_ADDRESS_SECOND                (uint16_t)0x02AA
00133 #define NOR_CMD_ADDRESS_THIRD                 (uint16_t)0x0555
00134 #define NOR_CMD_ADDRESS_FOURTH                (uint16_t)0x0555
00135 #define NOR_CMD_ADDRESS_FIFTH                 (uint16_t)0x02AA
00136 #define NOR_CMD_ADDRESS_SIXTH                 (uint16_t)0x0555
00137 
00138 /* Constants to define data to program a command */
00139 #define NOR_CMD_DATA_READ_RESET               (uint16_t)0x00F0
00140 #define NOR_CMD_DATA_FIRST                    (uint16_t)0x00AA
00141 #define NOR_CMD_DATA_SECOND                   (uint16_t)0x0055
00142 #define NOR_CMD_DATA_AUTO_SELECT              (uint16_t)0x0090
00143 #define NOR_CMD_DATA_PROGRAM                  (uint16_t)0x00A0
00144 #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD   (uint16_t)0x0080
00145 #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_FOURTH  (uint16_t)0x00AA
00146 #define NOR_CMD_DATA_CHIP_BLOCK_ERASE_FIFTH   (uint16_t)0x0055
00147 #define NOR_CMD_DATA_CHIP_ERASE               (uint16_t)0x0010
00148 #define NOR_CMD_DATA_CFI                      (uint16_t)0x0098
00149 
00150 #define NOR_CMD_DATA_BUFFER_AND_PROG          (uint8_t)0x25
00151 #define NOR_CMD_DATA_BUFFER_AND_PROG_CONFIRM  (uint8_t)0x29
00152 #define NOR_CMD_DATA_BLOCK_ERASE              (uint8_t)0x30
00153 
00154 #define NOR_CMD_READ_ARRAY                    (uint16_t)0x00FF
00155 #define NOR_CMD_WORD_PROGRAM                  (uint16_t)0x0040
00156 #define NOR_CMD_BUFFERED_PROGRAM              (uint16_t)0x00E8
00157 #define NOR_CMD_CONFIRM                       (uint16_t)0x00D0
00158 #define NOR_CMD_BLOCK_ERASE                   (uint16_t)0x0020
00159 #define NOR_CMD_BLOCK_UNLOCK                  (uint16_t)0x0060
00160 #define NOR_CMD_READ_STATUS_REG               (uint16_t)0x0070
00161 #define NOR_CMD_CLEAR_STATUS_REG              (uint16_t)0x0050
00162 
00163 /* Mask on NOR STATUS REGISTER */
00164 #define NOR_MASK_STATUS_DQ4                   (uint16_t)0x0010
00165 #define NOR_MASK_STATUS_DQ5                   (uint16_t)0x0020
00166 #define NOR_MASK_STATUS_DQ6                   (uint16_t)0x0040
00167 #define NOR_MASK_STATUS_DQ7                   (uint16_t)0x0080
00168 
00169 /* Address of the primary command set */
00170 #define NOR_ADDRESS_COMMAND_SET               (uint16_t)0x0013
00171 
00172 /* Command set code assignment (defined in JEDEC JEP137B version may 2004) */
00173 #define NOR_INTEL_SHARP_EXT_COMMAND_SET       (uint16_t)0x0001 /* Supported in this driver */
00174 #define NOR_AMD_FUJITSU_COMMAND_SET           (uint16_t)0x0002 /* Supported in this driver */
00175 #define NOR_INTEL_STANDARD_COMMAND_SET        (uint16_t)0x0003 /* Not Supported in this driver */
00176 #define NOR_AMD_FUJITSU_EXT_COMMAND_SET       (uint16_t)0x0004 /* Not Supported in this driver */
00177 #define NOR_WINDBOND_STANDARD_COMMAND_SET     (uint16_t)0x0006 /* Not Supported in this driver */
00178 #define NOR_MITSUBISHI_STANDARD_COMMAND_SET   (uint16_t)0x0100 /* Not Supported in this driver */
00179 #define NOR_MITSUBISHI_EXT_COMMAND_SET        (uint16_t)0x0101 /* Not Supported in this driver */
00180 #define NOR_PAGE_WRITE_COMMAND_SET            (uint16_t)0x0102 /* Not Supported in this driver */
00181 #define NOR_INTEL_PERFORMANCE_COMMAND_SET     (uint16_t)0x0200 /* Not Supported in this driver */
00182 #define NOR_INTEL_DATA_COMMAND_SET            (uint16_t)0x0210 /* Not Supported in this driver */
00183 
00184 /**
00185   * @}
00186   */
00187 
00188 /* Private macro -------------------------------------------------------------*/
00189 /* Private variables ---------------------------------------------------------*/
00190 /** @defgroup NOR_Private_Variables NOR Private Variables
00191   * @{
00192   */
00193 
00194 static uint32_t uwNORMemoryDataWidth  = NOR_MEMORY_8B;
00195 
00196 /**
00197   * @}
00198   */
00199 
00200 /* Private functions ---------------------------------------------------------*/
00201 /* Exported functions --------------------------------------------------------*/
00202 /** @defgroup NOR_Exported_Functions NOR Exported Functions
00203   * @{
00204   */
00205 
00206 /** @defgroup NOR_Exported_Functions_Group1 Initialization and de-initialization functions
00207   * @brief    Initialization and Configuration functions
00208   *
00209   @verbatim
00210   ==============================================================================
00211            ##### NOR Initialization and de_initialization functions #####
00212   ==============================================================================
00213   [..]
00214     This section provides functions allowing to initialize/de-initialize
00215     the NOR memory
00216 
00217 @endverbatim
00218   * @{
00219   */
00220 
00221 /**
00222   * @brief  Perform the NOR memory Initialization sequence
00223   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
00224   *                the configuration information for NOR module.
00225   * @param  Timing pointer to NOR control timing structure
00226   * @param  ExtTiming pointer to NOR extended mode timing structure
00227   * @retval HAL status
00228   */
00229 HAL_StatusTypeDef HAL_NOR_Init(NOR_HandleTypeDef *hnor, FMC_NORSRAM_TimingTypeDef *Timing,
00230                                FMC_NORSRAM_TimingTypeDef *ExtTiming)
00231 {
00232   uint32_t deviceaddress;
00233   HAL_StatusTypeDef status = HAL_OK;
00234 
00235   /* Check the NOR handle parameter */
00236   if (hnor == NULL)
00237   {
00238     return HAL_ERROR;
00239   }
00240 
00241   if (hnor->State == HAL_NOR_STATE_RESET)
00242   {
00243     /* Allocate lock resource and initialize it */
00244     hnor->Lock = HAL_UNLOCKED;
00245 
00246 #if (USE_HAL_NOR_REGISTER_CALLBACKS == 1)
00247     if (hnor->MspInitCallback == NULL)
00248     {
00249       hnor->MspInitCallback = HAL_NOR_MspInit;
00250     }
00251 
00252     /* Init the low level hardware */
00253     hnor->MspInitCallback(hnor);
00254 #else
00255     /* Initialize the low level hardware (MSP) */
00256     HAL_NOR_MspInit(hnor);
00257 #endif /* (USE_HAL_NOR_REGISTER_CALLBACKS) */
00258   }
00259 
00260   /* Initialize NOR control Interface */
00261   (void)FMC_NORSRAM_Init(hnor->Instance, &(hnor->Init));
00262 
00263   /* Initialize NOR timing Interface */
00264   (void)FMC_NORSRAM_Timing_Init(hnor->Instance, Timing, hnor->Init.NSBank);
00265 
00266   /* Initialize NOR extended mode timing Interface */
00267   (void)FMC_NORSRAM_Extended_Timing_Init(hnor->Extended, ExtTiming, hnor->Init.NSBank, hnor->Init.ExtendedMode);
00268 
00269   /* Enable the NORSRAM device */
00270   __FMC_NORSRAM_ENABLE(hnor->Instance, hnor->Init.NSBank);
00271 
00272   /* Initialize NOR Memory Data Width*/
00273   if (hnor->Init.MemoryDataWidth == FMC_NORSRAM_MEM_BUS_WIDTH_8)
00274   {
00275     uwNORMemoryDataWidth = NOR_MEMORY_8B;
00276   }
00277   else
00278   {
00279     uwNORMemoryDataWidth = NOR_MEMORY_16B;
00280   }
00281 
00282   /* Initialize the NOR controller state */
00283   hnor->State = HAL_NOR_STATE_READY;
00284 
00285   /* Select the NOR device address */
00286   if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
00287   {
00288     deviceaddress = NOR_MEMORY_ADRESS1;
00289   }
00290   else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
00291   {
00292     deviceaddress = NOR_MEMORY_ADRESS2;
00293   }
00294   else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
00295   {
00296     deviceaddress = NOR_MEMORY_ADRESS3;
00297   }
00298   else /* FMC_NORSRAM_BANK4 */
00299   {
00300     deviceaddress = NOR_MEMORY_ADRESS4;
00301   }
00302 
00303   FMC_NORSRAM_WriteOperation_Enable(hnor->Instance,hnor->Init.NSBank);
00304   
00305   /* Get the value of the command set */
00306   NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_CFI), NOR_CMD_DATA_CFI);
00307   hnor->CommandSet = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_ADDRESS_COMMAND_SET);
00308   
00309   status = HAL_NOR_ReturnToReadMode(hnor);
00310 
00311   if (hnor->Init.WriteOperation == FMC_WRITE_OPERATION_DISABLE)
00312   {
00313     FMC_NORSRAM_WriteOperation_Disable(hnor->Instance,hnor->Init.NSBank);
00314 
00315     /* Update the NOR controller state */
00316     hnor->State = HAL_NOR_STATE_PROTECTED;
00317   }
00318 
00319   return status;
00320 }
00321 
00322 /**
00323   * @brief  Perform NOR memory De-Initialization sequence
00324   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
00325   *                the configuration information for NOR module.
00326   * @retval HAL status
00327   */
00328 HAL_StatusTypeDef HAL_NOR_DeInit(NOR_HandleTypeDef *hnor)
00329 {
00330 #if (USE_HAL_NOR_REGISTER_CALLBACKS == 1)
00331   if (hnor->MspDeInitCallback == NULL)
00332   {
00333     hnor->MspDeInitCallback = HAL_NOR_MspDeInit;
00334   }
00335 
00336   /* DeInit the low level hardware */
00337   hnor->MspDeInitCallback(hnor);
00338 #else
00339   /* De-Initialize the low level hardware (MSP) */
00340   HAL_NOR_MspDeInit(hnor);
00341 #endif /* (USE_HAL_NOR_REGISTER_CALLBACKS) */
00342 
00343   /* Configure the NOR registers with their reset values */
00344   (void)FMC_NORSRAM_DeInit(hnor->Instance, hnor->Extended, hnor->Init.NSBank);
00345 
00346   /* Reset the NOR controller state */
00347   hnor->State = HAL_NOR_STATE_RESET;
00348 
00349   /* Release Lock */
00350   __HAL_UNLOCK(hnor);
00351 
00352   return HAL_OK;
00353 }
00354 
00355 /**
00356   * @brief  NOR MSP Init
00357   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
00358   *                the configuration information for NOR module.
00359   * @retval None
00360   */
00361 __weak void HAL_NOR_MspInit(NOR_HandleTypeDef *hnor)
00362 {
00363   /* Prevent unused argument(s) compilation warning */
00364   UNUSED(hnor);
00365 
00366   /* NOTE : This function Should not be modified, when the callback is needed,
00367             the HAL_NOR_MspInit could be implemented in the user file
00368    */
00369 }
00370 
00371 /**
00372   * @brief  NOR MSP DeInit
00373   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
00374   *                the configuration information for NOR module.
00375   * @retval None
00376   */
00377 __weak void HAL_NOR_MspDeInit(NOR_HandleTypeDef *hnor)
00378 {
00379   /* Prevent unused argument(s) compilation warning */
00380   UNUSED(hnor);
00381 
00382   /* NOTE : This function Should not be modified, when the callback is needed,
00383             the HAL_NOR_MspDeInit could be implemented in the user file
00384    */
00385 }
00386 
00387 /**
00388   * @brief  NOR MSP Wait for Ready/Busy signal
00389   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
00390   *                the configuration information for NOR module.
00391   * @param  Timeout Maximum timeout value
00392   * @retval None
00393   */
00394 __weak void HAL_NOR_MspWait(NOR_HandleTypeDef *hnor, uint32_t Timeout)
00395 {
00396   /* Prevent unused argument(s) compilation warning */
00397   UNUSED(hnor);
00398   UNUSED(Timeout);
00399 
00400   /* NOTE : This function Should not be modified, when the callback is needed,
00401             the HAL_NOR_MspWait could be implemented in the user file
00402    */
00403 }
00404 
00405 /**
00406   * @}
00407   */
00408 
00409 /** @defgroup NOR_Exported_Functions_Group2 Input and Output functions
00410   * @brief    Input Output and memory control functions
00411   *
00412   @verbatim
00413   ==============================================================================
00414                 ##### NOR Input and Output functions #####
00415   ==============================================================================
00416   [..]
00417     This section provides functions allowing to use and control the NOR memory
00418 
00419 @endverbatim
00420   * @{
00421   */
00422 
00423 /**
00424   * @brief  Read NOR flash IDs
00425   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
00426   *                the configuration information for NOR module.
00427   * @param  pNOR_ID  pointer to NOR ID structure
00428   * @retval HAL status
00429   */
00430 HAL_StatusTypeDef HAL_NOR_Read_ID(NOR_HandleTypeDef *hnor, NOR_IDTypeDef *pNOR_ID)
00431 {
00432   uint32_t deviceaddress;
00433   HAL_NOR_StateTypeDef state;
00434   HAL_StatusTypeDef status = HAL_OK;
00435 
00436   /* Check the NOR controller state */
00437   state = hnor->State;
00438   if (state == HAL_NOR_STATE_BUSY)
00439   {
00440     return HAL_BUSY;
00441   }
00442   else if ((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_PROTECTED))
00443   {
00444     /* Process Locked */
00445     __HAL_LOCK(hnor);
00446 
00447     /* Update the NOR controller state */
00448     hnor->State = HAL_NOR_STATE_BUSY;
00449 
00450     /* Select the NOR device address */
00451     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
00452     {
00453       deviceaddress = NOR_MEMORY_ADRESS1;
00454     }
00455     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
00456     {
00457       deviceaddress = NOR_MEMORY_ADRESS2;
00458     }
00459     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
00460     {
00461       deviceaddress = NOR_MEMORY_ADRESS3;
00462     }
00463     else /* FMC_NORSRAM_BANK4 */
00464     {
00465       deviceaddress = NOR_MEMORY_ADRESS4;
00466     }
00467 
00468     /* Send read ID command */
00469     if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
00470     {
00471       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
00472       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
00473       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD), NOR_CMD_DATA_AUTO_SELECT);
00474     }
00475     else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
00476     {
00477       NOR_WRITE(deviceaddress, NOR_CMD_DATA_AUTO_SELECT);
00478     }
00479     else
00480     {
00481       /* Primary command set not supported by the driver */
00482       status = HAL_ERROR;
00483     }
00484 
00485     if (status != HAL_ERROR)
00486     {
00487       /* Read the NOR IDs */
00488       pNOR_ID->Manufacturer_Code = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, MC_ADDRESS);
00489       pNOR_ID->Device_Code1      = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth,
00490                                                                      DEVICE_CODE1_ADDR);
00491       pNOR_ID->Device_Code2      = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth,
00492                                                                      DEVICE_CODE2_ADDR);
00493       pNOR_ID->Device_Code3      = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth,
00494                                                                      DEVICE_CODE3_ADDR);
00495     }
00496 
00497     /* Check the NOR controller state */
00498     hnor->State = state;
00499 
00500     /* Process unlocked */
00501     __HAL_UNLOCK(hnor);
00502   }
00503   else
00504   {
00505     return HAL_ERROR;
00506   }
00507 
00508   return status;
00509 }
00510 
00511 /**
00512   * @brief  Returns the NOR memory to Read mode.
00513   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
00514   *                the configuration information for NOR module.
00515   * @retval HAL status
00516   */
00517 HAL_StatusTypeDef HAL_NOR_ReturnToReadMode(NOR_HandleTypeDef *hnor)
00518 {
00519   uint32_t deviceaddress;
00520   HAL_NOR_StateTypeDef state;
00521   HAL_StatusTypeDef status = HAL_OK;
00522 
00523   /* Check the NOR controller state */
00524   state = hnor->State;
00525   if (state == HAL_NOR_STATE_BUSY)
00526   {
00527     return HAL_BUSY;
00528   }
00529   else if ((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_PROTECTED))
00530   {
00531     /* Process Locked */
00532     __HAL_LOCK(hnor);
00533 
00534     /* Update the NOR controller state */
00535     hnor->State = HAL_NOR_STATE_BUSY;
00536 
00537     /* Select the NOR device address */
00538     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
00539     {
00540       deviceaddress = NOR_MEMORY_ADRESS1;
00541     }
00542     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
00543     {
00544       deviceaddress = NOR_MEMORY_ADRESS2;
00545     }
00546     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
00547     {
00548       deviceaddress = NOR_MEMORY_ADRESS3;
00549     }
00550     else /* FMC_NORSRAM_BANK4 */
00551     {
00552       deviceaddress = NOR_MEMORY_ADRESS4;
00553     }
00554 
00555     if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
00556     {
00557       NOR_WRITE(deviceaddress, NOR_CMD_DATA_READ_RESET);
00558     }
00559     else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
00560     {
00561       NOR_WRITE(deviceaddress, NOR_CMD_READ_ARRAY);
00562     }
00563     else
00564     {
00565       /* Primary command set not supported by the driver */
00566       status = HAL_ERROR;
00567     }
00568 
00569     /* Check the NOR controller state */
00570     hnor->State = state;
00571 
00572     /* Process unlocked */
00573     __HAL_UNLOCK(hnor);
00574   }
00575   else
00576   {
00577     return HAL_ERROR;
00578   }
00579 
00580   return status;
00581 }
00582 
00583 /**
00584   * @brief  Read data from NOR memory
00585   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
00586   *                the configuration information for NOR module.
00587   * @param  pAddress pointer to Device address
00588   * @param  pData  pointer to read data
00589   * @retval HAL status
00590   */
00591 HAL_StatusTypeDef HAL_NOR_Read(NOR_HandleTypeDef *hnor, uint32_t *pAddress, uint16_t *pData)
00592 {
00593   uint32_t deviceaddress;
00594   HAL_NOR_StateTypeDef state;
00595   HAL_StatusTypeDef status = HAL_OK;
00596 
00597   /* Check the NOR controller state */
00598   state = hnor->State;
00599   if (state == HAL_NOR_STATE_BUSY)
00600   {
00601     return HAL_BUSY;
00602   }
00603   else if ((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_PROTECTED))
00604   {
00605     /* Process Locked */
00606     __HAL_LOCK(hnor);
00607 
00608     /* Update the NOR controller state */
00609     hnor->State = HAL_NOR_STATE_BUSY;
00610 
00611     /* Select the NOR device address */
00612     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
00613     {
00614       deviceaddress = NOR_MEMORY_ADRESS1;
00615     }
00616     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
00617     {
00618       deviceaddress = NOR_MEMORY_ADRESS2;
00619     }
00620     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
00621     {
00622       deviceaddress = NOR_MEMORY_ADRESS3;
00623     }
00624     else /* FMC_NORSRAM_BANK4 */
00625     {
00626       deviceaddress = NOR_MEMORY_ADRESS4;
00627     }
00628 
00629     /* Send read data command */
00630     if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
00631     {
00632       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
00633       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
00634       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD), NOR_CMD_DATA_READ_RESET);
00635     }
00636     else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
00637     {
00638       NOR_WRITE(pAddress, NOR_CMD_READ_ARRAY);
00639     }
00640     else
00641     {
00642       /* Primary command set not supported by the driver */
00643       status = HAL_ERROR;
00644     }
00645 
00646     if (status != HAL_ERROR)
00647     {
00648       /* Read the data */
00649       *pData = (uint16_t)(*(__IO uint32_t *)pAddress);
00650     }
00651 
00652     /* Check the NOR controller state */
00653     hnor->State = state;
00654 
00655     /* Process unlocked */
00656     __HAL_UNLOCK(hnor);
00657   }
00658   else
00659   {
00660     return HAL_ERROR;
00661   }
00662 
00663   return status;
00664 }
00665 
00666 /**
00667   * @brief  Program data to NOR memory
00668   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
00669   *                the configuration information for NOR module.
00670   * @param  pAddress Device address
00671   * @param  pData  pointer to the data to write
00672   * @retval HAL status
00673   */
00674 HAL_StatusTypeDef HAL_NOR_Program(NOR_HandleTypeDef *hnor, uint32_t *pAddress, uint16_t *pData)
00675 {
00676   uint32_t deviceaddress;
00677   HAL_StatusTypeDef status = HAL_OK;
00678 
00679   /* Check the NOR controller state */
00680   if (hnor->State == HAL_NOR_STATE_BUSY)
00681   {
00682     return HAL_BUSY;
00683   }
00684   else if (hnor->State == HAL_NOR_STATE_READY)
00685   {
00686     /* Process Locked */
00687     __HAL_LOCK(hnor);
00688 
00689     /* Update the NOR controller state */
00690     hnor->State = HAL_NOR_STATE_BUSY;
00691 
00692     /* Select the NOR device address */
00693     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
00694     {
00695       deviceaddress = NOR_MEMORY_ADRESS1;
00696     }
00697     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
00698     {
00699       deviceaddress = NOR_MEMORY_ADRESS2;
00700     }
00701     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
00702     {
00703       deviceaddress = NOR_MEMORY_ADRESS3;
00704     }
00705     else /* FMC_NORSRAM_BANK4 */
00706     {
00707       deviceaddress = NOR_MEMORY_ADRESS4;
00708     }
00709 
00710     /* Send program data command */
00711     if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
00712     {
00713       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
00714       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
00715       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD), NOR_CMD_DATA_PROGRAM);
00716     }
00717     else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
00718     {
00719       NOR_WRITE(pAddress, NOR_CMD_WORD_PROGRAM);
00720     }
00721     else
00722     {
00723       /* Primary command set not supported by the driver */
00724       status = HAL_ERROR;
00725     }
00726 
00727     if (status != HAL_ERROR)
00728     {
00729       /* Write the data */
00730       NOR_WRITE(pAddress, *pData);
00731     }
00732 
00733     /* Check the NOR controller state */
00734     hnor->State = HAL_NOR_STATE_READY;
00735 
00736     /* Process unlocked */
00737     __HAL_UNLOCK(hnor);
00738   }
00739   else
00740   {
00741     return HAL_ERROR;
00742   }
00743 
00744   return status;
00745 }
00746 
00747 /**
00748   * @brief  Reads a half-word buffer from the NOR memory.
00749   * @param  hnor pointer to the NOR handle
00750   * @param  uwAddress NOR memory internal address to read from.
00751   * @param  pData pointer to the buffer that receives the data read from the
00752   *         NOR memory.
00753   * @param  uwBufferSize  number of Half word to read.
00754   * @retval HAL status
00755   */
00756 HAL_StatusTypeDef HAL_NOR_ReadBuffer(NOR_HandleTypeDef *hnor, uint32_t uwAddress, uint16_t *pData,
00757                                      uint32_t uwBufferSize)
00758 {
00759   uint32_t deviceaddress;
00760   uint32_t size = uwBufferSize;
00761   uint32_t address = uwAddress;
00762   uint16_t *data = pData;
00763   HAL_NOR_StateTypeDef state;
00764   HAL_StatusTypeDef status = HAL_OK;
00765 
00766   /* Check the NOR controller state */
00767   state = hnor->State;
00768   if (state == HAL_NOR_STATE_BUSY)
00769   {
00770     return HAL_BUSY;
00771   }
00772   else if ((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_PROTECTED))
00773   {
00774     /* Process Locked */
00775     __HAL_LOCK(hnor);
00776 
00777     /* Update the NOR controller state */
00778     hnor->State = HAL_NOR_STATE_BUSY;
00779 
00780     /* Select the NOR device address */
00781     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
00782     {
00783       deviceaddress = NOR_MEMORY_ADRESS1;
00784     }
00785     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
00786     {
00787       deviceaddress = NOR_MEMORY_ADRESS2;
00788     }
00789     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
00790     {
00791       deviceaddress = NOR_MEMORY_ADRESS3;
00792     }
00793     else /* FMC_NORSRAM_BANK4 */
00794     {
00795       deviceaddress = NOR_MEMORY_ADRESS4;
00796     }
00797 
00798     /* Send read data command */
00799     if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
00800     {
00801       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
00802       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
00803       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD), NOR_CMD_DATA_READ_RESET);
00804     }
00805     else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
00806     {
00807       NOR_WRITE(deviceaddress, NOR_CMD_READ_ARRAY);
00808     }
00809     else
00810     {
00811       /* Primary command set not supported by the driver */
00812       status = HAL_ERROR;
00813     }
00814 
00815     if (status != HAL_ERROR)
00816     {
00817       /* Read buffer */
00818       while (size > 0U)
00819       {
00820         *data = *(__IO uint16_t *)address;
00821         data++;
00822         address += 2U;
00823         size--;
00824       }
00825     }
00826 
00827     /* Check the NOR controller state */
00828     hnor->State = state;
00829 
00830     /* Process unlocked */
00831     __HAL_UNLOCK(hnor);
00832   }
00833   else
00834   {
00835     return HAL_ERROR;
00836   }
00837 
00838   return status;
00839 }
00840 
00841 /**
00842   * @brief  Writes a half-word buffer to the NOR memory. This function must be used
00843             only with S29GL128P NOR memory.
00844   * @param  hnor pointer to the NOR handle
00845   * @param  uwAddress NOR memory internal start write address
00846   * @param  pData pointer to source data buffer.
00847   * @param  uwBufferSize Size of the buffer to write
00848   * @retval HAL status
00849   */
00850 HAL_StatusTypeDef HAL_NOR_ProgramBuffer(NOR_HandleTypeDef *hnor, uint32_t uwAddress, uint16_t *pData,
00851                                         uint32_t uwBufferSize)
00852 {
00853   uint16_t *p_currentaddress;
00854   const uint16_t *p_endaddress;
00855   uint16_t *data = pData;
00856   uint32_t deviceaddress;
00857   HAL_StatusTypeDef status = HAL_OK;
00858 
00859   /* Check the NOR controller state */
00860   if (hnor->State == HAL_NOR_STATE_BUSY)
00861   {
00862     return HAL_BUSY;
00863   }
00864   else if (hnor->State == HAL_NOR_STATE_READY)
00865   {
00866     /* Process Locked */
00867     __HAL_LOCK(hnor);
00868 
00869     /* Update the NOR controller state */
00870     hnor->State = HAL_NOR_STATE_BUSY;
00871 
00872     /* Select the NOR device address */
00873     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
00874     {
00875       deviceaddress = NOR_MEMORY_ADRESS1;
00876     }
00877     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
00878     {
00879       deviceaddress = NOR_MEMORY_ADRESS2;
00880     }
00881     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
00882     {
00883       deviceaddress = NOR_MEMORY_ADRESS3;
00884     }
00885     else /* FMC_NORSRAM_BANK4 */
00886     {
00887       deviceaddress = NOR_MEMORY_ADRESS4;
00888     }
00889 
00890     /* Initialize variables */
00891     p_currentaddress  = (uint16_t *)(deviceaddress + uwAddress);
00892     p_endaddress      = (uint16_t *)(deviceaddress + uwAddress + (2U * (uwBufferSize - 1U)));
00893 
00894     if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
00895     {
00896       /* Issue unlock command sequence */
00897       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
00898       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
00899 
00900       /* Write Buffer Load Command */
00901       NOR_WRITE((deviceaddress + uwAddress), NOR_CMD_DATA_BUFFER_AND_PROG);
00902       NOR_WRITE((deviceaddress + uwAddress), (uint16_t)(uwBufferSize - 1U));
00903     }
00904     else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
00905     {
00906       /* Write Buffer Load Command */
00907       NOR_WRITE((deviceaddress + uwAddress), NOR_CMD_BUFFERED_PROGRAM);
00908       NOR_WRITE((deviceaddress + uwAddress), (uint16_t)(uwBufferSize - 1U));
00909     }
00910     else
00911     {
00912       /* Primary command set not supported by the driver */
00913       status = HAL_ERROR;
00914     }
00915 
00916     if (status != HAL_ERROR)
00917     {
00918       /* Load Data into NOR Buffer */
00919       while (p_currentaddress <= p_endaddress)
00920       {
00921         NOR_WRITE(p_currentaddress, *data);
00922 
00923         data++;
00924         p_currentaddress ++;
00925       }
00926 
00927       if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
00928       {
00929         NOR_WRITE((deviceaddress + uwAddress), NOR_CMD_DATA_BUFFER_AND_PROG_CONFIRM);
00930       }
00931       else /* => hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET */
00932       {
00933         NOR_WRITE((deviceaddress + uwAddress), NOR_CMD_CONFIRM);
00934       }
00935     }
00936 
00937     /* Check the NOR controller state */
00938     hnor->State = HAL_NOR_STATE_READY;
00939 
00940     /* Process unlocked */
00941     __HAL_UNLOCK(hnor);
00942   }
00943   else
00944   {
00945     return HAL_ERROR;
00946   }
00947 
00948   return status;
00949 
00950 }
00951 
00952 /**
00953   * @brief  Erase the specified block of the NOR memory
00954   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
00955   *                the configuration information for NOR module.
00956   * @param  BlockAddress  Block to erase address
00957   * @param  Address Device address
00958   * @retval HAL status
00959   */
00960 HAL_StatusTypeDef HAL_NOR_Erase_Block(NOR_HandleTypeDef *hnor, uint32_t BlockAddress, uint32_t Address)
00961 {
00962   uint32_t deviceaddress;
00963   HAL_StatusTypeDef status = HAL_OK;
00964 
00965   /* Check the NOR controller state */
00966   if (hnor->State == HAL_NOR_STATE_BUSY)
00967   {
00968     return HAL_BUSY;
00969   }
00970   else if (hnor->State == HAL_NOR_STATE_READY)
00971   {
00972     /* Process Locked */
00973     __HAL_LOCK(hnor);
00974 
00975     /* Update the NOR controller state */
00976     hnor->State = HAL_NOR_STATE_BUSY;
00977 
00978     /* Select the NOR device address */
00979     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
00980     {
00981       deviceaddress = NOR_MEMORY_ADRESS1;
00982     }
00983     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
00984     {
00985       deviceaddress = NOR_MEMORY_ADRESS2;
00986     }
00987     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
00988     {
00989       deviceaddress = NOR_MEMORY_ADRESS3;
00990     }
00991     else /* FMC_NORSRAM_BANK4 */
00992     {
00993       deviceaddress = NOR_MEMORY_ADRESS4;
00994     }
00995 
00996     /* Send block erase command sequence */
00997     if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
00998     {
00999       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
01000       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
01001       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD),
01002                 NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD);
01003       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FOURTH),
01004                 NOR_CMD_DATA_CHIP_BLOCK_ERASE_FOURTH);
01005       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIFTH),
01006                 NOR_CMD_DATA_CHIP_BLOCK_ERASE_FIFTH);
01007       NOR_WRITE((uint32_t)(BlockAddress + Address), NOR_CMD_DATA_BLOCK_ERASE);
01008     }
01009     else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
01010     {
01011       NOR_WRITE((BlockAddress + Address), NOR_CMD_BLOCK_UNLOCK);
01012       NOR_WRITE((BlockAddress + Address), NOR_CMD_CONFIRM);
01013       NOR_WRITE((BlockAddress + Address), NOR_CMD_BLOCK_ERASE);
01014       NOR_WRITE((BlockAddress + Address), NOR_CMD_CONFIRM);
01015     }
01016     else
01017     {
01018       /* Primary command set not supported by the driver */
01019       status = HAL_ERROR;
01020     }
01021 
01022     /* Check the NOR memory status and update the controller state */
01023     hnor->State = HAL_NOR_STATE_READY;
01024 
01025     /* Process unlocked */
01026     __HAL_UNLOCK(hnor);
01027   }
01028   else
01029   {
01030     return HAL_ERROR;
01031   }
01032 
01033   return status;
01034 
01035 }
01036 
01037 /**
01038   * @brief  Erase the entire NOR chip.
01039   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
01040   *                the configuration information for NOR module.
01041   * @param  Address  Device address
01042   * @retval HAL status
01043   */
01044 HAL_StatusTypeDef HAL_NOR_Erase_Chip(NOR_HandleTypeDef *hnor, uint32_t Address)
01045 {
01046   uint32_t deviceaddress;
01047   HAL_StatusTypeDef status = HAL_OK;
01048   UNUSED(Address);
01049 
01050   /* Check the NOR controller state */
01051   if (hnor->State == HAL_NOR_STATE_BUSY)
01052   {
01053     return HAL_BUSY;
01054   }
01055   else if (hnor->State == HAL_NOR_STATE_READY)
01056   {
01057     /* Process Locked */
01058     __HAL_LOCK(hnor);
01059 
01060     /* Update the NOR controller state */
01061     hnor->State = HAL_NOR_STATE_BUSY;
01062 
01063     /* Select the NOR device address */
01064     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
01065     {
01066       deviceaddress = NOR_MEMORY_ADRESS1;
01067     }
01068     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
01069     {
01070       deviceaddress = NOR_MEMORY_ADRESS2;
01071     }
01072     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
01073     {
01074       deviceaddress = NOR_MEMORY_ADRESS3;
01075     }
01076     else /* FMC_NORSRAM_BANK4 */
01077     {
01078       deviceaddress = NOR_MEMORY_ADRESS4;
01079     }
01080 
01081     /* Send NOR chip erase command sequence */
01082     if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
01083     {
01084       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST), NOR_CMD_DATA_FIRST);
01085       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SECOND), NOR_CMD_DATA_SECOND);
01086       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_THIRD),
01087                 NOR_CMD_DATA_CHIP_BLOCK_ERASE_THIRD);
01088       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FOURTH),
01089                 NOR_CMD_DATA_CHIP_BLOCK_ERASE_FOURTH);
01090       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIFTH),
01091                 NOR_CMD_DATA_CHIP_BLOCK_ERASE_FIFTH);
01092       NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_SIXTH), NOR_CMD_DATA_CHIP_ERASE);
01093     }
01094     else
01095     {
01096       /* Primary command set not supported by the driver */
01097       status = HAL_ERROR;
01098     }
01099 
01100     /* Check the NOR memory status and update the controller state */
01101     hnor->State = HAL_NOR_STATE_READY;
01102 
01103     /* Process unlocked */
01104     __HAL_UNLOCK(hnor);
01105   }
01106   else
01107   {
01108     return HAL_ERROR;
01109   }
01110 
01111   return status;
01112 }
01113 
01114 /**
01115   * @brief  Read NOR flash CFI IDs
01116   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
01117   *                the configuration information for NOR module.
01118   * @param  pNOR_CFI  pointer to NOR CFI IDs structure
01119   * @retval HAL status
01120   */
01121 HAL_StatusTypeDef HAL_NOR_Read_CFI(NOR_HandleTypeDef *hnor, NOR_CFITypeDef *pNOR_CFI)
01122 {
01123   uint32_t deviceaddress;
01124   HAL_NOR_StateTypeDef state;
01125 
01126   /* Check the NOR controller state */
01127   state = hnor->State;
01128   if (state == HAL_NOR_STATE_BUSY)
01129   {
01130     return HAL_BUSY;
01131   }
01132   else if ((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_PROTECTED))
01133   {
01134     /* Process Locked */
01135     __HAL_LOCK(hnor);
01136 
01137     /* Update the NOR controller state */
01138     hnor->State = HAL_NOR_STATE_BUSY;
01139 
01140     /* Select the NOR device address */
01141     if (hnor->Init.NSBank == FMC_NORSRAM_BANK1)
01142     {
01143       deviceaddress = NOR_MEMORY_ADRESS1;
01144     }
01145     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK2)
01146     {
01147       deviceaddress = NOR_MEMORY_ADRESS2;
01148     }
01149     else if (hnor->Init.NSBank == FMC_NORSRAM_BANK3)
01150     {
01151       deviceaddress = NOR_MEMORY_ADRESS3;
01152     }
01153     else /* FMC_NORSRAM_BANK4 */
01154     {
01155       deviceaddress = NOR_MEMORY_ADRESS4;
01156     }
01157 
01158     /* Send read CFI query command */
01159     NOR_WRITE(NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, NOR_CMD_ADDRESS_FIRST_CFI), NOR_CMD_DATA_CFI);
01160 
01161     /* read the NOR CFI information */
01162     pNOR_CFI->CFI_1 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI1_ADDRESS);
01163     pNOR_CFI->CFI_2 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI2_ADDRESS);
01164     pNOR_CFI->CFI_3 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI3_ADDRESS);
01165     pNOR_CFI->CFI_4 = *(__IO uint16_t *) NOR_ADDR_SHIFT(deviceaddress, uwNORMemoryDataWidth, CFI4_ADDRESS);
01166 
01167     /* Check the NOR controller state */
01168     hnor->State = state;
01169 
01170     /* Process unlocked */
01171     __HAL_UNLOCK(hnor);
01172   }
01173   else
01174   {
01175     return HAL_ERROR;
01176   }
01177 
01178   return HAL_OK;
01179 }
01180 
01181 #if (USE_HAL_NOR_REGISTER_CALLBACKS == 1)
01182 /**
01183   * @brief  Register a User NOR Callback
01184   *         To be used instead of the weak (surcharged) predefined callback
01185   * @param hnor : NOR handle
01186   * @param CallbackId : ID of the callback to be registered
01187   *        This parameter can be one of the following values:
01188   *          @arg @ref HAL_NOR_MSP_INIT_CB_ID       NOR MspInit callback ID
01189   *          @arg @ref HAL_NOR_MSP_DEINIT_CB_ID     NOR MspDeInit callback ID
01190   * @param pCallback : pointer to the Callback function
01191   * @retval status
01192   */
01193 HAL_StatusTypeDef HAL_NOR_RegisterCallback(NOR_HandleTypeDef *hnor, HAL_NOR_CallbackIDTypeDef CallbackId,
01194                                            pNOR_CallbackTypeDef pCallback)
01195 {
01196   HAL_StatusTypeDef status = HAL_OK;
01197   HAL_NOR_StateTypeDef state;
01198 
01199   if (pCallback == NULL)
01200   {
01201     return HAL_ERROR;
01202   }
01203 
01204   /* Process locked */
01205   __HAL_LOCK(hnor);
01206 
01207   state = hnor->State;
01208   if ((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_RESET) || (state == HAL_NOR_STATE_PROTECTED))
01209   {
01210     switch (CallbackId)
01211     {
01212       case HAL_NOR_MSP_INIT_CB_ID :
01213         hnor->MspInitCallback = pCallback;
01214         break;
01215       case HAL_NOR_MSP_DEINIT_CB_ID :
01216         hnor->MspDeInitCallback = pCallback;
01217         break;
01218       default :
01219         /* update return status */
01220         status =  HAL_ERROR;
01221         break;
01222     }
01223   }
01224   else
01225   {
01226     /* update return status */
01227     status =  HAL_ERROR;
01228   }
01229 
01230   /* Release Lock */
01231   __HAL_UNLOCK(hnor);
01232   return status;
01233 }
01234 
01235 /**
01236   * @brief  Unregister a User NOR Callback
01237   *         NOR Callback is redirected to the weak (surcharged) predefined callback
01238   * @param hnor : NOR handle
01239   * @param CallbackId : ID of the callback to be unregistered
01240   *        This parameter can be one of the following values:
01241   *          @arg @ref HAL_NOR_MSP_INIT_CB_ID       NOR MspInit callback ID
01242   *          @arg @ref HAL_NOR_MSP_DEINIT_CB_ID     NOR MspDeInit callback ID
01243   * @retval status
01244   */
01245 HAL_StatusTypeDef HAL_NOR_UnRegisterCallback(NOR_HandleTypeDef *hnor, HAL_NOR_CallbackIDTypeDef CallbackId)
01246 {
01247   HAL_StatusTypeDef status = HAL_OK;
01248   HAL_NOR_StateTypeDef state;
01249 
01250   /* Process locked */
01251   __HAL_LOCK(hnor);
01252 
01253   state = hnor->State;
01254   if ((state == HAL_NOR_STATE_READY) || (state == HAL_NOR_STATE_RESET) || (state == HAL_NOR_STATE_PROTECTED))
01255   {
01256     switch (CallbackId)
01257     {
01258       case HAL_NOR_MSP_INIT_CB_ID :
01259         hnor->MspInitCallback = HAL_NOR_MspInit;
01260         break;
01261       case HAL_NOR_MSP_DEINIT_CB_ID :
01262         hnor->MspDeInitCallback = HAL_NOR_MspDeInit;
01263         break;
01264       default :
01265         /* update return status */
01266         status =  HAL_ERROR;
01267         break;
01268     }
01269   }
01270   else
01271   {
01272     /* update return status */
01273     status =  HAL_ERROR;
01274   }
01275 
01276   /* Release Lock */
01277   __HAL_UNLOCK(hnor);
01278   return status;
01279 }
01280 #endif /* (USE_HAL_NOR_REGISTER_CALLBACKS) */
01281 
01282 /**
01283   * @}
01284   */
01285 
01286 /** @defgroup NOR_Exported_Functions_Group3 NOR Control functions
01287   *  @brief   management functions
01288   *
01289 @verbatim
01290   ==============================================================================
01291                         ##### NOR Control functions #####
01292   ==============================================================================
01293   [..]
01294     This subsection provides a set of functions allowing to control dynamically
01295     the NOR interface.
01296 
01297 @endverbatim
01298   * @{
01299   */
01300 
01301 /**
01302   * @brief  Enables dynamically NOR write operation.
01303   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
01304   *                the configuration information for NOR module.
01305   * @retval HAL status
01306   */
01307 HAL_StatusTypeDef HAL_NOR_WriteOperation_Enable(NOR_HandleTypeDef *hnor)
01308 {
01309   /* Check the NOR controller state */
01310   if (hnor->State == HAL_NOR_STATE_PROTECTED)
01311   {
01312     /* Process Locked */
01313     __HAL_LOCK(hnor);
01314 
01315     /* Update the NOR controller state */
01316     hnor->State = HAL_NOR_STATE_BUSY;
01317 
01318     /* Enable write operation */
01319     (void)FMC_NORSRAM_WriteOperation_Enable(hnor->Instance, hnor->Init.NSBank);
01320 
01321     /* Update the NOR controller state */
01322     hnor->State = HAL_NOR_STATE_READY;
01323 
01324     /* Process unlocked */
01325     __HAL_UNLOCK(hnor);
01326   }
01327   else
01328   {
01329     return HAL_ERROR;
01330   }
01331 
01332   return HAL_OK;
01333 }
01334 
01335 /**
01336   * @brief  Disables dynamically NOR write operation.
01337   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
01338   *                the configuration information for NOR module.
01339   * @retval HAL status
01340   */
01341 HAL_StatusTypeDef HAL_NOR_WriteOperation_Disable(NOR_HandleTypeDef *hnor)
01342 {
01343   /* Check the NOR controller state */
01344   if (hnor->State == HAL_NOR_STATE_READY)
01345   {
01346     /* Process Locked */
01347     __HAL_LOCK(hnor);
01348 
01349     /* Update the NOR controller state */
01350     hnor->State = HAL_NOR_STATE_BUSY;
01351 
01352     /* Disable write operation */
01353     (void)FMC_NORSRAM_WriteOperation_Disable(hnor->Instance, hnor->Init.NSBank);
01354 
01355     /* Update the NOR controller state */
01356     hnor->State = HAL_NOR_STATE_PROTECTED;
01357 
01358     /* Process unlocked */
01359     __HAL_UNLOCK(hnor);
01360   }
01361   else
01362   {
01363     return HAL_ERROR;
01364   }
01365 
01366   return HAL_OK;
01367 }
01368 
01369 /**
01370   * @}
01371   */
01372 
01373 /** @defgroup NOR_Exported_Functions_Group4 NOR State functions
01374   *  @brief   Peripheral State functions
01375   *
01376 @verbatim
01377   ==============================================================================
01378                       ##### NOR State functions #####
01379   ==============================================================================
01380   [..]
01381     This subsection permits to get in run-time the status of the NOR controller
01382     and the data flow.
01383 
01384 @endverbatim
01385   * @{
01386   */
01387 
01388 /**
01389   * @brief  return the NOR controller state
01390   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
01391   *                the configuration information for NOR module.
01392   * @retval NOR controller state
01393   */
01394 HAL_NOR_StateTypeDef HAL_NOR_GetState(NOR_HandleTypeDef *hnor)
01395 {
01396   return hnor->State;
01397 }
01398 
01399 /**
01400   * @brief  Returns the NOR operation status.
01401   * @param  hnor pointer to a NOR_HandleTypeDef structure that contains
01402   *                the configuration information for NOR module.
01403   * @param  Address Device address
01404   * @param  Timeout NOR programming Timeout
01405   * @retval NOR_Status The returned value can be: HAL_NOR_STATUS_SUCCESS, HAL_NOR_STATUS_ERROR
01406   *         or HAL_NOR_STATUS_TIMEOUT
01407   */
01408 HAL_NOR_StatusTypeDef HAL_NOR_GetStatus(NOR_HandleTypeDef *hnor, uint32_t Address, uint32_t Timeout)
01409 {
01410   HAL_NOR_StatusTypeDef status = HAL_NOR_STATUS_ONGOING;
01411   uint16_t tmpsr1;
01412   uint16_t tmpsr2;
01413   uint32_t tickstart;
01414 
01415   /* Poll on NOR memory Ready/Busy signal ------------------------------------*/
01416   HAL_NOR_MspWait(hnor, Timeout);
01417 
01418   /* Get the NOR memory operation status -------------------------------------*/
01419 
01420   /* Get tick */
01421   tickstart = HAL_GetTick();
01422 
01423   if (hnor->CommandSet == NOR_AMD_FUJITSU_COMMAND_SET)
01424   {
01425     while ((status != HAL_NOR_STATUS_SUCCESS) && (status != HAL_NOR_STATUS_TIMEOUT))
01426     {
01427       /* Check for the Timeout */
01428       if (Timeout != HAL_MAX_DELAY)
01429       {
01430         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
01431         {
01432           status = HAL_NOR_STATUS_TIMEOUT;
01433         }
01434       }
01435 
01436       /* Read NOR status register (DQ6 and DQ5) */
01437       tmpsr1 = *(__IO uint16_t *)Address;
01438       tmpsr2 = *(__IO uint16_t *)Address;
01439 
01440       /* If DQ6 did not toggle between the two reads then return HAL_NOR_STATUS_SUCCESS  */
01441       if ((tmpsr1 & NOR_MASK_STATUS_DQ6) == (tmpsr2 & NOR_MASK_STATUS_DQ6))
01442       {
01443         return HAL_NOR_STATUS_SUCCESS ;
01444       }
01445 
01446       if ((tmpsr1 & NOR_MASK_STATUS_DQ5) == NOR_MASK_STATUS_DQ5)
01447       {
01448         status = HAL_NOR_STATUS_ONGOING;
01449       }
01450 
01451       tmpsr1 = *(__IO uint16_t *)Address;
01452       tmpsr2 = *(__IO uint16_t *)Address;
01453 
01454       /* If DQ6 did not toggle between the two reads then return HAL_NOR_STATUS_SUCCESS  */
01455       if ((tmpsr1 & NOR_MASK_STATUS_DQ6) == (tmpsr2 & NOR_MASK_STATUS_DQ6))
01456       {
01457         return HAL_NOR_STATUS_SUCCESS;
01458       }
01459       if ((tmpsr1 & NOR_MASK_STATUS_DQ5) == NOR_MASK_STATUS_DQ5)
01460       {
01461         return HAL_NOR_STATUS_ERROR;
01462       }
01463     }
01464   }
01465   else if (hnor->CommandSet == NOR_INTEL_SHARP_EXT_COMMAND_SET)
01466   {
01467     do
01468     {
01469       NOR_WRITE(Address, NOR_CMD_READ_STATUS_REG);
01470       tmpsr2 = *(__IO uint16_t *)(Address);
01471 
01472       /* Check for the Timeout */
01473       if (Timeout != HAL_MAX_DELAY)
01474       {
01475         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
01476         {
01477           return HAL_NOR_STATUS_TIMEOUT;
01478         }
01479       }
01480     } while ((tmpsr2 & NOR_MASK_STATUS_DQ7) == 0U);
01481 
01482     NOR_WRITE(Address, NOR_CMD_READ_STATUS_REG);
01483     tmpsr1 = *(__IO uint16_t *)(Address);
01484     if ((tmpsr1  & (NOR_MASK_STATUS_DQ5 | NOR_MASK_STATUS_DQ4)) != 0U)
01485     {
01486       /* Clear the Status Register  */
01487       NOR_WRITE(Address, NOR_CMD_READ_STATUS_REG);
01488       status = HAL_NOR_STATUS_ERROR;
01489     }
01490     else
01491     {
01492       status = HAL_NOR_STATUS_SUCCESS;
01493     }
01494   }
01495   else
01496   {
01497     /* Primary command set not supported by the driver */
01498     status = HAL_NOR_STATUS_ERROR;
01499   }
01500 
01501   /* Return the operation status */
01502   return status;
01503 }
01504 
01505 /**
01506   * @}
01507   */
01508 
01509 /**
01510   * @}
01511   */
01512 
01513 /**
01514   * @}
01515   */
01516 
01517 #endif /* HAL_NOR_MODULE_ENABLED */
01518 
01519 /**
01520   * @}
01521   */
01522 
01523 #endif /* FMC_BANK1 */