components/
ltc294x.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//!
7//! Usage
8//! -----
9//!
10//! ```rust
11//! let ltc294x = components::Ltc294xComponent::new(i2c_mux, 0x64, None)
12//!     .finalize(components::ltc294x_component_static!());
13//! let ltc294x_driver = components::Ltc294xDriverComponent::new(ltc294x, board_kernel, DRIVER_NUM)
14//!     .finalize(components::ltc294x_driver_component_static!());
15//! ```
16
17use capsules_core::virtualizers::virtual_i2c::{I2CDevice, MuxI2C};
18use capsules_extra::ltc294x::LTC294XDriver;
19use capsules_extra::ltc294x::LTC294X;
20use core::mem::MaybeUninit;
21use kernel::capabilities;
22use kernel::component::Component;
23use kernel::create_capability;
24use kernel::hil::gpio;
25use kernel::hil::i2c;
26
27#[macro_export]
28macro_rules! ltc294x_component_static {
29    ($I:ty $(,)?) => {{
30        let i2c_device =
31            kernel::static_buf!(capsules_core::virtualizers::virtual_i2c::I2CDevice<'static, $I>);
32        let ltc294x = kernel::static_buf!(
33            capsules_extra::ltc294x::LTC294X<
34                'static,
35                capsules_core::virtualizers::virtual_i2c::I2CDevice<'static, $I>,
36            >
37        );
38        let buffer = kernel::static_buf!([u8; capsules_extra::ltc294x::BUF_LEN]);
39
40        (i2c_device, ltc294x, buffer)
41    };};
42}
43
44#[macro_export]
45macro_rules! ltc294x_driver_component_static {
46    () => {{
47        kernel::static_buf!(capsules_extra::ltc294x::LTC294XDriver<'static>)
48    };};
49}
50
51pub struct Ltc294xComponent<I: 'static + i2c::I2CMaster<'static>> {
52    i2c_mux: &'static MuxI2C<'static, I>,
53    i2c_address: u8,
54    interrupt_pin: Option<&'static dyn gpio::InterruptPin<'static>>,
55}
56
57impl<I: 'static + i2c::I2CMaster<'static>> Ltc294xComponent<I> {
58    pub fn new(
59        i2c_mux: &'static MuxI2C<'static, I>,
60        i2c_address: u8,
61        interrupt_pin: Option<&'static dyn gpio::InterruptPin<'static>>,
62    ) -> Self {
63        Ltc294xComponent {
64            i2c_mux,
65            i2c_address,
66            interrupt_pin,
67        }
68    }
69}
70
71impl<I: 'static + i2c::I2CMaster<'static>> Component for Ltc294xComponent<I> {
72    type StaticInput = (
73        &'static mut MaybeUninit<I2CDevice<'static, I>>,
74        &'static mut MaybeUninit<LTC294X<'static, I2CDevice<'static, I>>>,
75        &'static mut MaybeUninit<[u8; capsules_extra::ltc294x::BUF_LEN]>,
76    );
77    type Output = &'static LTC294X<'static, I2CDevice<'static, I>>;
78
79    fn finalize(self, s: Self::StaticInput) -> Self::Output {
80        let ltc294x_i2c = s.0.write(I2CDevice::new(self.i2c_mux, self.i2c_address));
81
82        let buffer = s.2.write([0; capsules_extra::ltc294x::BUF_LEN]);
83
84        let ltc294x =
85            s.1.write(LTC294X::new(ltc294x_i2c, self.interrupt_pin, buffer));
86        ltc294x_i2c.set_client(ltc294x);
87        self.interrupt_pin.map(|pin| {
88            pin.set_client(ltc294x);
89        });
90
91        ltc294x
92    }
93}
94
95pub struct Ltc294xDriverComponent<I: 'static + i2c::I2CMaster<'static>> {
96    ltc294x: &'static LTC294X<'static, I2CDevice<'static, I>>,
97    board_kernel: &'static kernel::Kernel,
98    driver_num: usize,
99}
100
101impl<I: 'static + i2c::I2CMaster<'static>> Ltc294xDriverComponent<I> {
102    pub fn new(
103        ltc294x: &'static LTC294X<'static, I2CDevice<'static, I>>,
104        board_kernel: &'static kernel::Kernel,
105        driver_num: usize,
106    ) -> Self {
107        Ltc294xDriverComponent {
108            ltc294x,
109            board_kernel,
110            driver_num,
111        }
112    }
113}
114
115impl<I: 'static + i2c::I2CMaster<'static>> Component for Ltc294xDriverComponent<I> {
116    type StaticInput = &'static mut MaybeUninit<LTC294XDriver<'static, I2CDevice<'static, I>>>;
117    type Output = &'static LTC294XDriver<'static, I2CDevice<'static, I>>;
118
119    fn finalize(self, s: Self::StaticInput) -> Self::Output {
120        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
121        let grant = self.board_kernel.create_grant(self.driver_num, &grant_cap);
122
123        let ltc294x_driver = s.write(LTC294XDriver::new(self.ltc294x, grant));
124        self.ltc294x.set_client(ltc294x_driver);
125
126        ltc294x_driver
127    }
128}