components/
lps25hb.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 LPS25HB pressure sensor.
6
7use capsules_core::virtualizers::virtual_i2c::{I2CDevice, MuxI2C};
8use capsules_extra::lps25hb::LPS25HB;
9use core::mem::MaybeUninit;
10use kernel::capabilities;
11use kernel::component::Component;
12use kernel::create_capability;
13use kernel::hil::gpio;
14use kernel::hil::i2c;
15
16#[macro_export]
17macro_rules! lps25hb_component_static {
18    ($I:ty $(,)?) => {{
19        let i2c_device =
20            kernel::static_buf!(capsules_core::virtualizers::virtual_i2c::I2CDevice<'static>);
21        let lps25hb = kernel::static_buf!(
22            capsules_extra::lps25hb::LPS25HB<
23                'static,
24                capsules_core::virtualizers::virtual_i2c::I2CDevice<'static, $I>,
25            >
26        );
27        let buffer = kernel::static_buf!([u8; capsules_extra::lps25hb::BUF_LEN]);
28
29        (i2c_device, lps25hb, buffer)
30    };};
31}
32
33pub struct Lps25hbComponent<I: 'static + i2c::I2CMaster<'static>> {
34    i2c_mux: &'static MuxI2C<'static, I>,
35    i2c_address: u8,
36    interrupt_pin: &'static dyn gpio::InterruptPin<'static>,
37    board_kernel: &'static kernel::Kernel,
38    driver_num: usize,
39}
40
41impl<I: 'static + i2c::I2CMaster<'static>> Lps25hbComponent<I> {
42    pub fn new(
43        i2c_mux: &'static MuxI2C<'static, I>,
44        i2c_address: u8,
45        interrupt_pin: &'static dyn gpio::InterruptPin<'static>,
46        board_kernel: &'static kernel::Kernel,
47        driver_num: usize,
48    ) -> Self {
49        Lps25hbComponent {
50            i2c_mux,
51            i2c_address,
52            interrupt_pin,
53            board_kernel,
54            driver_num,
55        }
56    }
57}
58
59impl<I: 'static + i2c::I2CMaster<'static>> Component for Lps25hbComponent<I> {
60    type StaticInput = (
61        &'static mut MaybeUninit<I2CDevice<'static, I>>,
62        &'static mut MaybeUninit<LPS25HB<'static, I2CDevice<'static, I>>>,
63        &'static mut MaybeUninit<[u8; capsules_extra::lps25hb::BUF_LEN]>,
64    );
65    type Output = &'static LPS25HB<'static, I2CDevice<'static, I>>;
66
67    fn finalize(self, s: Self::StaticInput) -> Self::Output {
68        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
69        let grant = self.board_kernel.create_grant(self.driver_num, &grant_cap);
70
71        let lps25hb_i2c = s.0.write(I2CDevice::new(self.i2c_mux, self.i2c_address));
72
73        let buffer = s.2.write([0; capsules_extra::lps25hb::BUF_LEN]);
74
75        let lps25hb =
76            s.1.write(LPS25HB::new(lps25hb_i2c, self.interrupt_pin, buffer, grant));
77        lps25hb_i2c.set_client(lps25hb);
78        self.interrupt_pin.set_client(lps25hb);
79
80        lps25hb
81    }
82}