STM32756G_EVAL BSP User Manual: stm32756g_eval_nor.c Source File

STM32756G EVAL BSP Drivers

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