/******************************************************************************************************* * * 函数名称: * * 函数功能: * * 入口参数: * * 出口参数: 无 * * 说明: * *******************************************************************************************************/ void FlashWRDPUnlock(void) { uint32_t rt; FlagStatus RDP_state; FLASH_Status Flash_state; uint32_t optcr_byte; //如果匹配,进行Flash的读写解锁 FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); FLASH_Unlock(); FLASH_OB_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); rt = FLASH_OB_GetWRP(); do { //FlashWRPLock(WRITE, WRP_Sector_All); FLASH_OB_WRPConfig(WRP_Sector_All, DISABLE); //开启写保护 }while(FLASH_OB_Launch() != FLASH_COMPLETE); rt = FLASH_OB_GetWRP(); RDP_state = FLASH_OB_GetRDP(); do { FLASH_OB_RDPConfig(OB_RDP_Level_0);//关闭读保护 }while(FLASH_OB_Launch() != FLASH_COMPLETE); RDP_state = FLASH_OB_GetRDP(); FLASH_OB_Lock(); FLASH_Lock(); }
TM_BOR_Result_t TM_BOR_Set(TM_BOR_Level_t BORValue) { FLASH_Status status; /* Check current BOR value */ if (TM_BOR_Get() != BORValue) { /* Set new value */ /* Unlocks the option bytes block access */ FLASH_OB_Unlock(); /* Select the desired V(BOR) Level */ FLASH_OB_BORConfig((uint32_t)BORValue); /* Launch the option byte loading */ status = FLASH_OB_Launch(); /* Lock access to registers */ FLASH_OB_Lock(); /* Check success */ if (status != FLASH_COMPLETE) { /* Return error */ return TM_BOR_Result_Error; } } /* Return OK */ return TM_BOR_Result_Ok; }
/** * Check the brown out reset threshold is 2.7 volts and if not * resets it. This solves an issue that can prevent boards * powering up with some BEC */ void check_bor() { uint8_t bor = FLASH_OB_GetBOR(); if(bor != OB_BOR_LEVEL3) { FLASH_OB_Unlock(); FLASH_OB_BORConfig(OB_BOR_LEVEL3); FLASH_OB_Launch(); while(FLASH_WaitForLastOperation() == FLASH_BUSY); FLASH_OB_Lock(); while(FLASH_WaitForLastOperation() == FLASH_BUSY); } }
/******************************************************************************************************* * * 函数名称: * * 函数功能: * * 入口参数: * * 出口参数: 无 * * 说明: * *******************************************************************************************************/ void FlashWRPUnlock(uint8_t sector_num) { //FLASH_Unlock(); FLASH_OB_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); if(!(FLASH_OB_GetWRP() & (0x1<<sector_num))) { FLASH_OB_WRPConfig((0x1<<sector_num), DISABLE); //开启写保护 } FLASH_OB_Lock(); //FLASH_Lock(); }
/******************************************************************************************************* * * 函数名称: * * 函数功能: * * 入口参数: * * 出口参数: 无 * * 说明: * *******************************************************************************************************/ int16_t DataAreaWRPLock(COMM_FRAME *pcf) { uint8_t rv_comm_buff[20]; uint16_t wrp_lock_param[2]; uint8_t i; uint8_t rt; uint8_t buff[20]; FlagStatus RDP_state; wrp_lock_param[0] = MAKE_HWORD((pcf)->param[2], (pcf)->param[1]); wrp_lock_param[1] = MAKE_HWORD((pcf)->param[4], (pcf)->param[3]); //加锁命令序列 for(i = 0; i < pcf->len - 5; i++) { rv_comm_buff[i] = pcf->param[5 + i]; } //匹配加锁命令序列 rt = MemoryCmp(rv_comm_buff, LockBuff, sizeof(LockBuff)); if(rt) { //如果匹配,进行Flash的读写加锁 FLASH_Unlock(); FLASH_OB_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); //发送扇区擦除错误 buff[0] = FLASH_WRP_LOCK_CW; buff[1] = 0; SendFrame(UNIVER_ACK_CW, (uint8_t *)buff, 4); //2即2个字节,16bit if(FLASH_OB_GetRDP() != SET) { do { FLASH_OB_RDPConfig(OB_RDP_Level_1);//开启读保护 }while(FLASH_OB_Launch() != FLASH_COMPLETE); } RDP_state = FLASH_OB_GetRDP(); FLASH_OB_Lock(); FLASH_Lock(); } return 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 files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* Initialize LED1 on EVAL board */ STM_EVAL_LEDInit(LED1); /* Initialize Key Button mounted on EVAL board */ STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO); /* Test if Key push-button on EVAL Board is pressed */ if (STM_EVAL_PBGetState(BUTTON_KEY) == 0x00) { /* Get BOR Option Bytes */ if((FLASH_OB_GetBOR() & 0x0C) != BOR_LEVEL) { /* Unlocks the option bytes block access */ FLASH_OB_Unlock(); /* Select the desired V(BOR) Level */ FLASH_OB_BORConfig(BOR_LEVEL); /* Launch the option byte loading */ FLASH_OB_Launch(); /* Locks the option bytes block access */ FLASH_OB_Lock(); } } while (1) { /* Toggle LED1 */ STM_EVAL_LEDToggle(LED1); /* Inserted Delay */ for(uwCounter = 0; uwCounter < 0x5FFFF; uwCounter++); } }
// flash save to option bytes , 2*8 bit int flash2_fmc_write( int data1 , int data2) { FLASH_Unlock(); FLASH_OB_Unlock(); FLASH_OB_Erase(); FLASH_OB_ProgramData( 0x1FFFF804, data1 ); FLASH_OB_ProgramData( 0x1FFFF806, data2 ); FLASH_Lock(); FLASH_OB_Lock(); return 0; }
void FlashWRP(void) { uint32_t rt; FlagStatus RDP_state; FLASH_Unlock(); FLASH_OB_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); RDP_state = FLASH_OB_GetRDP(); rt = FLASH_OB_GetWRP(); do { FLASH_OB_WRPConfig(WRP_Sector_All, ENABLE); //开启写保护 }while(FLASH_OB_Launch() != FLASH_COMPLETE); rt = FLASH_OB_GetWRP(); FLASH_OB_Lock(); FLASH_Lock(); }
void set_bor(void) { __IO uint32_t BOROptionBytes = 0; /* Get BOR Option Bytes */ BOROptionBytes = FLASH_OB_GetBOR(); if((BOROptionBytes & 0x0F) != BOR_LEVEL) { /* Unlocks the option bytes block access */ FLASH_OB_Unlock(); /* Clears the FLASH pending flags */ FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR | FLASH_FLAG_OPTVERR); /* Select the desired V(BOR) Level ---------------------------------------*/ FLASH_OB_BORConfig(BOR_LEVEL); /* Launch the option byte loading */ FLASH_OB_Launch(); FLASH_OB_Lock(); } }
OSStatus internalFlashProtect(uint32_t StartAddress, uint32_t EndAddress, bool enable) { OSStatus err = kNoErr; uint16_t WRP = 0x0; uint32_t StartSector, EndSector, i = 0; bool needupdate = false; /* Get the sector where start the user flash area */ StartSector = _GetWRPSector(StartAddress); EndSector = _GetWRPSector(EndAddress); for(i = StartSector; i <= EndSector; i=i<<1) { WRP = FLASH_OB_GetWRP(); if( ( enable == true && (WRP & i) == 0x0 ) || ( enable == false && (WRP & i) ) ) { continue; } if( needupdate == false){ FLASH_OB_Unlock( ); needupdate = true; } if( enable == true ) FLASH_OB_WRPConfig( i, ENABLE ); else FLASH_OB_WRPConfig( i, DISABLE ); } if( needupdate == true){ FLASH_OB_Launch( ); FLASH_OB_Lock( ); } return err; }
/** * @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_stm32f40xx.s/startup_stm32f427x.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* Initialize LEDs mounted on EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* Initialize Key Button mounted on EVAL board */ STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO); /* Test if Key push-button on EVAL board is pressed */ if (STM_EVAL_PBGetState(BUTTON_KEY) == 0x00) { /* Get FLASH_WRP_SECTORS write protection status */ SectorsWRPStatus = FLASH_OB_GetWRP() & FLASH_WRP_SECTORS; if (SectorsWRPStatus == 0x00) { /* If FLASH_WRP_SECTORS are write protected, disable the write protection */ /* Enable the Flash option control register access */ FLASH_OB_Unlock(); /* Disable FLASH_WRP_SECTORS write protection */ FLASH_OB_WRPConfig(FLASH_WRP_SECTORS, DISABLE); /* Start the Option Bytes programming process */ if (FLASH_OB_Launch() != FLASH_COMPLETE) { /* User can add here some code to deal with this error */ while (1) { } } /* Disable the Flash option control register access (recommended to protect the option Bytes against possible unwanted operations) */ FLASH_OB_Lock(); /* Get FLASH_WRP_SECTORS write protection status */ SectorsWRPStatus = FLASH_OB_GetWRP() & FLASH_WRP_SECTORS; /* Check if FLASH_WRP_SECTORS write protection is disabled */ if (SectorsWRPStatus == FLASH_WRP_SECTORS) { /* OK, turn ON LED1 */ STM_EVAL_LEDOn(LED1); } else { /* KO, turn ON LED3 */ STM_EVAL_LEDOn(LED3); } } else { /* If FLASH_WRP_SECTORS are not write protected, enable the write protection */ /* Enable the Flash option control register access */ FLASH_OB_Unlock(); /* Enable FLASH_WRP_SECTORS write protection */ FLASH_OB_WRPConfig(FLASH_WRP_SECTORS, ENABLE); /* Start the Option Bytes programming process */ if (FLASH_OB_Launch() != FLASH_COMPLETE) { /* User can add here some code to deal with this error */ while (1) { } } /* Disable the Flash option control register access (recommended to protect the option Bytes against possible unwanted operations) */ FLASH_OB_Lock(); /* Get FLASH_WRP_SECTORS write protection status */ SectorsWRPStatus = FLASH_OB_GetWRP() & FLASH_WRP_SECTORS; /* Check if FLASH_WRP_SECTORS are write protected */ if (SectorsWRPStatus == 0x00) { /* OK, turn ON LED4 */ STM_EVAL_LEDOn(LED4); } else { /* KO, turn ON LED3 */ STM_EVAL_LEDOn(LED3); } } } /* Turn ON LED2 */ STM_EVAL_LEDOn(LED2); while (1) { } }
/******************************************************************************************************* * * 函数名称: * * 函数功能: * * 入口参数: * * 出口参数: 无 * * 说明: * *******************************************************************************************************/ int16_t DataAreaWRPUnlock(COMM_FRAME *pcf) { uint32_t wrp_state; uint8_t rv_comm_buff[20]; uint16_t wrp_unlock_param[2]; uint8_t i; uint8_t rt; uint8_t buff[20]; FlagStatus RDP_state; wrp_unlock_param[0] = MAKE_HWORD((pcf)->param[2], (pcf)->param[1]); wrp_unlock_param[1] = MAKE_HWORD((pcf)->param[4], (pcf)->param[3]); //加锁命令序列 for(i = 0; i < pcf->len - 5; i++) { rv_comm_buff[i] = pcf->param[5 + i]; } //匹配加锁命令序列 rt = MemoryCmp(rv_comm_buff, UnlockBuff, sizeof(UnlockBuff)); if(rt) { //如果匹配,进行Flash的读写解锁 FLASH_Unlock(); FLASH_OB_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_OPERR|FLASH_FLAG_WRPERR| FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); wrp_state = FLASH_OB_GetWRP(); if(!wrp_state) { do { FLASH_OB_WRPConfig(WRP_Sector_All, DISABLE); //关闭写保护 }while(FLASH_OB_Launch() != FLASH_COMPLETE); buff[1] = 0; } else { buff[1] = 1; } //发送扇区擦除错误 buff[0] = FLASH_WRP_UNLOCK_CW; SendFrame(UNIVER_ACK_CW, (uint8_t *)buff, 4); //2即2个字节,16bit __disable_irq(); if(FLASH_OB_GetRDP() == SET) { do { FLASH_OB_RDPConfig(OB_RDP_Level_0);//关闭读保护 }while(FLASH_OB_Launch() != FLASH_COMPLETE); } else { buff[0] = FLASH_WRP_UNLOCK_CW; buff[1] = 1; SendFrame(UNIVER_ACK_CW, (uint8_t *)buff, 4); //2即2个字节,16bit } //解锁之后Flash全部擦除,RAM调试会执行到下面的程序 __enable_irq(); RDP_state = FLASH_OB_GetRDP(); FLASH_OB_Lock(); FLASH_Lock(); } return 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_stm32f429_439xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* LCD initiatization */ LCD_Init(); LCD_LayerInit(); /* LTDC reload configuration */ LTDC_ReloadConfig(LTDC_IMReload); /* Enable the LTDC */ LTDC_Cmd(ENABLE); /* Set LCD foreground layer */ LCD_SetLayer(LCD_FOREGROUND_LAYER); /* Initialize LEDs mounted on STM32F429I-DISCO */ STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* Initialize User Button mounted on STM32F429I-DISCO */ STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_GPIO); /* Display tset name on LCD */ LCD_Clear(LCD_COLOR_WHITE); LCD_SetBackColor(LCD_COLOR_BLUE); LCD_SetTextColor(LCD_COLOR_WHITE); LCD_DisplayStringLine(LCD_LINE_4,(uint8_t*)" Flash Write "); LCD_DisplayStringLine(LCD_LINE_5,(uint8_t*)"protection test"); LCD_DisplayStringLine(LCD_LINE_7,(uint8_t*)" Press User "); LCD_DisplayStringLine(LCD_LINE_8,(uint8_t*)" push-button "); while (1) { /* Wait for User push-button is pressed */ while (STM_EVAL_PBGetState(BUTTON_USER) != Bit_RESET) { } /* Wait for User push-button is released */ while (STM_EVAL_PBGetState(BUTTON_USER) != Bit_SET) { } /* Get FLASH_WRP_SECTORS write protection status */ SectorsWRPStatus = FLASH_OB_GetWRP() & FLASH_WRP_SECTORS; if (SectorsWRPStatus == 0x00) { /* If FLASH_WRP_SECTORS are write protected, disable the write protection */ /* Enable the Flash option control register access */ FLASH_OB_Unlock(); /* Disable FLASH_WRP_SECTORS write protection */ FLASH_OB_WRPConfig(FLASH_WRP_SECTORS, DISABLE); /* Start the Option Bytes programming process */ if (FLASH_OB_Launch() != FLASH_COMPLETE) { /* User can add here some code to deal with this error */ while (1) { } } /* Disable the Flash option control register access (recommended to protect the option Bytes against possible unwanted operations) */ FLASH_OB_Lock(); /* Get FLASH_WRP_SECTORS write protection status */ SectorsWRPStatus = FLASH_OB_GetWRP() & FLASH_WRP_SECTORS; /* Check if FLASH_WRP_SECTORS write protection is disabled */ if (SectorsWRPStatus == FLASH_WRP_SECTORS) { LCD_Clear(LCD_COLOR_GREEN); LCD_SetTextColor(LCD_COLOR_BLACK); LCD_DisplayStringLine(LCD_LINE_5,(uint8_t*)" Write "); LCD_DisplayStringLine(LCD_LINE_6,(uint8_t*)" protection is "); LCD_DisplayStringLine(LCD_LINE_7,(uint8_t*)" disabled "); } else { LCD_Clear(LCD_COLOR_RED); LCD_SetTextColor(LCD_COLOR_BLACK); LCD_DisplayStringLine(LCD_LINE_5,(uint8_t*)" Write "); LCD_DisplayStringLine(LCD_LINE_6,(uint8_t*)" protection is "); LCD_DisplayStringLine(LCD_LINE_7,(uint8_t*)" not disabled "); } } else { /* If FLASH_WRP_SECTORS are not write protected, enable the write protection */ /* Enable the Flash option control register access */ FLASH_OB_Unlock(); /* Enable FLASH_WRP_SECTORS write protection */ FLASH_OB_WRPConfig(FLASH_WRP_SECTORS, ENABLE); /* Start the Option Bytes programming process */ if (FLASH_OB_Launch() != FLASH_COMPLETE) { /* User can add here some code to deal with this error */ while (1) { } } /* Disable the Flash option control register access (recommended to protect the option Bytes against possible unwanted operations) */ FLASH_OB_Lock(); /* Get FLASH_WRP_SECTORS write protection status */ SectorsWRPStatus = FLASH_OB_GetWRP() & FLASH_WRP_SECTORS; /* Check if FLASH_WRP_SECTORS are write protected */ if (SectorsWRPStatus == 0x00) { LCD_Clear(LCD_COLOR_GREEN); LCD_SetTextColor(LCD_COLOR_BLACK); LCD_DisplayStringLine(LCD_LINE_5,(uint8_t*)" Write "); LCD_DisplayStringLine(LCD_LINE_6,(uint8_t*)" protection is "); LCD_DisplayStringLine(LCD_LINE_7,(uint8_t*)" enabled "); } else { LCD_Clear(LCD_COLOR_RED); LCD_SetTextColor(LCD_COLOR_BLACK); LCD_DisplayStringLine(LCD_LINE_5,(uint8_t*)" Write "); LCD_DisplayStringLine(LCD_LINE_6,(uint8_t*)" protection is "); LCD_DisplayStringLine(LCD_LINE_7,(uint8_t*)" not enabled "); } } } }