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.

MSP430 Flash Write

Hello,

I want to test my program(flash read, write, erase) on the MSP430. I will write a value in the flash, e.g. temperature or something else. When there is no power supply this value should be stored until the the power is back. (Perhaps the batterie is down, blackout...) When the power is back the value should be read and i can do something with it.

I hope you can understand me. My problem is, that i want test it, if this works fine but i dont know how i can do this. With CCS5 i think i can only read variables when i stop the debug mode, but when i disconnect the uC, there is an error(sure). Is there any chance to test/simulate a blackout?

Thank you :)

  • Sascha W. said:
    With CCS5 i think i can only read variables

    You can read not only (RAM) variables but flash memory either. So after powerloss event next time you debug your program with CCS5 you shall be able to see flash memory contents to check that your program did write temperature or something else.

  • Hi Sascha,

    You have a couple of ways that you can test if you correctly wrote your value into flash and to observe it surviving a power-cycling event. For testing, write a specific known value into the location in flash instead of the temperature (let's say your wrote 0xA5A5 for example).

    Now, at the top of your code after you disable the watchdog, just add a line of code that reads the value from the flash address you wrote to, and compares it to the known value that should have been written there (0xA5A5). If the value matches, you can set an LED or toggle a pin. Then you can just run your code standalone (without the debugger) and disconnect/reconnect power from your board and you should be able to tell if the value was still there or not by the status of the LED or the pin.

    Another option in CCS is that you can connect to a target without loading any code to it (so your value won't be overwritten - if you just download your code again normally with CCS, depending on the flash area you used and your CCS settings that flash area will be erased so you need to do this instead):

    •  just program and run your code the first time, then power cycle your board like I mentioned above. 
    • Then in CCSv5.3 go to Project > Properties > Auto Run and Launch Options, scroll down to Launch Options and uncheck "Connect to the target on debugger startup".
    • Click the bug to launch the debug session.
    • At this point you'll see that the MSP430 debug session, but you'll notice that the MSP430 is not connected yet.
    • Go to Run > Connect Target.
    • Now you should be able to use Memory View to view the flash address that you programmed and verify that the correct value is still there.

    Hope this helps!

    -Katie

     

  • Hi Ilmars, hi Katie

    Thank you for your fast replies!! .."Hope this helps" Yes!.. I will try it immediatly!

    It's a very good idea with the LED. But i will try the other too. When it works fine i tell you about the result.

    With best regards

  • Hello,

    I've tried the possibilities. First i can say, writing the flash is running and reading the flash value is running. then i compare the flash value with .. lets say it is 17.. and the led is on! So i think it is running correctly. But when i will erase the flash value the led is still on. So i think the value of "17" is not erased.

    Here the code i used. Perhaps i have made a mistake.

    #include <msp430G2153.h>
    
    int i,b,c;
    
    
    //Write to Flash function
    void writeFlash(int Value){
    
            int *Flash_ptr;                               //Flash Pointer
    
            Flash_ptr = (int *)0x1040;                      //Flash Pointer adress
            FCTL3 = FWKEY;                                  //Flash clear
            FCTL1 = FWKEY + WRT;                          //Flash write bit
            *Flash_ptr = Value;                             //Flash write
            FCTL1 = FWKEY;                                  //Flash clear
            FCTL3 = FWKEY + LOCK;                           //Flash lock
    }
    
    //Read from Flash function
    int readFlash(){
    
            int readValue;                                  //Variable
            int *Flash_ptr;                                 //Flash Pointer
    
            Flash_ptr = (int *)0x1040;                      //Flash Pointer adress
            readValue = *Flash_ptr;                         //Flash Pointer to Variable
    
            return(readValue);
    }
    
    //Erase Flash function
    void eraseFlash(){
    
            int *Flash_ptr;                         //Flash Pointer
    
            Flash_ptr = (int *)0x1040;                      //Flash Pointer adress
            FCTL1 = FWKEY + ERASE;                          //Flash erase bit
            FCTL3 = FWKEY;                                  //Flash clear
            *Flash_ptr = 0;                                 //Flash Pointer Dummy write
            FCTL1 = FWKEY;                                  //Flash clear
            FCTL3 = FWKEY + LOCK;                           //Flash lock
    }
    
    void main(void){
    
            WDTCTL = WDTPW + WDTHOLD;
    
            BCSCTL1 = CALBC1_16MHZ;
            DCOCTL = CALDCO_16MHZ;
    
            P1DIR |= BIT5;
            P1SEL = 0x00;
            P1OUT = 0x00;
    
            while(1){
    
                    if(i==17)
                    	P1OUT |= BIT5;
    
                    if(i!=17)
                    	P1OUT &=~ BIT5;
    
            		if(P1IN & BIT1)
            			eraseFlash();
    
                    if(P1IN & BIT2)
                    	i = readFlash();
    
                    if(P1IN & BIT3)
                    	writeFlash(17);
            }
    }

    *Edit the code, first was an older one.

  • Hi Sascha.

    • At first glance, I can see that your eraseFlash() function has a little typo: FCTL1 = FWKEY instead of FCTL1 = FEKWY.
    • Your example given doesn't use the eraseFlash() function, so you'll always have a value of 17.
    • You don't configure the flash timing generator. Have you checked that the flash timing must be in the range from approximately 257 kHz to approximately 476 kHz. If the frequency deviates from the specification during the write or erase operation, the result of the write or erase may be unpredictable, or the flash memory may be stressed above the limits of reliable operation.

    Best regards,
    Christian

**Attention** This is a public forum