Created
November 24, 2021 12:50
-
-
Save 9names/476e20e055b9fc9a5fdb523068a19290 to your computer and use it in GitHub Desktop.
Use the Pimoroni Pico display pack from Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! Show ferris on the Pimoroni Pico Display pack | |
#![no_std] | |
#![no_main] | |
use cortex_m_rt::entry; | |
use defmt::*; | |
use defmt_rtt as _; | |
use embedded_hal::digital::v2::OutputPin; | |
use embedded_time::{fixed_point::FixedPoint, rate::Extensions}; | |
use panic_probe as _; | |
use rp2040_hal as hal; | |
use hal::{ | |
clocks::{init_clocks_and_plls, Clock}, | |
pac, | |
sio::Sio, | |
watchdog::Watchdog, | |
}; | |
use st7789::{Orientation, ST7789}; | |
use display_interface_spi::SPIInterface; | |
use embedded_graphics::pixelcolor::Rgb565; | |
use embedded_graphics::{image::*, prelude::*}; | |
#[link_section = ".boot2"] | |
#[used] | |
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080; | |
#[entry] | |
fn main() -> ! { | |
info!("Program start"); | |
let mut pac = pac::Peripherals::take().unwrap(); | |
let core = pac::CorePeripherals::take().unwrap(); | |
let mut watchdog = Watchdog::new(pac.WATCHDOG); | |
let sio = Sio::new(pac.SIO); | |
// External high-speed crystal on the pico board is 12Mhz | |
let external_xtal_freq_hz = 12_000_000u32; | |
let clocks = init_clocks_and_plls( | |
external_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()); | |
let pins = hal::gpio::Pins::new( | |
pac.IO_BANK0, | |
pac.PADS_BANK0, | |
sio.gpio_bank0, | |
&mut pac.RESETS, | |
); | |
let mut led_pin = pins.gpio25.into_push_pull_output(); | |
// These are implicitly used by the spi driver if they are in the correct mode | |
let _spi_sclk = pins.gpio18.into_mode::<hal::gpio::FunctionSpi>(); | |
let _spi_mosi = pins.gpio19.into_mode::<hal::gpio::FunctionSpi>(); | |
let spi = hal::spi::Spi::<_, _, 8>::new(pac.SPI0); | |
// Apparently we're using the miso pin for data/command | |
// let _spi_miso = pins.gpio16.into_mode::<hal::gpio::FunctionSpi>(); | |
let dc = pins.gpio16.into_push_pull_output(); | |
let cs = pins.gpio17.into_push_pull_output(); | |
let bl = pins.gpio21.into_push_pull_output(); | |
let spi = spi.init( | |
&mut pac.RESETS, | |
clocks.peripheral_clock.freq(), | |
16_000_000u32.Hz(), | |
&embedded_hal::spi::MODE_3, | |
); | |
// create driver | |
let di = SPIInterface::new(spi, dc, cs); | |
let mut display = ST7789::new(di, None, Some(bl), 240, 135); | |
// initialize | |
display.init(&mut delay).unwrap(); | |
// set default orientation | |
display.set_orientation(Orientation::Landscape).unwrap(); | |
let raw_image_data = ImageRawLE::new(include_bytes!("../assets/ferris.raw"), 86); | |
let ferris = Image::new(&raw_image_data, Point::new(60, 60)); | |
// draw image on black background | |
display.clear(Rgb565::BLACK).unwrap(); | |
ferris.draw(&mut display).unwrap(); | |
loop { | |
info!("on!"); | |
led_pin.set_high().unwrap(); | |
delay.delay_ms(500); | |
info!("off!"); | |
led_pin.set_low().unwrap(); | |
delay.delay_ms(500); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment