Skip to content

Commit 224a909

Browse files
committed
wrapping up interrupt draft
1 parent c4d05e9 commit 224a909

10 files changed

Lines changed: 32 additions & 18 deletions

File tree

mdbook/src/08-inputs-and-outputs/polling-sucks.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,10 @@ Superloops work and are often used in embedded systems, but the programmer has t
6666

6767
Doing multiple things at once is called *concurrent* programming, and shows up in many places in programming, but especially in embedded systems. There's a whole host of techniques for implementing systems that concurrently interact with peripherals while maintaining a high degree of responsiveness (e.g. interrupt handling, cooperative multitasking, event queues, etc.). We'll explore some of these in later chapters.
6868

69+
There is a good introduction to concurrency in an embedded context [here] that
70+
you might read through before proceeding.
71+
72+
[here]: https://docs.rust-embedded.org/book/concurrency/index.html
73+
74+
6975
For now, let's take a deeper look into what's happening when we call `button_a.is_low()` or `display_pins.row1.set_high()`.

mdbook/src/15-interrupts/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ microbit-v2 = "0.15"
1111
panic-rtt-target = "0.1"
1212
rtt-target = "0.5"
1313

14+
1415
[dependencies.cortex-m]
1516
version = "0.7"
1617
features = ["critical-section-single-core"]

mdbook/src/15-interrupts/debouncing.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ interrupt handler sees that the timer is running, it can just do nothing.
2323
The implementation of all this can be seen in the next example (`examples/count-debounce.rs`). When
2424
you run the example you should see one count per button press.
2525

26+
```rust
27+
{{#include examples/count-debounce.rs}}
28+
```
29+
2630
> **NOTE** The buttons on the MB2 are a little fiddly: it's pretty easy to push one down enough to
2731
feel a "click" but not enough to actually make contact with the switch. I recommend using a
2832
fingernail to press the button when testing.

mdbook/src/15-interrupts/examples/count-debounce.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#![no_main]
22
#![no_std]
33

4-
use core::sync::atomic::{AtomicUsize, Ordering::{Acquire, AcqRel}};
4+
use core::sync::atomic::{
5+
AtomicUsize,
6+
Ordering::{AcqRel, Acquire},
7+
};
58

69
use cortex_m::asm;
710
use cortex_m_rt::entry;
@@ -10,12 +13,11 @@ use panic_rtt_target as _;
1013
use rtt_target::{rprintln, rtt_init_print};
1114

1215
use microbit::{
13-
Board,
1416
hal::{
15-
self,
16-
gpiote,
17+
self, gpiote,
1718
pac::{self, interrupt},
1819
},
20+
Board,
1921
};
2022

2123
static COUNTER: AtomicUsize = AtomicUsize::new(0);

mdbook/src/15-interrupts/my-solution.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

mdbook/src/15-interrupts/turn-signaller-revisited.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

mdbook/src/15-interrupts/waiting-for-an-interrupt.md

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Waiting for an interrupt (wfi, wfe, nop)
2+
3+
You may have wondered why we have been using `asm::wfi()` (wait for instruction) in our main loop instead of
4+
something like `asm::nop()`.
5+
6+
As discussed before, `asm::nop()` means no-op(eration), and is an instruction that the CPU executes without doing anything . We definitely could have used `asm::nop()` in our main loop instead, and the program would have behaved the same way. The microcontroller, on the other hand, would behave differently.
7+
8+
Calling `asm::wfi()` puts the CPU into wfi mode. When the CPU is in wfi mode, it will sleep until an interrupt wakes it up. During sleep, the CPU will stop fetching instructions, turn off clocks and peripherals, and enter a low-power state, but still keep the core running. When an interrupt occurs, the CPU will wake up and execute as normal.
9+
10+
The main difference between `asm::wfi()` and `asm::nop()` is that the NOP instruction is still an instruction. It still needs to be fetched from the program memory and be executed even though the execution doesn't do anything. Most microcontrollers you'll find out there have a low-power mode (some even have several, each with varying things staying on and each with different power consumption characteristics) that can, and *should* in a lot of cases, be used to save power.
11+
12+
You'll find some interrupt-driven programs that consist of nothing but `asm::wfi()` in the main loop, with all program logic being implemented in the interrupt handlers.
13+
14+

mdbook/src/16-snake-game/README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ game that you can play on an MB2 using its 5×5 LED matrix as a display and its
55
controls. In doing so, we will build on some of the concepts covered in the earlier chapters of this
66
book, and also learn about some new peripherals and concepts.
77

8-
In particular, we will be using the concept of hardware interrupts to allow our program to interact
9-
with multiple peripherals at once. Interrupts are a common way to implement concurrency in embedded
10-
contexts. There is a good introduction to concurrency in an embedded context [here] that
11-
you might read through before proceeding.
12-
13-
[here]: https://docs.rust-embedded.org/book/concurrency/index.html
14-
158
## Modularity
169

1710
The source code here is more modular than it probably should be. This fine-grained modularity allows

mdbook/src/SUMMARY.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@
6666
- [NVIC and interrupt priority](15-interrupts/nvic-and-interrupt-priority.md)
6767
- [Sharing data with globals](15-interrupts/sharing-data-with-globals.md)
6868
- [Debouncing](15-interrupts/debouncing.md)
69-
- [Waiting for an interrupt (wfi, wfe, nop)](15-interrupts/waiting-for-an-interrupt.md)
70-
- [Turn signaller revisited](15-interrupts/turn-signaller-revisited.md)
71-
- [My solution](15-interrupts/my-solution.md)
72-
- [Concurrency](15-interrupts/concurrency.md)
69+
- [Waiting to be interrupted](15-interrupts/waiting-to-be-interrupted.md)
7370
- [Snake game](16-snake-game/README.md)
7471
- [Game logic](16-snake-game/game-logic.md)
7572
- [Controls](16-snake-game/controls.md)

0 commit comments

Comments
 (0)