Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should read offset be expressed in the same terms as write offset? #19

Open
huntc opened this issue Jul 16, 2021 · 8 comments
Open

Should read offset be expressed in the same terms as write offset? #19

huntc opened this issue Jul 16, 2021 · 8 comments

Comments

@huntc
Copy link

huntc commented Jul 16, 2021

Shouldn't the read offset be expressed in the same terms as the write offset and be a multiple of READ_SIZE?

See https://github.com/rust-embedded-community/embedded-storage/blob/master/src/nor_flash.rs#L14

@huntc
Copy link
Author

huntc commented Jul 17, 2021

FYI I've implemented nrf-rs/nrf-hal#337 on the assumption that the read offset is a multiple of READ_SIZE. It'd be great to get confirmation on this. Thanks.

@MathiasKoch
Copy link
Collaborator

MathiasKoch commented Jul 18, 2021

@Dirbaio i think you may be qualified to answer this? 🤷

I have only every used chips with READ_SIZE = 1

@Dirbaio
Copy link
Contributor

Dirbaio commented Jul 19, 2021

Yeah, the current docs are missing specifying that offset and bytes.len() are multiples of READ_SIZE.

However it's true that the majority of chips (all?) have READ_SIZE=1. Maybe we can remove READ_SIZE?

WRITE_SIZE matters especially if the NOR flash is not multiwrite, you don't want impls to do "tricks" behind your back to process unaligned writes because they may write to addresses you don't expect... However for READ_SIZE, if a chip really has a read alignment requirement, impls can read "slightly more" and then give you the subset of bytes, which is fine because reads aren't suppoed to have side effects...

@huntc
Copy link
Author

huntc commented Jul 19, 2021

Yeah, the current docs are missing specifying that offset and bytes.len() are multiples of READ_SIZE.

However it's true that the majority of chips (all?) have READ_SIZE=1. Maybe we can remove READ_SIZE?

WRITE_SIZE matters especially if the NOR flash is not multiwrite, you don't want impls to do "tricks" behind your back to process unaligned writes because they may write to addresses you don't expect... However for READ_SIZE, if a chip really has a read alignment requirement, impls can read "slightly more" and then give you the subset of bytes, which is fine because reads aren't suppoed to have side effects...

I’m happy to drop read size. However, I’m wondering if there’s still a case for dropping write size for the write buf len… for convenience.

I’m using Postcard to serialise what I wish to write. While I pass it in a buffer to serialise, it returns me a slice of that of buffer representing what was serialised. That slice len is byte aligned of course, not Nvmc write aligned in terms of len.

So could we relax the write buf len being write aligned and keep the offset as being write aligned?

@Dirbaio
Copy link
Contributor

Dirbaio commented Jul 19, 2021

However, I’m wondering if there’s still a case for dropping write size for the write buf len… for convenience.

The idea for the NorFlash is to be a low-level trait where impls just expose the hardware capabilities without extra magic. You need that if you're writing low level stuff such as filesystems, databases, key-value-stores.

If you just want "please write these bytes" you want the Storage trait. #14 has an adapter that converts NorFlash to Storage by doing the necessary magic padding and read-modify-writes.

@huntc
Copy link
Author

huntc commented Jul 19, 2021

Thanks for the explanations. Do you want a PR for the elimination of the READ_SIZE requirement?

@Dirbaio
Copy link
Contributor

Dirbaio commented Jul 20, 2021

I'm not sure what's best! 😅

On one hand, chips with READ_SIZE>1 are rare, and in that case the impl could easily do "magic" to simulate READ_SIZE=1.

On the other hand, NorFlash aims to be no-magic lowlevel, and the magic can be done in the Storage adapter instead, like for writing, which feels a bit more consistent.

@chrysn
Copy link

chrysn commented Feb 3, 2022

There is a point to be made about optimizations for aligned reads: If the underlying interface is not byte-oriented, the unaligned-read magics would be invoked on each read even when it is aligned.

Then again, the current interface (with its &[u8]) needs to cater can't be compiled into aligned reads/writes anyway.

How about a low-level read interface that works with something like read_words(offset: Self::WordIndex, &mut [Self::ReadWord; n]), and a provided read(offset: usize, &mut [u8]) that allows unaligned access (but is not used by applications that keenly avoid unaligned access already)?

(There might even be a provided middle ground read_aligned<N: usize>(offset: usize, &mut [[u8; N]]) that goes right for read_words if N >= READ_SIZE and does workarounds otherwise -- but that may be excessive).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants