STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/FLASH/Flash_WriteProtection/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    FLASH/FLASH_WriteProtection/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>&copy; 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_WriteProtection
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00041 
00042 /* Private define ------------------------------------------------------------*/
00043 #ifdef STM32F072
00044   #define FLASH_PAGE_SIZE                ((uint16_t)0x800)
00045   #define FLASH_PAGES_TO_BE_PROTECTED    (OB_WRP_Pages22to23 | OB_WRP_Pages24to25 | OB_WRP_Pages26to27 |\
00046                                           OB_WRP_Pages28to29 | OB_WRP_Pages30to31 | OB_WRP_Pages32to33 |\
00047                                           OB_WRP_Pages34to35 | OB_WRP_Pages60to61 | OB_WRP_Pages62to63|\
00048                                           OB_WRP_Pages54to55 | OB_WRP_Pages52to53 | OB_WRP_Pages58to59/*|OB_WRP_AllPages*/) 
00049   #define BANK1_WRITE_START_ADDR         ((uint32_t)0x08006000)
00050   #define BANK1_WRITE_END_ADDR           ((uint32_t)0x08008000)
00051 #elif defined (STM32F091)
00052   #define FLASH_PAGE_SIZE                ((uint16_t)0x800)
00053   #define FLASH_PAGES_TO_BE_PROTECTED    (OB_WRP_Pages28to29 | OB_WRP_Pages30to31 | OB_WRP_Pages32to33 |\
00054                                           OB_WRP_Pages34to35 | OB_WRP_Pages60to61 | OB_WRP_Pages62to127) 
00055 
00056   #define BANK1_WRITE_START_ADDR         ((uint32_t)0x08006000)
00057   #define BANK1_WRITE_END_ADDR           ((uint32_t)0x08040000)
00058 
00059 #else
00060   #define FLASH_PAGE_SIZE                ((uint16_t)0x400)
00061   #define FLASH_PAGES_TO_BE_PROTECTED    (OB_WRP_Pages20to23 | OB_WRP_Pages44to47 | OB_WRP_Pages60to63)  
00062 
00063   #define BANK1_WRITE_START_ADDR         ((uint32_t)0x08006000)
00064   #define BANK1_WRITE_END_ADDR           ((uint32_t)0x08008000)
00065 #endif /* STM32F072 */
00066 
00067 
00068 
00069  
00070 /* Uncomment this line to program the Flash pages */
00071 #define FLASH_PAGE_PROGRAM
00072 
00073 /* Uncomment this line to Enable Write Protection */
00074 //#define WRITE_PROTECTION_ENABLE
00075 
00076 /* Uncomment this line to Disable Write Protection */
00077 //#define WRITE_PROTECTION_DISABLE
00078 
00079 /* Private macro -------------------------------------------------------------*/
00080 /* Private variables ---------------------------------------------------------*/
00081 uint32_t EraseCounter = 0x0, Address = 0x0;
00082 uint16_t Data = 0x1753;
00083 uint32_t WRPR_Value = 0xFFFFFFFF, ProtectedPages = 0x0;
00084 uint32_t NbrOfPage;
00085 __IO FLASH_Status FLASHStatus = FLASH_COMPLETE;
00086 __IO TestStatus MemoryProgramStatus = PASSED;
00087 
00088 /* Private function prototypes -----------------------------------------------*/
00089 /* Private functions ---------------------------------------------------------*/
00090 
00091 /**
00092   * @brief  Main program.
00093   * @param  None
00094   * @retval None
00095   */
00096 int main(void)
00097 {
00098   /*!< At this stage the microcontroller clock setting is already configured, 
00099        this is done through SystemInit() function which is called from startup
00100        file (startup_stm32f0xx.s) before to branch to application main.
00101        To reconfigure the default setting of SystemInit() function, refer to
00102        system_stm32f0xx.c file
00103      */ 
00104   
00105   /* Unlock the Flash Program Erase controller */  
00106   FLASH_Unlock();
00107   
00108   FLASH_OB_Unlock();
00109   
00110   FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
00111     
00112   /* Get pages write protection status */
00113   WRPR_Value = FLASH_OB_GetWRP();
00114 
00115 #ifdef WRITE_PROTECTION_DISABLE 
00116   /* Get pages already write protected */
00117   ProtectedPages = ~(WRPR_Value | FLASH_PAGES_TO_BE_PROTECTED);
00118   
00119   /* Check if desired pages are already write protected */
00120   if((WRPR_Value | (~FLASH_PAGES_TO_BE_PROTECTED)) != 0xFFFFFFFF )
00121   {
00122     /* Erase all the option Bytes */
00123     FLASHStatus = FLASH_OB_Erase();
00124     
00125     /* Check if there is write protected pages */
00126     if(ProtectedPages != 0x0)
00127     {
00128       /* Restore write protected pages */
00129       FLASHStatus = FLASH_OB_EnableWRP(ProtectedPages);
00130     }
00131     /* Generate System Reset to load the new option byte values */
00132     FLASH_OB_Launch();
00133   }
00134 
00135 #elif defined WRITE_PROTECTION_ENABLE
00136   /* Get current write protected pages and the new pages to be protected */
00137   ProtectedPages =  (~WRPR_Value) | FLASH_PAGES_TO_BE_PROTECTED; 
00138   
00139   /* Check if desired pages are not yet write protected */
00140   if(((~WRPR_Value) & FLASH_PAGES_TO_BE_PROTECTED )!= FLASH_PAGES_TO_BE_PROTECTED)
00141   {
00142   
00143     /* Erase all the option Bytes because if a program operation is 
00144       performed on a protected page, the Flash memory returns a 
00145       protection error */
00146     FLASHStatus = FLASH_OB_Erase();
00147 
00148     /* Enable the pages write protection */
00149     FLASHStatus = FLASH_OB_EnableWRP(ProtectedPages);
00150   
00151     /* Generate System Reset to load the new option byte values */
00152     FLASH_OB_Launch();
00153   }  
00154 #endif /* WRITE_PROTECTION_DISABLE */
00155 
00156 #ifdef FLASH_PAGE_PROGRAM  
00157   /* Get the number of pages to be erased */
00158   NbrOfPage = (BANK1_WRITE_END_ADDR - BANK1_WRITE_START_ADDR) / FLASH_PAGE_SIZE;
00159 
00160   /* The selected pages are not write protected */
00161   if ( (WRPR_Value & FLASH_PAGES_TO_BE_PROTECTED) != 0x00)
00162   {
00163     /* Clear pending flags */
00164     FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPERR);        
00165 
00166     /* erase the FLASH pages */
00167     for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
00168     {
00169       FLASHStatus = FLASH_ErasePage(BANK1_WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter));
00170     }
00171   
00172     /* FLASH Half Word program of data 0x1753 at addresses defined by  BANK1_WRITE_START_ADDR and BANK1_WRITE_END_ADDR */
00173     Address = BANK1_WRITE_START_ADDR;
00174 
00175     while((Address < BANK1_WRITE_END_ADDR) && (FLASHStatus == FLASH_COMPLETE))
00176     {
00177       FLASHStatus = FLASH_ProgramHalfWord(Address, Data);
00178       Address = Address + 2;
00179     }
00180 
00181     /* Check the correctness of written data */
00182     Address = BANK1_WRITE_START_ADDR;
00183 
00184     while((Address < BANK1_WRITE_END_ADDR) && (MemoryProgramStatus != FAILED))
00185     {
00186       if((*(__IO uint16_t*) Address) != Data)
00187       {
00188         MemoryProgramStatus = FAILED;
00189       }
00190       Address += 2;
00191     }
00192   }
00193   else
00194   { 
00195     /* Error to program the flash : The desired pages are write protected */ 
00196     MemoryProgramStatus = FAILED;
00197   }
00198 #endif /* FLASH_PAGE_PROGRAM */
00199 
00200   while (1)
00201   {
00202   }
00203 }
00204 
00205 
00206 #ifdef  USE_FULL_ASSERT
00207 
00208 /**
00209   * @brief  Reports the name of the source file and the source line number
00210   *         where the assert_param error has occurred.
00211   * @param  file: pointer to the source file name
00212   * @param  line: assert_param error line source number
00213   * @retval None
00214   */
00215 void assert_failed(uint8_t* file, uint32_t line)
00216 { 
00217   /* User can add his own implementation to report the file name and line number,
00218      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00219 
00220   /* Infinite loop */
00221   while (1)
00222   {
00223   }
00224 }
00225 #endif
00226 
00227 /**
00228   * @}
00229   */
00230 
00231 /**
00232   * @}
00233   */
00234 
00235 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

 For complete documentation on STM32 Microcontrollers visit www.st.com/STM32