Skip to content

Commit

Permalink
removed micro:bit v1
Browse files Browse the repository at this point in the history
  • Loading branch information
BartMassey committed Jun 26, 2024
1 parent e640858 commit 303192e
Show file tree
Hide file tree
Showing 54 changed files with 118 additions and 590 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[workspace]
members = [
"src/03-setup",
"src/05-led-roulette",
"src/07-uart",
"src/08-i2c",
"src/09-led-compass",
"src/10-punch-o-meter",
"src/11-snake-game",
"mdbook/src/03-setup",
"mdbook/src/05-led-roulette",
"mdbook/src/07-uart",
"mdbook/src/08-i2c",
"mdbook/src/09-led-compass",
"mdbook/src/10-punch-o-meter",
"mdbook/src/11-snake-game",
]

[profile.release]
Expand Down
28 changes: 12 additions & 16 deletions mdbook/src/02-requirements/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,36 @@
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
the fact that `extern crate` is not necessary in the 2018 edition.
need to be familiar with the idioms of the current Rust [edition].

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

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

- A [micro:bit v2] board, alternatively a [micro:bit v1.5] board, the book
will refer to the v1.5 as just v1.
- A [Micro:Bit v2] (MB2) board.

[micro:bit v2]: https://tech.microbit.org/hardware/
[micro:bit v1.5]: https://tech.microbit.org/hardware/1-5-revision/

(You can purchase this board from several [electronics][0] [suppliers][1])
You can purchase this board from many suppliers, including
Amazon and Ali Baba. You can get a [list][0] of suppliers
directly from the BBC, the manufacturers of MB2.

[0]: https://microbit.org/buy/
[1]: https://www.mouser.com/microbit/_/N-aez3t?P=1y8um0l

<p align="center">
<img title="micro:bit" src="../assets/microbit-v2.jpg">
</p>

> **NOTE** This is an image of a micro:bit v2, the front of the v1 looks slightly different
- One micro-B USB cable, required to make the micro:bit board work.
Make sure that the cable supports data transfer as some cables only support charging devices.
- One micro-B USB cable (nothing special — you probably have many of these). This is required
to power the micro:bit board when not on battery, and to communicate with it. Make sure
that the cable supports data transfer, as some cables only support charging devices.

<p align="center">
<img title="micro-B USB cable" src="../assets/usb-cable.jpg">
</p>

> **NOTE** You may already have a cable like this, as some micro:bit kits ship with such cables.
> Some USB cables used to charge mobile devices may also work, if they are micro-B and have the
> capability to transmit data.
> **NOTE** Some micro:bit kits ship with such cables. USB cables used with other mobile
> devices should work, if they are micro-B and have the capability to transmit data.
> **FAQ**: Wait, why do I need this specific hardware?
Expand All @@ -56,7 +52,7 @@ if you intend to use a different one.
[Awesome Embedded Rust HAL list]: https://github.com/rust-embedded/awesome-embedded-rust#hal-implementation-crates

With a different development board, this text would lose most if not all its beginner friendliness
and "easy to follow"-ness, IMO.
and "easy to follow"-ness, in my opinion.

If you have a different development board and you don't consider yourself a total beginner, you are
better off starting with the [quickstart] project template.
Expand Down
3 changes: 1 addition & 2 deletions mdbook/src/03-setup/Embed.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
protocol = "Swd"

[default.general]
# chip = "nrf52833_xxAA" # uncomment this line for micro:bit V2
# chip = "nrf51822_xxAA" # uncomment this line for micro:bit V1
chip = "nrf52833_xxAA"

[default.rtt]
enabled = true
Expand Down
10 changes: 1 addition & 9 deletions mdbook/src/03-setup/IDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ However, you may have your favourite IDEs, providing you auto-complete, type ann
your preferred shortcuts and much more. This section explains how to get the most out
of your IDE using the code obtained from this book's repo.

# Auto-completion, type annotation, and more

Some IDEs fail to understand the code, because they fail to determine whether a term
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)

# IDE configuration

Below, we explain how to configure your IDE to get the most out of this book.
Expand All @@ -28,4 +20,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.
18 changes: 0 additions & 18 deletions mdbook/src/03-setup/verify.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,30 +101,12 @@ Debugging Xtensa targets over SWD is not supported. For these targets, JTAG is t

