Skip to content

Commit

Permalink
panic handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ImplFerris committed Oct 23, 2024
1 parent b51d58c commit 9ce5d32
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/blinky/no-std-main.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions src/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit 9ce5d32

Please sign in to comment.