components/
app_flash_driver.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Component for any App Flash Driver.
//!
//! Usage
//! -----
//! ```rust
//! let app_flash =
//!     components::app_flash_driver::AppFlashComponent::new(board_kernel, &base_peripherals.nvmc)
//!         .finalize(components::app_flash_component_static!(
//!             nrf52833::nvmc::Nvmc,
//!             512
//!     ));
//! ```

use capsules_extra::app_flash_driver::AppFlash;
use capsules_extra::nonvolatile_to_pages::NonvolatileToPages;
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
use kernel::create_capability;
use kernel::hil;
use kernel::hil::nonvolatile_storage::NonvolatileStorage;

#[macro_export]
macro_rules! app_flash_component_static {
    ($F:ty, $buffer_size: literal) => {{
        let buffer = kernel::static_buf!([u8; $buffer_size]);
        let page_buffer = kernel::static_buf!(<$F as kernel::hil::flash::Flash>::Page);
        let nv_to_page = kernel::static_buf!(
            capsules_extra::nonvolatile_to_pages::NonvolatileToPages<'static, $F>
        );
        let app_flash = kernel::static_buf!(capsules_extra::app_flash_driver::AppFlash<'static>);
        (buffer, page_buffer, nv_to_page, app_flash)
    };};
}

pub struct AppFlashComponent<
    F: 'static + hil::flash::Flash + hil::flash::HasClient<'static, NonvolatileToPages<'static, F>>,
    const BUF_LEN: usize,
> {
    board_kernel: &'static kernel::Kernel,
    driver_num: usize,
    storage: &'static F,
}

impl<
        F: 'static
            + hil::flash::Flash
            + hil::flash::HasClient<'static, NonvolatileToPages<'static, F>>,
        const BUF_LEN: usize,
    > AppFlashComponent<F, BUF_LEN>
{
    pub fn new(
        board_kernel: &'static kernel::Kernel,
        driver_num: usize,
        storage: &'static F,
    ) -> AppFlashComponent<F, BUF_LEN> {
        AppFlashComponent {
            board_kernel,
            driver_num,
            storage,
        }
    }
}

impl<
        F: 'static
            + hil::flash::Flash
            + hil::flash::HasClient<'static, NonvolatileToPages<'static, F>>,
        const BUF_LEN: usize,
    > Component for AppFlashComponent<F, BUF_LEN>
{
    type StaticInput = (
        &'static mut MaybeUninit<[u8; BUF_LEN]>,
        &'static mut MaybeUninit<<F as hil::flash::Flash>::Page>,
        &'static mut MaybeUninit<NonvolatileToPages<'static, F>>,
        &'static mut MaybeUninit<AppFlash<'static>>,
    );
    type Output = &'static AppFlash<'static>;

    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);

        let buffer = static_buffer.0.write([0; BUF_LEN]);

        let flash_pagebuffer = static_buffer
            .1
            .write(<F as hil::flash::Flash>::Page::default());

        let nv_to_page = static_buffer
            .2
            .write(NonvolatileToPages::new(self.storage, flash_pagebuffer));
        self.storage.set_client(nv_to_page);

        let app_flash = static_buffer
            .3
            .write(capsules_extra::app_flash_driver::AppFlash::new(
                nv_to_page,
                self.board_kernel.create_grant(self.driver_num, &grant_cap),
                buffer,
            ));

        nv_to_page.set_client(app_flash);

        app_flash
    }
}