⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 7xx_flash.c

📁 STR71x (ARM7TDMI) flash programming examples
💻 C
📖 第 1 页 / 共 2 页
字号:
* Function Name  : FLASH_ClearFlag
* Description    : Clears the Flash errors flags.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void FLASH_ClearFlag(void)
{
  /* Clear the correspondent flag */
  FLASH->ER = 0x0;
}

/*******************************************************************************
* Function Name  : FLASH_WaitForLastOperation
* Description    : Waits for the end of the last operation
* Input          : None
* Output         : None
* Return         : None
********************************************************************************
* Important Note :  THIS FUNCTION SHOULD BE EXECUTED FROM RAM
*******************************************************************************/
void FLASH_WaitForLastOperation(void)
{
  /* Wait until the Flash controller acknowledges the end of the last
    operation resetting the BSYAs and LOCK bits in the CR0 register */
  while((FLASH->CR0 & FLASH_FLAG_LOCKBSY) != RESET);
}

/*==============================================================================
                           PROTECTION FUNCTIONS
==============================================================================*/


/*******************************************************************************
* Function Name  : FLASH_WriteProtectionCmd
* Description    : Enables/disables the write protection for the needed sectors.
* Input          : - FLASH_Sectors: the sectors to be protected or unprotected.
*                  This parameter can be one of the following values:
*                      - FLASH_BANK0_SECTOR0: Bank 0 sector 0.
*                      - FLASH_BANK0_SECTOR1: Bank 0 sector 1.
*                      - FLASH_BANK0_SECTOR2: Bank 0 sector 2.
*                      - FLASH_BANK0_SECTOR3: Bank 0 sector 3.
*                      - FLASH_BANK0_SECTOR4: Bank 0 sector 4.
*                      - FLASH_BANK0_SECTOR5: Bank 0 sector 5.
*                      - FLASH_BANK0_SECTOR6: Bank 0 sector 6.
*                      - FLASH_BANK0_SECTOR7: Bank 0 sector 7.
*                      - FLASH_BANK0_MODULE:  Bank 0 module.
*                      - FLASH_BANK1_SECTOR0: Bank 1 sector 0. (N.A for STR73x)
*                      - FLASH_BANK1_SECTOR1: Bank 1 sector 1. (N.A for STR73x)
*                      - FLASH_BANK1_MODULE:  Bank 1 module.   (N.A for STR73x)
*                  - FLASH_NewState: Enable or disable the write protection.
*                  This parameter can be one of the following values:
*                      - ENABLE:  Enable the end of write interrupt.
*                      - DISABLE: Disable the end of write interrupt.
* Output         : None
* Return         : None
********************************************************************************
* Important Note : 
*   - The write protection can be disabled only on a temporary way.  
*   - For the STR73x family this function can work only when the STR73x is on 
*     SystemMemory boot mode. 
*******************************************************************************/
void FLASH_WriteProtectionCmd(u32 FLASH_Sectors, FunctionalState FLASH_NewState)
{
  if (FLASH_NewState == ENABLE)
  {
    /* Set the set protection bit */
    FLASH->CR0 |= FLASH_SPR_MASK;
    /* Set the write protection register address */
    FLASH->AR  = FLASH_NVWPAR_ADDRESS;
    /* Data to be programmed to the protection register */
    FLASH->DR0  = (*(u32*)FLASH_NVWPAR_ADDRESS) & (~FLASH_Sectors);
    /* Start the sequence */
    FLASH->CR0 |= FLASH_WMS_MASK;
  }
  else  /* DISABLE */
  {
    /* Set the set protection bit */
    FLASH->CR0 |= FLASH_SPR_MASK;
    /* Set the write protection register address */
    FLASH->AR  = FLASH_NVWPAR_ADDRESS;
    /* Data to be programmed to the protection register */
    FLASH->DR0  = (*(u32*)FLASH_NVWPAR_ADDRESS) | FLASH_Sectors;
    /* Start the sequence */
    FLASH->CR0 |= FLASH_WMS_MASK;
  }
}

