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.

How to write and read non-volatile variables

Other Parts Discussed in Thread: MSP430F5528, MSP430WARE

Hello to every one,

This is my first post here and I am also new with MSP430. I read many posts regarding this issue that I have but none of them fits with my purposes. I am trying to store a non-volatile variable into an INFO segment of the flash memory of my MSP430F5528 but when i generate a POR by SW the value is erased (I think). I was just trying to learn how to store it by creating a value that is constantly incremented.

I tryed many ways of doing that such as using pointers or using the pragma LOCATION sentences but with no results.

I hope you can help me through this.

Best regards,


  • Do you want to put the data there during runtime or while the program is loaded onto your MCU?

  • I would like to store the value while the program is executed by the microcontroller so when a reboot happens the value canot be erased.

  • Are you writing to information memory segment A? Segment A has to be unlocked in a different way than the other three.

    I had issues with this before where writing to info A did not work.

  • No, I am trying to write on segment C. It is possible that i am not doing it well. I tryed many ways of doing that but i'run out of ideas now. If someone knows how to do it properly I would be very grateful.

  • Daniel:

    There is an example of flash writing in the MSP430ware code examples for 5xx_6xx - called MSP430F55xx_flashwrite_01.c - It could be a good starting point for what you are trying to do.

    Hopefully, that helps... 

  • I have alredy seen it, and it helped me in order to understand how to work with flash but it is not what i am looking for. In fact my purpose is to save a value into flash memory so when i generate a reset i still keeping that value.

    Thank you for replying.

  • Just to be clear, this is how you are writing to flash?

    char *Flash_ptrD = (char *) 0x1800;


    FCTL3 = FWKEY;
    *Flash_ptrD = 0;
    while(FCTL3 & BUSY);


    *Flash_ptrD = (unsigned char)value; // <- The value you are writing

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



    And this is how you are reading it back?

    char *Flash_ptrD = (char *) 0x1800;

    unsigned char value = *Flash_ptrD;

  • I did something very similar to that but probably i was doing something wrong because i've just copied your code and now works fine. Thank you very much!!!

    Just to know it, is there any difference between writing into the flas with a pointer and using the sentence #pragma LOCATION( x , address );?

  • #pragma location tells the compiler and linker that a certain symbols is at a fixed address. The compiler may generate more efficient code if the final location is known, and the linker will place the object where given, no matter what kind of memory this location may be. Basically, you’ll get a pointer to this object that isn’t stored in a pointer variable (for indirect access) but rather hardcoded and directly used in the instructions.

    #pragma (x,0x1000)
    unsigned int x;

    Will place ‘x’ at address 0x1000, and you can use it as a normal variable. e.g. x=1 etc. (this means “write 1 to the location of x, which is 0x1000”)

    unsigned int * y = 0x1000;
    will give you a pointer ‚y‘ that points to a memory location of a nameless int . You access the value with *y=1 (which means: “write 1 to the location whose address is stored in y”). y is located somewhere else and holds the value 0x1000.

    The first one is faster, the second one is more flexible, as you can change the value stored in y and therefore the memory location it points to and which you access with *y.

**Attention** This is a public forum