/* 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.  */
blt_bool FlashWrite(blt_addr addr, blt_int32u len, blt_int8u *data)
/* 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.  */
blt_bool FlashWrite(blt_addr addr, blt_int32u len, blt_int8u *data)
{
  blt_bool result = BLT_TRUE;
  blt_addr base_addr;
  addr = FlashTranslateToNonCachedAddress(addr);
  if ((len - 1) > (FLASH_END_ADDRESS - addr))
  {
    result = BLT_FALSE;
  }
  if (result == BLT_TRUE)
  {
    if ((FlashGetSectorIdx(addr) == FLASH_INVALID_SECTOR_IDX) || \
        (FlashGetSectorIdx(addr+len-1) == FLASH_INVALID_SECTOR_IDX))
    {
      result = BLT_FALSE;
    }
  }
  if (result == BLT_TRUE)
  {
    base_addr = (addr/FLASH_WRITE_BLOCK_SIZE)*FLASH_WRITE_BLOCK_SIZE;
    if (base_addr == flashLayout[0].sector_start)
    {
      result = FlashAddToBlock(&bootBlockInfo, addr, data, len);
    }
    else
    {
      result = FlashAddToBlock(&blockInfo, addr, data, len);
    }
  }
  return result;
}