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 PROBLEM

Other Parts Discussed in Thread: MSP430G2101

Hello

I want to save two variables in segment B in  flash of msp430g2101 .

When in saved variable x1 at address 0x1080 with following funcation call, it get saved.

But when i saved variable x2 at address 0x1084(in same segment B) it get saved but this time variable x1 get deleted.

The function i used is given below 

void write_Seg(unsigned int addr,unsigned int value)
{
unsigned int *Flash_ptr; // Flash pointer
Flash_ptr =(unsigned int*)addr;
FCTL2=FN0+FSSEL0+FWKEY;
FCTL3 = FWKEY; // Clear Lock bi
FCTL1 = FWKEY + ERASE; // Set Erase bit

*Flash_ptr = 0; // Dummy write to erase Flash segment
while(FCTL3 & BUSY);
FCTL1 = FWKEY + WRT; // Set WRT bit for write operation

*Flash_ptr = value; // Write value to flash

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

Can you pls let me know why I am unable to save two variable in same segment B.

Thanks 

Manish 

  • Why do you use that function write_Seg()? Do you know what it dose? You got what you ask for.

  • write_Seg() is for to write a byte in particular segment of flash.

    Is there any other way to write any variable in  flash segment ? pls let me know.

     

    Manish 

  • The clue is in this line,

    *Flash_ptr = 0; // Dummy write to erase Flash segment

    The routine is designed to write a single int value, but each time you write you erase the complete segment.

    To save two variables in the same segment you need do one erase and two writes. 

    If you want to use a single routine to update a single value in a segment without affecting what is currently there , you need to read the complete segment into RAM, erase the segment and then write the values back, with the changes you require.

    Roy

  • Hello Roy

    The statement *Flash_ptr = 0; will only erase particular address where pointer Flash_ptr points to .

    How this is erasing complete segment ?

    Can you pls explain more ?

     

    Thanks 

    Manish 

  • Hi,

    The following is from MSP430x2xxx Family user guide Chapter 7

    7.3.2 Erasing Flash Memory
    The erased level of a flash memory bit is 1. Each bit can be programmed from 1 to 0 individually but to
    reprogram from 0 to 1 requires an erase cycle. The smallest amount of flash that can be erased is a
    segment. There are three erase modes selected with the ERASE and MERAS bits listed in Table 7-1.
    Table 7-1. Erase Modes
    MERAS ERASE Erase Mode
    0 1 Segment erase
    1 0 Mass erase (all main memory segments)
    LOCKA = 0: Erase main and information flash memory. 1 1
    LOCKA = 1: Erase only main flash memory.
    Any erase is initiated by a dummy write into the address range to be erased. 

    Effectively you can set individual bit from 1 to 0 on a byte by byte basis, but to set any bit in the segment from 0 to 1 (erase), you must do it to the complete segment. 

    Once you erase a segment you can individually program, bytes, words or long word or blocks. You can do this for individual values at any time, provided the location you are programming is blank, but if you need to update an already programmed location you have to erase the sector

    Roy

  • manish bhatt1 said:

    The statement *Flash_ptr = 0; will only erase particular address where pointer Flash_ptr points to .

    How this is erasing complete segment ?

    Can you pls explain more ?

     

    Everything regarding flashing is explained in family datasheet, with open source examples. If something there is not clear enough, ask.

    There is option to erase (set to $FFFF) main flash memory or segment, it's not possible to "only erase particular address".

  • manish bhatt1 said:
    The statement *Flash_ptr = 0; will only erase particular address where ointer Flash_ptr points to .
    How this is erasing complete segment ?


    Writing to a memory address actually only puts a write request on the memory bus. Where the address is decoded and the request is routed to the various modules. What a module does with a write request depends on the module.
    If the address belongs to RAM, it will just accept the value.
    In case of main flash, the flash controller will take it and normally ignore it. But if in erase mode, it strips down the lower 9 bits of the address, and erases the whole segment.
    So whether you write to 0x8000 or 0x81ff, you'll always erase 0x8000 to 0x81ff.
    In write mode, the flash controller will program the given value to the given address. But the flash cells that already contain 0 will refuse to change to 1 in write mode.
    Also, the flash controller may also just collect the write request, in case you wrote just one byte or word and are writing another byte/word to the following flash address next, so it can perhaps just do a single combined 32 bit write. (at least in the 5x family devices)

**Attention** This is a public forum