原创 AT45DB161D 擦除算法

2009-3-29 11:13 3294 6 6 分类: MCU/ 嵌入式

// 1 Chip = 16 Sectors = 512 Blocks = 4096 Pages
//            1 Sector = 32 Blocks  = 256 Pages
//                          1 Block = 8 Pages
//------------------------------------------------------------------------------
// Pm ... Pn [Bm ... Bn] [Sm ... Sn] [Bm ... Bn] Pm ... Pn
//------------------------------------------------------------------------------
// Caller must make dwAddress in Page boundary
// Example : AT45_ErasePages(247*512, 1+1*8+1*256+1*8+1);
//
void AT45_ErasePages(u32 dwAddress, u32 dwPageCount)
{


  if ( dwPageCount == 0)
    return;


  if ( ( dwAddress == 0) && ( dwPageCount >= 4096) )
  {
    AT45_ChipErase();
    return;
  }


  if ( dwPageCount >= 256 )
  {
    if ( (dwAddress & 0x1FFFF) == 0x00000000 )   // At Sector boundary
    {
      while ( dwPageCount >= 256 )
      {
        AT45_SectorErase(dwAddress);
        dwAddress += 256 * 512;
        dwPageCount -= 256;
      }


      BOOT_AT45_ErasePages(dwAddress, dwPageCount);
      return;
    }
  }


  if ( dwPageCount >= 8 )                      // Erase Blocks until Sector boundary
  {
    if ( (dwAddress & 0xFFF) == 0x00000000 )   // At Block boundary
    {
      while ( dwPageCount >= 8 )
      {
        if ( (dwAddress & 0x1FFFF) == 0x00000000 )   // At Sector boundary
        {
          if ( dwPageCount >= 256 )
          {
            BOOT_AT45_ErasePages(dwAddress, dwPageCount);
            return;
          }
        }


        AT45_BlockErase(dwAddress);
        dwAddress += 8 * 512;
        dwPageCount -= 8;
      }


      BOOT_AT45_ErasePages(dwAddress, dwPageCount);
      return;


    }
  }


  while ( dwPageCount > 0 )                    // Erase Pages until Block boundary
  {
    if ( (dwAddress & 0xFFF) == 0x00000000 )   // At Block boundary
    {
      if ( dwPageCount >= 8 )
      {
        BOOT_AT45_ErasePages(dwAddress, dwPageCount);
        return;
      }
    }


    AT45_PageErase(dwAddress);
    dwAddress += 1*512;
    dwPageCount -= 1;
  }


}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
6
关闭 站长推荐上一条 /1 下一条