0
\$\begingroup\$

In a MSP430 with internal FRAM, I am storing a long variable in FRAM with the following code.

#pragma PERSISTENT(x)
unsigned long x = 0;

    //FRAM write
    SYSCFG0 = DFWP; //unlock fram
    x++;
    SYSCFG0 =  PFWP | DFWP; //lock fram

Given this is a 32 bit value and it is a 16 bit microcontroller, if a power loss were to occur during the write, could data be corrupted? Do additional mitigations have to be taken such as having 3 variables so that the state of 2 is the correct value?

\$\endgroup\$
3
  • \$\begingroup\$ Is power loss your only concern? Because the decoupling capacitors discharge slow enougg that it is possible to detect the falling voltage and to execute some code to shut down. \$\endgroup\$
    – CL.
    Commented Mar 27, 2021 at 17:02
  • \$\begingroup\$ That is an option, but it would result in needing extra circuitry and additional power draw. \$\endgroup\$ Commented Mar 27, 2021 at 17:08
  • 1
    \$\begingroup\$ @EricJohnson not really that much extra circuitry – I think (haven't checked the datasheet) the MSP430 has brownout detection logic built in, for this exact purpose! \$\endgroup\$ Commented Mar 28, 2021 at 10:50

1 Answer 1

2
\$\begingroup\$

If power loss occur in the middle of any atomic or non-atomic operation than data corruption is highly probable.

"having 3 variables so that the state of 2 is the correct value?"

This is a good idea but the 3 writings should be done in three distant moments in time separated by at least 1 second.

In fact, the power supply fall off may last many milliseconds.

There's another approach that was used by the Journaling Flash File System years ago.

Basically, each data structure write is done in 2 different moments in time.

The first time you write the actual structure.

The second time, you calculate the structure's digest or CRC and then write the digest.

When you read the structure you verify that the digest you calculate is equal to the one that was written in memory.

In this case there's no need for data duplication.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Any appropriate form of error correction would work here too. Hamming codes would probably work fine, for instance. \$\endgroup\$
    – Hearth
    Commented Mar 27, 2021 at 17:16
  • \$\begingroup\$ Ha! @Hearth is literally recommending the code developed to make very early, very "glitchy" computers safe against glitches, like you're trying to make your FRAM writing safe against power glitching :) Love it! (You could use a (31,26)-Hamming code to take words of 26 bits (which probably suffices for your application?) and encode them as 31 bit words, ignore one bit and get a 32 bit word) \$\endgroup\$ Commented Mar 28, 2021 at 10:52
  • 1
    \$\begingroup\$ Note that these are not "optimal" by any definition arising from code theory for that length, but honestly, they're easy and very likely good enough for you – if the 32 bit word you've written has more than one bit error, you simply cannot recover it. \$\endgroup\$ Commented Mar 28, 2021 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.