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.

Compiler: TI MSP430 initialize flash on programming time

Other Parts Discussed in Thread: MSP430G2453

Tool/software: TI C/C++ Compiler

Hi.

I'd like to know how I can set(write) initial value to flash at the programming-time (only).

after program the microcontroller going to change the value of the flash and keep that value. again, i'd like to set initial value only in programming-time.

I am using the "CCS6.2.0" ,"TI-compiler" and My Device is "MSP430G2453".

Thanks

Best Regards,

Min.

  • Hi,

    You can define a variable with const or persistence type and initial value.

    Best Regards,
    Winter Yu
  • thanks for reply, Winter Yu.
    Could you mask some example for me?

    Best Regards,
    Min Gwak
  • Hi, Windter Yu.

    I should be better to explain my case of usage of the flash memory.
    When My msp430 first start up,(boot up) read configuration data that store in the NVM memory from flash.
    While running the msp430, if it need, write(change) the configuration data in NVM memory. so next time,
    when my msp430 re-boot up, it able to use changed configuration data. so far it works all good.

    Now, I'd like to set(initial value) the configuration data on the flash memory in programming time only, and the controller able to have
    same configuration value first time only.

    could you guide me how i can do this? if you can show me some example, that will be great. (my flash memory's start address is 0x1000, 16bit and size = 10)

    thanks
    Best Regards,
    Min Gwak
  • Hi Min,

    You can refer this code for example.

    //******************************************************************************
    //  MSP430G2xx3 Demo - Flash In-System Programming, Copy SegC to SegD
    //
    //  Description: This program first erases flash seg C, then it increments all
    //  values in seg C, then it erases seg D, then copies seg C to seg D.
    //  Assumed MCLK 771kHz - 1428kHz.
    //  //* Set Breakpoint on NOP in the Mainloop to avoid Stressing Flash *//
    //
    //               MSP430G2xx3
    //            -----------------
    //        /|\|              XIN|-
    //         | |                 |
    //         --|RST          XOUT|-
    //           |                 |
    //
    //  D. Dang
    //  Texas Instruments Inc.
    //  December 2010
    //   Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10
    //******************************************************************************
    
    #include <msp430.h>
    
    unsigned int i;
    unsigned short value[10];                   // value to store in flash
    
    /* * Define variables in Flash Section, with persistent type and initial value
     * * set(initial value) the configuration data on the flash memory in programming time
     */
    #pragma PERSISTENT(configureData)
    unsigned short configureData[10] = {1,2,3,4,5,6,7,8,9,10};
    
    // Function prototypes
    void write_ConfigureData (unsigned short *value);
    void read_ConfigureData (unsigned short *value);
    
    int main(void)
    {
      WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer
      if (CALBC1_1MHZ==0xFF)					// If calibration constant erased
      {											
        while(1);                               // do not load, trap CPU!!	
      }
      DCOCTL = 0;                               // Select lowest DCOx and MODx settings
      BCSCTL1 = CALBC1_1MHZ;                    // Set DCO to 1MHz
      DCOCTL = CALDCO_1MHZ;
      FCTL2 = FWKEY + FSSEL0 + FN1;             // MCLK/3 for Flash Timing Generator
    
      read_ConfigureData(value);                // Read value from configureData ( in Flash memory)
    
      for (i=0; i<10; i++)
      {
          value[i]++;                           // Increment per re-boot
      }
    
      write_ConfigureData(value);               // Write value to configureData ( in Flash memory)
    
      while(1){;}// Repeat forever
    
    }
    
    void write_ConfigureData (unsigned short *value)
    {
      unsigned short *Flash_ptr;                // Flash pointer
      unsigned int i;
    
      Flash_ptr = configureData;                // Initialize Flash pointer
      FCTL1 = FWKEY + ERASE;                    // Set Erase bit
      FCTL3 = FWKEY;                            // Clear Lock bit
      *Flash_ptr = 0;                           // Dummy write to erase Flash segment
    
      FCTL1 = FWKEY + WRT;                      // Set WRT bit for write operation
    
      for (i=0; i<10; i++)
      {
        *Flash_ptr++ = value[i];                // Write value to flash
      }
    
      FCTL1 = FWKEY;                            // Clear WRT bit
      FCTL3 = FWKEY + LOCK;                     // Set LOCK bit
    }
    
    void read_ConfigureData (unsigned short *value)
    {
      unsigned short *Flash_ptr;                // Segment C pointer
      unsigned int i;
    
      Flash_ptr = configureData;                // Initialize Flash segment C pointer
    
      for (i=0; i<10; i++)
      {
          value[i] = *Flash_ptr++;              // Write value to flash
      }
    }
    

    Best Regards,

    Winter Yu

  • Hi Min,

    Besides, you need add this code to your .cmd file. 

    Best Regards,

    Winter Yu

**Attention** This is a public forum