/** * @brief Disables the write protection of user desired pages * @param None * @retval 0: Write Protection successfully disabled * 1: Error: Flash write unprotection failed * 2: Flash memory is not write protected */ uint32_t FLASH_If_DisableWriteProtection(void) { uint32_t UserMemoryMask = 0, WRPR = 0; FLASH_Status status = FLASH_BUSY; /* Clear all FLASH flags */ FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR | FLASH_FLAG_BSY); /* Get Write protection */ WRPR = FLASH_OB_GetWRP(); /* Test if user memory is write protected */ if (FLASH_If_GetWriteProtectionStatus() != 0x00) { /* Enable the FLASH option byte access */ FLASH_OB_Unlock(); /* Erase option bytes */ status = FLASH_OB_Erase(); /* Compute the User_Mask */ UserMemoryMask = FLASH_PROTECTED_PAGES | WRPR; if (UserMemoryMask != 0xFFFFFFFF) { /* Disable Write protection */ status = FLASH_OB_EnableWRP((uint32_t)~UserMemoryMask); } if (status == FLASH_COMPLETE) { /* Write Protection successfully disabled */ return (0); } else { /* Error: Flash write unprotection failed */ return (1); } } else { /* Flash memory is not write protected */ return(2); } }
/** * @brief Display the Main Menu on HyperTerminal * @param None * @retval None */ void Main_Menu(void) { uint8_t key = 0; Serial_PutString((uint8_t *)"\r\n======================================================================"); Serial_PutString((uint8_t *)"\r\n= (C) COPYRIGHT 2015 STMicroelectronics ="); Serial_PutString((uint8_t *)"\r\n= ="); Serial_PutString((uint8_t *)"\r\n= STM32F1xx In-Application Programming Application (Version 1.0.0) ="); Serial_PutString((uint8_t *)"\r\n= ="); Serial_PutString((uint8_t *)"\r\n= By MCD Application Team ="); Serial_PutString((uint8_t *)"\r\n======================================================================"); Serial_PutString((uint8_t *)"\r\n\r\n"); /* Test if any sector of Flash memory where user application will be loaded is write protected */ FlashProtection = FLASH_If_GetWriteProtectionStatus(); while (1) { Serial_PutString((uint8_t *)"\r\n=================== Main Menu ============================\r\n\n"); Serial_PutString((uint8_t *)" Download image to the internal Flash ----------------- 1\r\n\n"); Serial_PutString((uint8_t *)" Upload image from the internal Flash ----------------- 2\r\n\n"); Serial_PutString((uint8_t *)" Execute the loaded application ----------------------- 3\r\n\n"); if(FlashProtection != FLASHIF_PROTECTION_NONE) { Serial_PutString((uint8_t *)" Disable the write protection ------------------------- 4\r\n\n"); } else { Serial_PutString((uint8_t *)" Enable the write protection -------------------------- 4\r\n\n"); } Serial_PutString((uint8_t *)"==========================================================\r\n\n"); /* Clean the input path */ __HAL_UART_FLUSH_DRREGISTER(&UartHandle); /* Receive key */ HAL_UART_Receive(&UartHandle, &key, 1, RX_TIMEOUT); switch (key) { case '1' : /* Download user application in the Flash */ SerialDownload(); break; case '2' : /* Upload user application from the Flash */ SerialUpload(); break; case '3' : Serial_PutString((uint8_t *)"Start program execution......\r\n\n"); /* execute the new program */ JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4); /* Jump to user application */ JumpToApplication = (pFunction) JumpAddress; /* Initialize user application's Stack Pointer */ __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS); JumpToApplication(); break; case '4' : if (FlashProtection != FLASHIF_PROTECTION_NONE) { /* Disable the write protection */ if (FLASH_If_WriteProtectionConfig(FLASHIF_WRP_DISABLE) == FLASHIF_OK) { Serial_PutString((uint8_t *)"Write Protection disabled...\r\n"); Serial_PutString((uint8_t *)"System will now restart...\r\n"); /* Launch the option byte loading */ HAL_FLASH_OB_Launch(); } else { Serial_PutString((uint8_t *)"Error: Flash write un-protection failed...\r\n"); } } else { if (FLASH_If_WriteProtectionConfig(FLASHIF_WRP_ENABLE) == FLASHIF_OK) { Serial_PutString((uint8_t *)"Write Protection enabled...\r\n"); Serial_PutString((uint8_t *)"System will now restart...\r\n"); /* Launch the option byte loading */ HAL_FLASH_OB_Launch(); } else { Serial_PutString((uint8_t *)"Error: Flash write protection failed...\r\n"); } } break; default: Serial_PutString((uint8_t *)"Invalid Number ! ==> The number should be either 1, 2, 3 or 4\r"); break; } } }
/** * @brief Display the Main Menu on HyperTerminal * @param None * @retval None */ void Main_Menu(void) { uint8_t key = 0; SerialPutString("\r\n======================================================================"); SerialPutString("\r\n= (C) COPYRIGHT 2011 STMicroelectronics ="); SerialPutString("\r\n= ="); SerialPutString("\r\n= STM32F4xx In-Application Programming Application (Version 1.0.0) ="); SerialPutString("\r\n= ="); SerialPutString("\r\n= By MCD Application Team ="); SerialPutString("\r\n======================================================================"); SerialPutString("\r\n\r\n"); /* Test if any sector of Flash memory where user application will be loaded is write protected */ if (FLASH_If_GetWriteProtectionStatus() == 0) { FlashProtection = 1; } else { FlashProtection = 0; } while (1) { SerialPutString("\r\n================== Main Menu ============================\r\n\n"); SerialPutString(" Download Image To the STM32F4xx Internal Flash ------- 1\r\n\n"); SerialPutString(" Upload Image From the STM32F4xx 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"); /* Receive key */ key = GetKey(); if (key == 0x31) { /* Download user application in the Flash */ SerialDownload(); } else if (key == 0x32) { /* Upload user application from the Flash */ SerialUpload(); } else if (key == 0x33) /* execute the new program */ { JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4); /* Jump to user application */ Jump_To_Application = (pFunction) JumpAddress; /* Initialize user application's Stack Pointer */ __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS); Jump_To_Application(); } else if ((key == 0x34) && (FlashProtection == 1)) { /* Disable the write protection */ switch (FLASH_If_DisableWriteProtection()) { case 1: { SerialPutString("Write Protection disabled...\r\n"); FlashProtection = 0; break; } case 2: { SerialPutString("Error: Flash write unprotection failed...\r\n"); break; } default: { } } } 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"); } } } }