STM32H743I_EVAL BSP User Manual: stm32h743i_eval_nor.c Source File

STM32H743I_EVAL BSP

stm32h743i_eval_nor.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32h743i_eval_nor.c
00004   * @author  MCD Application Team
00005   * @version V1.0.0
00006   * @date    21-April-2017
00007   * @brief   This file includes a standard driver for the PC28F128M29EWLA NOR flash memory
00008   *          device mounted on STM32H743I-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 STM32H743I-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) 2017 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 "stm32h743i_eval_nor.h"
00071 
00072 /** @addtogroup BSP
00073   * @{
00074   */
00075 
00076 /** @addtogroup STM32H743I_EVAL
00077   * @{
00078   */
00079 
00080 /** @addtogroup STM32H743I_EVAL_NOR
00081   * @{
00082   */
00083 
00084 /* Private typedef -----------------------------------------------------------*/
00085 
00086 /** @defgroup STM32H743I_EVAL_NOR_Private_Types_Definitions NOR Private Types Definitions
00087   * @{
00088   */
00089 
00090 /**
00091   * @}
00092   */  
00093 /* Private define ------------------------------------------------------------*/
00094 
00095 /** @defgroup STM32H743I_EVAL_NOR_Private_Defines NOR Private Defines
00096   * @{
00097   */
00098 
00099 /**
00100   * @}
00101   */
00102   
00103 /* Private macro -------------------------------------------------------------*/
00104 
00105 /** @defgroup STM32H743I_EVAL_NOR_Private_Macros NOR Private Macros
00106   * @{
00107   */
00108 
00109 /**
00110   * @}
00111   */
00112 
00113 /* Private variables ---------------------------------------------------------*/
00114 
00115 /** @defgroup STM32H743I_EVAL_NOR_Private_Variables NOR Private Variables
00116   * @{
00117   */
00118 NOR_HandleTypeDef norHandle;
00119 static FMC_NORSRAM_TimingTypeDef Timing;
00120 
00121 /**
00122   * @}
00123   */
00124 
00125 /* Private function prototypes -----------------------------------------------*/
00126 
00127 /** @defgroup STM32H743I_EVAL_NOR_Private_Function_Prototypes NOR Private Function Prototypes
00128   * @{
00129   */
00130 
00131  /**
00132   * @}
00133   */
00134   
00135 /* Exported functions ---------------------------------------------------------*/
00136 
00137 /** @addtogroup STM32H743I_EVAL_NOR_Exported_Functions NOR Exported Functions
00138   * @{
00139   */
00140 
00141 /**
00142   * @brief  Initializes the NOR device.
00143   * @retval NOR memory status
00144   */
00145 uint8_t BSP_NOR_Init(void)
00146 {
00147   static uint8_t nor_status = NOR_STATUS_ERROR;
00148   norHandle.Instance  = FMC_NORSRAM_DEVICE;
00149   norHandle.Extended  = FMC_NORSRAM_EXTENDED_DEVICE;
00150 
00151   /* NOR device configuration */
00152   /* Timing configuration derived from system clock (up to 400Mhz)
00153      for 100Mhz as NOR clock frequency */
00154   Timing.AddressSetupTime      = 4;
00155   Timing.AddressHoldTime       = 3;
00156   Timing.DataSetupTime         = 8;
00157   Timing.BusTurnAroundDuration = 1;
00158   Timing.CLKDivision           = 2;
00159   Timing.DataLatency           = 2;
00160   Timing.AccessMode            = FMC_ACCESS_MODE_A;
00161 
00162   norHandle.Init.NSBank             = FMC_NORSRAM_BANK1;
00163   norHandle.Init.DataAddressMux     = FMC_DATA_ADDRESS_MUX_DISABLE;
00164   norHandle.Init.MemoryType         = FMC_MEMORY_TYPE_NOR;
00165   norHandle.Init.MemoryDataWidth    = NOR_MEMORY_WIDTH;
00166   norHandle.Init.BurstAccessMode    = NOR_BURSTACCESS;
00167   norHandle.Init.WaitSignalPolarity = FMC_WAIT_SIGNAL_POLARITY_LOW;
00168   norHandle.Init.WaitSignalActive   = FMC_WAIT_TIMING_BEFORE_WS;
00169   norHandle.Init.WriteOperation     = FMC_WRITE_OPERATION_ENABLE;
00170   norHandle.Init.WaitSignal         = FMC_WAIT_SIGNAL_ENABLE;
00171   norHandle.Init.ExtendedMode       = FMC_EXTENDED_MODE_DISABLE;
00172   norHandle.Init.AsynchronousWait   = FMC_ASYNCHRONOUS_WAIT_ENABLE;
00173   norHandle.Init.WriteBurst         = NOR_WRITEBURST;
00174   norHandle.Init.ContinuousClock    = CONTINUOUSCLOCK_FEATURE;
00175 
00176   /* NOR controller initialization */
00177   BSP_NOR_MspInit(&norHandle, NULL); /* __weak function can be rewritten by the application */
00178 
00179   if(HAL_NOR_Init(&norHandle, &Timing, &Timing) != HAL_OK)
00180   {
00181     nor_status = NOR_STATUS_ERROR;
00182   }
00183   else
00184   {
00185     nor_status = NOR_STATUS_OK;
00186   }
00187   return nor_status;
00188 }
00189 
00190 /**
00191   * @brief  DeInitializes the NOR device.
00192   * @retval NOR status
00193   */
00194 uint8_t BSP_NOR_DeInit(void)
00195 {
00196   static uint8_t nor_status = NOR_STATUS_ERROR;
00197   /* NOR device de-initialization */
00198   norHandle.Instance = FMC_NORSRAM_DEVICE;
00199   norHandle.Extended = FMC_NORSRAM_EXTENDED_DEVICE;
00200 
00201   if(HAL_NOR_DeInit(&norHandle) != HAL_OK)
00202   {
00203     nor_status = NOR_STATUS_ERROR;
00204   }
00205   else
00206   {
00207     nor_status = NOR_STATUS_OK;
00208   }
00209 
00210   /* NOR controller de-initialization */
00211   BSP_NOR_MspDeInit(&norHandle, NULL);
00212 
00213   return nor_status;
00214 }
00215 
00216 /**
00217   * @brief  Reads an amount of data from the NOR device.
00218   * @param  uwStartAddress: Read start address
00219   * @param  pData: Pointer to data to be read
00220   * @param  uwDataSize: Size of data to read
00221   * @retval NOR memory status
00222   */
00223 uint8_t BSP_NOR_ReadData(uint32_t uwStartAddress, uint16_t* pData, uint32_t uwDataSize)
00224 {
00225   if(HAL_NOR_ReadBuffer(&norHandle, NOR_DEVICE_ADDR + uwStartAddress, pData, uwDataSize) != HAL_OK)
00226   {
00227     return NOR_STATUS_ERROR;
00228   }
00229   else
00230   {
00231     return NOR_STATUS_OK;
00232   }
00233 }
00234 
00235 /**
00236   * @brief  Returns the NOR memory to read mode.
00237   * @retval None
00238   */
00239 void BSP_NOR_ReturnToReadMode(void)
00240 {
00241    HAL_NOR_ReturnToReadMode(&norHandle);
00242 }
00243 
00244 /**
00245   * @brief  Writes an amount of data to the NOR device.
00246   * @param  uwStartAddress: Write start address
00247   * @param  pData: Pointer to data to be written
00248   * @param  uwDataSize: Size of data to write
00249   * @retval NOR memory status
00250   */
00251 uint8_t BSP_NOR_WriteData(uint32_t uwStartAddress, uint16_t* pData, uint32_t uwDataSize)
00252 {
00253   uint32_t index = uwDataSize;
00254 
00255   while(index > 0)
00256   {
00257     /* Write data to NOR */
00258     HAL_NOR_Program(&norHandle, (uint32_t *)(NOR_DEVICE_ADDR + uwStartAddress), pData);
00259 
00260     /* Read NOR device status */
00261     if(HAL_NOR_GetStatus(&norHandle, NOR_DEVICE_ADDR, PROGRAM_TIMEOUT) != HAL_NOR_STATUS_SUCCESS)
00262     {
00263       return NOR_STATUS_ERROR;
00264     }
00265 
00266     /* Update the counters */
00267     index--;
00268     uwStartAddress += 2;
00269     pData++;
00270   }
00271 
00272   return NOR_STATUS_OK;
00273 }
00274 
00275 /**
00276   * @brief  Programs an amount of data to the NOR device.
00277   * @param  uwStartAddress: Write start address
00278   * @param  pData: Pointer to data to be written
00279   * @param  uwDataSize: Size of data to write
00280   * @retval NOR memory status
00281   */
00282 uint8_t BSP_NOR_ProgramData(uint32_t uwStartAddress, uint16_t* pData, uint32_t uwDataSize)
00283 {
00284   /* Send NOR program buffer operation */
00285   HAL_NOR_ProgramBuffer(&norHandle, uwStartAddress, pData, uwDataSize);
00286 
00287   /* Return the NOR memory status */
00288   if(HAL_NOR_GetStatus(&norHandle, NOR_DEVICE_ADDR, PROGRAM_TIMEOUT) != HAL_NOR_STATUS_SUCCESS)
00289   {
00290     return NOR_STATUS_ERROR;
00291   }
00292   else
00293   {
00294     return NOR_STATUS_OK;
00295   }
00296 }
00297 
00298 /**
00299   * @brief  Erases the specified block of the NOR device.
00300   * @param  BlockAddress: Block address to erase
00301   * @retval NOR memory status
00302   */
00303 uint8_t BSP_NOR_Erase_Block(uint32_t BlockAddress)
00304 {
00305   /* Send NOR erase block operation */
00306   HAL_NOR_Erase_Block(&norHandle, BlockAddress, NOR_DEVICE_ADDR);
00307 
00308   /* Return the NOR memory status */
00309   if(HAL_NOR_GetStatus(&norHandle, NOR_DEVICE_ADDR, BLOCKERASE_TIMEOUT) != HAL_NOR_STATUS_SUCCESS)
00310   {
00311     return NOR_STATUS_ERROR;
00312   }
00313   else
00314   {
00315     return NOR_STATUS_OK;
00316   }
00317 }
00318 
00319 /**
00320   * @brief  Erases the entire NOR chip.
00321   * @retval NOR memory status
00322   */
00323 uint8_t BSP_NOR_Erase_Chip(void)
00324 {
00325   /* Send NOR Erase chip operation */
00326   HAL_NOR_Erase_Chip(&norHandle, NOR_DEVICE_ADDR);
00327 
00328   /* Return the NOR memory status */
00329   if(HAL_NOR_GetStatus(&norHandle, NOR_DEVICE_ADDR, CHIPERASE_TIMEOUT) != HAL_NOR_STATUS_SUCCESS)
00330   {
00331     return NOR_STATUS_ERROR;
00332   }
00333   else
00334   {
00335     return NOR_STATUS_OK;
00336   }
00337 }
00338 
00339 /**
00340   * @brief  Reads NOR flash IDs.
00341   * @param  pNOR_ID : Pointer to NOR ID structure
00342   * @retval NOR memory status
00343   */
00344 uint8_t BSP_NOR_Read_ID(NOR_IDTypeDef *pNOR_ID)
00345 {
00346   if(HAL_NOR_Read_ID(&norHandle, pNOR_ID) != HAL_OK)
00347   {
00348     return NOR_STATUS_ERROR;
00349   }
00350   else
00351   {
00352     return NOR_STATUS_OK;
00353   }
00354 }
00355 
00356 /**
00357   * @brief  Initializes the NOR MSP.
00358   * @param  norHandle: NOR handle
00359   * @param  Params: Pointer to void  
00360   * @retval None
00361   */
00362 __weak void BSP_NOR_MspInit(NOR_HandleTypeDef *norHandle, void *Params)
00363 {
00364   GPIO_InitTypeDef gpio_init_structure;
00365 
00366   /* Enable FMC clock */
00367   __HAL_RCC_FMC_CLK_ENABLE();
00368 
00369   /* Enable GPIOs clock */
00370   __HAL_RCC_GPIOC_CLK_ENABLE();  
00371   __HAL_RCC_GPIOD_CLK_ENABLE();
00372   __HAL_RCC_GPIOE_CLK_ENABLE();
00373   __HAL_RCC_GPIOF_CLK_ENABLE();
00374   __HAL_RCC_GPIOG_CLK_ENABLE();
00375 
00376   /* Common GPIO configuration */
00377   gpio_init_structure.Mode      = GPIO_MODE_AF_PP;
00378   gpio_init_structure.Pull      = GPIO_PULLUP;
00379   gpio_init_structure.Speed     = GPIO_SPEED_FREQ_VERY_HIGH;
00380 
00381   /* GPIOC configuration */
00382   gpio_init_structure.Alternate = GPIO_AF9_FMC;
00383   gpio_init_structure.Pin   = GPIO_PIN_6 | GPIO_PIN_7;
00384   HAL_GPIO_Init(GPIOC, &gpio_init_structure);
00385 
00386   /* GPIOD configuration */
00387   gpio_init_structure.Alternate = GPIO_AF12_FMC;
00388   gpio_init_structure.Pin   = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5|\
00389                               GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 |\
00390                               GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
00391   HAL_GPIO_Init(GPIOD, &gpio_init_structure);
00392 
00393   /* GPIOE configuration */
00394   gpio_init_structure.Pin   = GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6   |\
00395                               GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 |\
00396                               GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
00397   HAL_GPIO_Init(GPIOE, &gpio_init_structure);
00398 
00399   /* GPIOF configuration */
00400   gpio_init_structure.Pin   = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2| GPIO_PIN_3 | GPIO_PIN_4     |\
00401                               GPIO_PIN_5 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
00402   HAL_GPIO_Init(GPIOF, &gpio_init_structure);
00403 
00404   /* GPIOG configuration */
00405   gpio_init_structure.Pin   = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2| GPIO_PIN_3 | GPIO_PIN_4     |\
00406                               GPIO_PIN_5;
00407   HAL_GPIO_Init(GPIOG, &gpio_init_structure);
00408 }
00409 
00410 /**
00411   * @brief  DeInitializes NOR MSP.
00412   * @param  norHandle: NOR handle
00413   * @param  Params: Pointer to void  
00414   * @retval None
00415   */
00416 __weak void BSP_NOR_MspDeInit(NOR_HandleTypeDef  *norHandle, void *Params)
00417 {
00418     /* GPIO pins clock, FMC clock can be shut down in the application
00419        by surcharging this __weak function */
00420 }
00421 
00422 /**
00423   * @}
00424   */
00425 
00426 /**
00427   * @brief  NOR BSP Wait for Ready/Busy signal.
00428   * @param  norHandle: Pointer to NOR handle
00429   * @param  Timeout: Timeout duration
00430   * @retval None
00431   */
00432 void HAL_NOR_MspWait(NOR_HandleTypeDef *norHandle, uint32_t Timeout)
00433 {
00434   uint32_t timeout = Timeout;
00435 
00436   /* Polling on Ready/Busy signal */
00437   while((HAL_GPIO_ReadPin(NOR_READY_BUSY_GPIO, NOR_READY_BUSY_PIN) != NOR_BUSY_STATE) && (timeout > 0))
00438   {
00439     timeout--;
00440   }
00441 
00442   timeout = Timeout;
00443 
00444   /* Polling on Ready/Busy signal */
00445   while((HAL_GPIO_ReadPin(NOR_READY_BUSY_GPIO, NOR_READY_BUSY_PIN) != NOR_READY_STATE) && (timeout > 0))
00446   {
00447     timeout--;
00448   }
00449 }
00450 
00451 
00452 /**
00453   * @}
00454   */
00455 
00456 /**
00457   * @}
00458   */
00459 
00460 /**
00461   * @}
00462   */
00463 
00464 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Wed Aug 23 2017 17:45:13 for STM32H743I_EVAL BSP User Manual by   doxygen 1.7.6.1