```

Next up you will have to modify `Embed.toml` in the `src/03-setup` directory of the
book's source code. In the `default.general` section you will find two commented out
chip variants:

```toml
[default.general]
# chip = "nrf52833_xxAA" # uncomment this line for micro:bit V2
# chip = "nrf51822_xxAA" # uncomment this line for micro:bit V1
```

If you are working with the micro:bit v2 board uncomment the first, for the v1
uncomment the second line.

Next run one of these commands:

```
$ # make sure you are in src/03-setup of the books source code
$ # If you are working with micro:bit v2
$ rustup target add thumbv7em-none-eabihf
$ cargo embed --target thumbv7em-none-eabihf
$ # If you are working with micro:bit v1
$ rustup target add thumbv6m-none-eabi
$ cargo embed --target thumbv6m-none-eabi
```

If everything works correctly cargo-embed should first compile the small example program
Expand Down
2 changes: 0 additions & 2 deletions mdbook/src/04-meet-your-hardware/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,3 @@ If you happen to be interested in a more in detail description of the board you
can checkout the [micro:bit website](https://tech.microbit.org/hardware/).

Since the MCU is so important, let's take a closer look at the one sitting on our board.
Note that only one of the following two sections applies to your board, depending on whether
you are working with a micro:bit v2 or v1.
24 changes: 12 additions & 12 deletions mdbook/src/04-meet-your-hardware/microbit-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ current flows through a circuit. By enabling or disabling electrical current to
flow through a specific pin, an LED attached to that pin (via the traces) can
be turned on and off.

Each manufacturer uses a different part numbering scheme, but many will allow
you to determine information about a component simply by looking at the part
number. Looking at our MCU's part number (`N52833 QIAAA0 2024AL`, you probably cannot
see it with your bare eye, but it is on the chip), the `n` at the
front hints to us that this is a part manufactured by [Nordic Semiconductor].
Looking up the part number on their website we quickly find the [product page].
There we learn that our chip's main marketing point is that it is a
"Bluetooth Low Energy and 2.4 GHz SoC" (SoC being short for "System on a Chip"),
which explains the RF in the product name since RF is short for radio frequency.
If we search through the documentation of the chip linked on the [product page]
for a bit we find the [product specification] which contains chapter 10 "Ordering Information"
dedicated to explaining the weird chip naming. Here we learn that:
Each manufacturer uses a different part numbering scheme, but many will allow you to
determine information about a component simply by looking at the part number. Looking at our
MCU's part number we find `N52833 QIAAA0 2024AL`: you probably cannot see it with your bare
eye, but it is on the chip. (If you have a later revision of MB2, your number may vary
somewhat. This not an issue. The `N52833` part should be there, though.) The `N` at the
front hints to us that this is a part manufactured by [Nordic Semiconductor]. Looking up
the part number on their website we quickly find the [product page]. There we learn that
our chip's main marketing point is that it is a "Bluetooth Low Energy and 2.4 GHz SoC" (SoC
being short for "System on a Chip"), which explains the RF in the product name since RF is
short for radio frequency. If we search through the documentation of the chip linked on the
[product page] for a bit we find the [product specification] which contains chapter 10
"Ordering Information" dedicated to explaining the weird chip naming. Here we learn that:

[aQFN73]: https://en.wikipedia.org/wiki/Flat_no-leads_package
[Nordic Semiconductor]: https://www.nordicsemi.com/
Expand Down
4 changes: 1 addition & 3 deletions mdbook/src/04-meet-your-hardware/terminology.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ does actually have a [BSP] so we are going to use that on top of our
HAL as well.

[nrF52]: https://crates.io/crates/nrf52833-pac
[nrF51]: https://crates.io/crates/nrf51
[nrF52-hal]: https://crates.io/crates/nrf52833-hal
[nrF51-hal]: https://crates.io/crates/nrf51-hal
[BSP]: https://crates.io/crates/microbit
[BSP]: https://crates.io/crates/microbit-v2

## Unifying the layers

Expand Down
13 changes: 1 addition & 12 deletions mdbook/src/05-led-roulette/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@ version = "0.1.0"
authors = ["Henrik Böving <hargonix@gmail.com>"]
edition = "2018"

[dependencies.microbit-v2]
version = "0.12.0"
optional = true

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

[dependencies]
cortex-m = "0.7.3"
cortex-m-rt = "0.7.0"
panic-halt = "0.2.0"
microbit-v2 = "0.12.0"
#rtt-target = { version = "0.3.1", features = ["cortex-m"] }
#panic-rtt-target = { version = "0.1.2", features = ["cortex-m"] }

Expand All @@ -24,7 +17,3 @@ panic-halt = "0.2.0"
# docs with rustdoc.
rtt-target = { version = "0.3.1", features = ["cortex-m"] }
panic-rtt-target = { version = "0.1.2", features = ["cortex-m"] }

[features]
v2 = ["microbit-v2"]
v1 = ["microbit"]
3 changes: 1 addition & 2 deletions mdbook/src/05-led-roulette/Embed.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[default.general]
# chip = "nrf52833_xxAA" # uncomment this line for micro:bit V2
# chip = "nrf51822_xxAA" # uncomment this line for micro:bit V1
chip = "nrf52833_xxAA"

[default.reset]
halt_afterwards = true
Expand Down
3 changes: 1 addition & 2 deletions mdbook/src/05-led-roulette/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ Furthermore, there is also an `Embed.toml` file

This file tells `cargo-embed` that:

* we are working with either a nrf52833 or nrf51822, you will again have to remove the comments from the
chip you are using, just like you did in chapter 3.
* we are working with an NRF52833.
* we want to halt the chip after we flashed it so our program does not instantly jump to the loop
* we want to disable RTT, RTT being a protocol that allows the chip to send text to a debugger.
You have in fact already seen RTT in action, it was the protocol that sent "Hello World" in chapter 3.
Expand Down
53 changes: 11 additions & 42 deletions mdbook/src/05-led-roulette/build-it.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ families within that architecture:
- `thumbv8m.main-none-eabi`, for the Cortex-M33 and Cortex-M35P processors
- `thumbv8m.main-none-eabihf`, for the Cortex-M33**F** and Cortex-M35P**F** processors

For the micro:bit v2, we'll use the `thumbv7em-none-eabihf` target, for v1 the `thumbv6m-none-eabi` one.
(The `hf`/`F` parts have hardware floating point acceleration. This will make
numeric computations involving fractional ("floating decimal point") computations
much faster.)

For the micro:bit v2, we'll want the `thumbv7em-none-eabihf` target.

Before cross-compiling you have to download a pre-compiled version of the standard library
(a reduced version of it, actually) for your target. That's done using `rustup`:

``` console
# For micro:bit v2
$ rustup target add thumbv7em-none-eabihf
# For micro:bit v1
$ rustup target add thumbv6m-none-eabi
```

You only need to do the above step once; `rustup` will re-install a new standard library
Expand All @@ -39,23 +41,13 @@ With the `rust-std` component in place you can now cross compile the program usi
``` console
# make sure you are in the `src/05-led-roulette` directory

