Flash中的循环缓冲区 [英] Circular Buffer in Flash

查看:434
本文介绍了Flash中的循环缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在闪存芯片中将不同长度的项目存储在循环队列中。每个项目都会有它的封装,所以我可以弄清楚它是多大,下一个项目开始的地方。当缓冲区中有足够的项目时,它会包装到一开始。

I need to store items of varying length in a circular queue in a flash chip. Each item will have its encapsulation so I can figure out how big it is and where the next item begins. When there are enough items in the buffer, it will wrap to the beginning.

在闪存芯片中存储圆形队列的好方法是什么? / strong>

What is a good way to store a circular queue in a flash chip?

我想要存储数以万计的项目。所以从开始开始,读取到缓冲区的结尾并不理想,因为需要时间来搜索到最后。

There is a possibility of tens of thousands of items I would like to store. So starting at the beginning and reading to the end of the buffer is not ideal because it will take time to search to the end.

另外,因为它是循环的需要能够区分第一个项目和最后一个项目。

Also, because it is circular, I need to be able to distinguish the first item from the last.

最后一个问题是存储在闪存中,所以删除每个块都是耗时的,每个块只能设定一定次数。 p>

The last problem is that this is stored in flash, so erasing each block is both time consuming and can only be done a set number of times for each block.

推荐答案

首先,块管理

在每个块的开始放置一个较小的标题。您需要跟踪最旧和最新的主要事项是块号,它只是递增modulo k k 必须大于总块数。理想情况下,使 k 小于MAX值(例如0xFFFF),以便您可以轻松地判断擦除的块是什么。

Put a smaller header at the start of each block. The main thing you need to keep track of the "oldest" and "newest" is a block number, which simply increments modulo k. k must be greater than your total number of blocks. Ideally, make k less than your MAX value (e.g. 0xFFFF) so you can easily tell what is an erased block.

在启动时,你的代码依次读取每个块的标题,并且在序列中定位第n个和第i个块的第一个和最后一个块。 =(n i + 1)MODULO ķ。小心不要被擦除的块(块号例如0xFFFF)或某种损坏的数据(例如不完全擦除)而感到困惑。

At start-up, your code reads the headers of each block in turn, and locates the first and last blocks in the sequence that is ni+1 = (ni + 1) MODULO k. Take care not to get confused by erased blocks (block number is e.g. 0xFFFF) or data that is somehow corrupted (e.g. incomplete erase).

在每个块

每个块最初都为空(每个字节为0xFF)。每个记录都是一个接一个地写的。如果您有固定大小的记录,那么您可以使用简单的索引访问它。如果你有可变大小的记录,那么要读取它,你必须从块的开始扫描链接列表样式。

Each block initially starts empty (each byte is 0xFF). Each record is simply written one after the other. If you have fixed-size records, then you can access it with a simple index. If you have variable-size records, then to read it you have to scan from the start of the block, linked-list style.

如果你想要具有可变大小的记录,大小记录,但避免线性扫描,那么您可以在每个记录上有一个定义良好的标题。例如。使用0作为记录分隔符,并且 COBS -encode (或 COBS / R -encode )每条记录。或者使用您选择的字节作为分隔符,如果发生在每个记录中,则转义该字节(类似于 PPP协议)。

If you want to have variable-size records, but avoid linear scan, then you could have a well defined header on each record. E.g. use 0 as a record delimiter, and COBS-encode (or COBS/R-encode) each record. Or use a byte of your choice as a delimiter, and 'escape' that byte if it occurs in each record (similar to the PPP protocol).

在启动时,一旦你知道最新的块,你可以进行线性扫描最新记录。或者如果您有固定大小的记录或记录分隔符,则可以进行二进制搜索。

At start-up, once you know your latest block, you can do a linear scan for the latest record. Or if you have fixed-size records or record delimiters, you could do a binary search.

擦除计划

对于一些闪存芯片,擦除块可能需要很长时间 - 例如5秒。考虑将擦除作为后台任务调整一点提前。例如。当前区块是x%满时,然后开始擦除下一个区块。

For some Flash memory chips, erasing a block can take significant time--e.g. 5 seconds. Consider scheduling an erase as a background task a bit "ahead of time". E.g. when the current block is x% full, then start erasing the next block.

记录编号

您可能想要记录数字。过去我所做的一切就是在每个块的头部放置第一条记录的记录号。然后软件必须保持块内每个记录的编号。

You may want to number records. The way I've done it in the past is to put, in the header of each block, the record number of the first record. Then the software has to keep count of the numbers of each record within the block.

校验和或CRC

如果要检测到损坏的数据(例如由于意外的电源故障导致的不完整的写入或擦除),则可以向每个记录添加校验和或CRC,也可以添加到块头。注意,块标题CRC只会覆盖标题本身,而不是记录,因为每个新记录都不能被重新写入。

If you want to detect corrupted data (e.g. incomplete writes or erases due to unexpected power failure), then you can add a checksum or CRC to each record, and perhaps to the block header. Note the block header CRC would only cover the header itself, not the records, since it could not be re-written when each new record is written.

这篇关于Flash中的循环缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