STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/FLASH/Flash_Program/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file FLASH/FLASH_Program/main.c 00004 * @author MCD Application Team 00005 * @version V1.4.0 00006 * @date 24-July-2014 00007 * @brief Main program body 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00012 * 00013 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00014 * You may not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at: 00016 * 00017 * http://www.st.com/software_license_agreement_liberty_v2 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, 00021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 ****************************************************************************** 00026 */ 00027 00028 /* Includes ------------------------------------------------------------------*/ 00029 #include "main.h" 00030 00031 /** @addtogroup STM32F0xx_StdPeriph_Examples 00032 * @{ 00033 */ 00034 00035 /** @addtogroup FLASH_Program 00036 * @{ 00037 */ 00038 00039 /* Private typedef -----------------------------------------------------------*/ 00040 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus; 00041 /* Private define ------------------------------------------------------------*/ 00042 #ifdef STM32F072 00043 #define FLASH_PAGE_SIZE ((uint32_t)0x00000800) /* FLASH Page Size */ 00044 #define FLASH_USER_START_ADDR ((uint32_t)0x08009000) /* Start @ of user Flash area */ 00045 #define FLASH_USER_END_ADDR ((uint32_t)0x08020000) /* End @ of user Flash area */ 00046 #elif defined (STM32F091) 00047 #define FLASH_PAGE_SIZE ((uint32_t)0x00000800) /* FLASH Page Size */ 00048 #define FLASH_USER_START_ADDR ((uint32_t)0x08009000) /* Start @ of user Flash area */ 00049 #define FLASH_USER_END_ADDR ((uint32_t)0x08040000) /* End @ of user Flash area */ 00050 #else 00051 #define FLASH_PAGE_SIZE ((uint32_t)0x00000400) /* FLASH Page Size */ 00052 #define FLASH_USER_START_ADDR ((uint32_t)0x08006000) /* Start @ of user Flash area */ 00053 #define FLASH_USER_END_ADDR ((uint32_t)0x08007000) /* End @ of user Flash area */ 00054 #endif /* STM32F072 */ 00055 #define DATA_32 ((uint32_t)0x12345678) 00056 00057 /* Private macro -------------------------------------------------------------*/ 00058 /* Private variables ---------------------------------------------------------*/ 00059 uint32_t EraseCounter = 0x00, Address = 0x00; 00060 uint32_t Data = 0x3210ABCD; 00061 uint32_t NbrOfPage = 0x00; 00062 __IO FLASH_Status FLASHStatus = FLASH_COMPLETE; 00063 __IO TestStatus MemoryProgramStatus = PASSED; 00064 00065 /* Private function prototypes -----------------------------------------------*/ 00066 /* Private functions ---------------------------------------------------------*/ 00067 00068 /** 00069 * @brief Main program. 00070 * @param None 00071 * @retval None 00072 */ 00073 int main(void) 00074 { 00075 /*!< At this stage the microcontroller clock setting is already configured, 00076 this is done through SystemInit() function which is called from startup 00077 file (startup_stm32f0xx.s) before to branch to application main. 00078 To reconfigure the default setting of SystemInit() function, refer to 00079 system_stm32f0xx.c file 00080 */ 00081 00082 /* Unlock the Flash to enable the flash control register access *************/ 00083 FLASH_Unlock(); 00084 00085 /* Erase the user Flash area 00086 (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/ 00087 00088 /* Clear pending flags (if any) */ 00089 FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR); 00090 00091 /* Define the number of page to be erased */ 00092 NbrOfPage = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE; 00093 00094 /* Erase the FLASH pages */ 00095 for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++) 00096 { 00097 if (FLASH_ErasePage(FLASH_USER_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter))!= FLASH_COMPLETE) 00098 { 00099 /* Error occurred while sector erase. 00100 User can add here some code to deal with this error */ 00101 while (1) 00102 { 00103 } 00104 } 00105 } 00106 /* Program the user Flash area word by word 00107 (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/ 00108 00109 Address = FLASH_USER_START_ADDR; 00110 00111 while (Address < FLASH_USER_END_ADDR) 00112 { 00113 if (FLASH_ProgramWord(Address, DATA_32) == FLASH_COMPLETE) 00114 { 00115 Address = Address + 4; 00116 } 00117 else 00118 { 00119 /* Error occurred while writing data in Flash memory. 00120 User can add here some code to deal with this error */ 00121 while (1) 00122 { 00123 } 00124 } 00125 } 00126 00127 /* Lock the Flash to disable the flash control register access (recommended 00128 to protect the FLASH memory against possible unwanted operation) *********/ 00129 FLASH_Lock(); 00130 00131 00132 /* Check if the programmed data is OK 00133 MemoryProgramStatus = 0: data programmed correctly 00134 MemoryProgramStatus != 0: number of words not programmed correctly ******/ 00135 Address = FLASH_USER_START_ADDR; 00136 MemoryProgramStatus = PASSED; 00137 00138 while (Address < FLASH_USER_END_ADDR) 00139 { 00140 Data = *(__IO uint32_t *)Address; 00141 00142 if (Data != DATA_32) 00143 { 00144 MemoryProgramStatus = FAILED; 00145 } 00146 00147 Address = Address + 4; 00148 } 00149 00150 /* Infinite loop */ 00151 while (1) 00152 { 00153 } 00154 } 00155 00156 #ifdef USE_FULL_ASSERT 00157 00158 /** 00159 * @brief Reports the name of the source file and the source line number 00160 * where the assert_param error has occurred. 00161 * @param file: pointer to the source file name 00162 * @param line: assert_param error line source number 00163 * @retval None 00164 */ 00165 void assert_failed(uint8_t* file, uint32_t line) 00166 { 00167 /* User can add his own implementation to report the file name and line number, 00168 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00169 00170 /* Infinite loop */ 00171 while (1) 00172 { 00173 } 00174 } 00175 #endif 00176 00177 /** 00178 * @} 00179 */ 00180 00181 /** 00182 * @} 00183 */ 00184 00185 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/