tags:

views:

35

answers:

3

I have used timer A in MSP430 with high compiler optimization, but found that my timer code is failing when high compiler optimization used. When none optimization is used code works fine.

This code is used to achieve 1 ms timer tick. timeOutCNT is increamented in interrupt.

Following is the code

   //Disable interrupt and clear CCR0
   TIMER_A_TACTL = TIMER_A_TASSEL |                       // set the clock source as SMCLK
    TIMER_A_ID |                           // set the divider to 8
    TACLR |                                // clear the timer
    MC_1;      // continuous mode
   TIMER_A_TACTL &= ~TIMER_A_TAIE;                        // timer interrupt disabled
   TIMER_A_TACTL &= 0;                        // timer interrupt flag disabled

   CCTL0 = CCIE;                               // CCR0 interrupt enabled
   CCR0 = 500;
   TIMER_A_TACTL &= TIMER_A_TAIE;    //enable timer interrupt
   TIMER_A_TACTL &= TIMER_A_TAIFG;    //enable timer interrupt
   TACTL = TIMER_A_TASSEL + MC_1 + ID_3;                   // SMCLK, upmode

   timeOutCNT = 0;

   //timeOutCNT is increased in timer interrupt
   while(timeOutCNT <= 1); //delay of 1 milisecond 

   TIMER_A_TACTL = TIMER_A_TASSEL |                       // set the clock source as SMCLK
   TIMER_A_ID |                             // set the divider to 8
   TACLR |                                  // clear the timer
   MC_1;        // continuous mode
   TIMER_A_TACTL &= ~TIMER_A_TAIE;                        // timer interrupt disabled
   TIMER_A_TACTL &= 0x00;                        // timer interrupt flag disabled

Can anybody help me here to resolve this issue? Is there any other way we can use timer A so it works fine in optimization modes? Or do I have used is wrongly to achieve 1 ms interrupt?

A: 

I can't comment specifically on your timer code, but I saw a similar problem in general. When high levels of optimization were used, code would break in various places in seemingly unrelated ways. We ultimately chalked it up to compiler bugs then disabled optimization completely.

Throwback1986
+1  A: 

Are TIMER_A_TACTL and others volatile? If not, the compiler may reorder or combine reads and writes, under the assumption that these have no side effects.

You should be able to solve by either introducing barriers at appropriate positions, or by declaring these variables as volatile

Hasturkun
+1  A: 

Have a look at the list file output to see whether the assembler output contains output for the line

while(timeOutCNT <= 1);

If you are using the kickstart version of the compiler then the list file will not contain the assembler listing and you should load the code into C-Spy and look at the disassembler listing there.

I suspect that Hasturkun is on the right lines in that you have probably not declared timeOutCNT as volatile. If you forget this then the optimiser will assume that the while statement will reduce to

while (1) ;

Ian