components/
flash.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//! Component for Flash
6//!
7//! Provides `FlashMux` and `FlashUser` (virtual flash).
8//!
9//! Usage
10//! -----
11//! ```rust
12//!    let mux_flash = components::flash::FlashMuxComponent::new(&base_peripherals.nvmc).finalize(
13//!       components::flash_mux_component_static!(nrf52833::nvmc::Nvmc),
14//!    );
15//!
16//!    let virtual_app_flash = components::flash::FlashUserComponent::new(mux_flash).finalize(
17//!       components::flash_user_component_static!(nrf52833::nvmc::Nvmc),
18//!    );
19//! ```
20
21use capsules_core::virtualizers::virtual_flash::FlashUser;
22use capsules_core::virtualizers::virtual_flash::MuxFlash;
23use core::mem::MaybeUninit;
24use kernel::component::Component;
25use kernel::hil::flash::{Flash, HasClient};
26
27// Setup static space for the objects.
28#[macro_export]
29macro_rules! flash_user_component_static {
30    ($F:ty) => {{
31        kernel::static_buf!(capsules_core::virtualizers::virtual_flash::FlashUser<'static, $F>)
32    };};
33}
34
35#[macro_export]
36macro_rules! flash_mux_component_static {
37    ($F:ty) => {{
38        kernel::static_buf!(capsules_core::virtualizers::virtual_flash::MuxFlash<'static, $F>)
39    };};
40}
41
42pub struct FlashMuxComponent<F: 'static + Flash + HasClient<'static, MuxFlash<'static, F>>> {
43    flash: &'static F,
44}
45
46impl<F: 'static + Flash + HasClient<'static, MuxFlash<'static, F>>> FlashMuxComponent<F> {
47    pub fn new(flash: &'static F) -> FlashMuxComponent<F> {
48        FlashMuxComponent { flash }
49    }
50}
51
52impl<F: 'static + Flash + HasClient<'static, MuxFlash<'static, F>>> Component
53    for FlashMuxComponent<F>
54{
55    type StaticInput = &'static mut MaybeUninit<MuxFlash<'static, F>>;
56    type Output = &'static MuxFlash<'static, F>;
57
58    fn finalize(self, s: Self::StaticInput) -> Self::Output {
59        let mux_flash = s.write(MuxFlash::new(self.flash));
60        HasClient::set_client(self.flash, mux_flash);
61
62        mux_flash
63    }
64}
65
66pub struct FlashUserComponent<F: 'static + Flash + HasClient<'static, MuxFlash<'static, F>>> {
67    mux_flash: &'static MuxFlash<'static, F>,
68}
69
70impl<F: 'static + Flash + HasClient<'static, MuxFlash<'static, F>>> FlashUserComponent<F> {
71    pub fn new(mux_flash: &'static MuxFlash<'static, F>) -> Self {
72        Self { mux_flash }
73    }
74}
75
76impl<F: 'static + Flash + HasClient<'static, MuxFlash<'static, F>>> Component
77    for FlashUserComponent<F>
78{
79    type StaticInput = &'static mut MaybeUninit<FlashUser<'static, F>>;
80    type Output = &'static FlashUser<'static, F>;
81
82    fn finalize(self, s: Self::StaticInput) -> Self::Output {
83        s.write(FlashUser::new(self.mux_flash))
84    }
85}