/*******************************************************************************
* Function Name  : FLASH_GetWriteProtectionStatus
* Description    : Get the write protection status for the needed sectors. Only
*                  the non volatile part is returned.
* Input          : - FLASH_Sectors: the needed sectors.
*                  This parameter can be one of the following values:
*                      - FLASH_BANK0_SECTOR0: Bank 0 sector 0.
*                      - FLASH_BANK0_SECTOR1: Bank 0 sector 1.
*                      - FLASH_BANK0_SECTOR2: Bank 0 sector 2.
*                      - FLASH_BANK0_SECTOR3: Bank 0 sector 3.
*                      - FLASH_BANK0_SECTOR4: Bank 0 sector 4.
*                      - FLASH_BANK0_SECTOR5: Bank 0 sector 5.
*                      - FLASH_BANK0_SECTOR6: Bank 0 sector 6.
*                      - FLASH_BANK0_SECTOR7: Bank 0 sector 7.
*                      - FLASH_BANK0_MODULE:  Bank 0 module.
*                      - FLASH_BANK1_SECTOR0: Bank 1 sector 0. (N.A for STR73x)
*                      - FLASH_BANK1_SECTOR1: Bank 1 sector 1. (N.A for STR73x)
*                      - FLASH_BANK1_MODULE:  Bank 1 module.   (N.A for STR73x)
*                  - FLASH_NewState: Enable or disable the write protection.
*                  This parameter can be one of the following values:
*                      - ENABLE:  Enable the end of write interrupt.
*                      - DISABLE: Disable the end of write interrupt.
* Output         : None
* Return         : The status of the write protection (SET or RESET)
********************************************************************************
* Important Note : 
*   - For the STR73x family this function can work only when the STR73x is on 
*     SystemMemory boot mode. 
*******************************************************************************/
FlagStatus FLASH_GetWriteProtectionStatus(u32 FLASH_Sectors)
{
  if ((*(u32*)FLASH_NVWPAR_ADDRESS) & FLASH_Sectors)
  {
    return RESET;
  }
  else
  {
    return SET;
  } 
}

/*******************************************************************************
* Function Name  : FLASH_GetPENProtectionLevel
* Description    : Gets the protection enable level.
* Input          : None
* Output         : None
* Return         : The number of time the debug protection was enabled. 
*                  It's a value from 1 to 16.
********************************************************************************
* Important Note : 
*   - For the STR73x family this function can work only when the STR73x is on 
*     SystemMemory boot mode. 
*******************************************************************************/
u16 FLASH_GetPENProtectionLevel(void)
{
  u16 TmpBitIndex = 0;
  u16 ProtectionRegs = 0;

  ProtectionRegs = ~((*(u32 *)FLASH_NVAPR1_ADDRESS)>>16);

  while (((ProtectionRegs) != 0) && (TmpBitIndex < 16))
  {
    ProtectionRegs  = ProtectionRegs >>  1 ;
    TmpBitIndex++;
  }

  /* Return the number of times the FLASH is Debug protected */
  return TmpBitIndex;
}

/*******************************************************************************
* Function Name  : FLASH_GetPDSProtectionLevel
* Description    : Gets the protection disable level.
* Input          : None
* Output         : None
* Return         : The number of time the debug protection was disabled. 
*                  It's a value from 1 to 16.
********************************************************************************
* Important Note : 
*   - For the STR73x family this function can work only when the STR73x is on 
*     SystemMemory boot mode. 
*******************************************************************************/
u16 FLASH_GetPDSProtectionLevel(void)
{
  u16 TmpBitIndex = 0;
  u16 ProtectionRegs = 0;

  ProtectionRegs = ~(*(u32 *)FLASH_NVAPR1_ADDRESS);

  while (((ProtectionRegs) != 0) && (TmpBitIndex < 16))
  {
    ProtectionRegs  = ProtectionRegs >>  1 ;
    TmpBitIndex++;
  }

  /* Return the number of times the FLASH is Debug protected */
  return TmpBitIndex;
}

