STM3210E_EVAL BSP User Manual
|
stm3210e_eval_nor.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file stm3210e_eval_nor.c 00004 * @author MCD Application Team 00005 * @version V6.0.2 00006 * @date 29-April-2016 00007 * @brief This file includes a standard driver for the M29W128GL70ZA6E NOR flash memory 00008 * device mounted on STM3210E-EVAL evaluation board. 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2016 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 STM3210E-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 FSMC 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 "stm3210e_eval_nor.h" 00074 00075 /** @addtogroup BSP 00076 * @{ 00077 */ 00078 00079 /** @addtogroup STM3210E_EVAL 00080 * @{ 00081 */ 00082 00083 /** @defgroup STM3210E_EVAL_NOR STM3210E EVAL NOR 00084 * @{ 00085 */ 00086 00087 00088 /* Private variables ---------------------------------------------------------*/ 00089 00090 /** @defgroup STM3210E_EVAL_NOR_Private_Variables STM3210E EVAL NOR Private Variables 00091 * @{ 00092 */ 00093 static NOR_HandleTypeDef norHandle; 00094 static FSMC_NORSRAM_TimingTypeDef Timing; 00095 00096 /** 00097 * @} 00098 */ 00099 00100 /* Private function prototypes -----------------------------------------------*/ 00101 00102 /** @defgroup STM3210E_EVAL_NOR_Private_Function_Prototypes STM3210E EVAL NOR Private Function Prototypes 00103 * @{ 00104 */ 00105 00106 static void NOR_MspInit(void); 00107 00108 /** 00109 * @} 00110 */ 00111 00112 /* Private functions ---------------------------------------------------------*/ 00113 00114 /** @defgroup STM3210E_EVAL_NOR_Exported_Functions STM3210E EVAL NOR Exported Functions 00115 * @{ 00116 */ 00117 00118 /** 00119 * @brief Initializes the NOR device. 00120 * @retval NOR memory status 00121 */ 00122 uint8_t BSP_NOR_Init(void) 00123 { 00124 norHandle.Instance = FSMC_NORSRAM_DEVICE; 00125 norHandle.Extended = FSMC_NORSRAM_EXTENDED_DEVICE; 00126 00127 /* NOR device configuration */ 00128 Timing.AddressSetupTime = 2; 00129 Timing.AddressHoldTime = 1; 00130 Timing.DataSetupTime = 5; 00131 Timing.BusTurnAroundDuration = 0; 00132 Timing.CLKDivision = 2; 00133 Timing.DataLatency = 2; 00134 Timing.AccessMode = FSMC_ACCESS_MODE_B; 00135 00136 norHandle.Init.NSBank = FSMC_NORSRAM_BANK2; 00137 norHandle.Init.DataAddressMux = FSMC_DATA_ADDRESS_MUX_DISABLE; 00138 norHandle.Init.MemoryType = FSMC_MEMORY_TYPE_NOR; 00139 norHandle.Init.MemoryDataWidth = NOR_MEMORY_WIDTH; 00140 norHandle.Init.BurstAccessMode = NOR_BURSTACCESS; 00141 norHandle.Init.WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW; 00142 norHandle.Init.WrapMode = FSMC_WRAP_MODE_DISABLE; 00143 norHandle.Init.WaitSignalActive = FSMC_WAIT_TIMING_BEFORE_WS; 00144 norHandle.Init.WriteOperation = FSMC_WRITE_OPERATION_ENABLE; 00145 norHandle.Init.WaitSignal = FSMC_WAIT_SIGNAL_DISABLE; 00146 norHandle.Init.ExtendedMode = FSMC_EXTENDED_MODE_DISABLE; 00147 norHandle.Init.AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE; 00148 norHandle.Init.WriteBurst = NOR_WRITEBURST; 00149 00150 /* NOR controller initialization */ 00151 NOR_MspInit(); 00152 00153 if(HAL_NOR_Init(&norHandle, &Timing, &Timing) != HAL_OK) 00154 { 00155 return NOR_STATUS_ERROR; 00156 } 00157 else 00158 { 00159 return NOR_STATUS_OK; 00160 } 00161 } 00162 00163 /** 00164 * @brief Reads an amount of data from the NOR device. 00165 * @param uwStartAddress: Read start address 00166 * @param pData: Pointer to data to be read 00167 * @param uwDataSize: Size of data to read 00168 * @retval NOR memory status 00169 */ 00170 uint8_t BSP_NOR_ReadData(uint32_t uwStartAddress, uint16_t* pData, uint32_t uwDataSize) 00171 { 00172 if(HAL_NOR_ReadBuffer(&norHandle, NOR_DEVICE_ADDR + uwStartAddress, pData, uwDataSize) != HAL_OK) 00173 { 00174 return NOR_STATUS_ERROR; 00175 } 00176 else 00177 { 00178 return NOR_STATUS_OK; 00179 } 00180 } 00181 00182 /** 00183 * @brief Returns the NOR memory to read mode. 00184 */ 00185 void BSP_NOR_ReturnToReadMode(void) 00186 { 00187 HAL_NOR_ReturnToReadMode(&norHandle); 00188 } 00189 00190 /** 00191 * @brief Writes an amount of data to the NOR device. 00192 * @param uwStartAddress: Write start address 00193 * @param pData: Pointer to data to be written 00194 * @param uwDataSize: Size of data to write 00195 * @retval NOR memory status 00196 */ 00197 uint8_t BSP_NOR_WriteData(uint32_t uwStartAddress, uint16_t* pData, uint32_t uwDataSize) 00198 { 00199 uint32_t index = uwDataSize; 00200 00201 while(index > 0) 00202 { 00203 /* Write data to NOR */ 00204 HAL_NOR_Program(&norHandle, (uint32_t *)(NOR_DEVICE_ADDR + uwStartAddress), pData); 00205 00206 /* Read NOR device status */ 00207 if(HAL_NOR_GetStatus(&norHandle, NOR_DEVICE_ADDR, PROGRAM_TIMEOUT) != HAL_NOR_STATUS_SUCCESS) 00208 { 00209 return NOR_STATUS_ERROR; 00210 } 00211 00212 /* Update the counters */ 00213 index--; 00214 uwStartAddress += 2; 00215 pData++; 00216 } 00217 00218 return NOR_STATUS_OK; 00219 } 00220 00221 /** 00222 * @brief Programs an amount of data to the NOR device. 00223 * @param uwStartAddress: Write start address 00224 * @param pData: Pointer to data to be written 00225 * @param uwDataSize: Size of data to write 00226 * @retval NOR memory status 00227 */ 00228 uint8_t BSP_NOR_ProgramData(uint32_t uwStartAddress, uint16_t* pData, uint32_t uwDataSize) 00229 { 00230 /* Send NOR program buffer operation */ 00231 HAL_NOR_ProgramBuffer(&norHandle, uwStartAddress, pData, uwDataSize); 00232 00233 /* Return the NOR memory status */ 00234 if(HAL_NOR_GetStatus(&norHandle, NOR_DEVICE_ADDR, PROGRAM_TIMEOUT) != HAL_NOR_STATUS_SUCCESS) 00235 { 00236 return NOR_STATUS_ERROR; 00237 } 00238 else 00239 { 00240 return NOR_STATUS_OK; 00241 } 00242 } 00243 00244 /** 00245 * @brief Erases the specified block of the NOR device. 00246 * @param BlockAddress: Block address to erase 00247 * @retval NOR memory status 00248 */ 00249 uint8_t BSP_NOR_Erase_Block(uint32_t BlockAddress) 00250 { 00251 /* Send NOR erase block operation */ 00252 HAL_NOR_Erase_Block(&norHandle, BlockAddress, NOR_DEVICE_ADDR); 00253 00254 /* Return the NOR memory status */ 00255 if(HAL_NOR_GetStatus(&norHandle, NOR_DEVICE_ADDR, BLOCKERASE_TIMEOUT) != HAL_NOR_STATUS_SUCCESS) 00256 { 00257 return NOR_STATUS_ERROR; 00258 } 00259 else 00260 { 00261 return NOR_STATUS_OK; 00262 } 00263 } 00264 00265 /** 00266 * @brief Erases the entire NOR chip. 00267 * @retval NOR memory status 00268 */ 00269 uint8_t BSP_NOR_Erase_Chip(void) 00270 { 00271 /* Send NOR Erase chip operation */ 00272 HAL_NOR_Erase_Chip(&norHandle, NOR_DEVICE_ADDR); 00273 00274 /* Return the NOR memory status */ 00275 if(HAL_NOR_GetStatus(&norHandle, NOR_DEVICE_ADDR, CHIPERASE_TIMEOUT) != HAL_NOR_STATUS_SUCCESS) 00276 { 00277 return NOR_STATUS_ERROR; 00278 } 00279 else 00280 { 00281 return NOR_STATUS_OK; 00282 } 00283 } 00284 00285 /** 00286 * @brief Reads NOR flash IDs. 00287 * @param pNOR_ID : Pointer to NOR ID structure 00288 * @retval NOR memory status 00289 */ 00290 uint8_t BSP_NOR_Read_ID(NOR_IDTypeDef *pNOR_ID) 00291 { 00292 if(HAL_NOR_Read_ID(&norHandle, pNOR_ID) != HAL_OK) 00293 { 00294 return NOR_STATUS_ERROR; 00295 } 00296 else 00297 { 00298 return NOR_STATUS_OK; 00299 } 00300 } 00301 00302 /** 00303 * @brief Initializes the NOR MSP. 00304 */ 00305 static void NOR_MspInit(void) 00306 { 00307 GPIO_InitTypeDef gpioinitstruct = {0}; 00308 00309 /* Enable FSMC clock */ 00310 __HAL_RCC_FSMC_CLK_ENABLE(); 00311 00312 /* Enable GPIOs clock */ 00313 __HAL_RCC_GPIOD_CLK_ENABLE(); 00314 __HAL_RCC_GPIOE_CLK_ENABLE(); 00315 __HAL_RCC_GPIOF_CLK_ENABLE(); 00316 __HAL_RCC_GPIOG_CLK_ENABLE(); 00317 00318 /* Common GPIO configuration */ 00319 gpioinitstruct.Mode = GPIO_MODE_AF_PP; 00320 gpioinitstruct.Pull = GPIO_PULLUP; 00321 gpioinitstruct.Speed = GPIO_SPEED_FREQ_HIGH; 00322 00323 /*-- GPIO Configuration ------------------------------------------------------*/ 00324 /*!< NOR Data lines configuration */ 00325 gpioinitstruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_8 | GPIO_PIN_9 | 00326 GPIO_PIN_10 | GPIO_PIN_14 | GPIO_PIN_15; 00327 HAL_GPIO_Init(GPIOD, &gpioinitstruct); 00328 00329 gpioinitstruct.Pin = GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | 00330 GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | 00331 GPIO_PIN_14 | GPIO_PIN_15; 00332 HAL_GPIO_Init(GPIOE, &gpioinitstruct); 00333 00334 /*!< NOR Address lines configuration */ 00335 gpioinitstruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | 00336 GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_12 | GPIO_PIN_13 | 00337 GPIO_PIN_14 | GPIO_PIN_15; 00338 HAL_GPIO_Init(GPIOF, &gpioinitstruct); 00339 00340 gpioinitstruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | 00341 GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5; 00342 HAL_GPIO_Init(GPIOG, &gpioinitstruct); 00343 00344 gpioinitstruct.Pin = GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13; 00345 HAL_GPIO_Init(GPIOD, &gpioinitstruct); 00346 00347 gpioinitstruct.Pin = GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6; 00348 HAL_GPIO_Init(GPIOE, &gpioinitstruct); 00349 00350 /*!< NOE and NWE configuration */ 00351 gpioinitstruct.Pin = GPIO_PIN_4 | GPIO_PIN_5; 00352 HAL_GPIO_Init(GPIOD, &gpioinitstruct); 00353 00354 /*!< NE2 configuration */ 00355 gpioinitstruct.Pin = GPIO_PIN_9 | GPIO_PIN_12; 00356 HAL_GPIO_Init(GPIOG, &gpioinitstruct); 00357 00358 /*!< Configure PD6 for NOR memory Ready/Busy signal */ 00359 gpioinitstruct.Pin = GPIO_PIN_6; 00360 gpioinitstruct.Mode = GPIO_MODE_INPUT; 00361 HAL_GPIO_Init(GPIOD, &gpioinitstruct); 00362 00363 } 00364 00365 /** 00366 * @brief NOR BSP Wait for Ready/Busy signal. 00367 * @param hnor: Pointer to NOR handle 00368 * @param Timeout: Timeout duration 00369 */ 00370 void HAL_NOR_MspWait(NOR_HandleTypeDef *hnor, uint32_t Timeout) 00371 { 00372 uint32_t timeout = Timeout; 00373 00374 /* Polling on Ready/Busy signal */ 00375 while((HAL_GPIO_ReadPin(NOR_READY_BUSY_GPIO, NOR_READY_BUSY_PIN) != NOR_BUSY_STATE) && (timeout > 0)) 00376 { 00377 timeout--; 00378 } 00379 00380 timeout = Timeout; 00381 00382 /* Polling on Ready/Busy signal */ 00383 while((HAL_GPIO_ReadPin(NOR_READY_BUSY_GPIO, NOR_READY_BUSY_PIN) != NOR_READY_STATE) && (timeout > 0)) 00384 { 00385 timeout--; 00386 } 00387 } 00388 00389 /** 00390 * @} 00391 */ 00392 00393 /** 00394 * @} 00395 */ 00396 00397 /** 00398 * @} 00399 */ 00400 00401 /** 00402 * @} 00403 */ 00404 00405 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Fri Feb 24 2017 17:15:11 for STM3210E_EVAL BSP User Manual by
