Beispiel #1
0
/*******************************************************************************************************
 *
 *         函数名称: 
 *
 *         函数功能: 
 *
 *         入口参数: 
 *
 *         出口参数: 无
 *
 *             说明:
 *
 *******************************************************************************************************/
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();
    
}
Beispiel #2
0
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;
}
Beispiel #3
0
/*******************************************************************************************************
 *
 *         函数名称: 
 *
 *         函数功能: 
 *
 *         入口参数: 
 *
 *         出口参数: 无
 *
 *             说明:
 *
 *******************************************************************************************************/
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();    
    
}
Beispiel #4
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();    
    
}
Beispiel #5
0
/**
  * @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)
  {
  }
}
Beispiel #6
0
/*******************************************************************************************************
 *
 *         函数名称: 
 *
 *         函数功能: 
 *
 *         入口参数: 
 *
 *         出口参数: 无
 *
 *             说明:
 *
 *******************************************************************************************************/
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;    
}
Beispiel #7
0
/**
  * @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    ");
      }
    }
  }
}