我工作的一个Arduino库,将最大限度地AVR的EEPROM的寿命。 它需要的要存储的变量数和没有休息。 这是我的尝试,这并不适用于所有情况。
背景资料
爱特梅尔说每个存储器单元的额定100000写入/擦除周期。 他们还提供了一个应用指南 ,其中介绍了如何执行磨损均衡。 下面是应用笔记摘要。
通过交替在两个内存地址写,我们可以增加擦除/写入20万次。 三个内存地址给你30万擦/写周期等。 要自动执行此过程中,一个状态缓冲区用于跟踪的下一个写应该是。 状态缓冲器还必须是相同的长度参数缓冲器,因为磨损均衡,必须在其上,以及执行。 既然我们不能存储下一个写的指数,我们递增状态缓冲器对应的索引。
下面是一个例子。
<------------------- 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 ]
题
我该如何解决上述问题? 有没有更好的方法来追踪下一个元素应该被写的?