Skip to content

Commit

Permalink
Merge pull request #35 from ThadHouse/adafruit-metro-rp2040
Browse files Browse the repository at this point in the history
Add support for Adafruit Metro RP2040
  • Loading branch information
jannic authored Oct 15, 2023
2 parents 49d3079 + e0afd75 commit a6b1825
Show file tree
Hide file tree
Showing 8 changed files with 422 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"boards/adafruit-itsy-bitsy-rp2040",
"boards/adafruit-kb2040",
"boards/adafruit-macropad",
"boards/adafruit-metro-rp2040",
"boards/adafruit-qt-py-rp2040",
"boards/adafruit-trinkey-qt2040",
"boards/arduino_nano_connect",
Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Each BSP includes some examples to show off the features of that particular boar
### [rp-pico] - Board Support for the [Raspberry Pi Pico]

You should include this crate if you are writing code that you want to run on
a [Raspberry Pi Pico] - the original launch PCB for the RP2040 chip.
a [Raspberry Pi Pico] - the original launch PCB for the RP2040 chip.

This crate includes the [rp2040-hal], but also configures each pin of the
RP2040 chip according to how it is connected up on the Pico.
Expand Down Expand Up @@ -145,6 +145,17 @@ RP2040 chip according to how it is connected up on the Macropad.
[adafruit-macropad]: https://github.com/rp-rs/rp-hal-boards/tree/main/boards/adafruit-macropad
[Adafruit Macropad]: https://www.adafruit.com/product/5128

### [adafruit-metro-rp2040] - Board Support for the [Adafruit Metro RP2040]

You should include this crate if you are writing code that you want to run on
an [Adafruit Metro RP2040] - an RP2040 board in the Metro family.

This crate includes the [rp2040-hal], but also configures each pin of the
RP2040 chip according to how it is connected up on the Metro RP2040.

[Adafruit Metro RP2040]: https://www.adafruit.com/product/5786
[adafruit-metro-rp2040]: https://github.com/rp-rs/rp-hal-boards/tree/main/boards/adafruit-metro-rp2040

### [adafruit-qt-py-rp2040] - Board Support for the [Adafruit QT Py RP2040]

You should include this crate if you are writing code that you want to run on
Expand Down Expand Up @@ -335,7 +346,7 @@ RP2040 chip according to how it is connected up on the Feather.
### [waveshare-rp2040-lcd-0_96] - Board Support for the [Waveshare RP2040 LCD 0.96"]

