2

I'm trying to write and debug a C program for an ATMega8 device using the Atmel Studio Simulator.

For example, let's say I'm trying to debug this piece of code:

int main(void)
{
    while(1)
    {
        SPI_transaction(0xFF);
    }
}



uint8_t SPI_transaction(unsigned char byte_to_send){


    value = byte_to_send;   
    SPDR = byte_to_send;                        //Sends command
    while (checkBitStatus(SPSR,SPIF) == 0){};   //Do nothing until transfer is completed
    value = SPDR;                       //Receives command

    CLEARBIT(SPSR, SPIF);
    return value;

}

unsigned char checkBitStatus(unsigned char byte, unsigned char bit){

    unsigned char bit_status;

    if ((byte & (1 << bit)) == 0){

        bit_status = 0;
    }

    else{

        bit_status = 1;
    }

    return bit_status;
}

This code buids without any problem but when I try to debug this happens:

At some points the program counter goes outside my source files. I've been trying to find out why this happens but I have ot been able yet to find an answer. I leave here the disassembly code for you to have a look. I find it really strange.

Init of main function:

00000024  PUSH R28      Push register on stack 
00000025  PUSH R29      Push register on stack 
00000026  IN R28,0x3D       In from I/O location 
00000027  IN R29,0x3E       In from I/O location 
    SPI_transaction(0xFF);
00000028  SER R24       Set Register 
00000029  RCALL PC+0x0002       Relative call subroutine

    uint8_t SPI_transaction(unsigned char byte_to_send){
0000002B  PUSH R28      Push register on stack 
0000002C  PUSH R29      Push register on stack 
0000002D  PUSH R1       Push register on stack 
0000002E  IN R28,0x3D       In from I/O location 
0000002F  IN R29,0x3E       In from I/O location 
00000030  STD Y+1,R24       Store indirect with displacement 

while (checkBitStatus(SPSR,SPIF) == 0){};   //Do nothing until transfer is completed
00000039  NOP       No operation 
--- No source file -------------------------------------------------------------
0000003A  LDI R24,0x2E      Load immediate 
0000003B  LDI R25,0x00      Load immediate 
0000003C  MOVW R30,R24      Copy register pair 
--- No source file -------------------------------------------------------------
0000003D  LDD R24,Z+0       Load indirect with displacement 
0000003E  LDI R22,0x07      Load immediate 
0000003F  RCALL PC+0x0018       Relative call subroutine 

From that it jumps here:

    unsigned char checkBitStatus(unsigned char byte, unsigned char bit){
00000057  PUSH R28      Push register on stack 
00000058  PUSH R29      Push register on stack 
00000059  RCALL PC+0x0001       Relative call subroutine 
0000005A  PUSH R1       Push register on stack 
0000005B  IN R28,0x3D       In from I/O location 
0000005C  IN R29,0x3E       In from I/O location 
0000005D  STD Y+2,R24       Store indirect with displacement 
0000005E  STD Y+3,R22       Store indirect with displacement 
   if ((byte & (1 << bit)) == 0){
0000005F  LDD R24,Y+2       Load indirect with displacement 
00000060  MOV R24,R24       Copy register 
00000061  LDI R25,0x00      Load immediate 
00000062  LDD R18,Y+3       Load indirect with displacement 
00000063  MOV R18,R18       Copy register 
00000064  LDI R19,0x00      Load immediate 
00000065  MOV R0,R18        Copy register 
00000066  RJMP PC+0x0003        Relative jump 
00000067  ASR R25       Arithmetic shift right 
00000068  ROR R24       Rotate right through carry 
00000069  DEC R0        Decrement 
0000006A  BRPL PC-0x03      Branch if plus 
0000006B  ANDI R24,0x01     Logical AND with immediate 
0000006C  CLR R25       Clear Register 
0000006D  SBIW R24,0x00     Subtract immediate from word 
0000006E  BRNE PC+0x03      Branch if not equal 

            bit_status = 0;
0000006F  STD Y+1,R1        Store indirect with displacement 
00000070  RJMP PC+0x0003        Relative jump 
    bit_status = 1;
00000071  LDI R24,0x01      Load immediate 
00000072  STD Y+1,R24       Store indirect with displacement 
       return bit_status;
00000073  LDD R24,Y+1       Load indirect with displacement 
}
00000074  POP R0        Pop register from stack 
00000075  POP R0        Pop register from stack 
00000076  POP R0        Pop register from stack 
00000077  POP R29       Pop register from stack 
00000078  POP R28       Pop register from stack 
00000079  RET       Subroutine return 

The function returns to somewhere out my source files:

--- No source file -------------------------------------------------------------
00000040  TST R24       Test for Zero or Minus 
00000041  BREQ PC-0x07      Branch if equal
0000003A  LDI R24,0x2E      Load immediate 
0000003B  LDI R25,0x00      Load immediate 
0000003C  MOVW R30,R24      Copy register pair 
0000003D  LDD R24,Z+0       Load indirect with displacement 
0000003E  LDI R22,0x07      Load immediate 
0000003F  RCALL PC+0x0018       Relative call subroutine 

And the goes again to CheckBitStatus() function.

Any help will be really appreciated.

Alex

Aldridge1991
  • 1,326
  • 6
  • 24
  • 51
  • I'm seeing some very similar things with a particular project I'm currently working on. What options are you compiling with? – shuttle87 Nov 06 '14 at 18:19

2 Answers2

0

The code executed outside your source is most likely an interrupt service routine for a hardware interrupt handled by an atmel or c standard library. These routinely occur for stuff like USB events that must be handled asynchronously.

R Bryan
  • 56
  • 4
0

Another simple reason for the debugger to display the assembly view and the message "no source file found" , if you attempt to use the debugger while the configuration manager (Under Build tap) has the configuration option set for release, the program compilation will go to the release directory and not to the debug directory.

Luay
  • 1