AnsweredAssumed Answered

In Application Flash Programming Error

Question asked by Giovinetti Roberto on Jul 1, 2016
Latest reply on Jul 5, 2016 by Bob Paddock


Hello Everyone

I've a problem: I have to store periodically some parameters in the Non Volatile Memory. I use MKE02Z32VLC2 MCU. In previous revision of my application software I've used EEPROM space to store data, and all worked fine. Now I need to use free flash space because of the speed needed to store parameters: I need to create two separated copies of parameters so if user switch off the device while a write sequence is in progress, corrupted data will affect only one of the copy, and software can use the second one. In EEPROM this is not possible because I can only erase block (256 Bytes) or sector (2 bytes). But if I delete sector by sector in EEPROM the time needed is too much for the application.

If I use EEPROM commands I've no problem, the algorithm works fine.

The problem is when I use FLASH commands, such as ERASE SECTOR. I'm sure to use a sector that is not used by application. I've disabled interrupts, but there's still a problem, and the core goes in the lockup state and MCU resets.

I've tried to insert some delay but the problem is still present. I can't understand where's the matter.

Can anyone help me?

Thank you a lot!

 

Here the software I wrote:

...

...

     DisableInterrupts;

     Error = FALSE;

     do {

           Error = Flash_EraseSector(FLASH_DATA_START );

      } while (Error != FLASH_ERR_SUCCESS);

...

...

And here the function Flash_EraseSector:

 

uint16_t Flash_EraseSector(uint16_t FLASH_Address)

{

  uint16_t Error = FLASH_ERR_SUCCESS;

 

  // Check address to see if it is aligned to 4 bytes

  // Global address [1:0] must be 00.

  if (FLASH_Address & 0x03)

       {

            Error = FLASH_ERR_INVALID_PARAM;

            return (Error);

       }

  FTMRH_FSTAT = 0x30; // Clear error flags

 

  FTMRH_FCCOBIX = 0x0;   // Write index to specify the command code to be loaded ed first part of Global address

  FTMRH_FCCOBHI = FLASH_CMD_ERASE_SECTOR; // FLASH command 0x0A

  FTMRH_FCCOBLO = 0x0; // Global Address[23:16] = 0x0

 

  FTMRH_FCCOBIX = 0x1; // Write index to specify the Global Address

  FTMRH_FCCOBHI = (uint8_t) (FLASH_Address >> 8); // Global Address[15:8]

  FTMRH_FCCOBLO = (uint8_t) (FLASH_Address); // Global Address[7:0]

 

  FTMRH_FSTAT = 0x80; // Launch the command

 

  while (!(FTMRH_FSTAT & FTMRH_FSTAT_CCIF_MASK)){ }; // Wait till command is completed

 

 

  if(FTMRH_FSTAT & FTMRH_FSTAT_ACCERR_MASK) // Check error status

  {

  Error |= FLASH_ERR_ACCESS;

  }

  if(FTMRH_FSTAT & FTMRH_FSTAT_FPVIOL_MASK)

  {

  Error |= FLASH_ERR_PROTECTION;

  }

  if(FTMRH_FSTAT & FTMRH_FSTAT_MGSTAT0_MASK)

  {

  Error |= FLASH_ERR_MGSTAT0;

  }

  if(FTMRH_FSTAT & FTMRH_FSTAT_MGSTAT1_MASK)

  {

  Error |= FLASH_ERR_MGSTAT1;

  }

  return (Error);

}

 

 

Roberto

Outcomes