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
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.
//! Debug capsule to cause a button press to trigger a kernel panic.
//!
//! This can be useful especially when developing or debugging console
//! capsules.
//!
//! Usage
//! -----
//!
//! The recommended way is to use the `PanicButtonComponent`.
//!
//! Alternatively, a low-level way of using the capsule is as follows.
//!
//! ```rust,ignore
//! let panic_button = static_init!(
//! PanicButton,
//! PanicButton::new(
//! &sam4l::gpio::PA[16],
//! kernel::hil::gpio::ActivationMode::ActiveLow,
//! kernel::hil::gpio::FloatingState::PullUp
//! )
//! );
//! sam4l::gpio::PA[16].set_client(panic_button);
//! ```
use kernel::hil::gpio;
pub struct PanicButton<'a, IP: gpio::InterruptPin<'a>> {
pin: &'a IP,
mode: gpio::ActivationMode,
}
impl<'a, IP: gpio::InterruptPin<'a>> PanicButton<'a, IP> {
pub fn new(
pin: &'a IP,
mode: gpio::ActivationMode,
floating_state: gpio::FloatingState,
) -> Self {
pin.make_input();
pin.set_floating_state(floating_state);
pin.enable_interrupts(gpio::InterruptEdge::EitherEdge);
Self { pin, mode }
}
}
impl<'a, IP: gpio::InterruptPin<'a>> gpio::Client for PanicButton<'a, IP> {
fn fired(&self) {
if self.pin.read_activation(self.mode) == gpio::ActivationState::Active {
panic!("Panic button pressed");
}
}
}