components/
fxos8700.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 FXOS8700cq
6//!
7//! I2C Interface
8//!
9//! Usage
10//! -----
11//! ```rust
12//! let fxos8700 = components::fxos8700::Fxos8700Component::new(mux_i2c, PinId::AdB1_00.get_pin().as_ref().unwrap())
13//!    .finalize(components::fxos8700_component_static!());
14//!
15//! let ninedof = components::ninedof::NineDofComponent::new(board_kernel)
16//!    .finalize(components::ninedof_component_static!(fxos8700));
17//! ```
18
19// Based on the component written for sam4l by:
20// Author: Philip Levis <pal@cs.stanford.edu>
21// Last modified: 6/03/2020
22
23use capsules_core::virtualizers::virtual_i2c::{I2CDevice, MuxI2C};
24use capsules_extra::fxos8700cq::Fxos8700cq;
25
26use kernel::component::Component;
27
28use core::mem::MaybeUninit;
29use kernel::hil;
30use kernel::hil::gpio;
31use kernel::hil::i2c;
32
33#[macro_export]
34macro_rules! fxos8700_component_static {
35    ($I:ty $(,)?) => {{
36        let i2c_device =
37            kernel::static_buf!(capsules_core::virtualizers::virtual_i2c::I2CDevice<$I>);
38        let buffer = kernel::static_buf!([u8; capsules_extra::fxos8700cq::BUF_LEN]);
39        let fxo = kernel::static_buf!(capsules_extra::fxos8700cq::Fxos8700cq<'static>);
40
41        (i2c_device, buffer, fxo)
42    };};
43}
44
45pub struct Fxos8700Component<I: 'static + i2c::I2CMaster<'static>> {
46    i2c_mux: &'static MuxI2C<'static, I>,
47    i2c_address: u8,
48    gpio: &'static dyn gpio::InterruptPin<'static>,
49}
50
51impl<I: 'static + i2c::I2CMaster<'static>> Fxos8700Component<I> {
52    pub fn new(
53        i2c: &'static MuxI2C<'static, I>,
54        i2c_address: u8,
55        gpio: &'static dyn hil::gpio::InterruptPin<'static>,
56    ) -> Fxos8700Component<I> {
57        Fxos8700Component {
58            i2c_mux: i2c,
59            i2c_address,
60            gpio,
61        }
62    }
63}
64
65impl<I: 'static + i2c::I2CMaster<'static>> Component for Fxos8700Component<I> {
66    type StaticInput = (
67        &'static mut MaybeUninit<I2CDevice<'static, I>>,
68        &'static mut MaybeUninit<[u8; capsules_extra::fxos8700cq::BUF_LEN]>,
69        &'static mut MaybeUninit<Fxos8700cq<'static>>,
70    );
71    type Output = &'static Fxos8700cq<'static>;
72
73    fn finalize(self, s: Self::StaticInput) -> Self::Output {
74        let fxos8700_i2c = s.0.write(I2CDevice::new(self.i2c_mux, self.i2c_address));
75        let buffer = s.1.write([0; capsules_extra::fxos8700cq::BUF_LEN]);
76        let fxos8700 = s.2.write(Fxos8700cq::new(fxos8700_i2c, self.gpio, buffer));
77
78        fxos8700_i2c.set_client(fxos8700);
79        self.gpio.set_client(fxos8700);
80
81        fxos8700
82    }
83}