如何将 `__ramfunc` 固有函数应用于构造函数?

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

我需要将所有代码放入 ram(我正在编写 flash)。我正在使用 IAR 7.80,并且每个函数的 __ramfunc 内在函数都能正常工作,但 C++ 构造函数却不行。

例如我有以下 class:

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

我还没有找到在 ram 中定义构造函数 Os_Timer 的方法。编译器抱怨

expected an identifier

object attribute not allowed

在构造函数行上。

IAR 手册说 __ramfunc 必须放在 return 值之前,但构造函数没有 return 值。

我尝试强制执行 __ramfunc 行为但没有成功:

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

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

有人知道怎么做吗?

要将 __ramfunc 应用于 C++ 构造函数,您必须使用 _Pragma("object_attribute=__ramfunc"),如下例所示。

class Os_Timer
{
private:
  os_tmrcnt_t tmr;

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

请注意,要使其正常工作 os_TimerStart 也应声明为 __ramfunc,否则 os_TimerStart 将被放置在闪存中并可能被您的闪存更新覆盖。为了帮助您检测到这一点,如果您尝试从声明为 __ramfunc 的函数调用未声明为 __ramfunc 的函数,编译器将发出警告。