kernel/hil/nonvolatile_storage.rs
1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Generic interface for nonvolatile memory.
6
7use crate::errorcode::ErrorCode;
8
9/// Simple interface for reading and writing nonvolatile memory. It is expected
10/// that drivers for nonvolatile memory would implement this trait.
11pub trait NonvolatileStorage<'a> {
12 fn set_client(&self, client: &'a dyn NonvolatileStorageClient);
13
14 /// Read `length` bytes starting at address `address` in to the provided
15 /// buffer. The buffer must be at least `length` bytes long. The address
16 /// must be in the address space of the physical storage.
17 fn read(
18 &self,
19 buffer: &'static mut [u8],
20 address: usize,
21 length: usize,
22 ) -> Result<(), ErrorCode>;
23
24 /// Write `length` bytes starting at address `address` from the provided
25 /// buffer. The buffer must be at least `length` bytes long. This address
26 /// must be in the address space of the physical storage.
27 fn write(
28 &self,
29 buffer: &'static mut [u8],
30 address: usize,
31 length: usize,
32 ) -> Result<(), ErrorCode>;
33}
34
35/// Client interface for nonvolatile storage.
36pub trait NonvolatileStorageClient {
37 /// `read_done` is called when the implementor is finished reading in to the
38 /// buffer. The callback returns the buffer and the number of bytes that
39 /// were actually read.
40 fn read_done(&self, buffer: &'static mut [u8], length: usize);
41
42 /// `write_done` is called when the implementor is finished writing from the
43 /// buffer. The callback returns the buffer and the number of bytes that
44 /// were actually written.
45 fn write_done(&self, buffer: &'static mut [u8], length: usize);
46}