Skip to content

Commit 4d68e13

Browse files
bors[bot]hargoniX
andauthored
Merge #271
271: micro:bit: chapter 04; 05 r=eldruin a=hargoniX A rewrite of chapter 04 and partially chapter 05. I'm already uploading this now since the cargo-embed GDB stub is blowing up on my machine when trying to debug this chip so I'll probably have to spend some time debugging, however during that time my current work can already be reviewed so feel free to take a look at it! Co-authored-by: Henrik Böving <hargonix@gmail.com>
2 parents f8b0b5d + bf70e0c commit 4d68e13

25 files changed

+625
-728
lines changed

src/03-setup/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ should work but we have listed the version we have tested.
2929

3030
[`cargo-binutils`]: https://github.com/rust-embedded/cargo-binutils
3131

32-
- [`cargo-embed`]. Version 0.9.0 or newer.
32+
- [`cargo-embed`]. Version 0.9.1 or newer.
3333

3434
[`cargo-embed`]: https://github.com/probe-rs/cargo-embed
3535

src/04-meet-your-hardware/README.md

Lines changed: 69 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,40 @@
22

33
Let's get familiar with the hardware we'll be working with.
44

5-
## STM32F3DISCOVERY (the "F3")
5+
## micro:bit
66

77
<p align="center">
8-
<img title="F3" src="../assets/f3.jpg">
8+
<img title="micro:bit" src="../assets/microbit.jpg">
99
</p>
1010

11-
We'll refer to this board as "F3" throughout this book. Here are some of the
12-
many components on the board:
11+
Here are some of the many components on the board:
1312

1413
- A [microcontroller].
15-
- A number of LEDs, including the eight aligned in a "compass" formation.
16-
- Two buttons.
17-
- Two USB ports.
18-
- An [accelerometer].
19-
- A [magnetometer].
20-
- A [gyroscope].
14+
- A number of LEDs, most notably the LED matrix on the back
15+
- Two user buttons as well as a reset button (the one next to the USB port).
16+
- One USB port.
17+
- A sensor that is both a [magnetometer] and an [accelerometer]
2118

2219
[microcontroller]: https://en.wikipedia.org/wiki/Microcontroller
2320
[accelerometer]: https://en.wikipedia.org/wiki/Accelerometer
2421
[magnetometer]: https://en.wikipedia.org/wiki/Magnetometer
2522
[gyroscope]: https://en.wikipedia.org/wiki/Gyroscope
2623

2724
Of these components, the most important is the microcontroller (sometimes
28-
shortened to "MCU" for "microcontroller unit"), which is the large black square
29-
sitting in the center of your board. The MCU is what runs your code. You might
30-
sometimes read about "programming a board", when in reality what we are doing
31-
is programming the MCU that is installed on the board.
25+
shortened to "MCU" for "microcontroller unit"), which is the bigger of the two
26+
black squares sitting on the side of the board with the USB port. The MCU is
27+
what runs your code. You might sometimes read about "programming a board", when
28+
in reality what we are doing is programming the MCU that is installed on the board.
3229

