This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Writing array of data to flash address start 0x40000 on the MSPF430F6638.



Dos anybody have any sample code showing how to first erase and then write a large amount of data values to the internal flash on a MSP430F66?

And can do it 10 times in a row?

I have code that can do it 1-3 times, but before 10 tries the firmware “jumps” to something different.

 

Below is my code that works 1-3 times, but never 10 times.

Suddenly the code pointer jumps to something else.

 

 

 

#define LogbookData      0x40000

typedef void (*FARPTR)();

 

//*****************************************************************************

void x_write_short(FARPTR addr, unsigned short data)

{

  __data20_write_short((unsigned long)addr, data);

  __data20_write_short((unsigned long)addr, data);

}

 

//******************************************************************************

void Erase_Flash_Segment(unsigned long Adr)

 

{

   __bic_SR_register(GIE);              // disable Global Interrupt

  FCTL3 = FWKEY;                        // Clear Lock bit

  FCTL1 = FWKEY + ERASE;                // Set Erase bit

  x_write_short((FARPTR)Adr, 0xFFFF);   // Dummy write to erase Flash To_segment

  FCTL3 = FWKEY + LOCK;                 // Reset LOCK bit

  __bis_SR_register(GIE);               // enable Global Interrupt

 

 

//******************************************************************************

void Erase_Logbook_Segments(void)

 

{

  unsigned long Segment;

 

  for (Segment=0;Segment<20;Segment++)

  {

    Erase_Flash_Segment(LogbookData + (Segment*0x200));

  }

}

 

 

 //******************************************************************************

void Flash_Write_Logbook(void)

 

{                    

  unsigned int i;

  unsigned long Flash_Pointer;

 

  Erase_Logbook_Segments();

  Flash_Pointer =     LogbookData;                    // Initialize Logbook Flash pointer 

  FCTL3 = FWKEY;                                                     // Clear Lock bit

  FCTL1 = FWKEY + WRT;                                       // Set WRT bit for write operation

  Logbook_Ptr   = &Logbook.Device;               // Pointer to first byte in logbook struct  

  x_write_short((FARPTR)Flash_Pointer, 0xFFFF); // DUMMY write

  while (FCTL3 & BUSY);                                         // wait until BUSY=0

  FCTL1 = FWKEY + WRT;                                       // set write (byte) mode

  for (i=0;i<sizeof(Logbook)/sizeof(int);i++)

  {

     x_write_short((FARPTR)Flash_Pointer, Logbook_Ptr[i]);

     Flash_Pointer += 2;      

     while (FCTL3 & BUSY);                                      // wait until BUSY=0

  }

  FCTL1 = FWKEY;                                // Clear WRT bit

  FCTL3 = FWKEY + LOCK;                         // Reset LOCK bit

  while ( ! (FCTL3 & WAIT) );

}

 

//******************************************************************************

void Test_Flash_Logbook(void)

 

{

  Stop_Watchdog_Timer();

  for (i=1;i<10;i++)

  { 

    Flash_Write_Logbook();

  } 

}

  • Hello Steffen,

    You will need to provide some more detail as to the problems you are experiencing. On the fourth try, does it jump elsewhere at the beginning, end, or halfway through the write loop? Where is this "someplace different" inside of your code and is it the same place every time? Have you looked inside of the memory browser in the debugger to ensure that your Logbook data is being placed exactly where you expect inside of the memory? I don't know how large your Logbook is, is it possible that you exceed the allocated bank 3 space (ending at 0x047FFF)? The debugger is a valuable tool for problems such as this.

    Regards,
    Ryan
  • Hello Ryan,

    I erase 20 segments starting from 0x40000
    So it’s from 0x40000 to 0x44000
    Logbook size is currently 0x1C58

    I did find a solution.
    I think that some of the flash segments where not set to FF before I wrote to them.
    This made the code "jump" to a another location (Same location each time)

    My fix is:

    1)
    I have now include “while(BUSY & FCTL3)” 2 times in each flash erase segment rutine

    // Erase segment
    Flash_ptr = (Offset_prt + (Segment*0x200));
    while(BUSY & FCTL3); // Check if Flash is being used
    FCTL3 = FWKEY; // Clear Lock bit
    FCTL1 = FWKEY+ERASE; // Set Erase bit
    x_write_short((FARPTR)Flash_ptr, 0xFFFF); // Dummy write to erase Flash To_segment
    while(BUSY & FCTL3); // Check if Erase is done

    2)
    I also swapped 2 lines in the Flash writing:

    // Before
    FCTL3 = FWKEY + LOCK; // Set LOCK bit
    while ( ! (FCTL3 & WAIT) );

    // Now
    while(BUSY & FCTL3); // Check for write completion
    FCTL3 = FWKEY+LOCK; // Set LOCK

    I can now erase and rewrite 100 times without any problems.

    Regards Steffen

**Attention** This is a public forum