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.

save data to flash

Other Parts Discussed in Thread: MSP430F5510

Hi,

I'm using the MSP430F5510. i'd like to save some data in the mcu flash instead of using an external eeprom.

I found some examples code that show how to erase and write in flash. What i do not understand it's how to reserve a piece of flash for my data.

I dot not need to store a lot of data, let's say 20 bytes. Therefore i need to reserve a full 512 bytes segment in order to be able to erase the segment independently from the rest of the memory.

How can i reserve the segment for my data and be sure that when i will store my data the segment will be empty?

I know i have to deal with the linker but i do not know how to do it. Is there any application note or document that explain the linker file syntax?

Can you post a linker example code which reserve a segment for data?


Thanks!

Best regads,

Carloalberto

  • For exactly this purpose, the MSP provides an additional flash area, the info memory.

    Depending on the MSP you use, it has 2 or 4 flash segments of 64 to 256 bytes size. Normally, you do not use this memory in your code, so it is left empty. But if you know its address, you can do just write to it as you would do to any main flash sector, except that the segment size is smaller.

    For reading your data, you can even define a struct holding your data, and then use a pointer to your struct and assign it the address of the info sector. Then you can directly access the stored data. To write it, however, you'll need to make a backup of the data, change it, then erase the info sector segment and write the data back.

    You can reserve space in the normal main memory by jsu tdeclaring a static const array. Th elinekr will place it in memory. However, there is no guarantee that it will end up in its own 512 byte segment. The only way to ensure this is to alter th elinker scripts and define yor own data segment by shrinking the ain code segment by 512 bytes and add the segment definitions for your data segment at the gap. Rathe radvanced stuff and nor necessary for a few bytes of data.

  • I don’t know about any compiler (or linker) which auto make code are required to write for flash memory. Each time before writing: 

    a)      it’s possible to write by byte or word

    b)      be sure about the memory is empty by reading values (must be 0xFF…), If not erase full sector

    Before erasing - make backup of stored data at RAM (example for simple wordstructure):
    ----------------------------------------------------------------------------------------------------------------

    main:

    int buf[max_index]];

    for (int I=0; I<max_index]; I++) buf[I]=Your_Flash_Memory_Data[I];

    buf[new_data_index]=new_data;

    flash_clr(Your_Flash_Memory_Data);

    flash_from_buffer(Your_Flash_Memory_Data, buf);

     

    ----------------------------------------------------------------------------------------------------------------

    void* flash_clr( WORD* dest_ptr )

    {

      _DINT(); WDTCTL = WDTPW + WDTHOLD;

      FCTL1 = FWKEY|ERASE;     /* ERASE = 1 */

      FCTL3 = FWKEY;           /* Lock = 0 */

      *dest_ptr=0;             /* erase Flash segment */

      FCTL3^= FXKEY|LOCK;     /* Lock = 1 */

      //WDTCTL = WDT_MDLY_32;

      return dest_ptr;

    }

    ----------------------------------------------------------------------------------------------------------------

    void flash_buf60( unsigned int *dest_ptr, const unsigned int *source_ptr )

    {

      FCTL3 = FWKEY;

      while (FCTL3&(BUSY));

      do

      {

        FCTL1 = FWKEY|WRT;

        while ((FCTL3&(WAIT))==0);

        *dest_ptr= *source_ptr;

        dest_ptr++; source_ptr++;

      }

    while (dest_ptr<const_ Your_Flash_Memory_Data[max_index] )

    // !! or while ((((int)dest_ptr)&(0x1FF))!=0);

      FCTL1 = FWKEY;

      while (FCTL3&(BUSY));

      FCTL3^= FXKEY|LOCK;

    // restore   WDTCTL if watchdog is used

    }

     

  • Thanks you again Jens-Michael Gross, if you don't you should get paid from TI!

    So for my current purpose i will use one of the info segments as you suggest. I didn't know they are there for data purpose (I wondered their purpose).

    Anyway now that i spent some time on the linker command file, I would like to understand how it's possible to reserve some flash (I'll probably need it soon).

    That's the original linker

    MEMORY
    {
        ....
        INFOD                   : origin = 0x1800, length = 0x0080
        FLASH                   : origin = 0x8000, length = 0x7F80
        ....

    and

    SECTIONS
    {
        ....
        .text      : {} > FLASH              /* CODE                              */
        ....

    So in order to reserve the last 512 bytes segment, I would modify it in the following way:

    MEMORY
    {
        ....
        FLASH                   : origin = 0x8000, length = 0x7D80
        MYDATAFLASH    : origin = 0xFD80, length = 0x200
        ....
        
    SECTIONS
    {
        ....
        .text      : {} > FLASH              
        .mydatasec : {} > MYDATAFLASH
        ....

    Is it correct?

    Thanks!

    Regards,

    Carloalberto



     

  • Carloalberto Torghele said:
    if you don't you should get paid from TI

    Tell them :)

    Carloalberto Torghele said:
    I would modify it in the following way

    Almost. You're on the right track.
    However, you'll need your reserved area on a single flash segment, alinged to a physical 512 byte boundary. Since you cannot use the area required for the interrupt vector table, and creating a gap in the usable flash makes not much sense, I'd suggest putting your segment at the start of the flash area:

    FLASH                   : origin = 0x8200, length = 0x7D80
    MYDATAFLASH    : origin = 0x8000, length = 0x200

    This way you'll have your MYDATAFLASH segment aligned to a physical 512 byte flash segment, so you can erase it without erasing the adjacent areas.

  • ok, perfect!

    Thank you very much!

    Carloalberto

**Attention** This is a public forum