Microbit v2 Bluetooth

I've recently completed the Discovery book (this one: Introduction - Discovery) that uses the microbit v2, and I've also been able to successfully implement some of my own ideas (not in the book) with the microbit. I'm looking to take the next step in my embedded Rust journey, and I thought transmitting data via bluetooth would be a good idea. However, it has very quickly stomped me into submission :tired_face:.

  1. Is there a different board that's easier to use for bluetooth data transmission?
  2. If not, is the nrf-softdevice crate the way to go for the microbit v2?
  3. Any general advice/recommendations for trying to transmit sensor data over bluetooth with Rust?

Thanks!

I like the nrf-softdevice crateβ€”the examples cover a wide range of use cases, and the community provides great support. What specific challenges are you encountering?

1 Like

unfortunately, there's no open source bluetooth stack for this chip in rust, as far as I know. the softdevice is the only bluetooth stack on nrf chips, but its a proprietary blob, I have only used it with the C nrf sdk, I have zero experience with it in rust.

from the "readme" file, I have the impression that it should be easy to use, compared to the C sdk. but I'll let people with real experience to speak about it.

as for alternative boards, I have heard good words about rust support for the esp32 chips, thanks to the first party support from the chip vendor. you can find many esp32 based board on line, for example, there are several devkits from ada fruit. I recommend the C series such as esp32-c3, which is riscv based, over the xtensa based models, because the xtensa toolchain setup is a little bit more involved than the riscv toolchain.

there's a blog series about rust bluetooth on the esp32-c3 device, you can check it out:

1 Like

I tried getting the examples/src/bin/ble_advertise.rs example working and it appears to have compiled without error:

   Compiling microbit-light v0.1.0 (C:\Users\mthel\Rust\microbit-light)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.09s                                                                                                                                  
      Config default
      Target C:\Users\mthel\Rust\microbit-light\target\thumbv7em-none-eabihf\debug\microbit-light
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0001000: CIDR0 has invalid preamble (expected 0xd, got 0x0)
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0001000: CIDR2 has invalid preamble (expected 0x5, got 0x0)
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0001000: CIDR3 has invalid preamble (expected 0xb1, got 0x0)
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0000000: CIDR0 has invalid preamble (expected 0xd, got 0xb1)
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0000000: CIDR1 has invalid preamble (expected 0x0, got 0x1)
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0000000: CIDR2 has invalid preamble (expected 0x5, got 0xb1)
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0040000: CIDR0 has invalid preamble (expected 0xd, got 0x0)
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0040000: CIDR2 has invalid preamble (expected 0x5, got 0x0)
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0040000: CIDR3 has invalid preamble (expected 0xb1, got 0x0)
      Erasing βœ” 100% [####################]  76.00 KiB @  36.14 KiB/s (took 2s)
  Programming βœ” 100% [####################]  76.00 KiB @  14.23 KiB/s (took 5s)                                                                                                                                Finished in 5.34s
        Done processing config default

But I don't see any of the info! messages printed, nor do I see the device when I scan for it from my phone or PC.

Can you check your .cargo/config.toml?

For micro-bit, you want this:
runner = "probe-rs run --chip nRF52833_xxAA"

1 Like

Yep, I have it. I created a repo with everything here:

With my microbit v2 connected to the PC:

PS C:\Users\mthel\Rust\microbit-light> cargo embed --target thumbv7em-none-eabihf                             
   Compiling microbit-light v0.1.0 (C:\Users\mthel\Rust\microbit-light)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.60s                                                                                                                                  
      Config default
      Target C:\Users\mthel\Rust\microbit-light\target\thumbv7em-none-eabihf\debug\microbit-light
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0001000: CIDR0 has invalid preamble (expected 0xd, got 0x0)
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0001000: CIDR2 has invalid preamble (expected 0x5, got 0x0)
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0001000: CIDR3 has invalid preamble (expected 0xb1, got 0x0)
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0000000: CIDR0 has invalid preamble (expected 0xd, got 0xb1)
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0000000: CIDR1 has invalid preamble (expected 0x0, got 0x1)
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0000000: CIDR2 has invalid preamble (expected 0x5, got 0xb1)
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0040000: CIDR0 has invalid preamble (expected 0xd, got 0x0)
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0040000: CIDR2 has invalid preamble (expected 0x5, got 0x0)
 WARN probe_rs::architecture::arm::memory::romtable: Component at 0xe0040000: CIDR3 has invalid preamble (expected 0xb1, got 0x0)
      Erasing βœ” 100% [####################]  76.00 KiB @  36.16 KiB/s (took 2s)
  Programming βœ” 100% [####################]  76.00 KiB @  14.22 KiB/s (took 5s)                                                                                                                           
     Finished in 5.34s
        Done processing config default
PS C:\Users\mthel\Rust\microbit-light>

Thanks for sharing the repo. I was able to run your app as it is using a Mac (although it crashes at line 83). Here’s the output:

     Running `probe-rs run --chip nRF52833_xxAA target/thumbv7em-none-eabihf/debug/microbit-light`
      Erasing βœ” 100% [####################]  76.00 KiB @  37.39 KiB/s (took 2s)
  Programming βœ” 100% [####################]  76.00 KiB @  16.38 KiB/s (took 5s)                                                                                                                   Finished in 4.64s
0.000000 INFO  WE HAVE LIFT OFF...
└─ microbit_light::____embassy_main_task::{async_fn#0} @ src/main.rs:23  
0.000000 INFO  softdevice RAM: 11488 bytes
└─ nrf_softdevice::softdevice::{impl#0}::enable @ /Users/kentarookuda/.cargo/registry/src/index.crates.io-6f17d22bba15001f/nrf-softdevice-0.1.0/src/fmt.rs:138 
0.000000 WARN  You're giving more RAM to the softdevice than needed. You can change your app's RAM start address to 20002ce0
└─ nrf_softdevice::softdevice::{impl#0}::enable @ /Users/kentarookuda/.cargo/registry/src/index.crates.io-6f17d22bba15001f/nrf-softdevice-0.1.0/src/fmt.rs:151 
0.000000 INFO  Starting BLE advertisement...
└─ microbit_light::____embassy_main_task::{async_fn#0} @ src/main.rs:82  
0.000000 WARN  sd_ble_gap_adv_set_configure err InvalidFlags
└─ nrf_softdevice::ble::peripheral::start_adv::{closure#4} @ /Users/kentarookuda/.cargo/registry/src/index.crates.io-6f17d22bba15001f/nrf-softdevice-0.1.0/src/fmt.rs:151 
0.000000 WARN  sd_ble_gap_adv_stop: InvalidState
└─ nrf_softdevice::ble::peripheral::advertise_inner::{async_fn#0}::{closure#0} @ /Users/kentarookuda/.cargo/registry/src/index.crates.io-6f17d22bba15001f/nrf-softdevice-0.1.0/src/fmt.rs:151 
0.000000 ERROR panicked at src/main.rs:83:67:
called `Result::unwrap()` on an `Err` value: Raw(InvalidFlags)

Have you flashed SoftDevice? If not, follow this instruction and flash S140.

1 Like

That's interesting - you are at least seeing the info! prints and I don't get that at all. I did flash SoftDevice, but I'm not entirely sure because every time I do it it prints an error:

PS C:\Users\mthel> probe-rs download --verify --binary-format hex --chip nRF52833_xxAA "C:\Users\mthel\Rust\s140_nrf52_7.3.0\s140_nrf52_7.3.0_softdevice.hex"
ERROR probe_rs::architecture::arm::core::armv7m: The core is in locked up status as a result of an unrecoverable exception
      Erasing βœ” 100% [####################] 156.00 KiB @  37.28 KiB/s (took 4s)
  Programming βœ” 100% [####################] 156.00 KiB @  14.51 KiB/s (took 11s)
     Finished in 16.24s

I mean, it appears to flash despite the error, but I don't know if that's causing any problems.

Without knowing much of the details about the hardware, it may be helpful to add some sleeps between the printouts?
I noticed the working version prints all the messages at 0.0000, so it's possible the device gets into a locked up state before it gets to send the info to your console.

1 Like

Sounds like it is a probe-rs problem.
Do you use the latest version? Mine shows this:

probe-rs --version
probe-rs 0.25.0 (git commit: crates.io)

I also found this comment at the probe-rs repo. This person says downgrading to 0.22.0 worked for microbit-v2. You may want to try that.

Yes, I'm also using 0.25.0 and downgrading to 0.22.0 didn't fix the issue. I don't get the error anymore about the core being in locked up status when flashing SoftDevice, but the end result is the same when running cargo embed to flash the program to the microcontroller. Thanks for your help though, I really appreciate it!

I went ahead and ordered an ESP32-C3-DevKit so hopefully I'll be able to follow along with the blog posts that @nerditation linked to above and get something working with that board!

I do cargo run or cargo run --release. If I do cargo embed, I don't get info!.

Ha! That was it! After being able to see the errors, I was able to get something working (updated code in the repo). Thanks so much for your help, I appreciate it! Now, on to figuring out how to transmit some data! :grinning: