Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Update the microbit part of the book #575

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions microbit/src/02-requirements/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
The primary knowledge requirement to read this book is to know *some* Rust. It's
hard for me to quantify *some* but at least I can tell you that you don't need
to fully grok generics, but you do need to know how to *use* closures. You also
need to be familiar with the idioms of the [2018 edition], in particular with
need to be familiar with the idioms of the [2021 edition], in particular with
the fact that `extern crate` is not necessary in the 2018 edition.

[2018 edition]: https://rust-lang-nursery.github.io/edition-guide/
[2021 edition]: https://rust-lang-nursery.github.io/edition-guide/

Also, to follow this material you'll need the following hardware:

Expand Down
10 changes: 5 additions & 5 deletions microbit/src/03-setup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
name = "rtt-check"
version = "0.1.0"
authors = ["Henrik Böving <hargonix@gmail.com>"]
edition = "2018"
edition = "2021"

[dependencies]
cortex-m = "0.7.3"
cortex-m-rt = "0.7.0"
rtt-target = { version = "0.3.1", features = ["cortex-m"] }
panic-rtt-target = { version = "0.1.2", features = ["cortex-m"] }
cortex-m = { version = "0.7.7", features = ["critical-section-single-core"] }
cortex-m-rt = "0.7.3"
rtt-target = "0.5.0"
panic-rtt-target = "0.1.3"
4 changes: 2 additions & 2 deletions microbit/src/03-setup/IDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Some IDEs fail to understand the code, because they fail to determine whether a
is defined in the microbit or microbit-v2 codebase. If you fail to get auto-completion to work,
you may want to try to edit the `Cargo.toml` files you encounter through this book, and remove
all references to the version of microbit you are not using. That is:
in the `Cargo.toml` file you must remove the dependency and features you do not use (the part guarded by `#[cfg(feature = "vI")]` and the guard itself)
in the `Cargo.toml` file you must remove the dependency and features you do not use (the part guarded by `#[cfg(feature = "v1")]` and the guard itself)

# IDE configuration

Expand All @@ -28,4 +28,4 @@ When editing the IntelliJ build configuration, here are a few non-default values
You'll need to replace the default value `run` by the command `embed FLAGS`,
* You should enable "Emulate terminal in output console". Otherwise, your program will fail to print text to a terminal
* You should ensure that the working directory is `microbit/src/N-name`, with `N-name` being the directory of the chapter you
are reading. You can not run from the `src` directory since it contains no cargo file.
are reading. You can not run from the `src` directory since it contains no cargo file.
1 change: 1 addition & 0 deletions microbit/src/03-setup/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use panic_rtt_target as _;
use rtt_target::{rtt_init_print, rprintln};

use cortex_m as _;
use cortex_m_rt::entry;

#[entry]
Expand Down
19 changes: 10 additions & 9 deletions microbit/src/05-led-roulette/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@
name = "led-roulette"
version = "0.1.0"
authors = ["Henrik Böving <hargonix@gmail.com>"]
edition = "2018"
edition = "2021"

[dependencies.microbit-v2]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the number corrections (version numbers, runtimes, entry points, sizes, etc) will change quickly as the ecosystem updates. A note to that effect should be put here somewhere, so that we aren't doing this on some regular basis. Probably should just freeze things at some point and make careful notes about the reference point, I guess? I don't know — it's a hard problem.

In a fantasy world, this kind of stuff would be done automatically in CI. But that sounds even harder.

version = "0.12.0"
version = "0.15.0"
optional = true

[dependencies.microbit]
version = "0.12.0"
version = "0.15.0"
optional = true

[dependencies]
cortex-m = "0.7.3"
cortex-m-rt = "0.7.0"
cortex-m = { version = "0.7.7", features = ["critical-section-single-core"] }
cortex-m-rt = "0.7.3"
embedded-hal = "1.0.0"
panic-halt = "0.2.0"
#rtt-target = { version = "0.3.1", features = ["cortex-m"] }
#panic-rtt-target = { version = "0.1.2", features = ["cortex-m"] }
# rtt-target = "0.5.0"
# panic-rtt-target = "0.1.3"

[dev-dependencies]
# Sneak in dependencies for examples which clash with panic-hal when generating
# docs with rustdoc.
rtt-target = { version = "0.3.1", features = ["cortex-m"] }
panic-rtt-target = { version = "0.1.2", features = ["cortex-m"] }
rtt-target = "0.5.0"
panic-rtt-target = "0.1.3"

[features]
v2 = ["microbit-v2"]
Expand Down
32 changes: 16 additions & 16 deletions microbit/src/05-led-roulette/build-it.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ With the `rust-std` component in place you can now cross compile the program usi
# For micro:bit v2
$ cargo build --features v2 --target thumbv7em-none-eabihf
Compiling semver-parser v0.7.0
Compiling typenum v1.12.0
Compiling cortex-m v0.6.3
Compiling proc-macro2 v1.0.85
(...)
Compiling microbit-v2 v0.10.1
Finished dev [unoptimized + debuginfo] target(s) in 33.67s
Compiling microbit-v2 v0.14.0
Finished dev [unoptimized + debuginfo] target(s) in 4.33s

# For micro:bit v1
$ cargo build --features v1 --target thumbv6m-none-eabi
Compiling fixed v1.2.0
Compiling syn v1.0.39
Compiling cortex-m v0.6.3
Compiling semver-parser v0.7.0
Compiling proc-macro2 v1.0.85
Compiling cortex-m v0.7.7
(...)
Compiling microbit v0.10.1
Finished dev [unoptimized + debuginfo] target(s) in 22.73s
Compiling microbit v0.14.0
Finished dev [unoptimized + debuginfo] target(s) in 2.79s
```

> **NOTE** Be sure to compile this crate *without* optimizations. The provided Cargo.toml
Expand All @@ -80,16 +80,16 @@ ELF Header:
Type: EXEC (Executable file)
Machine: ARM
Version: 0x1
Entry point address: 0x117
Entry point address: 0x101
Start of program headers: 52 (bytes into file)
Start of section headers: 793112 (bytes into file)
Start of section headers: 777140 (bytes into file)
Flags: 0x5000400
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 4
Size of section headers: 40 (bytes)
Number of section headers: 21
Section header string table index: 19
Number of section headers: 23
Section header string table index: 21

# For micro:bit v1
# equivalent to `readelf -h target/thumbv6m-none-eabi/debug/led-roulette`
Expand All @@ -105,16 +105,16 @@ ELF Header:
Type: EXEC (Executable file)
Machine: ARM
Version: 0x1
Entry point address: 0xC1
Entry point address: 0xA9
Start of program headers: 52 (bytes into file)
Start of section headers: 693196 (bytes into file)
Start of section headers: 776152 (bytes into file)
Flags: 0x5000200
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 4
Size of section headers: 40 (bytes)
Number of section headers: 22
Section header string table index: 20
Number of section headers: 23
Section header string table index: 21
```

Next, we'll flash the program into our microcontroller.
11 changes: 6 additions & 5 deletions microbit/src/05-led-roulette/debug-it.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,23 @@ $ gdb target/thumbv6m-none-eabi/debug/led-roulette
> as long as it does not crash, you are fine.

Next we will have to connect to the GDB stub. It runs on `localhost:1337` per default so in order to
connect to it run the following:
connect to it run the following on the gdb commandline (`(gdb)`):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit confusing, I think. How about "on the gdb command line (that is, at the (gdb) prompt):"


```shell
(gdb) target remote :1337
Remote debugging using :1337
0x00000116 in nrf52833_pac::{{impl}}::fmt (self=0xd472e165, f=0x3c195ff7) at /home/nix/.cargo/registry/src/github.com-1ecc6299db9ec823/nrf52833-pac-0.9.0/src/lib.rs:157
157 #[derive(Copy, Clone, Debug)]
(...)
0x00000100 in cortex_m::delay::Delay::delay_us (self=0xedbeff37, us=439704628) at src/delay.rs:56
56 self.syst.set_reload(ticks - 1);
```

Next what we want to do is get to the main function of our program.
We will do this by first setting a breakpoint there and the continuing
We will do this by first setting a breakpoint there and then continuing
program execution until we hit the breakpoint:

