Example #1
0
/****************************************************************************************
** NAME:           FlashEraseSectors
** PARAMETER:      first_sector first flash sector number
**                 last_sector  last flash sector number
** RETURN VALUE:   BLT_TRUE if successful, BLT_FALSE otherwise.
** DESCRIPTION:    Erases the flash sectors from first_sector up until last_sector
**
****************************************************************************************/
static blt_bool FlashEraseSectors(blt_int8u first_sector, blt_int8u last_sector)
{
  blt_int16u nr_of_blocks;
  blt_int16u block_cnt;
  blt_addr   start_addr;
  blt_addr   end_addr;

  /* validate the sector numbers */
  if (first_sector > last_sector)
  {
    return BLT_FALSE;
  }
  if ( (first_sector < flashLayout[0].sector_num) || \
       (last_sector > flashLayout[FLASH_TOTAL_SECTORS-1].sector_num) )
  {
    return BLT_FALSE;
  }
  /* unlock the flash array */
  FlashUnlock();
  /* check that the flash peripheral is not busy */
  if ((FLASH->SR & FLASH_BSY_BIT) == FLASH_BSY_BIT)
  {
    /* lock the flash array again */
    FlashLock();
    /* could not perform erase operation */
    return BLT_FALSE;
  }
  /* set the page erase bit to indicate that we are about to erase a block */
  FLASH->CR |= FLASH_PER_BIT;

  /* determine how many blocks need to be erased */
  start_addr = FlashGetSectorBaseAddr(first_sector);
  end_addr = FlashGetSectorBaseAddr(last_sector) + FlashGetSectorSize(last_sector) - 1;
  nr_of_blocks = (end_addr - start_addr + 1) / FLASH_ERASE_BLOCK_SIZE;
  
  /* erase all blocks one by one */
  for (block_cnt=0; block_cnt<nr_of_blocks; block_cnt++)
  {
    /* store an address of the block that is to be erased to select the block */
    FLASH->AR = start_addr + (block_cnt * FLASH_ERASE_BLOCK_SIZE);
    /* start the block erase operation */
    FLASH->CR |= FLASH_STRT_BIT;
    /* wait for the erase operation to complete */
    while ((FLASH->SR & FLASH_BSY_BIT) == FLASH_BSY_BIT)
    {
      /* keep the watchdog happy */
      CopService();
    }
  }
  /* reset the page erase bit because we're all done erasing */
  FLASH->CR &= ~FLASH_PER_BIT;
  /* lock the flash array */
  FlashLock();
  /* still here so all went okay */
  return BLT_TRUE;
} /*** end of FlashEraseSectors ***/
Example #2
0
File: flash.c Project: x893/OpenBLT
/************************************************************************************//**
** \brief     Erases the flash sectors from first_sector up until last_sector.
** \param     first_sector First flash sector number.
** \param     last_sector  Last flash sector number.
** \return    BLT_TRUE if successful, BLT_FALSE otherwise.
**
****************************************************************************************/
static blt_bool FlashEraseSectors(blt_int8u first_sector, blt_int8u last_sector)
{
  blt_int16u nr_of_blocks;
  blt_int16u block_cnt;
  blt_addr   start_addr;
  blt_addr   end_addr;

  /* validate the sector numbers */
  if (first_sector > last_sector)
  {
    return BLT_FALSE;
  }
  if ( (first_sector < flashLayout[0].sector_num) || \
       (last_sector > flashLayout[FLASH_TOTAL_SECTORS-1].sector_num) )
  {
    return BLT_FALSE;
  }
  
  /* determine how many blocks need to be erased */
  start_addr = FlashGetSectorBaseAddr(first_sector);
  end_addr = FlashGetSectorBaseAddr(last_sector) + FlashGetSectorSize(last_sector) - 1;
  nr_of_blocks = (end_addr - start_addr + 1) / FLASH_ERASE_BLOCK_SIZE;

  /* erase all blocks one by one */
  for (block_cnt=0; block_cnt<nr_of_blocks; block_cnt++)
  {
    /* keep the watchdog happy */
    CopService();
    /* erase the flash page with size FLASH_ERASE_BLOCK_SIZE */
    if (FlashLibErase(start_addr + (block_cnt * FLASH_ERASE_BLOCK_SIZE)) != 0)
    {
      return BLT_FALSE;
    }
  }
  /* still here so all went okay */
  return BLT_TRUE;
} /*** end of FlashEraseSectors ***/