Hello Evreyone
I am continuously encountering Hard Fault on MKV42F64VLF16 MCU when connecting easing flash page
result = FLASH_Erase(&s_flashDriver, destAdrss, pflashSectorSize, kFTFx_ApiEraseKey);
result = FLASH_Erase(&s_flashDriver, destAdrss, pflashSectorSize, kFTFx_ApiEraseKey);
any ideas about the reason? or a solution?
Many Thanks for the cooperation
Hello
Hey amerzater,
A couple of things - could you try a new project with "flash" selected in the "drivers" selection? A number of people (including me) have seen problems when you copy driver files into a project rather than build the project with them and have MCUXpresso configure them.
When I look at your code, "desAdrss" will not be "right at the beginning of the last page", it looks like it will be at the last sector in Flash.
I asked about the "swap" function as it uses the last block in Flash - the "swap" function allows the two blocks to switch ("swap") start addresses in the memory map so that an application's firmware can be updated in the field (without any programming equipment). The function uses the last sector in each block for saving boot/configuration information - It looks like you are erasing the last sector in Flash, which is the last sector in Block 1. I had some issues with erasing/writing to those two blocks in the past and now I just avoid them all together.
Could you try a new project (with the "flash" driver specified) and then copy in your application code to see what happens? If there is still a problem, could I suggest that you set "destAdrss" to the start of Block 1 using the code:
uint32_t blockSize;
FLASH_GetProperty(&s_flashDriver
, kFLASH_PropertyPflash0BlockSize
, &blockSize);
destAdrss = blockSize;
Good luck,
myke
Hello Mr. mykepredkomimetics
I found that disabling the interrupts (DisableGlobalIRQ() ) before starting the flash write and re-enabling the interrupts after flash write process is finished , solves the problem and no hard fault exception is being thrown..
any idea about the relationship between flash write functions and the interrupt handling procedures.
because finally I need to be able to store my parameters on the flash while the mcu interrupts are fucntioning..
the active interrupts are:
UART0
UART1
FTM0
FTM1
FTM2
LPTMR
Hi amerzater,
That's great news! I'm happy you figured it out and shared it with everybody.
I'm honestly annoyed with myself that I didn't suggest that you halt interrupts before erase/writes - I know I ws told to ALWAYS DISABLE INTERRUPTS FOR WRITES years ago to always do it but the example code in the SDKs (ie "pflash") don't disable interrrupts so it didn't ding on me when I was looking at the code.
Here is my Erase/Write method and you'll notice that I disable interrupts before each write:
uint32_t flashSectorEraseWrite(uint32_t sector
, uint32_t* source) {
uint32_t returnValue = FLASH_RESPONSE_NOERROR;
status_t status;
__disable_irq();if (kStatus_FLASH_Success != (status = FLASH_Erase(&s_flashDriver
, sector
, FSL_FEATURE_FLASH_PFLASH_BLOCK_SECTOR_SIZE
// , kFLASH_ApiEraseKey))) {
, kFTFx_ApiEraseKey))) {
#if (2 != SDK_DEBUGCONSOLE)
PRINTF("\nFLASH_Erase returned %i"
, status);
#endif
returnValue = FLASH_RESPONSE_WRITEFAILURE;
}
__enable_irq();
if (FLASH_RESPONSE_NOERROR == returnValue) {
__disable_irq();
if (kStatus_FLASH_Success != (status = FLASH_Program(&s_flashDriver
, sector
, (uint8_t*)source
, FSL_FEATURE_FLASH_PFLASH_BLOCK_SECTOR_SIZE))) {
#if (2 != SDK_DEBUGCONSOLE)
PRINTF("\nFLASH_Program returned %i"
, status);
#endif
returnValue = FLASH_RESPONSE_WRITEFAILURE;
}
__enable_irq();
}return returnValue;
}
Apologies for not suggesting to disable interrupts - I should have looked at my own code.
Great job figuring out the issue,
myke
Good Day Mr. mykepredkomimetics
Many Thanks for your help
here is my FlashDriver Initializing Code
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. */
result = FLASH_Init(&s_flashDriver);
if (kStatus_FTFx_Success != result)
{
retRes=-1;
}
/* Setup flash cache driver structure for device and initialize variables. */
result = FTFx_CACHE_Init(&s_cacheDriver);
if (kStatus_FTFx_Success != result)
{
retRes=-2;
}
/* Get flash properties*/
FLASH_GetProperty(&s_flashDriver, kFLASH_PropertyPflash0BlockBaseAddr, &pflashBlockBase);
FLASH_GetProperty(&s_flashDriver, kFLASH_PropertyPflash0TotalSize, &pflashTotalSize);
FLASH_GetProperty(&s_flashDriver, kFLASH_PropertyPflash0SectorSize, &pflashSectorSize);result = FLASH_GetSecurityState(&s_flashDriver, &securityStatus);
if (kStatus_FTFx_Success != result)
{
retRes=-3;
}
if (kFTFx_SecurityStateNotSecure == securityStatus)
{
/* Pre-preparation work about flash Cache/Prefetch/Speculation. */
FTFx_CACHE_ClearCachePrefetchSpeculation(&s_cacheDriver, true);/* Erase a sector from destAdrss. */
#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*/
destAdrss = pflashBlockBase + (pflashTotalSize - (SECTOR_INDEX_FROM_END * pflashSectorSize * 2));
#else
destAdrss = pflashBlockBase + (pflashTotalSize - (SECTOR_INDEX_FROM_END * pflashSectorSize));
#endifresult = FLASH_Erase(&s_flashDriver, destAdrss, pflashSectorSize, kFTFx_ApiEraseKey);
if (kStatus_FTFx_Success != result)
{
retRes=-4;
}/* Verify sector if it's been erased. */
result = FLASH_VerifyErase(&s_flashDriver, destAdrss, pflashSectorSize, kFTFx_MarginValueUser);
if (kStatus_FTFx_Success != result)
{
retRes=-5;
}
/* Post-preparation work about flash Cache/Prefetch/Speculation. */
FTFx_CACHE_ClearCachePrefetchSpeculation(&s_cacheDriver, false);
Hi amerzater,
Looking at your code - it looks like it was taken from the "pflash" example - correct? What board example did you get that from?
I'm asking because I want to make sure that:
a) You're working with the same SDK between the example and your device. I found that if I'm running with SDK 2.7.0 on a Freedom board, I need to use the FLASH_Erase method call:
if (kStatus_FLASH_Success != (status = FLASH_Erase(&s_flashDriver
, sector
, FSL_FEATURE_FLASH_PFLASH_BLOCK_SECTOR_SIZE
, kFTFx_ApiEraseKey))) {
but if it's on my product (SDK 2.3.1), I need to use the FLASH_Erase method call:
if (kStatus_FLASH_Success != (status = FLASH_Erase(&s_flashDriver
, sector
, FSL_FEATURE_FLASH_PFLASH_BLOCK_SECTOR_SIZE
, kFLASH_ApiEraseKey))) {
Not a lot of difference, but one works and one doesn't.
b) You're not having an issue with the board swap keep out flash block (Sorry, I don't know the MKV42 as intimately as I know the K2x series). How are you verifying "destAdrss"? What if you set "destAdrss" to the start of the block after the end of your code?
c) You loaded the "flash" driver when you created the project and aren't copying in the flash.c/.h and associated files from anywhere else.
d) When you compile, do you get any errors or warnings? If so, what are they?
myke
Hi,
You can follow the Myke's advice. And I want to know, which line causes this problem. Not function, but may be a control of register or any other.
Have a great day,
TIC
-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!
- We are following threads for 7 days after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------
Hi amerzater,
Before anybody can make a determination could you show your initialization code for "s_flashDriver" and what is the value for "destAdrss"?
The initialization code (for FreeRTOS applications) that I use is:
/* Clean up Flash 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. */
if (kStatus_FLASH_Success != (result = FLASH_Init(&s_flashDriver))) {
#if (2 != SDK_DEBUGCONSOLE)
PRINTF("\nFailed to Initialize Flash File System - Error %i"
, result);
#endif
vTaskSuspend(NULL);
}
if (kStatus_FTFx_Success != (result = FTFx_CACHE_Init(&s_cacheDriver))) {
#if (2 != SDK_DEBUGCONSOLE)
PRINTF("\nFailed to Initialize Flash File System Cache - Error %i"
, result);
#endif
vTaskSuspend(NULL);
}
This is basically taken directly from the example code.
myke