/**************************************************************************************** ** 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 ***/
bool DoFlashUnlock(CMD_TBL *cptr, int argc, char **argv) { // error. if (argc != 1){ printf(cptr->usage); return false; } // Unlock of whole flash blocks. if (!FlashUnlock()) return false; return true; } // DoFlashUnlock.
/**************************************************************************************** ** 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 ***/