#![no_std]
#![no_main]
extern crate cortex_m;
extern crate cortex_m_rt as rt;
extern crate panic_semihosting;
extern crate stm32f1xx_hal as hal;
use cortex_m_rt::ExceptionFrame;
use cortex_m_rt::{entry, exception};
use embedded_graphics::prelude::*;
use embedded_graphics::primitives::{Circle, Line, Rect};
use hal::delay::Delay;
use hal::prelude::*;
use hal::spi::{Mode, Phase, Polarity, Spi};
use hal::stm32;
use ssd1306::prelude::*;
use ssd1306::Builder;
#[entry]
fn main() -> ! {
let cp = cortex_m::Peripherals::take().unwrap();
let dp = stm32::Peripherals::take().unwrap();
let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
let mut gpioa = dp.GPIOA.split(&mut rcc.apb2);
let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
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 mut delay = Delay::new(cp.SYST, clocks);
let mut rst = gpiob.pb0.into_push_pull_output(&mut gpiob.crl);
let dc = gpiob.pb1.into_push_pull_output(&mut gpiob.crl);
let spi = Spi::spi1(
dp.SPI1,
(sck, miso, mosi),
&mut afio.mapr,
Mode {
polarity: Polarity::IdleLow,
phase: Phase::CaptureOnFirstTransition,
},
8.mhz(),
clocks,
&mut rcc.apb2,
);
let mut disp: GraphicsMode<_> = Builder::new().connect_spi(spi, dc).into();
disp.reset(&mut rst, &mut delay);
disp.init().unwrap();
disp.flush().unwrap();
disp.draw(
Line::new(Coord::new(8, 16 + 16), Coord::new(8 + 16, 16 + 16))
.with_stroke(Some(1u8.into()))
.into_iter(),
);
disp.draw(
Line::new(Coord::new(8, 16 + 16), Coord::new(8 + 8, 16))
.with_stroke(Some(1u8.into()))
.into_iter(),
);
disp.draw(
Line::new(Coord::new(8 + 16, 16 + 16), Coord::new(8 + 8, 16))
.with_stroke(Some(1u8.into()))
.into_iter(),
);
disp.draw(
Rect::new(Coord::new(48, 16), Coord::new(48 + 16, 16 + 16))
.with_stroke(Some(1u8.into()))
.into_iter(),
);
disp.draw(
Circle::new(Coord::new(96, 16 + 8), 8)
.with_stroke(Some(1u8.into()))
.into_iter(),
);
disp.flush().unwrap();
loop {}
}
#[exception]
fn HardFault(ef: &ExceptionFrame) -> ! {
panic!("{:#?}", ef);
}