components/
panic_button.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//! Component to cause a button press to trigger a kernel panic.
6//!
7//! This can be useful especially when developing or debugging console
8//! capsules.
9//!
10//! Note: the process console has support for triggering a panic, which may be
11//! more convenient depending on the board.
12//!
13//! Usage
14//! -----
15//!
16//! ```rust
17//! components::panic_button::PanicButtonComponent::new(
18//!     &sam4l::gpio::PC[24],
19//!     kernel::hil::gpio::ActivationMode::ActiveLow,
20//!     kernel::hil::gpio::FloatingState::PullUp
21//! )
22//! .finalize(components::panic_button_component_static!(sam4l::gpio::GPIOPin));
23//! ```
24
25use capsules_extra::panic_button::PanicButton;
26use core::mem::MaybeUninit;
27use kernel::component::Component;
28use kernel::hil::gpio;
29
30#[macro_export]
31macro_rules! panic_button_component_static {
32    ($Pin:ty $(,)?) => {{
33        kernel::static_buf!(capsules_core::button::PanicButton<'static, $Pin>)
34    };};
35}
36
37pub struct PanicButtonComponent<'a, IP: gpio::InterruptPin<'a>> {
38    pin: &'a IP,
39    mode: gpio::ActivationMode,
40    floating_state: gpio::FloatingState,
41}
42
43impl<'a, IP: gpio::InterruptPin<'a>> PanicButtonComponent<'a, IP> {
44    pub fn new(
45        pin: &'a IP,
46        mode: gpio::ActivationMode,
47        floating_state: gpio::FloatingState,
48    ) -> Self {
49        Self {
50            pin,
51            mode,
52            floating_state,
53        }
54    }
55}
56
57impl<IP: 'static + gpio::InterruptPin<'static>> Component for PanicButtonComponent<'static, IP> {
58    type StaticInput = &'static mut MaybeUninit<PanicButton<'static, IP>>;
59    type Output = ();
60
61    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
62        let panic_button =
63            static_buffer.write(PanicButton::new(self.pin, self.mode, self.floating_state));
64        self.pin.set_client(panic_button);
65    }
66}