STM32F10x_StdPeriph_Examples/FLASH/Write_Protection/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    FLASH/Write_Protection/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 /** @addtogroup STM32F10x_StdPeriph_Examples
00025   * @{
00026   */
00027 
00028 /** @addtogroup FLASH_Write_Protection
00029   * @{
00030   */ 
00031 
00032 /* Private typedef -----------------------------------------------------------*/
00033 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00034 
00035 /* Private define ------------------------------------------------------------*/
00036 /* Define the STM32F10x FLASH Page Size depending on the used device */
00037 #if defined (STM32F10X_HD) || defined (STM32F10X_HD_VL) || (STM32F10X_CL) || defined (STM32F10X_XL)
00038   #define FLASH_PAGE_SIZE    ((uint16_t)0x800)
00039   #define FLASH_PAGES_TO_BE_PROTECTED (FLASH_WRProt_Pages12to13 | FLASH_WRProt_Pages14to15)  
00040 #else
00041   #define FLASH_PAGE_SIZE    ((uint16_t)0x400)
00042   #define FLASH_PAGES_TO_BE_PROTECTED (FLASH_WRProt_Pages24to27 | FLASH_WRProt_Pages28to31)  
00043 #endif
00044 
00045 #define BANK1_WRITE_START_ADDR  ((uint32_t)0x08006000)
00046 #define BANK1_WRITE_END_ADDR    ((uint32_t)0x08008000)
00047  
00048 /* Uncomment this line to program the Falsh pages */
00049 //#define FLASH_PAGE_PROGRAM
00050 /* Uncomment this line to Enable Write Protection */
00051 //#define WRITE_PROTECTION_ENABLE
00052 /* Uncomment this line to Disable Write Protection */
00053 #define WRITE_PROTECTION_DISABLE
00054 
00055 /* Private macro -------------------------------------------------------------*/
00056 /* Private variables ---------------------------------------------------------*/  
00057 uint32_t EraseCounter = 0x0, Address = 0x0;
00058 uint16_t Data = 0x1753;
00059 uint32_t WRPR_Value = 0xFFFFFFFF, ProtectedPages = 0x0;
00060 uint32_t NbrOfPage;
00061 volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;
00062 volatile TestStatus MemoryProgramStatus = PASSED;
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   /* Unlock the Flash Program Erase controller */  
00082   FLASH_Unlock();
00083 
00084   /* Get pages write protection status */
00085   WRPR_Value = FLASH_GetWriteProtectionOptionByte();
00086 
00087 #ifdef WRITE_PROTECTION_DISABLE
00088   
00089   /* Get pages already write protected */
00090   ProtectedPages = ~(WRPR_Value | FLASH_PAGES_TO_BE_PROTECTED);
00091   
00092   /* Check if desired pages are already write protected */
00093   if((WRPR_Value | (~FLASH_PAGES_TO_BE_PROTECTED)) != 0xFFFFFFFF )
00094   {
00095     /* Erase all the option Bytes */
00096     FLASHStatus = FLASH_EraseOptionBytes();
00097     
00098   /* Check if there is write protected pages */
00099   if(ProtectedPages != 0x0)
00100   {
00101       /* Restore write protected pages */
00102       FLASHStatus = FLASH_EnableWriteProtection(ProtectedPages);
00103     }
00104     /* Generate System Reset to load the new option byte values */
00105     NVIC_SystemReset();
00106   }
00107 
00108 #elif defined WRITE_PROTECTION_ENABLE
00109 
00110   /* Get current write protected pages and the new pages to be protected */
00111   ProtectedPages =  (~WRPR_Value) | FLASH_PAGES_TO_BE_PROTECTED; 
00112   
00113   /* Check if desired pages are not yet write protected */
00114   if(((~WRPR_Value) & FLASH_PAGES_TO_BE_PROTECTED )!= FLASH_PAGES_TO_BE_PROTECTED)
00115   {
00116   
00117     /* Erase all the option Bytes because if a program operation is 
00118       performed on a protected page, the Flash memory returns a 
00119       protection error */
00120     FLASHStatus = FLASH_EraseOptionBytes();
00121 
00122     /* Enable the pages write protection */
00123     FLASHStatus = FLASH_EnableWriteProtection(ProtectedPages);
00124   
00125     /* Generate System Reset to load the new option byte values */
00126     NVIC_SystemReset();
00127   }
00128   
00129 #endif
00130 
00131 #ifdef FLASH_PAGE_PROGRAM  
00132   /* Get the number of pages to be erased */
00133   NbrOfPage = (BANK1_WRITE_END_ADDR - BANK1_WRITE_START_ADDR) / FLASH_PAGE_SIZE;
00134 
00135   /* The selected pages are not write protected */
00136   if ( (WRPR_Value & FLASH_PAGES_TO_BE_PROTECTED) != 0x00)
00137   {
00138     /* Clear All pending flags */
00139     FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPRTERR);     
00140 
00141     /* erase the FLASH pages */
00142     for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
00143     {
00144       FLASHStatus = FLASH_ErasePage(BANK1_WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter));
00145     }
00146   
00147     /* FLASH Half Word program of data 0x1753 at addresses defined by  BANK1_WRITE_START_ADDR and BANK1_WRITE_END_ADDR */
00148     Address = BANK1_WRITE_START_ADDR;
00149 
00150     while((Address < BANK1_WRITE_END_ADDR) && (FLASHStatus == FLASH_COMPLETE))
00151     {
00152       FLASHStatus = FLASH_ProgramHalfWord(Address, Data);
00153       Address = Address + 2;
00154     }
00155 
00156     /* Check the corectness of written data */
00157     Address = BANK1_WRITE_START_ADDR;
00158 
00159     while((Address < BANK1_WRITE_END_ADDR) && (MemoryProgramStatus != FAILED))
00160     {
00161       if((*(__IO uint16_t*) Address) != Data)
00162       {
00163         MemoryProgramStatus = FAILED;
00164       }
00165       Address += 2;
00166     }
00167   }
00168   else
00169   { 
00170     /* Error to programm the falsh : The desired pages are write protected */ 
00171     MemoryProgramStatus = FAILED;
00172   }
00173  
00174 #endif
00175 
00176   while (1)
00177   {
00178   }
00179 }
00180 
00181 #ifdef  USE_FULL_ASSERT
00182 
00183 /**
00184   * @brief  Reports the name of the source file and the source line number
00185   *         where the assert_param error has occurred.
00186   * @param  file: pointer to the source file name
00187   * @param  line: assert_param error line source number
00188   * @retval None
00189   */
00190 void assert_failed(uint8_t* file, uint32_t line)
00191 { 
00192   /* User can add his own implementation to report the file name and line number,
00193      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00194 
00195   while (1)
00196   {
00197   }
00198 }
00199 #endif
00200 
00201 /**
00202   * @}
00203   */ 
00204 
00205 /**
00206   * @}
00207   */ 
00208 
00209 /******************* (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