Skip to content

sundbom-grepit/atsamd

 
 

Repository files navigation

atsamd & atsame support for Rust

This repository holds various crates that support/enable working with Atmel samd11, samd21, samd51 and same5x based devices using Rust.

CI Build

The Peripheral Access Crates (PACs) are automatically generated, and provide low-level access to the peripherals specified by a device's SVD file.

The Hardware Abstraction Layer (HAL) is the result of reading the datasheet for the device and encoding a type-safe layer over the raw PACs. This crate implements traits specified by the embedded-hal project, making it compatible with various drivers in the embedded rust ecosystem.

In addition to the PACs and HAL, there numerous Board Support Packages (BSPs) for popular development boards. They aim to rename pins to match silk screens or Arduino pin assignments, add helpers for initialization, and re-export the atsamd-hal crate. These BSPs are listed beside their respective PACs below.

Crate Version Board Support Packages
atsamd11c Crates.io
atsamd21e Crates.io Gemma M0, Serpente, Trinket M0
atsamd21g Crates.io Arduino Nano 33 IOT, Circuit Playground Express, Feather M0, ItsyBitsy M0, Metro M0, MKR1000, MKR Vidor 4000, MKR ZERO, SAMD21 Mini, SODAQ ONE, Wio Lite MG126, Xiao M0
atsamd21j Crates.io SODAQ SARA AFF
atsamd51g Crates.io ItsyBitsy M4, Trellis M4
atsamd51j Crates.io EdgeBadge, Feather M4, Metro M4, PyPortal
atsame51n Crates.io
atsamd51p Crates.io Grand Central M4 Express, Wio Terminal
atsame51g Crates.io
atsame51j Crates.io
atsame51n Crates.io
atsame53j Crates.io
atsame53n Crates.io
atsame54n Crates.io
atsame54p Crates.io PathfinderZA Proto1, SAM E54 Xplained Pro Evaluation Kit
atsamd-hal Crates.io

Building

Make sure that you have a new enough version of the gcc toolchain; the one installable even on recent versions of Ubuntu can fail to correctly link the vector table:

Note: you may be able to avoid this step if using cargo-binutils. See board specific instructions in boards/ for more details.

$ sudo add-apt-repository ppa:team-gcc-arm-embedded/ppa -y
$ sudo apt update
$ sudo apt install gcc-arm-embedded

You'll need to add the proper compilation target prior to building as well:

$ # for samd11, samd21:
$ rustup target add thumbv6m-none-eabi
$ # for samd51, same51, same53, same54:
$ rustup target add thumbv7em-none-eabihf

Since a number of different MCUs are used, building the examples requires changing directory into one of the board support package directories prior to building. For example:

$ cd metro_m0
$ cargo build --examples
$ cd ../gemma_m0
$ cargo build --examples

How to use a BSP (i.e. getting started writing your own code)

A BSP (Board Support Package) is a crate that contains definitions specific to a particular board. These generally contain pin definitions, sometimes helper functions to setup certain peripherals, as well as examples to quickly get up and running with your board. BSPs are separated into 2 tiers:

  • Tier 1 boards are guaranteed to be up to date with the latest version of atsamd-hal

  • Tier 2 boards are tied to a specific version of atsamd-hal. They are not guaranteed to be updated when a new version of atsamd-hal gets released.

Tier 1 BSPs

  • feather_m0

  • feather_m4

  • metro_m0

  • metro_m4

  • pygamer

  • samd11_bare

  • wio_terminal

To bootstrap your own project you should be able to copy/paste the Rust code from the examples folder within the folder of the BSP you've chosen. But you shouldn't copy the Cargo.toml file from there, since that's not only used for the examples, but also for the whole BSP itself. You want to make your own Cargo.toml file. If you're new to this and have no clue what you're doing then this is probably the line you want in there:

[dependencies]
feather_m0 = { git = "https://github.com/atsamd-rs/atsamd" }

Replace "feather_m0" with the name of the BSP you want to use. Note a few things:

  • By specifying the dependency as a remote git repo without specifying a branch, rather than pinning a specific version, we ask Cargo to always grab the latest master branch from Github. This is probably what you want in the beginning, at least now when the project is young, but expect that you'll later want to pin down a specific version.
  • There's a whole bunch of crates inside that git repo, and Cargo automatically figures out which one you want to use by checking what you're naming the dependency. In this case we named it "feather_m0" so Cargo will try to find a BSP called that, somewhere inside the repo.

Now the imports in your Rust code should work, if they look something like this:

use feather_m0 as bsp;
use bsp::hal;
use bsp::pac;

You should now have objects called bsp and hal and pac, which contain all the good stuff.

Getting code onto the device: Adafruit M0/M4 board (such as Gemma M0 & Feather M0)

If you want to flash the device using the tools that come with the Adafruit arduino support package:

$ cd gemma_m0
$ cargo build --example blinky_basic
$ arm-none-eabi-objcopy -O binary \
    target/thumbv6m-none-eabi/debug/examples/blinky_basic \
    target/thumbv6m-none-eabi/debug/examples/blinky_basic.bin
