|
| 1 | +//! Send random raw data to the display, emulating an old untuned TV. This example `release()`s the |
| 2 | +//! underlying display properties struct and allows calling of the low-level `draw()` method, |
| 3 | +//! sending a 1024 byte buffer straight to the display. |
| 4 | +//! |
| 5 | +//! This example is for the STM32F103 "Blue Pill" board using I2C1. |
| 6 | +//! |
| 7 | +//! Wiring connections are as follows for a CRIUS-branded display: |
| 8 | +//! |
| 9 | +//! ``` |
| 10 | +//! Display -> Blue Pill |
| 11 | +//! (black) GND -> GND |
| 12 | +//! (red) +5V -> VCC |
| 13 | +//! (yellow) SDA -> PB9 |
| 14 | +//! (green) SCL -> PB8 |
| 15 | +//! ``` |
| 16 | +//! |
| 17 | +//! Run on a Blue Pill with `cargo run --example noise_i2c`. Best results when using `--release`. |
| 18 | +
|
| 19 | +#![no_std] |
| 20 | +#![no_main] |
| 21 | + |
| 22 | +extern crate cortex_m; |
| 23 | +extern crate cortex_m_rt as rt; |
| 24 | +extern crate panic_semihosting; |
| 25 | +extern crate stm32f1xx_hal as hal; |
| 26 | + |
| 27 | +use cortex_m_rt::{entry, exception, ExceptionFrame}; |
| 28 | +use embedded_graphics::{ |
| 29 | + pixelcolor::BinaryColor, |
| 30 | + prelude::*, |
| 31 | + primitives::{Circle, Rectangle, Triangle}, |
| 32 | + style::PrimitiveStyleBuilder, |
| 33 | +}; |
| 34 | +use hal::{ |
| 35 | + i2c::{BlockingI2c, DutyCycle, Mode}, |
| 36 | + prelude::*, |
| 37 | + stm32, |
| 38 | +}; |
| 39 | +use rand::prelude::*; |
| 40 | +use ssd1306::{mode::displaymode::DisplayModeTrait, prelude::*, Builder}; |
| 41 | + |
| 42 | +#[entry] |
| 43 | +fn main() -> ! { |
| 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 gpiob = dp.GPIOB.split(&mut rcc.apb2); |
| 54 | + |
| 55 | + let scl = gpiob.pb8.into_alternate_open_drain(&mut gpiob.crh); |
| 56 | + let sda = gpiob.pb9.into_alternate_open_drain(&mut gpiob.crh); |
| 57 | + |
| 58 | + let i2c = BlockingI2c::i2c1( |
| 59 | + dp.I2C1, |
| 60 | + (scl, sda), |
| 61 | + &mut afio.mapr, |
| 62 | + Mode::Fast { |
| 63 | + frequency: 400_000.hz(), |
| 64 | + duty_cycle: DutyCycle::Ratio2to1, |
| 65 | + }, |
| 66 | + clocks, |
| 67 | + &mut rcc.apb1, |
| 68 | + 1000, |
| 69 | + 10, |
| 70 | + 1000, |
| 71 | + 1000, |
| 72 | + ); |
| 73 | + |
| 74 | + let mut disp: GraphicsMode<_> = Builder::new().connect_i2c(i2c).into(); |
| 75 | + |
| 76 | + disp.init().unwrap(); |
| 77 | + |
| 78 | + let mut props = disp.release(); |
| 79 | + |
| 80 | + let mut buf = [0x00u8; 1024]; |
| 81 | + |
| 82 | + let mut rng = SmallRng::seed_from_u64(0xdead_beef_cafe_d00d); |
| 83 | + |
| 84 | + loop { |
| 85 | + rng.fill_bytes(&mut buf); |
| 86 | + |
| 87 | + props.draw(&buf); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +#[exception] |
| 92 | +fn HardFault(ef: &ExceptionFrame) -> ! { |
| 93 | + panic!("{:#?}", ef); |
| 94 | +} |
0 commit comments