avr-chat
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-chat] avr-g++ and virtual function call crashes on target mega32


From: Klaus Rudolph
Subject: [avr-chat] avr-g++ and virtual function call crashes on target mega32
Date: Wed, 21 Mar 2012 20:46:11 +0100
User-agent: Thunderbird 2.0.0.23 (X11/20090817)

Hi all,

I try to play around a bit with c++ on avr.

I wrote a small test which uses virtual functions and try to call them.
On simulavr the test behaves as expected, but on the real hardware the
systems seems to restart immediately while calling a virtual function.

I am totally confused, cause the simualtion works but not the target! I
have no jtag interface to debug on the real hardware.

Some hints?

Regards
 Klaus



--- code ---


#include <avr/io.h>
#include <avr/pgmspace.h>

void SerialInit()
{
   /*
   //Setting up the UART bei 7.3728 Mhz
   23     //19200
   11     //38400
   7      //57600
   3      //115200
   */
#ifdef __AVR_ATmega32__
   UBRRL=3;
   UBRRH=0;
   UCSRB=(1<<RXEN)|(1<<TXEN);
#endif

#ifdef __AVR_ATmega128__
   UBRR0L=3;
   UBRR0H=0;
   UCSR0B=(1<<RXEN0)|(1<<TXEN0);
#endif

}

void SerialWriteData(const char d) {
#ifdef __AVR_ATmega32__
   while (!((UCSRA)&0x20));
   UDR=d;
#endif

#ifdef __AVR_ATmega128__
   while (!((UCSR0A)&0x20));   //fuer das Tracen
   UDR0=d;
#endif
}

void SerialWriteString(const char *b){
   const char *cnt=b;
   while(*cnt!=0) SerialWriteData(*cnt++);
}

void SerialWriteString_P(const char *s) {
   char global_buffer[50];
   strcpy_P(global_buffer, s);
   SerialWriteString(global_buffer);
}




class TBase
{
   public:
      char x;
      TBase(char _x):x(_x){}

      virtual void F1()
      {
         SerialWriteString_P(PSTR("Func Base\n"));
      }

      void AnyFunc() { SerialWriteString_P(PSTR("Any Func\n")); }
};

class T1: public TBase
{
   public:
      T1(char _x):TBase(_x){}

      virtual void F1()
      {
      //   SerialWriteString_P(PSTR("Func T1\n"));
      }
};

class T2:public TBase
{
   public:
      T2(char _x):TBase(_x){}
      virtual void F1()
      {
       //  SerialWriteString_P(PSTR("Func T2\n"));
      }
};

void TestFunc()
{
   SerialWriteString_P(PSTR("TestFunc\n"));
}

int main()
{
   SerialInit();

   while(1)
   {
      //calling via "normal" Pointer
      void (*f)()=&TestFunc;
      f();

      // create a Pointer to Base class type
      TBase* zgr;

      // create three instances of derived types
      T1 t1('a');
      T2 t2('b');
      TBase tbase('c');

      SerialWriteString_P(PSTR("1\n"));
      zgr= &t1;

      // call to function of base, non virtual -> ok on target and simulator
      zgr->AnyFunc();

      // access data on instance -> ok on both
      SerialWriteData(zgr->x);

      SerialWriteString_P(PSTR("2\n"));

      // try to call virtual function -> ok on simulavr, seems to jumo
to reset on real target
      zgr->F1();
      SerialWriteString_P(PSTR("3\n"));

      zgr= &t2;
      SerialWriteData(zgr->x);
      zgr->F1();

      zgr= &tbase;
      zgr->F1();

      SerialWriteString_P(PSTR("END\n"));
   }
}

---

Makefile
----
all: go

CXX=/opt/avr_4.6.3/bin/avr-g++
OBJCOPY=/opt/avr_4.6.3/bin/avr-objcopy

go: main.cpp
        $(CXX) -Wall -pedantic -g main.cpp -mmcu=atmega32 -Os -o go
        #$(CXX) -g main.cpp -mmcu=atmega128 -Os -o go

go.srec: go
        $(OBJCOPY) -j .text -O srec $< $@

dl: go.srec
        avrdude -p m32  -c stk500v2 -P /dev/ttyS0  -U flash:w:go.srec

clean:
        rm -f go
        rm -f go.srec





reply via email to

[Prev in Thread] Current Thread [Next in Thread]