/******************************************************************************* * 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 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 Display the Main Menu on to HyperTerminal * @param None * @retval None */ void Main_Menu(void) { uint8_t key = 0; /* Get the number of block (4 or 2 pages) from where the user program will be loaded */ BlockNbr = (FlashDestination - 0x08000000) >> 12; /* Compute the mask to test if the Flash memory, where the user program will be loaded, is write protected */ #if defined (STM32F10X_MD) || defined (STM32F10X_MD_VL) UserMemoryMask = ((uint32_t)~((1 << BlockNbr) - 1)); #else /* USE_STM3210E_EVAL */ if (BlockNbr < 62) { UserMemoryMask = ((uint32_t)~((1 << BlockNbr) - 1)); } else { UserMemoryMask = ((uint32_t)0x80000000); } #endif /* (STM32F10X_MD) || (STM32F10X_MD_VL) */ /* Test if any page of Flash memory where program user will be loaded is write protected */ if ((FLASH_GetWriteProtectionOptionByte() & UserMemoryMask) != UserMemoryMask) { FlashProtection = 1; } else { FlashProtection = 0; } while (1) { SerialPutString("\r\n================== Main Menu ============================\r\n\n"); SerialPutString(" Download Image To the STM32F10x Internal Flash ------- 1\r\n\n"); SerialPutString(" Upload Image From the STM32F10x Internal Flash ------- 2\r\n\n"); SerialPutString(" Execute The New Program ------------------------------ 3\r\n\n"); if(FlashProtection != 0) { SerialPutString(" Disable the write protection ------------------------- 4\r\n\n"); } SerialPutString("==========================================================\r\n\n"); key = GetKey(); if (key == 0x31) { /* Download user application in the Flash */ SerialPutString(" File --> Transfer --> YMODEM --> Send... \r\n\n"); SerialDownload(); } else if (key == 0x32) { /* Upload user application from the Flash */ SerialUpload(); } else if (key == 0x33) { JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4); /* Jump to user application */ Jump_To_Application = (pFunction) JumpAddress; /* Initialize user application's Stack Pointer */ __set_MSP(*(__IO uint32_t*) ApplicationAddress); Jump_To_Application(); } else if ((key == 0x34) && (FlashProtection == 1)) { /* Disable the write protection of desired pages */ FLASH_DisableWriteProtectionPages(); } else { if (FlashProtection == 0) { SerialPutString("Invalid Number ! ==> The number should be either 1, 2 or 3\r"); } else { SerialPutString("Invalid Number ! ==> The number should be either 1, 2, 3 or 4\r"); } } } }
/** * @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"); } }
/******************************************************************************* * Function Name : Main_Menu * Description : Display the Main Menu on to HyperTerminal * Input : None * Output : None * Return : None *******************************************************************************/ void Main_Menu(void) { u8 key = 0; /* Get the number of block (4 pages) from where the user program will be loaded */ BlockNbr = (FlashDestination - 0x08000000) >> 12; /* Compute the mask to test if the Flash memory, where the user program will be loaded, is write protected */ UserMemoryMask = ((u32)~((1<<BlockNbr)-1)); /* Test if any page of Flash memory where program user will be loaded is write protected */ if ((FLASH_GetWriteProtectionOptionByte() & UserMemoryMask) != UserMemoryMask) { FlashProtection = TRUE; SerialPutString("\r\n================== Main Menu ============================\r\n\n"); SerialPutString(" Download Image To the STM32F10x Internal Flash ------- 1\r\n\n"); SerialPutString(" Execute The New Program ------------------------------ 2\r\n\n"); SerialPutString(" Disable the write protection ------------------------- 3\r\n\n"); SerialPutString("==========================================================\r\n\n"); } else { FlashProtection = FALSE; SerialPutString("\r\n================== Main Menu ============================\r\n\n"); SerialPutString(" Download Image To the STM32F10x Internal Flash ------- 1\r\n\n"); SerialPutString(" Execute The New Program ------------------------------ 2\r\n\n"); SerialPutString("==========================================================\r\n\n"); } while (1) { // SerialPutString("ready to get choice\r\n"); key = GetKey(); // SerialPutString("\r\n"); // SerialPutChar(key); // SerialPutString("\r\nget choice\r\n"); if (key == 0x31) { /* Download user application in the Flash */ SerialDownload(); } else if (key == 0x32) { JumpAddress = *(vu32*) (ApplicationAddress + 4); //the address value /* Jump to user application */ Jump_To_Application = (pFunction) JumpAddress; /* Initialize user application's Stack Pointer */ __set_MSP(*(vu32*) ApplicationAddress); Jump_To_Application(); } else if ((key == 0x33)&& (FlashProtection == TRUE)) { /* Disable the write protection of desired pages */ FLASH_DisableWriteProtectionPages(); } else { if(FlashProtection == FALSE) { SerialPutString("Invalid Number ! ==> The number should be either 1 or 2\r\n"); } else { SerialPutString("Invalid Number ! ==> The number should be either 1, 2 or 3\r\n"); } } } }
/** * @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) { } }