Search code examples
c++constructorembeddediar

How to apply the `__ramfunc` instrinsic to a constructor?


I need to put all my code in ram (I'm writing the flash). I'm using IAR 7.80 and everything works fine with the __ramfunc intrinsic on every function but not for the C++ constructors.

For example I have the following class:

class Os_Timer {
  private:
    os_tmrcnt_t tmr;
  public:
    __ramfunc Os_Timer() { reset(); }
    __ramfunc void reset() { os_TimerStart( &tmr ); }
};

I haven't find a way to define the constructor Os_Timer in ram. The compiler complains

expected an identifier

and

object attribute not allowed

on the constructor line.

The IAR manual says that the __ramfunc has to be placed before the return value but the constructor doesn't have a return value.

I have tried without success to force the __ramfunc behaviour:

_Pragma("location=\"section  .textrw\"") 

and

_Pragma("location=\"RAM_region\"") 

Someone know how to do it?


Solution

  • To apply __ramfunc to a C++ constructor you have to use _Pragma("object_attribute=__ramfunc") as in the example below.

    class Os_Timer
    {
    private:
      os_tmrcnt_t tmr;
    
    public:
      _Pragma("object_attribute=__ramfunc") Os_Timer() { reset(); }
      __ramfunc void reset() { os_TimerStart(&tmr); }
    };
    

    Note that it for this to work os_TimerStart should also be declared as __ramfunc, otherwise os_TimerStart will be placed in flash and may be overwritten by your flash update. To help you detect this the compiler will issue a warning if you try to call a function that is not declared as __ramfunc from a function that is.