components/
lps22hb.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 2023.
4
5//! Component for LPS22HB pressure sensor.
6
7use capsules_core::virtualizers::virtual_i2c::{I2CDevice, MuxI2C};
8use capsules_extra::lps22hb::Lps22hb;
9use core::mem::MaybeUninit;
10use kernel::component::Component;
11use kernel::hil::i2c;
12
13#[macro_export]
14macro_rules! lps22hb_component_static {
15    ($I:ty $(,)?) => {{
16        let i2c_device =
17            kernel::static_buf!(capsules_core::virtualizers::virtual_i2c::I2CDevice<$I>);
18        let lps22hb = kernel::static_buf!(
19            capsules_extra::lps22hb::Lps22hb<
20                'static,
21                capsules_core::virtualizers::virtual_i2c::I2CDevice<$I>,
22            >
23        );
24        let buffer = kernel::static_buf!([u8; 4]);
25
26        (i2c_device, lps22hb, buffer)
27    };};
28}
29
30pub struct Lps22hbComponent<I: 'static + i2c::I2CMaster<'static>> {
31    i2c_mux: &'static MuxI2C<'static, I>,
32    i2c_address: u8,
33}
34
35impl<I: 'static + i2c::I2CMaster<'static>> Lps22hbComponent<I> {
36    pub fn new(i2c_mux: &'static MuxI2C<'static, I>, i2c_address: u8) -> Self {
37        Lps22hbComponent {
38            i2c_mux,
39            i2c_address,
40        }
41    }
42}
43
44impl<I: 'static + i2c::I2CMaster<'static>> Component for Lps22hbComponent<I> {
45    type StaticInput = (
46        &'static mut MaybeUninit<I2CDevice<'static, I>>,
47        &'static mut MaybeUninit<Lps22hb<'static, I2CDevice<'static, I>>>,
48        &'static mut MaybeUninit<[u8; 4]>,
49    );
50    type Output = &'static Lps22hb<'static, I2CDevice<'static, I>>;
51
52    fn finalize(self, s: Self::StaticInput) -> Self::Output {
53        let lps22hb_i2c = s.0.write(I2CDevice::new(self.i2c_mux, self.i2c_address));
54
55        let buffer = s.2.write([0; 4]);
56
57        let lps22hb = s.1.write(Lps22hb::new(lps22hb_i2c, buffer));
58        lps22hb_i2c.set_client(lps22hb);
59
60        lps22hb
61    }
62}