コード例 #1
0
ファイル: WRProtect.c プロジェクト: meterhu/stm32F4
/*******************************************************************************************************
 *
 *         函数名称: 
 *
 *         函数功能: 
 *
 *         入口参数: 
 *
 *         出口参数: 无
 *
 *             说明:
 *
 *******************************************************************************************************/
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();
    
}
コード例 #2
0
/**
  * @brief Get the Option byte configuration
  * @note  The parameter Banks of the pOBInit structure must be exclusively FLASH_BANK_1 or FLASH_BANK_2
           as this parameter is use to get the given Bank WRP, PCROP and secured area.   
  * @param  pOBInit pointer to an FLASH_OBInitStruct structure that
  *         contains the configuration information for the programming.
  * 
  * @retval None
  */
void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
{
  /* Check the parameters */
  assert_param(IS_FLASH_BANK_EXCLUSIVE(pOBInit->Banks));  
  pOBInit->OptionType = (OPTIONBYTE_WRP  | OPTIONBYTE_RDP         | \
                         OPTIONBYTE_USER | OPTIONBYTE_PCROP       | \
                         OPTIONBYTE_BOR  | OPTIONBYTE_BOOTADD | \
                         OPTIONBYTE_SECURE_AREA);

  /* Get write protection on the selected area */
  FLASH_OB_GetWRP(&(pOBInit->WRPState), &(pOBInit->WRPSector), pOBInit->Banks);
  
  /* Get Read protection level */
  pOBInit->RDPLevel = FLASH_OB_GetRDP();
  
  /* Get the user option bytes */
  pOBInit->USERConfig = FLASH_OB_GetUser();
  
  /* Get the Proprietary code readout protection */
  FLASH_OB_GetPCROP(&(pOBInit->PCROPConfig), &(pOBInit->PCROPStartAddr), &(pOBInit->PCROPEndAddr), pOBInit->Banks);
  
  /*Get BOR Level*/
  pOBInit->BORLevel = FLASH_OB_GetBOR();
  
  /*Get Boot Address*/
  FLASH_OB_GetBootAdd(&(pOBInit->BootAddr0), &(pOBInit->BootAddr1));
  /*Get Bank Secure area*/
  FLASH_OB_GetSecureArea(&(pOBInit->SecureAreaConfig), &(pOBInit->SecureAreaStartAddr), &(pOBInit->SecureAreaEndAddr), pOBInit->Banks);
}
コード例 #3
0
/**
  * @brief  Get the Option byte configuration
  * @param  pOBInit pointer to an FLASH_OBInitStruct structure that
  *         contains the configuration information for the programming.
  *
  * @retval None
  */
void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
{
  pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER;

  /*Get WRP*/
  pOBInit->WRPPage = FLASH_OB_GetWRP();

  /*Get RDP Level*/
  pOBInit->RDPLevel = FLASH_OB_GetRDP();

  /*Get USER*/
  pOBInit->USERConfig = FLASH_OB_GetUser();
}
コード例 #4
0
ファイル: WRProtect.c プロジェクト: meterhu/stm32F4
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();    
    
}
コード例 #5
0
ファイル: WRProtect.c プロジェクト: meterhu/stm32F4
/*******************************************************************************************************
 *
 *         函数名称: 
 *
 *         函数功能: 
 *
 *         入口参数: 
 *
 *         出口参数: 无
 *
 *             说明:
 *
 *******************************************************************************************************/
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();    
    
}
コード例 #6
0
/**
  * @brief   Get the Option byte configuration
  * @param  pOBInit: pointer to an FLASH_OBInitStruct structure that
  *         contains the configuration information for the programming.
  * 
  * @retval None
  */
void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
{
  pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR;

  /*Get WRP*/
  pOBInit->WRPSector = (uint32_t)FLASH_OB_GetWRP();

  /*Get RDP Level*/
  pOBInit->RDPLevel = (uint32_t)FLASH_OB_GetRDP();

  /*Get USER*/
  pOBInit->USERConfig = (uint8_t)FLASH_OB_GetUser();

  /*Get BOR Level*/
  pOBInit->BORLevel = (uint32_t)FLASH_OB_GetBOR();
}
コード例 #7
0
ファイル: flash_if.c プロジェクト: jesstr/smarthome
/**
  * @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);
  }
}
コード例 #8
0
ファイル: platform_flash.c プロジェクト: SmartArduino/MICO-1
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;
}
コード例 #9
0
ファイル: main.c プロジェクト: Azizou/stm32f0_devel
/**
  * @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)
  {
  }
}
コード例 #10
0
ファイル: main.c プロジェクト: BlockWorksCo/Platform
/**
  * @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)
  {
  }
}
コード例 #11
0
/**
  * @brief  Disable the write protection of the desired pages
  * @note   An option byte erase is done automatically in this function. 
  * @note   When the memory read protection level is selected (RDP level = 1), 
  *         it is not possible to program or erase the flash page i if   
  *         debug features are connected or boot code is executed in RAM, even if nWRPi = 1 
  * 
  * @param  WriteProtectPage: specifies the page(s) to be write unprotected.
  *         The value of this parameter depend on device used within the same series 
  * @retval HAL status 
  */
