STM32F10x_StdPeriph_Examples/FLASH/Program/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    FLASH/Program/main.c 
00004   * @author  MCD Application Team
00005   * @version V3.4.0
00006   * @date    10/15/2010
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @copy
00010   *
00011   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017   *
00018   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00019   */ 
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32f10x.h"
00023 
00024 
00025 /** @addtogroup STM32F10x_StdPeriph_Examples
00026   * @{
00027   */
00028 
00029 /** @addtogroup FLASH_Program
00030   * @{
00031   */ 
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00035 
00036 /* Private define ------------------------------------------------------------*/
00037 /* Define the STM32F10x FLASH Page Size depending on the used STM32 device */
00038 #if defined (STM32F10X_HD) || defined (STM32F10X_HD_VL) || defined (STM32F10X_CL) || defined (STM32F10X_XL)
00039   #define FLASH_PAGE_SIZE    ((uint16_t)0x800)
00040 #else
00041   #define FLASH_PAGE_SIZE    ((uint16_t)0x400)
00042 #endif
00043 
00044 #define BANK1_WRITE_START_ADDR  ((uint32_t)0x08008000)
00045 #define BANK1_WRITE_END_ADDR    ((uint32_t)0x0800C000)
00046 
00047 #ifdef STM32F10X_XL
00048  #define BANK2_WRITE_START_ADDR   ((uint32_t)0x08088000)
00049  #define BANK2_WRITE_END_ADDR     ((uint32_t)0x0808C000)
00050 #endif /* STM32F10X_XL */
00051 
00052 /* Private macro -------------------------------------------------------------*/
00053 /* Private variables ---------------------------------------------------------*/  
00054 uint32_t EraseCounter = 0x00, Address = 0x00;
00055 uint32_t Data = 0x3210ABCD;
00056 uint32_t NbrOfPage = 0x00;
00057 volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;
00058 volatile TestStatus MemoryProgramStatus = PASSED;
00059 
00060 #ifdef STM32F10X_XL
00061 volatile TestStatus MemoryProgramStatus2 = PASSED;
00062 #endif /* STM32F10X_XL */
00063 
00064 /* Private function prototypes -----------------------------------------------*/   
00065 /* Private functions ---------------------------------------------------------*/
00066 
00067 /**
00068   * @brief   Main program
00069   * @param  None
00070   * @retval None
00071   */
00072 int main(void)
00073 {
00074   /*!< At this stage the microcontroller clock setting is already configured, 
00075        this is done through SystemInit() function which is called from startup
00076        file (startup_stm32f10x_xx.s) before to branch to application main.
00077        To reconfigure the default setting of SystemInit() function, refer to
00078        system_stm32f10x.c file
00079      */     
00080 
00081 /* Porgram FLASH Bank1 ********************************************************/       
00082   /* Unlock the Flash Bank1 Program Erase controller */
00083   FLASH_UnlockBank1();
00084 
00085   /* Define the number of page to be erased */
00086   NbrOfPage = (BANK1_WRITE_END_ADDR - BANK1_WRITE_START_ADDR) / FLASH_PAGE_SIZE;
00087 
00088   /* Clear All pending flags */
00089   FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);     
00090 
00091   /* Erase the FLASH pages */
00092   for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
00093   {
00094     FLASHStatus = FLASH_ErasePage(BANK1_WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter));
00095   }
00096   
00097   /* Program Flash Bank1 */
00098   Address = BANK1_WRITE_START_ADDR;
00099 
00100   while((Address < BANK1_WRITE_END_ADDR) && (FLASHStatus == FLASH_COMPLETE))
00101   {
00102     FLASHStatus = FLASH_ProgramWord(Address, Data);
00103     Address = Address + 4;
00104   }
00105 
00106   FLASH_LockBank1();
00107   
00108   /* Check the corectness of written data */
00109   Address = BANK1_WRITE_START_ADDR;
00110 
00111   while((Address < BANK1_WRITE_END_ADDR) && (MemoryProgramStatus != FAILED))
00112   {
00113     if((*(__IO uint32_t*) Address) != Data)
00114     {
00115       MemoryProgramStatus = FAILED;
00116     }
00117     Address += 4;
00118   }
00119 
00120 #ifdef STM32F10X_XL
00121 /* Porgram FLASH Bank2 ********************************************************/  
00122   /* Unlock the Flash Bank2 Program Erase controller */
00123   FLASH_UnlockBank2();
00124 
00125   /* Define the number of page to be erased */
00126   NbrOfPage = (BANK2_WRITE_END_ADDR - BANK2_WRITE_START_ADDR) / FLASH_PAGE_SIZE;
00127 
00128   /* Clear All pending flags */
00129   FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);     
00130 
00131   /* Erase the FLASH pages */
00132   for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
00133   {
00134     FLASHStatus = FLASH_ErasePage(BANK2_WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter));
00135   }
00136   
00137   /* Program Flash Bank2 */
00138   Address = BANK2_WRITE_START_ADDR;
00139 
00140   while((Address < BANK2_WRITE_END_ADDR) && (FLASHStatus == FLASH_COMPLETE))
00141   {
00142     FLASHStatus = FLASH_ProgramWord(Address, Data);
00143     Address = Address + 4;
00144   }
00145 
00146   FLASH_LockBank2();
00147     
00148   /* Check the corectness of written data */
00149   Address = BANK2_WRITE_START_ADDR;
00150 
00151   while((Address < BANK2_WRITE_END_ADDR) && (MemoryProgramStatus2 != FAILED))
00152   {
00153     if((*(__IO uint32_t*) Address) != Data)
00154     {
00155       MemoryProgramStatus2 = FAILED;
00156     }
00157     Address += 4;
00158   }
00159 #endif /* STM32F10X_XL */ 
00160   
00161   while (1)
00162   {
00163   }
00164 }
00165 
00166 #ifdef  USE_FULL_ASSERT
00167 
00168 /**
00169   * @brief  Reports the name of the source file and the source line number
00170   *         where the assert_param error has occurred.
00171   * @param  file: pointer to the source file name
00172   * @param  line: assert_param error line source number
00173   * @retval None
00174   */
00175 void assert_failed(uint8_t* file, uint32_t line)
00176 { 
00177   /* User can add his own implementation to report the file name and line number,
00178      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00179 
00180   while (1)
00181   {
00182   }
00183 }
00184 #endif
00185 
00186 /**
00187   * @}
00188   */ 
00189 
00190 /**
00191   * @}
00192   */ 
00193 
00194 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32