components/
can.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
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022
// Copyright OxidOS Automotive SRL 2022
//
// Author: Teona Severin <teona.severin@oxidos.io>

//! Component for CAN syscall interface.
//!
//! This provides one Component, `CanComponent`, which implements a
//! userspace syscall interface to the Can peripheral.
//!
//! Usage
//! -----
//! ```rust
//! let can = components::can::CanComponent::new(
//!     board_kernel,
//!     capsules_extra::can::DRIVER_NUM,
//!     &peripherals.can1
//! ).finalize(components::can_component_static!(
//!     stm32f429zi::can::Can<'static>
//! ));
//! ```
//!

use capsules_extra::can::CanCapsule;
use core::mem::MaybeUninit;
use kernel::component::Component;
use kernel::hil::can;
use kernel::{capabilities, create_capability};

#[macro_export]
macro_rules! can_component_static {
    ($C:ty $(,)?) => {{
        use capsules_extra::can::CanCapsule;
        use core::mem::MaybeUninit;
        use kernel::hil::can;
        use kernel::static_buf;

        let CAN_TX_BUF = static_buf!([u8; can::STANDARD_CAN_PACKET_SIZE]);
        let CAN_RX_BUF = static_buf!([u8; can::STANDARD_CAN_PACKET_SIZE]);
        let can = static_buf!(capsules_extra::can::CanCapsule<'static, $C>);
        (can, CAN_TX_BUF, CAN_RX_BUF)
    };};
}

pub struct CanComponent<A: 'static + can::Can> {
    board_kernel: &'static kernel::Kernel,
    driver_num: usize,
    can: &'static A,
}

impl<A: 'static + can::Can> CanComponent<A> {
    pub fn new(
        board_kernel: &'static kernel::Kernel,
        driver_num: usize,
        can: &'static A,
    ) -> CanComponent<A> {
        CanComponent {
            board_kernel,
            driver_num,
            can,
        }
    }
}

impl<A: 'static + can::Can> Component for CanComponent<A> {
    type StaticInput = (
        &'static mut MaybeUninit<CanCapsule<'static, A>>,
        &'static mut MaybeUninit<[u8; can::STANDARD_CAN_PACKET_SIZE]>,
        &'static mut MaybeUninit<[u8; can::STANDARD_CAN_PACKET_SIZE]>,
    );
    type Output = &'static CanCapsule<'static, A>;

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

        let can = static_buffer.0.write(capsules_extra::can::CanCapsule::new(
            self.can,
            grant_can,
            static_buffer.1.write([0; can::STANDARD_CAN_PACKET_SIZE]),
            static_buffer.2.write([0; can::STANDARD_CAN_PACKET_SIZE]),
        ));
        can::Controller::set_client(self.can, Some(can));
        can::Transmit::set_client(self.can, Some(can));
        can::Receive::set_client(self.can, Some(can));

        can
    }
}