yes ,Before I write into the flash, I am erasing it firstly.
with Following Function
FLASH_Erase(&s_flashDriver, destAddr, pflashSectorSize, kFTFx_ApiEraseKey);
My flash operation is based on sector operation.
For Your Reference I am adding each of the function definition that I am using
uint32_t value = 0; // Variable to read flash - To Debug
uint32_t FlashBuffer[16];
/*******************************************************************************
* Definitions
******************************************************************************/
#define FSL_SUPPORT_ERASE_SECTOR_NON_BLOCKING 1U
#define BUFFER_LEN 6
/*******************************************************************************
* Prototypes
******************************************************************************/
void error_trap(void);
void flash_init(void);
void flash_write(uint32_t *DataArray, int DataCount);
void flash_read(void);
/*******************************************************************************
* Variables
******************************************************************************/
/*! @brief Flash driver Structure */
static flash_config_t s_flashDriver;
/*! @brief Flash cache driver Structure */
static ftfx_cache_config_t s_cacheDriver;
/*! @brief Buffer for program */
static uint32_t s_buffer[BUFFER_LEN];
/*! @brief Buffer for readback */
static uint32_t s_buffer_rbc[BUFFER_LEN];
/* flash related variables */
ftfx_security_state_t securityStatus = kFTFx_SecurityStateNotSecure; /* Return protection status */
status_t result; /* Return code from each flash driver function */
uint32_t destAddr; /* Address of the target location */
uint32_t i, failAddr, failDat;
uint32_t pflashBlockBase = 0;
uint32_t pflashTotalSize = 0;
uint32_t pflashSectorSize = 0;
void flash_init(void){
/* Clean up Flash, Cache driver Structure*/
memset(&s_flashDriver, 0, sizeof(flash_config_t));
memset(&s_cacheDriver, 0, sizeof(ftfx_cache_config_t));
/* Setup flash driver structure for device and initialize variables. */
FLASH_Init(&s_flashDriver);
/* Setup flash cache driver structure for device and initialize variables. */
FTFx_CACHE_Init(&s_cacheDriver);
/* Get flash properties*/
FLASH_GetProperty(&s_flashDriver, kFLASH_PropertyPflash0BlockBaseAddr, &pflashBlockBase);
FLASH_GetProperty(&s_flashDriver, kFLASH_PropertyPflash0TotalSize, &pflashTotalSize);
FLASH_GetProperty(&s_flashDriver, kFLASH_PropertyPflash0SectorSize, &pflashSectorSize);
/* Check security status. */
FLASH_GetSecurityState(&s_flashDriver, &securityStatus);
}
void flash_write(uint32_t *DataArray, int DataCount){
/*erase before writing into flash***/
FLASH_Erase(&s_flashDriver, destAddr, pflashSectorSize, kFTFx_ApiEraseKey);
/* Test pflash basic opeation only if flash is unsecure. */
if (kFTFx_SecurityStateNotSecure == securityStatus)
{
/* Pre-preparation work about flash Cache/Prefetch/Speculation. */
FTFx_CACHE_ClearCachePrefetchSpeculation(&s_cacheDriver, true);
#ifndef SECTOR_INDEX_FROM_END
#define SECTOR_INDEX_FROM_END 1U
#endif
#ifdef TEST_TARGET_ADDRESS
destAddr = TEST_TARGET_ADDRESS;
#else
/* Erase a sector from destAddr. */
#if defined(FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP) && FSL_FEATURE_FLASH_HAS_PFLASH_BLOCK_SWAP
/* Note: we should make sure that the sector shouldn't be swap indicator sector*/
destAddr = pflashBlockBase + (pflashTotalSize - (SECTOR_INDEX_FROM_END * pflashSectorSize * 2));
#else
destAddr = pflashBlockBase + (pflashTotalSize - (SECTOR_INDEX_FROM_END * pflashSectorSize));
#endif
#endif // TEST_TARGET_ADDRESS
/* Print message for user. */
for (uint32_t i = 0; i < DataCount; i++)
{
s_buffer[i] = DataArray[i];
}
#if defined(FSL_SUPPORT_ERASE_SECTOR_NON_BLOCKING) && FSL_SUPPORT_ERASE_SECTOR_NON_BLOCKING
FLASH_EraseSectorNonBlocking(&s_flashDriver, destAddr, kFTFx_ApiEraseKey);
/* Before programming the flash, check whether the erase sector command is completed,*/
/* and get the flash status. */
FLASH_GetCommandState();
#else
FLASH_Erase(&s_flashDriver, destAddr, pflashSectorSize, kFTFx_ApiEraseKey);
#endif
/* Verify sector if it's been erased. */
FLASH_VerifyErase(&s_flashDriver, destAddr, pflashSectorSize, kFTFx_MarginValueUser);
/* Program user buffer into flash*/
FLASH_Program(&s_flashDriver, destAddr, (uint8_t *)s_buffer, sizeof(s_buffer));
/* Verify programming by Program Check command with user margin levels */
FLASH_VerifyProgram(&s_flashDriver, destAddr, sizeof(s_buffer), (const uint8_t *)s_buffer,
kFTFx_MarginValueUser, &failAddr, &failDat);
/* Post-preparation work about flash Cache/Prefetch/Speculation. */
FTFx_CACHE_ClearCachePrefetchSpeculation(&s_cacheDriver, false);
}
}
void flash_read(void){
/* test code */
/* address starts from 0xFC00*/
uint32_t* fl = (uint32_t*)0xFC00;
value = *fl;
}