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.

MSP430F1611: Overwriting Flash without Erase

Part Number: MSP430F1611

Hi everyone,

I modified the msp430 Flash example to overwrite the msp430 Flash without Erase. If I understand writing to Flash correctly, this should fail (since you cannot convert 0 to 1 without erase).

But strangely this seems to work. I would like to understand if doing something like this will corrupt the Flash. Here is the modified code from the example.

void write_SegA (char value)
{
  char *Flash_ptr;                          // Flash pointer
  unsigned int i;

  Flash_ptr = (char *) 0xBE00;              // Initialize Flash pointer

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

  for (i=0; i<128; i++)
  {
    *Flash_ptr = i;                         // OVERWRITING WORKS
  }

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

  • shadowfax47 said:
    I modified the msp430 Flash example to overwrite the msp430 Flash without Erase. If I understand writing to Flash correctly, this should fail (since you cannot convert 0 to 1 without erase).

    It will not fail. For each write (without erase), bit(s) "1" will be changed to "0", and that's it. Erase will reset "0" back to "1". By default, factory chip is blank, erased.

    Also, CCS / IAR will do mass (main flash) erase before downloading the code.

  • I have set IAR to fill unused memory to 0xFF. So the initial value is 0xFF. In the for loop, first assignment is ok (since 1s to 0s without erase is Ok).

    But when I change *Flash_ptr from 0 to 1, without erase, should it not fail?
  • As I can see in your code, at 1st iteration (i=0) to 0BE00h will be written word 00000h, and it will stay like this, until the end, because 00000h can be changed to 0FFFFh only after erase. It will not fail, just wrong value at 0BE00h. I guess that this function is executed from flash, not from RAM.

  • Well, when i say it works I mean it does not stay at 0x0000 (as expected). Instead it is changed to 0x007F (at the end of the loop) even without the erase.

  • I guess that this is because compiler will optimize complete loop to *Flash_ptr = 127
  • I doubt it as I have set the compiler optimization level to NONE.
  • Exchange loop with *Flash_ptr = value and call write function few times from different places with different value and everything will be clear.
  • If you meant something like this, It still works

    void  write_SegA (char value);
    
    int main(void)
    {
      WDTCTL = WDTPW + WDTHOLD;
      BCSCTL2 |= DIVM_0;
    
      DCOCTL = DCO2 | DCO0 | MOD4 | MOD2;
      BCSCTL1 = RSEL2 | RSEL1 | RSEL0;
      BCSCTL2 &= ~SELS;
      BCSCTL2 = SELM_0 + DIVS_0; 
      
      FCTL2 = FWKEY + FSSEL0 + FN3 + FN2;
    
      write_SegA(1);
      write_SegA(2); 
      write_SegA(3); 
    }
    
    void write_SegA (char value)
    {
      char *Flash_ptr;
      volatile unsigned int i;
    
      Flash_ptr = (char *) 0xBE00;
      FCTL3 = FWKEY;
      FCTL1 = FWKEY + WRT;
    
      *Flash_ptr = value;
    
      FCTL1 = FWKEY;
      FCTL3 = FWKEY + LOCK;
    }

  • Ok I found the issue :) I was debugging this on a simulator and for some reason it always works. But when I tested it on real hardware, It fails (stops after changing to 0). Thank you for your support.

**Attention** This is a public forum