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