Refs #821. Refactored flash driver template.

git-svn-id: https://svn.code.sf.net/p/openblt/code/trunk@696 5dc33758-31d5-4daf-9ae8-b24bf3d40d73
This commit is contained in:
Frank Voorburg 2019-06-12 10:43:38 +00:00
parent a6cc436df6
commit 542f59c068
2 changed files with 272 additions and 226 deletions

View File

@ -30,8 +30,8 @@
* \defgroup Target__template_can CAN driver of a port * \defgroup Target__template_can CAN driver of a port
* \brief This module implements the CAN driver of a microcontroller port. * \brief This module implements the CAN driver of a microcontroller port.
* \details For the most parts, this driver is already implemented. The only parts that * \details For the most parts, this driver is already implemented. The only parts that
* need porting are the UART initialization, byte reception and byte * need porting are the CAN initialization, CAN message reception and CAN
* transmission. * message transmission.
* \ingroup Target__template * \ingroup Target__template
****************************************************************************************/ ****************************************************************************************/

View File

@ -59,8 +59,8 @@
/**************************************************************************************** /****************************************************************************************
* Macro definitions * Macro definitions
****************************************************************************************/ ****************************************************************************************/
/** \brief Value for an invalid flash sector. */ /** \brief Value for an invalid sector entry index into flashLayout[]. */
#define FLASH_INVALID_SECTOR (0xff) #define FLASH_INVALID_SECTOR_IDX (0xff)
/** \brief Value for an invalid flash address. */ /** \brief Value for an invalid flash address. */
#define FLASH_INVALID_ADDRESS (0xffffffff) #define FLASH_INVALID_ADDRESS (0xffffffff)
/** \brief Standard size of a flash block for writing. */ /** \brief Standard size of a flash block for writing. */
@ -150,10 +150,9 @@ static tFlashBlockInfo *FlashSwitchBlock(tFlashBlockInfo *block, blt_addr base_a
static blt_bool FlashAddToBlock(tFlashBlockInfo *block, blt_addr address, static blt_bool FlashAddToBlock(tFlashBlockInfo *block, blt_addr address,
blt_int8u *data, blt_int32u len); blt_int8u *data, blt_int32u len);
static blt_bool FlashWriteBlock(tFlashBlockInfo *block); static blt_bool FlashWriteBlock(tFlashBlockInfo *block);
static blt_bool FlashEraseSectors(blt_int8u first_sector, blt_int8u last_sector); static blt_bool FlashEraseSectors(blt_int8u first_sector_idx,
static blt_int8u FlashGetSector(blt_addr address); blt_int8u last_sector_idx);
static blt_addr FlashGetSectorBaseAddr(blt_int8u sector); static blt_int8u FlashGetSectorIdx(blt_addr address);
static blt_addr FlashGetSectorSize(blt_int8u sector);
/**************************************************************************************** /****************************************************************************************
@ -287,30 +286,45 @@ void FlashReinit(void)
****************************************************************************************/ ****************************************************************************************/
blt_bool FlashWrite(blt_addr addr, blt_int32u len, blt_int8u *data) blt_bool FlashWrite(blt_addr addr, blt_int32u len, blt_int8u *data)
{ {
blt_bool result = BLT_TRUE;
blt_addr base_addr; blt_addr base_addr;
/* validate the len parameter */ /* validate the len parameter */
if ((len - 1) > (FLASH_END_ADDRESS - addr)) if ((len - 1) > (FLASH_END_ADDRESS - addr))
{ {
return BLT_FALSE; result = BLT_FALSE;
} }
/* make sure the addresses are within the flash device */ /* only continue if all is okay so far */
if ((FlashGetSector(addr) == FLASH_INVALID_SECTOR) || \ if (result == BLT_TRUE)
(FlashGetSector(addr+len-1) == FLASH_INVALID_SECTOR))
{ {
return BLT_FALSE; /* make sure the addresses are within the flash device */
if ((FlashGetSectorIdx(addr) == FLASH_INVALID_SECTOR_IDX) || \
(FlashGetSectorIdx(addr+len-1) == FLASH_INVALID_SECTOR_IDX))
{
result = BLT_FALSE;
}
} }
/* only continue if all is okay so far */
if (result == BLT_TRUE)
{
/* if this is the bootblock, then let the boot block manager handle it */ /* if this is the bootblock, then let the boot block manager handle it */
base_addr = (addr/FLASH_WRITE_BLOCK_SIZE)*FLASH_WRITE_BLOCK_SIZE; base_addr = (addr/FLASH_WRITE_BLOCK_SIZE)*FLASH_WRITE_BLOCK_SIZE;
if (base_addr == flashLayout[0].sector_start) if (base_addr == flashLayout[0].sector_start)
{ {
/* let the boot block manager handle it */ /* let the boot block manager handle it */
return FlashAddToBlock(&bootBlockInfo, addr, data, len); result = FlashAddToBlock(&bootBlockInfo, addr, data, len);
} }
else
{
/* let the block manager handle it */ /* let the block manager handle it */
return FlashAddToBlock(&blockInfo, addr, data, len); result = FlashAddToBlock(&blockInfo, addr, data, len);
}
}
/* give the result back to the caller */
return result;
} /*** end of FlashWrite ***/ } /*** end of FlashWrite ***/
@ -325,25 +339,39 @@ blt_bool FlashWrite(blt_addr addr, blt_int32u len, blt_int8u *data)
****************************************************************************************/ ****************************************************************************************/
blt_bool FlashErase(blt_addr addr, blt_int32u len) blt_bool FlashErase(blt_addr addr, blt_int32u len)
{ {
blt_int8u first_sector; blt_bool result = BLT_TRUE;
blt_int8u last_sector; blt_int8u first_sector_idx;
blt_int8u last_sector_idx;
/* validate the len parameter */ /* validate the len parameter */
if ((len - 1) > (FLASH_END_ADDRESS - addr)) if ((len - 1) > (FLASH_END_ADDRESS - addr))
{ {
return BLT_FALSE; result = BLT_FALSE;
} }
/* obtain the first and last sector number */ /* only continue if all is okay so far */
first_sector = FlashGetSector(addr); if (result == BLT_TRUE)
last_sector = FlashGetSector(addr+len-1);
/* check them */
if ((first_sector == FLASH_INVALID_SECTOR) || (last_sector == FLASH_INVALID_SECTOR))
{ {
return BLT_FALSE; /* obtain the first and last sector entry indices to the flashLayout[] array. */
first_sector_idx = FlashGetSectorIdx(addr);
last_sector_idx = FlashGetSectorIdx(addr+len-1);
/* check them */
if ((first_sector_idx == FLASH_INVALID_SECTOR_IDX) ||
(last_sector_idx == FLASH_INVALID_SECTOR_IDX))
{
result = BLT_FALSE;
} }
}
/* only continue if all is okay so far */
if (result == BLT_TRUE)
{
/* erase the sectors */ /* erase the sectors */
return FlashEraseSectors(first_sector, last_sector); result = FlashEraseSectors(first_sector_idx, last_sector_idx);
}
/* give the result back to the caller */
return result;
} /*** end of FlashErase ***/ } /*** end of FlashErase ***/
@ -358,6 +386,7 @@ blt_bool FlashErase(blt_addr addr, blt_int32u len)
****************************************************************************************/ ****************************************************************************************/
blt_bool FlashWriteChecksum(void) blt_bool FlashWriteChecksum(void)
{ {
blt_bool result = BLT_TRUE;
blt_int32u signature_checksum = 0; blt_int32u signature_checksum = 0;
/* TODO ##Port Calculate and write the signature checksum such that it appears at the /* TODO ##Port Calculate and write the signature checksum such that it appears at the
@ -380,21 +409,21 @@ blt_bool FlashWriteChecksum(void)
* bootblock is not part of the reprogramming this time and therefore no * bootblock is not part of the reprogramming this time and therefore no
* new checksum needs to be written * new checksum needs to be written
*/ */
if (bootBlockInfo.base_addr == FLASH_INVALID_ADDRESS) if (bootBlockInfo.base_addr != FLASH_INVALID_ADDRESS)
{ {
return BLT_TRUE;
}
#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) #if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0)
/* perform decryption of the bootblock, before calculating the checksum and writing it /* perform decryption of the bootblock, before calculating the checksum and writing it
* to flash memory. * to flash memory.
*/ */
if (FlashCryptoDecryptDataHook(bootBlockInfo.data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) if (FlashCryptoDecryptDataHook(bootBlockInfo.data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE)
{ {
return BLT_FALSE; result = BLT_FALSE;
} }
#endif #endif
/* only continue if all is okay so far */
if (result == BLT_TRUE)
{
/* compute the checksum. note that the user program's vectors are not yet written /* compute the checksum. note that the user program's vectors are not yet written
* to flash but are present in the bootblock data structure at this point. * to flash but are present in the bootblock data structure at this point.
*/ */
@ -409,8 +438,13 @@ blt_bool FlashWriteChecksum(void)
signature_checksum += 1; /* two's complement */ signature_checksum += 1; /* two's complement */
/* write the checksum */ /* write the checksum */
return FlashWrite(flashLayout[0].sector_start+BOOT_FLASH_VECTOR_TABLE_CS_OFFSET, result = FlashWrite(flashLayout[0].sector_start+BOOT_FLASH_VECTOR_TABLE_CS_OFFSET,
sizeof(blt_addr), (blt_int8u *)&signature_checksum); sizeof(blt_addr), (blt_int8u *)&signature_checksum);
}
}
/* give the result back to the caller */
return result;
} /*** end of FlashWriteChecksum ***/ } /*** end of FlashWriteChecksum ***/
@ -422,6 +456,7 @@ blt_bool FlashWriteChecksum(void)
****************************************************************************************/ ****************************************************************************************/
blt_bool FlashVerifyChecksum(void) blt_bool FlashVerifyChecksum(void)
{ {
blt_bool result = BLT_TRUE;
blt_int32u signature_checksum = 0; blt_int32u signature_checksum = 0;
/* TODO ##Port Implement code here that basically does the reverse of /* TODO ##Port Implement code here that basically does the reverse of
@ -447,13 +482,14 @@ blt_bool FlashVerifyChecksum(void)
*/ */
signature_checksum += *((blt_int32u *)(flashLayout[0].sector_start+BOOT_FLASH_VECTOR_TABLE_CS_OFFSET)); signature_checksum += *((blt_int32u *)(flashLayout[0].sector_start+BOOT_FLASH_VECTOR_TABLE_CS_OFFSET));
/* sum should add up to an unsigned 32-bit value of 0 */ /* sum should add up to an unsigned 32-bit value of 0 */
if (signature_checksum == 0) if (signature_checksum != 0)
{ {
/* checksum okay */ /* checksum not okay */
return BLT_TRUE; result = BLT_FALSE;
} }
/* checksum incorrect */
return BLT_FALSE; /* give the result back to the caller */
return result;
} /*** end of FlashVerifyChecksum ***/ } /*** end of FlashVerifyChecksum ***/
@ -465,25 +501,34 @@ blt_bool FlashVerifyChecksum(void)
****************************************************************************************/ ****************************************************************************************/
blt_bool FlashDone(void) blt_bool FlashDone(void)
{ {
blt_bool result = BLT_TRUE;
/* check if there is still data waiting to be programmed in the boot block */ /* check if there is still data waiting to be programmed in the boot block */
if (bootBlockInfo.base_addr != FLASH_INVALID_ADDRESS) if (bootBlockInfo.base_addr != FLASH_INVALID_ADDRESS)
{ {
if (FlashWriteBlock(&bootBlockInfo) == BLT_FALSE) if (FlashWriteBlock(&bootBlockInfo) == BLT_FALSE)
{ {
return BLT_FALSE; /* update the result value to flag the error */
result = BLT_FALSE;
} }
} }
/* only continue if all is okay so far */
if (result == BLT_TRUE)
{
/* check if there is still data waiting to be programmed */ /* check if there is still data waiting to be programmed */
if (blockInfo.base_addr != FLASH_INVALID_ADDRESS) if (blockInfo.base_addr != FLASH_INVALID_ADDRESS)
{ {
if (FlashWriteBlock(&blockInfo) == BLT_FALSE) if (FlashWriteBlock(&blockInfo) == BLT_FALSE)
{ {
return BLT_FALSE; /* update the result value to flag the error */
result = BLT_FALSE;
} }
} }
/* still here so all is okay */ }
return BLT_TRUE;
/* give the result back to the caller */
return result;
} /*** end of FlashDone ***/ } /*** end of FlashDone ***/
@ -495,7 +540,12 @@ blt_bool FlashDone(void)
****************************************************************************************/ ****************************************************************************************/
blt_addr FlashGetUserProgBaseAddress(void) blt_addr FlashGetUserProgBaseAddress(void)
{ {
return flashLayout[0].sector_start; blt_addr result;
result = flashLayout[0].sector_start;
/* give the result back to the caller */
return result;
} /*** end of FlashGetUserProgBaseAddress ***/ } /*** end of FlashGetUserProgBaseAddress ***/
@ -509,21 +559,29 @@ blt_addr FlashGetUserProgBaseAddress(void)
****************************************************************************************/ ****************************************************************************************/
static blt_bool FlashInitBlock(tFlashBlockInfo *block, blt_addr address) static blt_bool FlashInitBlock(tFlashBlockInfo *block, blt_addr address)
{ {
blt_bool result = BLT_TRUE;
/* check address alignment */ /* check address alignment */
if ((address % FLASH_WRITE_BLOCK_SIZE) != 0) if ((address % FLASH_WRITE_BLOCK_SIZE) != 0)
{ {
return BLT_FALSE; /* update the result value to flag the error */
result = BLT_FALSE;
} }
/* make sure that we are initializing a new block and not the same one */
if (block->base_addr == address) /* only continue if all is okay so far */
if (result == BLT_TRUE)
{
/* make sure that we are initializing a new block and not the same one */
if (block->base_addr != address)
{ {
/* block already initialized, so nothing to do */
return BLT_TRUE;
}
/* set the base address and copies the current data from flash */ /* set the base address and copies the current data from flash */
block->base_addr = address; block->base_addr = address;
CpuMemCopy((blt_addr)block->data, address, FLASH_WRITE_BLOCK_SIZE); CpuMemCopy((blt_addr)block->data, address, FLASH_WRITE_BLOCK_SIZE);
return BLT_TRUE; }
}
/* give the result back to the caller */
return result;
} /*** end of FlashInitBlock ***/ } /*** end of FlashInitBlock ***/
@ -532,12 +590,14 @@ static blt_bool FlashInitBlock(tFlashBlockInfo *block, blt_addr address)
** next. ** next.
** \param block Pointer to flash block info structure to operate on. ** \param block Pointer to flash block info structure to operate on.
** \param base_addr Base address of the next block. ** \param base_addr Base address of the next block.
** \return The pointer of the block info struct that is no being used, or a NULL ** \return The pointer of the block info struct that is now being used, or a NULL
** pointer in case of error. ** pointer in case of error.
** **
****************************************************************************************/ ****************************************************************************************/
static tFlashBlockInfo *FlashSwitchBlock(tFlashBlockInfo *block, blt_addr base_addr) static tFlashBlockInfo *FlashSwitchBlock(tFlashBlockInfo *block, blt_addr base_addr)
{ {
tFlashBlockInfo * result = BLT_NULL;
/* check if a switch needs to be made away from the boot block. in this case the boot /* check if a switch needs to be made away from the boot block. in this case the boot
* block shouldn't be written yet, because this is done at the end of the programming * block shouldn't be written yet, because this is done at the end of the programming
* session by FlashDone(), this is right after the checksum was written. * session by FlashDone(), this is right after the checksum was written.
@ -546,6 +606,7 @@ static tFlashBlockInfo *FlashSwitchBlock(tFlashBlockInfo *block, blt_addr base_a
{ {
/* switch from the boot block to the generic block info structure */ /* switch from the boot block to the generic block info structure */
block = &blockInfo; block = &blockInfo;
result = block;
} }
/* check if a switch back into the bootblock is needed. in this case the generic block /* check if a switch back into the bootblock is needed. in this case the generic block
* doesn't need to be written here yet. * doesn't need to be written here yet.
@ -555,24 +616,31 @@ static tFlashBlockInfo *FlashSwitchBlock(tFlashBlockInfo *block, blt_addr base_a
/* switch from the generic block to the boot block info structure */ /* switch from the generic block to the boot block info structure */
block = &bootBlockInfo; block = &bootBlockInfo;
base_addr = flashLayout[0].sector_start; base_addr = flashLayout[0].sector_start;
result = block;
} }
else else
{ {
/* need to switch to a new block, so program the current one and init the next */ /* need to switch to a new block, so program the current one and init the next */
if (FlashWriteBlock(block) == BLT_FALSE) if (FlashWriteBlock(block) == BLT_FALSE)
{ {
return BLT_NULL; /* invalidate the result value to flag the error */
result = BLT_NULL;
} }
} }
/* initialize tne new block when necessary */ /* only continue if all is okay sofar */
if (result != BLT_NULL)
{
/* initialize the new block when necessary */
if (FlashInitBlock(block, base_addr) == BLT_FALSE) if (FlashInitBlock(block, base_addr) == BLT_FALSE)
{ {
return BLT_NULL; /* invalidate the result value to flag the error */
result = BLT_NULL;
}
} }
/* still here to all is okay */ /* Give the result back to the caller. */
return block; return result;
} /*** end of FlashSwitchBlock ***/ } /*** end of FlashSwitchBlock ***/
@ -591,6 +659,7 @@ static tFlashBlockInfo *FlashSwitchBlock(tFlashBlockInfo *block, blt_addr base_a
static blt_bool FlashAddToBlock(tFlashBlockInfo *block, blt_addr address, static blt_bool FlashAddToBlock(tFlashBlockInfo *block, blt_addr address,
blt_int8u *data, blt_int32u len) blt_int8u *data, blt_int32u len)
{ {
blt_bool result = BLT_TRUE;
blt_addr current_base_addr; blt_addr current_base_addr;
blt_int8u *dst; blt_int8u *dst;
blt_int8u *src; blt_int8u *src;
@ -604,10 +673,13 @@ static blt_bool FlashAddToBlock(tFlashBlockInfo *block, blt_addr address,
/* initialize the blockInfo struct for the current block */ /* initialize the blockInfo struct for the current block */
if (FlashInitBlock(block, current_base_addr) == BLT_FALSE) if (FlashInitBlock(block, current_base_addr) == BLT_FALSE)
{ {
return BLT_FALSE; result = BLT_FALSE;
} }
} }
/* only continue if all is okay so far */
if (result == BLT_TRUE)
{
/* check if the new data fits in the current block */ /* check if the new data fits in the current block */
if (block->base_addr != current_base_addr) if (block->base_addr != current_base_addr)
{ {
@ -615,10 +687,14 @@ static blt_bool FlashAddToBlock(tFlashBlockInfo *block, blt_addr address,
block = FlashSwitchBlock(block, current_base_addr); block = FlashSwitchBlock(block, current_base_addr);
if (block == BLT_NULL) if (block == BLT_NULL)
{ {
return BLT_FALSE; result = BLT_FALSE;
}
} }
} }
/* only continue if all is okay so far */
if (result == BLT_TRUE)
{
/* add the data to the current block, but check for block overflow */ /* add the data to the current block, but check for block overflow */
dst = &(block->data[address - block->base_addr]); dst = &(block->data[address - block->base_addr]);
src = data; src = data;
@ -633,7 +709,9 @@ static blt_bool FlashAddToBlock(tFlashBlockInfo *block, blt_addr address,
block = FlashSwitchBlock(block, current_base_addr+FLASH_WRITE_BLOCK_SIZE); block = FlashSwitchBlock(block, current_base_addr+FLASH_WRITE_BLOCK_SIZE);
if (block == BLT_NULL) if (block == BLT_NULL)
{ {
return BLT_FALSE; /* flag error and stop looping */
result = BLT_FALSE;
break;
} }
/* reset destination pointer */ /* reset destination pointer */
dst = &(block->data[0]); dst = &(block->data[0]);
@ -647,8 +725,10 @@ static blt_bool FlashAddToBlock(tFlashBlockInfo *block, blt_addr address,
len--; len--;
} }
while (len > 0); while (len > 0);
/* still here so all is good */ }
return BLT_TRUE;
/* give the result back to the caller */
return result;
} /*** end of FlashAddToBlock ***/ } /*** end of FlashAddToBlock ***/
@ -661,10 +741,16 @@ static blt_bool FlashAddToBlock(tFlashBlockInfo *block, blt_addr address,
****************************************************************************************/ ****************************************************************************************/
static blt_bool FlashWriteBlock(tFlashBlockInfo *block) static blt_bool FlashWriteBlock(tFlashBlockInfo *block)
{ {
blt_bool result = BLT_TRUE;
blt_addr prog_addr; blt_addr prog_addr;
blt_int32u prog_data; blt_int32u prog_data;
blt_int32u word_cnt; blt_int32u word_cnt;
blt_bool result = BLT_TRUE;
/* check that the address is actually within flash */
if (FlashGetSectorIdx(block->base_addr) == FLASH_INVALID_SECTOR_IDX)
{
result = BLT_FALSE;
}
#if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0) #if (BOOT_FLASH_CRYPTO_HOOKS_ENABLE > 0)
#if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE == 0) #if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE == 0)
@ -677,11 +763,14 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block)
/* perform decryption of the program data before writing it to flash memory. */ /* perform decryption of the program data before writing it to flash memory. */
if (FlashCryptoDecryptDataHook(block->data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE) if (FlashCryptoDecryptDataHook(block->data, FLASH_WRITE_BLOCK_SIZE) == BLT_FALSE)
{ {
return BLT_FALSE; result = BLT_FALSE;
} }
} }
#endif #endif
/* only continue if all is okay so far */
if (result == BLT_TRUE)
{
/* TODO ##Port Program the data contents in 'block' to flash memory here and read the /* TODO ##Port Program the data contents in 'block' to flash memory here and read the
* programmed data values back directory from flash memory to verify that the flash * programmed data values back directory from flash memory to verify that the flash
* program operation was successful. The example implementation assumes that flash * program operation was successful. The example implementation assumes that flash
@ -713,6 +802,7 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block)
/*break;*/ /*break;*/
} }
} }
}
/* Give the result back to the caller. */ /* Give the result back to the caller. */
return result; return result;
@ -720,13 +810,14 @@ static blt_bool FlashWriteBlock(tFlashBlockInfo *block)
/************************************************************************************//** /************************************************************************************//**
** \brief Erases the flash sectors from first_sector up until last_sector. ** \brief Erases the flash sectors from indices first_sector_idx up until
** \param first_sector First flash sector number. ** last_sector_idx into the flashLayout[] array.
** \param last_sector Last flash sector number. ** \param first_sector_idx First flash sector number index into flashLayout[].
** \param last_sector_idx Last flash sector number index into flashLayout[].
** \return BLT_TRUE if successful, BLT_FALSE otherwise. ** \return BLT_TRUE if successful, BLT_FALSE otherwise.
** **
****************************************************************************************/ ****************************************************************************************/
static blt_bool FlashEraseSectors(blt_int8u first_sector, blt_int8u last_sector) static blt_bool FlashEraseSectors(blt_int8u first_sector_idx, blt_int8u last_sector_idx)
{ {
blt_bool result = BLT_TRUE; blt_bool result = BLT_TRUE;
blt_int8u sectorIdx; blt_int8u sectorIdx;
@ -734,27 +825,31 @@ static blt_bool FlashEraseSectors(blt_int8u first_sector, blt_int8u last_sector)
blt_int32u sectorSize; blt_int32u sectorSize;
/* validate the sector numbers */ /* validate the sector numbers */
if (first_sector > last_sector) if (first_sector_idx > last_sector_idx)
{
result = BLT_FALSE;
}
if ((first_sector < flashLayout[0].sector_num) || \
(last_sector > flashLayout[FLASH_TOTAL_SECTORS-1].sector_num))
{ {
result = BLT_FALSE; result = BLT_FALSE;
} }
/* only move forward with the erase operation if all is okay so far */ /* only continue if all is okay so far */
if (result == BLT_TRUE) if (result == BLT_TRUE)
{ {
/* erase all sectors one by one */ if (last_sector_idx > (FLASH_TOTAL_SECTORS-1))
for (sectorIdx=first_sector; sectorIdx<= last_sector; sectorIdx++)
{ {
/* keep the watchdog happy */ result = BLT_FALSE;
}
}
/* only continue if all is okay so far */
if (result == BLT_TRUE)
{
/* erase the sectors one by one */
for (sectorIdx = first_sector_idx; sectorIdx <= last_sector_idx; sectorIdx++)
{
/* service the watchdog */
CopService(); CopService();
/* get information about the sector */ /* get information about the sector */
sectorBaseAddr = FlashGetSectorBaseAddr(sectorIdx); sectorBaseAddr = flashLayout[sectorIdx].sector_start;
sectorSize = FlashGetSectorSize(sectorIdx); sectorSize = flashLayout[sectorIdx].sector_size;
/* validate the sector information */ /* validate the sector information */
if ( (sectorBaseAddr == FLASH_INVALID_ADDRESS) || (sectorSize == 0) ) if ( (sectorBaseAddr == FLASH_INVALID_ADDRESS) || (sectorSize == 0) )
{ {
@ -783,14 +878,15 @@ static blt_bool FlashEraseSectors(blt_int8u first_sector, blt_int8u last_sector)
/************************************************************************************//** /************************************************************************************//**
** \brief Determines the flash sector the address is in. ** \brief Determines the index into the flashLayout[] array of the flash sector that
** the specified address is in.
** \param address Address in the flash sector. ** \param address Address in the flash sector.
** \return Flash sector number or FLASH_INVALID_SECTOR. ** \return Flash sector index in flashLayout[] or FLASH_INVALID_SECTOR_IDX.
** **
****************************************************************************************/ ****************************************************************************************/
static blt_int8u FlashGetSector(blt_addr address) static blt_int8u FlashGetSectorIdx(blt_addr address)
{ {
blt_int8u result = FLASH_INVALID_SECTOR; blt_int8u result = FLASH_INVALID_SECTOR_IDX;
blt_int8u sectorIdx; blt_int8u sectorIdx;
/* search through the sectors to find the right one */ /* search through the sectors to find the right one */
@ -803,65 +899,15 @@ static blt_int8u FlashGetSector(blt_addr address)
(address < (flashLayout[sectorIdx].sector_start + \ (address < (flashLayout[sectorIdx].sector_start + \
flashLayout[sectorIdx].sector_size))) flashLayout[sectorIdx].sector_size)))
{ {
/* found the sector we are looking for so store it */ /* update the result value and stop looping */
result = flashLayout[sectorIdx].sector_num; result = sectorIdx;
/* all done so no need to continue looping */
break; break;
} }
} }
/* give the result back to the caller */ /* give the result back to the caller */
return result; return result;
} /*** end of FlashGetSector ***/ } /*** end of FlashGetSectorIdx ***/
/************************************************************************************//**
** \brief Determines the flash sector base address.
** \param sector Sector to get the base address of.
** \return Flash sector base address or FLASH_INVALID_ADDRESS.
**
****************************************************************************************/
static blt_addr FlashGetSectorBaseAddr(blt_int8u sector)
{
blt_int8u sectorIdx;
/* search through the sectors to find the right one */
for (sectorIdx = 0; sectorIdx < FLASH_TOTAL_SECTORS; sectorIdx++)
{
/* keep the watchdog happy */
CopService();
if (flashLayout[sectorIdx].sector_num == sector)
{
return flashLayout[sectorIdx].sector_start;
}
}
/* still here so no valid sector found */
return FLASH_INVALID_ADDRESS;
} /*** end of FlashGetSectorBaseAddr ***/
/************************************************************************************//**
** \brief Determines the flash sector size.
** \param sector Sector to get the size of.
** \return Flash sector size or 0.
**
****************************************************************************************/
static blt_addr FlashGetSectorSize(blt_int8u sector)
{
blt_int8u sectorIdx;
/* search through the sectors to find the right one */
for (sectorIdx = 0; sectorIdx < FLASH_TOTAL_SECTORS; sectorIdx++)
{
/* keep the watchdog happy */
CopService();
if (flashLayout[sectorIdx].sector_num == sector)
{
return flashLayout[sectorIdx].sector_size;
}
}
/* still here so no valid sector found */
return 0;
} /*** end of FlashGetSectorSize ***/
/*********************************** end of flash.c ************************************/ /*********************************** end of flash.c ************************************/