components/
l3gd20.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Components for the L3GD20 sensor.
//!
//! Uses a SPI Interface.
//!
//! Usage
//! -----
//! ```rust
//! let l3gd20 = components::l3gd20::L3gd20Component::new(spi_mux, stm32f429zi::gpio::PinId::PE03).finalize(
//!     components::l3gd20_component_static!(stm32f429zi::spi::Spi));
//! ```

use capsules_core::virtualizers::virtual_spi::{MuxSpiMaster, VirtualSpiMasterDevice};
use capsules_extra::l3gd20::L3gd20Spi;
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
use kernel::create_capability;
use kernel::hil::spi;
use kernel::hil::spi::SpiMasterDevice;

// Setup static space for the objects.
#[macro_export]
macro_rules! l3gd20_component_static {
    ($S:ty $(,)?) => {{
        let txbuffer = kernel::static_buf!([u8; capsules_extra::l3gd20::TX_BUF_LEN]);
        let rxbuffer = kernel::static_buf!([u8; capsules_extra::l3gd20::RX_BUF_LEN]);

        let spi = kernel::static_buf!(
            capsules_core::virtualizers::virtual_spi::VirtualSpiMasterDevice<'static, $S>
        );
        let l3gd20spi = kernel::static_buf!(
            capsules_extra::l3gd20::L3gd20Spi<
                'static,
                capsules_core::virtualizers::virtual_spi::VirtualSpiMasterDevice<'static, $S>,
            >
        );

        (spi, l3gd20spi, txbuffer, rxbuffer)
    };};
}

pub type L3gd20ComponentType<S> = capsules_extra::l3gd20::L3gd20Spi<'static, S>;

pub struct L3gd20Component<
    S: 'static + spi::SpiMaster<'static>,
    CS: spi::cs::IntoChipSelect<S::ChipSelect, spi::cs::ActiveLow>,
> {
    spi_mux: &'static MuxSpiMaster<'static, S>,
    chip_select: CS,
    board_kernel: &'static kernel::Kernel,
    driver_num: usize,
}

impl<
        S: 'static + spi::SpiMaster<'static>,
        CS: spi::cs::IntoChipSelect<S::ChipSelect, spi::cs::ActiveLow>,
    > L3gd20Component<S, CS>
{
    pub fn new(
        spi_mux: &'static MuxSpiMaster<'static, S>,
        chip_select: CS,
        board_kernel: &'static kernel::Kernel,
        driver_num: usize,
    ) -> Self {
        Self {
            spi_mux,
            chip_select,
            board_kernel,
            driver_num,
        }
    }
}

impl<
        S: 'static + spi::SpiMaster<'static>,
        CS: spi::cs::IntoChipSelect<S::ChipSelect, spi::cs::ActiveLow>,
    > Component for L3gd20Component<S, CS>
{
    type StaticInput = (
        &'static mut MaybeUninit<VirtualSpiMasterDevice<'static, S>>,
        &'static mut MaybeUninit<L3gd20Spi<'static, VirtualSpiMasterDevice<'static, S>>>,
        &'static mut MaybeUninit<[u8; capsules_extra::l3gd20::TX_BUF_LEN]>,
        &'static mut MaybeUninit<[u8; capsules_extra::l3gd20::RX_BUF_LEN]>,
    );
    type Output = &'static L3gd20Spi<'static, VirtualSpiMasterDevice<'static, S>>;

    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
        let grant = self.board_kernel.create_grant(self.driver_num, &grant_cap);

        let spi_device = static_buffer.0.write(VirtualSpiMasterDevice::new(
            self.spi_mux,
            self.chip_select.into_cs(),
        ));
        spi_device.setup();

        let txbuffer = static_buffer
            .2
            .write([0; capsules_extra::l3gd20::TX_BUF_LEN]);
        let rxbuffer = static_buffer
            .3
            .write([0; capsules_extra::l3gd20::RX_BUF_LEN]);

        let l3gd20 = static_buffer
            .1
            .write(L3gd20Spi::new(spi_device, txbuffer, rxbuffer, grant));
        spi_device.set_client(l3gd20);

        // TODO verify SPI return value
        let _ = l3gd20.configure();

        l3gd20
    }
}