Hi,
I am trying to make a variable persistent using the PERSISTENT pragma. The code is a copy of an example made by Andreas Dannenberg (posted somewhere on the TI forums):
//============================================================================
// 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;
}
I've created a blank project, uploaded this code, but the persistent variable won't change value. It'll stay at 0. The value cannot be changed with the debugger (program stopped at a breakpoint): it simple flips back to 0.
The .map file shows that the variable is located in the persistent section:
output attributes/
section page origin length input sections
-------- ---- ---------- ---------- ----------------
.TI.persistent
* 0 0000f100 00000002
0000f100 00000002 main.obj (.TI.persistent)
What could be the problem?
EDIT: After a power cycle (removed power for a few seconds) the example worked. However, I have a more complex program that still shows the same behavior. Clues are very welcome
Thanks,
Justus