/******************************************************************************* * Function Name : FLASH_DisableWriteProtectionPages * Description : Disable the write protection of desired pages * Input : None * Output : None * Return : None *******************************************************************************/ void FLASH_DisableWriteProtectionPages(void) { u32 useroptionbyte = 0, WRPR = 0; u16 var1 = OB_IWDG_SW, var2 = OB_STOP_NoRST, var3 = OB_STDBY_NoRST; FLASH_Status status = FLASH_BUSY; WRPR = FLASH_GetWriteProtectionOptionByte(); /* Test if user memory is write protected */ if ((WRPR & UserMemoryMask) != UserMemoryMask){ useroptionbyte = FLASH_GetUserOptionByte(); UserMemoryMask |= WRPR; status = FLASH_EraseOptionBytes(); if(UserMemoryMask != 0xFFFFFFFF){ status = FLASH_EnableWriteProtection((u32)~UserMemoryMask); } /* Test if user Option Bytes are programmed */ if((useroptionbyte & 0x07) != 0x07){ /* Restore user Option Bytes */ if((useroptionbyte & 0x01) == 0x0){ var1 = OB_IWDG_HW; } if((useroptionbyte & 0x02) == 0x0){ var2 = OB_STOP_RST; } if((useroptionbyte & 0x04) == 0x0){ var3 = OB_STDBY_RST; } FLASH_UserOptionByteConfig(var1, var2, var3); } if (status == FLASH_COMPLETE){ SerialPutString("Write Protection disabled...\r\n"); SerialPutString("...and a System Reset will be generated to re-load the new option bytes\r\n"); /* Enable WWDG clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE); /* Generate a system Reset to re-load the new option bytes: enable WWDG and set counter value to 0x4F, as T6 bit is cleared this will generate a WWDG reset */ WWDG_Enable(0x4F); }else{ SerialPutString("Error: Flash write unprotection failed...\r\n"); } }else{ SerialPutString("Flash memory not write protected\r\n"); } }
void Flash_ProtectMode(u32 *ProtectedPages) { vu32 WRPR_Value; FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPRTERR); /* Get pages write protection status */ WRPR_Value = FLASH_GetWriteProtectionOptionByte(); *ProtectedPages = WRPR_Value & 0x000000C0; #ifdef WriteProtection_Disable if (ProtectedPages == 0x00) {// Pages are write protected // Disable the write protection FLASHStatus = FLASH_EraseOptionBytes(); // Generate System Reset to load the new option byte values NVIC_GenerateSystemReset(); } #else #ifdef WriteProtection_Enable if (ProtectedPages != 0x00) { // Pages not write protected #ifdef USE_STM3210B_EVAL // Enable the pages write protection FLASHStatus = FLASH_EnableWriteProtection(FLASH_WRProt_Pages24to27 |FLASH_WRProt_Pages28to31); #else // Enable the pages write protection FLASHStatus = FLASH_EnableWriteProtection(FLASH_WRProt_Pages12to13 |FLASH_WRProt_Pages14to15); #endif // Generate System Reset to load the new option byte values NVIC_GenerateSystemReset(); } #endif #endif }
/** * @brief Disable the write protection of desired pages * @param None * @retval None */ void FLASH_DisableWriteProtectionPages(void) { uint32_t useroptionbyte = 0, WRPR = 0; uint16_t var1 = OB_IWDG_SW, var2 = OB_STOP_NoRST, var3 = OB_STDBY_NoRST; FLASH_Status status = FLASH_BUSY; WRPR = FLASH_GetWriteProtectionOptionByte(); /* Test if user memory is write protected */ if ((WRPR & UserMemoryMask) != UserMemoryMask) { useroptionbyte = FLASH_GetUserOptionByte(); UserMemoryMask |= WRPR; status = FLASH_EraseOptionBytes(); if (UserMemoryMask != 0xFFFFFFFF) { status = FLASH_EnableWriteProtection((uint32_t)~UserMemoryMask); } /* Test if user Option Bytes are programmed */ if ((useroptionbyte & 0x07) != 0x07) { /* Restore user Option Bytes */ if ((useroptionbyte & 0x01) == 0x0) { var1 = OB_IWDG_HW; } if ((useroptionbyte & 0x02) == 0x0) { var2 = OB_STOP_RST; } if ((useroptionbyte & 0x04) == 0x0) { var3 = OB_STDBY_RST; } FLASH_UserOptionByteConfig(var1, var2, var3); } if (status == FLASH_COMPLETE) { SerialPutString("Write Protection disabled...\r\n"); SerialPutString("...and a System Reset will be generated to re-load the new option bytes\r\n"); /* Generate System Reset to load the new option byte values */ NVIC_SystemReset(); } else { SerialPutString("Error: Flash write unprotection failed...\r\n"); } } else { SerialPutString("Flash memory not write protected\r\n"); } }
/** * @brief Main program * @param None * @retval None */ int main(void) { FLASHStatus = FLASH_COMPLETE; MemoryProgramStatus = PASSED; Data = 0x1753; EraseCounter = 0x0; /* RCC Configuration */ RCC_Configuration(); /* Unlock the Flash Program Erase controller */ FLASH_Unlock(); /* Define the number of page to be erased */ NbrOfPage = (EndAddr - StartAddr) / FLASH_PAGE_SIZE; FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPRTERR); /* Get pages write protection status */ WRPR_Value = FLASH_GetWriteProtectionOptionByte(); ProtectedPages = WRPR_Value & 0x000000C0; #ifdef WriteProtection_Disable if (ProtectedPages == 0x00) {/* Pages are write protected */ /* Disable the write protection */ FLASHStatus = FLASH_EraseOptionBytes(); /* Generate System Reset to load the new option byte values */ NVIC_SystemReset(); } #elif defined WriteProtection_Enable if (ProtectedPages != 0x00) { /* Pages not write protected */ #if defined (STM32F10X_LD) || defined (STM32F10X_MD) /* Enable the pages write protection */ FLASHStatus = FLASH_EnableWriteProtection(FLASH_WRProt_Pages24to27 |FLASH_WRProt_Pages28to31); #else /* (STM32F10X_HD) || defined (STM32F10X_CL) */ /* Enable the pages write protection */ FLASHStatus = FLASH_EnableWriteProtection(FLASH_WRProt_Pages12to13 |FLASH_WRProt_Pages14to15); #endif /* Generate System Reset to load the new option byte values */ NVIC_SystemReset(); } #endif /* If Pages are not write protected, perform erase and program operations Else nothing */ if (ProtectedPages != 0x00) { /* Clear All pending flags */ FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPRTERR); /* erase the FLASH pages */ for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++) { FLASHStatus = FLASH_ErasePage(StartAddr + (FLASH_PAGE_SIZE * EraseCounter)); } /* FLASH Half Word program of data 0x1753 at addresses defined by StartAddr and EndAddr */ Address = StartAddr; while((Address < EndAddr) && (FLASHStatus == FLASH_COMPLETE)) { FLASHStatus = FLASH_ProgramHalfWord(Address, Data); Address = Address + 2; } /* Check the corectness of written data */ Address = StartAddr; while((Address < EndAddr) && (MemoryProgramStatus != FAILED)) { if((*(__IO uint16_t*) Address) != Data) { MemoryProgramStatus = FAILED; } Address += 2; } } while (1) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* Unlock the Flash Program Erase controller */ FLASH_Unlock(); /* Get pages write protection status */ WRPR_Value = FLASH_GetWriteProtectionOptionByte(); #ifdef WRITE_PROTECTION_DISABLE /* Get pages already write protected */ ProtectedPages = ~(WRPR_Value | FLASH_PAGES_TO_BE_PROTECTED); /* Check if desired pages are already write protected */ if((WRPR_Value | (~FLASH_PAGES_TO_BE_PROTECTED)) != 0xFFFFFFFF ) { /* Erase all the option Bytes */ FLASHStatus = FLASH_EraseOptionBytes(); /* Check if there is write protected pages */ if(ProtectedPages != 0x0) { /* Restore write protected pages */ FLASHStatus = FLASH_EnableWriteProtection(ProtectedPages); } /* Generate System Reset to load the new option byte values */ NVIC_SystemReset(); } #elif defined WRITE_PROTECTION_ENABLE /* Get current write protected pages and the new pages to be protected */ ProtectedPages = (~WRPR_Value) | FLASH_PAGES_TO_BE_PROTECTED; /* Check if desired pages are not yet write protected */ if(((~WRPR_Value) & FLASH_PAGES_TO_BE_PROTECTED )!= FLASH_PAGES_TO_BE_PROTECTED) { /* Erase all the option Bytes because if a program operation is performed on a protected page, the Flash memory returns a protection error */ FLASHStatus = FLASH_EraseOptionBytes(); /* Enable the pages write protection */ FLASHStatus = FLASH_EnableWriteProtection(ProtectedPages); /* Generate System Reset to load the new option byte values */ NVIC_SystemReset(); } #endif #ifdef FLASH_PAGE_PROGRAM /* Get the number of pages to be erased */ NbrOfPage = (BANK1_WRITE_END_ADDR - BANK1_WRITE_START_ADDR) / FLASH_PAGE_SIZE; /* The selected pages are not write protected */ if ( (WRPR_Value & FLASH_PAGES_TO_BE_PROTECTED) != 0x00) { /* Clear All pending flags */ FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPRTERR); /* erase the FLASH pages */ for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++) { FLASHStatus = FLASH_ErasePage(BANK1_WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter)); } /* FLASH Half Word program of data 0x1753 at addresses defined by BANK1_WRITE_START_ADDR and BANK1_WRITE_END_ADDR */ Address = BANK1_WRITE_START_ADDR; while((Address < BANK1_WRITE_END_ADDR) && (FLASHStatus == FLASH_COMPLETE)) { FLASHStatus = FLASH_ProgramHalfWord(Address, Data); Address = Address + 2; } /* Check the corectness of written data */ Address = BANK1_WRITE_START_ADDR; while((Address < BANK1_WRITE_END_ADDR) && (MemoryProgramStatus != FAILED)) { if((*(__IO uint16_t*) Address) != Data) { MemoryProgramStatus = FAILED; } Address += 2; } } else { /* Error to programm the falsh : The desired pages are write protected */ MemoryProgramStatus = FAILED; } #endif while (1) { } }