capsules_extra/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//! Debug capsule 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//! Usage
11//! -----
12//!
13//! The recommended way is to use the `PanicButtonComponent`.
14//!
15//! Alternatively, a low-level way of using the capsule is as follows.
16//!
17//! ```rust,ignore
18//! let panic_button = static_init!(
19//! PanicButton,
20//! PanicButton::new(
21//! &sam4l::gpio::PA[16],
22//! kernel::hil::gpio::ActivationMode::ActiveLow,
23//! kernel::hil::gpio::FloatingState::PullUp
24//! )
25//! );
26//! sam4l::gpio::PA[16].set_client(panic_button);
27//! ```
28
29use kernel::hil::gpio;
30
31pub struct PanicButton<'a, IP: gpio::InterruptPin<'a>> {
32 pin: &'a IP,
33 mode: gpio::ActivationMode,
34}
35
36impl<'a, IP: gpio::InterruptPin<'a>> PanicButton<'a, IP> {
37 pub fn new(
38 pin: &'a IP,
39 mode: gpio::ActivationMode,
40 floating_state: gpio::FloatingState,
41 ) -> Self {
42 pin.make_input();
43 pin.set_floating_state(floating_state);
44 pin.enable_interrupts(gpio::InterruptEdge::EitherEdge);
45
46 Self { pin, mode }
47 }
48}
49
50impl<'a, IP: gpio::InterruptPin<'a>> gpio::Client for PanicButton<'a, IP> {
51 fn fired(&self) {
52 if self.pin.read_activation(self.mode) == gpio::ActivationState::Active {
53 panic!("Panic button pressed");
54 }
55 }
56}