From 612c44db46b0e8bc754b9f9191ebbce772c92efa Mon Sep 17 00:00:00 2001 From: Andrew Christiansen Date: Sun, 7 Nov 2021 14:19:35 -0800 Subject: [PATCH] Adafruit ItsyBitsy RP2040 Board Support Package (#192) * Created an itsy bitsy rp2040 board support package adapted from the bsp of the qt py rp2040. Special thanks to ithinuel for help in matrix * Changed the board description so it doesn't match the qt py rp2040 * Changed blinky.rs to itsy_bitsy_blinky.rs and ran changes through cargo fmt --- Cargo.toml | 1 + boards/itsy_bitsy_rp2040/Cargo.toml | 29 +++++ boards/itsy_bitsy_rp2040/README.md | 94 ++++++++++++++++ .../examples/itsy_bitsy_blinky.rs | 94 ++++++++++++++++ .../examples/itsy_bitsy_rainbow.rs | 102 ++++++++++++++++++ boards/itsy_bitsy_rp2040/src/lib.rs | 61 +++++++++++ 6 files changed, 381 insertions(+) create mode 100644 boards/itsy_bitsy_rp2040/Cargo.toml create mode 100644 boards/itsy_bitsy_rp2040/README.md create mode 100644 boards/itsy_bitsy_rp2040/examples/itsy_bitsy_blinky.rs create mode 100644 boards/itsy_bitsy_rp2040/examples/itsy_bitsy_rainbow.rs create mode 100644 boards/itsy_bitsy_rp2040/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index d207a18..be240cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ members = [ "rp2040-hal", "boards/feather_rp2040", "boards/qt_py_rp2040", + "boards/itsy_bitsy_rp2040", "boards/pico", "boards/pico_explorer", "boards/pico_lipo_16mb", diff --git a/boards/itsy_bitsy_rp2040/Cargo.toml b/boards/itsy_bitsy_rp2040/Cargo.toml new file mode 100644 index 0000000..7602bde --- /dev/null +++ b/boards/itsy_bitsy_rp2040/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "itsy_bitsy_rp2040" +version = "0.1.0" +authors = ["Andrew Christiansen "] +edition = "2018" +homepage = "https://github.com/rp-rs/rp-hal/boards/itsy_bitsy_rp2040" +description = "Board Support Package for the Adafruit ItsyBitsy RP2040" +license = "MIT OR Apache-2.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +cortex-m = "0.7.2" +rp2040-hal = { path = "../../rp2040-hal", version = "0.3.0"} +cortex-m-rt = { version = "0.7", optional = true } +embedded-time = "0.12.0" + +[dev-dependencies] +panic-halt= "0.2.0" +embedded-hal ="0.2.5" +rp2040-boot2 = "0.2" +smart-leds = "0.3" +nb = "1.0.0" +pio = { git = "https://github.com/rp-rs/pio-rs.git", branch = "main" } +ws2812-pio = { git = "https://github.com/ithinuel/ws2812-pio-rs" } + +[features] +default = ["rt"] +rt = ["cortex-m-rt","rp2040-hal/rt"] diff --git a/boards/itsy_bitsy_rp2040/README.md b/boards/itsy_bitsy_rp2040/README.md new file mode 100644 index 0000000..f640609 --- /dev/null +++ b/boards/itsy_bitsy_rp2040/README.md @@ -0,0 +1,94 @@ +# [itsy_bitsy_rp2040] - Board Support for the [Adafruit ItsyBitsy RP2040] + +You should include this crate if you are writing code that you want to run on +an [Adafruit ItsyBitsy RP2040] - an RP2040 board in the Itsy 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 ItsyBitsy RP2040. + +[Adafruit ItsyBitsy RP2040]: https://www.adafruit.com/product/4888 +[itsy_bitsy_rp2040]: https://github.com/rp-rs/rp-hal/tree/main/boards/itsy_bitsy_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 +itsy_bitsy_rp2040 = { git = "https://github.com/rp-rs/rp-hal.git" } +``` + +In your program, you will need to call `itsy_bitsy_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_ repository and run: + +```console +rp-hal/boards/itsy_bitsy_rp2040 $ cargo build --release --example +``` + +You will get an ELF file called +`./target/thumbv6m-none-eabi/release/examples/`, where the `target` +folder is located at the top of the _rp-hal_ 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/itsy_bitsy_rp2040 $ cargo run --release --example +``` + +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. +``` + +### [itsy_bitsy_rainbow](./examples/itsy_bitsy_rainbow.rs) + +Continuously changes the color of the ItsyBitsy'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/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. diff --git a/boards/itsy_bitsy_rp2040/examples/itsy_bitsy_blinky.rs b/boards/itsy_bitsy_rp2040/examples/itsy_bitsy_blinky.rs new file mode 100644 index 0000000..250eb8e --- /dev/null +++ b/boards/itsy_bitsy_rp2040/examples/itsy_bitsy_blinky.rs @@ -0,0 +1,94 @@ +//! # GPIO 'Blinky' Example +//! +//! This application demonstrates how to control a GPIO pin on the RP2040. +//! +//! It may need to be adapted to your particular board layout and/or pin assignment. +//! +//! See the `Cargo.toml` file for Copyright and licence details. + +#![no_std] +#![no_main] + +// The macro for our start-up function +use cortex_m_rt::entry; + +// Ensure we halt the program on panic (if we don't mention this crate it won't +// be linked) +use panic_halt as _; + +// Alias for our HAL crate +use rp2040_hal as hal; + +// A shorter alias for the Peripheral Access Crate, which provides low-level +// register access +use hal::pac; + +// Some traits we need +use embedded_hal::digital::v2::OutputPin; +use embedded_time::fixed_point::FixedPoint; +use rp2040_hal::clocks::Clock; + +/// The linker will place this boot block at the start of our program image. We +// need this to help the ROM bootloader get our code up and running. +#[link_section = ".boot2"] +#[used] +pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080; + +/// External high-speed crystal on the Raspberry Pi Pico board is 12 MHz. Adjust +/// if your board has a different frequency +const XTAL_FREQ_HZ: u32 = 12_000_000u32; + +/// 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 = hal::watchdog::Watchdog::new(pac.WATCHDOG); + + // Configure the clocks + let clocks = hal::clocks::init_clocks_and_plls( + XTAL_FREQ_HZ, + pac.XOSC, + pac.CLOCKS, + pac.PLL_SYS, + pac.PLL_USB, + &mut pac.RESETS, + &mut watchdog, + ) + .ok() + .unwrap(); + + let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().integer()); + + // The single-cycle I/O block controls our GPIO pins + let sio = hal::sio::Sio::new(pac.SIO); + + // Set the pins to their default state + let pins = hal::gpio::Pins::new( + pac.IO_BANK0, + pac.PADS_BANK0, + sio.gpio_bank0, + &mut pac.RESETS, + ); + + // Configure GPIO25 as an output + let mut led_pin = pins.gpio11.into_push_pull_output(); + loop { + led_pin.set_high().unwrap(); + // TODO: Replace with proper 1s delays once we have clocks working + delay.delay_ms(500); + led_pin.set_low().unwrap(); + delay.delay_ms(500); + } +} + +// End of file diff --git a/boards/itsy_bitsy_rp2040/examples/itsy_bitsy_rainbow.rs b/boards/itsy_bitsy_rp2040/examples/itsy_bitsy_rainbow.rs new file mode 100644 index 0000000..5dbe3db --- /dev/null +++ b/boards/itsy_bitsy_rp2040/examples/itsy_bitsy_rainbow.rs @@ -0,0 +1,102 @@ +//! Continuously changes the color of the Neopixel on a Adafruit ItsyBitsy RP2040 board +#![no_std] +#![no_main] + +use core::iter::once; +use cortex_m_rt::entry; +use embedded_hal::digital::v2::OutputPin; +use embedded_hal::timer::CountDown; +use embedded_time::duration::Extensions; +use panic_halt as _; +use rp2040_hal::pio::PIOExt; +use smart_leds::{brightness, SmartLedsWrite, RGB8}; +use ws2812_pio::Ws2812; + +use itsy_bitsy_rp2040::{ + hal::{ + clocks::{init_clocks_and_plls, Clock}, + gpio::{FunctionPio0, Pin}, + pac, + sio::Sio, + watchdog::Watchdog, + }, + Pins, XOSC_CRYSTAL_FREQ, +}; + +#[link_section = ".boot2"] +#[used] +pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080; + +#[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: Pin<_, FunctionPio0> = pins.neopixel_data.into_mode(); + + pins.neopixel_power + .into_push_pull_output() + .set_high() + .unwrap(); + + let timer = rp2040_hal::timer::Timer::new(pac.TIMER, &mut pac.RESETS); + let mut delay = timer.count_down(); + + let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS); + let mut ws = Ws2812::new( + 17, + &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.milliseconds()); + 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() + } +} diff --git a/boards/itsy_bitsy_rp2040/src/lib.rs b/boards/itsy_bitsy_rp2040/src/lib.rs new file mode 100644 index 0000000..79a4680 --- /dev/null +++ b/boards/itsy_bitsy_rp2040/src/lib.rs @@ -0,0 +1,61 @@ +#![no_std] + +pub extern crate rp2040_hal as hal; + +#[cfg(feature = "rt")] +extern crate cortex_m_rt; +#[cfg(feature = "rt")] +pub use cortex_m_rt::entry; + +pub use hal::pac; + +hal::bsp_pins!( + Gpio0 { + name: tx, + aliases: { FunctionUart: UartTx } + }, + Gpio1 { + name: rx, + aliases: { FunctionUart: UartRx } + }, + Gpio2 { + name: sda, + aliases: { FunctionI2C: Sda } + }, + Gpio3 { + name: scl, + aliases: { FunctionI2C: Scl } + }, + Gpio4 { name: d4 }, + Gpio5 { name: d3 }, + Gpio6 { name: d7 }, + Gpio7 { name: d9 }, + Gpio8 { name: d10 }, + Gpio9 { name: d11 }, + Gpio10 { name: d12 }, + Gpio11 { name: d13 }, + Gpio12 { name: d2 }, + Gpio14 { name: d5 }, + Gpio16 { name: neopixel_power }, + Gpio17 { name: neopixel_data }, + Gpio18 { + name: sclk, + aliases: { FunctionSpi: Sclk } + }, + Gpio19 { + name: mosi, + aliases: { FunctionSpi: Mosi } + }, + Gpio20 { + name: miso + aliases: { FunctionSpi: Miso } + }, + Gpio24 { name: d24 }, + Gpio25 { name: d25 }, + Gpio26 { name: a0 }, + Gpio27 { name: a1 }, + Gpio28 { name: a2 }, + Gpio29 { name: a3 }, +); + +pub const XOSC_CRYSTAL_FREQ: u32 = 12_000_000;