components/
panic_button.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
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Component to cause a button press to trigger a kernel panic.
//!
//! This can be useful especially when developing or debugging console
//! capsules.
//!
//! Note: the process console has support for triggering a panic, which may be
//! more convenient depending on the board.
//!
//! Usage
//! -----
//!
//! ```rust
//! components::panic_button::PanicButtonComponent::new(
//!     &sam4l::gpio::PC[24],
//!     kernel::hil::gpio::ActivationMode::ActiveLow,
//!     kernel::hil::gpio::FloatingState::PullUp
//! )
//! .finalize(components::panic_button_component_static!(sam4l::gpio::GPIOPin));
//! ```

use capsules_extra::panic_button::PanicButton;
use core::mem::MaybeUninit;
use kernel::component::Component;
use kernel::hil::gpio;

#[macro_export]
macro_rules! panic_button_component_static {
    ($Pin:ty $(,)?) => {{
        kernel::static_buf!(capsules_core::button::PanicButton<'static, $Pin>)
    };};
}

pub struct PanicButtonComponent<'a, IP: gpio::InterruptPin<'a>> {
    pin: &'a IP,
    mode: gpio::ActivationMode,
    floating_state: gpio::FloatingState,
}

impl<'a, IP: gpio::InterruptPin<'a>> PanicButtonComponent<'a, IP> {
    pub fn new(
        pin: &'a IP,
        mode: gpio::ActivationMode,
        floating_state: gpio::FloatingState,
    ) -> Self {
        Self {
            pin,
            mode,
            floating_state,
        }
    }
}

impl<IP: 'static + gpio::InterruptPin<'static>> Component for PanicButtonComponent<'static, IP> {
    type StaticInput = &'static mut MaybeUninit<PanicButton<'static, IP>>;
    type Output = ();

    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
        let panic_button =
            static_buffer.write(PanicButton::new(self.pin, self.mode, self.floating_state));
        self.pin.set_client(panic_button);
    }
}