Skip to main content

components/
mlx90614.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//! Components for the MLX90614 IR Temperature Sensor.
6//!
7//! Usage
8//! -----
9//! ```rust
10//! let mlx90614 = components::mlx90614::Mlx90614I2CComponent::new(mux_i2c, i2c_addr,
11//! board_kernel)
12//!    .finalize(components::mlx90614_component_static!());
13//!
14//! let temp = static_init!(
15//!        capsules_extra::temperature::TemperatureSensor<'static>,
16//!        capsules_extra::temperature::TemperatureSensor::new(mlx90614,
17//!                                                 grant_temperature));
18//! kernel::hil::sensors::TemperatureDriver::set_client(mlx90614, temp);
19//! ```
20
21use capsules_core::virtualizers::virtual_i2c::{MuxI2C, SMBusDevice};
22use capsules_extra::mlx90614::Mlx90614SMBus;
23use core::mem::MaybeUninit;
24use kernel::capabilities::MemoryAllocationCapability;
25use kernel::component::Component;
26use kernel::hil::i2c::{self, NoSMBus};
27
28// Setup static space for the objects.
29#[macro_export]
30macro_rules! mlx90614_component_static {
31    () => {{
32        let i2c_device = kernel::static_buf!(capsules_core::virtualizers::virtual_i2c::SMBusDevice);
33        let buffer = kernel::static_buf!([u8; 14]);
34        let mlx90614 = kernel::static_buf!(capsules_extra::mlx90614::Mlx90614SMBus<'static>);
35
36        (i2c_device, buffer, mlx90614)
37    }};
38}
39
40pub struct Mlx90614SMBusComponent<
41    I: 'static + i2c::I2CMaster<'static>,
42    CAP: MemoryAllocationCapability + 'static,
43    S: 'static + i2c::SMBusMaster<'static> = NoSMBus,
44> {
45    i2c_mux: &'static MuxI2C<'static, I, S>,
46    i2c_address: u8,
47    board_kernel: &'static kernel::Kernel,
48    driver_num: usize,
49    mem_cap: CAP,
50}
51
52impl<
53    I: 'static + i2c::I2CMaster<'static>,
54    CAP: MemoryAllocationCapability + 'static,
55    S: 'static + i2c::SMBusMaster<'static>,
56> Mlx90614SMBusComponent<I, CAP, S>
57{
58    pub fn new(
59        i2c: &'static MuxI2C<'static, I, S>,
60        i2c_address: u8,
61        board_kernel: &'static kernel::Kernel,
62        driver_num: usize,
63        mem_cap: CAP,
64    ) -> Self {
65        Mlx90614SMBusComponent {
66            i2c_mux: i2c,
67            i2c_address,
68            board_kernel,
69            driver_num,
70            mem_cap,
71        }
72    }
73}
74
75impl<
76    I: 'static + i2c::I2CMaster<'static>,
77    CAP: MemoryAllocationCapability + 'static,
78    S: 'static + i2c::SMBusMaster<'static>,
79> Component for Mlx90614SMBusComponent<I, CAP, S>
80{
81    type StaticInput = (
82        &'static mut MaybeUninit<SMBusDevice<'static, I, S>>,
83        &'static mut MaybeUninit<[u8; 14]>,
84        &'static mut MaybeUninit<Mlx90614SMBus<'static, SMBusDevice<'static, I, S>>>,
85    );
86    type Output = &'static Mlx90614SMBus<'static, SMBusDevice<'static, I, S>>;
87
88    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
89        let mlx90614_smbus = static_buffer
90            .0
91            .write(SMBusDevice::new(self.i2c_mux, self.i2c_address));
92        let buffer = static_buffer.1.write([0; 14]);
93        let mlx90614 = static_buffer.2.write(Mlx90614SMBus::new(
94            mlx90614_smbus,
95            buffer,
96            self.board_kernel
97                .create_grant(self.driver_num, &self.mem_cap),
98        ));
99
100        mlx90614_smbus.set_client(mlx90614);
101        mlx90614
102    }
103}