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 and read

Other Parts Discussed in Thread: MSP430G2553

hai,

  I want to read and write some data permenently during operation. So I choose flash write and read.I found  lot of TI discussion and from that I modified one code. Iam using msp430g2553 and CCS v5
. Iam trying first write a character and comment that line and again upload read code. How can I check my code working or not?  my code is pasted below


#include <msp430g2553.h>
char value_wrt=0x52;//character to write
char value_read;
void erase_flash(unsigned short address);
void write_flash (char value,unsigned short address);
unsigned short read_flash (unsigned short address);
int main(void)
{
    WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer
    P1DIR =0xff;
    P1OUT =0xff;
     ///////////////////////////////////////////////////////////comment when reading
    erase_flash(0x1040);         //erase
    write_flash (value_wrt,0x1040);    ////write flash value
    /////////////////////////////////////////////////////////////////comment when writing
     // value_read=read_flash (0x1040);  ////read flash value
     // if(value_read==value_wrt)
     // {
     //  P1OUT =0x00;    // if value read ok , led will off
     // }
      //////////////////////////////////////////////////////////////

    return 0;
}
void erase_flash(unsigned short address)
{
     __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
     char *Flash_ptr;
     Flash_ptr = (char*) address;
     FCTL1 = FWKEY + ERASE;
     FCTL3 = FWKEY;
     *Flash_ptr = 0;
     while(BUSY & FCTL3);                 // Check if Flash being used
     FCTL1 = FWKEY;                       // Clear WRT bit
     FCTL3 = FWKEY +  LOCK;
     __enable_interrupt();
}
void write_flash (char value,unsigned short address)
{
   __disable_interrupt();
  char *Flash_ptr;
  Flash_ptr = (char*) address;
  FCTL2 = FWKEY + FSSEL_1 + FN0;       // Clk = SMCLK/4
  FCTL3 = FWKEY;                       // Clear Lock bit
  FCTL1 = FWKEY + WRT;                 // Set WRT bit for write operation
  *Flash_ptr = value;
  FCTL1 = FWKEY;
  FCTL3 = FWKEY +  LOCK;
  while(BUSY & FCTL3);
  __enable_interrupt();
}

unsigned short read_flash (unsigned short address)
{
   __disable_interrupt();
   FCTL2 = FWKEY + FSSEL_1 + FN0;       // Clk = SMCLK/4
   FCTL3 = FWKEY;                       // Clear Lock bit
  char value;
  char *Flash_ptr;
  Flash_ptr = (char*) address;

  value = *Flash_ptr;
  return (value);
}


  • To check if it's working, use debugger and select "view memory", sub-select INFO in your case.
    And you don't need a special handling reading from flash, it's just like ram in read mode.

    The smallest amount you can erase is 512bytes (a block/segment), Info-mem does have 64byte block/segments though
    So you need to dedicate that at as minimum just for your flash stored variables

    Flash that have been cleared by compiler/firmware-uploader etc, don't need to be erased again.

    You can even dedicate a few bytes along other constants as long you just reserve space as Non-Initialized
    and then use just use flash_write (one time) to these bytes as long there where 0xff

    example: to give your device  a serial number to use, nothing is better than the macid from a external module.

    __no_init unsigned long long mymacid;  // after you get your modules macid, flash-write it here//

    or optional:
    __no_init char mymacidbytes[6] @ 0xFFB8; ; // use flash-write to store the 48bit as bytes in a big-endian fashion//
    optional fixed location at last few bytes before interrupt table.

**Attention** This is a public forum