1

I'm working with a MCF51EM256 Freescale microcontroller and I've some problems to store data in the external flash memory to make it persistent.

I need to store this struct:

typedef struct {
  ui64_s Ea_ps; 
  ui64_s Ea_ng;  
  ui64_s Er_q1;
  ui64_s Er_q2;
  ui64_s Er_q3;
  ui64_s Er_q4;
  uint16 F_ea;
  uint16 F_er;
}Ws_EnergyAcc64;

Where:

typedef union{    
  uint64 v;
  uint32 p[2];  
} ui64_s;

and:

typedef unsigned long long int uint64;
typedef unsigned long int uint32;
typedef unsigned short int uint16;

In order to do this, I've implemented this function:

void Save_Flash_WsEnergyAcc(long addr, Ws_EnergyAcc64* Acc) {

  // WsEnergyAcc struct needs 56 bytes in Flash

  uint32 F_ea_32 = (uint32) Acc->F_ea;
  uint32 F_er_32 = (uint32) Acc->F_er;

  Flash_Erase(addr);
  Flash_Erase(addr + 4);
  Flash_Burst(addr, 2, Acc->Ea_ps.p);

  Flash_Erase(addr + 8);
  Flash_Erase(addr + 12);
  Flash_Burst(addr + 8, 2, Acc->Ea_ng.p);

  Flash_Erase(addr + 16);
  Flash_Erase(addr + 20);
  Flash_Burst(addr + 16, 2, Acc->Er_q1.p);

  Flash_Erase(addr + 24);
  Flash_Erase(addr + 28);
  Flash_Burst(addr + 24, 2, Acc->Er_q2.p);

  Flash_Erase(addr + 32);
  Flash_Erase(addr + 36);
  Flash_Burst(addr + 32, 2, Acc->Er_q3.p);

  Flash_Erase(addr + 40);
  Flash_Erase(addr + 44);
  Flash_Burst(addr + 40, 2, Acc->Er_q4.p);

  Flash_Erase(addr + 48);
  Flash_Burst(addr + 48, 2, &F_ea_32);

  Flash_Erase(addr + 52);
  Flash_Burst(addr + 52, 2, &F_er_32);

}

Where "Flash_Burst" and "Flash_Erase":

#define FLASH_MASS_ERASE_CMD  0x41
#define FLASH_ERASE_CMD       0x40
#define FLASH_PROGRAM_CMD     0x20
#define FLASH_BURST_CMD       0x25

/* Macros to call the function using the different features */
#define Flash_Erase(Address) \
      Flash_Cmd((UINT32)Address, (UINT16)1, (UINT32*)CUSTOM_ROM_ADDRESS, FLASH_ERASE_CMD)

#define Flash_Burst(Address, Size, DataPtr) \
      Flash_Cmd((UINT32)Address, (UINT16)Size, (UINT32*)DataPtr, FLASH_BURST_CMD)

UINT8 /*far*/ 
Flash_Cmd(UINT32 FlashAddress, 
      UINT16 FlashDataCounter, 
      UINT32 *pFlashDataPtr, 
      UINT8 FlashCommand)
{
  /* Check to see if FACCERR or PVIOL is set */
  if (FSTAT &0x30)  
  {         
      /* Clear Flags if set*/
      FSTAT = 0x30;  
  }

  if (FlashDataCounter)
  {
    do
    {
        /* Wait for the Last Busrt Command to complete */
        while(!(FSTAT&FSTAT_FCBEF_MASK)){};/*wait until termination*/

        /* Write Data into Flash*/
        (*((volatile unsigned long *)(FlashAddress))) = *pFlashDataPtr;
        FlashAddress += 4;
        pFlashDataPtr++;

        /* Write Command */
        FCMD = FlashCommand;

        /* Put FCBEF at 1 */
        FSTAT = FSTAT_FCBEF_MASK;

        asm (NOP);
        asm (NOP);
        asm (NOP);

         /* Check if Flash Access Error or Protection Violation Error are Set */
        if (FSTAT&0x30)
        {     
          /* If so, finish the function returning 1 to indicate error */
          return (1);
        }

    }while (--FlashDataCounter);
  }
  /* wait for the last command to complete */
  while ((FSTAT&FSTAT_FCCF_MASK)==0){};/*wait until termination*/

  /* Return zero to indicate that the function executed OK */
  return (0);
}

I also defined:

extern unsigned char __CUSTOM_ROM[];
extern unsigned char __CUSTOM_ROM_SIZE[];

#define CUSTOM_ROM_ADDRESS      (unsigned long int)__CUSTOM_ROM
#define CUSTOM_ROM_SIZE         (unsigned long int)__CUSTOM_ROM_SIZE

I don't understand what is CUSTOM_ROM_ADDRESS, and it causes a link error in my project:

C:/Freescale/CW MCU    v10.6.4/MCU/ColdFire_Tools/Command_Line_Tools/mwldmcf|Linker|Error
>Undefined : "__CUSTOM_ROM"

I think it could be the data wich is stored in the erased address, and I tryed to do something like this (instead Flash_Erase(address)):

void EraseFlash(long addr) {

    uint32 eraseData = 0xFFFFFFFF;
    Flash_Cmd((uint32)addr, (uint16)1, (uint32*)&eraseData, 0x40);

}

It works in the first execution of "Save_Flash_WsEnergyAcc", but I cannot explain why it blocks the MCU the next times.

¿Can someone tell me what I'm doing wrong? Thank you all!

9
  • 1
    CUSTOM_ROM (and the associated #defines) doesn't look like anything that is needed by your program - Maybe it is some linker magic that should be described somewhere in your board docs. Also, I have the strong suspicion that you'd need to reset something after the EraseFlash command executed.
    – tofro
    Commented Mar 28, 2016 at 11:04
  • 1
    strongly suggest using: #include <stdint.h> for defining uint32, etc. That will make the code much less error prone, much easier to maintain, and much easier to debug. It will also make the code much more portable. Commented Mar 29, 2016 at 0:55
  • All involved pointers must be declared as volatile.
    – Lundin
    Commented Mar 29, 2016 at 9:34
  • "It works in the first execution of "Save_Flash_WsEnergyAcc", but I cannot explain why it blocks the MCU the next times." This smells as if you are executing the flash programming code from the same flash bank as you are programming. Try to single-step the program, does everything suddenly work? If so, then this is the problem. You must ensure that the flash driver is not located in the bank which is getting programmed.
    – Lundin
    Commented Mar 29, 2016 at 9:36
  • Also, most Freescale microcontroller flash is picky with a correct pre-scaler clock. I don't know about this particular device, but IIRC typically they want a prescaler that gives a clock between 150-200kHz (check the data sheet). If the clock is not within the allowed frequency, you'll get all manner of strange behavior. Getting the correct clock is always the trickiest part when writing these flash drivers. There is a flag you can check to ensure that the clock is fine, you must check that flag!
    – Lundin
    Commented Mar 29, 2016 at 9:40

1 Answer 1

0

The MCF51EM256 Freescale microcontroller only allows erase flash memory by 1024 bytes sectors.

If I call the Erase function each time I want to write the flash memory the MCU will be blocked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.