Because of the holidays, TI E2E™ design support forum responses will be delayed from Dec. 25 through Jan. 2. Thank you for your patience.

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 and then Reading from Flash Memory on msp430g2553

Wonder if someone can help me.  I am fairly new to C coding on these chips, but have had a lot of success through trial and error and reading a lot of blogs, forum posts etc.

Most of the code below is borrowed from other peoples articles about erasing/writing and reading from the MSP430 flash memory system.

#include <msp430.h> 


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

// Erase Information Flash

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

void flash_erase(int *addr)
{
  __disable_interrupt();               // Disable interrupts. This is important, otherwise,
                                       // a flash operation in progress while interrupt may
                                       // crash the system.
  while(BUSY & FCTL3);                 // Check if Flash being used
  FCTL2 = FWKEY + FSSEL_1 + FN3;       // Clk = SMCLK/4
  FCTL1 = FWKEY + ERASE;               // Set Erase bit
  FCTL3 = FWKEY;                       // Clear Lock bit
  *addr = 0;                           // Dummy write to erase Flash segment
  while(BUSY & FCTL3);                 // Check if Flash being used
  FCTL1 = FWKEY;                       // Clear WRT bit
  FCTL3 = FWKEY + LOCK;                // Set LOCK bit
  __enable_interrupt();
}


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

// Read stored Information flash

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

void flash_write(int *addr)
{
  __disable_interrupt();
  FCTL2 = FWKEY + FSSEL_1 + FN0;       // Clk = SMCLK/4
  FCTL3 = FWKEY;                       // Clear Lock bit
  FCTL1 = FWKEY + WRT;                 // Set WRT bit for write operation

  *addr++ = 50;         // copy value to flash
  *addr++ = 51;         // copy value to flash

  FCTL1 = FWKEY;                        // Clear WRT bit
  FCTL3 = FWKEY + LOCK;                 // Set LOCK bit
  while(BUSY & FCTL3);
  __enable_interrupt();
}


/*
 * main.c
 */
int main(void) {
    WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer
	
    int *addr = (int *)0x0E000 ; // Address of the flash memory segment starting
    int *addr2 = (int *)0x0E000 ; // Address of the flash memory segment starting

    int i;
    int j;
    flash_erase(addr);
    flash_write(addr);
    i = *addr2;
    j = *addr2;
	return 0;
}

 

The code above is just test code to see if I can write to memory and then read it back afterwards.

If I run this code it does work, using the CCS Memory dump tool I can see the values of 50 and 51 in the right locations of flash memory.  However, if I try to put a breakpoint at either line 63 or 64, it actually won't hit debug until the return 0; statement that follows, and there is nothing in variables I or J.

I am guessing I am doing something stupid - but I really can't tell what it is.

Any idea? (thanks in advance!)

 

 

  • Chris Boar said:
    and there is nothing in variables

    Sure. Because you assign variables but they are not actually used later in the code so compiler optimizations eliminate both i and j assignments as unneeded (trash) code. Either completely switch off compiler optimizations of do something seemingly useful to compiler, with variables after assignment.

  • I had a feeling it was something like this - thanks for your input, I am editing in CCS, would you happen to know how to switch off these compiler optimizations?

  • Chris Boar said:
    I am editing in CCS, would you happen to know how to switch off these compiler optimizatio

    No. Never had to switch optimizations off. Because I don't write trash code. Why don't you simply write few more code lines instead? :DDD

  • Funny, one man's "trash" is another's test code simply to learn by.  I found how to switch off optimizations elsewhere.  Good news for me is that the code to read and write to Flash does work correctly and is now in my trashy production code! :-) 

  • Chris Boar said:
    Funny, one man's "trash" is another's test code simply to learn by.

    Don't get it wrong. I was talking about meaningless code - from perspective of optimizing compiler.

    Chris Boar said:
    I found how to switch off optimizations elsewhere

    Sorry that I did not install compiler you use, read help for you and suggest ready to use solution for you. My bad.

    Anyway you are welcome.

  • That's the problem with written communication, its hard to tell if someone is attempting to be humorous or not.

    So seriously, I am very grateful for your help, as you did not need to reply to my initial post at all, but I didn't expect you to install the compiler, read help for me, or suggest a solution, I merely wondered if you already happened to know how to do it. 

    I think this thread has run its course now.

    Cheers.

  • Your code doesn't use i or j anywhere. The compiler likely has detected it and optimized both variables away. No code was generated for the (pointless) asignment and therefoer the debugger cannot stop at these instrucitons.

    Yes, compilers are that smart. Especially if every byte and instruction cycle counts, as with microcontrollers.

    btw: where do you thing does you main() return to? there is no OS. It simply falls off of the code. What happens after the return depends on the compiler and isn't specified. Worst case, you fetch a return address from stack that was never plkaced there (becaus emain wasn't called form anywhere).

    You should enter LPM or enter an endless loop at the end of main.

  • Jens-Michael Gross said:

    Your code doesn't use i or j anywhere. The compiler likely has detected it and optimized both variables away. No code was generated for the (pointless) asignment and therefoer the debugger cannot stop at these instrucitons.

    Yes, compilers are that smart. Especially if every byte and instruction cycle counts, as with microcontrollers.

    Thanks for your help, this code was never meant for production, just for me to check that my write and read from flash was actually working correctly.  Thanks to some help earlier in this thread, I was able to switch off the optimizations and check the values in the debugger.  All was good.

    Jens-Michael Gross said:

    btw: where do you thing does you main() return to? there is no OS. It simply falls off of the code. What happens after the return depends on the compiler and isn't specified. Worst case, you fetch a return address from stack that was never plkaced there (becaus emain wasn't called form anywhere).

    You should enter LPM or enter an endless loop at the end of main.

    In my production code I do drop into a low power mode, can I assume there is no need to put in an endless loop after dropping into lower mode right?

    Thank you!

  • Chris Boar said:

    In my production code I do drop into a low power mode, can I assume there is no need to put in an endless loop after dropping into lower mode right?

    It is safe until you make mistake in some ISR bringing CPU out of LPM on return. It's not so much extra code, will not hurt much to add "for(;;) {}" loop at the end of the main(). I guess it's 2 bytes of the code.

  • Ilmars said:
    It is safe until you make mistake in some ISR bringing CPU out of LPM on return. It's not so much extra code, will not hurt much to add "for(;;) {}" loop at the end of the main(). I guess it's 2 bytes of the code.

    You can even do

    while(1)
      LPM0;

**Attention** This is a public forum