Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
jezule
PostPosted: May 28, 2012 - 10:50 AM
Newbie


Joined: Apr 27, 2010
Posts: 9


Hi all,

I'm having problems to get this piece of code to work properly on an atmega328p:

Code:

void ModemEnable( void )
{
   // Turn Modem on
   PORTB |= _BV( PB2 );
   //
   Timer1Start();
   set_sleep_mode( SLEEP_MODE_IDLE );
   do{
      sleep_mode();
   }while( (T1Timeout == 0) );   // 16.5s per timeout
   //
   Timer1Stop();

   SwUartEnable();
}


It seems that the call to Timer1Start does not have any effect because the MCU gets stuck at the loop, I pause the debugger and I can check that Timer1 is nor configured neither running. Timer1Start works as it should cause I'm using this function in other parts of the code with no trouble. As I'm using size optimizations I assumed that the optimizer was dropping that function call but in the lss file I can check that is not dropping any call:

Code:

void ModemEnable( void )
{
   // Turn Modem on
   PORTB |= _BV( PB2 );
     ad6:   2a 9a          sbi   0x05, 2   ; 5
   //
   Timer1Start();
     ad8:   1c d1          rcall   .+568       ; 0xd12 <Timer1Start>
   set_sleep_mode( SLEEP_MODE_IDLE );
     ada:   83 b7          in   r24, 0x33   ; 51
     adc:   81 7f          andi   r24, 0xF1   ; 241
     ade:   83 bf          out   0x33, r24   ; 51
   do{
      sleep_mode();
     ae0:   83 b7          in   r24, 0x33   ; 51
     ae2:   81 60          ori   r24, 0x01   ; 1
     ae4:   83 bf          out   0x33, r24   ; 51
     ae6:   88 95          sleep
     ae8:   83 b7          in   r24, 0x33   ; 51
     aea:   8e 7f          andi   r24, 0xFE   ; 254
     aec:   83 bf          out   0x33, r24   ; 51
   }while( (T1Timeout == 0) );   // 16.5s per timeout
     aee:   80 91 92 02    lds   r24, 0x0292
     af2:   88 23          and   r24, r24
     af4:   a9 f3          breq   .-22        ; 0xae0 <ModemEnable+0xa>
   //
   Timer1Stop();
     af6:   1c d1          rcall   .+568       ; 0xd30 <Timer1Stop>

   SwUartEnable();
}
     af8:   00 c0          rjmp   .+0         ; 0xafa <SwUartEnable>


In fact, if I set a breakpoint at the beginning of the function ModemEnable, before the call to Timer1Start(), everything works as it should. If there is no breakpoint then the call to Timer1Start has no effect. If compile the code with optimization turned off everything works as expected, no breakpoint needed.

Any idea what can be happening? I'm using AS6+AVR Dragon for debugging and WinAVR-20100110+Eclipse+AVR Plugin for development.

Regards
 
 View user's profile Send private message  
Reply with quote Back to top
Koshchi
PostPosted: May 28, 2012 - 03:21 PM
10k+ Postman


Joined: Nov 17, 2004
Posts: 13846
Location: Vancouver, BC

You do not show where T1Timeout is changed, but my guess is that it is in an ISR. If so, T1Timeout must be declared volatile.

_________________
Regards,
Steve A.

The Board helps those that help themselves.
 
 View user's profile Send private message  
Reply with quote Back to top
jezule
PostPosted: May 28, 2012 - 10:40 PM
Newbie


Joined: Apr 27, 2010
Posts: 9


Yes, you are right, T1Timeout is changed within an ISR but it is declared as volatile.

Source file:
Code:

#include <stdint.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/power.h>

#include "Utils.h"

volatile uint8_t T1Timeout;

void Timer1Start( void )
{
   T1Timeout = 0;
   power_timer1_enable();
   TCNT1 = 0;
   TCCR1B = _BV(CS12)|_BV(CS10);
}

void Timer1Stop( void )
{
   TCCR1B = 0;
   power_timer1_disable();
}

ISR( TIMER1_OVF_vect )
{
   // ISR period = 65536/(4MHz/1024) = approx. 16.7 s
   T1Timeout += 1;
}


and header file:

Code:

#ifndef UTILS_H_
#define UTILS_H_

extern volatile uint8_t T1Timeout;

void Timer1Start( void );
void Timer1Stop( void );

#endif /* UTILS_H_ */


Regards
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: May 29, 2012 - 07:20 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62324
Location: (using avr-gcc in) Finchingfield, Essex, England

Just out of interest what is the point of tbe do...while? It's not going to iterate is it?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
jezule
PostPosted: May 30, 2012 - 10:03 AM
Newbie


Joined: Apr 27, 2010
Posts: 9


Just in case it goes out of sleep due to another interrupt
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits