STM32L476G_EVAL BSP User Manual: stm32l476g_eval_nor.c Source File

STM32L476G_EVAL BSP

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