Skip to main content

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) => {{ kernel::static_buf!(capsules_core::virtualizers::virtual_flash::FlashUser<'static, $F>) }};
31}
32
33#[macro_export]
34macro_rules! flash_mux_component_static {
35    ($F:ty) => {{ kernel::static_buf!(capsules_core::virtualizers::virtual_flash::MuxFlash<'static, $F>) }};
36}
37
38pub struct FlashMuxComponent<F: 'static + Flash + HasClient<'static, MuxFlash<'static, F>>> {
39    flash: &'static F,
40}
41
42impl<F: 'static + Flash + HasClient<'static, MuxFlash<'static, F>>> FlashMuxComponent<F> {
43    pub fn new(flash: &'static F) -> FlashMuxComponent<F> {
44        FlashMuxComponent { flash }
45    }
46}
47
48impl<F: 'static + Flash + HasClient<'static, MuxFlash<'static, F>>> Component
49    for FlashMuxComponent<F>
50{
51    type StaticInput = &'static mut MaybeUninit<MuxFlash<'static, F>>;
52    type Output = &'static MuxFlash<'static, F>;
53
54    fn finalize(self, s: Self::StaticInput) -> Self::Output {
55        let mux_flash = s.write(MuxFlash::new(self.flash));
56        HasClient::set_client(self.flash, mux_flash);
57
58        mux_flash
59    }
60}
61
62pub struct FlashUserComponent<F: 'static + Flash + HasClient<'static, MuxFlash<'static, F>>> {
63    mux_flash: &'static MuxFlash<'static, F>,
64}
65
66impl<F: 'static + Flash + HasClient<'static, MuxFlash<'static, F>>> FlashUserComponent<F> {
67    pub fn new(mux_flash: &'static MuxFlash<'static, F>) -> Self {
68        Self { mux_flash }
69    }
70}
71
72impl<F: 'static + Flash + HasClient<'static, MuxFlash<'static, F>>> Component
73    for FlashUserComponent<F>
74{
75    type StaticInput = &'static mut MaybeUninit<FlashUser<'static, F>>;
76    type Output = &'static FlashUser<'static, F>;
77
78    fn finalize(self, s: Self::StaticInput) -> Self::Output {
79        s.write(FlashUser::new(self.mux_flash))
80    }
81}