```
(gdb) break main
Breakpoint 1 at 0x104: file src/05-led-roulette/src/main.rs, line 9.
Breakpoint 1 at 0x15c: file src/05-led-roulette/src/main.rs, line 9.
Note: automatically using hardware breakpoints for read-only addresses.
(gdb) continue
Continuing.
Expand Down
28 changes: 28 additions & 0 deletions microbit/src/05-led-roulette/examples/it-blinks-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![deny(unsafe_code)]
#![no_main]
#![no_std]

use cortex_m_rt::entry;
use embedded_hal::delay::DelayNs;
use rtt_target::{
rtt_init_print,
rprintln,
};
use panic_rtt_target as _;
use microbit::board::Board;
use microbit::hal::timer::Timer;

#[entry]
fn main() -> ! {
rtt_init_print!();

let board = Board::take().unwrap();

let mut timer = Timer::new(board.TIMER0);

loop {
timer.delay_ms(1_000u32);
rprintln!("1000 ms passed");
}
}

38 changes: 38 additions & 0 deletions microbit/src/05-led-roulette/examples/it-blinks-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#![deny(unsafe_code)]
#![no_main]
#![no_std]

use cortex_m_rt::entry;
use rtt_target::{
rtt_init_print,
rprintln,
};
use panic_rtt_target as _;
use embedded_hal::{
delay::DelayNs,
digital::OutputPin,
};
use microbit::board::Board;
use microbit::hal::timer::Timer;

#[entry]
fn main() -> ! {
rtt_init_print!();

let mut board = Board::take().unwrap();

let mut timer = Timer::new(board.TIMER0);

board.display_pins.col1.set_low().unwrap();
let mut row1 = board.display_pins.row1;

loop {
row1.set_low().unwrap();
rprintln!("Dark!");
timer.delay_ms(1_000_u32);

row1.set_high().unwrap();
rprintln!("Light!");
timer.delay_ms(1_000_u32);
}
}
18 changes: 18 additions & 0 deletions microbit/src/05-led-roulette/examples/light-it-up.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![deny(unsafe_code)]
#![no_main]
#![no_std]

use cortex_m_rt::entry;
use embedded_hal::digital::OutputPin;
use panic_halt as _;
use microbit::board::Board;

#[entry]
fn main() -> ! {
let mut board = Board::take().unwrap();

board.display_pins.col1.set_low().unwrap();
board.display_pins.row1.set_high().unwrap();

loop {}
}
7 changes: 7 additions & 0 deletions microbit/src/05-led-roulette/examples/my-solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ fn main() -> ! {
rtt_init_print!();

let board = Board::take().unwrap();

let mut timer = Timer::new(board.TIMER0);
let mut display = Display::new(board.display_pins);

// Setup the display delay so the math works as expected later.
display.set_delay_ms(1);

let mut leds = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
Expand All @@ -37,7 +42,9 @@ fn main() -> ! {
for current_led in PIXELS.iter() {
leds[last_led.0][last_led.1] = 0;
leds[current_led.0][current_led.1] = 1;

display.show(&mut timer, leds, 30);

last_led = *current_led;
}
}
Expand Down
45 changes: 45 additions & 0 deletions microbit/src/05-led-roulette/examples/the-challenge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#![deny(unsafe_code)]
#![no_main]
#![no_std]

use cortex_m_rt::entry;
use rtt_target::rtt_init_print;
use panic_rtt_target as _;
use embedded_hal::delay::DelayNs;
use microbit::{
board::Board,
display::blocking::Display,
hal::Timer,
};

#[entry]
fn main() -> ! {
rtt_init_print!();

let board = Board::take().unwrap();

let mut timer = Timer::new(board.TIMER0);
let mut display = Display::new(board.display_pins);

// Setup the display delay so the math works as expected later.
display.set_delay_ms(1);

let light_it_all = [
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
];

loop {
// Show light_it_all for 1000ms
display.show(&mut timer, light_it_all, 1_000);

// clear the display again
display.clear();

timer.delay_ms(1000_u32);
}
}

Loading
Loading