Skip to content

kknives/motor-neuron-rs

Repository files navigation

R23 Motor Neuron

The Raspberry Pi Pico based Motor/Encoder (and other stuff too) Peripheral Access Device for R23.

The Motor Neuron manages access to the following peripherals:

  • 5 Serial Motor Drivers
  • 6 Rotary Encoders
  • Upto 16 servos through I2C using external PCA9685 module

Note: The LED on Pico turns on only if the configuration of all peripherals succeeds. This includes the I2C module. If the I2C module is disconnected, this means the Motor Neuron fails to configure and panics. To avoid this, flash the Pico with a version with I2C disabled. Check the releases for the same.

Using the excellent type system, HAL and PAC crates in the Rust ecosystem and community, the Motor Neuron was completed without the use of a debugger. Most of the bugs were eliminated at compile time.

Table of Contents

  1. Requirements
  2. Installation of development dependencies
  3. Running
  4. Alternative runners
  5. License

Requirements

Installation of development dependencies

rustup target install thumbv6m-none-eabi
cargo install flip-link
# This is our suggested default 'runner'
cargo install probe-run
# If you want to use elf2uf2-rs instead of probe-run, instead do...
cargo install elf2uf2-rs --locked

Running

For a debug build

cargo run

For a release build

cargo run --release

If you do not specify a DEFMT_LOG level, it will be set to debug. That means println!(""), info!("") and debug!("") statements will be printed. If you wish to override this, you can change it in .cargo/config.toml

[env]
DEFMT_LOG = "off"

You can also set this inline (on Linux/MacOS)

DEFMT_LOG=trace cargo run

or set the environment variable so that it applies to every cargo run call that follows:

Linux/MacOS/unix

export DEFMT_LOG=trace

Setting the DEFMT_LOG level for the current session
for bash

export DEFMT_LOG=trace

Windows

Windows users can only override DEFMT_LOG through config.toml or by setting the environment variable as a separate step before calling cargo run

  • cmd
set DEFMT_LOG=trace
  • powershell
$Env:DEFMT_LOG = trace
cargo run

Alternative runners

If you don't have a debug probe or if you want to do interactive debugging you can set up an alternative runner for cargo.

Some of the options for your runner are listed below:

  • cargo embed
    Step 1 - Install cargo embed:

    $ cargo install cargo-embed

    Step 2 - Make sure your .cargo/config contains the following

    [target.thumbv6m-none-eabi]
    runner = "cargo embed"

    Step 3 - Update settings in Embed.toml

    • The defaults are to flash, reset, and start a defmt logging session You can find all the settings and their meanings in the cargo-embed repo

    Step 4 - Use cargo run, which will compile the code and start the specified 'runner'. As the 'runner' is cargo embed, it will flash the device and start running immediately

    $ cargo run --release
  • probe-rs-debugger

    Step 1 - Download probe-rs-debugger VSCode plugin 0.4.0

    Step 2 - Install probe-rs-debugger VSCode plugin

    $ code --install-extension probe-rs-debugger-0.4.0.vsix

    Step 3 - Install probe-rs-debugger

    $ cargo install probe-rs-debugger

    Step 4 - Open this project in VSCode

    Step 5 - Launch a debug session by choosing Run>Start Debugging (or press F5)

  • Loading a UF2 over USB
    Step 1 - Install elf2uf2-rs:

    $ cargo install elf2uf2-rs --locked

    Step 2 - Make sure your .cargo/config contains the following

    [target.thumbv6m-none-eabi]
    runner = "elf2uf2-rs -d"

    The thumbv6m-none-eabi target may be replaced by the all-Arm wildcard 'cfg(all(target_arch = "arm", target_os = "none"))'.

    Step 3 - Boot your RP2040 into "USB Bootloader mode", typically by rebooting whilst holding some kind of "Boot Select" button. On Linux, you will also need to 'mount' the device, like you would a USB Thumb Drive.

    Step 4 - Use cargo run, which will compile the code and start the specified 'runner'. As the 'runner' is the elf2uf2-rs tool, it will build a UF2 file and copy it to your RP2040.

    $ cargo run --release
  • Loading with picotool
    As ELF files produced by compiling Rust code are completely compatible with ELF files produced by compiling C or C++ code, you can also use the Raspberry Pi tool picotool. The only thing to be aware of is that picotool expects your ELF files to have a .elf extension, and by default Rust does not give the ELF files any extension. You can fix this by simply renaming the file.

    This means you can't easily use it as a cargo runner - yet.

    Also of note is that the special pico-sdk macros which hide information in the ELF file in a way that picotool info can read it out, are not supported in Rust. An alternative is TBC.

License

The contents of this repository are dual-licensed under the MIT OR Apache 2.0 License. That means you can chose either the MIT licence or the Apache-2.0 licence when you re-use this code. See MIT or APACHE2.0 for more information on each specific licence.

Any submissions to this project (e.g. as Pull Requests) must be made available under these terms.