33-
## STM32F303VCT6 (the "STM32F3")
30+
If you happen to be interested in a more in detail description of the board you
31+
can checkout the [micro:bit website](https://tech.microbit.org/hardware/).
32+
33+
## Nordic nRF51822 (the "nRF51")
3434

3535
Since the MCU is so important, let's take a closer look at the one sitting on our board.
3636

37-
Our MCU is surrounded by 100 tiny metal **pins**. These pins are connected to
38-
**traces**, the little "roads" that act as the wires connecting components
37+
Our MCU has 48 tiny metal **pins** sitting right underneath it (it's a so called [QFN48] chip).
38+
These pins are connected to **traces**, the little "roads" that act as the wires connecting components
3939
together on the board. The MCU can dynamically alter the electrical properties
4040
of the pins. This works similar to a light switch altering how electrical
4141
current flows through a circuit. By enabling or disabling electrical current to
@@ -44,68 +44,57 @@ be turned on and off.
4444

4545
Each manufacturer uses a different part numbering scheme, but many will allow
4646
you to determine information about a component simply by looking at the part
47-
number. Looking at our MCU's part number (`STM32F303VCT6`), the `ST` at the
48-
front hints to us that this is a part manufactured by [ST Microelectronics].
49-
Searching through [ST's marketing materials] we can also learn the following:
50-
51-
[ST Microelectronics]: https://st.com/
52-
[ST's marketing materials]: https://www.st.com/en/microcontrollers-microprocessors/stm32-mainstream-mcus.html
53-
54-
- The `M32` represents that this is an Arm®-based 32-bit microcontroller.
55-
- The `F3` represents that the MCU is from ST's "STM32F3" series. This is a
56-
series of MCUs based on the Cortex®-M4 processor design.
57-
- The remainder of the part number goes into more details about things like
58-
extra features and RAM size, which at this point we're less concerned about.
59-
60-
> ### Arm? Cortex-M4?
61-
>
62-
> If our chip is manufactured by ST, then who is Arm? And if our chip is the
63-
> STM32F3, what is the Cortex-M4?
64-
>
65-
> You might be surprised to hear that while "Arm-based" chips are quite
66-
> popular, the company behind the "Arm" trademark ([Arm Holdings][]) doesn't
67-
> actually manufacture chips for purchase. Instead, their primary business
68-
> model is to just *design* parts of chips. They will then license those designs to
69-
> manufacturers, who will in turn implement the designs (perhaps with some of
70-
> their own tweaks) in the form of physical hardware that can then be sold.
71-
> Arm's strategy here is different from companies like Intel, which both
72-
> designs *and* manufactures their chips.
73-
>
74-
> Arm licenses a bunch of different designs. Their "Cortex-M" family of designs
75-
> are mainly used as the core in microcontrollers. For example, the Cortex-M0
76-
> is designed for low cost and low power usage. The Cortex-M7 is higher cost,
77-
> but with more features and performance. The core of our STM32F3 is based on
78-
> the Cortex-M4, which is in the middle: more features and performance than the
79-
> Cortex-M0, but less expensive than the Cortex-M7.
80-
>
81-
> Luckily, you don't need to know too much about different types of processors
82-
> or Cortex designs for the sake of this book. However, you are hopefully now a
83-
> bit more knowledgeable about the terminology of your device. While you are
84-
> working specifically with an STM32F3, you might find yourself reading
85-
> documentation and using tools for Cortex-M-based chips, as the STM32F3 is
86-
> based on a Cortex-M design.
47+
number. Looking at our MCU's part number (`nRF51822-QFAA-R`, you probably cannot
48+
see it with your bare eye, but it is on the chip), the `n` at the
49+
front hints to us that this is a part manufactured by [Nordic Semiconductor].
50+
Looking up the part number on their website we quickly find the [product page].
51+
There we learn that our chip's main marketing point is that it is a
52+
"Bluetooth Low Energy and 2.4 GHz SoC" (SoC being short for "System on a Chip"),
53+
which explains the RF in the product name since RF is short for radio frequency.
54+
If we search through the documentation of the chip linked on the [product page]
55+
for a bit we find the [product specification] which contains chapter 10 "Ordering Information"
56+
dedicated to explaining the weird chip naming. Here we learn that:
57+
58+
[QFN48]: https://en.wikipedia.org/wiki/Flat_no-leads_package
59+
[Nordic Semiconductor]: https://www.nordicsemi.com/
60+
[product page]: https://www.nordicsemi.com/Products/Low-power-short-range-wireless/nRF51822
61+
[product specification]: https://infocenter.nordicsemi.com/pdf/nRF51822_PS_v3.3.pdf
62+
63+
- The `nRF51` is the MCU's series, indicating that there are other `nRF51` MCUs
64+
- The `822` is the part code
65+
- The `QF` is short for `QFN48`
66+
- The `AA` is the variant code, indicating how much RAM and flash memory the MCU has,
67+
in our case 256 kilobyte flash and 16 kilobyte RAM
68+
- The `R` is the packaging code which is relevant for factories manufacturing boards
69+
with this chip on them in larger scales
70+
71+
The product specification does of course contain a lot more useful information about
72+
the chip, for example that it is based on an ARM® Cortex™-M0 32 bit processor.
73+
74+
### Arm? Cortex-M0?
75+
76+
If our chip is manufactured by Nordic, then who is Arm? And if our chip is the
77+
nRF51822, what is the Cortex-M0?
78+
79+
You might be surprised to hear that while "Arm-based" chips are quite
80+
popular, the company behind the "Arm" trademark ([Arm Holdings][]) doesn't
81+
actually manufacture chips for purchase. Instead, their primary business
82+
model is to just *design* parts of chips. They will then license those designs to
83+
manufacturers, who will in turn implement the designs (perhaps with some of
84+
their own tweaks) in the form of physical hardware that can then be sold.
85+
Arm's strategy here is different from companies like Intel, which both
86+
designs *and* manufactures their chips.
87+
88+
Arm licenses a bunch of different designs. Their "Cortex-M" family of designs
89+
are mainly used as the core in microcontrollers. For example, the Cortex-M0
90+
(the core our chip is based on) is designed for low cost and low power usage.
91+
The Cortex-M7 is higher cost, but with more features and performance.
92+
93+
Luckily, you don't need to know too much about different types of processors
94+
or Cortex designs for the sake of this book. However, you are hopefully now a
95+
bit more knowledgeable about the terminology of your device. While you are
96+
working specifically with an nRF51822, you might find yourself reading
97+
documentation and using tools for Cortex-M-based chips, as the nRF51822 is
98+
based on a Cortex-M design.
8799

88100
[Arm Holdings]: https://www.arm.com/
89-
90-
## The Serial module
91-
92-
<p align="center">
93-
<img title="Serial module" src="../assets/serial.jpg">
94-
</p>
95-
96-
If you have an older revision of the discovery board, you can use this module to
97-
exchange data between the microcontroller in the F3 and your computer. This module
98-
will be connected to your computer using an USB cable. I won't say more at this
99-
point.
100-
101-
If you have a newer release of the board then you don't need this module. The
102-
ST-LINK will double as a USB<->serial converter connected to the microcontroller USART1 at pins PC4 and PC5.
103-
104-
## The Bluetooth module
105-
106-
<p align="center">
107-
<img title="The HC-05 Bluetooth module" src="../assets/bluetooth.jpg">
108-
</p>
109-
110-
This module has the exact same purpose as the serial module but it sends the data over Bluetooth
111-
instead of over USB.

src/05-led-roulette/.cargo/config

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
[target.thumbv7em-none-eabihf]
2-
runner = "arm-none-eabi-gdb -q"
1+
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
32
rustflags = [
43
"-C", "link-arg=-Tlink.x",
54
]
5+
6+
[build]
7+
target = "thumbv6m-none-eabi"

src/05-led-roulette/Cargo.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
[package]
2-
authors = ["Jorge Aparicio <jorge@japaric.io>"]
3-
edition = "2018"
42
name = "led-roulette"
53
version = "0.1.0"
4+
authors = ["Henrik Böving <hargonix@gmail.com>"]
5+
edition = "2018"
66

77
[dependencies]
8-
aux5 = { path = "auxiliary" }
8+
cortex-m = "0.6.0"
9+
cortex-m-rt = "0.6.10"
10+
panic-halt = "0.2.0"
11+
nrf51-hal = "0.11.0"
12+
rtt-target = { version = "0.2.2", features = ["cortex-m"] }
13+
panic-rtt-target = { version = "0.1.1", features = ["cortex-m"] }

src/05-led-roulette/Embed.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[default.general]
2+
chip = "nrf51822_xxAA"
3+
4+
[default.reset]
5+
halt_afterwards = true
6+
7+
[default.rtt]
8+
enabled = false
9+
10+
[default.gdb]
11+
enabled = true

src/05-led-roulette/README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,17 @@
33
Alright, let's start by building the following application:
44

55
<p align="center">
6-
<img src="https://i.imgur.com/0k1r2Lc.gif">
6+
<video src="../assets/roulette_fast.mp4" loop autoplay>
77
</p>
88

99
I'm going to give you a high level API to implement this app but don't worry we'll do low level
1010
stuff later on. The main goal of this chapter is to get familiar with the *flashing* and debugging
1111
process.
1212

13-
Throughout this text we'll be using the starter code that's in the [discovery] repository. Make sure
14-
you always have the latest version of the master branch because this website tracks that branch.
15-
1613
The starter code is in the `src` directory of that repository. Inside that directory there are more
1714
directories named after each chapter of this book. Most of those directories are starter Cargo
1815
projects.
1916

20-
[discovery]: https://github.com/rust-embedded/discovery
21-
2217
Now, jump into the `src/05-led-roulette` directory. Check the `src/main.rs` file:
2318

2419
``` rust
@@ -46,4 +41,18 @@ as well. This directory contains a Cargo configuration file (`.cargo/config`) th
4641
linking process to tailor the memory layout of the program to the requirements of the target device.
4742
This modified linking process is a requirement of the `cortex-m-rt` crate.
4843

44+
Furthermore there is also an `Embed.toml` file
45+
46+
```toml
47+
{{#include Embed.toml}}
48+
```
49+
50+
This file tells `cargo-embed` that:
51+
52+
* we are working with a nrf51822,
53+
* we want to halt the chip after we flashed it so our program does not instantly jump to the loop
54+
* we want to disable RTT, RTT being a protocol that allows the chip to send text to a debugger.
55+
You have in fact already seen RTT in action, it was the protocol that sent "Hello World" in chapter 3.
56+
* we want to enable GDB, this will be required for the debugging procedure
57+
4958
Alright, let's start by building this program.

src/05-led-roulette/auxiliary/Cargo.toml

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/05-led-roulette/auxiliary/src/lib.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)