Beispiel #1
0
/************************************************************************************//**
** \brief     Erases the flash memory. Note that this function also checks that no 
**            data is erased outside the flash memory region, so the bootloader can 
**            never be erased.
** \param     addr Start address.
** \param     len  Length in bytes.
** \return    BLT_TRUE if successful, BLT_FALSE otherwise. 
**
****************************************************************************************/
blt_bool FlashErase(blt_addr addr, blt_int32u len)
{
  blt_int8u first_sector;
  blt_int8u last_sector;
  
  /* obtain the first and last sector number */
  first_sector = FlashGetSector(addr);
  last_sector  = FlashGetSector(addr+len-1);
  /* check them */
  if ( (first_sector == FLASH_INVALID_SECTOR) || (last_sector == FLASH_INVALID_SECTOR) )
  {
    return BLT_FALSE;
  }
  /* erase the sectors */
  return FlashEraseSectors(first_sector, last_sector);
} /*** end of FlashErase ***/
Beispiel #2
0
/************************************************************************************//**
** \brief     Writes the data to flash through a flash block manager. Note that this
**            function also checks that no data is programmed outside the flash 
**            memory region, so the bootloader can never be overwritten.
** \param     addr Start address.
** \param     len  Length in bytes.
** \param     data Pointer to the data buffer.
** \return    BLT_TRUE if successful, BLT_FALSE otherwise. 
**
****************************************************************************************/
blt_bool FlashWrite(blt_addr addr, blt_int32u len, blt_int8u *data)
{
  blt_addr base_addr;

  /* make sure the addresses are within the flash device */
  if ( (FlashGetSector(addr) == FLASH_INVALID_SECTOR) || \
       (FlashGetSector(addr+len-1) == FLASH_INVALID_SECTOR) )
  {
    return BLT_FALSE;       
  }

  /* if this is the bootblock, then let the boot block manager handle it */
  base_addr = (addr/FLASH_WRITE_BLOCK_SIZE)*FLASH_WRITE_BLOCK_SIZE;
  if (base_addr == flashLayout[0].sector_start)
  {
    /* let the boot block manager handle it */
    return FlashAddToBlock(&bootBlockInfo, addr, data, len);
  }
  /* let the block manager handle it */
  return FlashAddToBlock(&blockInfo, addr, data, len);
} /*** end of FlashWrite ***/
Beispiel #3
0
/************************************************************************************//**
** \brief     Programs FLASH_WRITE_BLOCK_SIZE bytes to flash from the block->data
**            array.
** \param     block   Pointer to flash block info structure to operate on.
** \return    BLT_TRUE if successful, BLT_FALSE otherwise.
**
****************************************************************************************/
static blt_bool FlashWriteBlock(tFlashBlockInfo *block)
{
  blt_int8u  sector_num;
  blt_bool   result = BLT_TRUE;
  blt_addr   prog_addr;
  blt_int32u prog_data;
  blt_int32u word_cnt;

  /* check that address is actually within flash */
  sector_num = FlashGetSector(block->base_addr);
  if (sector_num == FLASH_INVALID_SECTOR)
  {
    return BLT_FALSE;
  }
  
  /* program all words in the block one by one */
  for (word_cnt=0; word_cnt<(FLASH_WRITE_BLOCK_SIZE/sizeof(blt_int32u)); word_cnt++)
  {
    prog_addr = block->base_addr + (word_cnt * sizeof(blt_int32u));
    prog_data = *(volatile blt_int32u*)(&block->data[word_cnt * sizeof(blt_int32u)]);
    /* keep the watchdog happy */
    CopService();
    /* program the word to flash */
    if (FlashLibProgram((uint32_t *)&prog_data, prog_addr, sizeof(blt_int32u)) != 0)
    {
      result = BLT_FALSE;
      break;
    }
    /* verify that the written data is actually there */
    if (*(volatile blt_int32u*)prog_addr != prog_data)
    {
      result = BLT_FALSE;
      break;
    }
  }
  /* still here so all is okay */
  return result;
} /*** end of FlashWriteBlock ***/
Beispiel #4
0
/****************************************************************************************
** NAME:           FlashWriteBlock
** PARAMETER:      block pointer to flash block info structure to operate on.
** RETURN VALUE:   BLT_TRUE if successful, BLT_FALSE otherwise.
** DESCRIPTION:    Programs FLASH_WRITE_BLOCK_SIZE bytes to flash from the block->data
**                 array. 
**
****************************************************************************************/
static blt_bool FlashWriteBlock(tFlashBlockInfo *block)
{
  blt_int8u  sector_num;
  blt_bool   result = BLT_TRUE;
  blt_addr   prog_addr;
  blt_int32u prog_data;
  blt_int32u word_cnt;

  /* check that address is actually within flash */
  sector_num = FlashGetSector(block->base_addr);
  if (sector_num == FLASH_INVALID_SECTOR)
  {
    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 program bit to indicate that we are about to program data */
  FLASH->CR |= FLASH_PG_BIT;
  /* program all words in the block one by one */
  for (word_cnt=0; word_cnt<(FLASH_WRITE_BLOCK_SIZE/sizeof(blt_int32u)); word_cnt++)
  {
    prog_addr = block->base_addr + (word_cnt * sizeof(blt_int32u));
    prog_data = *(volatile blt_int32u*)(&block->data[word_cnt * sizeof(blt_int32u)]);
    /* program the first half word */
    *(volatile blt_int16u*)prog_addr = (blt_int16u)prog_data;
    /* wait for the program operation to complete */
    while ((FLASH->SR & FLASH_BSY_BIT) == FLASH_BSY_BIT)
    {
      /* keep the watchdog happy */
      CopService();
    }
    /* program the second half word */
    *(volatile blt_int16u*)(prog_addr+2) = (blt_int16u)(prog_data >> 16);
    /* wait for the program operation to complete */
    while ((FLASH->SR & FLASH_BSY_BIT) == FLASH_BSY_BIT)
    {
      /* keep the watchdog happy */
      CopService();
    }
    /* verify that the written data is actually there */
    if (*(volatile blt_int32u*)prog_addr != prog_data)
    {
      result = BLT_FALSE;
      break;
    }
  }
  /* reset the program bit to indicate that we are done programming data */
  FLASH->CR &= ~FLASH_PG_BIT;
  /* lock the flash array */
  FlashLock();
  /* still here so all is okay */
  return result;
} /*** end of FlashWriteBlock ***/