144 lines
4.1 KiB
Rust
144 lines
4.1 KiB
Rust
#![deny(unsafe_code)]
|
|
#![no_std]
|
|
#![no_main]
|
|
|
|
extern crate panic_semihosting;
|
|
|
|
use cortex_m_rt::entry;
|
|
use embedded_graphics::image::Image;
|
|
use embedded_graphics::pixelcolor::Rgb565;
|
|
use embedded_graphics::prelude::*;
|
|
use embedded_graphics::primitives::rectangle::Rectangle;
|
|
use embedded_graphics::style::PrimitiveStyleBuilder;
|
|
use embedded_hal::digital::v2::OutputPin;
|
|
use rtt_target::{rprintln, rtt_init_print};
|
|
use st7735_lcd::Orientation;
|
|
use stm32f1xx_hal::{
|
|
delay::Delay,
|
|
pac,
|
|
prelude::*,
|
|
spi::{Mode, Phase, Polarity, Spi},
|
|
};
|
|
|
|
use tinybmp::Bmp;
|
|
|
|
#[entry]
|
|
fn main() -> ! {
|
|
rtt_init_print!();
|
|
|
|
// Get access to the core peripherals from the cortex-m crate
|
|
let cp = cortex_m::Peripherals::take().unwrap();
|
|
// Get access to the device specific peripherals from the peripheral access crate
|
|
let dp = pac::Peripherals::take().unwrap();
|
|
|
|
// Take ownership over the raw flash and rcc devices and convert them into the corresponding
|
|
// HAL structs
|
|
let mut flash = dp.FLASH.constrain();
|
|
|
|
let mut rcc = dp.RCC.constrain();
|
|
|
|
// Freeze the configuration of all the clocks in the system and store the frozen frequencies in
|
|
// `clocks`
|
|
let clocks = rcc
|
|
.cfgr
|
|
.use_hse(8.mhz())
|
|
.sysclk(72.mhz())
|
|
.pclk1(36.mhz())
|
|
.freeze(&mut flash.acr);
|
|
|
|
// Acquire the GPIOC peripheral
|
|
let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
|
|
|
|
// Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the function
|
|
// in order to configure the port. For pins 0-7, crl should be passed instead.
|
|
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
|
|
|
|
let mut delay = Delay::new(cp.SYST, clocks);
|
|
|
|
let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
|
|
|
|
let mut gpioa = dp.GPIOA.split(&mut rcc.apb2);
|
|
|
|
// SPI1
|
|
let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
|
|
let miso = gpioa.pa6;
|
|
let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
|
|
|
|
let disp_cs = gpioa.pa0.into_push_pull_output(&mut gpioa.crl);
|
|
|
|
let rst = gpioa.pa1.into_push_pull_output(&mut gpioa.crl);
|
|
let dc = gpioa.pa4.into_push_pull_output(&mut gpioa.crl);
|
|
let mut disp_led = gpioa.pa8.into_push_pull_output(&mut gpioa.crh);
|
|
|
|
disp_led.set_high();
|
|
|
|
let spi = Spi::spi1(
|
|
dp.SPI1,
|
|
(sck, miso, mosi),
|
|
&mut afio.mapr,
|
|
Mode {
|
|
polarity: Polarity::IdleLow,
|
|
phase: Phase::CaptureOnFirstTransition,
|
|
},
|
|
16.mhz(),
|
|
clocks,
|
|
&mut rcc.apb2,
|
|
);
|
|
|
|
let mut disp = st7735_lcd::ST7735::new(spi, dc, rst, true, false, 160, 128);
|
|
|
|
disp.hard_reset(&mut delay).unwrap();
|
|
disp.init(&mut delay).unwrap();
|
|
disp.set_orientation(&Orientation::LandscapeSwapped)
|
|
.unwrap();
|
|
|
|
let style_black = PrimitiveStyleBuilder::new()
|
|
.fill_color(Rgb565::BLACK)
|
|
.build();
|
|
Rectangle::new(Point::new(0, 0), Point::new(160, 128))
|
|
.into_styled(style_black)
|
|
.draw(&mut disp)
|
|
.unwrap();
|
|
|
|
/*
|
|
let style_red = PrimitiveStyleBuilder::new().fill_color(Rgb565::RED).build();
|
|
let style_green = PrimitiveStyleBuilder::new()
|
|
.fill_color(Rgb565::GREEN)
|
|
.build();
|
|
let style_blue = PrimitiveStyleBuilder::new()
|
|
.fill_color(Rgb565::BLUE)
|
|
.build();
|
|
Rectangle::new(Point::new(0, 0), Point::new(10, 20))
|
|
.into_styled(style_red)
|
|
.draw(&mut disp)
|
|
.unwrap();
|
|
|
|
disp.set_pixel(10, 20, 0x07E0);
|
|
|
|
|
|
Rectangle::new(Point::new(55, 0), Point::new(105, 128))
|
|
.into_styled(style_green)
|
|
.draw(&mut disp)
|
|
.unwrap();
|
|
|
|
Rectangle::new(Point::new(110, 0), Point::new(159, 128))
|
|
.into_styled(style_blue)
|
|
.draw(&mut disp)
|
|
.unwrap();
|
|
*/
|
|
|
|
let bmp = Bmp::from_slice(include_bytes!("logo.bmp")).unwrap();
|
|
let image = Image::new(&bmp, Point::new(16, 0));
|
|
image.draw(&mut disp).unwrap();
|
|
|
|
// Blink using the delay function
|
|
loop {
|
|
rprintln!("blink");
|
|
led.set_high().unwrap();
|
|
delay.delay_ms(1000u16);
|
|
rprintln!("blonk");
|
|
led.set_low().unwrap();
|
|
delay.delay_ms(1000u16);
|
|
}
|
|
}
|