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 Optimization issues - setting, directives possible?

Other Parts Discussed in Thread: MSP430F2013

Hi,

 I am developing an application on the MSP430F2013 which requires storage of an integer value in INFO segments. CCS version is 5.1, Compiler V4.0.0.

The write_flash() function (enclosed below) is a modified version of the example code for 2xx devices, the change being that I am passing parameters to it.

I need to use Optimization level 1 or 2 to reduce code size by about 50 – 80 bytes to fit in all the other code.

 The problem I am facing is that when Compiler Optimization is set to 1 or higher, the write_flash() does not write to flash or writes a wrong value.

Analysis of the Disassembly code shows that the instruction corresponding to the dummy write ‘*Flash_ptr = 0;’ is missing in the assembly code generated when using Optimization. So the flash erase does not start and hence the erratic write behavior.

The line is removed in the assembly code even if write_flash() is the only function in the entire code i.e. full code size is only about 160 bytes.

 Without Optimization the code runs perfectly.

 

Is there a way a exclude optimization of this particular function or some other directive so that the instruction is not removed? Or some other workaround.

Thanks and regards,

Tanmay Sutaria.

Code:

 void main(void)

{       WDTCTL = WDTPW + WDTHOLD;     // Stop watchdog

       DCOCTL = CALDCO_8MHZ; BCSCTL1 = CALBC1_8MHZ;//clock configuration

       BCSCTL2 = 0x06;//SMCLK div=8 i.e. SMCLK=1MHz //0x02 for mclk/2, 0x04 for mclk/4, 0x06 for mclk/8

       FCTL2 = FWKEY + FSSEL_1 + FN4 + FN2 + FN1 ; // MCLK/3 for Flash Timing Generator 23 ie. 347K for 8MHZ

       .

       while(1)

                    {              if(. . . .)

                                                write_flash(cal_multi, 0x1044); //cal_multi is value to be written, 0x1044 is address location.

                     }

}

 

void write_flash(unsigned int v1, unsigned int ad)

{        unsigned int *Flash_ptr;           // Flash pointer

      __disable_interrupt();

          Flash_ptr = (unsigned int *) ad;          // 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

         *Flash_ptr = v1;

         FCTL1 = FWKEY;                            // Clear WRT bit

         FCTL3 = FWKEY + LOCK;                     // Set LOCK bit

         __enable_interrupt();

}

Thanks in advance.