STM324x9I_EVAL BSP User Manual: stm324x9i_eval_nor.c Source File

STM32429I/STM32439I/STM32469I/STM32479I EVAL BSP Drivers

stm324x9i_eval_nor.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm324x9i_eval_nor.c
00004   * @author  MCD Application Team
00005   * @version V2.2.2
00006   * @date    13-January-2016
00007   * @brief   This file includes a standard driver for the M29W256GL70ZA6E NOR flash memory 
00008   *          device mounted on STM324x9I-EVAL evaluation board.
00009   ******************************************************************************
00010   * @attention
00011   *
00012   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00013   *
00014   * Redistribution and use in source and binary forms, with or without modification,
00015   * are permitted provided that the following conditions are met:
00016   *   1. Redistributions of source code must retain the above copyright notice,
00017   *      this list of conditions and the following disclaimer.
00018   *   2. Redistributions in binary form must reproduce the above copyright notice,
00019   *      this list of conditions and the following disclaimer in the documentation
00020   *      and/or other materials provided with the distribution.
00021   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022   *      may be used to endorse or promote products derived from this software
00023   *      without specific prior written permission.
00024   *
00025   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035   *
00036   ******************************************************************************
00037   */ 
00038 
00039 /* File Info : -----------------------------------------------------------------
00040                                    User NOTES
00041 1. How To use this driver:
00042 --------------------------
00043    - This driver is used to drive the M29W128GL NOR flash external memory mounted
00044      on STM324x9I-EVAL evaluation board.
00045    - This driver does not need a specific component driver for the NOR device
00046      to be included with.
00047 
00048 2. Driver description:
00049 ---------------------
00050   + Initialization steps:
00051      o Initialize the NOR external memory using the BSP_NOR_Init() function. This 
00052        function includes the MSP layer hardware resources initialization and the
00053        FMC controller configuration to interface with the external NOR memory.
00054   
00055   + NOR flash operations
00056      o NOR external memory can be accessed with read/write operations once it is
00057        initialized.
00058        Read/write operation can be performed with AHB access using the functions
00059        BSP_NOR_ReadData()/BSP_NOR_WriteData(). The BSP_NOR_WriteData() performs write operation
00060        of an amount of data by unit (halfword). You can also perform a program data
00061        operation of an amount of data using the function BSP_NOR_ProgramData().
00062      o The function BSP_NOR_Read_ID() returns the chip IDs stored in the structure 
00063        "NOR_IDTypeDef". (see the NOR IDs in the memory data sheet)
00064      o Perform erase block operation using the function BSP_NOR_Erase_Block() and by
00065        specifying the block address. You can perform an erase operation of the whole 
00066        chip by calling the function BSP_NOR_Erase_Chip(). 
00067      o After other operations, the function BSP_NOR_ReturnToReadMode() allows the NOR 
00068        flash to return to read mode to perform read operations on it. 
00069  
00070 ------------------------------------------------------------------------------*/
00071 
00072 /* Includes ------------------------------------------------------------------*/
00073 #include "stm324x9i_eval_nor.h"
00074 
00075 /** @addtogroup BSP
00076   * @{
00077   */
00078 
00079 /** @addtogroup STM324x9I_EVAL
00080   * @{
00081   */ 
00082   
00083 /** @defgroup STM324x9I_EVAL_NOR STM324x9I EVAL NOR
00084   * @{
00085   */ 
00086   
00087 /* Private typedef -----------------------------------------------------------*/
00088 
00089 /** @defgroup STM324x9I_EVAL_NOR_Private_Types_Definitions STM324x9I EVAL NOR Private Types Definitions
00090   * @{
00091   */ 
00092   
00093 /**
00094   * @}
00095   */  
00096   
00097 /* Private define ------------------------------------------------------------*/
00098 
00099 /** @defgroup STM324x9I_EVAL_NOR_Private_Defines STM324x9I EVAL NOR Private Defines
00100   * @{
00101   */
00102 
00103 /**
00104   * @}
00105   */ 
00106   
00107 /* Private macro -------------------------------------------------------------*/
00108 
00109 /** @defgroup STM324x9I_EVAL_NOR_Private_Macros STM324x9I EVAL NOR Private Macros
00110   * @{
00111   */  
00112   
00113 /**
00114   * @}
00115   */ 
00116   
00117 /* Private variables ---------------------------------------------------------*/
00118 
00119 /** @defgroup STM324x9I_EVAL_NOR_Private_Variables STM324x9I EVAL NOR Private Variables
00120   * @{
00121   */       
00122 static NOR_HandleTypeDef norHandle;
00123 static FMC_NORSRAM_TimingTypeDef Timing;
00124 
00125 /**
00126   * @}
00127   */ 
00128 
00129 /* Private function prototypes -----------------------------------------------*/
00130 
00131 /** @defgroup STM324x9I_EVAL_NOR_Private_Function_Prototypes STM324x9I EVAL NOR Private Function Prototypes
00132   * @{
00133   */ 
00134   
00135 /**
00136   * @}
00137   */
00138  
00139 /* Private functions ---------------------------------------------------------*/
00140     
00141 /** @defgroup STM324x9I_EVAL_NOR_Private_Functions STM324x9I EVAL NOR Private Functions
00142   * @{
00143   */ 
00144 static void NOR_MspInit(void);
00145 
00146 /**
00147   * @}
00148   */
00149     
00150 /**
00151   * @brief  Initializes the NOR device.
00152   * @retval NOR memory status
00153   */
00154 uint8_t BSP_NOR_Init(void)
00155 { 
00156   norHandle.Instance  = FMC_NORSRAM_DEVICE;
00157   norHandle.Extended  = FMC_NORSRAM_EXTENDED_DEVICE;
00158   
00159   /* NOR device configuration */  
00160   Timing.AddressSetupTime      = 4;
00161   Timing.AddressHoldTime       = 3;
00162   Timing.DataSetupTime         = 7;
00163   Timing.BusTurnAroundDuration = 1;
00164   Timing.CLKDivision           = 2;
00165   Timing.DataLatency           = 2;
00166   Timing.AccessMode            = FMC_ACCESS_MODE_A;
00167   
00168   norHandle.Init.NSBank             = FMC_NORSRAM_BANK1;
00169   norHandle.Init.DataAddressMux     = FMC_DATA_ADDRESS_MUX_DISABLE;
00170   norHandle.Init.MemoryType         = FMC_MEMORY_TYPE_NOR;
00171   norHandle.Init.MemoryDataWidth    = NOR_MEMORY_WIDTH;
00172   norHandle.Init.BurstAccessMode    = NOR_BURSTACCESS;
00173   norHandle.Init.WaitSignalPolarity = FMC_WAIT_SIGNAL_POLARITY_LOW;
00174   norHandle.Init.WrapMode           = FMC_WRAP_MODE_DISABLE;
00175   norHandle.Init.WaitSignalActive   = FMC_WAIT_TIMING_BEFORE_WS;
00176   norHandle.Init.WriteOperation     = FMC_WRITE_OPERATION_ENABLE;
00177   norHandle.Init.WaitSignal         = FMC_WAIT_SIGNAL_ENABLE;
00178   norHandle.Init.ExtendedMode       = FMC_EXTENDED_MODE_DISABLE;
00179   norHandle.Init.AsynchronousWait   = FMC_ASYNCHRONOUS_WAIT_ENABLE;
00180   norHandle.Init.WriteBurst         = NOR_WRITEBURST;
00181   norHandle.Init.ContinuousClock    = CONTINUOUSCLOCK_FEATURE;
00182     
00183   /* NOR controller initialization */
00184   NOR_MspInit();
00185   
00186   if(HAL_NOR_Init(&norHandle, &Timing, &Timing) != HAL_OK)
00187   {
00188     return NOR_STATUS_ERROR;
00189   }
00190   else
00191   {
00192     return NOR_STATUS_OK;
00193   }
00194 }
00195 
00196 /**
00197   * @brief  Reads an amount of data from the NOR device.
00198   * @param  uwStartAddress: Read start address
00199   * @param  pData: Pointer to data to be read
00200   * @param  uwDataSize: Size of data to read    
00201   * @retval NOR memory status
00202   */
00203 uint8_t BSP_NOR_ReadData(uint32_t uwStartAddress, uint16_t* pData, uint32_t uwDataSize)
00204 {
00205   if(HAL_NOR_ReadBuffer(&norHandle, NOR_DEVICE_ADDR + uwStartAddress, pData, uwDataSize) != HAL_OK)
00206   {
00207     return NOR_STATUS_ERROR;
00208   }
00209   else
00210   {
00211     return NOR_STATUS_OK;
00212   }
00213 }
00214 
00215 /**
00216   * @brief  Returns the NOR memory to read mode.
00217   */
00218 void BSP_NOR_ReturnToReadMode(void)
00219 {
00220    HAL_NOR_ReturnToReadMode(&norHandle);
00221 }
00222 
00223 /**
00224   * @brief  Writes an amount of data to the NOR device.
00225   * @param  uwStartAddress: Write start address
00226   * @param  pData: Pointer to data to be written
00227   * @param  uwDataSize: Size of data to write    
00228   * @retval NOR memory status
00229   */
00230 uint8_t BSP_NOR_WriteData(uint32_t uwStartAddress, uint16_t* pData, uint32_t uwDataSize)
00231 {
00232   uint32_t index = uwDataSize;
00233   
00234   while(index > 0)
00235   {
00236     /* Write data to NOR */
00237     HAL_NOR_Program(&norHandle, (uint32_t *)(NOR_DEVICE_ADDR + uwStartAddress), pData);
00238     
00239     /* Read NOR device status */
00240     if(HAL_NOR_GetStatus(&norHandle, NOR_DEVICE_ADDR, PROGRAM_TIMEOUT) != NOR_SUCCESS)
00241     {
00242       return NOR_STATUS_ERROR;
00243     }
00244     
00245     /* Update the counters */
00246     index--;
00247     uwStartAddress += 2;
00248     pData++; 
00249   }
00250   
00251   return NOR_STATUS_OK;
00252 }
00253 
00254 /**
00255   * @brief  Programs an amount of data to the NOR device.
00256   * @param  uwStartAddress: Write start address
00257   * @param  pData: Pointer to data to be written
00258   * @param  uwDataSize: Size of data to write    
00259   * @retval NOR memory status
00260   */
00261 uint8_t BSP_NOR_ProgramData(uint32_t uwStartAddress, uint16_t* pData, uint32_t uwDataSize)
00262 {
00263   /* Send NOR program buffer operation */
00264   HAL_NOR_ProgramBuffer(&norHandle, uwStartAddress, pData, uwDataSize);
00265   
00266   /* Return the NOR memory status */
00267   if(HAL_NOR_GetStatus(&norHandle, NOR_DEVICE_ADDR, PROGRAM_TIMEOUT) != NOR_SUCCESS)
00268   {
00269     return NOR_STATUS_ERROR;
00270   }
00271   else
00272   {
00273     return NOR_STATUS_OK;
00274   }
00275 }
00276 
00277 /**
00278   * @brief  Erases the specified block of the NOR device. 
00279   * @param  BlockAddress: Block address to erase  
00280   * @retval NOR memory status
00281   */
00282 uint8_t BSP_NOR_Erase_Block(uint32_t BlockAddress)
00283 {
00284   /* Send NOR erase block operation */
00285   HAL_NOR_Erase_Block(&norHandle, BlockAddress, NOR_DEVICE_ADDR);
00286   
00287   /* Return the NOR memory status */  
00288   if(HAL_NOR_GetStatus(&norHandle, NOR_DEVICE_ADDR, BLOCKERASE_TIMEOUT) != NOR_SUCCESS)
00289   {
00290     return NOR_STATUS_ERROR;
00291   }
00292   else
00293   {
00294     return NOR_STATUS_OK;
00295   }
00296 }
00297 
00298 /**
00299   * @brief  Erases the entire NOR chip.
00300   * @retval NOR memory status
00301   */
00302 uint8_t BSP_NOR_Erase_Chip(void)
00303 {
00304   /* Send NOR Erase chip operation */
00305   HAL_NOR_Erase_Chip(&norHandle, NOR_DEVICE_ADDR);
00306   
00307   /* Return the NOR memory status */
00308   if(HAL_NOR_GetStatus(&norHandle, NOR_DEVICE_ADDR, CHIPERASE_TIMEOUT) != NOR_SUCCESS)
00309   {
00310     return NOR_STATUS_ERROR;
00311   }
00312   else
00313   {
00314     return NOR_STATUS_OK;
00315   } 
00316 }
00317 
00318 /**
00319   * @brief  Reads NOR flash IDs.
00320   * @param  pNOR_ID : Pointer to NOR ID structure
00321   * @retval NOR memory status
00322   */
00323 uint8_t BSP_NOR_Read_ID(NOR_IDTypeDef *pNOR_ID)
00324 {
00325   if(HAL_NOR_Read_ID(&norHandle, pNOR_ID) != HAL_OK)
00326   {
00327     return NOR_STATUS_ERROR;
00328   }
00329   else
00330   {
00331     return NOR_STATUS_OK;
00332   }
00333 }
00334 
00335 /**
00336   * @brief  Initializes the NOR MSP.
00337   */
00338 static void NOR_MspInit(void)
00339 {
00340   GPIO_InitTypeDef GPIO_Init_Structure;
00341   
00342   /* Enable FMC clock */
00343   __FMC_CLK_ENABLE();
00344 
00345   /* Enable GPIOs clock */
00346   __GPIOD_CLK_ENABLE();
00347   __GPIOE_CLK_ENABLE();
00348   __GPIOF_CLK_ENABLE();
00349   __GPIOG_CLK_ENABLE();
00350   
00351   /* Common GPIO configuration */
00352   GPIO_Init_Structure.Mode      = GPIO_MODE_AF_PP;
00353   GPIO_Init_Structure.Pull      = GPIO_PULLUP;
00354   GPIO_Init_Structure.Speed     = GPIO_SPEED_HIGH;
00355   GPIO_Init_Structure.Alternate = GPIO_AF12_FMC;
00356   
00357   /* GPIOD configuration */
00358   GPIO_Init_Structure.Pin   = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6   |\
00359                               GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 |\
00360                               GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
00361   HAL_GPIO_Init(GPIOD, &GPIO_Init_Structure);
00362 
00363   /* GPIOE configuration */  
00364   GPIO_Init_Structure.Pin   = GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6   |\
00365                               GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 |\
00366                               GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
00367   HAL_GPIO_Init(GPIOE, &GPIO_Init_Structure);
00368   
00369   /* GPIOF configuration */  
00370   GPIO_Init_Structure.Pin   = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2| GPIO_PIN_3 | GPIO_PIN_4     |\
00371                               GPIO_PIN_5 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
00372   HAL_GPIO_Init(GPIOF, &GPIO_Init_Structure);
00373   
00374   /* GPIOG configuration */  
00375   GPIO_Init_Structure.Pin   = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2| GPIO_PIN_3 | GPIO_PIN_4     |\
00376                               GPIO_PIN_5;
00377   HAL_GPIO_Init(GPIOG, &GPIO_Init_Structure); 
00378 }
00379 
00380 /**
00381   * @brief  NOR BSP Wait for Ready/Busy signal.
00382   * @param  hnor: Pointer to NOR handle
00383   * @param  Timeout: Timeout duration  
00384   */
00385 void HAL_NOR_MspWait(NOR_HandleTypeDef *hnor, uint32_t Timeout)
00386 {
00387   uint32_t timeout = Timeout;
00388   
00389   /* Polling on Ready/Busy signal */
00390   while((HAL_GPIO_ReadPin(NOR_READY_BUSY_GPIO, NOR_READY_BUSY_PIN) != NOR_BUSY_STATE) && (timeout > 0)) 
00391   {
00392     timeout--;
00393   }
00394   
00395   timeout = Timeout;
00396   
00397   /* Polling on Ready/Busy signal */
00398   while((HAL_GPIO_ReadPin(NOR_READY_BUSY_GPIO, NOR_READY_BUSY_PIN) != NOR_READY_STATE) && (timeout > 0)) 
00399   {
00400     timeout--;
00401   }  
00402 } 
00403   
00404 /**
00405   * @}
00406   */ 
00407   
00408 /**
00409   * @}
00410   */
00411   
00412 /**
00413   * @}
00414   */
00415 
00416 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Wed Jan 13 2016 15:52:54 for STM324x9I_EVAL BSP User Manual by   doxygen 1.7.6.1