/*******************************************************************************
* Function Name  : FLASH_PermanentProtectionCmd
* Description    : Enable or disable the Debug/Readout protection in a permanent
*                  way.
* Input          : - FLASH_NewState: Enable or disable the Debug/Readout 
*                  protection. 
*                  This parameter can be one of the following values:
*                      - ENABLE:  Enable the Debug/Readout protection.
*                      - DISABLE: Disable the Debug/Readout protection.
* Output         : None
* Return         : None
********************************************************************************
* Important Note :  
*   - For the STR73x family this function can work only when the STR73x is on 
*     SystemMemory boot mode. 
*******************************************************************************/
void FLASH_PermanentProtectionCmd(FunctionalState FLASH_NewState)
{
  u16 PEN_ProtectionLevel = FLASH_GetPENProtectionLevel();
  u16 PDS_ProtectionLevel = FLASH_GetPDSProtectionLevel();

  if (FLASH_NewState == ENABLE)
  {
    /*  If it is the first protection, reset the DEBUG bit */
    if(!PDS_ProtectionLevel)
    {
      /* Set the Set Protection bit */
      FLASH->CR0 |= FLASH_SPR_MASK;
      /* Set the Debug Protection register address */
      FLASH->AR  = FLASH_NVAPR0_ADDRESS;
      /* Data to be programmed to the protection register */
      FLASH->DR0 = ~FLASH_PROTECTION_MASK;
      /* Start the operation */
      FLASH->CR0 |= FLASH_WMS_MASK;
    }
    else /* The protection now can be enabled using the PEN bits */
    {
      /* Set the Set Protection bit */
      FLASH->CR0 |= FLASH_SPR_MASK;
      /* Set the Debug Protection register address */
      FLASH->AR  = FLASH_NVAPR1_ADDRESS;
      /* Data to be programmed to the protection register */
      FLASH->DR0 =((~(1 << (16 + PEN_ProtectionLevel))) & (*(u32*)FLASH_NVAPR1_ADDRESS));
      /* Start the operation */
      FLASH->CR0 |= FLASH_WMS_MASK;
    }
  }
  else /* Disable the Debug/ReadOut protection */ 
  {
    /* Set the Set Protection bit */
    FLASH->CR0 |= FLASH_SPR_MASK;
    /* Set the Debug Protection register address */
    FLASH->AR  = FLASH_NVAPR1_ADDRESS;
    /* Data to be programmed to the protection register */
    FLASH->DR0 =(~(1<<(PDS_ProtectionLevel)) & (*(u32*)FLASH_NVAPR1_ADDRESS));
    /* Start the operation */
    FLASH->CR0 |= FLASH_WMS_MASK;
  }
}

/*******************************************************************************
* Function Name  : FLASH_TemporaryProtectionDisable
* Description    : Disable the Debug/Readout protection in a temporary way.
* Input          : None
* Output         : None
* Return         : None
********************************************************************************
* Important Note : 
*   - For the STR73x family this function can work only when the STR73x is on 
*     SystemMemory boot mode. 
*******************************************************************************/
void FLASH_TemporaryProtectionDisable(void)
{
  /* Set the Set Protection bit */
  FLASH->CR0 |= FLASH_SPR_MASK;
  /* Set the Protection register address */
  FLASH->AR  = FLASH_NVAPR0_ADDRESS;
  /* Data to be programmed to the protection register */
  FLASH->DR0 = (*(u32*)FLASH_NVAPR0_ADDRESS) | FLASH_PROTECTION_MASK;
  /* Start the operation */
  FLASH->CR0 |= FLASH_WMS_MASK;
}
/*******************(C)COPYRIGHT 2006 STMicroelectronics *****END OF FILE******/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -