Skip to content

Commit dbbdd1c

Browse files
committed
Add support for micro:bit v2
This change is a major rewrite. The crate is split into a multi-crate workspace layed out in a similar way to nrf-rs/nrf-hal. - `microbit-common` almost all the code, feature flagged for the different major board versions (V1 and V2). - `microbit` the V1 board support crate - `microbit-v2` the V2 board support crate - `xtask` cargo xtask crate for running CI accross the different versions. - `examples/*` the examples now need to be crates as they may support both versions Code changes to support micro:bit V2 ------------------------------------ Add features `v1` and `v2`. Originally they were `microbit-v1` and `microbit-v2`, however having the feature name the same as the crate name requires the namespaced-features cargo feature which basically restricts us to nightly. Add microbit v2 support to display module ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This was hard and I am not super happy with the solution. It is much more bit twiddling than I am comfortable with. The issue that made it so difficult is that on the microbit v2 the column pins are on the P0 and P1 ports so all the row wise writes have to be split accross the two ports. Get microbit v2 led examples working ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - Create a new v2 gpio module and conditionally use v1 or v2 - Move NUM_ROWS and NUM_COLS consts into gpio module so they can differ between v1 and v2 - Move degrading the LED pins into the gpio module so it can differ between v1 and v2 The code for the nonblocking display is a nightmare. The columns are split between the two GPIO ports so the cols argument to `display_row_leds` needs to be split between the two ports. This is a bit painful but doable. The bit that is really confusing me is that columns 2 and 3 seem to be swapped. CI changes ---------- I have mostly copied CI from nrf-rs/nrf-hal with some minor differences. I didn't like having to list all the examples in the CI so it derives the features and targets required for each example from the cargo manifest. Copying nrf-hal also meant getting the doc tests building as well which is nice. For the docs I've favoured complexity in the source for the sake of docs that are clearer. Examples changes ---------------- The microbit-v2 crate does not yet support serial so I have chosen to favour probe-run and defmt for all the examples except those that are specifically about serial communication (those now just run on the v1 crate). I have not ported over the examples that I could not get working (receivedcf77 and the magnetometer ones). I plan on implementing the magnetometer for both versions in the near future so examples for that will hopefully come back soon.
1 parent 2da6da9 commit dbbdd1c

File tree

57 files changed

+1446
-913
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1446
-913
lines changed

.cargo/config

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

.cargo/config.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[alias]
2+
xtask = "run --package xtask --"
3+
4+
[target.thumbv6m-none-eabi]
5+
runner = 'probe-run --chip nRF51822_xxAA'
6+
rustflags = [
7+
"-C", "linker=flip-link",
8+
"-C", "link-arg=-Tlink.x",
9+
"-C", "link-arg=-Tdefmt.x",
10+
]
11+
12+
[target.thumbv7em-none-eabihf]
13+
# Execute binary using gdb when calling cargo run
14+
runner = "probe-run --chip nRF52833_xxAA"
15+
# Tweak to the linking process required by the cortex-m-rt crate
16+
rustflags = [
17+
"-C", "linker=flip-link",
18+
"-C", "link-arg=-Tlink.x",
19+
"-C", "link-arg=-Tdefmt.x",
20+
]

.github/workflows/ci.yaml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,10 @@ jobs:
2626
profile: minimal
2727
toolchain: ${{ matrix.rust }}
2828
override: true
29-
target: thumbv6m-none-eabi
3029
components: rustfmt, clippy
3130

3231
- name: rustfmt
3332
run: cargo fmt -- --check
3433

3534
- name: build
36-
run: cargo build --target=thumbv6m-none-eabi
37-
38-
- name: clippy
39-
run: cargo clippy --color=always -- -D warnings
40-
41-
- name: build examples
42-
run: cargo build --examples --target=thumbv6m-none-eabi
35+
run: cargo xtask ci

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Cargo.lock
55
bloat_log*
66
!.gitignore
77
!.github
8+
!.cargo

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add support for micro:bit V2. This is a significant change that splits
13+
this repository into multiple crates.
14+
1015
## [0.9.0] - 2021-04-29
1116

1217
### Added

Cargo.toml

Lines changed: 7 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,8 @@
1-
[package]
2-
edition = "2018"
3-
authors = ["Daniel Egger <[email protected]>", "Michael Droogleever <[email protected]>"]
4-
categories = [
5-
"hardware-support",
6-
"embedded",
7-
"no-std",
1+
[workspace]
2+
members = [
3+
"microbit-common",
4+
"microbit",
5+
"microbit-v2",
6+
"examples/*",
7+
"xtask",
88
]
9-
description = "Board support crate for the BBC Micro:bit"
10-
documentation = "https://docs.rs/microbit"
11-
keywords = [
12-
"arm",
13-
"cortex-m",
14-
"nrf51",
15-
]
16-
license = "0BSD"
17-
name = "microbit"
18-
readme = "README.md"
19-
repository = "https://github.com/therealprof/microbit"
20-
version = "0.9.0"
21-
22-
[dependencies]
23-
cortex-m = "0.6.1"
24-
cortex-m-rt = "0.6.10"
25-
nb = "0.1.2"
26-
nrf51-hal = "0.12.1"
27-
tiny-led-matrix = "1.0.1"
28-
embedded-hal = "0.2.4"
29-
30-
defmt = "0.1.3"
31-
32-
[dev-dependencies]
33-
numtoa = "0.2.3"
34-
dcf77 = "0.1.0"
35-
mag3110 = "0.1.4"
36-
panic-halt = "0.2.0"
37-
cortex-m-rtic = "0.5"
38-
defmt-rtt = "0.1.0"
39-
panic-probe = { version = "0.1.0", features = ["print-defmt"] }
40-
41-
[features]
42-
# set logging levels here
43-
default = [
44-
"defmt-default",
45-
# "dependency-a/defmt-trace",
46-
]
47-
48-
# do NOT modify these features
49-
defmt-default = []
50-
defmt-trace = []
51-
defmt-debug = []
52-
defmt-info = []
53-
defmt-warn = []
54-
defmt-error = []
55-
56-
[dev-dependencies.rand]
57-
default-features = false
58-
version = "0.8.3"
59-
60-
[dev-dependencies.rand_chacha]
61-
version = "0.3.0"
62-
default-features = false
63-
64-
[profile.dev]
65-
debug = true
66-
67-
[profile.release]
68-
debug = true
69-
lto = true
70-
opt-level = "s"

README.md

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,65 @@
11
# microbit
22

3-
[![microbit on crates.io][cratesio-image]][cratesio]
4-
[![microbit on docs.rs][docsrs-image]][docsrs]
3+
_microbit_ contains everything required to get started using Rust to create firmwares for the fabulous
4+
[BBC micro:bit](https://microbit.org) microcontroller board. This little board has everything and a kitchen sink built-in,
5+
even a capable debugging interface.
56

6-
[cratesio-image]: https://img.shields.io/crates/v/microbit.svg
7-
[cratesio]: https://crates.io/crates/microbit
8-
[docsrs-image]: https://docs.rs/microbit/badge.svg
9-
[docsrs]: https://docs.rs/microbit
7+
## Getting started
108

11-
_microbit_ contains everything required to get started with the use of Rust to create firmwares for the fabulous [BBC micro:bit][] microcontroller board. This little board has everything and a kitchen sink built-in, even a capable debugging interface, so all that one needs to get going with programming this device is:
9+
All you need to start programming this device is:
1210

13-
* A BBC micro:bit board (at the moment v1 only)
11+
* A BBC micro:bit board
1412
* A computer (macOS and Linux work perfectly, [Windows tested as well](http://flames-of-code.netlify.com/blog/rust-microbit-windows/))
1513
* A bit of open source software
1614

17-
Some very preliminary examples of using this crate can be found [here in this repo][examples] or [here on my blog][myblog].
15+
### Know your version
1816

19-
A guide to embedded development with Rust on the _microbit_ using this crate can be found in the [MicroRust book][microrust].
17+
The micro:bit comes in different versions. There is a separate crate for each major board version. See the table below to identify
18+
which crate you need to use.
2019

21-
The [BBC micro:bit][] and this crate is compatible with the fantastic tooling
22-
provided by the [knurling] project. If you haven't done so already, installing
23-
`probe-run` and playing around with the examples is highly recommended:
20+
| Crate | Board version | Board image | Docs | crates.io | target |
21+
| ------------------------------ | ----- | ---- | --------- | ------ |
22+
| [`microbit`](./microbit) | V1 | [<img src="https://github.com/microbit-foundation/microbit-svg/raw/master/microbit-drawing-back-1-5.png" width="124px" height="100px">](https://github.com/microbit-foundation/microbit-svg/blob/master/microbit-drawing-back-1-5.png) | [![docs.rs](https://docs.rs/microbit/badge.svg)](https://docs.rs/microbit) | [![crates.io](https://img.shields.io/crates/d/microbit.svg)](https://crates.io/crates/microbit) | `thumbv6m-none-eabi` |
23+
| [`microbit-v2`](./microbit-v2) | V2 | [<img src="https://github.com/microbit-foundation/microbit-svg/raw/master/microbit-drawing-back-2.png" width="124px" height="100px">](https://github.com/microbit-foundation/microbit-svg/blob/master/microbit-drawing-back-2.png) | [![docs.rs](https://docs.rs/microbit-v2/badge.svg)](https://docs.rs/microbit-v2) | [![crates.io](https://img.shields.io/crates/d/microbit-v2.svg)](https://crates.io/crates/microbit-v2) | `thumbv7em-none-eabihf` |
24+
25+
### Install dependencies
26+
27+
The examples make use of some of the fantastic tooling from the [knurling](https://knurling.ferrous-systems.com/) project.
28+
In order to run the examples you need to install [`probe-run`](https://github.com/knurling-rs/probe-run#installation)
29+
and [`flip-link`](https://github.com/knurling-rs/flip-link#installation).
30+
31+
```bash
32+
> cargo install probe-run flip-link
33+
```
34+
35+
### Run an example
36+
37+
The first thing to try is one of the [examples](./examples) in this repository. Plug in your micro:bit and
38+
run one of the commands below.
39+
40+
*For micro:bit V1*
2441
```bash
25-
# cargo install probe-run
26-
# cargo run --release --example led_blocking
27-
Compiling microbit v0.8.0
28-
Finished release [optimized + debuginfo] target(s) in 15.39s
29-
Running `probe-run --chip nRF51822_xxAA target/thumbv6m-none-eabi/release/examples/led_blocking`
30-
(HOST) INFO flashing program (1.95 KiB)
31-
(HOST) INFO success!
32-
RTT logs not available; blocking until the device halts..
42+
> cargo run --manifest-path ./examples/led-blocking/Cargo.toml --features v1 --target thumbv6m-none-eabi
3343
```
3444

35-
[BBC micro:bit]: https://microbit.org
36-
[cortex-m]:(https://github.com/japaric/cortex-m)
37-
[cortex-m-rt]:(https://github.com/japaric/cortex-m-rt)
38-
[examples]: https://github.com/therealprof/microbit/tree/master/examples
39-
[myblog]: https://www.eggers-club.de/blog/2018/05/31/rust-on-the-microbit-101-part-1
40-
[microrust]: https://droogmic.github.io/microrust/
41-
[knurling]: https://knurling.ferrous-systems.com/
45+
*For micro:bit V2*
46+
```bash
47+
> cargo run --manifest-path ./examples/led-blocking/Cargo.toml --features v2 --target thumbv7em-none-eabihf
48+
```
49+
50+
You should see a lot of build output, the orange LED on the back of the micro:bit should flash quickly and
51+
a message should appear on the LED display.
52+
53+
Congratulations! You've flashed your first rust program onto your micro:bit!
54+
55+
## Further reading
56+
57+
A guide to embedded development with Rust on the _microbit_ using this crate can be found in the [MicroRust book](https://droogmic.github.io/microrust/).
58+
59+
Other useful resources:
60+
- [micro:bit developer community](https://tech.microbit.org)
61+
- [micro:bit hardware overview](https://tech.microbit.org/hardware/)
62+
- [nrf-hal](https://github.com/nrf-rs/nrf-hal#readme) the hardware abstraction layer (HAL) this repository is based on
4263

4364
## License
4465

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[package]
2+
name = "gpio-direct-blinky"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
[dependencies]
7+
cortex-m = "0.6.1"
8+
cortex-m-rt = "0.6.12"
9+
panic-halt = "0.2.0"
10+
defmt = "0.2.0"
11+
12+
[dependencies.microbit]
13+
path = "../../microbit"
14+
optional = true
15+
16+
[dependencies.microbit-v2]
17+
path = "../../microbit-v2"
18+
optional = true
19+
20+
[features]
21+
v1 = ["microbit"]
22+
v2 = ["microbit-v2"]
23+
24+
default = [
25+
"defmt-default",
26+
]
27+
28+
# do NOT modify these features
29+
defmt-default = []
30+
defmt-trace = []
31+
defmt-debug = []
32+
defmt-info = []
33+
defmt-warn = []
34+
defmt-error = []

examples/gpio_direct_blinky.rs renamed to examples/gpio-direct-blinky/src/main.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use panic_halt as _;
55

66
use cortex_m_rt::entry;
77

8+
#[cfg(feature = "v1")]
89
#[entry]
910
fn main() -> ! {
1011
if let Some(p) = microbit::Peripherals::take() {
@@ -33,3 +34,33 @@ fn main() -> ! {
3334
continue;
3435
}
3536
}
37+
38+
#[cfg(feature = "v2")]
39+
#[entry]
40+
fn main() -> ! {
41+
if let Some(p) = microbit::Peripherals::take() {
42+
p.P0.pin_cnf[28].write(|w| w.dir().output());
43+
p.P0.pin_cnf[21].write(|w| w.dir().output());
44+
45+
p.P0.out.write(|w| unsafe { w.bits(1 << 21) });
46+
47+
let mut count: u8 = 0;
48+
loop {
49+
count += 1;
50+
51+
if count & 1 == 1 {
52+
p.P0.out.write(|w| unsafe { w.bits(1 << 21) });
53+
} else {
54+
p.P0.out.write(|w| unsafe { w.bits(0) });
55+
}
56+
57+
for _ in 0..50_000 {
58+
cortex_m::asm::nop();
59+
}
60+
}
61+
};
62+
63+
loop {
64+
continue;
65+
}
66+
}

examples/gpio-hal-blinky/Cargo.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[package]
2+
name = "gpio-hal-blinky"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
[dependencies]
7+
embedded-hal = "0.2.4"
8+
cortex-m-rt = "0.6.12"
9+
panic-halt = "0.2.0"
10+
defmt-rtt = "0.2.0"
11+
defmt = "0.2.0"
12+
13+
[dependencies.microbit]
14+
path = "../../microbit"
15+
optional = true
16+
17+
[dependencies.microbit-v2]
18+
path = "../../microbit-v2"
19+
optional = true
20+
21+
[features]
22+
v1 = ["microbit"]
23+
v2 = ["microbit-v2"]
24+
25+
default = [
26+
"defmt-default",
27+
]
28+
29+
# do NOT modify these features
30+
defmt-default = []
31+
defmt-trace = []
32+
defmt-debug = []
33+
defmt-info = []
34+
defmt-warn = []
35+
defmt-error = []

0 commit comments

Comments
 (0)