components/
bmm150.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 the BMM150 Magnetometer Sensor.
6//!
7//!
8//! Usage
9//! -----
10//! ```rust
11//! let BMM150 = BMM150Component::new(mux_i2c, 0x10).finalize(
12//!     components::bmm150_component_static!(nrf5240::i2c::TWI));
13//! let ninedof = components::ninedof::NineDofComponent::new(board_kernel)
14//!     .finalize(components::ninedof_component_static!(BMM150));
15//! ```
16
17use capsules_core::virtualizers::virtual_i2c::{I2CDevice, MuxI2C};
18use capsules_extra::bmm150::BMM150;
19use core::mem::MaybeUninit;
20use kernel::component::Component;
21use kernel::hil::i2c;
22
23// Setup static space for the objects.
24#[macro_export]
25macro_rules! bmm150_component_static {
26    ($I:ty $(,)?) => {{
27        let i2c_device =
28            kernel::static_buf!(capsules_core::virtualizers::virtual_i2c::I2CDevice<$I>);
29        let buffer = kernel::static_buf!([u8; 8]);
30        let bmm150 = kernel::static_buf!(
31            capsules_extra::bmm150::BMM150<
32                'static,
33                capsules_core::virtualizers::virtual_i2c::I2CDevice<$I>,
34            >
35        );
36
37        (i2c_device, buffer, bmm150)
38    };};
39}
40
41pub struct BMM150Component<I: 'static + i2c::I2CMaster<'static>> {
42    i2c_mux: &'static MuxI2C<'static, I>,
43    i2c_address: u8,
44}
45
46impl<I: 'static + i2c::I2CMaster<'static>> BMM150Component<I> {
47    pub fn new(i2c: &'static MuxI2C<'static, I>, i2c_address: u8) -> Self {
48        BMM150Component {
49            i2c_mux: i2c,
50            i2c_address,
51        }
52    }
53}
54
55impl<I: 'static + i2c::I2CMaster<'static>> Component for BMM150Component<I> {
56    type StaticInput = (
57        &'static mut MaybeUninit<I2CDevice<'static, I>>,
58        &'static mut MaybeUninit<[u8; 8]>,
59        &'static mut MaybeUninit<BMM150<'static, I2CDevice<'static, I>>>,
60    );
61    type Output = &'static BMM150<'static, I2CDevice<'static, I>>;
62
63    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
64        let bmm150_i2c = static_buffer
65            .0
66            .write(I2CDevice::new(self.i2c_mux, self.i2c_address));
67        let buffer = static_buffer.1.write([0; 8]);
68        let bmm150 = static_buffer.2.write(BMM150::new(buffer, bmm150_i2c));
69
70        bmm150_i2c.set_client(bmm150);
71        bmm150
72    }
73}