# if using cargo-binutils, you can `rust-objcopy` with the same flags, or combine the previous 2 steps with `cargo objcopy`
$ stty -F /dev/ttyACM1 ospeed 1200
$ ~/.arduino15/packages/arduino/tools/bossac/1.7.0/bossac -i -d \
    --port=ttyACM1 -U -e -w -v \
    target/thumbv6m-none-eabi/debug/examples/blinky_basic.bin -R

This same technique should work for all of the Adafruit M0/M4 boards, as they all ship with a bossac compatible bootloader. Note that M0 devices may need -o 0x2000 and M4 devices may need -o 0x4000 added to the bossac parameter lists.

Getting code onto the device: JLink

If you have a board with a SWD debug header, such as the Metro M0, or if you attached the header yourself, you can use your JLink programmer to either flash the device or debug it (together with gdb).

You can use JFlashLiteExe to just flash the .bin file (note that for some boards, such as Feather M0, you need to specify the program memory starting address offset or you'll just get a nondescript flashing error).

To debug your board you can run JLinkGDBServer instead of JFlashLiteExe. @wez prefers using the JLinkGDBServer, but you can also use OpenOCD. Note that if you have a load command within your .gdbinit then starting a debug session triggers a flashing, so you don't need a separate flashing step before this.

In one window, run JLinkGDBServer -if SWD -device ATSAMD21G18, then in another, run these commands from the root of this repo so that you pick up its .gdbinit file:

$ cargo build --manifest-path metro_m0/Cargo.toml --example blinky_basic
$ arm-none-eabi-gdb metro_m0/target/thumbv6m-none-eabi/debug/examples/blinky_basic

If you prefer or otherwise need to use OpenOCD, then you'd run it in place of the JLinkGDBServer and then modify the .gdbinit file to comment out the JLink section and uncomment the OpenOCD section.

Getting code onto the devices with bootloaders: hf2-rs

This is the preferred pure rust ecosystem method for interacting with bootloaders.

hf2-rs implements Microsofts HID Flashing Format (HF2) to upload firmware to UF2 bootloaders. UF2 is factory programmed extensively by Microsoft MakeCode and Adafruit hardware.

The cargo-hf2 crate replaces the cargo build command to include flashing over USB to connected UF2 devices, using hf2 flashing over HID protocol.

$ cargo install cargo-hf2

and from a bsp directory

$ cargo hf2 --example blinky_basic --features unproven --release

If you are on Linux and hf2 fails to flash your board even if it is connected and in bootloader mode, you might need to add some udev rules if you have not done that yet.

You might want to have all the hf2 related rules in a single file, i.e. /etc/udev/rules.d/99-hf2-boards.rules, or have a different rules file for each vendor.

The rules for Seeeduino and Adafruit boards look like this:

#adafruit rules
ATTRS{idVendor}=="239a", ENV{ID_MM_DEVICE_IGNORE}="1"
SUBSYSTEM=="usb", ATTRS{idVendor}=="239a", MODE="0666"
SUBSYSTEM=="tty", ATTRS{idVendor}=="239a", MODE="0666"

#seeeduino rules
ATTRS{idVendor}=="2886", ENV{ID_MM_DEVICE_IGNORE}="1"
SUBSYSTEM=="usb", ATTRS{idVendor}=="2886", MODE="0666"
SUBSYSTEM=="tty", ATTRS{idVendor}=="2886", MODE="0666"

If you want to add boards from another vendor, you can get the vendor id with the lsusb command, for example:

$ lsusb
Bus 001 Device 005: ID 2886:002f Seeed Technology Co., Ltd. Seeeduino XIAO
...

Here 2886 is the vendor id and 002f the product id.

After adding the rules remember to reboot or run:

sudo udevadm control --reload-rules
sudo udevadm trigger

For more information, refer to the README files for each crate:

Getting code onto the devices with bootloaders: uf2conv-rs

uf2conv adds a uf2 header Microsofts HID Flashing Format (UF2) for copying over to UF2 bootloader mass storage devices. UF2 is factory programmed extensively by Microsoft MakeCode and Adafruit hardware. cargo-binutils replaces the cargo build command to find and convert elf files into binary.

Install the dependencies

$ rustup component add llvm-tools-preview
$ cargo install uf2conv cargo-binutils

Then for say, metro_m0 examples

$ cargo objcopy --example blinky_basic --features unproven --release -- -O binary blinky_basic.bin
$ uf2conv blinky_basic.bin --base 0x2000 --output blinky_basic.uf2
$ cp blinky_basic.uf2 /Volumes/PYGAMERBOOT/

For more information, refer to the README files for each crate:

Getting code onto the device with bootloaders: bossac

If you want to flash the device using the tools that come with the Adafruit arduino support package:

$ cd gemma_m0
$ cargo build --example blinky_basic
$ arm-none-eabi-objcopy -O binary \
    target/thumbv6m-none-eabi/debug/examples/blinky_basic \
    target/thumbv6m-none-eabi/debug/examples/blinky_basic.bin
# if using cargo-binutils, you can `rust-objcopy` with the same flags, or combine the previous 2 steps with `cargo objcopy`
$ stty -F /dev/ttyACM1 ospeed 1200
$ ~/.arduino15/packages/arduino/tools/bossac/1.7.0/bossac -i -d \
    --port=ttyACM1 -U -e -w -v \
    target/thumbv6m-none-eabi/debug/examples/blinky_basic.bin -R

This same technique should work for all of the Adafruit M0/M4 boards, as they all ship with a bossac compatible bootloader. Note that M0 devices may need -o 0x2000 and M4 devices may need -o 0x4000 added to the bossac parameter lists.

Getting code onto the device with debugger: cargo-flash

This is the preferred pure rust ecosystem method for flashing with debugger.

cargo flash replaces the cargo build command to include flashing over debugger using probe-rs and libusb.

$ cargo install cargo-flash

We need to know the specific id of your device's chip. Luckily adafruit lists ATSAMD21G18 for metro_m0

$ cargo flash --list-chips | grep ATSAMD21G18
        ATSAMD21G18A
        ATSAMD21G18AU

You can stash this chip in the cargo toml so you never have to pass it as an argument, which we recommend.

# for cargo flash
[package.metadata]
chip = "ATSAMD21G18A"

And cargo flash simply replaces your cargo build command!

$ cargo flash --example blinky_basic --features unproven --release

Or you can provide it via the chip command line argument

$ cargo flash --example blinky_basic --features unproven --release --chip ATSAMD21G18A

Debugging: probe-run

This is the preferred pure rust ecosystem method for debugging. It requires no external gdb server, nor C or Python tooling like openocd.

probe-run attemps to bring the hosted cargo run print line debugging experience to embedded. It also has advanced logging features to vastly reduce format size under the defmt project which is not covered here.

probe-run needs to be set as your runner in the .cargo/config along with the id of your chip. Also debug symbols need to be enabled for any profile you're building for. In your application you'll want to use a probe-run compatible panic crate like panic-probe and an rtt debug logging crate like rtt-target. Also don't forget to init your rtt machinery.

probe-run will then be called after a successful build to flash the code directly to the target via debugger and will then wait to receive any rtt prints from your target. Finally if a panic occurs or you ever call cortex_m::asm::bkpt() probe-run will detect, print a stack trace, and exit. You can exit probe-run on the host side with ctrl-c.

$ cargo install probe-run

Then simply use your ide's run or play button, or run:

$ cargo run --release --example adc --features=unproven
    Finished release [optimized + debuginfo] target(s) in 1.10s
     Running `probe-run --chip ATSAMD11C14A target/thumbv6m-none-eabi/release/examples/adc`
  (HOST) INFO  flashing program (35.17 KiB)
  (HOST) INFO  success!
────────────────────────────────────────────────────────────────────────────────
4129
4074
4177
^Cstack backtrace:
   0: <atsamd_hal::common::delay::Delay as embedded_hal::blocking::delay::DelayUs<u32>>::delay_us
        at /home/atsamd/hal/src/common/delay.rs:72
   1: <atsamd_hal::common::delay::Delay as embedded_hal::blocking::delay::DelayMs<u32>>::delay_ms
        at /home/atsamd/hal/src/common/delay.rs:35
   2: <atsamd_hal::common::delay::Delay as embedded_hal::blocking::delay::DelayMs<u16>>::delay_ms
        at /home/atsamd/hal/src/common/delay.rs:41
   3: neopixel_adc_battery::__cortex_m_rt_main
        at examples/neopixel_adc_battery.rs:50
   4: main
        at examples/neopixel_adc_battery.rs:23
   5: ResetTrampoline
        at /home/.cargo/registry/src/github.com-1ecc6299db9ec823/cortex-m-rt-0.6.13/src/lib.rs:547
   6: Reset
        at /home/.cargo/registry/src/github.com-1ecc6299db9ec823/cortex-m-rt-0.6.13/src/lib.rs:550

Debugging: JLink

If you have a board with a SWD debug header, such as the Metro M0, or if you attached the header yourself, you can use your JLink together with gdb. @wez prefers using the JLinkGDBServer, but you can also use OpenOCD.

In one window, run JLinkGDBServer -if SWD -device ATSAMD21G18, then in another, run these commands from the root of this repo so that you pick up its .gdbinit file:

$ cargo build --manifest-path metro_m0/Cargo.toml --example blinky_basic
$ arm-none-eabi-gdb metro_m0/target/thumbv6m-none-eabi/debug/examples/blinky_basic

If you prefer or otherwise need to use OpenOCD, then you'd run it in place of the JLinkGDBServer and then modify the .gdbinit file to comment out the JLink section and uncomment the OpenOCD section.

Adding a new board

See our wiki page for a complete guide on adding a new board.

Building everything locally

If you'd like to build all the same things that the CI would build but on your local system, you can run:

$ ./build-all.py

Please note that this script requires Python 3.

License

The included SVD files are sourced from http://packs.download.atmel.com/ and are licensed under the Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0).

The remainder of the code is licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

Target atsamd microcontrollers using Rust

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%