我正在开发一个 Arduino 库,它将最大限度地延长 AVR 的 EEPROM 的寿命。它需要您要存储的变量数量并完成其余的工作。这是我的尝试,并非在所有情况下都有效。
背景资料
Atmel 表示,每个存储单元的额定写入/擦除周期为 100,000 次。它们还提供了应用说明,描述了如何执行磨损均衡。这是应用笔记的摘要。
通过在两个内存地址上交替写入,我们可以将擦除/写入次数增加到 200,000 次。三个内存地址为您提供 300,000 次擦除/写入周期,依此类推。为了使这个过程自动化,使用状态缓冲区来跟踪下一次写入的位置。状态缓冲区也必须与参数缓冲区的长度相同,因为也必须对其执行磨损均衡。由于我们无法存储下一次写入的索引,我们在状态缓冲区中增加相应的索引。
这是一个例子。
<------------------- EEPROM -------------------->
0 N
-------------------------------------------------
Parameter Buffer | Status Buffer |
-------------------------------------------------
Initial state.
[ 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 ]
First write is a 7. The corresponding position
in the status buffer is changed to previous value + 1.
Both buffers are circular.
[ 7 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 ]
A second value, 4, is written to the parameter buffer
and the second element in the status buffer becomes
the previous element, 1 in this case, plus 1.
[ 7 | 4 | 0 | 0 | 0 | 0 | 1 | 2 | 0 | 0 | 0 | 0 ]
And so on
[ 7 | 4 | 9 | 0 | 0 | 0 | 1 | 2 | 3 | 0 | 0 | 0 ]
为了确定下一次写入应该发生的位置,我们查看元素之间的差异。如果前一个元素 + 1 不等于下一个元素,那么这就是下一次写入发生的地方。例如:
Compute the differences by starting at the first
element in the status buffer and wrapping around.
General algo: previous element + 1 = current element
1st element: 0 + 1 = 1 = 1st element (move on)
2nd element: 1 + 1 = 2 = 2nd element (move on)
3rd element: 2 + 1 = 3 = 3rd element (move on)
4th element: 3 + 1 = 4 != 4th element (next write occurs here)
[ 7 | 4 | 9 | 0 | 0 | 0 | 1 | 2 | 3 | 0 | 0 | 0 ]
^ ^
| |
Another edge case to consider is when the incrementing values
wrap around at 256 because we are writing bytes. With the
following buffer we know the next write is at the 3rd element
because 255 + 1 = 0 != 250 assuming we are using byte arithmetic.
[ x | x | x | x | x | x | 254 | 255 | 250 | 251 | 252 | 253 ]
^
|
After we write at element 3 the status buffer is incremented
to the next value using byte arithmetic and looks like this.
255 + 1 = 0 (wrap around)
0 + 1 != 251 (next write is here)
[ x | x | x | x | x | x | 254 | 255 | 0 | 251 | 252 | 253 ]
^
|
上面的这些例子展示了如何为一个变量延长 EEPROM 的寿命。对于多个变量,想象将 EEPROM 分割成具有相同数据结构但缓冲区更小的多个段。
问题
我有上面描述的工作代码。我的问题是当缓冲区长度 >= 256 时算法不起作用。这就是发生的情况
Buffer length of 256. The last zero is from
the initial state of the buffer. At every index
previous element + 1 == next element. No way to
know where the next write should be.
<-------------- Status Buffer ------------>
[ 1 | 2 | ... | 253 | 254 | 255 | 0 ]
A similar problem occurs when the buffer length
is greater than 256. A lookup for a read will think
the next element is at the first 0 when it should be at
255.
<-------------------- Status Buffer ------------------>
[ 1 | 2 | ... | 253 | 254 | 255 | 0 | 0 | 0 ]
问题
我该如何解决上述问题?有没有更好的方法来跟踪下一个元素应该写在哪里?