components/
fm25cl.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
// 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 FM25CL FRAM chip.
//!
//! Uses a SPI Interface.
//!
//! Usage
//! -----
//! ```rust
//! let fm25cl = components::fm25cl::Fm25clComponent::new(spi_mux, stm32f429zi::gpio::PinId::PE03)
//!     .finalize(components::fm25cl_component_static!(stm32f429zi::spi::Spi));
//! ```

use capsules_core::virtualizers::virtual_spi::{MuxSpiMaster, VirtualSpiMasterDevice};
use capsules_extra::fm25cl::FM25CL;
use core::mem::MaybeUninit;
use kernel::component::Component;
use kernel::hil::spi;
use kernel::hil::spi::SpiMasterDevice;

#[macro_export]
macro_rules! fm25cl_component_static {
    ($S:ty $(,)?) => {{
        let txbuffer = kernel::static_buf!([u8; capsules_extra::fm25cl::BUF_LEN]);
        let rxbuffer = kernel::static_buf!([u8; capsules_extra::fm25cl::BUF_LEN]);

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

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

pub struct Fm25clComponent<
    S: 'static + spi::SpiMaster<'static>,
    CS: spi::cs::IntoChipSelect<S::ChipSelect, spi::cs::ActiveLow>,
> {
    spi_mux: &'static MuxSpiMaster<'static, S>,
    chip_select: CS,
}

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

impl<
        S: 'static + spi::SpiMaster<'static>,
        CS: spi::cs::IntoChipSelect<S::ChipSelect, spi::cs::ActiveLow>,
    > Component for Fm25clComponent<S, CS>
{
    type StaticInput = (
        &'static mut MaybeUninit<VirtualSpiMasterDevice<'static, S>>,
        &'static mut MaybeUninit<FM25CL<'static, VirtualSpiMasterDevice<'static, S>>>,
        &'static mut MaybeUninit<[u8; capsules_extra::fm25cl::BUF_LEN]>,
        &'static mut MaybeUninit<[u8; capsules_extra::fm25cl::BUF_LEN]>,
    );
    type Output = &'static FM25CL<'static, VirtualSpiMasterDevice<'static, S>>;

    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
        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::fm25cl::BUF_LEN]);
        let rxbuffer = static_buffer.3.write([0; capsules_extra::fm25cl::BUF_LEN]);

        let fm25cl = static_buffer
            .1
            .write(FM25CL::new(spi_device, txbuffer, rxbuffer));
        spi_device.set_client(fm25cl);

        fm25cl
    }
}