Search code examples
algorithmchecksumcrc

Simple error checking to replace repetition code in flash


I have a 16 bit field in flash memory in which to store an 8-bit number (more specifically, a value in the closed range from 0 to 254). I'd like to use the extra 8+ bits for error checking (error correction not needed), and the most obvious approach is simply repeat the value twice. Only slightly less obvious is the XMODEM packet number approach: store the number in the first octet, and 255 minus the number in the second octet.

Are there any better options that will provide more robust error detection in the available space and that are simple to implement and fast to execute? Perhaps the fact that flash bits are more likely to go from 1 to 0 than 0 to 1 can be taken advantage of?


Solution

  • Note up front: I think the XMODEM approach is reasonable, so I'd take that and get to work on something more important. Anyhow, you tagged this question with algorithm, so you can also approach this in a way that proves this...

    Since you want error detection, the most important part is that any changing a single bit can be detected, so two representations of the number must not be just a single bit apart, preferable they are as far apart as possible. Further, not every change to a bit is equally likely to occur.

    If you model this as a graph, you will get vertices identified by a 16-bit number and directed edges between two vertices that define the probability of that transition. This will be a complete graph, so think about how you store it (if you store it at all instead of computing it on demand). What you are now searching for is a circular path of exactly 255 vertices with a maximum weight.

    For that, just search for any circular path with 255 vertices, using DFS with a tendency to heavy edges. From this circle, take its lightest edge and remove all non-heavier edges from the graph. Then, repeat searching for a graph in the resulting (possibly disconnected!) graph, until you can't find one any more.

    Finally, map your input values to vertex IDs of (one of) the remaining circles to store them in your flash memory.