|
| 1 | +//! Draw a colourful Rust logo on an SSD1331 display over SPI |
| 2 | +//! |
| 3 | +//! Image was exported with The GIMP. Export as `*.bmp` and use the 16 bit "R5 G6 B5" option under |
| 4 | +//! "advanced options". |
| 5 | +//! |
| 6 | +//! This example is for the STM32F103 "Blue Pill" board using a 4 wire interface to the display on |
| 7 | +//! SPI1. |
| 8 | +//! |
| 9 | +//! Wiring connections are as follows |
| 10 | +//! |
| 11 | +//! ``` |
| 12 | +//! GND -> GND |
| 13 | +//! 3V3 -> VCC |
| 14 | +//! PA5 -> SCL |
| 15 | +//! PA7 -> SDA |
| 16 | +//! PB0 -> RST |
| 17 | +//! PB1 -> D/C |
| 18 | +//! ``` |
| 19 | +//! |
| 20 | +//! Run on a Blue Pill with `cargo run --example image`. |
| 21 | +
|
| 22 | +#![no_std] |
| 23 | +#![no_main] |
| 24 | + |
| 25 | +extern crate cortex_m; |
| 26 | +extern crate cortex_m_rt as rt; |
| 27 | +extern crate panic_semihosting; |
| 28 | +extern crate stm32f1xx_hal as hal; |
| 29 | + |
| 30 | +use cortex_m_rt::ExceptionFrame; |
| 31 | +use cortex_m_rt::{entry, exception}; |
| 32 | +use embedded_graphics::image::ImageBmp; |
| 33 | +use embedded_graphics::prelude::*; |
| 34 | +use hal::delay::Delay; |
| 35 | +use hal::prelude::*; |
| 36 | +use hal::spi::{Mode, Phase, Polarity, Spi}; |
| 37 | +use hal::stm32; |
| 38 | +use ssd1331::prelude::*; |
| 39 | +use ssd1331::Builder; |
| 40 | + |
| 41 | +#[entry] |
| 42 | +fn main() -> ! { |
| 43 | + let cp = cortex_m::Peripherals::take().unwrap(); |
| 44 | + let dp = stm32::Peripherals::take().unwrap(); |
| 45 | + |
| 46 | + let mut flash = dp.FLASH.constrain(); |
| 47 | + let mut rcc = dp.RCC.constrain(); |
| 48 | + |
| 49 | + let clocks = rcc.cfgr.freeze(&mut flash.acr); |
| 50 | + |
| 51 | + let mut afio = dp.AFIO.constrain(&mut rcc.apb2); |
| 52 | + |
| 53 | + let mut gpioa = dp.GPIOA.split(&mut rcc.apb2); |
| 54 | + let mut gpiob = dp.GPIOB.split(&mut rcc.apb2); |
| 55 | + |
| 56 | + // SPI1 |
| 57 | + let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl); |
| 58 | + let miso = gpioa.pa6; |
| 59 | + let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl); |
| 60 | + |
| 61 | + let mut delay = Delay::new(cp.SYST, clocks); |
| 62 | + |
| 63 | + let mut rst = gpiob.pb0.into_push_pull_output(&mut gpiob.crl); |
| 64 | + let dc = gpiob.pb1.into_push_pull_output(&mut gpiob.crl); |
| 65 | + |
| 66 | + let spi = Spi::spi1( |
| 67 | + dp.SPI1, |
| 68 | + (sck, miso, mosi), |
| 69 | + &mut afio.mapr, |
| 70 | + Mode { |
| 71 | + polarity: Polarity::IdleLow, |
| 72 | + phase: Phase::CaptureOnFirstTransition, |
| 73 | + }, |
| 74 | + 8.mhz(), |
| 75 | + clocks, |
| 76 | + &mut rcc.apb2, |
| 77 | + ); |
| 78 | + |
| 79 | + let mut disp: GraphicsMode<_> = Builder::new().connect_spi(spi, dc).into(); |
| 80 | + |
| 81 | + disp.reset(&mut rst, &mut delay); |
| 82 | + disp.init().unwrap(); |
| 83 | + disp.flush().unwrap(); |
| 84 | + |
| 85 | + let im = ImageBmp::new(include_bytes!("./rust-pride.bmp")).unwrap(); |
| 86 | + |
| 87 | + let moved = im.translate(Coord::new((96 - im.width() as i32) / 2, 0)); |
| 88 | + |
| 89 | + disp.draw(moved.into_iter()); |
| 90 | + |
| 91 | + disp.flush().unwrap(); |
| 92 | + |
| 93 | + loop {} |
| 94 | +} |
| 95 | + |
| 96 | +#[exception] |
| 97 | +fn HardFault(ef: &ExceptionFrame) -> ! { |
| 98 | + panic!("{:#?}", ef); |
| 99 | +} |
0 commit comments