Circular Buffer in Flash

前端 未结 5 1868
悲&欢浪女
悲&欢浪女 2021-01-02 18:25

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

5条回答
  •  Happy的楠姐
    2021-01-02 19:00

    The "circular" in a flash can be done on basis of block size, which means that you must declare how much blocks of the flash you allocate for this buffer.

    The actual size of the buffer will be at each particular time between n-1 (n is the number of blocks) and n.

    Each block should start with an header that contains sequential number or timestamp that could be used to determine which block is older than the other.

    Each Item encapsulated with an header and a footer. the default header contains whatever you want but according to this header you must know the size of the item. The default footer is 0xFFFFFFFF. This value indicates a null termination.

    In your RAM you must save a pointer to the oldest block and the latest block and pointer to the oldest item and latest item. On power up you go over all blocks find the relevant blocks and load this members.

    When you want to store a new item, you check if the latest block contain enough space for this item. If it does you save the item at the end of the previous item and the change the previous footer to point to this item. If it does not contain enough space you need to erase the oldest block. Before you erase this block change the oldest block members (RAM) to point on the next block and the oldest item to point on the first item in this block. Then you can save the new item in this block and change the footer of the latest item to point this item.

    I know that the explanation may sounds complicated but the process is very simple and if you write it correct you can make it even power fail safe (always keep in you mind the order of the writes).

    Pay attention that the circularity of the buffer is not saved in the flash but the flash only contains a blocks with items that you can decide according to the blocks headers and items headers what is the order of these items

提交回复
热议问题