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

📄 flash.c

📁 意法半導體STR710,USB範例程式,模擬U盤
💻 C
📖 第 1 页 / 共 2 页
字号:
* Input 2        : Data Source Address
* Input 3        : Nbr of word to be Read
* Return         : Word
*******************************************************************************/
void FLASH_BlockRead(u32 DestAdd, u32 SourceAdd, u32 NbrData)
{
  u32 TmpaddrD,TmpaddrS,TmpNbrData;
  // Number of data to be Read from the Flash
  TmpNbrData = NbrData;
  // Source Address
  TmpaddrS = SourceAdd;
  // Destination Address
  TmpaddrD = DestAdd;

  WaitForLastTask(FLASH_BANK0);
  WaitForLastTask(FLASH_BANK1);

  while(TmpNbrData > 0)
  {
    *(u32 *)TmpaddrD = FLASH_WordRead(TmpaddrS);
    // Increase the Source Address
    TmpaddrS += 4;
    // Increase the Destination Address
    TmpaddrD += 4;
    // Decrease the Number of data to be read
    TmpNbrData --;
  }
}

/*******************************************************************************
* Function Name  : FLASH_WritePrConfig
* Description    : Configures The Write Protection Bits
* Input 1        : Flash Bank
* Input 2        : Enable Or disable Protection
* Return         : None
*******************************************************************************/
void FLASH_WritePrConfig(u32 Xsectors, FunctionalState Xstatus)
{
  u32 TmpProtection;

  TmpProtection = FLASHPR->NVWPAR;
  WaitForLastTask(FLASH_BANK0);
  WaitForLastTask(FLASH_BANK1);
  if (Xstatus == DISABLE) TmpProtection |= Xsectors;
  else TmpProtection &= ~Xsectors;
  // Set the Set protection Bit
  FLASHR->CR0 |= FLASH_SPR_Mask;
  // Set the Register Address
  FLASHR->AR  = 0x4010DFB0;
  // Data To be Programmed to the Protection Register
  FLASHR->DR0  = TmpProtection;
  // Set the WMS bit to Start the Sequence
  FLASHR->CR0 |= FLASH_WMS_Mask;
}

/*******************************************************************************
* Function Name  : FLASH_DebugPrConfig
* Description    : Configures The Debug Protection Bits
* Input 1        : ENABLE or DISABLE
* Return         : Word
*******************************************************************************/
void FLASH_DebugPrConfig(FunctionalState Xstatus)
{
  u16 TmpPEN, TmpPDS, TmpProtection;
  TmpPEN = (FLASHPR->NVAPR1 & 0xFFFF0000) >> 16;
  TmpPDS = (FLASHPR->NVAPR1 & 0x0000FFFF);

  if (Xstatus == ENABLE)
  {
    //  If the First Protection Reset the DBGP bit
    if ((FLASHPR->NVAPR0 & 0x2)==0x02) FLASHPR->NVAPR0 &= ~FLASH_DBGP_Mask;
    else FLASHPR->NVAPR1 = ResetBit(FLASHPR->NVAPR1, ProtectionLevel(TmpPEN) + 16);
    //  FLASHPR->NVAPR1 =  TmpPDS |((TmpPEN & ~(1<<ProtectionLevel(TmpPEN)))<< 16);
  }
  else
  {
    //  Test wether the Protection is already Enabled
    if ((FLASHPR->NVAPR0 & 0x2) == 0x0) FLASHPR->NVAPR1 = ResetBit(FLASHPR->NVAPR1, ProtectionLevel(TmpPDS));
    //  FLASHPR->NVAPR1 =(TmpPEN << 16)|(TmpPDS &~(1 << ProtectionLevel(TmpPDS)));
  }
  // Set the Set protection Bit
  FLASHR->CR0 |= FLASH_SPR_Mask;
  // Set the Register Address
  FLASHR->AR  = 0x4010DFB0;
  // Data To be Programmed to the Protection Register
  FLASHR->DR0 = TmpProtection;
  // Set the WMS bit to Start the Sequence
  FLASHR->CR0 |= FLASH_WMS_Mask;
}

