/******************************************************************************************************* * * 函数名称: * * 函数功能: * * 入口参数: * * 出口参数: 无 * * 说明: * *******************************************************************************************************/ 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); } }
/******************************************************************************************************* * * 函数名称: * * 函数功能: * * 入口参数: * * 出口参数: 无 * * 说明: * *******************************************************************************************************/ 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++); } }
/** * @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_stm32l1xx_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32l1xx.c file */ /* Initialize LED1 */ STM_EVAL_LEDInit(LED1); /* Get BOR Option Bytes */ BOROptionBytes = FLASH_OB_GetBOR(); #ifdef BOR_MODIFY 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(); } #endif while (1) { /* Toggle The LED1 */ STM_EVAL_LEDToggle(LED1); /* Inserted Delay */ for(Counter = 0; Counter < 0x5FFFF; Counter++); } }
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_stm32f0xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file */ /* Unlock the Flash Program Erase controller */ FLASH_Unlock(); FLASH_OB_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR); /* Get pages write protection status */ WRPR_Value = FLASH_OB_GetWRP(); #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_OB_Erase(); /* Check if there is write protected pages */ if(ProtectedPages != 0x0) { /* Restore write protected pages */ FLASHStatus = FLASH_OB_EnableWRP(ProtectedPages); } /* Generate System Reset to load the new option byte values */ FLASH_OB_Launch(); } #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_OB_Erase(); /* Enable the pages write protection */ FLASHStatus = FLASH_OB_EnableWRP(ProtectedPages); /* Generate System Reset to load the new option byte values */ FLASH_OB_Launch(); } #endif /* WRITE_PROTECTION_DISABLE */ #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 pending flags */ FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPERR); /* 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 correctness 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 program the flash : The desired pages are write protected */ MemoryProgramStatus = FAILED; } #endif /* FLASH_PAGE_PROGRAM */ 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_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_stm32l1xx_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32l1xx.c file */ /* Set the vector table address */ #if defined(BOOT_FROM_BANK1) /* Set the vector table to the Bank1 start address */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, BANK1_START_ADDRESS); #elif defined(BOOT_FROM_BANK2) /* Set the vector table to the Bank1 start address */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, BANK2_START_ADDRESS); #endif /* BOOT_FROM_BANK1 */ /* Initialize LEDs, Buttons and LCD on STM32L152D-EVAL board ****************/ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* Save the first BANK1 page */ FLASH_SaveBANK1(); /* Save the first BANK2 page */ FLASH_SaveBANK2(); /* SysTick end of count event each 10ms */ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); /* Configure the Joystick buttons */ STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_RIGHT, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_LEFT, BUTTON_MODE_GPIO); /* Initialize the LCD */ STM32L152D_LCD_Init(); /* Display message on STM32L152D-EVAL LCD ************************************/ /* Clear the LCD */ LCD_Clear(LCD_COLOR_WHITE); /* Set the LCD Back Color */ #if defined(BOOT_FROM_BANK1) LCD_SetBackColor(LCD_COLOR_BLUE); #elif defined(BOOT_FROM_BANK2) LCD_SetBackColor(LCD_COLOR_RED); #endif /* BOOT_FROM_BANK1 */ /* Set the LCD Text Color */ LCD_SetTextColor(LCD_COLOR_WHITE); LCD_DisplayStringLine(LCD_LINE_0, MESSAGE1); LCD_DisplayStringLine(LCD_LINE_1, MESSAGE2); LCD_DisplayStringLine(LCD_LINE_2, MESSAGE3); LCD_DisplayStringLine(LCD_LINE_4, MESSAGE4); LCD_SetFont(&Font12x12); LCD_SetTextColor(LCD_COLOR_GREEN); LCD_DisplayStringLine(LCD_LINE_10, MESSAGE5); LCD_DisplayStringLine(LCD_LINE_11, MESSAGE6); LCD_SetTextColor(LCD_COLOR_WHITE); LCD_DisplayStringLine(LCD_LINE_12, MESSAGE7); LCD_DisplayStringLine(LCD_LINE_13, MESSAGE8); LCD_SetTextColor(LCD_COLOR_GREEN); LCD_DisplayStringLine(LCD_LINE_14, MESSAGE9); LCD_DisplayStringLine(LCD_LINE_15, MESSAGE10); LCD_SetTextColor(LCD_COLOR_WHITE); LCD_DisplayStringLine(LCD_LINE_16, MESSAGE11); LCD_DisplayStringLine(LCD_LINE_17, MESSAGE12); LCD_SetTextColor(LCD_COLOR_GREEN); LCD_DisplayStringLine(LCD_LINE_18, MESSAGE13); LCD_DisplayStringLine(LCD_LINE_19, MESSAGE14); LCD_SetTextColor(LCD_COLOR_WHITE); LCD_SetFont(&Font16x24); /* Turn on leds available on STM32L152D-EVAL ************************************/ STM_EVAL_LEDOn(LED1); STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED4); /* Infinite loop */ while (1) { /*--- If Joystick DOWN button is pushed, reset BFB2 bit to enable boot from Bank2 (active after next reset, w/ Boot pins set in Boot from Flash memory position ---*/ if (STM_EVAL_PBGetState(BUTTON_DOWN) == 0) { /* Reset BFB2 bit to enable boot from Flash Bank2 */ FLASH_Unlock(); FLASH_OB_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR | FLASH_FLAG_OPTVERR | FLASH_FLAG_OPTVERRUSR); FLASH_OB_UserConfig(OB_IWDG_SW, OB_STOP_NoRST, OB_STDBY_NoRST); FLASH_OB_BORConfig(OB_BOR_LEVEL1); if (FLASH_OB_BootConfig(OB_BOOT_BANK2) == FLASH_COMPLETE) { /* Generate System Reset to load the new option byte values */ FLASH_OB_Launch(); } else { /* Display information */ LCD_DisplayStringLine(LCD_LINE_6, MESSAGE15); } } /*--- If Joystick UP button is pushed, set BFB2 bit to enable boot from Bank1 (active after next reset, w/ Boot pins set in Boot from Flash memory position ---*/ if (STM_EVAL_PBGetState(BUTTON_UP) == 0) { /* Set BFB2 bit to enable boot from Flash Bank2 */ FLASH_Unlock(); FLASH_OB_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR | FLASH_FLAG_OPTVERR | FLASH_FLAG_OPTVERRUSR); FLASH_OB_UserConfig(OB_IWDG_SW, OB_STOP_NoRST, OB_STDBY_NoRST); FLASH_OB_BORConfig(OB_BOR_LEVEL1); if (FLASH_OB_BootConfig(OB_BOOT_BANK1) == FLASH_COMPLETE) { /* Generate System Reset to load the new option byte values */ FLASH_OB_Launch(); } else { /* Display information */ LCD_DisplayStringLine(LCD_LINE_6, MESSAGE15); } } /*--- If Joystick LEFT button is pushed, program the content of address 0x08000000 (base address of Bank1) to 0x00 ---*/ if (STM_EVAL_PBGetState(BUTTON_LEFT) == 0) { FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR | FLASH_FLAG_OPTVERR | FLASH_FLAG_OPTVERRUSR); /* Erase stack pointer value at Bank 1 start address */ FLASH_ErasePage(0x08000000); FLASH_Lock(); LCD_ClearLine(LCD_LINE_5); LCD_ClearLine(LCD_LINE_7); LCD_ClearLine(LCD_LINE_8); LCD_ClearLine(LCD_LINE_9); /* Check if erase operation is OK */ if ((uint32_t)(*(uint32_t *)BANK1_START_ADDRESS) != 0x00) { /* Display information */ LCD_DisplayStringLine(LCD_LINE_6, MESSAGE15); } else { /* Display information */ LCD_DisplayStringLine(LCD_LINE_6, MESSAGE16); } } /*--- If Joystick RIGHT button is pushed, program the content of address 0x08030000 (base address of Bank2) to 0x00 ---*/ if (STM_EVAL_PBGetState(BUTTON_RIGHT) == 0) { FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR | FLASH_FLAG_OPTVERR | FLASH_FLAG_OPTVERRUSR); /* Erase stack pointer value at Bank 2 start address */ FLASH_ErasePage(0x08030000); FLASH_Lock(); LCD_ClearLine(LCD_LINE_5); LCD_ClearLine(LCD_LINE_7); LCD_ClearLine(LCD_LINE_8); LCD_ClearLine(LCD_LINE_9); /* Check if erase operation is OK */ if ((uint32_t)(*(uint32_t *)BANK2_START_ADDRESS) != 0x00) { /* Display information */ LCD_DisplayStringLine(LCD_LINE_6, MESSAGE15); } else { /* Display information */ LCD_DisplayStringLine(LCD_LINE_6, MESSAGE16); } } /*--- If Joystick UP button is pushed, copy the program to the cross bank --*/ if (STM_EVAL_PBGetState(BUTTON_SEL) == 0) { FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR | FLASH_FLAG_OPTVERR | FLASH_FLAG_OPTVERRUSR); #if defined (BOOT_FROM_BANK1) /* Erase one page in BANK2 to store the first page */ FLASH_ErasePage(0x08030000); for(index = 0; index < 64; index++) { FLASH_FastProgramWord((0x08030000 + (index * 4)), (*(uint32_t *)(0x08020000 + (index * 4)))); } #elif defined (BOOT_FROM_BANK2) /* Erase one page in BANK1 to store the first page */ FLASH_ErasePage(0x08000000); for(index = 0; index < 64; index++) { FLASH_FastProgramWord((0x08000000 + (index * 4)), (*(uint32_t *)(0x08050000 + (index * 4)))); } #endif } /* Toggle LD3 */ STM_EVAL_LEDToggle(LED3); /* Insert 50 ms delay */ Delay(5); /* Toggle LD2 */ STM_EVAL_LEDToggle(LED2); /* Insert 100 ms delay */ Delay(10); } }
/** * @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 "); } } } }
/** * @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_stm32l1xx_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32l1xx.c file */ /* Enable PWR APB1 Clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_RTCAccessCmd(ENABLE); /* Reset RTC Domain */ RCC_RTCResetCmd(ENABLE); RCC_RTCResetCmd(DISABLE); #ifdef BOR_MODIFY /* 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(); } #endif #if defined (LP_RUN_SRAM_MODE) /* Low Power Run Mode Entry: - System Running at MSI (~32KHz) - Flash 0 wait state - Voltage Range 2 - Code running from Internal SRAM - All peripherals OFF - FLASH switched OFF - VDD from 1.65V to 3.6V - Current Consumption ~10.5uA - Wakeup using Key Button PA.00 */ LowPowerRunMode_Measure(); #elif defined (LP_RUN_FLASH_MODE) /* Low Power Run Mode Entry: - System Running at MSI (~32KHz) - Flash 0 wait state - Voltage Range 2 - Code running from Internal FLASH - All peripherals OFF - VDD from 1.65V to 3.6V - Current Consumption ~25uA - Wakeup using Key Button PA.00 */ LowPowerRunMode_Measure(); #elif defined (SLEEP_MODE) /* Sleep Mode Entry - System Running at HSI (16MHz) - Flash 1 wait state - Voltage Range 2 - Code running from Internal FLASH - Current Consumption ~1mA - Wakeup using EXTI Line (Key Button PA.00) */ SleepMode_Measure(); #elif defined (LP_SLEEP_MODE) /* Low Power Sleep Mode Entry - System Running at MSI (~32KHz) - Flash 0 wait state - Voltage Range 2 - Code running from Internal FLASH - All peripherals OFF - VDD from 1.65V to 3.6V - Current Consumption ~4.07uA - Wakeup using EXTI Line (Key Button PA.00) */ LowPowerSleepMode_Measure(); #elif defined (STOP_MODE) /* STOP Mode Entry - Regulator in LP mode - LSI, HSI and HSE OFF - No IWDG - Current Consumption ~0.5uA - Wakeup using EXTI Line (Key Button PA.00) */ StopMode_Measure(); #elif defined (STOP_RTC_LSE_MODE) /* STOP Mode with RTC on LSE Entry - RTC Clocked by LSE external Clock (32.768KHz) - Regulator in LP mode - LSI, HSI and HSE OFF - No IWDG - Current Consumption ~1.6uA - Automatic Wakeup using RTC on LSE (4s) */ StopRTCLSEMode_Measure(); #elif defined (STOP_RTC_LSI_MODE) /* STOP Mode with RTC on LSI Entry - RTC Clocked by LSI - Regulator in LP mode - HSI and HSE OFF - No IWDG - Current Consumption ~1.3uA - Automatic Wakeup using RTC on LSI (after ~4s) */ StopRTCLSIMode_Measure(); #elif defined (STANDBY_MODE) /* STANDBY Mode Entry - IWDG and LSI OFF - Current Consumption ~0.3uA - Wakeup using WakeUp Pin 1 (PA.00) */ StandbyMode_Measure(); #elif defined (STANDBY_RTC_LSE_MODE) /* STANDBY Mode with RTC on LSE Entry - RTC Clocked by LSE external Clock (32.768KHz) - IWDG and LSI OFF - Current Consumption ~1.3uA - Automatic Wakeup using RTC on LSE (after 4s) */ StandbyRTCLSEMode_Measure(); #elif defined (STANDBY_RTC_LSI_MODE) /* STANDBY Mode with RTC on LSI Entry - RTC Clocked by LSI - IWDG OFF - Current Consumption ~1.1uA - Automatic Wakeup using RTC on LSI (after ~4s) */ StandbyRTCLSIMode_Measure(); #else /* Initialize LED1 on STM32L152-EVAL board */ STM_EVAL_LEDInit(LED1); /* Infinite loop */ while (1) { /* Toggle The LED1 */ STM_EVAL_LEDToggle(LED1); /* Inserted Delay */ for(Counter = 0; Counter < 0x5FF; Counter++); } #endif }