This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
您好!
我正在尝试使用 PERSISTENT pragma 使变量保持持久性。 该代码是 Andreas Dannenberg 所做的示例的副本(发布在 TI 论坛上某处):
//============================================================================ // Name : #pragma PERSISTENT Test for C++ // Author : Andreas Dannenberg // Version : 1.0 //============================================================================ #include "msp430.h" // Declare a simple class that maintains a static counter variable. Note that // only one copy of this variable is shared by all instances of this class. class PersistentTest { public: int getCounter() { return counter; } void setCounter(int newCounterValue) { counter = newCounterValue; } void incrementCounter() { counter++; } private: static int counter; }; // In C++ the counter needs to be explicitly defined/initialized. For data // members this has to be done outside the class in the namespace scope. This // is also where we are going to apply the PERSISTENT pragma. #pragma PERSISTENT int PersistentTest::counter = 0; int main() { // Stop the MSP430's watchdog timer WDTCTL = WDTPW | WDTHOLD; // Create an instance of our test class PersistentTest persistentTest; // Call the member function to increment the class-internal counter persistentTest.incrementCounter(); // Obtain a snapshot of the class-internal counter volatile int currentCounterValue = persistentTest.getCounter(); // Set breakpoint here and use the debugger to look at the value of // 'currentCounterValue'. Then, reset the device and run and re-inspect. __no_operation(); return 0; }
我已经创建了一个空白项目、上传了此代码、但 persistent 变量不会更改值。 它将保持为0。 调试器不能更改该值(程序在断点处停止):它可以简单地反转至0。
map 文件显示该变量位于 persistent 段中:
output attributes/ section page origin length input sections -------- ---- ---------- ---------- ---------------- .TI.persistent * 0 0000f100 00000002 0000f100 00000002 main.obj (.TI.persistent)
问题可能是什么?
编辑:在一个电源循环(关闭电源几秒钟)后、这个示例就有效了。 但是、我有一个更复杂的程序、仍然显示相同的行为。 线索非常受欢迎
谢谢。
尤斯图斯
附加信息:简单示例.map 文件会在全局符号列表中显示持久性符号:
GLOBAL SYMBOLS: SORTED BY Symbol Address address name ------- ---- 000000a0 __STACK_SIZE .....removed lines..... 00002360 _stack 00002400 __STACK_END 0000f100 _ZN14PersistentTest7counterE
我的其他程序不是这种情况(它在.TI.persistent 段中具有变量、也位于0000f100):
SECTION ALLOCATION MAP output attributes/ section page origin length input sections -------- ---- ---------- ---------- ---------------- .TI.persistent * 0 0000f100 00000011 0000f100 00000010 main.obj (.TI.persistent:_ZN29_INTERNAL_8_main_cpp_183a4ba47Globals6alarmsE$0) 0000f110 00000001 main.obj (.TI.persistent) ....lines removed.... GLOBAL SYMBOLS: SORTED BY Symbol Address address name ------- ---- 000000a0 __STACK_SIZE ....lines removed.... 00002360 _stack 00002400 __STACK_END 0000f12e __TI_Handler_Table_Base 0000f134 __TI_Handler_Table_Limit
请注意、此程序使用 MCU 中的几乎所有 FRAM、优化级别设置为整个程序优化(4)、并且--opt_for_speed 设置为 size (0)。
复位时、FRAM 受写保护。 要对其进行写入、您需要设置 SYSCFG0:PFWP=0 (然后在完成后将其设置回=1)。 [参考用户指南(SLAU445I)表1-24]
下面的示例 msp430fr231x_framwrite.c 中说明了这一点:
https://dev.ti.com/tirex/explore/node?node=A__AHbDS4Rv1nVVIcfPWJjnKg__msp430ware__IOGqZri__LATEST