|
| 1 | +#![no_main] |
| 2 | +#![no_std] |
| 3 | + |
| 4 | +// Required |
| 5 | +extern crate panic_semihosting; |
| 6 | + |
| 7 | +use cortex_m_rt::entry; |
| 8 | +use embedded_graphics::{ |
| 9 | + mono_font::{ascii::FONT_6X9, MonoTextStyle}, |
| 10 | + pixelcolor::{Rgb565, RgbColor}, |
| 11 | + prelude::*, |
| 12 | + primitives::{Circle, PrimitiveStyle, Rectangle}, |
| 13 | + text::Text, |
| 14 | +}; |
| 15 | + |
| 16 | +use stm32f4xx_hal::{ |
| 17 | + ltdc::{BluePins, GreenPins, Layer, LtdcPins, PixelFormat, RedPins}, |
| 18 | + pac, |
| 19 | + prelude::*, |
| 20 | + rcc::Rcc, |
| 21 | +}; |
| 22 | + |
| 23 | +mod screen; |
| 24 | + |
| 25 | +// DIMENSIONS |
| 26 | +const WIDTH: u16 = 480; |
| 27 | +const HEIGHT: u16 = 272; |
| 28 | + |
| 29 | +// Graphics framebuffer |
| 30 | +const FB_GRAPHICS_SIZE: usize = (WIDTH as usize) * (HEIGHT as usize); |
| 31 | +static mut FB_LAYER1: [u16; FB_GRAPHICS_SIZE] = [0; FB_GRAPHICS_SIZE]; |
| 32 | + |
| 33 | +#[entry] |
| 34 | +fn main() -> ! { |
| 35 | + let perif = pac::Peripherals::take().unwrap(); |
| 36 | + let _cp = cortex_m::Peripherals::take().unwrap(); |
| 37 | + |
| 38 | + let rcc_hal: Rcc = perif.RCC.constrain(); |
| 39 | + |
| 40 | + // Set up pins |
| 41 | + let _gpioa = perif.GPIOA.split(); |
| 42 | + let _gpiob = perif.GPIOB.split(); |
| 43 | + let gpioe = perif.GPIOE.split(); |
| 44 | + let gpiog = perif.GPIOG.split(); |
| 45 | + let gpioh = perif.GPIOH.split(); |
| 46 | + let gpioi = perif.GPIOI.split(); |
| 47 | + let gpioj = perif.GPIOJ.split(); |
| 48 | + let gpiok = perif.GPIOK.split(); |
| 49 | + |
| 50 | + let pins = LtdcPins::new( |
| 51 | + RedPins::new( |
| 52 | + gpioi.pi15, gpioj.pj0, gpioj.pj1, gpioj.pj2, gpioj.pj3, gpioj.pj4, gpioj.pj5, gpioj.pj6, |
| 53 | + ), |
| 54 | + GreenPins::new( |
| 55 | + gpioj.pj7, gpioj.pj8, gpioj.pj9, gpioj.pj10, gpioj.pj11, gpiok.pk0, gpiok.pk1, |
| 56 | + gpiok.pk2, |
| 57 | + ), |
| 58 | + BluePins::new( |
| 59 | + gpioe.pe4, gpioj.pj13, gpioj.pj14, gpioj.pj15, gpiog.pg12, gpiok.pk4, gpiok.pk5, |
| 60 | + gpiok.pk6, |
| 61 | + ), |
| 62 | + gpioi.pi10, |
| 63 | + gpioi.pi9, |
| 64 | + gpiok.pk7, |
| 65 | + gpioi.pi14, |
| 66 | + ); |
| 67 | + |
| 68 | + // HSE osc out in High Z |
| 69 | + gpioh.ph1.into_floating_input(); |
| 70 | + let _clocks = rcc_hal |
| 71 | + .cfgr |
| 72 | + .use_hse(25.MHz()) |
| 73 | + .bypass_hse_oscillator() |
| 74 | + .sysclk(216.MHz()) |
| 75 | + .hclk(216.MHz()) |
| 76 | + .freeze(); |
| 77 | + |
| 78 | + // LCD enable: set it low first to avoid LCD bleed while setting up timings |
| 79 | + let mut disp_on = gpioi.pi12.into_push_pull_output(); |
| 80 | + disp_on.set_low(); |
| 81 | + |
| 82 | + // LCD backlight enable |
| 83 | + let mut backlight = gpiok.pk3.into_push_pull_output(); |
| 84 | + backlight.set_high(); |
| 85 | + |
| 86 | + let mut display = screen::Stm32F7DiscoDisplay::new(perif.LTDC, perif.DMA2D, pins); |
| 87 | + display |
| 88 | + .controller |
| 89 | + .config_layer(Layer::L1, unsafe { &mut FB_LAYER1 }, PixelFormat::RGB565); |
| 90 | + |
| 91 | + display.controller.enable_layer(Layer::L1); |
| 92 | + display.controller.reload(); |
| 93 | + |
| 94 | + let display = &mut display; |
| 95 | + |
| 96 | + // LCD enable: activate LCD ! |
| 97 | + disp_on.set_high(); |
| 98 | + |
| 99 | + Rectangle::new(Point::new(0, 0), Size::new(479, 271)) |
| 100 | + .into_styled(PrimitiveStyle::with_fill(Rgb565::new(0, 0b11110, 0b11011))) |
| 101 | + .draw(display) |
| 102 | + .ok(); |
| 103 | + |
| 104 | + let c1 = Circle::new(Point::new(20, 20), 2 * 8) |
| 105 | + .into_styled(PrimitiveStyle::with_fill(Rgb565::new(0, 63, 0))); |
| 106 | + |
| 107 | + let c2 = Circle::new(Point::new(25, 20), 2 * 8) |
| 108 | + .into_styled(PrimitiveStyle::with_fill(Rgb565::new(31, 0, 0))); |
| 109 | + |
| 110 | + let t = Text::new( |
| 111 | + "Hello Rust!", |
| 112 | + Point::new(100, 100), |
| 113 | + MonoTextStyle::new(&FONT_6X9, RgbColor::WHITE), |
| 114 | + ); |
| 115 | + |
| 116 | + c1.draw(display).ok(); |
| 117 | + c2.draw(display).ok(); |
| 118 | + t.draw(display).ok(); |
| 119 | + |
| 120 | + for i in 0..300 { |
| 121 | + Circle::new(Point::new(20 + i, 20), 2 * 8) |
| 122 | + .into_styled(PrimitiveStyle::with_fill(RgbColor::GREEN)) |
| 123 | + .draw(display) |
| 124 | + .ok(); |
| 125 | + } |
| 126 | + |
| 127 | + #[allow(clippy::empty_loop)] |
| 128 | + loop {} |
| 129 | +} |
0 commit comments