components/
ninedof.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 9DOF
6//!
7//! Usage
8//! -----
9//!
10//! ```rust
11//! let ninedof = components::ninedof::NineDofComponent::new(board_kernel)
12//!     .finalize(components::ninedof_component_static!(driver1, driver2, ...));
13//! ```
14
15use capsules_extra::ninedof::NineDof;
16use core::mem::MaybeUninit;
17use kernel::capabilities;
18use kernel::component::Component;
19use kernel::create_capability;
20
21#[macro_export]
22macro_rules! ninedof_component_static {
23    ($($P:expr),+ $(,)?) => {{
24        use kernel::count_expressions;
25
26        const NUM_DRIVERS: usize = count_expressions!($($P),+);
27
28        let drivers = kernel::static_init!(
29            [&'static dyn kernel::hil::sensors::NineDof; NUM_DRIVERS],
30            [
31                $($P,)*
32            ]
33        );
34        let ninedof = kernel::static_buf!(capsules_extra::ninedof::NineDof<'static>);
35        (ninedof, drivers)
36    };};
37}
38
39pub struct NineDofComponent {
40    board_kernel: &'static kernel::Kernel,
41    driver_num: usize,
42}
43
44impl NineDofComponent {
45    pub fn new(board_kernel: &'static kernel::Kernel, driver_num: usize) -> NineDofComponent {
46        NineDofComponent {
47            board_kernel,
48            driver_num,
49        }
50    }
51}
52
53impl Component for NineDofComponent {
54    type StaticInput = (
55        &'static mut MaybeUninit<NineDof<'static>>,
56        &'static [&'static dyn kernel::hil::sensors::NineDof<'static>],
57    );
58    type Output = &'static capsules_extra::ninedof::NineDof<'static>;
59
60    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
61        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
62        let grant_ninedof = self.board_kernel.create_grant(self.driver_num, &grant_cap);
63
64        let ninedof = static_buffer.0.write(capsules_extra::ninedof::NineDof::new(
65            static_buffer.1,
66            grant_ninedof,
67        ));
68
69        for driver in static_buffer.1 {
70            kernel::hil::sensors::NineDof::set_client(*driver, ninedof);
71        }
72
73        ninedof
74    }
75}