Skip to content

Commit

Permalink
Add support for the Pimoroni Tufty 2040 (#53)
Browse files Browse the repository at this point in the history
* Add support for Tufty 2040

* Rename tufty_blinky to tufty_demo

Also improve the description of the demo.

* Enable rom-intrinsics-v2 by default on tufty2040
  • Loading branch information
ricky26 authored Mar 16, 2024
1 parent 773ee73 commit 6eb85ab
Show file tree
Hide file tree
Showing 7 changed files with 675 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ members = [
"boards/pimoroni-plasma-2040",
"boards/pimoroni-servo2040",
"boards/pimoroni-tiny2040",
"boards/pimoroni-tufty2040",
"boards/rp-pico",
"boards/seeeduino-xiao-rp2040",
"boards/solderparty-rp2040-stamp",
Expand Down
8 changes: 8 additions & 0 deletions boards/pimoroni-tufty2040/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 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
54 changes: 54 additions & 0 deletions boards/pimoroni-tufty2040/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[package]
name = "pimoroni-tufty2040"
version = "0.1.0"
authors = ["The rp-rs Developers"]
edition = "2018"
homepage = "https://github.com/rp-rs/rp-hal-boards/tree/main/boards/pimoroni-tufty2040"
description = "Board Support Package for the Pimoroni Tufty2040"
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.workspace = true
cortex-m-rt = { workspace = true, optional = true }
embedded-hal.workspace = true
fugit.workspace = true
rp2040-boot2 = { workspace = true, optional = true }
rp2040-hal.workspace = true
st7789.workspace = true
display-interface.workspace = true
embedded-graphics.workspace = true
pio.workspace = true
pio-proc.workspace = true

[dev-dependencies]
panic-halt.workspace = true
nb.workspace = true

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

# 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 also works for RP2040 B2 and above
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"]
96 changes: 96 additions & 0 deletions boards/pimoroni-tufty2040/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# [pimoroni-tufty2040] - Board Support for the [Pimoroni Tufty2040]

You should include this crate if you are writing code that you want to run on
a [Pimoroni Tufty2040] - a hackable, programmable badge with a LCD colour
display, powered by a Raspberry Pi RP2040.

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

[Pimoroni Tufty2040]: https://shop.pimoroni.com/products/tufty-2040/
[pimoroni-tufty2040]: https://github.com/rp-rs/rp-hal-boards/tree/main/boards/pimoroni-tufty2040
[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
pimoroni_tufty2040 = "0.1.0"
```

In your program, you will need to call `pimoroni_tufty2040::Board::take().unwrap()` to create
a new `Boards` structure. This will set up all the GPIOs for any on-board
devices and configure common clocks. 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/pimoroni-tufty2040 $ 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/pimoroni-tufty2040 $ 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 try repeating the `cargo run` command above.

### [tufty_demo](./examples/tufty_demo)

Flashes the Tufty2040's LED and draws a circle on the screen.

## 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 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.
152 changes: 152 additions & 0 deletions boards/pimoroni-tufty2040/examples/tufty_demo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
//! # Tufty2040 Demo Example
//!
//! Draws a circle on the LCD screen and then blinks the user LED on the Tufty 2040.
//!
//! See the `Cargo.toml` file for Copyright and licence details.
#![no_std]
#![no_main]

use pimoroni_tufty2040 as tufty;

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

// GPIO traits
use embedded_hal::digital::v2::{OutputPin, PinState};

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

// A shorter alias for the Hardware Abstraction Layer, which provides
// higher-level drivers.
use tufty::hal;

// A shorter alias for the Peripheral Access Crate, which provides low-level
// register access
use hal::pac;

use hal::clocks::ClockSource;
use hal::gpio::{FunctionPio0, PullNone};
use hal::Clock;
use hal::Timer;

use tufty::DummyPin;

// A few traits required for using the CountDown timer
use embedded_graphics::draw_target::DrawTarget;
use embedded_graphics::geometry::Point;
use embedded_graphics::pixelcolor::{Rgb565, RgbColor};
use embedded_graphics::primitives::{Circle, Primitive, PrimitiveStyleBuilder};
use embedded_graphics::Drawable;
use embedded_hal::timer::CountDown;
use fugit::ExtU32;
use st7789::ST7789;

#[entry]
fn main() -> ! {
// Grab our singleton objects
let mut pac = pac::Peripherals::take().unwrap();
let cp = pac::CorePeripherals::take().unwrap();

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

// Configure the clocks
//
// The default is to generate a 125 MHz system clock
let clocks = hal::clocks::init_clocks_and_plls(
tufty::XOSC_CRYSTAL_FREQ,
pac.XOSC,
pac.CLOCKS,
pac.PLL_SYS,
pac.PLL_USB,
&mut pac.RESETS,
&mut watchdog,
)
.ok()
.unwrap();

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

// Set the pins up according to their function on this particular board
let pins = tufty::Pins::new(
pac.IO_BANK0,
pac.PADS_BANK0,
sio.gpio_bank0,
&mut pac.RESETS,
);

// Configure the timer peripheral to be a CountDown timer for our blinky delay
let timer = Timer::new(pac.TIMER, &mut pac.RESETS, &clocks);
let mut delay_timer = timer.count_down();

let mut delay = cortex_m::delay::Delay::new(cp.SYST, clocks.system_clock.get_freq().to_Hz());

// Set the LED to be an output
let mut led_pin = pins.led.into_push_pull_output();

pins.lcd_backlight
.into_push_pull_output_in_state(PinState::High);
pins.lcd_rd.into_push_pull_output_in_state(PinState::High);

let display_data = {
use hal::dma::DMAExt;
use hal::pio::PIOExt;

let dma = pac.DMA.split(&mut pac.RESETS);
let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);

let wr = pins.lcd_wr.reconfigure::<FunctionPio0, PullNone>();
let d0 = pins.lcd_db0.reconfigure::<FunctionPio0, PullNone>();
pins.lcd_db1.reconfigure::<FunctionPio0, PullNone>();
pins.lcd_db2.reconfigure::<FunctionPio0, PullNone>();
pins.lcd_db3.reconfigure::<FunctionPio0, PullNone>();
pins.lcd_db4.reconfigure::<FunctionPio0, PullNone>();
pins.lcd_db5.reconfigure::<FunctionPio0, PullNone>();
pins.lcd_db6.reconfigure::<FunctionPio0, PullNone>();
pins.lcd_db7.reconfigure::<FunctionPio0, PullNone>();

tufty::PioDataLines::new(
&mut pio,
clocks.system_clock.freq(),
wr.id(),
d0.id(),
sm0,
dma.ch0,
)
};

let display_interface = tufty::ParallelDisplayInterface::new(
pins.lcd_cs.into_push_pull_output_in_state(PinState::High),
pins.lcd_dc.into_push_pull_output_in_state(PinState::High),
display_data,
);

let mut display = ST7789::new(display_interface, DummyPin, 240, 320);
display.init(&mut delay).unwrap();
display.clear(Rgb565::BLUE).unwrap();

let style = PrimitiveStyleBuilder::default()
.fill_color(Rgb565::RED)
.build();
Circle::new(Point::new(50, 50), 10)
.into_styled(style)
.draw(&mut display)
.unwrap();

// Blink the LED at 1 Hz
loop {
// LED on, and wait for 500ms
led_pin.set_high().unwrap();
delay_timer.start(500.millis());
let _ = nb::block!(delay_timer.wait());

// LED off, and wait for 500ms
led_pin.set_low().unwrap();
delay_timer.start(500.millis());
let _ = nb::block!(delay_timer.wait());
}
}
Loading

0 comments on commit 6eb85ab

Please sign in to comment.