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;
25use kernel::component::Component;
26use kernel::create_capability;
27use kernel::hil::i2c::{self, NoSMBus};
28
29// Setup static space for the objects.
30#[macro_export]
31macro_rules! mlx90614_component_static {
32    () => {{
33        let i2c_device = kernel::static_buf!(capsules_core::virtualizers::virtual_i2c::SMBusDevice);
34        let buffer = kernel::static_buf!([u8; 14]);
35        let mlx90614 = kernel::static_buf!(capsules_extra::mlx90614::Mlx90614SMBus<'static>);
36
37        (i2c_device, buffer, mlx90614)
38    };};
39}
40
41pub struct Mlx90614SMBusComponent<
42    I: 'static + i2c::I2CMaster<'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}
50
51impl<I: 'static + i2c::I2CMaster<'static>, S: 'static + i2c::SMBusMaster<'static>>
52    Mlx90614SMBusComponent<I, S>
53{
54    pub fn new(
55        i2c: &'static MuxI2C<'static, I, S>,
56        i2c_address: u8,
57        board_kernel: &'static kernel::Kernel,
58        driver_num: usize,
59    ) -> Self {
60        Mlx90614SMBusComponent {
61            i2c_mux: i2c,
62            i2c_address,
63            board_kernel,
64            driver_num,
65        }
66    }
67}
68
69impl<I: 'static + i2c::I2CMaster<'static>, S: 'static + i2c::SMBusMaster<'static>> Component
70    for Mlx90614SMBusComponent<I, S>
71{
72    type StaticInput = (
73        &'static mut MaybeUninit<SMBusDevice<'static, I, S>>,
74        &'static mut MaybeUninit<[u8; 14]>,
75        &'static mut MaybeUninit<Mlx90614SMBus<'static, SMBusDevice<'static, I, S>>>,
76    );
77    type Output = &'static Mlx90614SMBus<'static, SMBusDevice<'static, I, S>>;
78
79    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
80        let mlx90614_smbus = static_buffer
81            .0
82            .write(SMBusDevice::new(self.i2c_mux, self.i2c_address));
83        let buffer = static_buffer.1.write([0; 14]);
84        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
85        let mlx90614 = static_buffer.2.write(Mlx90614SMBus::new(
86            mlx90614_smbus,
87            buffer,
88            self.board_kernel.create_grant(self.driver_num, &grant_cap),
89        ));
90
91        mlx90614_smbus.set_client(mlx90614);
92        mlx90614
93    }
94}