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.

MSP430F5529: Flash Memory Data is lost when launchpad power is reset

Part Number: MSP430F5529


Hello Everybody:

I have a bit of a problem, I hope somebody can enlighten me.

I already checked previous posts but couldn't find one that could explain what is happening.

I am using a TI demo code to write to the flash memory (MSP430F552x Demo - Single-Byte Flash In-System Programming, Copy SegC to SegD). See included file.

Everything seems to work fine. I look at the flash memory using the Memory Browser tool in CCS, and the contents is there. See image:

Except when I reset the power on the Launchpad and check again, the data is lost and only shows FF in every byte. If I am writing to the FLASH memory, why is the data lost?

Does reseting the power on the Launchpad cause some kind of memory erase when it is replugged? Is something missing in the code?

Or am I interpreting it wrong?

I would really appreciate any comment.

Thanks for your time.

Ralf.

//
//  MSP430F552x Demo - Single-Byte Flash In-System Programming, Copy SegC to SegD
//
//  Description: This program first erases flash seg C, then it increments all
//  values in seg C, then it erases seg D, then copies seg C to seg D. Starting
//  addresses of segments defined in this code: Seg C-0x1880, Seg D-0x1800.
//  ACLK = REFO = 32kHz, MCLK = SMCLK = default DCO 1048576Hz
//   Set Breakpoint on NOP in the Mainloop to avoid Stressing Flash
//
//                MSP430F552x
//            -----------------
//        /|\|              XIN|-
//         | |                 |
//         --|RST          XOUT|-
//           |                 |
//
//   Bhargavi Nisarga
//   Texas Instruments Inc.
//   April 2009
//   Built with CCSv4 and IAR Embedded Workbench Version: 4.21
//  ******************************************************************************

#include <msp430.h>

char value;                                 // 8-bit value to write to seg C

// Function prototypes
void write_SegC(char value);
void copy_C2D(void);

int main(void)
{
  WDTCTL = WDTPW+WDTHOLD;                   // Stop WDT
  value = 0;                                // initialize value

  while(1)
  {
    write_SegC(value++);                    // Write segment C, increment value
    copy_C2D();                             // Copy segment C to D
    __no_operation();                       // Loop forever, SET BREAKPOINT HERE
  }
}

//------------------------------------------------------------------------------
// Input = value, holds value to write to Seg C
//------------------------------------------------------------------------------
void write_SegC(char value)
{
  unsigned int i;
  char * Flash_ptr;                         // Initialize Flash pointer
  Flash_ptr = (char *) 0x1880;
  FCTL3 = FWKEY;                            // Clear Lock bit
  FCTL1 = FWKEY+ERASE;                      // Set Erase bit
  *Flash_ptr = 0;                           // Dummy write to erase Flash seg



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

  for (i = 0; i < 128; i++)
  {
    *Flash_ptr++ = value;                   // Write value to flash
  }

  FCTL1 = FWKEY;                            // Clear WRT bit
  FCTL3 = FWKEY+LOCK;                       // Set LOCK bit
}

//------------------------------------------------------------------------------
// Copy Seg C to Seg D
//------------------------------------------------------------------------------
void copy_C2D(void)
{
  unsigned int i;
  char *Flash_ptrC;
  char *Flash_ptrD;

  Flash_ptrC = (char *) 0x1880;             // Initialize Flash segment C ptr
  Flash_ptrD = (char *) 0x1800;             // Initialize Flash segment D ptr

  __disable_interrupt();                    // 5xx Workaround: Disable global
                                            // interrupt while erasing. Re-Enable
                                            // GIE if needed
  FCTL3 = FWKEY;                            // Clear Lock bit
  FCTL1 = FWKEY+ERASE;                      // Set Erase bit
  *Flash_ptrD = 0;                          // Dummy write to erase Flash seg D
  FCTL1 = FWKEY+WRT;                        // Set WRT bit for write operation

  for (i = 0; i < 128; i++)
  {
    *Flash_ptrD++ = *Flash_ptrC++;          // copy value segment C to seg D
  }

  FCTL1 = FWKEY;                            // Clear WRT bit
  FCTL3 = FWKEY+LOCK;                       // Set LOCK bit
}

  • This program looks a lot like flashwrite_01 (SLAC300G), which doesn't just erase/write the flash, it continually erases/writes the flash.

    Erasing takes about 32ms, writing (128 bytes) takes about (128*0.064)=~8ms (see also SLAS590M sec 5.48). Based on this, hitting the reset button has a high probability of catching it in mid-erase. (Stopping at a breakpoint after the write is done has of course about 0% chance of catching it in mid-erase.)

    Attaching the debugger also may involve one or more MCU resets. Since the erase is the first significant thing this program does, those also have a high probability of reset-ing mid-erase.

    I suggest you alter this program to (1) do the write/copy sequence once, then drop into LPM0 (forever) or an empty while() loop (2) Add a delay -- a hundred milliseconds, maybe, just before the write/copy sequence, to deal with any debugger resets.

    More generally, you probably want to avoid exercising this program (in this form) very much. My arithmetic says it will wear out the flash in as little as 7 minutes of constant running.
  • Hello Bruce:
    Thanks for the note.
    I will try your suggestions and let you know.
    Thanks.
  • Hello Bruce:
    You were right on the money!
    All it needed was time to completely write to the flash.
    I put the delay as you recommended and it worked just great!
    The data is there no matter how many times I unplug/replug the launchpad.
    And yes, I will avoid writing continuously to the Flash.

**Attention** This is a public forum