/*******************************************************************************
* Function Name  : FLASH_AccessPrConfig
* Description    : Configures The Access Protection Bits
* Input 1        : ENABLE or DISABLE
* Return         : Word
*******************************************************************************/
void FLASH_AccessPrConfig(FunctionalState Xstatus)
{
  u16 TmpPEN, TmpPDS;
  TmpPEN = (FLASHPR->NVAPR1 & 0xFFFF0000) >> 16;
  TmpPDS = (FLASHPR->NVAPR1 & 0x0000FFFF);

  if (Xstatus == ENABLE)
  {
    //  If the First Protection Reset the DBGP bit
    if ((FLASHPR->NVAPR0 & 0x1)==0x01) FLASHPR->NVAPR0 &= ~FLASH_ACCP_Mask;
    else FLASHPR->NVAPR1 = ResetBit(FLASHPR->NVAPR1, ProtectionLevel(TmpPEN) + 16);
    //  FLASHPR->NVAPR1 =  TmpPDS |((TmpPEN & ~(1<<ProtectionLevel(TmpPEN)))<< 16);
  }
  else
  {
    //  Test wether the Protection is already Enabled
    if ((FLASHPR->NVAPR0 & 0x2)== 0x0) FLASHPR->NVAPR1 = ResetBit(FLASHPR->NVAPR1, ProtectionLevel(TmpPDS));
    //  FLASHPR->NVAPR1 =(TmpPEN << 16)|(TmpPDS &~(1 << ProtectionLevel(TmpPDS)));
  }
}

/*******************************************************************************
* Function Name  : FLASH_FlagStatus
* Description    : Returns the NewState of Flash flags
* Input 1        : Flash Flag
* Return         : flagstate
*******************************************************************************/
FlagStatus FLASH_FlagStatus(flashflags Xflag)
{
  FlagStatus TmpResult;
  u8 TmpReg, TmpPos;
  // get the Register Index
  TmpReg = (Xflag & FLASH_Reg_Mask) >> 5;
  // get the Flag Index
  TmpPos = (Xflag & FLASH_Flag_Mask);

  switch(TmpReg)
  {
    case 0 : // CR0
    {
      // Returns the status of the CR0[TmpPos] flag
      TmpResult = (FLASHR->CR0 & (1<<TmpPos))==0 ? RESET : SET;
      break;
    }
    case 1 : // CR1
    {
      // Returns the status of the CR1[TmpPos] flag
      TmpResult = (FLASHR->CR1 & (1<<TmpPos))==0 ? RESET : SET;
      break;
    }
    case 5 : // ER
    {
      // Returns the status of the ER[TmpPos] flag
      TmpResult = (FLASHR->ER  & (1<<TmpPos))==0 ? RESET : SET;
      break;
    }
  }
  return(TmpResult);
}

/*******************************************************************************
* Function Name  : FLASH_FlagClear
* Description    : Clears a flash flag
* Input 1        : Flash Flag
* Return         : None
*******************************************************************************/
void FLASH_FlagClear(flashflags Xflag)
{
  u8 TmpReg, TmpPos;
  TmpReg = (Xflag & FLASH_Reg_Mask) >> 5;
  TmpPos = (Xflag & FLASH_Flag_Mask);

  switch(TmpReg)
  {
    case 0 : // CR0
    {
      // Clears the status of the CR0[TmpPos] flag
      FLASHR->CR0 &= ~(1<<TmpPos);
      break;
    }
    case 1 : // CR1
    {
      // Clears the status of the CR1[TmpPos] flag
      FLASHR->CR1 &= ~(1<<TmpPos);
      break;
    }
    case 5 : // ER
    {
      // Clears the status of the ER[TmpPos] flag
      FLASHR->ER &= ~(1<<TmpPos);
      break;
    }
  }
}

/*******************************************************************************
* Function Name  : ProtectionLevel
* Description    : Returns the Index of the Last bit used to Enable / Disable the
*                  Permanent protection.
* Input 1        : None
* Return         : None
*******************************************************************************/
u16 ProtectionLevel(u16 ProtectionRegs)
{
  u16 TmpBitIndex = 0;
  while ((ProtectionRegs & 0x1) == 0 && TmpBitIndex < 16)
  {
    ProtectionRegs>>=1;
    TmpBitIndex++;
  }
  return TmpBitIndex;
}

/*******************************************************************************
* Function Name  : Wait For Last Task
* Description    : Waits for the end of last task on a Flash Bank
* Input 1        : Bank number.
* Return         : The value passed in parameter with the bit (Bitindex) reset
*******************************************************************************/
void WaitForLastTask(flashbanks Xbank)
{
  if (Xbank == FLASH_BANK0) while (FLASH_FlagStatus(FLASH_BSY0) == SET);
  else while (FLASH_FlagStatus(FLASH_BSY1) == SET);
}

/*******************(C)COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

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