From 9ce5d32501739a9c7d3cb4b5e32e721d33ced90f Mon Sep 17 00:00:00 2001 From: ImplFerris Date: Wed, 23 Oct 2024 09:23:42 +0530 Subject: [PATCH] panic handler --- src/blinky/no-std-main.md | 20 ++++++++++++++++++++ src/quick-start.md | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/blinky/no-std-main.md b/src/blinky/no-std-main.md index 17c1c28..908fa73 100644 --- a/src/blinky/no-std-main.md +++ b/src/blinky/no-std-main.md @@ -24,6 +24,26 @@ A panic handler is a function in Rust that defines what happens when your progra You don't have to define your own panic handler function; you can use existing crates such as panic_halt or panic_probe instead. +For example, we used the panic_halt crate to halt execution when a panic occurs. +```rust +use panic_halt as _; +``` +The program will stop and remain in this infinite loop whenever a panic occurs. + +In fact, the [panic_halt crate's code](https://github.com/korken89/panic-halt/blob/master/src/lib.rs) implements a simple panic handler, which looks like this: +```rust +use core::panic::PanicInfo; +use core::sync::atomic::{self, Ordering}; + +#[inline(never)] +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + loop { + atomic::compiler_fence(Ordering::SeqCst); + } +} +``` + **Related Resources:** - [Rust official doc](https://doc.rust-lang.org/nomicon/panic-handler.html) - [The Embedded Rust Book](https://docs.rust-embedded.org/book/start/panicking.html) diff --git a/src/quick-start.md b/src/quick-start.md index 5daad8b..73b22e7 100644 --- a/src/quick-start.md +++ b/src/quick-start.md @@ -10,7 +10,7 @@ This example code is taken from rp235x-hal repo (It also includes additional exa It creates a blinking effect by toggling the pin's output state between high and low. ## The code snippet -This is not the entire code. There are things you have to initialize and import crates. +This is only part of the code. You'll need to set up some initial configurations and import the necessary crates. ```rust #[embassy_executor::main] async fn main(_spawner: Spawner) { @@ -30,7 +30,7 @@ async fn main(_spawner: Spawner) { ``` ## Clone the Quick start project -You can clone the blinky project I created and navigate to the project folder and run it. +You can clone the quick start project I created and navigate to the project folder and run it. ```sh git clone https://github.com/ImplFerris/pico2-quick