View Single Post
  #10   Spotlight this post!  
Unread 08-10-2007, 11:15
dcbrown dcbrown is offline
Registered User
AKA: Bud
no team
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Hollis,NH
Posts: 236
dcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud of
Re: Error in code light on, trouble finding error

A nit. Some RAM issues are detected/caught at compile time. But resource exhaustion issues are caught at link time.

There are several "standard" methods of helping debug embedded code. The simplest is to use a macro wrapper and use it with each function call. The macro can be used to either output a message via printf or to set a value into an output port that can be measured or displayed via leds.

When done debugging, just redefine the macro to nothing and allow the code to execute without any debug code overhead. For example, something like:


Code:
int myroutine( int a, int b, int c, int d );
#define DEBUG

#ifdef DEBUG
#define _Call( _arg )  (printf( "CALL: %lX[%d]\n", _arg, __LINE__ )==0)?printf("ERROR!\n"):_arg
#define _Trace( _arg ) printf( "TRAK: %s[%d]\n", _arg, __LINE__ )
#define _Return( _return_value ) { printf( "RETN: [%d] %d\n", __LINE__, _return_value ); return( _return_value ); }
#else
#define _Call( _arg ) _arg
#define _Trace( _arg )
#define _Return( _arg ) return( _arg )
#endif

int main()
{
int status;

    status = _Call( myroutine )(1,2,3,4);
    printf( "status = %d\n", status );
    status = _Call( myroutine )(4,3,3,4);
    printf( "status = %d\n", status );
}
int myroutine( int a, int b, int c, int d)
{
    _Trace( "myroutine" );
    /* routine stuff here... */
    _Return(a+b+c+d);
}
The result is something like:
Code:
CALL: 1200012A0[20]
TRAK: myroutine[27]
RETN: [29] 10
status = 10
CALL: 1200012A0[22]
TRAK: myroutine[27]
RETN: [29] 14
status = 14
You can change the printf call to be any user defined routine you need call to help in debugging the program. You'll need the .map output file to determine which function the routine address matches if just using the call macro. You can get fancier too, if desired.

For interrupt routines, don't use printfs! Instead just toggle a pin to show when you enter/leave the interrupt service routine. Use a scope to track.