Conversation
c38bb6d to
b20dbe0
Compare
| pub const Flash = struct { | ||
| // TODO: We must also wait for flash dma transfers to finish in addition to | ||
| // critical sections | ||
| // TODO: Maybe instead of asserts we should return errors? |
There was a problem hiding this comment.
I think so, unless we're going down we shouldn't make a hal that makes it hard to make a resilient system
|
|
||
| const BASE = hal.flash.XIP_BASE; | ||
| // We should get this from the memory map because it might be smaller. | ||
| const SIZE = 16 * 1024 * 1024; |
| }); | ||
| rp2xxx.uart.init_logger(uart); | ||
|
|
||
| const flash_storage_start: u32 = 256 * 1024; |
There was a problem hiding this comment.
Can we take this from the target? We don't want someone to build an example and accidentally wipe something important.
| var current_offset: u32 = flash_storage_start; | ||
| while (current_offset < flash_storage_end) : (current_offset += flash.SECTOR_SIZE) { | ||
| std.log.info("Erasing sector at offset {x}", .{current_offset}); | ||
| flash.range_erase(current_offset, flash.SECTOR_SIZE); |
There was a problem hiding this comment.
shouldn't this function handle whatever looping it needs to do to wipe everything in the requested range?
| //! | ||
| const std = @import("std"); | ||
|
|
||
| pub const storage = @import("storage.zig"); |
There was a problem hiding this comment.
might want to call this kv storage or persistent kv or something?
|
|
||
| /// A generic storage implementation that can be used with any flash device. | ||
| /// Items are stored sequentially in sectors (inspired by the rust crate | ||
| /// sequential storage). It should be resilient to power loss and flash |
There was a problem hiding this comment.
can you link the crate?
| src_offset += @truncate(data.len); | ||
| dst_offset += @truncate(data.len); | ||
| }) { | ||
| // if this fails it is something seriosly wrong |
| const WRITE_SIZE = Flash.WRITE_SIZE; | ||
| const ERASE_SIZE = Flash.ERASE_SIZE; |
There was a problem hiding this comment.
Does it makes sense to comptime enforce that these are powers of two? Or is that a safe assumption?
| try testing.expectEqual(@as(?u32, null), try s.fetch(3, u32)); | ||
| } | ||
|
|
||
| test "overwrite frees the old item" { |
| if (i > 1000) return error.NeverRanOutOfMemory; | ||
| } | ||
| } | ||
| }; |
There was a problem hiding this comment.
I imagine you plan on writing tests which utilize the tripwires?
|
In addition to this PR, could you write a couple issues to implement this driver for architectures unlike the rp2xxx? I want to try to catch any leaky abstractions by having you think through some systems like AVR or MSP430, but I don't want to make you implement a patch for hardware you may not have. |
Generic flash storage driver inspired by the rust crate sequential-storage. I needed a persistent storage lib for a project of mine and this is my attempt at writing one. I tried to make it as resilient as possible, like an interrupted store operation shouldn't mess anything up. There are still some TODOs left, but I'd love some feedback on it. I wrote a tiny rp2xxx demo example if anyone wants to try it out.