# For micro:bit v2
$ cargo build --features v2 --target thumbv7em-none-eabihf
$ cargo build --target thumbv7em-none-eabihf
Compiling semver-parser v0.7.0
Compiling typenum v1.12.0
Compiling cortex-m v0.6.3
(...)
Compiling microbit-v2 v0.10.1
Finished dev [unoptimized + debuginfo] target(s) in 33.67s

# 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 microbit v0.10.1
Finished dev [unoptimized + debuginfo] target(s) in 22.73s
```

> **NOTE** Be sure to compile this crate *without* optimizations. The provided Cargo.toml
Expand All @@ -66,9 +58,8 @@ it's just a simplified version that we will build upon later in the chapter.
As a sanity check, let's verify that the produced executable is actually an ARM binary:

``` console
# For micro:bit v2
# equivalent to `readelf -h target/thumbv7em-none-eabihf/debug/led-roulette`
$ cargo readobj --features v2 --target thumbv7em-none-eabihf --bin led-roulette -- --file-headers
$ cargo readobj --target thumbv7em-none-eabihf --bin led-roulette -- --file-headers
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Expand All @@ -90,31 +81,9 @@ ELF Header:
Size of section headers: 40 (bytes)
Number of section headers: 21
Section header string table index: 19

# For micro:bit v1
# equivalent to `readelf -h target/thumbv6m-none-eabi/debug/led-roulette`
$ cargo readobj --features v1 --target thumbv6m-none-eabi --bin led-roulette -- --file-headers
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: ARM
Version: 0x1
Entry point address: 0xC1
Start of program headers: 52 (bytes into file)
Start of section headers: 693196 (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
```

If your numbers don't exactly match these, don't worry: a lot of this is quite dependent
on the current build environment.

Next, we'll flash the program into our microcontroller.
4 changes: 0 additions & 4 deletions mdbook/src/05-led-roulette/debug-it.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ Since `cargo-embed` is blocking our current shell we can simply open a new one a
directory. Once we are there we first have to open the binary in gdb like this:

```shell
# For micro:bit v2
$ gdb target/thumbv7em-none-eabihf/debug/led-roulette

# For micro:bit v1
$ gdb target/thumbv6m-none-eabi/debug/led-roulette
```

> **NOTE**: Depending on which GDB you installed you will have to use a different command to launch it,
Expand Down
23 changes: 9 additions & 14 deletions mdbook/src/05-led-roulette/flash-it.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ nothing. `led-roulette` has full control over the device.
Flashing the binary itself is quite simple thanks to `cargo embed`.

Before executing that command though, let's look into what it actually does. If you look at the side of your micro:bit
with the USB connector facing upwards you will notice, that there are actually 2 black squares on there
(on the micro:bit v2 there is a third and biggest one, which is a speaker), one is our MCU
we already talked about but what purpose does the other one serve? The other chip has 3 main purposes:

1. Provide power from the USB connector to our MCU
with the USB connector facing upwards, you will notice that
there are actually three black squares on there. The biggest
one is a speaker. Another is our MCU
we already talked about… but what purpose does the remaining
one serve? This chip is another MCU, an NRF52820 almost as powerful as
the NRF52833 we will be programming! This chip has three main purposes:

1. Enable power and reset control of our NRF52833 MCU from the USB connector
2. Provide a serial to USB bridge for our MCU (we will look into that in a later chapter)
3. Being a programmer/debugger (this is the relevant purpose for now)

Expand All @@ -25,19 +28,11 @@ the MCU, inspect its state via a debugger and other things.
So lets flash it!

```console
# For micro:bit v2
$ cargo embed --features v2 --target thumbv7em-none-eabihf
$ cargo embed --target thumbv7em-none-eabihf
(...)
Erasing sectors ✔ [00:00:00] [####################################################################################################################################################] 2.00KiB/ 2.00KiB @ 4.21KiB/s (eta 0s )
Programming pages ✔ [00:00:00] [####################################################################################################################################################] 2.00KiB/ 2.00KiB @ 2.71KiB/s (eta 0s )
Finished flashing in 0.608s

# For micro:bit v1
$ cargo embed --features v1 --target thumbv6m-none-eabi
(...)
Erasing sectors ✔ [00:00:00] [####################################################################################################################################################] 2.00KiB/ 2.00KiB @ 4.14KiB/s (eta 0s )
Programming pages ✔ [00:00:00] [####################################################################################################################################################] 2.00KiB/ 2.00KiB @ 2.69KiB/s (eta 0s )
Finished flashing in 0.614s
```


Expand Down
6 changes: 3 additions & 3 deletions mdbook/src/05-led-roulette/it-blinks.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ Note that we changed our panic implementation from `panic_halt` to
RTT lines from `Cargo.toml` and comment the `panic-halt` one out,
since Rust only allows one panic implementation at a time.

In order to actually see the prints we have to change `Embed.toml` like this:
In order to actually see the prints we have to change
`Embed.toml` to turn off `gdb` and `halt_afterwards` and turn on `rtt`, like this:
```
[default.general]
# chip = "nrf52833_xxAA" # uncomment this line for micro:bit V2
# chip = "nrf51822_xxAA" # uncomment this line for micro:bit V1
chip = "nrf52833_xxAA"
[default.reset]
halt_afterwards = false
Expand Down
Loading

0 comments on commit 303192e

Please sign in to comment.