You should include this crate if you are writing code that you want to run on
an [Waveshare RP2040 LCD 0.96"] - a very small RP2040 breakout board with USB-C,
an [Waveshare RP2040 LCD 0.96"] - a very small RP2040 breakout board with USB-C,
a 65K IPS LCD 160x80, 16MBit Flash and 1A battery charger from Waveshare.

This crate includes the [rp2040-hal], but also configures each pin of the
Expand Down
10 changes: 10 additions & 0 deletions boards/adafruit-metro-rp2040/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

- Initial release
51 changes: 51 additions & 0 deletions boards/adafruit-metro-rp2040/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[package]
name = "adafruit-metro-rp2040"
version = "0.1.0"
authors = ["Thad House <[email protected]>", "The rp-rs Developers"]
edition = "2018"
homepage = "https://github.com/rp-rs/rp-hal-boards/tree/main/boards/adafruit-metro-rp2040"
description = "Board Support Package for the Adafruit Metro RP2040"
license = "MIT OR Apache-2.0"
repository = "https://github.com/rp-rs/rp-hal-boards.git"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cortex-m-rt = { workspace = true, optional = true }
rp2040-boot2 = { workspace = true, optional = true }
rp2040-hal.workspace = true

[dev-dependencies]
cortex-m.workspace = true
panic-halt.workspace = true
embedded-hal.workspace = true
smart-leds.workspace = true
nb.workspace = true
ws2812-pio.workspace = true
fugit.workspace = true

[features]
# This is the set of features we enable by default
default = ["boot2", "rt", "critical-section-impl", "rom-func-cache"]

# critical section that is safe for multicore use
critical-section-impl = ["rp2040-hal/critical-section-impl"]

# 2nd stage bootloaders for rp2040
boot2 = ["rp2040-boot2"]

# Minimal startup / runtime for Cortex-M microcontrollers
rt = ["cortex-m-rt","rp2040-hal/rt"]

# This enables a fix for USB errata 5: USB device fails to exit RESET state on busy USB bus.
# Only required for RP2040 B0 and RP2040 B1, but it doesn't hurt to enable it
rp2040-e5 = ["rp2040-hal/rp2040-e5"]

# Memoize(cache) ROM function pointers on first use to improve performance
rom-func-cache = ["rp2040-hal/rom-func-cache"]

# Disable automatic mapping of language features (like floating point math) to ROM functions
disable-intrinsics = ["rp2040-hal/disable-intrinsics"]

# This enables ROM functions for f64 math that were not present in the earliest RP2040s
rom-v2-intrinsics = ["rp2040-hal/rom-v2-intrinsics"]
98 changes: 98 additions & 0 deletions boards/adafruit-metro-rp2040/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# [adafruit-metro-rp2040] - Board Support for the [Adafruit Metro RP2040]

You should include this crate if you are writing code that you want to run on
an [Adafruit Metro RP2040] - an RP2040 board in the Metro family from Adafruit.

This crate includes the [rp2040-hal], but also configures each pin of the
RP2040 chip according to how it is connected up on the Metro RP2040.

[Adafruit Metro RP2040]: https://www.adafruit.com/product/5786
[adafruit-metro-rp2040]: https://github.com/rp-rs/rp-hal-boards/tree/main/boards/adafruit-metro-rp2040
[rp2040-hal]: https://github.com/rp-rs/rp-hal/tree/main/rp2040-hal
[Raspberry Silicon RP2040]: https://www.raspberrypi.org/products/rp2040/

## Using

To use this crate, your `Cargo.toml` file should contain:

```toml
adafruit-metro-rp2040 = "0.1.0"
```

In your program, you will need to call `adafruit_metro_rp2040::Pins::new` to create
a new `Pins` structure. This will set up all the GPIOs for any on-board
devices. See the [examples](./examples) folder for more details.

## Examples

### General Instructions

To compile an example, clone the _rp-hal-boards_ repository and run:

```console
rp-hal-boards/boards/adafruit-metro-rp2040 $ cargo build --release --example <name>
```

You will get an ELF file called
`./target/thumbv6m-none-eabi/release/examples/<name>`, where the `target`
folder is located at the top of the _rp-hal-boards_ repository checkout. Normally
you would also need to specify `--target=thumbv6m-none-eabi` but when
building examples from this git repository, that is set as the default.

If you want to convert the ELF file to a UF2 and automatically copy it to the
USB drive exported by the RP2040 bootloader, simply boot your board into
bootloader mode and run:

```console
rp-hal-boards/boards/adafruit-metro-rp2040 $ cargo run --release --example <name>
```

If you get an error about not being able to find `elf2uf2-rs`, try:

```console
$ cargo install elf2uf2-rs, then repeating the `cargo run` command above.
```

### [adafruit_metro_blinky](./examples/adafruit_metro_blinky.rs)

Blink an LED on and off.

### [adafruit_metro_rainbow](./examples/adafruit_metro_rainbow.rs)

Continuously changes the color of the Metro's onboard Neopixel.

## Contributing

Contributions are what make the open source community such an amazing place to
be learn, inspire, and create. Any contributions you make are **greatly
appreciated**.

The steps are:

1. Fork the Project by clicking the 'Fork' button at the top of the page.
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
3. Make some changes to the code or documentation.
4. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
5. Push to the Feature Branch (`git push origin feature/AmazingFeature`)
6. Create a [New Pull Request](https://github.com/rp-rs/rp-hal-boards/pulls)
7. An admin will review the Pull Request and discuss any changes that may be required.
8. Once everyone is happy, the Pull Request can be merged by an admin, and your work is part of our project!

## Code of Conduct

Contribution to this crate is organized under the terms of the [Rust Code of
Conduct][CoC], and the maintainer of this crate, the [rp-rs team], promises
to intervene to uphold that code of conduct.

[CoC]: CODE_OF_CONDUCT.md
[rp-rs team]: https://github.com/orgs/rp-rs/teams/rp-rs

## License

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

Any submissions to this project (e.g. as Pull Requests) must be made available
under these terms.
84 changes: 84 additions & 0 deletions boards/adafruit-metro-rp2040/examples/adafruit_metro_blinky.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//! # GPIO 'Blinky' Example
//!
//! Blinks the LED on a Adafruit Metro RP2040 board
//!
//! It may need to be adapted to your particular board layout and/or pin assignment.
//!
//! See the `Cargo.toml` file for Copyright and license details.
#![no_std]
#![no_main]

// The macro for our start-up function
use adafruit_metro_rp2040::entry;

// Ensure we halt the program on panic (if we don't mention this crate it won't
// be linked)
use panic_halt as _;

// Some traits we need
use embedded_hal::digital::v2::OutputPin;

use adafruit_metro_rp2040::{
hal::{
clocks::{init_clocks_and_plls, Clock},
pac,
sio::Sio,
watchdog::Watchdog,
},
Pins, XOSC_CRYSTAL_FREQ,
};

use cortex_m::delay::Delay;

/// Entry point to our bare-metal application.
///
/// The `#[entry]` macro ensures the Cortex-M start-up code calls this function
/// as soon as all global variables are initialised.
///
/// The function configures the RP2040 peripherals, then toggles a GPIO pin in
/// an infinite loop. If there is an LED connected to that pin, it will blink.
#[entry]
fn main() -> ! {
// Grab our singleton objects
let mut pac = pac::Peripherals::take().unwrap();
let core = pac::CorePeripherals::take().unwrap();

// Set up the watchdog driver - needed by the clock setup code
let mut watchdog = Watchdog::new(pac.WATCHDOG);

// Configure the clocks
let clocks = init_clocks_and_plls(
XOSC_CRYSTAL_FREQ,
pac.XOSC,
pac.CLOCKS,
pac.PLL_SYS,
pac.PLL_USB,
&mut pac.RESETS,
&mut watchdog,
)
.ok()
.unwrap();

let mut delay = Delay::new(core.SYST, clocks.system_clock.freq().to_Hz());

// The single-cycle I/O block controls our GPIO pins
let sio = Sio::new(pac.SIO);

let pins = Pins::new(
pac.IO_BANK0,
pac.PADS_BANK0,
sio.gpio_bank0,
&mut pac.RESETS,
);
let mut led_pin = pins.d13.into_push_pull_output();

loop {
led_pin.set_high().unwrap();
delay.delay_ms(500);
led_pin.set_low().unwrap();
delay.delay_ms(500);
}
}

// End of file
91 changes: 91 additions & 0 deletions boards/adafruit-metro-rp2040/examples/adafruit_metro_rainbow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//! Continuously changes the color of the Neopixel on a Adafruit Metro RP2040 board
#![no_std]
#![no_main]

use adafruit_metro_rp2040::entry;
use core::iter::once;
use embedded_hal::timer::CountDown;
use fugit::ExtU32;
use panic_halt as _;
use smart_leds::{brightness, SmartLedsWrite, RGB8};
use ws2812_pio::Ws2812;

use adafruit_metro_rp2040::{
hal::{
clocks::{init_clocks_and_plls, Clock},
pac,
pio::PIOExt,
watchdog::Watchdog,
Sio, Timer,
},
Pins, XOSC_CRYSTAL_FREQ,
};

#[entry]
fn main() -> ! {
let mut pac = pac::Peripherals::take().unwrap();
let mut watchdog = Watchdog::new(pac.WATCHDOG);

let clocks = init_clocks_and_plls(
XOSC_CRYSTAL_FREQ,
pac.XOSC,
pac.CLOCKS,
pac.PLL_SYS,
pac.PLL_USB,
&mut pac.RESETS,
&mut watchdog,
)
.ok()
.unwrap();

let sio = Sio::new(pac.SIO);

let pins = Pins::new(
pac.IO_BANK0,
pac.PADS_BANK0,
sio.gpio_bank0,
&mut pac.RESETS,
);

let led = pins.neopixel_data.into_function();

let timer = Timer::new(pac.TIMER, &mut pac.RESETS, &clocks);
let mut delay = timer.count_down();

let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
let mut ws = Ws2812::new(
led,
&mut pio,
sm0,
clocks.peripheral_clock.freq(),
timer.count_down(),
);

let mut n: u8 = 128;
loop {
ws.write(brightness(once(wheel(n)), 32)).unwrap();
n = n.wrapping_add(1);

delay.start(25.millis());
let _ = nb::block!(delay.wait());
}
}

/// Convert a number from `0..=255` to an RGB color triplet.
///
/// The colours are a transition from red, to green, to blue and back to red.
fn wheel(mut wheel_pos: u8) -> RGB8 {
wheel_pos = 255 - wheel_pos;
if wheel_pos < 85 {
// No green in this sector - red and blue only
(255 - (wheel_pos * 3), 0, wheel_pos * 3).into()
} else if wheel_pos < 170 {
// No red in this sector - green and blue only
wheel_pos -= 85;
(0, wheel_pos * 3, 255 - (wheel_pos * 3)).into()
} else {
// No blue in this sector - red and green only
wheel_pos -= 170;
(wheel_pos * 3, 255 - (wheel_pos * 3), 0).into()
}
}
Loading

0 comments on commit a6b1825

Please sign in to comment.