MKE16 Hardfault With FlashWrite and interrupts

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MKE16 Hardfault With FlashWrite and interrupts

Jump to solution
2,959 Views
amalmathewtech
Contributor I

Hello,

   I have been trying to implement Flash Driver Example(....\boards\frdmke16z\driver_examples\flash) from SDK_2_10_0_FRDM-KE16Z for  MKE16Z64VLD4   Micro controller .

When I checked without any interrupts code, it was working well. When I included  SysTick  interrupt, During FlashWrite Operation  , HardFault occurs . 

By assuming ISR is causing the HardFault  , I disabled the Systick interrupt using the following before FlashWrite

 

SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;

 

Also Enabled Systick interrupt after FlashWrite

 

SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk |            SysTick_CTRL_ENABLE_Msk;

 

 

Still it causes Hardfault.

0 Kudos
1 Solution
2,839 Views
nxf56274
NXP Employee
NXP Employee

I have modified the sample to run the systick. Please refer the attachment. I use '   //*******   '   to tell you where I modify 

 

View solution in original post

10 Replies
2,941 Views
nxf56274
NXP Employee
NXP Employee

Hi

Please try to disable the global interrupt before flash operation.

nxf56274_0-1635143831943.png

 

0 Kudos
2,936 Views
amalmathewtech
Contributor I

I have disabled global interrupt before flash operation. After this Flash Write is happening successfully.

and once Flash Operation is completed I have enabled global interrupts (EnableGlobalIRQ) back. But the ISR (SysTick ISR - In my case) is not running now.

Am I missing something ?

I am adding  a code snippet for your reference.

volatile uint32_t msTicks = 0;
/*-----------------------------------------------------------------------------*
  SysTick_Handler
*-----------------------------------------------------------------------------*/

void SysTick_Handler(void) {
	msTicks++;
}

int main(void)
{
	SystemCoreClockUpdate();
	SysTick_Config(SystemCoreClock/2000);
	NVIC_SetPriority(SysTick_IRQn, 2);
	flash_init();       // Function made from frdmke16z library 
	FlashBuffer[0] = 9; // Test variable to be written
	uint32_t  var = DisableGlobalIRQ();
        flash_write(FlashBuffer,1);  // Function made from  frdmke16z          library 
	EnableGlobalIRQ(var);
	flash_read();
	for(;;){

	}
    return 0;
}



 

0 Kudos
2,930 Views
nxf56274
NXP Employee
NXP Employee

Hi,

Before you write into the flash, is the flash empty? You can try to erase firstly. 

Is your flash operation based on sector operation? Do you sure that you write the data into flash successfully? 

0 Kudos
2,927 Views
amalmathewtech
Contributor I

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;
}

 

0 Kudos
2,920 Views
nxf56274
NXP Employee
NXP Employee

Hi,

The s_buffer size is not a sector. 

nxf56274_0-1635150129205.png

 

0 Kudos
2,917 Views
amalmathewtech
Contributor I

Okay , But why interrupt handler  stopped working, Can you help me on this .

0 Kudos
2,907 Views
nxf56274
NXP Employee
NXP Employee

I am doubting the wrong flash operation causing the problems. After the flash writing, do you check if the the data is written into flash? Although you use the Flash_VerifyProgram, but you don't check the return value.

0 Kudos
2,904 Views
amalmathewtech
Contributor I

I am attaching driver example folder provided on the SDK for MCUXpresso.

On the given sample code all flash related functions are working fine. When Interrupts are included on the code during flash Write operation it was causing HardFault. 

As per your suggestion I have disabled global interrupt before flash write and enabled back once flash write is completed. After this Flash Related Functions are working , But Interrupt handler stopped working.

Any suggestion on this. 

 

 

 

0 Kudos
2,840 Views
nxf56274
NXP Employee
NXP Employee

I have modified the sample to run the systick. Please refer the attachment. I use '   //*******   '   to tell you where I modify 

 

2,830 Views
amalmathewtech
Contributor I

Thank you Very much @nxf56274  , It worked.

0 Kudos