static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage)
{
  HAL_StatusTypeDef status = HAL_OK;
  uint16_t WRP0_Data = 0xFFFF;
#if defined(FLASH_WRP1_WRP1)
  uint16_t WRP1_Data = 0xFFFF;
#endif /* FLASH_WRP1_WRP1 */
#if defined(FLASH_WRP2_WRP2)
  uint16_t WRP2_Data = 0xFFFF;
#endif /* FLASH_WRP2_WRP2 */
#if defined(FLASH_WRP3_WRP3)
  uint16_t WRP3_Data = 0xFFFF;
#endif /* FLASH_WRP3_WRP3 */
  
  /* Check the parameters */
  assert_param(IS_OB_WRP(WriteProtectPage));

  /* Get current write protected pages and the new pages to be unprotected ******/
  WriteProtectPage = (FLASH_OB_GetWRP() | WriteProtectPage);

#if defined(OB_WRP_PAGES0TO15MASK)
  WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK);
#elif defined(OB_WRP_PAGES0TO31MASK)
  WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO31MASK);
#endif /* OB_WRP_PAGES0TO31MASK */
  
#if defined(OB_WRP_PAGES16TO31MASK)
  WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8);
#elif defined(OB_WRP_PAGES32TO63MASK)
  WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO63MASK) >> 8);
#endif /* OB_WRP_PAGES32TO63MASK */
 
#if defined(OB_WRP_PAGES64TO95MASK)
  WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES64TO95MASK) >> 16);
#endif /* OB_WRP_PAGES64TO95MASK */
#if defined(OB_WRP_PAGES32TO47MASK)
  WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16);
#endif /* OB_WRP_PAGES32TO47MASK */

#if defined(OB_WRP_PAGES96TO127MASK)
  WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES96TO127MASK) >> 24); 
#elif defined(OB_WRP_PAGES48TO255MASK)
  WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO255MASK) >> 24); 
#elif defined(OB_WRP_PAGES48TO511MASK)
  WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO511MASK) >> 24); 
#elif defined(OB_WRP_PAGES48TO127MASK)
  WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24); 
#endif /* OB_WRP_PAGES96TO127MASK */

    
  /* Wait for last operation to be completed */
  status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);

  if(status == HAL_OK)
  { 
    /* Clean the error context */
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;

    /* To be able to write again option byte, need to perform a option byte erase */
    status = HAL_FLASHEx_OBErase();
    if (status == HAL_OK)  
    {
      SET_BIT(FLASH->CR, FLASH_CR_OPTPG);

#if defined(FLASH_WRP0_WRP0)
      if(WRP0_Data != 0xFF)
      {
        OB->WRP0 |= WRP0_Data;
        
        /* Wait for last operation to be completed */
        status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
      }
#endif /* FLASH_WRP0_WRP0 */

#if defined(FLASH_WRP1_WRP1)
      if((status == HAL_OK) && (WRP1_Data != 0xFF))
      {
        OB->WRP1 |= WRP1_Data;
        
        /* Wait for last operation to be completed */
        status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
      }
#endif /* FLASH_WRP1_WRP1 */

#if defined(FLASH_WRP2_WRP2)
      if((status == HAL_OK) && (WRP2_Data != 0xFF))
      {
        OB->WRP2 |= WRP2_Data;
        
        /* Wait for last operation to be completed */
        status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
      }
#endif /* FLASH_WRP2_WRP2 */

#if defined(FLASH_WRP3_WRP3)
      if((status == HAL_OK) && (WRP3_Data != 0xFF))
      {
        OB->WRP3 |= WRP3_Data;
        
        /* Wait for last operation to be completed */
        status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
      }
#endif /* FLASH_WRP3_WRP3 */

      /* if the program operation is completed, disable the OPTPG Bit */
      CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG);
    }
  }
  return status;
}
コード例 #12
0
ファイル: WRProtect.c プロジェクト: meterhu/stm32F4
/*******************************************************************************************************
 *
 *         函数名称: 
 *
 *         函数功能: 
 *
 *         入口参数: 
 *
 *         出口参数: 无
 *
 *             说明:
 *
 *******************************************************************************************************/
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;    
}
コード例 #13
0
ファイル: main.c プロジェクト: ChingHengWang/stm32_ws
/**
  * @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    ");
      }
    }
  }
}